|
| 1 | +import { handler } from './lambda'; |
| 2 | +import { Context } from 'aws-lambda'; |
| 3 | +import { APIGatewayRequestAuthorizerEventV2 } from 'aws-lambda/trigger/api-gateway-authorizer'; |
| 4 | + |
| 5 | +const context: Context = { |
| 6 | + awsRequestId: '1', |
| 7 | + callbackWaitsForEmptyEventLoop: false, |
| 8 | + functionName: '', |
| 9 | + functionVersion: '', |
| 10 | + getRemainingTimeInMillis: () => 0, |
| 11 | + invokedFunctionArn: '', |
| 12 | + logGroupName: '', |
| 13 | + logStreamName: '', |
| 14 | + memoryLimitInMB: '', |
| 15 | + done: () => { |
| 16 | + return; |
| 17 | + }, |
| 18 | + fail: () => { |
| 19 | + return; |
| 20 | + }, |
| 21 | + succeed: () => { |
| 22 | + return; |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +// Pretty much copy/paste from here: |
| 27 | +// https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html |
| 28 | +const event: APIGatewayRequestAuthorizerEventV2 = { |
| 29 | + version: '2.0', |
| 30 | + type: 'REQUEST', |
| 31 | + routeArn: 'arn:aws:execute-api:us-east-1:123456789012:abcdef123/test/GET/request', |
| 32 | + identitySource: ['user1', '123'], |
| 33 | + routeKey: '$default', |
| 34 | + rawPath: '/my/path', |
| 35 | + rawQueryString: 'parameter1=value1¶meter1=value2¶meter2=value', |
| 36 | + cookies: ['cookie1', 'cookie2'], |
| 37 | + headers: { |
| 38 | + header1: 'value1', |
| 39 | + header2: 'value2', |
| 40 | + }, |
| 41 | + queryStringParameters: { |
| 42 | + parameter1: 'value1,value2', |
| 43 | + parameter2: 'value', |
| 44 | + }, |
| 45 | + requestContext: { |
| 46 | + accountId: '123456789012', |
| 47 | + apiId: 'api-id', |
| 48 | + authentication: { |
| 49 | + clientCert: { |
| 50 | + clientCertPem: 'CERT_CONTENT', |
| 51 | + subjectDN: 'www.example.com', |
| 52 | + issuerDN: 'Example issuer', |
| 53 | + serialNumber: '1', |
| 54 | + validity: { |
| 55 | + notBefore: 'May 28 12:30:02 2019 GMT', |
| 56 | + notAfter: 'Aug 5 09:36:04 2021 GMT', |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + domainName: 'id.execute-api.us-east-1.amazonaws.com', |
| 61 | + domainPrefix: 'id', |
| 62 | + http: { |
| 63 | + method: 'POST', |
| 64 | + path: '/my/path', |
| 65 | + protocol: 'HTTP/1.1', |
| 66 | + sourceIp: '81.123.56.13', |
| 67 | + userAgent: 'agent', |
| 68 | + }, |
| 69 | + requestId: 'id', |
| 70 | + routeKey: '$default', |
| 71 | + stage: '$default', |
| 72 | + time: '12/Mar/2020:19:03:58 +0000', |
| 73 | + timeEpoch: 1583348638390, |
| 74 | + }, |
| 75 | + pathParameters: { parameter1: 'value1' }, |
| 76 | + stageVariables: { stageVariable1: 'value1', stageVariable2: 'value2' }, |
| 77 | +}; |
| 78 | + |
| 79 | +jest.mock('@aws-github-runner/aws-powertools-util'); |
| 80 | + |
| 81 | +describe('Webhook auth', () => { |
| 82 | + beforeAll(() => { |
| 83 | + jest.resetAllMocks(); |
| 84 | + }); |
| 85 | + it('should not pass if env var does not exist', async () => { |
| 86 | + const result = await handler(event, context); |
| 87 | + expect(result).toEqual({ isAuthorized: false }); |
| 88 | + }); |
| 89 | + it('should pass the IP allow list using exact ip', async () => { |
| 90 | + process.env.CIDR_IPV4_ALLOW_LIST = '81.123.56.13/32,81.123.56.52/32,10.0.0.0/8'; |
| 91 | + const result = await handler(event, context); |
| 92 | + expect(result).toEqual({ isAuthorized: true }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should not pass the IP allow list.', async () => { |
| 96 | + process.env.CIDR_IPV4_ALLOW_LIST = '81.123.56.52/32,10.0.0.0/8'; |
| 97 | + const result = await handler(event, context); |
| 98 | + expect(result).toEqual({ isAuthorized: false }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should pass the IP allow list using CIDR range', async () => { |
| 102 | + process.env.CIDR_IPV4_ALLOW_LIST = '81.123.0.0/16,10.0.0.0/8'; |
| 103 | + const result = await handler(event, context); |
| 104 | + expect(result).toEqual({ isAuthorized: true }); |
| 105 | + }); |
| 106 | + it('should not pass of CIDR_IPV4_ALLOW_LIST has the wrong format', async () => { |
| 107 | + process.env.CIDR_IPV4_ALLOW_LIST = '81.123.0.0/16,10.0.0.0'; |
| 108 | + const result = await handler(event, context); |
| 109 | + expect(result).toEqual({ isAuthorized: false }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments