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
This commit is contained in:
Matheus André authored and GitHub committed 2026-11-14 01:33:49 -04:00
1 parent ff5b6df114
commit f9eb910b62
2 files changed
+116 -28

No files matched your search

@@ -136,6 +136,8 @@ public class DynamoDbService {
private final ConcurrentHashMap<String, ConcurrentHashMap<String, ReentrantLock>> 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) {
@@ -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<Future<Boolean>> 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<Boolean> attempt : attempts) {
if (attempt.get()) {
attached++;
}
}
assertEquals(1, attached);
} finally {
executor.shutdownNow();
}
}
@Test
void putResourcePolicyRejectsInvalidResourceArn() {
ObjectNode putRequest = mapper.createObjectNode();