Skip to content

APIGatewayRouteArn does not work with a proxy resource. #1047

Closed
@brysontyrrell

Description

@brysontyrrell

What were you trying to accomplish?

API Gateway with a Lambda Authorizer.

Path is /name/{proxy+}.

Allowing the route requested in the policy using APIGatewayAuthorizerResponse:

# APIGatewayAuthorizerTokenEvent
event_arn = event.parsed_arn
authorizer_policy = APIGatewayAuthorizerResponse(...)
authorizer_policy.allow_route(
    http_method=event_arn.http_method, resource=event_arn.resource
)
authorizer_policy.asdict()

Expected Behavior

The policy contains the correct resource path and can be returned to API Gateway.

Current Behavior

The resource is blank which causes this error to be thrown:

[ERROR] ValueError: Invalid resource path: . Path should match ^[/.a-zA-Z0-9-_\*]+$

I tracked it down to this line:

https://github.com/awslabs/aws-lambda-powertools-python/blame/develop/aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py#L63

I don't know what the reason was for a hard check on the length of the parts for the path. @michaelbrewer could you share some light there?

Possible Solution

This issue is resolved with the following modification:

def alt_parse_api_gateway_arn(arn: str) -> APIGatewayRouteArn:
    """Parses a gateway route arn as a APIGatewayRouteArn class

    Parameters
    ----------
    arn : str
        ARN string for a methodArn or a routeArn
    Returns
    -------
    APIGatewayRouteArn
    """
    arn_parts = arn.split(":")
    api_gateway_arn_parts = arn_parts[5].split("/")
    return APIGatewayRouteArn(
        region=arn_parts[3],
        aws_account_id=arn_parts[4],
        api_id=api_gateway_arn_parts[0],
        stage=api_gateway_arn_parts[1],
        http_method=api_gateway_arn_parts[2],
        resource="/".join(api_gateway_arn_parts[3:]) if len(api_gateway_arn_parts) >= 4 else "",
    )

Steps to Reproduce (for bugs)

Create a proxy resource and setup a Lambda Authorizer using Powertools as described above.

Environment

  • Powertools version used: 1.25.1
  • Packaging format (Layers, PyPi): Both
  • AWS Lambda function runtime: Python 3.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions