azureProxy.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 bodyParser from 'body-parser'
  17. import axios from 'axios'
  18. const forwardHeaders = ['authorization']
  19. const buildError = (message: any) => ({
  20. error: { message: `Proxy - ${message}` },
  21. })
  22. export default (app: express.Application) => {
  23. const jsonParser = bodyParser.json()
  24. app.post('/azure-login', jsonParser, (req, res) => {
  25. const handleResponse = (err: any, credentials: any) => {
  26. if (err) {
  27. console.log(err)
  28. res.status(401).send(buildError('Azure API authentication error'))
  29. } else {
  30. res.send(credentials)
  31. }
  32. }
  33. const connInfo = req.body
  34. const userCred = connInfo.user_credentials
  35. const servicePrin = connInfo.service_principal_credentials
  36. if (userCred && userCred.username && userCred.password) {
  37. MsRest.loginWithUsernamePassword(userCred.username, userCred.password, handleResponse)
  38. } else if (servicePrin && servicePrin.client_id && servicePrin.client_secret) {
  39. MsRest.loginWithServicePrincipalSecret(
  40. servicePrin.client_id, servicePrin.client_secret, connInfo.tenant, handleResponse,
  41. )
  42. } else {
  43. res.status(401).send(buildError('Azure API authentication error'))
  44. }
  45. })
  46. app.get('/proxy/*', (req, res) => {
  47. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
  48. const url = Buffer.from(req.url.substr('/proxy/'.length), 'base64').toString()
  49. const headers: any = {}
  50. forwardHeaders.forEach(headerName => {
  51. if (req.headers[headerName] != null) {
  52. headers[headerName] = req.headers[headerName]
  53. }
  54. })
  55. axios({ url, headers }).then(response => {
  56. res.send(response.data)
  57. }).catch(error => {
  58. if (error.response) {
  59. res.status(error.response.status).send(buildError(error.response.data.error.message))
  60. } else if (error.request) {
  61. console.log(error)
  62. res.status(500).send(buildError('No Response!'))
  63. } else {
  64. res.status(500).send(buildError('Error creating request!'))
  65. }
  66. })
  67. })
  68. }