fix(dynamodb): give resource-tag conditions the tags of the table a request names (#8623)

The generated request resources give DynamoDB's table, index and stream
types aws:ResourceTag/ keys, but DynamoDB had no StoredResourceArns, so
a condition on a table's tags saw none. DynamoDbStoredResourceArns now
answers from the selected backend: the native one reads the table
stored under the ARN in the account and Region it names, a
pre-multi-account one of the default account in place, matched on the
whole ARN; the DynamoDB Local one reads the tags Floci records for it
while DynamoDB Local still has the table. An index ARN gets its table's
tags when the table holds that index; a stream ARN gets none. Native tag
writes, CreateTable's included, now replace the map whole under one
lock, keeping tags already put, and the field is volatile.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 15:02:13 -04:00
1 parent 9de6cd74b8
commit 3d6f59c064
14 files changed
+272 -21

No files matched your search

@@ -169,6 +169,17 @@ class NativeDynamoDbTableServiceTest {
assertNoTableAndNoStream();
}
@Test
void creationTagsKeepATagATagResourceAlreadyPutOnTheStoredTable() {
TableDefinition table = dynamoDbService.createTable(TABLE, List.of(new KeySchemaElement("id", "HASH")),
List.of(new AttributeDefinition("id", "S")), 5L, 5L, REGION);
dynamoDbService.tagResource(table.getTableArn(), Map.of("stage", "prod", "team", "web"), REGION);
dynamoDbService.putCreationTags(table, Map.of("team", "data", "env", "test"));
assertEquals(Map.of("stage", "prod", "team", "web", "env", "test"), table.getTags());
}
@Test
void createSettingsSurviveARestart() {
Map<String, String> tags = new LinkedHashMap<>();
@@ -426,6 +426,31 @@ class DynamoDbLocalBackendTest {
assertEquals(Map.of("env", "dev"), backend.listTagsOfResource(SCOPE, ORDERS_ARN));
}
@Test
void storedTagsAreTheTagsHeldForTheTableTheArnNamesAndForAnIndexItHolds() throws Exception {
answer("DescribeTable", 200, "{\"Table\":{\"TableName\":\"orders\",\"TableArn\":\"" + LOCAL
+ "table/orders\",\"TableStatus\":\"ACTIVE\","
+ "\"GlobalSecondaryIndexes\":[{\"IndexName\":\"byCustomer\"}]}}");
backend.tagResource(SCOPE, ORDERS_ARN, Map.of("team", "data"));
assertEquals(Map.of("team", "data"), backend.storedTags(ORDERS_ARN));
assertEquals(Map.of("team", "data"), backend.storedTags(ORDERS_ARN + "/index/byCustomer"));
assertEquals(Map.of(), backend.storedTags(ORDERS_ARN + "/index/byStatus"));
assertEquals(Map.of(), backend.storedTags(PUBLIC + STREAM_RESOURCE));
assertEquals(Map.of(), backend.storedTags(ORDERS_ARN.replace(ACCOUNT, "111122223333")));
assertEquals(Map.of(), backend.storedTags(ORDERS_ARN.replace(REGION, "us-east-1")));
assertEquals(Map.of(), backend.storedTags("arn:aws-cn:dynamodb:" + REGION + ":" + ACCOUNT + ":table/orders"));
}
@Test
void storedTagsAreNoneForATableDynamoDbLocalNoLongerHas() throws Exception {
tags.putForAccount(ACCOUNT, REGION + "/orders", Map.of("team", "data"));
answer("DescribeTable", 400, "{\"__type\":\"com.amazonaws.dynamodb.v20120810#ResourceNotFoundException\","
+ "\"message\":\"Requested resource not found\"}");
assertEquals(Map.of(), backend.storedTags(ORDERS_ARN));
}
@Test
void deleteTableDropsTags() throws Exception {
tags.putForAccount(ACCOUNT, REGION + "/orders", Map.of("env", "dev"));
@@ -180,6 +180,37 @@ class DynamoDbNativeBackendTest {
assertEquals(json("{\"id\":{\"S\":\"k\"},\"n\":{\"N\":\"5\"}}"), backend.getItem(SCOPE, TABLE, key));
}
@Test
void storedTagsAreTheTablesForItAndItsIndexesInTheArnsAccountAndRegionAndTaggingPutsANewMap() throws Exception {
call(Api.DYNAMODB, "CreateTable", json("""
{"TableName":"%s",
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"},
{"AttributeName":"customer","AttributeType":"S"}],
"GlobalSecondaryIndexes":[{"IndexName":"byCustomer",
"KeySchema":[{"AttributeName":"customer","KeyType":"HASH"}],
"Projection":{"ProjectionType":"ALL"}}],
"BillingMode":"PAY_PER_REQUEST",
"Tags":[{"Key":"team","Value":"data"}]}
""".formatted(TABLE)));
String arn = backend.describeTable(SCOPE, TABLE).getTableArn();
Map<String, String> held = backend.listTagsOfResource(SCOPE, arn);
assertEquals(Map.of("team", "data"), backend.storedTags(arn));
assertEquals(Map.of("team", "data"), backend.storedTags(arn + "/index/byCustomer"));
assertEquals(Map.of(), backend.storedTags(arn + "/index/byStatus"));
assertEquals(Map.of(), backend.storedTags(arn + "/stream/2026-11-16T00:00:00.000"));
assertEquals(Map.of(), backend.storedTags(arn.replace(ACCOUNT, "111122223333")));
assertEquals(Map.of(), backend.storedTags(arn.replace(REGION, "us-west-2")));
assertEquals(Map.of(), backend.storedTags(arn.replace("arn:aws:", "arn:aws-cn:")));
backend.tagResource(SCOPE, arn, Map.of("stage", "prod"));
backend.untagResource(SCOPE, arn, List.of("team"));
assertEquals(Map.of("stage", "prod"), backend.storedTags(arn + "/index/byCustomer"));
assertEquals(Map.of("team", "data"), held);
}
@Test
void typedTimeToLiveIsWhatDescribeTimeToLiveReportsUnderTheScopesAccount() throws Exception {
Scope owner = new Scope("111122223333", REGION);
@@ -169,6 +169,12 @@ public final class RecordingDynamoDbBackend implements DynamoDbItemAccess, Dynam
record("untagResource", scope);
}
@Override
public Map<String, String> storedTags(String arn) {
invocations.add(new Invocation("storedTags", null, null));
return Map.of();
}
@Override
public TableDefinition applyReplicaUpdates(Scope scope, String tableName, List<String> addRegions,
List<String> removeRegions) {
@@ -2133,6 +2133,42 @@ class IamRequestResourceEnforcementIntegrationTest {
+ otherRegistry + "\"}}"), "glue:GetRegistry");
}
/**
* An {@code aws:ResourceTag} condition reads the tags DynamoDB holds on the table a request names, as created and
* as tagged later, so an allow on one team's tag lets the caller describe that team's tables and not another's.
*/
@Test
void aDynamoDbResourceTagConditionReadsTheTagsOfTheTable() {
String tagged = dynamoDbTable("res-tag-ddb-" + suffix(), "data");
String other = dynamoDbTable("res-tag-ddb-" + suffix(), "web");
User user = user("res-ddb-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"dynamodb:DescribeTable\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
send(user, "dynamodb", "DynamoDB_20120810.DescribeTable", "1.0", "{\"TableName\":\"" + tagged + "\"}")
.statusCode(200);
refusedJson(send(user, "dynamodb", "DynamoDB_20120810.DescribeTable", "1.0", "{\"TableName\":\"" + other
+ "\"}"), "dynamodb:DescribeTable");
adminJson("dynamodb", "DynamoDB_20120810.TagResource", "1.0").body("{\"ResourceArn\":\"arn:aws:dynamodb:"
+ REGION + ":" + ACCOUNT + ":table/" + other + "\",\"Tags\":[{\"Key\":\"team\",\"Value\":\"data\"}]}")
.post("/").then().statusCode(200);
send(user, "dynamodb", "DynamoDB_20120810.DescribeTable", "1.0", "{\"TableName\":\"" + other + "\"}")
.statusCode(200);
}
/** A DynamoDB table of the name, keyed on {@code id} and tagged with the team, deleted afterwards. */
private String dynamoDbTable(String name, String team) {
adminJson("dynamodb", "DynamoDB_20120810.CreateTable", "1.0").body("{\"TableName\":\"" + name + "\","
+ "\"KeySchema\":[{\"AttributeName\":\"id\",\"KeyType\":\"HASH\"}],"
+ "\"AttributeDefinitions\":[{\"AttributeName\":\"id\",\"AttributeType\":\"S\"}],"
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"Tags\":[{\"Key\":\"team\",\"Value\":\"" + team + "\"}]}")
.post("/").then().statusCode(200);
cleanup.register(() -> adminJson("dynamodb", "DynamoDB_20120810.DeleteTable", "1.0")
.body("{\"TableName\":\"" + name + "\"}").post("/"));
return name;
}
/**
* What Glue's batch deletes name, the tables of {@code TablesToDelete} and the connections of
* {@code ConnectionNameList}, is checked one by one, so a deny scoped to one of them holds under an allow on