diff --git a/aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py b/aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py index bcf035b61c0..57f90be0d35 100644 --- a/aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py +++ b/aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py @@ -514,6 +514,7 @@ def from_route_arn( parsed_arn.stage, context, usage_identifier_key, + partition=parsed_arn.partition, ) def _add_route(self, effect: str, http_method: str, resource: str, conditions: list[dict] | None = None): @@ -667,6 +668,7 @@ def from_route_arn( parsed_arn.stage, context, usage_identifier_key, + partition=parsed_arn.partition, ) # Note: we need ignore[override] because we are removing the http_method field diff --git a/tests/unit/data_classes/required_dependencies/test_api_gateway_authorizer.py b/tests/unit/data_classes/required_dependencies/test_api_gateway_authorizer.py index cb2dc718036..4bde0df83e1 100644 --- a/tests/unit/data_classes/required_dependencies/test_api_gateway_authorizer.py +++ b/tests/unit/data_classes/required_dependencies/test_api_gateway_authorizer.py @@ -273,3 +273,16 @@ def test_parse_api_gateway_arn_with_resource(): response = authorizer_policy.asdict() assert mock_event["methodArn"] == response["policyDocument"]["Statement"][0]["Resource"][0] + + +@pytest.mark.parametrize("partition", ["aws", "aws-cn", "aws-us-gov"]) +def test_authorizer_response_from_route_arn_keeps_partition(partition): + # GIVEN a method ARN from a non-commercial partition + arn = f"arn:{partition}:execute-api:cn-north-1:123456789012:api123/prod/GET/orders" + + # WHEN the response is built with from_route_arn + builder = APIGatewayAuthorizerResponse.from_route_arn(arn, principal_id="foo") + builder.allow_route(http_method="GET", resource="/orders") + + # THEN the policy resource is the ARN API Gateway sent, partition included + assert builder.asdict()["policyDocument"]["Statement"][0]["Resource"] == [arn] diff --git a/tests/unit/data_classes/required_dependencies/test_api_gateway_authorizer_websocket_event.py b/tests/unit/data_classes/required_dependencies/test_api_gateway_authorizer_websocket_event.py index d4d6abd6ba8..5302fa147f9 100644 --- a/tests/unit/data_classes/required_dependencies/test_api_gateway_authorizer_websocket_event.py +++ b/tests/unit/data_classes/required_dependencies/test_api_gateway_authorizer_websocket_event.py @@ -181,3 +181,16 @@ def test_authorizer_response_allow_route_with_underscore(builder: APIGatewayAuth ], }, } + + +@pytest.mark.parametrize("partition", ["aws", "aws-cn", "aws-us-gov"]) +def test_authorizer_response_from_route_arn_keeps_partition(partition): + # GIVEN a route ARN from a non-commercial partition + arn = f"arn:{partition}:execute-api:cn-north-1:123456789012:api123/prod/$connect" + + # WHEN the response is built with from_route_arn + builder = APIGatewayAuthorizerResponseWebSocket.from_route_arn(arn, principal_id="foo") + builder.allow_route(resource="$connect") + + # THEN the policy resource is the ARN API Gateway sent, partition included + assert builder.asdict()["policyDocument"]["Statement"][0]["Resource"] == [arn]