Skip to content

fix(iam): an IAM entity belongs to an account, not to the emulator (#737) - #769

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/737-iam-account-scoped-state-keys
Aug 23, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/737-iam-account-scoped-state-keys

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

What

Every IAM state key was account-blind — a user at user:{name}, a role's attachments at role_policies:{name} — so one server held one IAM directory no matter how many accounts it served. Every key now reads {kind}:{account}/{rest}, the shape table:{account}/{name} and the other account-aware plugins already used.

The account goes after the kind so a per-account listing stays one prefix scan (List("user:111122223333/")) rather than a full sweep and filter, and so a HasPrefix(key, "user:") test on the kind keeps working unchanged.

Three observable defects, all fixed

Confirmed live against a two-account credentials: block (#736), before and after:

before after
CreateRole deploy in a second account EntityAlreadyExists arn:aws:iam::444455556666:role/deploy
ListUsers as B after A creates alice ["alice"] []
ListPolicies --scope Local as B after A creates p1 A's ARN []

The third was a standalone bug: ListPolicies scanned "policy:" across every account although a policy ARN already named its owner.

Where the account comes from

The ARN, wherever an ARN is the authority — never the request:

  • resolveIAMEntity and SimulatePrincipalPolicy take it from the principal ARN, so a cross-account principal resolves against its own account's policies rather than a same-named entity in the account the request resolved to.
  • AssumeRole reads the role named by RoleArn from that ARN's account. Keying it on the caller's account would have broken the exact case cross-account assumption exists for.
  • A policy record is addressed by the account inside its own ARN, so a caller holding only an attachment's ARN reaches it without knowing whose request it is.

iamEntity gained an Account field for this: it is part of the entity's identity, not context about the request.

The one key with no account

accesskey:{id} keeps none, and cannot have one: an access key ID is what determines an account, so resolvePrincipal looks the record up before any account is known. The account is a field on IAMAccessKey instead — which makes this a schema change rather than a key rename — and resolvePrincipal now returns it as an account to adopt, the same way the STS-session arm already did with sess.AccountID. A record written before this release has the field empty and falls back to the account the request resolved to, which is the account it always used.

Why the builders

The ~90 sites that concatenated these strings inline now go through emulator/iam_state_keys.go. A missed site is invisible — it compiles, it reads and writes state, and it simply addresses a key nothing else uses — so every conversion was an exact literal match with a required occurrence count, and two mismatches caught sites an earlier grep had missed.

Two sites were invisible to a grep for iamNamespace at all, because they spelled the namespace "iam" as a literal inside StackDeployer's drift comparison.

One thing the plan flagged as a hazard turned out to be dead code: scanIAMEntities' ContainsAny(TrimPrefix(k, "user:"), ":") guard claimed to filter index keys out of a List("user:"), but "user_policies:" never had the user: prefix, so it never fired. Deleted with a comment saying so, rather than "fixed".

Tests

emulator/iam_account_scope_test.go is new and is the fail-before proof — all four tests fail on main:

  • TestIAM_TwoAccountsHoldTheSameNames — user, role, group, instance profile and policy created in both accounts
  • TestIAM_ListsSeeOnlyTheCallersAccount — five listings, 1 for A and empty for B
  • TestIAM_ReadsDoNotCrossAccounts — GetRole/DeleteRole from B answer NoSuchEntity and A's role survives
  • TestIAM_InlineAndAttachedPoliciesAreScoped

The ~30 existing test sites that hardcoded a key now call Xxx…ForTest builders exported from export_test.go, so when the layout changes again the tests move with the production code instead of drifting into addressing keys nothing reads.

Compatibility

IAM state written before this release is unreachable. A snapshot or exported fixture holding user:alice is not read by a handler now looking for user:123456789012/alice. Re-seed it through the API. This is the same class of break as v0.108.0's account-default change, for the same reason, and is recorded in docs/services.md.

The Marker a paginated IAM listing returns is base64 of a state key, so a marker captured before this release does not match after it. It is opaque and per-listing, so nothing carries one across an upgrade.

Verification

gofmt -l . && go build ./... && go vet ./... && golangci-lint run ./...   # 0 issues
make test                                                                 # green, race on
make coverage                                                             # emulator 83.4%, total 82.8%
make docs-reference docs-reference-check docs-versions                    # green
npm --prefix docs run build                                               # green; no dangling anchors
go vet -C test/e2e ./... && go test -C test/e2e ./...                      # green

Closes #737

)

Every IAM state key was account-blind, so one server held one IAM
directory no matter how many accounts it served: two accounts could not
both hold a role called "deploy", a listing reported every account's
entities, and resolveIAMEntity discarded the account in the principal
ARN it was handed.

Every key now reads {kind}:{account}/{rest}, the shape DynamoDB and the
other account-aware plugins already used, with the account after the
kind so a per-account listing is one prefix scan. The account comes from
the ARN wherever an ARN is the authority — the principal ARN for
resolution and simulation, RoleArn's own account for AssumeRole, a
policy ARN's account for the policy record.

accesskey:{id} keeps no account deliberately: the ID determines the
account, so the record carries it as a field instead.
@codecov

codecov Bot commented Aug 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.07981% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/iam_plugin.go 85.29% 0 Missing and 15 partials ⚠️
emulator/iam_groups.go 81.81% 0 Missing and 4 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 47624cb into main Aug 23, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/737-iam-account-scoped-state-keys branch August 23, 2026 07:27
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: state keys are account-blind, so an entity resolves by name across accounts

1 participant