Skip to content

Commit cd6c512

Browse files
authored
fix(parser): handle API Gateway Test UI sourceIp (#2531)
1 parent 9624ad8 commit cd6c512

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

Diff for: packages/parser/src/schemas/apigw.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ const APIGatewayEventIdentity = z.object({
2222
cognitoIdentityId: z.string().nullish(),
2323
cognitoIdentityPoolId: z.string().nullish(),
2424
principalOrgId: z.string().nullish(),
25-
sourceIp: z.string().ip().optional(),
25+
/**
26+
* When invoking the API Gateway REST API using the Test Invoke feature,
27+
* the sourceIp is hardcoded to `test-invoke-source-ip`. This is a stopgap
28+
* solution to allow customers to test their API and have successful parsing.
29+
*
30+
* See aws-powertools/powertools-lambda-python#1562 for more information.
31+
*/
32+
sourceIp: z
33+
.union([z.string().ip(), z.literal('test-invoke-source-ip')])
34+
.optional(),
2635
user: z.string().nullish(),
2736
userAgent: z.string().nullish(),
2837
userArn: z.string().nullish(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"version": "1.0",
3+
"resource": "/my/path",
4+
"path": "/my/path",
5+
"httpMethod": "GET",
6+
"headers": {
7+
"Header1": "value1",
8+
"Header2": "value2",
9+
"Origin": "https://aws.amazon.com"
10+
},
11+
"multiValueHeaders": {
12+
"Header1": [
13+
"value1"
14+
],
15+
"Header2": [
16+
"value1",
17+
"value2"
18+
]
19+
},
20+
"queryStringParameters": {
21+
"parameter1": "value1",
22+
"parameter2": "value"
23+
},
24+
"multiValueQueryStringParameters": {
25+
"parameter1": [
26+
"value1",
27+
"value2"
28+
],
29+
"parameter2": [
30+
"value"
31+
]
32+
},
33+
"requestContext": {
34+
"accountId": "123456789012",
35+
"apiId": "id",
36+
"authorizer": {
37+
"claims": null,
38+
"scopes": null
39+
},
40+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
41+
"domainPrefix": "id",
42+
"extendedRequestId": "request-id",
43+
"httpMethod": "GET",
44+
"identity": {
45+
"accessKey": null,
46+
"accountId": null,
47+
"caller": null,
48+
"cognitoAuthenticationProvider": null,
49+
"cognitoAuthenticationType": null,
50+
"cognitoIdentityId": null,
51+
"cognitoIdentityPoolId": null,
52+
"principalOrgId": null,
53+
"sourceIp": "test-invoke-source-ip",
54+
"user": null,
55+
"userAgent": "user-agent",
56+
"userArn": null,
57+
"clientCert": {
58+
"clientCertPem": "CERT_CONTENT",
59+
"subjectDN": "www.example.com",
60+
"issuerDN": "Example issuer",
61+
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
62+
"validity": {
63+
"notBefore": "May 28 12:30:02 2019 GMT",
64+
"notAfter": "Aug 5 09:36:04 2021 GMT"
65+
}
66+
}
67+
},
68+
"path": "/my/path",
69+
"protocol": "HTTP/1.1",
70+
"requestId": "id=",
71+
"requestTime": "04/Mar/2020:19:15:17 +0000",
72+
"requestTimeEpoch": 1583349317135,
73+
"resourceId": null,
74+
"resourcePath": "/my/path",
75+
"stage": "$default"
76+
},
77+
"pathParameters": null,
78+
"stageVariables": null,
79+
"body": "Hello from Lambda!",
80+
"isBase64Encoded": false
81+
}

Diff for: packages/parser/tests/unit/schema/apigw.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ describe('APIGateway ', () => {
6868
apiGatewayProxyOtherEvent
6969
);
7070
});
71+
it('should not throw when event has sourceIp as test-invoke-source-ip', () => {
72+
const apiGatewayProxyEventTestUi = TestEvents.apiGatewayProxyEventTestUI;
73+
expect(() =>
74+
APIGatewayProxyEventSchema.parse(apiGatewayProxyEventTestUi)
75+
).not.toThrow();
76+
});
7177
it('should throw error when event is not a valid proxy event', () => {
7278
const event = {
7379
resource: '/',

Diff for: packages/parser/tests/unit/schema/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const filenames = [
2020
'apiGatewayProxyEventPrincipalId',
2121
'apiGatewayProxyEvent_noVersionAuth',
2222
'apiGatewayProxyOtherEvent',
23+
'apiGatewayProxyEventTestUI',
2324
'apiGatewayProxyV2Event',
2425
'apiGatewayProxyV2EventPathTrailingSlash',
2526
'apiGatewayProxyV2Event_GET',

0 commit comments

Comments
 (0)