使用 ABAC 搭配 DynamoDB 資料表與索引的範例 - Amazon DynamoDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 ABAC 搭配 DynamoDB 資料表與索引的範例

下列範例展示使用標籤實作屬性式條件的多種使用案例。

範例 1:使用 aws:ResourceTag 允許動作

使用 aws:ResourceTag/tag-key 條件鍵,您可以比較 IAM 政策中指定的標籤鍵值對與附加在 DynamoDB 資料表中的鍵值對。例如,若 IAM 政策與資料表的標籤條件相符,即可允許執行特定動作,如 PutItem。請依下列步驟操作:

Using the AWS CLI
  1. 建立資料表。下列範例使用 create-table AWS CLI 命令來建立名為 的資料表myMusicTable

    aws dynamodb create-table \ --table-name myMusicTable \ --attribute-definitions AttributeName=id,AttributeType=S \ --key-schema AttributeName=id,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \ --region us-east-1
  2. 為此資料表新增一個標籤。下列 tag-resource AWS CLI 命令範例會將標籤鍵值對新增至 Title: ProductManager myMusicTable

    aws dynamodb tag-resource --region us-east-1 --resource-arn arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable --tags Key=Title,Value=ProductManager
  3. 建立內嵌政策並將其新增至已連接 AmazonDynamoDBReadOnlyAccess AWS 受管政策的角色,如下列範例所示。

    JSON
    JSON
    { "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "dynamodb:PutItem", "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:ResourceTag/Title": "ProductManager" } } } ] }

    當附加至資料表的標籤鍵和值與政策中指定的標籤相符時,此政策允許在該資料表上執行 PutItem 動作。

  4. 依步驟 3 中說明的政策假設該角色。

  5. 使用 put-item AWS CLI 命令將項目放入 myMusicTable

    aws dynamodb put-item \ --table-name myMusicTable --region us-east-1 \ --item '{ "id": {"S": "2023"}, "title": {"S": "Happy Day"}, "info": {"M": { "rating": {"N": "9"}, "Artists": {"L": [{"S": "Acme Band"}, {"S": "No One You Know"}]}, "release_date": {"S": "2023-07-21"} }} }'
  6. 掃描資料表以確認項目是否已成功新增。

    aws dynamodb scan --table-name myMusicTable --region us-east-1
Using the AWS SDK for Java 2.x
  1. 建立資料表。下列範例使用 CreateTable API 建立名為 myMusicTable 的資料表。

    DynamoDbClient dynamoDB = DynamoDbClient.builder().region(region).build(); CreateTableRequest createTableRequest = CreateTableRequest.builder() .attributeDefinitions( Arrays.asList( AttributeDefinition.builder() .attributeName("id") .attributeType(ScalarAttributeType.S) .build() ) ) .keySchema( Arrays.asList( KeySchemaElement.builder() .attributeName("id") .keyType(KeyType.HASH) .build() ) ) .provisionedThroughput(ProvisionedThroughput.builder() .readCapacityUnits(5L) .writeCapacityUnits(5L) .build() ) .tableName("myMusicTable") .build(); CreateTableResponse createTableResponse = dynamoDB.createTable(createTableRequest); String tableArn = createTableResponse.tableDescription().tableArn(); String tableName = createTableResponse.tableDescription().tableName();
  2. 為此資料表新增一個標籤。下列範例中,TagResource API 會將標籤鍵值對 Title: ProductManager 新增至 myMusicTable

    TagResourceRequest tagResourceRequest = TagResourceRequest.builder() .resourceArn(tableArn) .tags( Arrays.asList( Tag.builder() .key("Title") .value("ProductManager") .build() ) ) .build(); dynamoDB.tagResource(tagResourceRequest);
  3. 建立內嵌政策並將其新增至已連接 AmazonDynamoDBReadOnlyAccess AWS 受管政策的角色,如下列範例所示。

    JSON
    JSON
    { "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "dynamodb:PutItem", "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:ResourceTag/Title": "ProductManager" } } } ] }

    當附加至資料表的標籤鍵和值與政策中指定的標籤相符時,此政策允許在該資料表上執行 PutItem 動作。

  4. 依步驟 3 中說明的政策假設該角色。

  5. 使用 PutItem API 將項目新增至 myMusicTable

    HashMap<String, AttributeValue> info = new HashMap<>(); info.put("rating", AttributeValue.builder().s("9").build()); info.put("artists", AttributeValue.builder().ss(List.of("Acme Band","No One You Know").build()); info.put("release_date", AttributeValue.builder().s("2023-07-21").build()); HashMap<String, AttributeValue> itemValues = new HashMap<>(); itemValues.put("id", AttributeValue.builder().s("2023").build()); itemValues.put("title", AttributeValue.builder().s("Happy Day").build()); itemValues.put("info", AttributeValue.builder().m(info).build()); PutItemRequest putItemRequest = PutItemRequest.builder() .tableName(tableName) .item(itemValues) .build(); dynamoDB.putItem(putItemRequest);
  6. 掃描資料表以確認項目是否已成功新增。

    ScanRequest scanRequest = ScanRequest.builder() .tableName(tableName) .build(); ScanResponse scanResponse = dynamoDB.scan(scanRequest);
Using the 適用於 Python (Boto3) 的 AWS SDK
  1. 建立資料表。下列範例使用 CreateTable API 建立名為 myMusicTable 的資料表。

    create_table_response = ddb_client.create_table( AttributeDefinitions=[ { 'AttributeName': 'id', 'AttributeType': 'S' }, ], TableName='myMusicTable', KeySchema=[ { 'AttributeName': 'id', 'KeyType': 'HASH' }, ], ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 }, ) table_arn = create_table_response['TableDescription']['TableArn']
  2. 為此資料表新增一個標籤。下列範例中,TagResource API 會將標籤鍵值對 Title: ProductManager 新增至 myMusicTable

    tag_resouce_response = ddb_client.tag_resource( ResourceArn=table_arn, Tags=[ { 'Key': 'Title', 'Value': 'ProductManager' }, ] )
  3. 建立內嵌政策並將其新增至已連接 AmazonDynamoDBReadOnlyAccess AWS 受管政策的角色,如下列範例所示。

    JSON
    JSON
    { "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "dynamodb:PutItem", "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:ResourceTag/Title": "ProductManager" } } } ] }

    當附加至資料表的標籤鍵和值與政策中指定的標籤相符時,此政策允許在該資料表上執行 PutItem 動作。

  4. 依步驟 3 中說明的政策假設該角色。

  5. 使用 PutItem API 將項目新增至 myMusicTable

    put_item_response = client.put_item( TableName = 'myMusicTable' Item = { 'id': '2023', 'title': 'Happy Day', 'info': { 'rating': '9', 'artists': ['Acme Band','No One You Know'], 'release_date': '2023-07-21' } } )
  6. 掃描資料表以確認項目是否已成功新增。

    scan_response = client.scan( TableName='myMusicTable' )
未啟用 ABAC

如果您的 未啟用 ABAC AWS 帳戶,則 IAM 政策和 DynamoDB 資料表中的標籤條件不相符。因此,PutItem 動作會因 AmazonDynamoDBReadOnlyAccess 政策的影響而返回 AccessDeniedException

An error occurred (AccessDeniedException) when calling the PutItem operation: User: arn:aws:sts::123456789012:assumed-role/DynamoDBReadOnlyAccess/Alice is not authorized to perform: dynamodb:PutItem on resource: arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable because no identity-based policy allows the dynamodb:PutItem action.
啟用 ABAC

如果您的 已啟用 ABAC AWS 帳戶,則put-item動作會成功完成,並將新項目新增至您的資料表。這是因為當 IAM 政策與資料表中的標籤條件相符時,資料表中的內嵌政策會允許執行 PutItem 動作。

範例 2:使用 aws:RequestTag 允許動作

使用 aws:RequestTag/tag-key 條件金鑰,您可以比較請求中傳遞的標籤鍵值對與 IAM 政策中指定的鍵值對。例如,若標籤條件不相符,您可以使用 aws:RequestTag 來允許特定動作 (如 CreateTable)。請依下列步驟操作:

Using the AWS CLI
  1. 建立內嵌政策並將其新增至已連接 AmazonDynamoDBReadOnlyAccess AWS 受管政策的角色,如下列範例所示。

    JSON
    JSON
    { "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:RequestTag/Owner": "John" } } } ] }
  2. 建立一個包含 "Owner": "John" 標籤鍵值對的資料表。

    aws dynamodb create-table \ --attribute-definitions AttributeName=ID,AttributeType=S \ --key-schema AttributeName=ID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=1000,WriteCapacityUnits=500 \ --region us-east-1 \ --tags Key=Owner,Value=John \ --table-name myMusicTable
Using the 適用於 Python (Boto3) 的 AWS SDK
  1. 建立內嵌政策並將其新增至已連接 AmazonDynamoDBReadOnlyAccess AWS 受管政策的角色,如下列範例所示。

    JSON
    JSON
    { "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:RequestTag/Owner": "John" } } } ] }
  2. 建立一個包含 "Owner": "John" 標籤鍵值對的資料表。

    ddb_client = boto3.client('dynamodb') create_table_response = ddb_client.create_table( AttributeDefinitions=[ { 'AttributeName': 'id', 'AttributeType': 'S' }, ], TableName='myMusicTable', KeySchema=[ { 'AttributeName': 'id', 'KeyType': 'HASH' }, ], ProvisionedThroughput={ 'ReadCapacityUnits': 1000, 'WriteCapacityUnits': 500 }, Tags=[ { 'Key': 'Owner', 'Value': 'John' }, ], )
未啟用 ABAC

如果您的 未啟用 ABAC AWS 帳戶,則內嵌政策和 DynamoDB 資料表中的標籤條件不相符。因此,CreateTable 請求會失敗,資料表將不會建立。

An error occurred (AccessDeniedException) when calling the CreateTable operation: User: arn:aws:sts::123456789012:assumed-role/Admin/John is not authorized to perform: dynamodb:CreateTable on resource: arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable because no identity-based policy allows the dynamodb:CreateTable action.
啟用 ABAC

如果您的 已啟用 ABAC AWS 帳戶,您的資料表建立請求會成功完成。由於 "Owner": "John" 的標籤鍵值對存在於 CreateTable 請求中,內嵌政策允許使用者 John 執行 CreateTable 動作。

範例 3:使用 aws:TagKeys 拒絕動作

使用 aws:TagKeys 條件金鑰,您可以比較請求中的標籤金鑰與 IAM 政策中指定的金鑰。例如,若請求中未包含特定標籤鍵 not,您可以使用 aws:TagKeys 來拒絕特定動作 (如 CreateTable)。請依下列步驟操作:

Using the AWS CLI
  1. 客戶受管政策新增至已連接 AmazonDynamoDBFullAccess AWS 受管政策的角色,如下列範例所示。

    JSON
    JSON
    { "Version":"2012-10-17", "Statement": [ { "Effect": "Deny", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "Null": { "aws:TagKeys": "false" }, "ForAllValues:StringNotEquals": { "aws:TagKeys": "CostCenter" } } } ] }
  2. 以附加該政策的角色身分建立含有標籤鍵 Title 的資料表。

    aws dynamodb create-table \ --attribute-definitions AttributeName=ID,AttributeType=S \ --key-schema AttributeName=ID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=1000,WriteCapacityUnits=500 \ --region us-east-1 \ --tags Key=Title,Value=ProductManager \ --table-name myMusicTable
Using the 適用於 Python (Boto3) 的 AWS SDK
  1. 客戶受管政策新增至已連接 AmazonDynamoDBFullAccess AWS 受管政策的角色,如下列範例所示。

    JSON
    JSON
    { "Version":"2012-10-17", "Statement": [ { "Effect": "Deny", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "Null": { "aws:TagKeys": "false" }, "ForAllValues:StringNotEquals": { "aws:TagKeys": "CostCenter" } } } ] }
  2. 以附加該政策的角色身分建立含有標籤鍵 Title 的資料表。

    ddb_client = boto3.client('dynamodb') create_table_response = ddb_client.create_table( AttributeDefinitions=[ { 'AttributeName': 'id', 'AttributeType': 'S' }, ], TableName='myMusicTable', KeySchema=[ { 'AttributeName': 'id', 'KeyType': 'HASH' }, ], ProvisionedThroughput={ 'ReadCapacityUnits': 1000, 'WriteCapacityUnits': 500 }, Tags=[ { 'Key': 'Title', 'Value': 'ProductManager' }, ], )
未啟用 ABAC

如果您的 未啟用 ABAC AWS 帳戶,DynamoDB 不會將create-table命令中的標籤金鑰傳送至 IAM。Null 條件可確保當請求中沒有標籤鍵時,條件的評估結果為 false。由於 Deny 政策不相符,create-table 指令將成功完成。

啟用 ABAC

如果您的 已啟用 ABAC AWS 帳戶,則 create-table命令中傳遞的標籤金鑰會傳遞給 IAM。標籤鍵 Title 會根據 Deny 政策中所設定的條件型標籤鍵 CostCenter 進行比對。由於 StringNotEquals 運算子,Title 標籤鍵與 Deny 政策中的標籤鍵不相符。因此,CreateTable 動作將失敗,資料表不會建立。執行 create-table 指令時會傳回 AccessDeniedException

An error occurred (AccessDeniedException) when calling the CreateTable operation: User: arn:aws:sts::123456789012:assumed-role/DynamoFullAccessRole/ProductManager is not authorized to perform: dynamodb:CreateTable on resource: arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable with an explicit deny in an identity-based policy.