mirror of
https://github.com/floci-io/floci.git
synced 2026-11-30 17:05:21 -05:00
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.