

# 在 API Gateway 中为私有 API 使用 VPC 端点策略
<a name="apigateway-vpc-endpoint-policies"></a>

要提高私有 API 的安全性，您可以创建 VPC 端点策略。VPC 端点策略是一种 IAM 资源策略，您可以将其附加到接口端点。有关更多信息，请参阅[使用 VPC 端点控制对服务的访问](https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints-access.html)。

您可能需要创建 VPC 端点策略来执行以下任务：
+ 仅允许某些组织或资源访问您的 VPC 端点和调用您的 API。
+ 使用单一策略，并避免使用基于会话或基于角色的策略，来控制 API 的流量。
+ 从本地迁移到 AWS 时，加强应用程序的安全边界。

## VPC 端点策略注意事项
<a name="apigateway-vpc-endpoint-policies-considerations"></a>

以下是 VPC 端点策略的注意事项：
+ 根据 `Authorization` 标头值评估调用方的身份。首先评估 VPC 端点策略，然后 API Gateway 根据在方法请求中配置的授权类型对请求进行评估。下表显示了如何根据 `Authorization` 标头值的内容评估 VPC 端点策略。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/apigateway-vpc-endpoint-policies.html)
+ 如果访问控制依赖于使用持有者令牌，例如 Lambda 或 Amazon Cognito 授权方，则可以使用[资源的属性](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-resource-properties)来控制安全边界。
+  如果授权控制使用 IAM 授权，则可以使用[资源的属性](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-resource-properties)和[主体的属性](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-resource-principal)来控制安全边界。
+ VPC 端点策略可以与 API Gateway 资源策略一起使用。API Gateway 资源策略指定哪些主体可以访问 API。端点策略指定了谁可以访问 VPC，以及可以从 VPC 端点调用哪些 API。您的私有 API 需要资源策略，但您不需要创建自定义 VPC 端点策略。

## VPC 端点策略示例
<a name="apigateway-vpc-endpoint-policies-examples"></a>

您可以为 Amazon API Gateway 创建 Amazon Virtual Private Cloud 端点策略，并在其中指定以下内容：
+ 可执行操作的主体。
+ 可执行的操作。
+ 可用于执行操作的资源。

这可能取决于授权标头的内容。有关更多信息，请参阅 [VPC 端点策略注意事项](#apigateway-vpc-endpoint-policies-considerations)。有关其它策略示例，请参阅 GitHub 网站上的 [Data perimeter policy examples](https://github.com/aws-samples/data-perimeter-policy-examples)。

要将策略附加到 VPC 端点，您需要使用 VPC 控制台。有关更多信息，请参阅[使用 VPC 端点控制对服务的访问](https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints-access.html)。

## 示例 1：授予对两个 API 的访问权限的 VPC 端点策略
<a name="apigateway-vpc-endpoint-policies-example-1"></a>

以下示例策略只授予通过该策略附加到的 VPC 端点访问两个特定 API。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Principal": "*",
            "Action": [
                "execute-api:Invoke"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:execute-api:us-east-1:123412341234:a1b2c3d4e5/*",
                "arn:aws:execute-api:us-east-1:123412341234:aaaaa11111/*"
            ]
        }
    ]
}
```

------

## 示例 2：授予对 GET 方法的访问权限的 VPC 端点策略
<a name="apigateway-vpc-endpoint-policies-example-2"></a>

以下示例策略授予用户通过该策略附加到的 VPC 端点访问特定 API 的 `GET` 方法。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Principal": "*",
            "Action": [
                "execute-api:Invoke"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:execute-api:us-east-1:123412341234:a1b2c3d4e5/stageName/GET/*"
            ]
        }
    ]
}
```

------

## 示例 3：授予用户对特定 API 的特定用户访问权限的 VPC 端点策略
<a name="apigateway-vpc-endpoint-policies-example-3"></a>

以下示例策略授予特定用户通过该策略附加到的 VPC 端点访问特定 API。

在这种情况下，因为策略限制了对特定 IAM 主体的访问权限，所以您必须将方法的 `authorizationType` 设置为 `AWS_IAM` 或 `NONE`。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Principal": {
                "AWS": [
                    "arn:aws:iam::123412341234:user/MyUser"
                ]
            },
            "Action": [
                "execute-api:Invoke"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:execute-api:us-east-1:123412341234:a1b2c3d4e5/*"
            ]
        }
    ]
}
```

------

## 示例 4：VPC 端点策略授予用户对特定自定义域名以及映射到该域的每个 API 的访问权限
<a name="apigateway-vpc-endpoint-policies-example-4"></a>

以下示例策略通过该策略附加到的 VPC 端点，向用户授予对私有 API 的特定自定义域名的访问权限。通过此策略，只要用户在 VPC 端点与自定义域名之间创建了域名访问关联，并且用户被授予了调用自定义域名以及映射到自定义域名的任何私有 API 的权限，用户就可以调用映射到此自定义域名的任何 API。有关更多信息，请参阅 [API Gateway 中私有 API 的自定义域名](apigateway-private-custom-domains.md)。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "execute-api:Invoke",
      "Resource": [
        "*"
      ],
       "Condition": {
        "ArnEquals": {
          "execute-api:viaDomainArn": "arn:aws:execute-api:us-west-2:111122223333:/domainnames/private.test.com+f4g5h6",
        }
      }
    }
  ]
}
```

------

## 示例 5：VPC 端点策略授予或拒绝对特定 API 和域资源的访问权限
<a name="apigateway-vpc-endpoint-policies-example-5"></a>

以下示例策略向用户授予对特定 API 和域资源的访问权限。通过此策略，只要用户在 VPC 端点与自定义域名之间创建了域名访问关联，并且用户被授予了调用自定义域名以及映射到自定义域名的任何私有 API 的权限，用户就可以调用允许的私有 API 和域资源。有关更多信息，请参阅 [API Gateway 中私有 API 的自定义域名](apigateway-private-custom-domains.md)。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "execute-api:Invoke",
      "Resource": [
        "arn:aws:execute-api:us-west-2:111122223333:/domainnames/private.test.com+f4g5h6",
        "arn:aws:execute-api:us-west-2:111122223333:a1b2c3d4e5/*"
      ]
    },
    {
      "Effect": "Deny",
      "Principal": {
        "AWS": "*"
      },
      "Action": "execute-api:Invoke",
      "Resource": [
        "arn:aws:execute-api:us-west-2:111122223333:a1b2c3d4e5/admin/*",
        "arn:aws:execute-api:us-west-2:111122223333:bcd123455/*"
      ]
    }
  ]
}
```

------

## 示例 6：VPC 端点策略按属于组织的主体和资源授予或拒绝访问权限
<a name="apigateway-vpc-endpoint-policies-example-6"></a>

以下示例策略向属于某个组织的主体和资源授予访问权限。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Condition": {
                "StringEquals": {
                    "aws:ResourceOrgID": "o-abcd1234",
                    "aws:PrincipalOrgID": "o-abcd1234"
                }
            },
            "Action": "*",
            "Resource": "*",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Sid": "AllowRequestsByOrgsIdentitiesToOrgsResources"
        }
    ]
}
```

------