Skip to content

Commit 2751314

Browse files
authored
Fix system proxy endpoint forwarding query params (#2245)
DEFRA/water-abstraction-team#54 https://eaflood.atlassian.net/browse/WATER-3787 In a previous change we [Create system proxy endpoint](#2229). We need this to be able to send requests to the [water-abstraction-system](https://github.com/DEFRA/water-abstraction-system) when using our AWS environments. That all seemed to work until we changed an endpoint in **water-abstraction-system** to require a query param to be passed through. Our proxy isn't doing this so now **water-abstraction-system** errors because of the missing param. This change fixes the issue by switching to [h2o2](https://github.com/hapijs/h2o2) which "adds proxying functionality to a hapi server." When we first implemented our proxy, we did it ourselves. The issue with the query params would require us to continue to expand that functionality, and we haven't even got to `POST`, `PUT` and `DELETE` requests yet! So, before we went down that route we investigated what options were available and found this plugin. The documentation and current examples all direct you to use this if you need Hapi to act like a reverse/pass-through proxy server. We should have done this from the outset, but we've at least stopped before we expanded our own functionality further.
1 parent 08d62f7 commit 2751314

File tree

7 files changed

+158
-288
lines changed

7 files changed

+158
-288
lines changed

package-lock.json

+119
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@hapi/boom": "^9.1.4",
3535
"@hapi/catbox-redis": "^6.0.2",
3636
"@hapi/good": "^9.0.1",
37+
"@hapi/h2o2": "^10.0.0",
3738
"@hapi/hapi": "^20.2.1",
3839
"@hapi/hoek": "^9.2.1",
3940
"@hapi/inert": "^6.0.5",

server-internal.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { createPlugins, createCache } = require('./server-common')
55

66
// -------------- Require vendor code -----------------
77
const Hapi = require('@hapi/hapi')
8+
const H2o2 = require('@hapi/h2o2')
89

910
// -------------- Require project code -----------------
1011
const config = require('./src/internal/config')
@@ -103,6 +104,9 @@ async function start () {
103104
// Auth plugin
104105
await server.register(authPlugin)
105106

107+
// Proxy plugin
108+
await server.register(H2o2)
109+
106110
server.route(routes)
107111

108112
await server.start()

src/internal/lib/connectors/services/water/SystemProxyService.js

-12
This file was deleted.

src/internal/modules/system-proxy/controller.js

-70
This file was deleted.

src/internal/modules/system-proxy/routes.js

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
'use strict'
22

3-
const controller = require('./controller')
3+
const config = require('../../config')
4+
5+
const systemUrl = new URL(config.services.system)
6+
const proxyDefaults = {
7+
timeout: 15000, // 15 seconds - default is 3 minutes
8+
// Forwards the headers from the client to the upstream service. This is needed for system to receive things like the
9+
// `accept:` header, which indicates what MIME type is requested.
10+
passThrough: true,
11+
// Sets the 'X-Forwarded-For', 'X-Forwarded-Port', 'X-Forwarded-Proto', 'X-Forwarded-Host' headers. 'X-Forwarded-For',
12+
// for example, is the standard header for identifying the originating IP address of a client connecting to a web
13+
// server through a proxy server. As such, it is often used in security and authentication checks.
14+
xforward: true
15+
}
416

517
const routes = [
618
{
719
method: 'GET',
20+
// This will match all path segments after /system. Note in our proxy URI we refer to this only as {tail}. The path
21+
// param hapi provides will contain all the segments, for example, request.params.tail === '/test/supplementary'.
22+
// We need to refer to it in the same way we access it on the request object.
823
path: '/system/{tail*}',
9-
handler: controller.getSystemProxy,
24+
handler: {
25+
proxy: {
26+
uri: `${systemUrl.protocol}//${systemUrl.hostname}:${systemUrl.port}/{tail}{query}`,
27+
...proxyDefaults
28+
}
29+
},
1030
config: {
1131
auth: false,
1232
description: 'Proxies requests to the Water Abstraction System'
@@ -15,7 +35,12 @@ const routes = [
1535
{
1636
method: 'GET',
1737
path: '/assets/all.js',
18-
handler: controller.getSystemJsProxy,
38+
handler: {
39+
proxy: {
40+
uri: `${systemUrl.protocol}//${systemUrl.hostname}:${systemUrl.port}/assets/all.js`,
41+
...proxyDefaults
42+
}
43+
},
1944
config: {
2045
auth: false,
2146
description: 'Proxies JS asset requests to the Water Abstraction System'
@@ -24,7 +49,12 @@ const routes = [
2449
{
2550
method: 'GET',
2651
path: '/assets/stylesheets/application.css',
27-
handler: controller.getSystemCssProxy,
52+
handler: {
53+
proxy: {
54+
uri: `${systemUrl.protocol}//${systemUrl.hostname}:${systemUrl.port}/assets/stylesheets/application.css`,
55+
...proxyDefaults
56+
}
57+
},
2858
config: {
2959
auth: false,
3060
description: 'Proxies CSS asset requests to the Water Abstraction System'

0 commit comments

Comments
 (0)