Skip to content

feat(iam): PermissionsBoundaryUsageCount on the policy shapes (#815) - #839

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/815-permissions-boundary-usage-count
Sep 13, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/815-permissions-boundary-usage-count

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #815

PermissionsBoundaryUsageCount was the one documented member of AWS's Policy data type
substrate did not render. v0.113.0's #807 rendered Description and IsAttachable and left this
one as its stated cost, with a comment at emulator/iam_xml.go:330 naming the two candidate
implementations; that comment is now the implementation.

What AWS documents

The Policy data type (API_Policy),
verbatim:

Contains information about a managed policy.

This data type is used as a response element in the CreatePolicy, GetPolicy, and
ListPolicies operations.

PermissionsBoundaryUsageCount
The number of entities (users and roles) for which the policy is used to set the permissions
boundary.
For more information about permissions boundaries, see Permissions boundaries for IAM identities
in the IAM User Guide.
Type: Integer
Required: No

The member carries no per-operation carve-out. Description on the same type is the contrast
that makes that meaningful:

Description
A friendly description of the policy.
This element is included in the response to the GetPolicy operation. It is not included in
the response to the ListPolicies operation.

ListPolicies' own sample response confirms it, rendering a count on every member — two of them
non-zero, and no Description on any member:

      <member>
        <Arn>arn:aws:iam::aws:policy/ReadOnlyAccess</Arn>
        <AttachmentCount>6</AttachmentCount>
        ...
        <PermissionsBoundaryUsageCount>2</PermissionsBoundaryUsageCount>

So all three operations report it, and it is rendered unconditionally rather than omitted at zero,
for the reason IsAttachable is: a policy no entity uses as a boundary has a count, and it is
zero, while an omitted integer decodes to the same 0 in every SDK.

One honest wrinkle, recorded rather than papered over: CreatePolicy's sample response omits
the member — but it also omits IsAttachable, which #807 renders anyway on the same reasoning, and
the type-level scope note names CreatePolicy explicitly. The sample is abbreviated, not a
carve-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. New
emulator/iam_boundary_usage.go adds iamBoundaryUsageCounts, which walks the user and role
prefixes once and returns map[policyARN]int. It is the sibling of the existing
iamPolicyAttachmentCounts, which derives AttachmentCount for the same reason.

Why deriving beats reference counting — three of the issue's criteria then hold by
construction
:

  • 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 — neither has to learn about policies at all;
  • 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.

A counter on IAMPolicy would instead have to be maintained on all four
Put*/Delete*PermissionsBoundary paths (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. iamPolicyXMLFields takes the count as an argument (rendered after
AttachmentCount, before IsAttachable, so it reaches both iamSinglePolicyXML and
iamPolicyListXML) 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: one helper means no
second 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
CreatePolicy is about to make.

GetAccountAuthorizationDetails

It 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 two
comments; there is no handler, so ManagedPolicyDetail has no renderer to share. When that
operation lands it should render through iamPolicyXMLFields and take the count from
iamBoundaryUsageCounts, which is why the helper is a method on the plugin rather than a closure
inside a single operation.

Tests — fail before, pass after

emulator/iam_boundary_usage_test.go (package emulator_test, table-driven where the assertion is
a 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.

Test Pins
..._CountsUsersAndRoles a boundary on a user and on a role counted together (2), then down to 1, then to 0 when the last is removed
..._FollowsEntityDeletion DeleteUser then DeleteRole decrementing 2 → 1 → 0
..._OnEveryPolicyShape the same value on GetPolicy and ListPolicies, and 0 on CreatePolicy
..._CountsABundledPolicy a bundled AWS managed policy counted, and reported 0 in another emulator
..._SurvivesReplay a recorded run replayed into empty state reports the live run's count

Fail-before / pass-after. Reverting the three edited source files to main and removing
iam_boundary_usage.go, all five tests fail (12 failing cases including subtests), every one with
no PermissionsBoundaryUsageCount in <GetPolicyResponse …> — the member is absent from the response
entirely, e.g.:

--- FAIL: TestIAM_PermissionsBoundaryUsageCount_CountsUsersAndRoles
    Messages: no PermissionsBoundaryUsageCount in <GetPolicyResponse …><Policy>…
              <AttachmentCount>0</AttachmentCount><IsAttachable>true</IsAttachable>…

With the change in place all five pass. The replay test is non-vacuous by construction: a GetPolicy
against 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
TimeController pinned to a fixed instant and ReplayConfig{} (no speed multiplier, so no sleeps).

Verification

Gate Result
gofmt -l . && go build ./... && go vet ./... clean
golangci-lint run ./... 0 issues
make test pass (race)
GOTOOLCHAIN=go1.26.6 make test pass
GOTOOLCHAIN=go1.26.6 make coverage emulator 83.7%, total 83.1% — baseline held
make docs-reference docs-reference-check docs-versions authz-reference-check pass; docs/services.md already up to date (67 plugins)
npm --prefix docs run build pass (vitepress 1.6.4)
go vet -C test/e2e ./... && go test -C test/e2e ./... pass

docs/services.md states the member is computed per read with its complexity, and its shape table
row moves from not modelled to rendered on both shapes; the "two members are deliberately
unmodelled" paragraph becomes one (RoleLastUsed, #816). CHANGELOG.md has the entry under
## [Unreleased] → ### Added. No version number appears in either, per
scripts/check-doc-versions.sh.

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

codecov Bot commented Sep 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.05128% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/iam_boundary_usage.go 80.00% 4 Missing ⚠️
emulator/iam_plugin.go 77.77% 2 Missing ⚠️
emulator/iam_list_policies.go 75.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit c226f69 into main Sep 13, 2026
16 of 17 checks passed
@scttfrdmn
scttfrdmn deleted the feat/815-permissions-boundary-usage-count branch September 13, 2026 03:37
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: model PermissionsBoundaryUsageCount on the policy shapes

1 participant