使用AWS API Gateway上的资源策略允许特定的API路径访问IP地址

时间:2019-08-26 12:42:43

标签: amazon-web-services amazon-ec2 api-gateway

场景

我有一个EC2服务器,它通过Internet上的API命中了“ n ”号,还有一组用户通过API命中了“ m ”个API。仅对于那些数量众多的 n 个API,我仅需要此EC2服务器即可访问它,而无需其他用户。

问题

在设置资源策略以将EC2列入白名单以访问“ n ” API的同时,其他“ m ” API路径也受到限制。

说明

API结构

请注意,所有请求都是GET for n。

/
|--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 '路径时,当它命中' n 'API时,会收到与用户相同的json。就是

  

{“ message”:“用户:匿名无权执行:   execute-api:在资源上调用:arn:aws:execute-api:...

1 个答案:

答案 0 :(得分:0)

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

解决方案

步骤1:如果IP地址不是我们所需的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地址是我们所需的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步:允许公共/用户访问根路径

没有提到IP地址条件,这是因为“公共”!

{
            "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/*"
        }
    ]
}

这就像一种魅力!