Skip to content

Commit 51ff350

Browse files
Michael Brewerheitorlessa
Michael Brewer
andauthored
fix(lambda-authorizer): allow proxy resources path in arn (#1051)
Co-authored-by: Heitor Lessa <[email protected]>
1 parent 6e4cbd7 commit 51ff350

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def parse_api_gateway_arn(arn: str) -> APIGatewayRouteArn:
6060
api_id=api_gateway_arn_parts[0],
6161
stage=api_gateway_arn_parts[1],
6262
http_method=api_gateway_arn_parts[2],
63-
resource=api_gateway_arn_parts[3] if len(api_gateway_arn_parts) == 4 else "",
63+
# conditional allow us to handle /path/{proxy+} resources, as their length changes.
64+
resource="/".join(api_gateway_arn_parts[3:]) if len(api_gateway_arn_parts) >= 4 else "",
6465
)
6566

6667

tests/functional/data_classes/test_api_gateway_authorizer.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
44
DENY_ALL_RESPONSE,
55
APIGatewayAuthorizerResponse,
6+
APIGatewayAuthorizerTokenEvent,
67
HttpVerb,
78
)
89

@@ -195,3 +196,26 @@ def test_authorizer_response_allow_route_with_underscore(builder: APIGatewayAuth
195196
],
196197
},
197198
}
199+
200+
201+
def test_parse_api_gateway_arn_with_resource():
202+
mock_event = {
203+
"type": "TOKEN",
204+
"methodArn": "arn:aws:execute-api:us-east-2:1234567890:abcd1234/latest/GET/path/part/part/1",
205+
"authorizationToken": "Bearer TOKEN",
206+
}
207+
event = APIGatewayAuthorizerTokenEvent(mock_event)
208+
event_arn = event.parsed_arn
209+
assert event_arn.resource == "path/part/part/1"
210+
211+
authorizer_policy = APIGatewayAuthorizerResponse(
212+
principal_id="fooPrinciple",
213+
region=event_arn.region,
214+
aws_account_id=event_arn.aws_account_id,
215+
api_id=event_arn.api_id,
216+
stage=event_arn.stage,
217+
)
218+
authorizer_policy.allow_route(http_method=event_arn.http_method, resource=event_arn.resource)
219+
response = authorizer_policy.asdict()
220+
221+
assert mock_event["methodArn"] == response["policyDocument"]["Statement"][0]["Resource"][0]

0 commit comments

Comments
 (0)