首页 > 解决方案 > AWS Api Gateway 附加现有策略

问题描述

通常,如果我想创建一个私有AWS::ApiGateway::RestApi策略,只允许 VPC 流量调用 API 上的任何资源,我会这样做:

"ApiGatewayRestApi": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Name": "api-foo-bar",
        "EndpointConfiguration": {
          "Types": [
            "PRIVATE"
          ]
        },
        "Policy": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": "*",
              "Action": [
                "execute-api:Invoke"
              ],
              "Resource": "execute-api:/*",
              "Condition": {
                "StringEquals": {
                  "aws:SourceVpc": "vpc-000000000000"
                }
              }
            }
          ]
        }
      }
    }

有人问我是否可以创建一个策略,然后将其重用于我们可能创建的不同 Api 网关?其中包括:

"ApiGatewayRestApi": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Name": "api-foo-bar",
        "EndpointConfiguration": {
          "Types": [
            "PRIVATE"
          ]
        },
        "Policy": "arn:aws:*whatever*"
      }
    },

我不知道!我也找不到任何说明这一点的文档或示例。有人做过吗?这完全可行吗?谢谢 :)

标签: amazon-web-servicesamazon-cloudformationaws-api-gateway

解决方案


不,目前不可行。您附加到 Api 网关的策略是基于资源的策略。

从 aws 文档中,

使用基于资源的策略,您可以指定谁有权访问资源以及他们可以对其执行哪些操作。

Aws 文档显示了 cloudformation 中每个属性可以采用的类型。以下是“AWS::ApiGateway::RestApi”中允许的属性和类型

{
  "Type" : "AWS::ApiGateway::RestApi",
  "Properties" : {
      "ApiKeySourceType" : String,
      "BinaryMediaTypes" : [ String, ... ],
      "Body" : Json,
      "BodyS3Location" : S3Location,
      "CloneFrom" : String,
      "Description" : String,
      "DisableExecuteApiEndpoint" : Boolean,
      "EndpointConfiguration" : EndpointConfiguration,
      "FailOnWarnings" : Boolean,
      "MinimumCompressionSize" : Integer,
      "Mode" : String,
      "Name" : String,
      "Parameters" : {Key : Value, ...},
      "Policy" : Json,
      "Tags" : [ Tag, ... ]
    }
}

请注意,Policy 属性采用 JSON 类型。此外,文档为 Policy 属性编写了以下内容:

包含 RestApi 资源权限的策略文档。

并提示我们 Policy 属性不采用以下形式:"Policy": "arn:aws:*whatever*"并且只接受 JSON 形式的策略文档作为 Api Gateway 的基于资源的策略。

参考: https ://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-policy https://docs.aws.amazon.com/IAM /latest/UserGuide/access_policies_identity-vs-resource.html


推荐阅读