From d6617aeca37651e12d039dc22d3663a1639f4b79 Mon Sep 17 00:00:00 2001 From: Allen Sanborn Date: Sat, 31 Oct 2026 20:52:54 -0500 Subject: [PATCH] fix(iam): serialize managed policy attach/detach so parallel detaches are not lost (#7144) --- .../floci/services/iam/IamService.java | 27 +++++++---- .../services/iam/IamConcurrencyTest.java | 45 +++++++++++++++++++ 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java index d5439cc7de..3cacc7d220 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java @@ -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) { diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamConcurrencyTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamConcurrencyTest.java index 5fe4c9ccfd..9e43f90886 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamConcurrencyTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamConcurrencyTest.java @@ -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(); }