azureProxy.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(servicePrin.client_id, servicePrin.client_secret, connInfo.tenant, handleResponse)
  40. } else {
  41. res.status(401).send(buildError('Azure API authentication error'))
  42. }
  43. })
  44. app.get('/proxy/*', (req, res) => {
  45. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
  46. const url = Buffer.from(req.url.substr('/proxy/'.length), 'base64').toString()
  47. const headers: any = {}
  48. forwardHeaders.forEach(headerName => {
  49. if (req.headers[headerName] != null) {
  50. headers[headerName] = req.headers[headerName]
  51. }
  52. })
  53. axios({ url, headers }).then(response => {
  54. res.send(response.data)
  55. }).catch(error => {
  56. if (error.response) {
  57. res.status(error.response.status).send(buildError(error.response.data.error.message))
  58. } else if (error.request) {
  59. console.log(error)
  60. res.status(500).send(buildError('No Response!'))
  61. } else {
  62. res.status(500).send(buildError('Error creating request!'))
  63. }
  64. })
  65. })
  66. }