AWS Builder Center: Learn, Build and Connect with builders in the AWS community
AWS Builder Center is the official home for builders on AWS. Share and read what others are working on, follow people who inspire you, explore training and workshops, and find tools to support what you're building.
如何将 IAM 用户或角色限制为 DynamoDB 表中的特定属性?
我想确保 AWS Identity and Access Management (IAM) 用户或角色只能访问 Amazon DynamoDB 表中的特定属性。
简短描述
使用精细访问控制 (FGAC) 的 IAM 策略条件。FGAC 使您能够控制对单个项目或项目属性的访问,以便在 DynamoDB 表中进行读取和写入操作。
解决方法
您可以使用预定义的 AWS 范围密钥和 DynamoDB 特定密钥来指定访问策略中的条件。以下是一些 DynamoDB 条件密钥常见使用案例的示例:
- dynamodb:LeadingKeys:允许用户访问具有特定分区键值的项。允许用户仅访问具有与唯一用户 ID(例如,${www.amazon.com:user_id})匹配的分区键值的项。
- dynamodb:Select:允许索引上的查询或扫描操作在仅当这些操作请求索引的投影属性时才返回响应。仅允许请求项目计数而不是实际项目的操作。
- dynamodb:Attributes:仅允许请求特定属性的请求。对于 PutItem,仅当项目具有除主键和排序键属性之外的特定属性时才允许请求。
- dynamodb:ReturnValues:对于 UpdateItem,仅允许已将 ReturnValues 设为 NONE(默认值)或 UPDATED_OLD(返回更新属性,如其在 UpdateItem 操作之前所示)的请求。
- dynamodb:ReturnConsumedCapacity:仅允许已将 ReturnConsumedCapacity 设为 NONE 或者未指定 ReturnConsumedCapacity 的请求。
示例:允许对表项目的某些属性进行只读访问
此示例中的表具有以下属性:
- 表名称:GameScores
- 主分区键:具有 UserId 属性的字符串
- 主排序键:具有 GameTitle 属性的字符串
- 非键属性:PersonalDetails、Wins、Losses、TopScore、TopScoreDateTime
假设您需要允许第三方应用程序读取每个用户的 TopScore 和 TopScoreDateTime 属性。由于 UserId 和 GameTitle 是分区和排序键属性,所以您还必须在 IAM 策略中允许这些属性。示例策略应满足以下条件:
- 对于查询或扫描 API 操作:仅允许 Select 参数已设为 SPECIFIC_ATTRIBUTES 时或 ProjectionExpression 包含 UserId、GameTitle、TopScore 或 TopScoreDateTime 时的请求。
- 对于 GetItem、BatchGetItem 和 TransactGetItem API 操作:仅允许 ProjectionExpression 参数包含 UserId、GameTitle、TopScore 或 TopScoreDateTime 时的请求。
**注意:**如果应用程序也正在读取此表的二级索引,则策略还必须允许二级索引的分区和排序键。
示例 IAM 策略使用 dynamodb:Attributes 条件键:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "LimitAccessToSpecificAttributes", "Effect": "Allow", "Action": [ "dynamodb:GetItem", "dynamodb:Query", "dynamodb:BatchGetItem", "dynamodb:Scan", "dynamodb:TransactGetItems" ], "Resource": [ "arn:aws:dynamodb:eu-west-1:123456789012:table/GameScores" ], "Condition": { "ForAllValues:StringEquals": { "dynamodb:Attributes": [ "TopScoreDateTime", "TopScore", "UserId", "GameTitle" ] }, "StringEquals": { "dynamodb:Select": "SPECIFIC_ATTRIBUTES" } } } ] }
客户端的请求必须指定允许的属性。如果请求未指定属性,则 DynamoDB 将尝试返回所有属性。由于 IAM 策略不允许客户端读取所有属性,则客户端将返回 AccessDeniedException。
以下示例中的查询请求将返回 AccessDeniedException,因为它未使用 Select 或 ProjectionExpression 来指定允许的属性:
$ aws dynamodb query --table-name GameScores --key-condition-expression "UserId = :useridval" --expression-attribute-values '{":useridval":{"S":"stefano_123"}}'
以下是一个成功的查询请求示例,它指定了 TopScore、TopScoreDateTime 和 GameTitle 属性:
$ aws dynamodb query --table-name GameScores --key-condition-expression "UserId = :useridval" --expression-attribute-values '{":useridval":{"S":"stefano_123"}}' --projection-expression "TopScore, TopScoreDateTime, GameTitle"
以下是一个成功的 GetItem 请求示例:
$ aws dynamodb get-item --table-name GameScores --key '{"UserId":{"S":"stefano_123"},"GameTitle":{"S":"Game Zero"}}' --projection-expression "UserId, GameTitle, TopScore, TopScoreDateTime"
有关如何使用条件键来限制访问的其他示例,请参阅权限使用案例。
