Repository navigation
Description
DeletePolicy deletes a policy that is still attached, and every entity keeps an ARN pointing at nothing.
deletePolicy (emulator/iam_plugin.go:1382-1414) goes straight from the raw == nil → NoSuchEntity/404 check to p.state.Delete(...). There is no attachment check at all:
if raw == nil {
return iamErrorResponse("NoSuchEntity", ...
}
if err := p.state.Delete(goCtx, iamNamespace, iamPolicyKey(params.PolicyArn)); err != nil {
return nil, fmt.Errorf("delete policy: %w", err)
}
return iamXMLEmptyResponse("DeletePolicy"), nilAPI_DeletePolicy states the precondition in its second paragraph:
Before you can delete a managed policy, you must first detach the policy from all users, groups, and roles that it is attached to.
and publishes the refusal:
DeleteConflict — The request was rejected because it attempted to delete a resource that has attached subordinate entities. The error message describes these entities. HTTP Status Code: 409
Seven other IAM delete paths already check, so the pattern is in the same file: iam_plugin.go:369, :385, :671, :691, :905, :915, :2939. This one is the outlier.
What the silent delete leaves behind
An attach writes only the ARN onto {user,group,role}_policies:<acct>/<name>; nothing writes a back-reference onto the policy. So after a delete:
- every entity's attached-policy list still names the deleted ARN, and
ListAttachedUserPoliciesstill reports it; - the authorization evaluator loads no document for it, so the entity silently loses the permissions the policy granted — a test asserting a deny now passes for the wrong reason;
GetPolicyon the ARN answersNoSuchEntity, so the state is self-contradictory across two operations;- and since iam: GetPolicy reports the stored AttachmentCount (always 0) while ListPolicies derives it #847
AttachmentCountis derived from those same lists, a re-created policy of the same ARN inherits the stale attachments.
That last point is why this is worth doing after #847 rather than before: the count now makes the inconsistency observable through the API instead of only in state.
Two smaller findings on the same handler
- No ARN shape validation.
DeletePolicypublishesInvalidInput/400 andPolicyArncarries the usual 20–2048 length constraint, but the handler passes any non-empty string toiamPolicyKey.iamValidatePolicyARN(emulator/iam_policy_arn.go) is the existing check, already used by the three attach operations and, as of iam: Put{User,Role}PermissionsBoundary accepts a policy ARN that names no policy #846, by bothPut*PermissionsBoundary. - A bundled AWS managed ARN answers
NoSuchEntity. The handler reads state only, soDeletePolicyonarn:aws:iam::aws:policy/PowerUserAccessreports the policy does not exist whileGetPolicyon the same ARN returns it. What AWS answers for a delete of an AWS managed policy is not published on the page, so the right code here needs its own sourcing before anything is implemented; recorded so the two operations' disagreement is not discovered again from scratch.
Acceptance criteria
-
DeletePolicyanswersDeleteConflict/409 when the policy is attached to any user, group or role, with a message naming the attached entities as AWS's description says it does. - The refusal is derived from the same three
{user,group,role}_policies:prefixesiamPolicyAttachmentCountsreads, so a detach makes the delete succeed immediately and the two cannot disagree about what "attached" means. - A malformed
PolicyArnanswersInvalidInput/400 throughiamValidatePolicyARN, matching the attach operations. - Tests: attach → delete refuses with 409 and the policy is still readable through
GetPolicy; detach → delete succeeds; a policy attached to a group only is also refused (all three kinds, since one prefix scanned would pass a two-kind test); andAttachmentCountreads 0 for the deleted-then-recreated ARN rather than inheriting stale attachments. - What AWS answers for a delete of a bundled AWS managed ARN is either sourced from an AWS page or recorded as substrate's reading, not guessed.
Found while implementing #847 and #846 (v0.115.0); not fixed there, to keep that PR to the two reporting defects it is about.
Activity
scttfrdmn commentedon Sep 15, 2026
Verified against
mainat3503416before implementing. Every substantive claim in the body holds. Five corrections and one addition, recorded here so the PR does not carry them silently.Corrections
1. Line numbers have all drifted.
deletePolicyis atemulator/iam_plugin.go:1430-1462, not:1382-1414. The body's quoted code is verbatim-correct and there is still no attachment check and no ARN check.2. The seven sibling sites are exact in count, but every line number moved. All seven
DeleteConflict/409 arms are still there:iam_plugin.go:373DeleteUseriam_plugin.go:390DeleteUseriam_plugin.go:679DeleteRoleiam_plugin.go:700DeleteRoleiam_plugin.go:922DeleteGroupiam_plugin.go:931DeleteGroupiam_plugin.go:3063DeleteInstanceProfile:2939in the body is:3063today.3. Four of those seven do not name the entities, so "as the seven sibling arms already do" overstates the precedent by half. Every arm refusing over attached policies uses the bare
"Cannot delete entity, must detach all policies first."; only the three refusing over a membership name what to remove, andDeleteRole's carries the rationale explicitly: "The reference says 'The error message describes these entities', so name them: a caller has to know which profiles to detach from." This issue's AC 1 is right and the precedent for it is those three, not all seven. Worth stating because the naming is the part that takes work here — see 4.4.
iamPolicyAttachmentCountscannot satisfy AC 1 and AC 2 together as written. It reads exactly the right three prefixes (iam_list_policies.go:213-234), but it accumulatescounts[arn]++and discards which entity and which kind each membership came from, so it can answer whether a policy is attached and not to what. AC 2's requirement — that the refusal and the count cannot disagree about what "attached" means — is met by a sibling helper over the same threeiamAttachedPoliciesPrefixkeys and the sameloadPolicyList, returning the entity names per kind. Not by changingiamPolicyAttachmentCounts, whose signature five callers depend on and whose per-request hoisting is load-bearing forGetPolicyandListPolicies.5.
ListEntitiesForPolicyis not implemented, and that raises the stakes on AC 1. A repo-wide search finds it only in the generated authz data — no dispatch arm, no handler.API_DeletePolicy's own description tells the caller to "useListEntitiesForPolicy" to find what to detach, so in substrate the 409 message is the only way a caller can learn it. That is why naming the entities is not a nicety here.Addition: AC 4's last clause is discharged structurally, not by an assertion
"
AttachmentCountreads 0 for the deleted-then-recreated ARN rather than inheriting stale attachments" becomes true by construction once the refusal lands: a successful delete now implies zero attachments, so there is nothing stale left to inherit. The test is still worth writing — it asserts the invariant rather than the mechanism — but it is a consequence of AC 1, not separate work.The two things being recorded rather than implemented
The version precondition is vacuous. AWS's description requires deleting all non-default versions first, but publishes no error code for it —
DeleteConflict's own text is about "attached subordinate entities" — and substrate models exactly one version per policy (iam_policy_versions.go:21-24). So it gets a comment, not code.AC 5, the bundled AWS-managed ARN: recorded as substrate's reading, because no AWS source states the code.
API_DeletePolicyis silent. The strongest published statement is "You cannot change the permissions defined in AWS managed policies" (Managed policies and inline policies), which establishes that the customer does not administer them but publishes no code and does not mention deletion. So the guard ships stated as substrate's reading, using theiamPolicyARNIsAWSManagedpredicate that already exists (iam_policy_arn.go:72-74) — what matters for this issue is thatDeletePolicystops answeringNoSuchEntityfor an ARNGetPolicyresolves from the catalog (iam_plugin.go:1402), i.e. that the two operations stop contradicting each other about whether the policy exists.AC 3 confirmed as stated
iamValidatePolicyARNexists and is applied at four call sites serving five operations —AttachUserPolicy(iam_plugin.go:1002),AttachRolePolicy(:1140),AttachGroupPolicy(iam_groups.go:174), andputPermissionsBoundary(:2232), which bothPutUserPermissionsBoundaryandPutRolePermissionsBoundaryroute through.DeletePolicypasses any non-empty string straight toiamPolicyKey. The body's claim is exact.Also noted for a separate issue rather than folded in: the three detach operations —
detachUserPolicy(:1044),detachRolePolicy(:1182),detachGroupPolicy(iam_groups.go:216) — do not validate the ARN either, so a malformed one is accepted and silently matches nothing.fix(iam): an attached policy cannot be deleted (#853) (#876)