Api 网关上的 AWS 资源策略:匿名无权使用显式拒绝对资源执行调用

AWS resource policy on Api Gateway: anonymous is not authorized to perform invoke on resource with explicit deny

低于 AWS 上的资源策略 API- 从 VPC 外部和内部调用时生成此响应的网关

{"Message":"User: anonymous is not authorized to perform: execute-api:Invoke on resource: arn:aws:execute-api:ap-south-1:********2818:d5cbeh0e78/default/GET/autoimageresize-staging with an explicit deny"}

资源策略:白名单 VPC

{
    "Version": "2012-10-17",<
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-south-1:********2818:d5cbeh0e78/*/*/*"
        },
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-south-1:********2818:d5cbeh0e78/*/*/*",
            "Condition": {
                "StringNotEquals": {
                    "aws:SourceVpc": "vpc-********"
                }
            }
        }
    ]
}

而将具有相关 IP 的资源列入白名单在以下资源策略下运行良好

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-south-1:********2818:d5cbeh0e78/*/*/*"
        },
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-south-1:********2818:d5cbeh0e78/*/*/*",
            "Condition": {
                "NotIpAddress": {
                    "aws:SourceIp": "xx.xxx.xxx.xx"
                }
            }
        }
    ]
}

任何人都可以建议我可能哪里出错了,如果不是我所期望的,我如何验证我得到的 aws:SourceVpc 值。还有一件事,有些地方我在 aws 文档中将密钥作为 SourceVpc 而在其他地方作为 sourceVpc。

提前致谢

API 网关 API 是 public,除非您为 api 网关创建私有 VPC 端点。

仅当存在私有 VPC 端点时:

  • 你可以查看SourceVPC
  • 私有 ip 地址的 SourceIp 条件

这是因为流量来自内网

如果您有 API 网关 VPC 端点:

创建私有 VPC 端点后,同一帐户中的所有现有 public api 只能通过其自定义域访问。

还要确保重新部署 api 以反映资源策略更改。

参考: https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-resource-policies-aws-condition-keys.html

我发现您需要两件事来创建私有 REST API。首先,您需要一个允许从 VPC 访问的资源策略。然后您需要在尝试访问私有 REST API.

的 VPC 中创建一个 VPC 端点

当troubleshooting/revising一个Resource Policy时,必须按顺序执行以下步骤。

  1. 保存资源策略。
  2. 重新部署 API(资源 - 操作 | 部署 API)
  3. 等待 10 - 15 秒。

未能等待更改传播将导致混乱的结果。

在解决这些类型的问题时,我发现 API 网关日志没有那么有用。通常有一个条目显示 "The client is not authorized to perform this operation",类似于 HTTP 403(禁止访问)。