From f9eb910b62c048d523e56e7a15dfe3ed67d7aa67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Andr=C3=A9?= <92062874+matheusandre1@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:33:49 -0300 Subject: [PATCH] fix(dynamodb): accept ExpectedRevisionId NO_POLICY on a table with no resource policy (#8265) * fix(dynamodb): accept ExpectedRevisionId NO_POLICY on a table with no resource policy --- .../services/dynamodb/DynamoDbService.java | 66 +++++++++------- .../NativeDynamoDbJsonHandlerTest.java | 78 +++++++++++++++++++ 2 files changed, 116 insertions(+), 28 deletions(-) diff --git a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java index 211cb02e13..1594024e7e 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java @@ -136,6 +136,8 @@ public class DynamoDbService { private final ConcurrentHashMap> itemLocks = new ConcurrentHashMap<>(); // Held from an account quota check until the capacity it allowed is stored. private final Object capacityLock = new Object(); + // Held from a resource policy revision check until the policy it allowed is stored. + private final Object resourcePolicyLock = new Object(); // ClientRequestToken idempotency for TransactWriteItems. AWS retains tokens for // ~10 minutes; floci uses the same window. The cache entry stores a hash of the // request body so a replay with the same token but different parameters can be @@ -3442,20 +3444,26 @@ public class DynamoDbService { /** Result of a successful GetResourcePolicy call: the raw policy document and its revision id. */ public record ResourcePolicyResult(String policy, String revisionId) {} + private static final String NO_POLICY_REVISION_ID = "NO_POLICY"; + public String putResourcePolicy(String resourceArn, String policy, String expectedRevisionId, String region) { - TableDefinition table = findTableByArn(resourceArn, region); - if (expectedRevisionId != null - && !expectedRevisionId.equals(table.getResourcePolicyRevisionId())) { - throw new AwsException("PolicyNotFoundException", - "Policy with revision id " + expectedRevisionId + " does not exist for: " + resourceArn, 400); + synchronized (resourcePolicyLock) { + TableDefinition table = findTableByArn(resourceArn, region); + boolean revisionMatches = NO_POLICY_REVISION_ID.equals(expectedRevisionId) + ? table.getResourcePolicy() == null + : expectedRevisionId == null || expectedRevisionId.equals(table.getResourcePolicyRevisionId()); + if (!revisionMatches) { + throw new AwsException("PolicyNotFoundException", + "Policy with revision id " + expectedRevisionId + " does not exist for: " + resourceArn, 400); + } + String revisionId = UUID.randomUUID().toString(); + table.setResourcePolicy(policy); + table.setResourcePolicyRevisionId(revisionId); + String storageKey = regionKey(region, table.getTableName()); + tableStore.put(storageKey, table); + LOG.debugv("Put resource policy: {0}", resourceArn); + return revisionId; } - String revisionId = UUID.randomUUID().toString(); - table.setResourcePolicy(policy); - table.setResourcePolicyRevisionId(revisionId); - String storageKey = regionKey(region, table.getTableName()); - tableStore.put(storageKey, table); - LOG.debugv("Put resource policy: {0}", resourceArn); - return revisionId; } public ResourcePolicyResult getResourcePolicy(String resourceArn, String region) { @@ -3468,23 +3476,25 @@ public class DynamoDbService { } public String deleteResourcePolicy(String resourceArn, String expectedRevisionId, String region) { - TableDefinition table = findTableByArn(resourceArn, region); - if (table.getResourcePolicy() == null) { - throw new AwsException("PolicyNotFoundException", - "No resource policy found for: " + resourceArn, 400); + synchronized (resourcePolicyLock) { + TableDefinition table = findTableByArn(resourceArn, region); + if (table.getResourcePolicy() == null) { + throw new AwsException("PolicyNotFoundException", + "No resource policy found for: " + resourceArn, 400); + } + if (expectedRevisionId != null + && !expectedRevisionId.equals(table.getResourcePolicyRevisionId())) { + throw new AwsException("PolicyNotFoundException", + "Policy with revision id " + expectedRevisionId + " does not exist for: " + resourceArn, 400); + } + String revisionId = table.getResourcePolicyRevisionId(); + table.setResourcePolicy(null); + table.setResourcePolicyRevisionId(null); + String storageKey = regionKey(region, table.getTableName()); + tableStore.put(storageKey, table); + LOG.debugv("Deleted resource policy: {0}", resourceArn); + return revisionId; } - if (expectedRevisionId != null - && !expectedRevisionId.equals(table.getResourcePolicyRevisionId())) { - throw new AwsException("PolicyNotFoundException", - "Policy with revision id " + expectedRevisionId + " does not exist for: " + resourceArn, 400); - } - String revisionId = table.getResourcePolicyRevisionId(); - table.setResourcePolicy(null); - table.setResourcePolicyRevisionId(null); - String storageKey = regionKey(region, table.getTableName()); - tableStore.put(storageKey, table); - LOG.debugv("Deleted resource policy: {0}", resourceArn); - return revisionId; } private TableDefinition findTableByArn(String arn, String region) { diff --git a/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java b/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java index 6e091ab983..317df227ac 100644 --- a/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java +++ b/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java @@ -479,6 +479,84 @@ class NativeDynamoDbJsonHandlerTest { assertEquals("PolicyNotFoundException", ex.getErrorCode()); } + @Test + void putResourcePolicyAttachesTheFirstPolicyWhenNoPolicyIsExpected() throws Exception { + TableDefinition table = createUsersTable("eu-west-1"); + String tableArn = table.getTableArn(); + + ObjectNode putRequest = mapper.createObjectNode(); + putRequest.put("ResourceArn", tableArn); + putRequest.put("Policy", "{}"); + putRequest.put("ExpectedRevisionId", "NO_POLICY"); + + Response putResponse = handler.handle("PutResourcePolicy", putRequest, "eu-west-1"); + assertEquals(200, putResponse.getStatus()); + JsonNode putBody = mapper.convertValue(putResponse.getEntity(), JsonNode.class); + String revisionId = putBody.get("RevisionId").asText(); + assertFalse(revisionId.isBlank()); + + ObjectNode getRequest = mapper.createObjectNode(); + getRequest.put("ResourceArn", tableArn); + JsonNode getBody = mapper.convertValue( + handler.handle("GetResourcePolicy", getRequest, "eu-west-1").getEntity(), JsonNode.class); + assertEquals(revisionId, getBody.get("RevisionId").asText()); + } + + @Test + void putResourcePolicyRejectsNoPolicyWhenAPolicyIsAttached() throws Exception { + TableDefinition table = createUsersTable("eu-west-1"); + String tableArn = table.getTableArn(); + + ObjectNode putRequest = mapper.createObjectNode(); + putRequest.put("ResourceArn", tableArn); + putRequest.put("Policy", "{}"); + handler.handle("PutResourcePolicy", putRequest, "eu-west-1"); + + putRequest.put("ExpectedRevisionId", "NO_POLICY"); + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("PutResourcePolicy", putRequest, "eu-west-1")); + assertEquals("PolicyNotFoundException", ex.getErrorCode()); + } + + @Test + void putResourcePolicyLetsOneConcurrentNoPolicyAttachSucceed() throws Exception { + TableDefinition table = createUsersTable("eu-west-1"); + String tableArn = table.getTableArn(); + int callers = 8; + CountDownLatch start = new CountDownLatch(1); + ExecutorService executor = Executors.newFixedThreadPool(callers); + try { + List> attempts = new ArrayList<>(); + for (int i = 0; i < callers; i++) { + attempts.add(executor.submit(() -> { + ObjectNode putRequest = mapper.createObjectNode(); + putRequest.put("ResourceArn", tableArn); + putRequest.put("Policy", "{}"); + putRequest.put("ExpectedRevisionId", "NO_POLICY"); + start.await(); + try { + handler.handle("PutResourcePolicy", putRequest, "eu-west-1"); + return true; + } catch (AwsException expected) { + // The losing callers find the winner's policy already attached. + assertEquals("PolicyNotFoundException", expected.getErrorCode()); + return false; + } + })); + } + start.countDown(); + int attached = 0; + for (Future attempt : attempts) { + if (attempt.get()) { + attached++; + } + } + assertEquals(1, attached); + } finally { + executor.shutdownNow(); + } + } + @Test void putResourcePolicyRejectsInvalidResourceArn() { ObjectNode putRequest = mapper.createObjectNode();