Skip to content

fix(iam): an attached policy cannot be deleted (#853) - #876

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/853-delete-policy-conflict
Sep 15, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/853-delete-policy-conflict

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Why

deletePolicy went straight from the NoSuchEntity check to state.Delete. There was no attachment check and no ARN check, and the silent success is worse than the missing error code: nothing writes a back-reference onto a policy, so after the delete every entity still held the ARN. ListAttachedUserPolicies still reported it, the authorization evaluator loaded no document for it — so the entity silently lost the permissions the policy granted, and a test asserting a deny passed for the wrong reason — and GetPolicy answered NoSuchEntity for the same ARN two other operations still named.

What changed

DeleteConflict/409 while attached, naming the entities. API_DeletePolicy publishes the code for exactly this — "attempted to delete a resource that has attached subordinate entities. The error message describes these entities" — and states the precondition in prose.

The refusal reads the same three <kind>_policies: prefixes, through the same loadPolicyList, that the AttachmentCount GetPolicy and ListPolicies report is derived from, so the count and the refusal cannot disagree about what "attached" means. iamPolicyAttachmentCounts could not serve both: it accumulates counts[arn]++ and so answers whether a policy is attached rather than to what, and five callers depend on its signature and its once-per-request hoisting. The names come from a sibling helper over the same keys instead.

Naming them is not a nicety here. API_DeletePolicy's description tells the caller to use ListEntitiesForPolicy to find what to detach, and substrate does not implement that operation — no dispatch arm, no handler, it appears only in the generated authz data. So the 409's message is the only way a substrate caller can learn what to detach, and an unnamed entity is an unrecoverable wedge. Only three of the seven sibling DeleteConflict arms name what to remove (DeleteUser's group memberships, DeleteRole's instance profiles, DeleteGroup's users); the four that refuse over attached policies name nothing, so the wording follows those three rather than the majority.

This also closes a hazard #847's derived AttachmentCount made observable: a delete now succeeds only when nothing is attached, so a policy re-created under a deleted ARN cannot inherit the previous one's attachments. That is the issue's fourth criterion, discharged structurally rather than by a separate mechanism — the test asserts the invariant.

InvalidInput/400 for a malformed PolicyArn, through the same iamValidatePolicyARN the three attach operations and both Put*PermissionsBoundary already apply, before authorize as they do it. Previously any non-empty string became a state key that could never match, so a bare policy name was reported as a policy that does not exist — the wrong answer to a request that was never well-formed enough to name one.

A bundled AWS managed ARN stops contradicting GetPolicy. The handler read state only, so arn:aws:iam::aws:policy/PowerUserAccess was reported as not existing while GetPolicy resolved the same ARN from the catalog and returned the policy — one ARN, two operations, opposite answers about whether the thing exists.

The code is substrate's reading of a constrained choice, not a documented one, and the constraint is real: API_DeletePolicy is silent on AWS managed ARNs, and the strongest published statement anywhere 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 names no code and does not mention deletion. So the code is drawn from the operation's own Errors section, where of the five published InvalidInput is the only one describing a rejected input value: NoSuchEntity is false because the policy is readable, DeleteConflict means attached subordinate entities, LimitExceeded and ServiceFailure are unrelated. UnmodifiableEntity — which DeleteRole answers for a service-linked role — fits the meaning better but is not published for this operation, and answering a code AWS does not list would trade one wrong answer for another.

Recorded rather than implemented. AWS's other stated precondition — delete every non-default version first — publishes no error code (DeleteConflict's own text is about attached subordinate entities) and cannot arise, because substrate models exactly one version per policy. It gets a comment.

Filed rather than folded in. The three detach operations do not validate PolicyArn either, so a malformed one is accepted and silently detaches nothing — the exact asymmetry where the attach of a string is refused and the detach of the same string succeeds. Filed as #875.

Corrections recorded on the issue before this PR

Posted to #853 in full. In summary: every line number in the body has drifted (deletePolicy is at iam_plugin.go:1430, not :1382); the seven sibling sites are exact in count but four of the seven do not name the entities, so "as the seven sibling arms already do" overstated the precedent by half; iamPolicyAttachmentCounts cannot satisfy AC 1 and AC 2 together because it discards which entity each membership came from; ListEntitiesForPolicy is not implemented, which raises the stakes on the naming; and AC 4's AttachmentCount clause is discharged as a consequence of AC 1 rather than as separate work. AC 3 is exact as written and AC 5 ships as substrate's reading, as the body asks.

Verification

The nine-command gate, all green:

  • gofmt -l . && go build ./... && go vet ./... — clean
  • golangci-lint run ./... — 0 issues
  • make test — exit 0
  • GOTOOLCHAIN=go1.26.6 make test — exit 0
  • GOTOOLCHAIN=go1.26.6 make coverage — 83.9% emulator / 83.3% total, holding the baseline
  • make docs-reference docs-reference-check docs-versions authz-reference-check — exit 0
  • npm --prefix docs run build — exit 0
  • go vet -C test/e2e ./... && go test -C test/e2e ./... — exit 0

Every entity is created and attached through the API, per #765: a helper writing an attachment straight into state cannot prove the refusal reads the same keys an attach writes, which is the whole of the issue's second acceptance criterion. Each of the three kinds is refused on its own as well as together — one prefix scanned would pass a test that only attaches to a user — and the walk down from three attachments to zero asserts the intermediate steps, since a delete that stayed refused after two detaches or succeeded after one would still pass a test that only checked the endpoints. Four store-failure arms assert the refusal cannot be turned off by a broken read, which is the one direction that loses data.

Compatibility

DeletePolicy refuses a policy it used to delete: a consumer that deleted an attached policy and relied on the success now gets DeleteConflict/409 and must detach first, which is what AWS requires. A malformed PolicyArn now answers InvalidInput/400 where it answered NoSuchEntity/404, and a bundled AWS managed ARN answers InvalidInput/400 where it also answered NoSuchEntity/404.

Closes #853

DeletePolicy went straight from the NoSuchEntity check to state.Delete with
no attachment check, and what the silent success left behind is worse than
the missing error code. Nothing writes a back-reference onto a policy, so
every entity kept its ARN: ListAttachedUserPolicies still reported it, the
authorization evaluator loaded no document for it so the entity silently
lost the permissions the policy granted — a test asserting a deny then
passed for the wrong reason — and GetPolicy answered NoSuchEntity for the
same ARN two other operations still named.

It now answers DeleteConflict/409, which API_DeletePolicy publishes for
exactly this, with a message naming the users, groups and roles as that
page's description says it does. The refusal reads the same three
"<kind>_policies:" prefixes, through the same loader, that the
AttachmentCount GetPolicy and ListPolicies report is derived from, so the
count and the refusal cannot disagree about what attached means. The counts
helper could not serve both: it accumulates counts[arn]++ and so answers
whether a policy is attached rather than to what, and five callers depend on
its signature and its once-per-request hoisting.

Naming the entities is not a nicety. API_DeletePolicy's description tells
the caller to use ListEntitiesForPolicy to find what to detach, and
substrate does not implement that operation, so the message is the only way
a caller can learn it. Only three of the seven sibling DeleteConflict arms
name what to remove; the four refusing over attached policies name nothing,
so the wording follows those three rather than the majority.

The fix also closes a hazard #847's derived AttachmentCount made observable:
a delete now succeeds only when nothing is attached, so a policy re-created
under a deleted ARN cannot inherit the previous one's attachments.

A malformed PolicyArn answers InvalidInput/400 through the same shape check
the three attach operations and both Put*PermissionsBoundary already apply.
Previously any non-empty string became a state key that could never match,
so a bare policy name was reported as a policy that does not exist — the
wrong answer to a request never well-formed enough to name one.

A bundled AWS managed ARN stops contradicting GetPolicy, which resolved it
from the catalog while this handler read state only and reported it missing.
It now answers InvalidInput/400, and the code is substrate's reading of a
constrained choice rather than a documented one: API_DeletePolicy is silent
on AWS managed ARNs, and the strongest published statement anywhere is "You
cannot change the permissions defined in AWS managed policies", which
establishes that the customer does not administer them but names no code.
So the code comes from the operation's own Errors section, where of the five
published InvalidInput is the only one describing a rejected input value.
UnmodifiableEntity fits the meaning better but is not published for this
operation, and answering a code AWS does not list would trade one wrong
answer for another.

Recorded rather than implemented: AWS's other stated precondition, deleting
every non-default version first, publishes no error code and cannot arise,
because substrate models exactly one version per policy. Filed as #875
rather than folded in: the three detach operations do not validate PolicyArn
either.

Closes #853
@scttfrdmn scttfrdmn added this to the v0.116.0 milestone Sep 15, 2026
@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 8236e28 into main Sep 15, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/853-delete-policy-conflict branch September 15, 2026 04:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iam: DeletePolicy has no DeleteConflict check, so an attached policy deletes silently and every entity keeps a dangling ARN

1 participant