

# AWS Glue 基于资源的策略示例
<a name="security_iam_resource-based-policy-examples"></a>

本部分包含基于资源的策略示例，其中包括用于授予跨账户存取的策略。

此示例使用 AWS Command Line Interface（AWS CLI）与 AWS Glue 服务 API 操作进行交互。您可以使用 AWS Glue 控制台或使用 AWS SDK 之一执行相同操作。

**重要**  
通过更改 AWS Glue 资源策略，您可能意外撤消您账户中现有 AWS Glue 用户的权限并导致意外中断。仅在开发或测试账户中尝试这些示例，并确保它们不会中断任何现有的工作流，然后再进行更改。

**Topics**
+ [将基于资源的策略用于 AWS Glue 的注意事项](#security_iam_resource-based-policy-examples-considerations)
+ [使用资源策略控制同一账户中的访问](#glue-policy-resource-policies-example-same-account)

## 将基于资源的策略用于 AWS Glue 的注意事项
<a name="security_iam_resource-based-policy-examples-considerations"></a>

**注意**  
IAM policy 和 AWS Glue 资源策略都需要几秒钟进行传播。附加新策略后，您可能会注意到，旧策略仍有效，直至新策略传播到整个系统。

可使用以 JSON 格式编写的策略文档来创建或修改资源策略。策略语法与基于身份的 IAM policy 相同（请参阅 [IAM JSON 策略参考](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies.html)），但存在以下例外：
+ 每条策略语句需要一个 `"Principal"` 或 `"NotPrincipal"` 块。
+ `"Principal"` 或 `"NotPrincipal"` 必须识别有效的现有主体。不允许使用通配符模式（如 `arn:aws:iam::account-id:user/*`）。
+ 策略中的 `"Resource"` 块要求所有资源 ARN 均与以下正则表达式语法匹配（其中第一个 `%s` 为 *region*，第二个 `%s` 为 *account-id*）：

  ```
  *arn:aws:glue:%s:%s:(\*|[a-zA-Z\*]+\/?.*)
  ```

  例如，`arn:aws:glue:us-west-2:account-id:*` 和 `arn:aws:glue:us-west-2:account-id:database/default` 都允许使用，但不允许 `*`。
+ 与基于身份的策略不同，AWS Glue 资源策略只能包含属于该策略附加到的目录的资源的 Amazon 资源名称（ARN）。此类 ARN 总是以 `arn:aws:glue:` 开头。
+ 策略不能导致创建它的身份被锁定，无法进一步创建或修改策略。
+ 资源策略 JSON 文档的大小不能超过 10 KB。

## 使用资源策略控制同一账户中的访问
<a name="glue-policy-resource-policies-example-same-account"></a>

在此示例中，账户 A 中的管理员用户创建一个资源策略，该策略向账户 A 中的 IAM 用户 `Alice` 授予对目录的完整访问权限。Alice 未附加 IAM policy。

为执行此操作，管理员用户需运行以下 AWS CLI 命令。

```
# Run as admin of Account A
$ aws glue put-resource-policy --profile administrator-name --region us-west-2 --policy-in-json '{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Principal": {
        "AWS": [
          "arn:aws:iam::account-A-id:user/Alice"
        ]
      },
      "Effect": "Allow",
      "Action": [
        "glue:*"
      ],
      "Resource": [
        "arn:aws:glue:us-west-2:account-A-id:*"
      ]
    }
  ]
}'
```

不是在 AWS CLI 命令中输入 JSON 策略文档，您可以将策略文档保存在一个文件中，然后在 AWS CLI 命令中引用该文件路径，前缀为 `file://`。下面是具体操作示例。

```
$ echo '{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Principal": {
        "AWS": [
          "arn:aws:iam::account-A-id:user/Alice"
        ]
      },
      "Effect": "Allow",
      "Action": [
        "glue:*"
      ],
      "Resource": [
        "arn:aws:glue:us-west-2:account-A-id:*"
      ]
    }
  ]
}' > /temp/policy.json

$ aws glue put-resource-policy --profile admin1 \
    --region us-west-2 --policy-in-json file:///temp/policy.json
```

在此资源策略传播后，Alice 可以访问账户 A 中的所有 AWS Glue 资源，如下所示。

```
# Run as user Alice
$ aws glue create-database --profile alice --region us-west-2 --database-input '{
    "Name": "new_database",
    "Description": "A new database created by Alice",
    "LocationUri": "s3://amzn-s3-demo-bucket"
}'

$ aws glue get-table --profile alice --region us-west-2 --database-name "default" --table-name "tbl1"}
```

为了响应 Alice 的 `get-table` 调用，AWS Glue 服务将返回以下内容。

```
{
  "Table": {
    "Name": "tbl1",
    "PartitionKeys": [],
    "StorageDescriptor": {
        ......
    },
    ......
  }
}
```