Skip to content

APIGatewayRouteArn does not work with a proxy resource. #1047

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

Closed
brysontyrrell opened this issue Feb 28, 2022 · 9 comments
Closed

APIGatewayRouteArn does not work with a proxy resource. #1047

brysontyrrell opened this issue Feb 28, 2022 · 9 comments
Labels
bug Something isn't working p1

Comments

@brysontyrrell
Copy link

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
@brysontyrrell brysontyrrell added bug Something isn't working triage Pending triage from maintainers labels Feb 28, 2022
@michaelbrewer
Copy link
Contributor

Thanks. I will have a look

@michaelbrewer
Copy link
Contributor

@brysontyrrell can you add a sample event to make it easier for the failing test :-)

@michaelbrewer
Copy link
Contributor

@brysontyrrell this fix will shift the error to the allow_route call next and {proxy+} is included in the resource

        if not self._resource_pattern.match(resource):
>           raise ValueError(f"Invalid resource path: {resource}. Path should match {self.path_regex}")
E           ValueError: Invalid resource path: name/{proxy+}. Path should match ^[/.a-zA-Z0-9-_\*]+$

@michaelbrewer
Copy link
Contributor

@brysontyrrell i have a partial fix for the arn parsing:

But this will still fail to handle {proxy+} part

@brysontyrrell
Copy link
Author

@michaelbrewer sorry for not including an example event. The path does not actually contain {proxy+} in the Lambda Function's event.

{
    "type": "TOKEN",
    "methodArn": "arn:aws:execute-api:us-east-2:1234567890:abcd1234/latest/GET/path/part/part/1",
    "authorizationToken": "Bearer TOKEN"
}

In the above my API Gateway route may be configured to have /path/{proxy+} but the APIGatewayAuthorizerTokenEvent event methodArn is /path/part/part/1.

@michaelbrewer
Copy link
Contributor

Ok, @brysontyrrell then the PR should be valid!

@heitorlessa heitorlessa added p1 pending-release Fix or implementation already in dev waiting to be released and removed triage Pending triage from maintainers labels Mar 2, 2022
@heitorlessa
Copy link
Contributor

Thanks everyone, just merged and hope to make a patch release with this and another fix by end of the week.

I suspect we might not need this hard check in case additional changes occur to ARNs, but it's also hard to predict whether this will happen - leaving as-is and will add to the maintenance list to revisit in the future with more time to test unhappy paths.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2022

This is now released under 1.25.2 version!

@github-actions github-actions bot closed this as completed Mar 7, 2022
@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Mar 7, 2022
@michaelbrewer
Copy link
Contributor

Thanks everyone, just merged and hope to make a patch release with this and another fix by end of the week.

I suspect we might not need this hard check in case additional changes occur to ARNs, but it's also hard to predict whether this will happen - leaving as-is and will add to the maintenance list to revisit in the future with more time to test unhappy paths.

If there is an AWS regex or ARN parser then we can incorporate it into the tests. Otherwise a general purpose ARN parser could be used in many other places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p1
Projects
None yet
Development

No branches or pull requests

3 participants