Skip to content

Commit f504d06

Browse files
CCM-11171 Implement mTLS on proxy
* CCM-11171 Implement mTLS on proxy * Remove getting certs * CCM-11171: simplify the authorizer lambda * CCM-11171: auth lambda to use supplier-id header --------- Co-authored-by: mark.slowey1 <[email protected]>
1 parent 9b6853d commit f504d06

File tree

15 files changed

+49
-34
lines changed

15 files changed

+49
-34
lines changed

.github/actions/build-proxies/action.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ runs:
1919
run: npm ci
2020
shell: bash
2121

22+
2223
- name: Setup Proxy Name and target
2324
shell: bash
2425
run: |
@@ -28,10 +29,13 @@ runs:
2829
echo "INSTANCE=$PROXYGEN_API_NAME" >> $GITHUB_ENV
2930
echo "TARGET=https://suppliers.dev.nhsnotify.national.nhs.uk" >> $GITHUB_ENV
3031
echo "SANDBOX_TAG=latest" >> $GITHUB_ENV
32+
echo "MTLS_NAME=notify-supplier-mtls" >> $GITHUB_ENV
3133
else
3234
echo "TARGET=https://pr$PR_NUMBER.suppliers.dev.nhsnotify.national.nhs.uk" >> $GITHUB_ENV
3335
echo "INSTANCE=$PROXYGEN_API_NAME-PR-$PR_NUMBER" >> $GITHUB_ENV
3436
echo "SANDBOX_TAG=pr$PR_NUMBER" >> $GITHUB_ENV
37+
echo "MTLS_NAME=notify-supplier-mtls-pr$PR_NUMBER" >> $GITHUB_ENV
38+
3539
fi
3640
3741
@@ -50,22 +54,23 @@ runs:
5054
envsubst < ./.github/proxygen-settings.yaml > ${HOME}/.proxygen/settings.yaml
5155
envsubst < ./.github/proxygen-settings.yaml | cat
5256
57+
5358
- name: Build internal dev oas
5459
working-directory: .
5560
shell: bash
5661
run: |
5762
if [ -z $PR_NUMBER ]
5863
then
59-
make build-json-oas-spec APIM_ENV=dev
64+
make build-json-oas-spec APIM_ENV=internal-dev
6065
else
61-
make build-json-oas-spec APIM_ENV=dev-pr
66+
make build-json-oas-spec APIM_ENV=internal-dev-pr
6267
fi
6368
64-
- name: Set target
69+
- name: Set target and cert
6570
shell: bash
6671
run: |
6772
jq --arg newurl "$TARGET" '.["x-nhsd-apim"].target.url = $newurl' build/notify-supplier.json > build/notify-supplier_target.json && mv build/notify-supplier_target.json build/notify-supplier.json
68-
73+
jq --arg newmtls "$MTLS_NAME" '.["x-nhsd-apim"].target.security.secret = $newmtls' build/notify-supplier.json > build/notify-supplier_target.json && mv build/notify-supplier_target.json build/notify-supplier.json
6974
7075
- name: Deploy to Internal Dev
7176
shell: bash

.github/workflows/stage-3-build.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ on:
3636
required: false
3737
type: string
3838

39+
permissions:
40+
id-token: write # This is required for requesting the JWT
41+
contents: read # This is required for actions/checkout
3942
jobs:
4043
artefact-jekyll-docs:
4144
name: "Build Docs"

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,3 @@ dist
2525
.reports
2626
/sandbox/*.log
2727
/sandbox-staging
28-
/specification/components/x-nhsd-apim/access.yml
29-
/specification/components/x-nhsd-apim/target.yml
30-
/specification/components/security/security.yml

infrastructure/terraform/components/api/resources/spec.tmpl.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"securitySchemes": {
44
"LambdaAuthorizer": {
55
"in": "header",
6-
"name": "Authorization",
6+
"name": "NHSD-Supplier-ID",
77
"type": "apiKey",
88
"x-amazon-apigateway-authorizer": {
99
"authorizerCredentials": "${APIG_EXECUTION_ROLE_ARN}",
1010
"authorizerResultTtlInSeconds": 0,
1111
"authorizerUri": "arn:aws:apigateway:${AWS_REGION}:lambda:path/2015-03-31/functions/${AUTHORIZER_LAMBDA_ARN}/invocations",
12-
"identitySource": "method.request.header.Authorization",
12+
"identitySource": "method.request.header.NHSD-Supplier-ID",
1313
"type": "request"
1414
},
1515
"x-amazon-apigateway-authtype": "custom"

lambdas/authorizer/src/__tests__/index.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { APIGatewayRequestAuthorizerEvent, Callback, Context } from 'aws-lambda';
12
import { handler } from '../index';
2-
import { APIGatewayRequestAuthorizerEvent, Context, Callback } from 'aws-lambda';
33

44
describe('Authorizer Lambda Function', () => {
55
let mockEvent: APIGatewayRequestAuthorizerEvent;
@@ -81,4 +81,24 @@ describe('Authorizer Lambda Function', () => {
8181
}),
8282
}));
8383
});
84+
85+
it('Should handle additional headers correctly', () => {
86+
mockEvent.headers = {
87+
headerauth1: 'headervalue1' ,
88+
otherheader1: 'headervalue2',
89+
otherheader2: 'headervalue3'
90+
};
91+
92+
handler(mockEvent, mockContext, mockCallback);
93+
94+
expect(mockCallback).toHaveBeenCalledWith(null, expect.objectContaining({
95+
policyDocument: expect.objectContaining({
96+
Statement: expect.arrayContaining([
97+
expect.objectContaining({
98+
Effect: 'Allow',
99+
}),
100+
]),
101+
}),
102+
}));
103+
});
84104
});

lambdas/authorizer/src/index.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,28 @@
1111

1212
// See https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html for the original JS documentation
1313

14-
import { APIGatewayRequestAuthorizerEvent, Context, Callback, APIGatewayAuthorizerResult } from 'aws-lambda';
14+
import { APIGatewayAuthorizerResult, APIGatewayRequestAuthorizerEvent, Callback, Context } from 'aws-lambda';
15+
import pino from 'pino';
1516

1617
export const handler = (
1718
event: APIGatewayRequestAuthorizerEvent,
1819
context: Context,
19-
callback: Callback<APIGatewayAuthorizerResult>
20+
callback: Callback<APIGatewayAuthorizerResult>,
21+
log = pino()
2022
): void => {
21-
console.log('Received event:', JSON.stringify(event, null, 2));
23+
log.info(event, 'Received event');
2224

2325
const headers = event.headers || {};
24-
const tmp = event.methodArn.split(':');
25-
const apiGatewayArnTmp = tmp[5].split('/');
26-
const awsAccountId = tmp[4];
27-
const region = tmp[3];
28-
const restApiId = apiGatewayArnTmp[0];
29-
const stage = apiGatewayArnTmp[1];
30-
const method = apiGatewayArnTmp[2];
31-
let resource = '/'; // root resource
32-
33-
if (apiGatewayArnTmp[3]) {
34-
resource += apiGatewayArnTmp[3];
35-
}
3626

3727
// Perform authorization to return the Allow policy for correct parameters and
3828
// the 'Unauthorized' error, otherwise.
3929
if (
4030
headers['headerauth1'] === 'headervalue1'
4131
) {
32+
log.info('Allow event');
4233
callback(null, generateAllow('me', event.methodArn));
4334
} else {
35+
log.info('Deny event');
4436
callback(null, generateDeny('me', event.methodArn));
4537
}
4638
};

specification/api/components/security/security-dev-pr.yml renamed to specification/api/components/security/security-internal-dev-pr.yml

File renamed without changes.

specification/api/components/security/security-dev.yml renamed to specification/api/components/security/security-internal-dev.yml

File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
$ref: security-dev-pr.yml
1+
$ref: security-sandbox.yml

specification/api/components/x-nhsd-apim/access-dev-pr.yml renamed to specification/api/components/x-nhsd-apim/access-internal-dev-pr.yml

File renamed without changes.

0 commit comments

Comments
 (0)