Repository navigation
Description
ListPolicies derives AttachmentCount from state; GetPolicy renders the stored field, which
no attach operation ever increments. So the same policy reports a different attachment count
depending on which operation you ask.
What substrate does
iamPolicyAttachmentCounts (emulator/iam_list_policies.go:209-231) derives the count from the
three <kind>_policies:<name> prefixes, and its own doc comment states why:
The count is derived rather than stored. AttachUserPolicy and its siblings record the ARN on
the entity's list and never touch a count on the policy, and the bundled catalog is immutable
— so a stored count would be zero for a bundled policy and could go stale for a created one.
listPolicies uses it (:99, applied at :132). getPolicy does not. It derives
PermissionsBoundaryUsageCount (emulator/iam_plugin.go:1355-1358, added by #815) and then
renders the policy as stored:
if mp, ok := GetManagedPolicy(params.PolicyArn); ok {
return iamXMLResponse(http.StatusOK, "GetPolicy", iamSinglePolicyXML(mp, boundaryUsage[mp.ARN]))
}
…
return iamXMLResponse(http.StatusOK, "GetPolicy", iamSinglePolicyXML(&policy, boundaryUsage[policy.ARN]))iamPolicyXMLFields writes p.AttachmentCount straight out of the struct
(emulator/iam_xml.go:394), and IAMPolicy.AttachmentCount (emulator/iam_types.go:88) is set
by nothing after CreatePolicy stores 0. The bundled catalog carries 0 for every policy.
The observable divergence
aws iam create-policy --policy-name p --policy-document …
aws iam attach-role-policy --role-name r --policy-arn arn:…:policy/p
aws iam list-policies --scope Local # AttachmentCount: 1
aws iam get-policy --policy-arn … # AttachmentCount: 0
Same shape, same policy, two answers. GetPolicy on any bundled AWS managed policy reports 0
however many roles are attached to it.
What AWS documents
The Policy type is used as a response element by CreatePolicy, GetPolicy and
ListPolicies, and AttachmentCount is described once for all three:
The number of entities (users, groups, and roles) that the policy is attached to.
There is no per-operation carve-out — unlike Description, which carries an explicit "not
included in the response to the ListPolicies operation". So the member must mean the same thing
on both operations, which is exactly what it does not do today.
ManagedPolicyDetail, which GetAccountAuthorizationDetails returns, carries the same member —
so whatever helper answers it needs the derived count too (see #848, which is that operation's
absence).
Why it slipped
#815 hoisted the boundary scan into getPolicy because it had to, and the attachment scan was
already hoisted in listPolicies for the O(policies × entities) reason. Nobody threaded the
attachment count into the single-policy path, because the single-policy path never needed a scan
before. The fix is one call and one assignment on a local copy — the same local-copy discipline
listPolicies documents at :124-131, since GetManagedPolicy hands back a shared pointer and
writing through it would leak a count into every later read.
Acceptance criteria
-
getPolicyderivesAttachmentCountthroughiamPolicyAttachmentCountsand reports it,
for a bundled policy and a customer-managed one alike. - The catalog's shared
*IAMPolicyis not mutated — a local copy, aslistPolicies
already does, asserted by a test that reads the same bundled policy twice with an attach
in between and then a detach, and sees 1 then 0 rather than a value that accumulates. -
GetPolicyandListPoliciesreport the identical count for the same policy in the same
state, asserted directly against each other rather than against a literal. -
CreatePolicyreports 0, which is necessarily correct and is the documented shape. - Attaching to a user, a group and a role each increment it, and detaching decrements —
three entity kinds, since the derivation reads three prefixes. -
IAMPolicy.AttachmentCount's doc comment records that the field is not the answer any
read reports, or the field is removed if nothing reads it, so the next change does not
reintroduce the split. -
docs/services.mdstates thatAttachmentCountis computed per read on every operation
that reports it, beside thePermissionsBoundaryUsageCountnote IAM: model PermissionsBoundaryUsageCount on the policy shapes #815 added.
Activity
fix(iam): a boundary an SDK can read, and a count something wrote (#852…
feat(iam): the account authorization snapshot, and MaxItems refused r…
fix(iam): an attached policy cannot be deleted (#853) (#876)