【问题标题】:How to allow only an IP/range access to AWS API Gateway resources如何仅允许 IP/范围访问 AWS API Gateway 资源
【发布时间】:2019-06-26 16:10:37
【问题描述】:

如何最好地通过 IP 限制对 AWS API 网关中某些路由的访问? 我想只允许我的 ECS 集群访问 API 网关中的某些路由。我尝试将 ECS NAT 网关、VPC CIDR 范围放在aws:SourceIp 中,但总是被拒绝。我什至尝试了我的个人计算机公共IP地址......结果相同......这是正确的方法吗?或者我应该尝试 IAM 授权人吗? IAM 授权方的缺点是我需要签署我的 API 调用?也许使用 API Gateway SDK?这意味着我更愿意避免代码更改。

{
  "Id": "MY_API_POLICY",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Deny",
      "Principal": "*",
      "Action": "execute-api:Invoke",
      "Condition": {
        "NotIpAddress": {
          "aws:SourceIp": ["XX.XX.XX.XX/32"]
        }
      },
      "Resource": [
        "arn:aws:execute-*:*:apiid/stagename/*/private/route"
      ]
    },
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": "execute-api:Invoke",
      "Resource": [
        "arn:aws:execute-*:*:apiid/stagename/*/public/route"
      ]
    }
  ]
}

【问题讨论】:

  • 使用 Amazon API Gateway,您可以创建只能使用接口 VPC 终端节点从您的 Amazon Virtual Private Cloud (VPC) 访问的私有 REST API。这有帮助吗?
  • @jarmod 但我是一些公开的 API
  • 集群的安全组,用于阻止您不希望它访问的那些 ips 的入站/出站流量,
  • 是的资源策略是限制 ip/range 访问的正确方法。尝试关注aws.amazon.com/blogs/compute/… 以找到正确的方法。
  • @BanjoObayomi 如果您仅阻止安全组的端口,这可能不足以更好地阻止 api 网关上的 IP 范围

标签: amazon-web-services authentication authorization aws-api-gateway


【解决方案1】:

正如@Visal 已经提到的那样,限制 ip/范围是正确的方法。示例如下:https://aws.amazon.com/de/blogs/compute/control-access-to-your-apis-using-amazon-api-gateway-resource-policies/

有一个策略示例,允许访问某个 ip 范围:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::<account_idA>:user/<user>",
                    "arn:aws:iam::<account_idA>:root"
                ]
            },
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:us-east-1:<account_idB>:qxz8y9c8a4/*/*/*"
        },
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:us-east-1:<account_idB>:qxz8y9c8a4/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": " 203.0.113.0/24"
                }
            }
        }
    ]
}

或者如果你想拒绝访问,那么你会发现这个政策:

{
    "Effect": "Deny",
    "Principal": "*",
    "Action": "execute-api:Invoke",
    "Resource": "arn:aws:execute-api:us-east-1:<account_idB>:qxz8y9c8a4/*",
    "Condition": {
        "IpAddress": {
            "aws:SourceIp": "203.0.113.0/24"
        }
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2016-07-23
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 2021-09-28
    • 2018-06-23
    • 1970-01-01
    相关资源
    最近更新 更多