fix(iam): serialize managed policy attach/detach so parallel detaches are not lost (#7144)

This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-31 21:52:54 -04:00
1 parent 48b19f76f3
commit d6617aeca3
2 files changed
+63 -9

No files matched your search

@@ -176,6 +176,13 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
*/
private final Object tagLock = new Object();
/**
* Guards the read-modify-write of a customer managed policy's AttachmentCount. Attaching or
* detaching one policy on several principals at once would otherwise lose updates, leaving a
* fully detached policy that DeletePolicy still refuses as attached.
*/
private final Object policyAttachmentCountLock = new Object();
/** CSPRNG for long-term secret access keys; ordinary resource IDs keep using {@link ThreadLocalRandom}. */
private final SecureRandom secureRandom = new SecureRandom();
@@ -1708,21 +1715,23 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
}
private void incrementCustomerManagedPolicyAttachmentCount(IamPolicy policy) {
if (AwsManagedPolicies.isManagedPolicyArn(policy.getArn())) {
return;
}
policy.setAttachmentCount(policy.getAttachmentCount() + 1);
policies.put(policy.getArn(), policy);
adjustCustomerManagedPolicyAttachmentCount(policy.getArn(), 1);
}
private void decrementCustomerManagedPolicyAttachmentCount(String policyArn) {
adjustCustomerManagedPolicyAttachmentCount(policyArn, -1);
}
private void adjustCustomerManagedPolicyAttachmentCount(String policyArn, int delta) {
if (AwsManagedPolicies.isManagedPolicyArn(policyArn)) {
return;
}
policies.get(policyArn).ifPresent(policy -> {
policy.setAttachmentCount(Math.max(0, policy.getAttachmentCount() - 1));
policies.put(policyArn, policy);
});
synchronized (policyAttachmentCountLock) {
policies.get(policyArn).ifPresent(policy -> {
policy.setAttachmentCount(Math.max(0, policy.getAttachmentCount() + delta));
policies.put(policyArn, policy);
});
}
}
private void rejectIfAwsManaged(String policyArn) {
@@ -110,6 +110,41 @@ class IamConcurrencyTest {
return i -> iam.attachGroupPolicy("g", arns[i]);
}, iam -> iam.listAttachedGroupPolicies("g", null).size()),
// One policy across many principals: every attach/detach updates the
// policy's AttachmentCount, so a lost update leaves DeletePolicy refusing.
new Scenario("policy.attachmentCount.attachGroup", iam -> {
String arn = iam.createPolicy("pol", "/", null, "{}", null).getArn();
for (int i = 0; i < N; i++) iam.createGroup("g" + i, "/");
return i -> iam.attachGroupPolicy("g" + i, arn);
}, iam -> iam.getPolicy(onlyPolicyArn(iam)).getAttachmentCount()),
new Scenario("policy.attachmentCount.detachGroup", iam -> {
String arn = iam.createPolicy("pol", "/", null, "{}", null).getArn();
for (int i = 0; i < N; i++) {
iam.createGroup("g" + i, "/");
iam.attachGroupPolicy("g" + i, arn);
}
return i -> iam.detachGroupPolicy("g" + i, arn);
}, IamConcurrencyTest::detachedThenDeleted),
new Scenario("policy.attachmentCount.detachUser", iam -> {
String arn = iam.createPolicy("pol", "/", null, "{}", null).getArn();
for (int i = 0; i < N; i++) {
iam.createUser("u" + i, "/");
iam.attachUserPolicy("u" + i, arn);
}
return i -> iam.detachUserPolicy("u" + i, arn);
}, IamConcurrencyTest::detachedThenDeleted),
new Scenario("policy.attachmentCount.detachRole", iam -> {
String arn = iam.createPolicy("pol", "/", null, "{}", null).getArn();
for (int i = 0; i < N; i++) {
iam.createRole("r" + i, "/", "{}", null, 3600, null);
iam.attachRolePolicy("r" + i, arn);
}
return i -> iam.detachRolePolicy("r" + i, arn);
}, IamConcurrencyTest::detachedThenDeleted),
new Scenario("policy.tags", iam -> {
IamPolicy p = iam.createPolicy("pol", "/", null, "{}", null);
return i -> iam.tagPolicy(p.getArn(), Map.of("k" + i, "v" + i));
@@ -578,6 +613,16 @@ class IamConcurrencyTest {
return arns;
}
/** N when every detach landed: the count read back is zero and DeletePolicy succeeds. */
private static int detachedThenDeleted(IamService iam) {
String arn = onlyPolicyArn(iam);
int remaining = iam.getPolicy(arn).getAttachmentCount();
if (remaining == 0) {
iam.deletePolicy(arn);
}
return N - remaining;
}
private static String onlyPolicyArn(IamService iam) {
return iam.listPolicies(null, null).get(0).getArn();
}