Skip to content

Commit ea74fd3

Browse files
authored
Proxy API (#190)
* Proxy API Signed-off-by: Miroslav Kovar <[email protected]>
1 parent 9ab766e commit ea74fd3

File tree

6 files changed

+107
-5
lines changed

6 files changed

+107
-5
lines changed

vcxagency-node/config/localhost-ea.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ MYSQL_PASSWORD_SECRET=mysecretpassword
2020
MYSQL_DATABASE_APPLICATION=agency_application
2121
MYSQL_DATABASE_WALLET=agency_wallets
2222
MYSQL_DATABASE_WALLET_CONNECTION_LIMIT=50
23+
24+
PROXY_TARGET_URL=http://localhost:3001/api/didcomm

vcxagency-node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"express-validation": "^3.0.6",
6262
"express-winston": "^4.0.3",
6363
"fs-extra": "^4.0.2",
64+
"http-proxy-middleware": "^2.0.6",
6465
"joi": "^17.3.0",
6566
"lodash": "^4.17.21",
6667
"redis": "^3.0.2",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2020 ABSA Group Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict'
18+
19+
const { createProxyMiddleware } = require('http-proxy-middleware')
20+
const logger = require('../logging/logger-builder')(__filename)
21+
22+
module.exports = function (router, proxyPrefix, proxyTargetUrl) {
23+
const proxyMiddleware = createProxyMiddleware({
24+
target: proxyTargetUrl,
25+
changeOrigin: true,
26+
pathRewrite: {
27+
[`^${proxyPrefix}`]: ''
28+
}
29+
})
30+
31+
const logProxy = function (req, res, next) {
32+
logger.debug(`Proxying request to ${req.method} ${proxyTargetUrl}`)
33+
next()
34+
}
35+
36+
router.use('/', logProxy)
37+
router.use('/', proxyMiddleware)
38+
}

vcxagency-node/src/configuration/app-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ const OP_MODES = {
136136
..._walletValidationRules(),
137137
..._applicationValidationRules(),
138138
AGENCY_DID: Joi.string().required(),
139-
ECS_CONTAINER_METADATA_URI_V4: Joi.string().uri()
139+
ECS_CONTAINER_METADATA_URI_V4: Joi.string().uri(),
140+
PROXY_TARGET_URL: Joi.string().uri()
140141
}
141142
}
142143
}

vcxagency-node/src/execution/server.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const logger = require('../logging/logger-builder')(__filename)
2727
const apiAgency = require('../api/api-agency')
2828
const apiMessaging = require('../api/api-messaging')
2929
const apiHealth = require('../api/api-health')
30+
const apiProxy = require('../api/api-proxy')
3031
const {
3132
logRequestsWithBody,
3233
setReqId,
@@ -68,8 +69,20 @@ async function setupExpressApp (expressApp, application, appConfig) {
6869
expressApp.use(['/api/health', '/'], healthRouter)
6970
apiHealth(healthRouter)
7071

71-
logger.info('Setting up express Aries API.')
7272
const maxRequestSizeKb = appConfig.SERVER_MAX_REQUEST_SIZE_KB
73+
if (appConfig.PROXY_TARGET_URL) {
74+
const proxyPrefix = '/api/proxy'
75+
logger.info(`Requests to ${proxyPrefix} will be forwarded to ${appConfig.PROXY_TARGET_URL}`)
76+
const routerProxy = express.Router()
77+
routerProxy.use(bodyParser.raw({
78+
inflate: false,
79+
limit: `${maxRequestSizeKb}kb`
80+
}))
81+
expressApp.use(proxyPrefix, routerProxy)
82+
apiProxy(routerProxy, proxyPrefix, appConfig.PROXY_TARGET_URL)
83+
}
84+
85+
logger.info('Setting up express Aries API.')
7386
const expressAppAriesApi = express.Router()
7487
expressAppAriesApi.use(bodyParser.raw({
7588
inflate: true,

vcxagency-node/yarn.lock

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,13 @@
17421742
resolved "https://registry.yarnpkg.com/@types/hapi__joi/-/hapi__joi-16.0.12.tgz#fb9113f17cf5764d6b3586ae9817d1606cc7c90c"
17431743
integrity sha512-xJYifuz59jXdWY5JMS15uvA3ycS3nQYOGqoIIE0+fwQ0qI3/4CxBc6RHsOTp6wk9M0NWEdpcTl02lOQOKMifbQ==
17441744

1745+
"@types/http-proxy@^1.17.8":
1746+
version "1.17.9"
1747+
resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.9.tgz#7f0e7931343761efde1e2bf48c40f02f3f75705a"
1748+
integrity sha512-QsbSjA/fSk7xB+UXlCT3wHBy5ai9wOcNDWwZAtud+jXhwOM3l+EYZh8Lng4+/6n8uar0J7xILzqftJdJ/Wdfkw==
1749+
dependencies:
1750+
"@types/node" "*"
1751+
17451752
"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
17461753
version "2.0.4"
17471754
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz#8467d4b3c087805d63580480890791277ce35c44"
@@ -2956,7 +2963,7 @@ dotenv@^7.0.0:
29562963
integrity sha512-M3NhsLbV1i6HuGzBUH8vXrtxOk+tWmzWKDMbAVSUp3Zsjm7ywFeuwrUXhmhQyRK1q5B5GGy7hcXPbj3bnfZg2g==
29572964

29582965
"easy-indysdk@file:../easy-indysdk":
2959-
version "2.2.0"
2966+
version "2.3.0"
29602967
dependencies:
29612968
indy-sdk "1.15.0"
29622969
mysql "^2.18.1"
@@ -3305,6 +3312,11 @@ etag@~1.8.1:
33053312
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
33063313
integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==
33073314

3315+
eventemitter3@^4.0.0:
3316+
version "4.0.7"
3317+
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
3318+
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==
3319+
33083320
exec-sh@^0.3.2:
33093321
version "0.3.6"
33103322
resolved "https://registry.yarnpkg.com/exec-sh/-/exec-sh-0.3.6.tgz#ff264f9e325519a60cb5e273692943483cca63bc"
@@ -3659,6 +3671,11 @@ [email protected]:
36593671
resolved "https://registry.yarnpkg.com/fn.name/-/fn.name-1.1.0.tgz#26cad8017967aea8731bc42961d04a3d5988accc"
36603672
integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==
36613673

3674+
follow-redirects@^1.0.0:
3675+
version "1.15.2"
3676+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
3677+
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
3678+
36623679
follow-redirects@^1.14.0:
36633680
version "1.15.1"
36643681
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5"
@@ -4017,6 +4034,26 @@ http-proxy-agent@^4.0.1:
40174034
agent-base "6"
40184035
debug "4"
40194036

4037+
http-proxy-middleware@^2.0.6:
4038+
version "2.0.6"
4039+
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-2.0.6.tgz#e1a4dd6979572c7ab5a4e4b55095d1f32a74963f"
4040+
integrity sha512-ya/UeJ6HVBYxrgYotAZo1KvPWlgB48kUJLDePFeneHsVujFaW5WNj2NgWCAE//B1Dl02BIfYlpNgBy8Kf8Rjmw==
4041+
dependencies:
4042+
"@types/http-proxy" "^1.17.8"
4043+
http-proxy "^1.18.1"
4044+
is-glob "^4.0.1"
4045+
is-plain-obj "^3.0.0"
4046+
micromatch "^4.0.2"
4047+
4048+
http-proxy@^1.18.1:
4049+
version "1.18.1"
4050+
resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549"
4051+
integrity sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==
4052+
dependencies:
4053+
eventemitter3 "^4.0.0"
4054+
follow-redirects "^1.0.0"
4055+
requires-port "^1.0.0"
4056+
40204057
http-signature@~1.2.0:
40214058
version "1.2.0"
40224059
resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.2.0.tgz#9aecd925114772f3d95b65a60abb8f7c18fbace1"
@@ -4343,6 +4380,11 @@ is-number@^7.0.0:
43434380
resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b"
43444381
integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==
43454382

4383+
is-plain-obj@^3.0.0:
4384+
version "3.0.0"
4385+
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-3.0.0.tgz#af6f2ea14ac5a646183a5bbdb5baabbc156ad9d7"
4386+
integrity sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==
4387+
43464388
is-plain-object@^2.0.3, is-plain-object@^2.0.4:
43474389
version "2.0.4"
43484390
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677"
@@ -6631,6 +6673,11 @@ require-main-filename@^2.0.0:
66316673
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-2.0.0.tgz#d0b329ecc7cc0f61649f62215be69af54aa8989b"
66326674
integrity sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==
66336675

6676+
requires-port@^1.0.0:
6677+
version "1.0.0"
6678+
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
6679+
integrity sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==
6680+
66346681
resolve-cwd@^3.0.0:
66356682
version "3.0.0"
66366683
resolved "https://registry.yarnpkg.com/resolve-cwd/-/resolve-cwd-3.0.0.tgz#0f0075f1bb2544766cf73ba6a6e2adfebcb13f2d"
@@ -7674,10 +7721,10 @@ vary@~1.1.2:
76747721
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==
76757722

76767723
"vcxagency-client@file:../vcxagency-client":
7677-
version "2.2.0"
7724+
version "2.3.0"
76787725
dependencies:
76797726
axios "^0.21.1"
7680-
easy-indysdk "file:../../../../Library/Caches/Yarn/v6/npm-vcxagency-client-2.2.0-adf99288-1204-4e9d-b268-76a10845621d-1660129035798/node_modules/easy-indysdk"
7727+
easy-indysdk "file:../../../Library/Caches/Yarn/v6/npm-vcxagency-client-2.3.0-0f622624-e985-4fe6-a50c-1c8feaf6a0ca-1667897101854/node_modules/easy-indysdk"
76817728
query-string "^6.8.2"
76827729

76837730

0 commit comments

Comments
 (0)