Skip to content

Commit 1ff3966

Browse files
marcotranchinojfhardenDomBelcher
committed
Use hpagent with axios for Apple Pay Merchant Validation with proxy
With this change, we have update the Apple Pay Merchant Validation implementation in order to use `hpagent` and `axios` in the presence of an egress proxy, as it's the case on our AWS environments. This is needed because we want to remove the use of `requestretry`, however `axios` has a problem preventing it from working with an egress proxy[1]. For this reason, we need to use an HttpsProxyAgent with it. We would want to use `https-proxy-agent`, however it has its own problem[2]. While we wait for these issues to be fixed, we can use `hpagent` which has been tested and works well with an egress proxy. Further information in the JIRA ticket[3]. [1] axios/axios#4531 [2] TooTallNate/proxy-agents#235 [3] https://payments-platform.atlassian.net/browse/PP-12853 Co-authored-by: Jonathan Harden <[email protected]> Co-authored-by: Dominic Belcher <[email protected]> Co-authored-by: Marco Tranchino <[email protected]>
1 parent 4628629 commit 1ff3966

File tree

5 files changed

+140
-133
lines changed

5 files changed

+140
-133
lines changed

.secrets.baseline

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
{
7676
"path": "detect_secrets.filters.allowlist.is_line_allowlisted"
7777
},
78+
{
79+
"path": "detect_secrets.filters.common.is_baseline_file",
80+
"filename": ".secrets.baseline"
81+
},
7882
{
7983
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
8084
"min_level": 2
@@ -114,7 +118,7 @@
114118
"filename": "app/controllers/web-payments/apple-pay/merchant-validation.controller.js",
115119
"hashed_secret": "1348b145fa1a555461c1b790a2f66614781091e9",
116120
"is_verified": false,
117-
"line_number": 20
121+
"line_number": 19
118122
}
119123
],
120124
"test/controllers/web-payments/apple-pay/normalise-apple-pay-payload.test.js": [
@@ -385,5 +389,5 @@
385389
}
386390
]
387391
},
388-
"generated_at": "2024-07-30T15:02:23Z"
392+
"generated_at": "2024-08-09T08:40:47Z"
389393
}

app/controllers/web-payments/apple-pay/merchant-validation.controller.js

+16-31
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const request = require('requestretry') // to be removed once axios is in use
44
const logger = require('../../../utils/logger')(__filename)
55
const { getLoggingFields } = require('../../../utils/logging-fields-helper')
66
const axios = require('axios')
7-
const https = require('https')
8-
const { HttpsProxyAgent } = require('https-proxy-agent')
7+
const { HttpsProxyAgent } = require('hpagent')
98
const proxyUrl = process.env.HTTPS_PROXY
109
const applePayMerchantValidationViaAxios = process.env.APPLE_PAY_MERCHANT_VALIDATION_VIA_AXIOS === 'true'
1110

@@ -55,10 +54,13 @@ module.exports = async (req, res) => {
5554
return res.sendStatus(400)
5655
}
5756

58-
const httpsAgent = new https.Agent({
59-
cert: merchantIdentityVars.cert,
60-
key: merchantIdentityVars.key
61-
});
57+
const httpsAgent = new HttpsProxyAgent({
58+
proxy: proxyUrl,
59+
cert: merchantIdentityVars.cert,
60+
key: merchantIdentityVars.key
61+
})
62+
63+
const axiosInstance = axios.create({ httpsAgent, proxy: false });
6264

6365
if (proxyUrl) {
6466
logger.info('Using proxy URL')
@@ -95,59 +97,42 @@ module.exports = async (req, res) => {
9597

9698
if (applePayMerchantValidationViaAxios) {
9799
if (proxyUrl) {
98-
logger.info('Generating Apple Pay session via axios and https proxy agent')
100+
logger.info('Generating Apple Pay session via axios and https proxy agent (hpagent)')
99101

100102
const data = {
101-
cert: merchantIdentityVars.cert,
102-
key: merchantIdentityVars.key,
103103
merchantIdentifier: merchantIdentityVars.merchantIdentifier,
104104
displayName: 'GOV.UK Pay',
105105
initiative: 'web',
106106
initiativeContext: process.env.APPLE_PAY_MERCHANT_DOMAIN
107107
}
108108

109-
110-
const httpsProxyAgent = new HttpsProxyAgent(proxyUrl, {
111-
cert: merchantIdentityVars.cert,
112-
key: merchantIdentityVars.key
113-
});
114-
115-
// const httpsAgent = new https.Agent({
116-
// proxy: httpsProxyAgent
117-
// });
118-
119-
const axiosInstance = axios.create({
120-
httpsAgent: httpsProxyAgent
121-
});
122-
123-
124109
try {
125110
const response = await axiosInstance.post(url, data, { headers: { 'Content-Type': 'application/json; charset=utf-8' } })
126111

127-
logger.info('Apple Pay session successfully generated via axios and https proxy agent')
112+
logger.info('Apple Pay session successfully generated via axios and https proxy agent (hpagent)')
128113
res.status(200).send(response.data)
129114
} catch (error) {
130-
logger.info('Error generating Apple Pay session', {
115+
logger.info('Error generating Apple Pay session with axios and https proxy agent (hpagent)', {
131116
...getLoggingFields(req),
132117
error: error.message,
133118
status: error.response ? error.response.status : 'No status'
134119
})
135-
logger.info('Apple Pay session via axios and https proxy agent failed', 'Apple Pay Error')
120+
logger.info('Apple Pay session via axios and https proxy agent (hpagent) failed', 'Apple Pay Error')
136121
res.status(500).send('Apple Pay Error')
137122
}
138123
} else {
139-
logger.info('Generating Apple Pay session via axios and https agent (local machine)')
124+
logger.info('Generating Apple Pay session via axios and https proxy agent (hpagent) (NO PROXY)')
140125
try {
141126
const response = await axios(options)
142127

143-
logger.info('Apple Pay session successfully generated via axios and https agent')
128+
logger.info('Apple Pay session successfully generated via axios and https proxy agent (hpagent) (NO PROXY)')
144129
res.status(200).send(response.data)
145130
} catch (error) {
146-
logger.info('Error generating Apple Pay session', {
131+
logger.info('Error generating Apple Pay session (NO PROXY)', {
147132
...getLoggingFields(req),
148133
error: error.message
149134
})
150-
logger.info('Apple Pay session via axios and https agent failed', 'Apple Pay Error')
135+
logger.info('Apple Pay session via axios and https proxy agent (hpagent) with no proxy failed', 'Apple Pay Error')
151136
res.status(500).send('Apple Pay Error')
152137
}
153138
}

package-lock.json

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"gaap-analytics": "^3.1.0",
8888
"govuk-frontend": "^4.8.0",
8989
"helmet": "^7.1.0",
90-
"https-proxy-agent": "^7.0.5",
90+
"hpagent": "^1.2.0",
9191
"i18n": "0.15.x",
9292
"lodash": "4.17.x",
9393
"mailcheck": "^1.1.1",

0 commit comments

Comments
 (0)