使用 AWS API 网关上的资源策略允许特定 API IP 地址路径

Allow a specific API Path to an IP address using Resource Policy on AWS API Gateway

场景

我有一个 EC2 服务器,他点击了“n”个数,一组用户点击了“m”个 APIs 通过互联网,共 APIs。对于那些“n”数量的 APIs,我只需要这个 EC2 服务器来访问它,不需要其他用户。

问题

同时设置资源策略,为EC2白名单访问'n'APIs,其他'm' API 路径也受到限制。

描述

API结构

请注意,所有请求都是针对 n 的 GET。

/
|--m
   |--square GET
   |--triangle POST
   L--circle GET, POST
|--n
   |--red GET
   |--green GET
      |--trees GET
      L--pear GET
   L--blue GET

已创建资源策略

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:xx-direction-1:123456789101:abcdefghij/alpha/GET/m/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": "100.200.100.200/32"
                }
            }
        }
    ]
}

当我通过 curl 访问“m”路径时,我收到与用户点击“[=34=”时相同的 json ]n' APIs。即。

{"message":"User: anonymous is not authorized to perform: execute-api:Invoke on resource: arn:aws:execute-api:...

可以说,这里的问题是什么,但很简单。诀窍是在相同的 API 路径上允许和拒绝,解决方案让我想起了电子产品的 NAND 门和 NOR 门类中使用的逻辑。

解决方案

第一步:如果IP地址不是我们需要的,则拒绝路径

注意,Condition 如何调用 NotIpAddress

{
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:xx-direction-1:123456789101:abcdefghij/alpha/GET/m/*",
            "Condition": {
                "NotIpAddress": {
                    "aws:SourceIp": "100.200.100.200/32"
                }
            }
        }

第 2 步:如果 IP 地址是我们需要的,则允许该路径

把这里的Condition改一下,这是我漏掉的

{
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:xx-direction-1:123456789101:abcdefghij/alpha/GET/m/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": "100.200.100.200/32"
                }
            }
        }

第 3 步:允许 Public / Users

访问根路径

没有提到 IP 地址条件,因为“Public”!

{
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-south-1:480833364711:q9omcoj8ba/alpha/GET/*"
        }

完整的资源策略如下所示:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:xx-direction-1:123456789101:abcdefghij/alpha/GET/m/*",
            "Condition": {
                "NotIpAddress": {
                    "aws:SourceIp": "100.200.100.200/32"
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:xx-direction-1:123456789101:abcdefghij/alpha/GET/m/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": "100.200.100.200/32"
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-south-1:480833364711:q9omcoj8ba/alpha/GET/*"
        }
    ]
}

这很有魅力!