Repository navigation
fix(iam): an attached policy cannot be deleted (#853) - #876
Merged
Merged
Conversation
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
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
deletePolicywent straight from theNoSuchEntitycheck tostate.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.ListAttachedUserPoliciesstill 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 — andGetPolicyansweredNoSuchEntityfor the same ARN two other operations still named.What changed
DeleteConflict/409 while attached, naming the entities.API_DeletePolicypublishes 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 sameloadPolicyList, that theAttachmentCountGetPolicyandListPoliciesreport is derived from, so the count and the refusal cannot disagree about what "attached" means.iamPolicyAttachmentCountscould not serve both: it accumulatescounts[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 useListEntitiesForPolicyto 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 siblingDeleteConflictarms 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
AttachmentCountmade 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 malformedPolicyArn, through the sameiamValidatePolicyARNthe three attach operations and bothPut*PermissionsBoundaryalready apply, beforeauthorizeas 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, soarn:aws:iam::aws:policy/PowerUserAccesswas reported as not existing whileGetPolicyresolved 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_DeletePolicyis 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 publishedInvalidInputis the only one describing a rejected input value:NoSuchEntityis false because the policy is readable,DeleteConflictmeans attached subordinate entities,LimitExceededandServiceFailureare unrelated.UnmodifiableEntity— whichDeleteRoleanswers 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
PolicyArneither, 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 (
deletePolicyis atiam_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;iamPolicyAttachmentCountscannot satisfy AC 1 and AC 2 together because it discards which entity each membership came from;ListEntitiesForPolicyis not implemented, which raises the stakes on the naming; and AC 4'sAttachmentCountclause 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 ./...— cleangolangci-lint run ./...— 0 issuesmake test— exit 0GOTOOLCHAIN=go1.26.6 make test— exit 0GOTOOLCHAIN=go1.26.6 make coverage— 83.9% emulator / 83.3% total, holding the baselinemake docs-reference docs-reference-check docs-versions authz-reference-check— exit 0npm --prefix docs run build— exit 0go vet -C test/e2e ./... && go test -C test/e2e ./...— exit 0Every 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
DeletePolicyrefuses a policy it used to delete: a consumer that deleted an attached policy and relied on the success now getsDeleteConflict/409 and must detach first, which is what AWS requires. A malformedPolicyArnnow answersInvalidInput/400 where it answeredNoSuchEntity/404, and a bundled AWS managed ARN answersInvalidInput/400 where it also answeredNoSuchEntity/404.Closes #853