【问题标题】:User: anonymous is not authorized to perform: execute-api:Invoke on resource用户:匿名无权执行:execute-api:Invoke on resource
【发布时间】:2021-05-16 16:50:26
【问题描述】:

我正在尝试遵循此文档中的“仅限 API 网关资源策略”流程:https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-authorization-flow.html#apigateway-authorization-flow-resource-policy-only

当尝试使用授权签名访问受保护的路由时,我收到的响应显示 API Gateway 认为该请求是由匿名用户而不是有凭据的用户发出的。

我有一个具有以下资源策略的公共 API 网关部署。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": "execute-api:Invoke",
      "Resource": "arn:aws:execute-api:*:*:*/api/GET/server"
    },
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::<account-id>:user/api-auth"
      },
      "Action": "execute-api:Invoke",
      "Resource": "arn:aws:execute-api:*:*:*/api/POST/server"
    }
  ]
}

意外是,当我尝试使用 aws4_request Auth 签名使用 api-auth 用户的访问/密钥访问 POST 路由时,我得到:

用户:匿名无权执行:execute-api:Invoke on resource:

>>> import boto3
>>> import requests
>>> from requests_aws4auth import AWS4Auth
>>> 
>>> auth = AWS4Auth("<access-key>", "<secret-key>", "us-east-1", "execute-api")
>>> response = requests.request("POST", "https://<endpoint>.execute-api.us-east-1.amazonaws.com/api/server", auth=auth, data='', headers={})
>>> print(response.text)
{"Message":"User: anonymous is not authorized to perform: execute-api:Invoke on resource: arn:aws:execute-api:us-east-1:<account-id>:<endpoint>/api/POST/server"}

用户有以下政策

{
   "Effect": "Allow",
   "Action": "execute-api:Invoke",
   "Resource": "*"
}

从故障排除文档中可以看出,我应该正确配置:https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-troubleshoot-403-forbidden/

编辑 1

我的示例使用requests_aws4auth,它应该创建正确的标题。再举一个例子,从 Postman 生成的以下代码会导致同样的问题:

curl --location --request POST '<endpoint>.execute-api.us-east-1.amazonaws.com/api/server' \
--header 'X-Amz-Content-Sha256: <data>' \
--header 'X-Amz-Date: <date-data>' \
--header 'Authorization: AWS4-HMAC-SHA256 Credential=<access-key>/<date>/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=<signature>' \
--header 'Content-Type: application/json' \
--data-raw '<request>"}'

为了它的价值......

  1. 在没有Authorization 的情况下访问GET 路由可以正常工作。这是意料之中的。

  2. 当尝试访问标头中没有Authorization 签名的POST 路由时,我会收到相同的User: anonymous 消息。这也是意料之中的。

【问题讨论】:

  • User: anonymous 告诉您,您在 auth 对象中的身份验证尝试未被识别。您的请求似乎与this example in the docs 的签名不同,例如您没有 x-amz-date 标头,并且文档说 >“主机”和“x-amz-date”始终是必需的。
  • 感谢@rowanu 的评论 - 我在示例中使用的 python 包 (requests-aws4auth) 为我插入了这些标题。作为另一个示例,我包含了一个产生相同结果的curl 请求。

标签: amazon-web-services aws-api-gateway amazon-iam


【解决方案1】:

对于允许访问特定方法的资源策略,您需要将“方法执行”下的“授权”设置设置为“AWS_IAM”

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 2020-06-08
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多