Repository navigation
feat(iam): PermissionsBoundaryUsageCount on the policy shapes (#815) - #839
Merged
Merged
Conversation
iamPolicyXMLFields rendered every documented member of AWS's `Policy` data type except one. `PermissionsBoundaryUsageCount` — "The number of entities (users and roles) for which the policy is used to set the permissions boundary" — was absent from CreatePolicy, GetPolicy and ListPolicies, the three operations the type's own scope note names, and #807 recorded it as unmodeled with a comment at iam_xml.go:330 pointing here. Unlike `Description`, which carries an explicit "not included in the response to the ListPolicies operation" carve-out, this member carries none, and ListPolicies' own sample response renders it on all four members with two non-zero. A consumer setting a boundary could not observe that the policy was in use as one. New iam_boundary_usage.go derives the count instead of storing it. Substrate keeps a boundary as an ARN on the entity — IAMUser.PermissionsBoundary at iam_types.go:28, IAMRole at :42 — and not as a back-reference on the policy, so iamBoundaryUsageCounts walks the user and role prefixes once and returns map[ARN]int, decoding only the `PermissionsBoundary` member both records share rather than two whole entity types. It is the sibling of iamPolicyAttachmentCounts, which derives AttachmentCount for the same reason. Deriving it makes three of #815's criteria hold by construction rather than by maintenance: the count reaches zero when the last boundary is removed because there is no counter to forget to decrement; DeleteUser (iam_plugin.go:336) and DeleteRole (:624) decrement it because both delete the whole entity record and the boundary goes with it, so neither has to learn about policies; and a replayed run reports the live run's count because the count is a function of the state replay rebuilds rather than an accumulator that has to end up agreeing with one accumulated live. The rejected alternative, a counter on IAMPolicy, would have to be maintained on all four Put*/Delete*PermissionsBoundary paths (:2092-2247) plus those two deletes, where missing any one path reports a count that is then wrong forever. The cost #815 names — ListPolicies at O(policies x entities) — is avoided by hoisting the scan to one per request. Each of the three operations counts once and hands the map to the renderer, so a read is O(policies + entities); iamPolicyXMLFields takes the count as an argument rather than reading a field, which also keeps a bundled catalog policy countable without mutating the shared pointer ListManagedPolicies hands back — the aliasing hazard the local copy at iam_list_policies.go:122 exists for. GetPolicy counts before its catalog arm, because in a fresh emulator every available boundary ARN is a bundled one. CreatePolicy scans too rather than hardcoding the zero a new policy must have. AWS's CreatePolicy sample omits the member (as it omits IsAttachable, which #807 renders anyway), but the type's scope note names the operation, and reading from the same helper leaves no second answer to keep true. It also stays right in the one case where zero would be wrong: substrate does not require a boundary's policy to exist, so an ARN can already name the policy CreatePolicy is about to make. iam_boundary_usage_test.go pins the count over both entity kinds together, down to zero, across DeleteUser/DeleteRole, equal on GetPolicy and ListPolicies, zero on CreatePolicy, on a bundled policy, and equal after replaying a recorded run into empty state. All five fail against the pre-change code with the member absent from the response entirely. Assertions read raw XML, because a decoded map cannot tell an absent member from a zero one.
Codecov Report❌ Patch coverage is
📢 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.
Closes #815
PermissionsBoundaryUsageCountwas the one documented member of AWS'sPolicydata typesubstrate did not render. v0.113.0's #807 rendered
DescriptionandIsAttachableand left thisone as its stated cost, with a comment at
emulator/iam_xml.go:330naming the two candidateimplementations; that comment is now the implementation.
What AWS documents
The
Policydata type (API_Policy),verbatim:
The member carries no per-operation carve-out.
Descriptionon the same type is the contrastthat makes that meaningful:
ListPolicies' own sample response confirms it, rendering a count on every member — two of themnon-zero, and no
Descriptionon any member:So all three operations report it, and it is rendered unconditionally rather than omitted at zero,
for the reason
IsAttachableis: a policy no entity uses as a boundary has a count, and it iszero, while an omitted integer decodes to the same
0in every SDK.One honest wrinkle, recorded rather than papered over:
CreatePolicy's sample response omitsthe member — but it also omits
IsAttachable, which #807 renders anyway on the same reasoning, andthe type-level scope note names
CreatePolicyexplicitly. The sample is abbreviated, not acarve-out; a carve-out is written in prose on the member, as
Description's is.Design: scan on read, hoisted to one scan per request
Substrate keeps a boundary as an ARN on the entity (
IAMUser.PermissionsBoundary,IAMRole.PermissionsBoundary) and not as a back-reference on the policy. Newemulator/iam_boundary_usage.goaddsiamBoundaryUsageCounts, which walks the user and roleprefixes once and returns
map[policyARN]int. It is the sibling of the existingiamPolicyAttachmentCounts, which derivesAttachmentCountfor the same reason.Why deriving beats reference counting — three of the issue's criteria then hold by
construction:
to decrement;
DeleteUser(iam_plugin.go:336) andDeleteRole(:624) decrement it, because both delete thewhole entity record and the boundary goes with it — neither has to learn about policies at all;
rebuilds rather than an accumulator that has to end up agreeing with one accumulated live.
A counter on
IAMPolicywould instead have to be maintained on all fourPut*/Delete*PermissionsBoundarypaths (iam_plugin.go:2092-2247) plus those two deletes,where missing any one path reports a count that is then wrong forever — and the live counter would
still have to be reconciled against a replayed one.
Why the performance cost the issue names does not materialise. The scan is hoisted to one per
request: each operation counts once and hands the map to the renderer, so a policy read is
O(policies + entities) rather than the O(policies × entities) a per-policy scan would make
ListPolicies.iamPolicyXMLFieldstakes the count as an argument (rendered afterAttachmentCount, beforeIsAttachable, so it reaches bothiamSinglePolicyXMLandiamPolicyListXML) rather than reading a field — which also keeps a bundled catalog policycountable without mutating the shared pointer
ListManagedPolicieshands back, the aliasing hazardthe local copy at
iam_list_policies.go:122exists for.GetPolicycounts before its catalog arm,because in a fresh emulator every available boundary ARN is a bundled one.
CreatePolicyscans too rather than hardcoding the zero a new policy must have: one helper means nosecond answer to keep true, and it stays right in the one case where zero would be wrong —
substrate does not require a boundary's policy to exist, so an ARN can already name the policy
CreatePolicyis about to make.GetAccountAuthorizationDetailsIt is not implemented at all, so it did not get the member. The only references in the tree are
its action name in the generated authz table (
emulator/authz_reference_gen.go:1570) and twocomments; there is no handler, so
ManagedPolicyDetailhas no renderer to share. When thatoperation lands it should render through
iamPolicyXMLFieldsand take the count fromiamBoundaryUsageCounts, which is why the helper is a method on the plugin rather than a closureinside a single operation.
Tests — fail before, pass after
emulator/iam_boundary_usage_test.go(package emulator_test, table-driven where the assertion isa sequence of steps). Assertions read raw XML, because a decoded map cannot tell an absent
member from a zero one — the same reason #807's shape tests do.
..._CountsUsersAndRoles..._FollowsEntityDeletionDeleteUserthenDeleteRoledecrementing 2 → 1 → 0..._OnEveryPolicyShapeGetPolicyandListPolicies, and0onCreatePolicy..._CountsABundledPolicy..._SurvivesReplayFail-before / pass-after. Reverting the three edited source files to
mainand removingiam_boundary_usage.go, all five tests fail (12 failing cases including subtests), every one withno PermissionsBoundaryUsageCount in <GetPolicyResponse …>— the member is absent from the responseentirely, e.g.:
With the change in place all five pass. The replay test is non-vacuous by construction: a
GetPolicyagainst un-rebuilt state would answer
NoSuchEntity/404 and the helper requires 200.No test touches the network or the wall clock — the recorded/replayed stacks run on a
TimeControllerpinned to a fixed instant andReplayConfig{}(no speed multiplier, so no sleeps).Verification
gofmt -l . && go build ./... && go vet ./...golangci-lint run ./...make testGOTOOLCHAIN=go1.26.6 make testGOTOOLCHAIN=go1.26.6 make coveragemake docs-reference docs-reference-check docs-versions authz-reference-checkdocs/services.md already up to date (67 plugins)npm --prefix docs run buildgo vet -C test/e2e ./... && go test -C test/e2e ./...docs/services.mdstates the member is computed per read with its complexity, and its shape tablerow moves from not modelled to rendered on both shapes; the "two members are deliberately
unmodelled" paragraph becomes one (
RoleLastUsed, #816).CHANGELOG.mdhas the entry under## [Unreleased]→### Added. No version number appears in either, perscripts/check-doc-versions.sh.