Skip to content

fix(parser): handle API Gateway Test UI sourceIp #2531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/parser/src/schemas/apigw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ const APIGatewayEventIdentity = z.object({
cognitoIdentityId: z.string().nullish(),
cognitoIdentityPoolId: z.string().nullish(),
principalOrgId: z.string().nullish(),
sourceIp: z.string().ip().optional(),
/**
* When invoking the API Gateway REST API using the Test Invoke feature,
* the sourceIp is hardcoded to `test-invoke-source-ip`. This is a stopgap
* solution to allow customers to test their API and have successful parsing.
*
* See aws-powertools/powertools-lambda-python#1562 for more information.
*/
sourceIp: z
.union([z.string().ip(), z.literal('test-invoke-source-ip')])
.optional(),
user: z.string().nullish(),
userAgent: z.string().nullish(),
userArn: z.string().nullish(),
Expand Down
81 changes: 81 additions & 0 deletions packages/parser/tests/events/apiGatewayProxyEventTestUI.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"version": "1.0",
"resource": "/my/path",
"path": "/my/path",
"httpMethod": "GET",
"headers": {
"Header1": "value1",
"Header2": "value2",
"Origin": "https://aws.amazon.com"
},
"multiValueHeaders": {
"Header1": [
"value1"
],
"Header2": [
"value1",
"value2"
]
},
"queryStringParameters": {
"parameter1": "value1",
"parameter2": "value"
},
"multiValueQueryStringParameters": {
"parameter1": [
"value1",
"value2"
],
"parameter2": [
"value"
]
},
"requestContext": {
"accountId": "123456789012",
"apiId": "id",
"authorizer": {
"claims": null,
"scopes": null
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"extendedRequestId": "request-id",
"httpMethod": "GET",
"identity": {
"accessKey": null,
"accountId": null,
"caller": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"principalOrgId": null,
"sourceIp": "test-invoke-source-ip",
"user": null,
"userAgent": "user-agent",
"userArn": null,
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"path": "/my/path",
"protocol": "HTTP/1.1",
"requestId": "id=",
"requestTime": "04/Mar/2020:19:15:17 +0000",
"requestTimeEpoch": 1583349317135,
"resourceId": null,
"resourcePath": "/my/path",
"stage": "$default"
},
"pathParameters": null,
"stageVariables": null,
"body": "Hello from Lambda!",
"isBase64Encoded": false
}
6 changes: 6 additions & 0 deletions packages/parser/tests/unit/schema/apigw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ describe('APIGateway ', () => {
apiGatewayProxyOtherEvent
);
});
it('should not throw when event has sourceIp as test-invoke-source-ip', () => {
const apiGatewayProxyEventTestUi = TestEvents.apiGatewayProxyEventTestUI;
expect(() =>
APIGatewayProxyEventSchema.parse(apiGatewayProxyEventTestUi)
).not.toThrow();
});
it('should throw error when event is not a valid proxy event', () => {
const event = {
resource: '/',
Expand Down
1 change: 1 addition & 0 deletions packages/parser/tests/unit/schema/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const filenames = [
'apiGatewayProxyEventPrincipalId',
'apiGatewayProxyEvent_noVersionAuth',
'apiGatewayProxyOtherEvent',
'apiGatewayProxyEventTestUI',
'apiGatewayProxyV2Event',
'apiGatewayProxyV2EventPathTrailingSlash',
'apiGatewayProxyV2Event_GET',
Expand Down