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) {