本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
AWS Glue 基于资源的访问控制策略示例
本部分包含示例资源策略,其中包括用于授予跨账户访问权限的策略。
通过更改 AWS Glue 资源策略,您可能意外撤消您账户中现有 AWS Glue 用户的权限并导致意外中断。仅在开发或测试账户中尝试这些示例,并确保它们不会中断任何现有的工作流,然后再进行更改。
IAM 策略和 AWS Glue 资源策略都需要几秒钟进行传播。附加新策略后,您可能会注意到,旧策略仍有效,直至新策略传播到整个系统。
以下示例使用 AWS Command Line Interface (AWS CLI) 与 AWS Glue 服务 APIs 进行交互。 您可以在 AWS Glue 控制台上或使用 AWS SDKs之一执行相同的操作。
设置 AWS CLI
-
按照 AWS CLIAWS 命令行界面用户指南 中的AWS Command Line Interface安装 中的说明安装 。
-
按照AWS CLI配置和凭证文件中的说明配置 。使用您的 AWS 账户管理员凭证创建管理员配置文件。将默认 AWS 区域配置为 us-west-2(或您使用的区域),并将默认输出格式设置为
JSON
。 -
通过运行以下命令(替换 AWS Glue)测试对 API 的访问权限
Alice
替换为您的账户中的真正 IAM 用户或角色)。# Run as admin of account
account-id
$ aws glue put-resource-policy --profileadministrator-name
--region us-west-2 --policy-in-json '{ "Version": "2012-10-17", "Statement": [ { "Principal": { "AWS": [ "arn:aws:iam::account-id
:user/Alice" ] }, "Effect": "Allow", "Action": [ "glue:*" ], "Resource": [ "arn:aws:glue:us-west-2:account-id
:*" ] } ] }' -
为用于测试资源策略和跨账户访问的账户中的每个 IAM 用户配置用户配置文件。
示例 1. 使用资源策略控制同一账户中的访问
在此示例中,账户 A 中的管理员用户创建一个资源策略,该策略向账户 A 中的 IAM 用户 Alice
授予对目录的完整访问权限。Alice 未附加 IAM 策略。
为执行此操作,管理员用户需运行以下 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 命令中引用带有前缀 AWS CLI 的文件路径,而不是输入 JSON 策略文档作为 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://my-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": { ...... }, ...... } }
示例 2. 使用资源策略授予跨账户访问权限
在此示例中,账户 A 中的资源策略用于向账户 B 中的 Bob 授予对账户 A 中所有数据目录资源的只读访问权限。为此,需要执行四个步骤:
-
验证账户 A 是否已将其 Amazon Athena 数据目录迁移到 AWS Glue。
如果资源所有者账户未将其 Athena 数据目录迁移至 AWS Glue,则不允许跨账户访问 AWS Glue。有关如何将 Athena 目录迁移到 AWS Glue 的更多详细信息,请参阅 中的AWS Glue 数据目录升级到 分步指南。Amazon Athena 用户指南
# Verify that the value "ImportCompleted" is true. This value is region specific. $ aws glue get-catalog-import-status --profile admin1 --region us-west-2 { "ImportStatus": { "ImportCompleted": true, "ImportTime": 1502512345.0, "ImportedBy": "StatusSetByDefault" } }
-
账户 A 中的管理员创建一个资源策略,用于向账户 B 授予访问权限。
# Run as admin of Account A $ aws glue put-resource-policy --profile admin1 --region us-west-2 --policy-in-json '{ "Version": "2012-10-17", "Statement": [ { "Principal": { "AWS": [ "arn:aws:iam::
account-B-id
:root" ] }, "Effect": "Allow", "Action": [ "glue:Get*", "glue:BatchGet*" ], "Resource": [ "arn:aws:glue:us-west-2:account-A-id
:*" ] } ] }' -
账户 B 中的管理员使用 IAM 策略向 Bob 授予对账户 A 的访问权限。
# Run as admin of Account B $ aws iam put-user-policy --profile admin2 --user-name Bob --policy-name CrossAccountReadOnly --policy-document '{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "glue:Get*", "glue:BatchGet*" ], "Resource": [ "arn:aws:glue:us-west-2:
account-A-id
:*" ] } ] }' -
验证 Bob 对账户 A 中资源的访问权限。
# Run as user Bob. This call succeeds in listing Account A databases. $ aws glue get-databases --profile bob --region us-west-2 --catalog-id
account-A-id
{ "DatabaseList": [ { "Name": "db1" }, { "Name": "db2" }, ...... ] } # This call succeeds in listing tables in the 'default' Account A database. $ aws glue get-table --profile alice --region us-west-2 --catalog-idaccount-A-id
\ --database-name "default" --table-name "tbl1" { "Table": { "Name": "tbl1", "PartitionKeys": [], "StorageDescriptor": { ...... }, ...... } } # This call fails with access denied, because Bob has only been granted read access. $ aws glue create-database --profile bob --region us-west-2 --catalog-idaccount-A-id
--database-input '{ "Name": "new_database2", "Description": "A new database created by Bob", "LocationUri": "s3://my-bucket2" }' An error occurred (AccessDeniedException) when calling the CreateDatabase operation: User: arn:aws:iam::account-B-id
:user/Bob is not authorized to perform: glue:CreateDatabase on resource: arn:aws:glue:us-west-2:account-A-id
:database/new_database2
在步骤 2 中,账户 A 中的管理员向账户 B 的根用户授予权限。然后,该根用户可通过将 IAM 策略附加到 IAM 委托人(用户、角色、组等)来将其拥有的权限委派给这些委托人。由于管理员用户已附加完整访问权限的 IAM 策略,所以管理员将自动拥有已授予根用户的权限以及向账户中的其他 IAM 用户委派权限的权限。
也可以在步骤 2 中,直接将权限授予用户 Bob 的 Amazon 资源名称 (ARN)。这会将跨账户访问权限仅授予 Bob。但是,要使 Bob 实际获得跨账户访问权限,仍需执行步骤 3。对于跨账户访问,需要同时具有资源账户中的资源策略和用户账户中的 IAM 策略,才能使用访问权限。这不同于示例 1 中的同一账户访问,其中资源策略或 IAM 策略可以独立授予访问权限。