azureProxy.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import express from "express";
  15. import MsRest from "ms-rest-azure";
  16. import axios from "axios";
  17. const forwardHeaders = ["authorization"];
  18. const buildError = (message: any) => ({
  19. error: { message: `Proxy - ${message}` },
  20. });
  21. export default (router: express.Router) => {
  22. router.post("/azure/login", (req, res) => {
  23. const handleResponse = (err: any, credentials: any) => {
  24. if (err) {
  25. console.log(err);
  26. res.status(401).send(buildError("Azure API authentication error"));
  27. } else {
  28. res.send(credentials);
  29. }
  30. };
  31. const connInfo = req.body;
  32. const userCred = connInfo.user_credentials;
  33. const servicePrin = connInfo.service_principal_credentials;
  34. if (userCred && userCred.username && userCred.password) {
  35. MsRest.loginWithUsernamePassword(
  36. userCred.username,
  37. userCred.password,
  38. handleResponse
  39. );
  40. } else if (
  41. servicePrin &&
  42. servicePrin.client_id &&
  43. servicePrin.client_secret
  44. ) {
  45. MsRest.loginWithServicePrincipalSecret(
  46. servicePrin.client_id,
  47. servicePrin.client_secret,
  48. connInfo.tenant,
  49. handleResponse
  50. );
  51. } else {
  52. res.status(401).send(buildError("Azure API authentication error"));
  53. }
  54. });
  55. router.get("/azure/*", (req, res) => {
  56. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  57. const url = Buffer.from(
  58. req.url.substr("/proxy/".length),
  59. "base64"
  60. ).toString();
  61. const headers: any = {};
  62. forwardHeaders.forEach(headerName => {
  63. if (req.headers[headerName] != null) {
  64. headers[headerName] = req.headers[headerName];
  65. }
  66. });
  67. axios({ url, headers })
  68. .then(response => {
  69. res.send(response.data);
  70. })
  71. .catch(error => {
  72. if (error.response) {
  73. res
  74. .status(error.response.status)
  75. .send(buildError(error.response.data.error.message));
  76. } else if (error.request) {
  77. console.log(error);
  78. res.status(500).send(buildError("No Response!"));
  79. } else {
  80. res.status(500).send(buildError("Error creating request!"));
  81. }
  82. });
  83. });
  84. };