mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): make entity stores thread-safe under concurrent mutation (#1509)
The storage backend hands every caller the same entity instance, and IamService mutates that entity's collections in place before storing it back (getInlinePolicies().put(...), getAttachedPolicyArns().add(...), etc.) with no synchronization. When two requests touch the same entity at once - e.g. Terraform applying many aws_iam_role_policy resources under -parallelism - the plain HashMap/ArrayList lose entries and corrupt their internal tables. PutRolePolicy returns success but the immediate GetRolePolicy then reports NoSuchEntity (so the provider's create-waiter exhausts and the apply fails), and listRolePolicies can throw ArrayIndexOutOfBoundsException off the corrupted map. Make the shared collections concurrency-safe across all IAM entities: ConcurrentHashMap for tag and inline-policy maps, CopyOnWriteArrayList for the user/group/role/instance-profile lists, with defensive setters so a Jackson rehydrate from disk can't swap a plain collection back in. Policy versions keep insertion order via a synchronized LinkedHashMap, and the compound check-then-act paths that a thread-safe collection alone can't make atomic are guarded explicitly: createPolicyVersion (version id derived from size()+1), delete/setDefaultPolicyVersion, listPolicyVersions, and the single-role cap in addRoleToInstanceProfile. Add concurrency tests that drive each collection from many threads and assert no entry is lost or corrupted; they fail reliably before the fix.
This commit is contained in:
1 parent
0b28554626
commit
976f819e5c
8 files changed
+478
-61
No files matched your search
@@ -0,0 +1,260 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamPolicy;
|
||||
import org.junit.jupiter.api.DynamicTest;
|
||||
import org.junit.jupiter.api.TestFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.IntConsumer;
|
||||
import java.util.function.ToIntFunction;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Concurrent mutation of every shared IAM entity collection (tags, inline policies,
|
||||
* attachments, group membership, policy versions, instance-profile roles) must not lose
|
||||
* or corrupt entries. Each scenario runs over many trials to surface the race reliably.
|
||||
*/
|
||||
class IamConcurrencyTest {
|
||||
|
||||
private static final int TRIALS = 200;
|
||||
private static final int N = 24;
|
||||
private static final int THREADS = 8;
|
||||
|
||||
private static IamService newIamService() {
|
||||
return new IamService(
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new RegionResolver("eu-central-1", "000000000000"), false);
|
||||
}
|
||||
|
||||
/** Sets up one entity on the given service and returns the i-th concurrent mutation. */
|
||||
private interface RaceSetup {
|
||||
IntConsumer prepare(IamService iam);
|
||||
}
|
||||
|
||||
@TestFactory
|
||||
Stream<DynamicTest> everyIamCollectionStaysRaceFree() {
|
||||
record Scenario(String name, RaceSetup setup, ToIntFunction<IamService> finalCount) {}
|
||||
|
||||
List<Scenario> scenarios = List.of(
|
||||
new Scenario("role.tags", iam -> {
|
||||
iam.createRole("r", "/", "{}", null, 3600, null);
|
||||
return i -> iam.tagRole("r", java.util.Map.of("k" + i, "v" + i));
|
||||
}, iam -> iam.getRole("r").getTags().size()),
|
||||
|
||||
new Scenario("role.inlinePolicies", iam -> {
|
||||
iam.createRole("r", "/", "{}", null, 3600, null);
|
||||
return i -> iam.putRolePolicy("r", "p" + i, "{\"S\":" + i + "}");
|
||||
}, iam -> iam.listRolePolicies("r").size()),
|
||||
|
||||
new Scenario("role.attachedPolicyArns", iam -> {
|
||||
iam.createRole("r", "/", "{}", null, 3600, null);
|
||||
String[] arns = createPolicies(iam, "rp");
|
||||
return i -> iam.attachRolePolicy("r", arns[i]);
|
||||
}, iam -> iam.listAttachedRolePolicies("r", null).size()),
|
||||
|
||||
new Scenario("user.tags", iam -> {
|
||||
iam.createUser("u", "/");
|
||||
return i -> iam.tagUser("u", java.util.Map.of("k" + i, "v" + i));
|
||||
}, iam -> iam.getUser("u").getTags().size()),
|
||||
|
||||
new Scenario("user.inlinePolicies", iam -> {
|
||||
iam.createUser("u", "/");
|
||||
return i -> iam.putUserPolicy("u", "p" + i, "{\"S\":" + i + "}");
|
||||
}, iam -> iam.listUserPolicies("u").size()),
|
||||
|
||||
new Scenario("user.attachedPolicyArns", iam -> {
|
||||
iam.createUser("u", "/");
|
||||
String[] arns = createPolicies(iam, "up");
|
||||
return i -> iam.attachUserPolicy("u", arns[i]);
|
||||
}, iam -> iam.listAttachedUserPolicies("u", null).size()),
|
||||
|
||||
new Scenario("user.groupNames", iam -> {
|
||||
iam.createUser("u", "/");
|
||||
for (int i = 0; i < N; i++) iam.createGroup("g" + i, "/");
|
||||
return i -> iam.addUserToGroup("g" + i, "u");
|
||||
}, iam -> iam.getUser("u").getGroupNames().size()),
|
||||
|
||||
new Scenario("group.userNames", iam -> {
|
||||
iam.createGroup("g", "/");
|
||||
for (int i = 0; i < N; i++) iam.createUser("u" + i, "/");
|
||||
return i -> iam.addUserToGroup("g", "u" + i);
|
||||
}, iam -> iam.getGroup("g").getUserNames().size()),
|
||||
|
||||
new Scenario("group.inlinePolicies", iam -> {
|
||||
iam.createGroup("g", "/");
|
||||
return i -> iam.putGroupPolicy("g", "p" + i, "{\"S\":" + i + "}");
|
||||
}, iam -> iam.listGroupPolicies("g").size()),
|
||||
|
||||
new Scenario("group.attachedPolicyArns", iam -> {
|
||||
iam.createGroup("g", "/");
|
||||
String[] arns = createPolicies(iam, "gp");
|
||||
return i -> iam.attachGroupPolicy("g", arns[i]);
|
||||
}, iam -> iam.listAttachedGroupPolicies("g", null).size()),
|
||||
|
||||
new Scenario("policy.tags", iam -> {
|
||||
IamPolicy p = iam.createPolicy("pol", "/", null, "{}", null);
|
||||
return i -> iam.tagPolicy(p.getArn(), java.util.Map.of("k" + i, "v" + i));
|
||||
}, iam -> iam.getPolicy(onlyPolicyArn(iam)).getTags().size())
|
||||
);
|
||||
|
||||
return scenarios.stream().map(s -> DynamicTest.dynamicTest(s.name(), () -> runRace(s.name(), s.setup(), s.finalCount())));
|
||||
}
|
||||
|
||||
private void runRace(String name, RaceSetup setup, ToIntFunction<IamService> finalCount) throws Exception {
|
||||
ExecutorService pool = Executors.newFixedThreadPool(THREADS);
|
||||
try {
|
||||
for (int trial = 0; trial < TRIALS; trial++) {
|
||||
IamService iam = newIamService();
|
||||
IntConsumer op = setup.prepare(iam);
|
||||
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
CountDownLatch done = new CountDownLatch(N);
|
||||
List<Throwable> errors = new CopyOnWriteArrayList<>();
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int idx = i;
|
||||
pool.submit(() -> {
|
||||
try {
|
||||
start.await();
|
||||
op.accept(idx);
|
||||
} catch (Throwable t) {
|
||||
errors.add(t);
|
||||
} finally {
|
||||
done.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
start.countDown();
|
||||
assertTrue(done.await(30, TimeUnit.SECONDS), name + ": workers stalled (HashMap CPU spin)");
|
||||
|
||||
int actual;
|
||||
try {
|
||||
actual = finalCount.applyAsInt(iam);
|
||||
} catch (Throwable t) {
|
||||
fail(name + " trial " + trial + ": reading final state threw (corrupted collection): " + t);
|
||||
return;
|
||||
}
|
||||
if (actual != N || !errors.isEmpty()) {
|
||||
fail(name + " trial " + trial + ": expected " + N + " entries, got " + actual
|
||||
+ "; mutation errors=" + errors.size()
|
||||
+ (errors.isEmpty() ? "" : " e.g. " + errors.get(0)));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyVersionsStayRaceFree() throws Exception {
|
||||
int trials = 400;
|
||||
int extraVersions = 4; // v1 is seeded by createPolicy; cap is 5
|
||||
ExecutorService pool = Executors.newFixedThreadPool(extraVersions);
|
||||
try {
|
||||
for (int trial = 0; trial < trials; trial++) {
|
||||
IamService iam = newIamService();
|
||||
IamPolicy p = iam.createPolicy("pol", "/", null, "{}", null);
|
||||
String arn = p.getArn();
|
||||
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
CountDownLatch done = new CountDownLatch(extraVersions);
|
||||
List<Throwable> errors = new CopyOnWriteArrayList<>();
|
||||
for (int i = 0; i < extraVersions; i++) {
|
||||
pool.submit(() -> {
|
||||
try {
|
||||
start.await();
|
||||
iam.createPolicyVersion(arn, "{\"v\":true}", false);
|
||||
} catch (Throwable t) {
|
||||
errors.add(t);
|
||||
} finally {
|
||||
done.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
start.countDown();
|
||||
assertTrue(done.await(30, TimeUnit.SECONDS), "version workers stalled");
|
||||
|
||||
List<?> versions = iam.listPolicyVersions(arn);
|
||||
Set<String> ids = new HashSet<>();
|
||||
iam.getPolicy(arn).getVersions().keySet().forEach(ids::add);
|
||||
|
||||
if (!errors.isEmpty() || versions.size() != 1 + extraVersions || ids.size() != 1 + extraVersions) {
|
||||
fail("policy.versions trial " + trial + ": expected " + (1 + extraVersions)
|
||||
+ " distinct versions, got list=" + versions.size() + " distinctIds=" + ids.size()
|
||||
+ "; errors=" + errors.size() + (errors.isEmpty() ? "" : " e.g. " + errors.get(0)));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void instanceProfileRoleNamesStayRaceFree() throws Exception {
|
||||
int trials = 400;
|
||||
int racers = 8;
|
||||
ExecutorService pool = Executors.newFixedThreadPool(racers);
|
||||
try {
|
||||
for (int trial = 0; trial < trials; trial++) {
|
||||
IamService iam = newIamService();
|
||||
iam.createInstanceProfile("prof", "/");
|
||||
for (int i = 0; i < racers; i++) iam.createRole("role" + i, "/", "{}", null, 3600, null);
|
||||
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
CountDownLatch done = new CountDownLatch(racers);
|
||||
List<Throwable> unexpected = new CopyOnWriteArrayList<>();
|
||||
for (int i = 0; i < racers; i++) {
|
||||
int idx = i;
|
||||
pool.submit(() -> {
|
||||
try {
|
||||
start.await();
|
||||
iam.addRoleToInstanceProfile("prof", "role" + idx);
|
||||
} catch (io.github.hectorvent.floci.core.common.AwsException expected) {
|
||||
// LimitExceeded once one role is in
|
||||
} catch (Throwable t) {
|
||||
unexpected.add(t);
|
||||
} finally {
|
||||
done.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
start.countDown();
|
||||
assertTrue(done.await(30, TimeUnit.SECONDS), "profile workers stalled");
|
||||
|
||||
int roles = iam.getInstanceProfile("prof").getRoleNames().size();
|
||||
if (roles != 1 || !unexpected.isEmpty()) {
|
||||
fail("instanceProfile.roleNames trial " + trial + ": cap violated/corrupted, roleNames="
|
||||
+ roles + " unexpectedErrors=" + unexpected.size());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
private static String[] createPolicies(IamService iam, String prefix) {
|
||||
String[] arns = new String[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
arns[i] = iam.createPolicy(prefix + "-" + i, "/", null, "{}", null).getArn();
|
||||
}
|
||||
return arns;
|
||||
}
|
||||
|
||||
private static String onlyPolicyArn(IamService iam) {
|
||||
return iam.listPolicies(null, null).get(0).getArn();
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Concurrent PutRolePolicy on the same role must not lose or corrupt inline policies:
|
||||
* a policy that PutRolePolicy acked has to stay visible to a later GetRolePolicy.
|
||||
*/
|
||||
class IamRolePolicyConcurrencyTest {
|
||||
|
||||
private static IamService newIamService() {
|
||||
return new IamService(
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new RegionResolver("eu-central-1", "000000000000"),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentPutRolePolicyOnSameRoleStaysReadable() throws Exception {
|
||||
int trials = 300;
|
||||
int policiesPerTrial = 32;
|
||||
int threads = 8;
|
||||
ExecutorService pool = Executors.newFixedThreadPool(threads);
|
||||
|
||||
try {
|
||||
for (int trial = 0; trial < trials; trial++) {
|
||||
IamService iam = newIamService();
|
||||
String roleName = "repro-role-" + trial;
|
||||
iam.createRole(roleName, "/", "{}", null, 3600, null);
|
||||
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
CountDownLatch done = new CountDownLatch(policiesPerTrial);
|
||||
List<String> lostOnReadBack = new CopyOnWriteArrayList<>();
|
||||
List<Throwable> corruption = new CopyOnWriteArrayList<>();
|
||||
|
||||
for (int i = 0; i < policiesPerTrial; i++) {
|
||||
String policyName = "inline-" + i;
|
||||
String doc = "{\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"secretsmanager:GetSecretValue\","
|
||||
+ "\"Resource\":\"*\",\"Sid\":\"" + policyName + "\"}]}";
|
||||
pool.submit(() -> {
|
||||
try {
|
||||
start.await();
|
||||
iam.putRolePolicy(roleName, policyName, doc);
|
||||
try {
|
||||
iam.getRolePolicy(roleName, policyName);
|
||||
} catch (AwsException e) {
|
||||
lostOnReadBack.add(policyName);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
corruption.add(t);
|
||||
} finally {
|
||||
done.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
start.countDown();
|
||||
assertTrue(done.await(30, TimeUnit.SECONDS),
|
||||
"trial " + trial + ": workers stalled (possible HashMap CPU spin under concurrent resize)");
|
||||
|
||||
int finalCount;
|
||||
try {
|
||||
finalCount = iam.getRole(roleName).getInlinePolicies().size();
|
||||
} catch (Throwable t) {
|
||||
finalCount = -1;
|
||||
corruption.add(t);
|
||||
}
|
||||
|
||||
if (!lostOnReadBack.isEmpty() || !corruption.isEmpty() || finalCount != policiesPerTrial) {
|
||||
System.out.println("[repro] FAILED on trial " + trial);
|
||||
System.out.println("[repro] NoSuchEntity on read-back : " + lostOnReadBack.size()
|
||||
+ " " + sorted(lostOnReadBack));
|
||||
System.out.println("[repro] corruption exceptions : " + corruption.size()
|
||||
+ (corruption.isEmpty() ? "" : " e.g. " + corruption.get(0)));
|
||||
System.out.println("[repro] inline policies retained : " + finalCount + " / " + policiesPerTrial);
|
||||
fail("Concurrent PutRolePolicy on one role lost/corrupted inline policies "
|
||||
+ "(unsynchronized read-modify-write in IamService.putRolePolicy). "
|
||||
+ "NoSuchEntity reads=" + sorted(lostOnReadBack)
|
||||
+ ", retained=" + finalCount + "/" + policiesPerTrial
|
||||
+ ", corruption=" + corruption.size());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> sorted(List<String> in) {
|
||||
List<String> copy = new java.util.ArrayList<>(in);
|
||||
Collections.sort(copy);
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user