Skip to content

iam: GetPolicy reports the stored AttachmentCount (always 0) while ListPolicies derives it #847

Closed

Description

@scttfrdmn

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

  • getPolicy derives AttachmentCount through iamPolicyAttachmentCounts and reports it,
    for a bundled policy and a customer-managed one alike.
  • The catalog's shared *IAMPolicy is not mutated — a local copy, as listPolicies
    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.
  • GetPolicy and ListPolicies report the identical count for the same policy in the same
    state, asserted directly against each other rather than against a literal.
  • CreatePolicy reports 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.md states that AttachmentCount is computed per read on every operation
    that reports it, beside the PermissionsBoundaryUsageCount note IAM: model PermissionsBoundaryUsageCount on the policy shapes #815 added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: iamtype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions