AWS API Gateway custom Authorizer strange showing error

前端 未结 5 2040
悲哀的现实
悲哀的现实 2021-02-05 04:10

Here is the context:

  • I set up a resource in the API gateway. /user/company
  • This resource have 2 methods. Get and POST.
  • I have configured a custom
5条回答
  •  眼角桃花
    2021-02-05 04:36

    This error will occur if you use event.methodArn as a resource for generated policy and share an authorizer between different functions, because of how policy caching works. For provided token it caches a policy across an entire API, it will be the same cache entry for all methods and resources within the same API and stage (if they share the same authorizer).

    For example, when making a request to GET /users, ARN will look something like this:

    arn:aws:execute-api:us-1:abc:123/prod/GET/users
    

    Next call to any endpoint with the same authentication token will use a cached policy, which was created on the first call to GET /users. The problem with that cached policy is that it's resource only allows a single particular resource arn: ... /prod/GET/users, any other resource will be rejected.

    Depending on how much do you want to limit policy permissions, you can either mention every possible resource when creating a policy

    {
      "principalId": "user",
      "policyDocument": {
        "Statement": [
          {
            "Action": "execute-api:Invoke",
            "Effect": "Allow",
            "Resource": [
              "arn:aws:execute-api:us-1:abc:123/prod/GET/v1/users",
              "arn:aws:execute-api:us-1:abc:123/prod/POST/v1/users",
              "arn:aws:execute-api:us-1:abc:123/prod/GET/v1/orders"
            ]
          }
        ],
        "Version": "2012-10-17"
      }
    }
    

    or use wildcards

    "Resource": "arn:aws:execute-api:us-1:abc:123/prod/*/v?/*"
    

    or even

    "Resource": "*"
    

    You can use policy variables for some advanced templates.

    It is also possible to use a blacklist approach by allowing everything using wildcards and then denying specific resources in another policy statement.

    Sources:

    • AWS forums: API Gateway issue about custom authorizers
    • AWS docs: IAM Policy resource field

提交回复
热议问题