Skip to content

feat(kms): the default key policy, and the policy operations' refusals (#983) - #1048

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/983-key-policy
Sep 18, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/983-key-policy

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Two defects with one cause: createKey decoded no Policy member at all, and getKeyPolicy answered a hardcoded {"Version":"2012-10-17","Statement":[]} for any key that had never had PutKeyPolicy called on it. So a caller that attached a policy at creation time read back a document it had never sent, and every other key reported a policy that grants nothing.

The second half is the one that matters beyond fidelity. A key policy is the only place a KMS key's own permissions live — an IAM policy cannot grant access to a key whose key policy does not delegate to IAM — so an empty-statement document is not a neutral placeholder. It states the opposite of what AWS attaches, and a consumer reading a key policy to decide whether a principal has access would conclude that nobody does.

The default document

Transcribed from API_GetKeyPolicy's own Example Response, not the developer guide's Default key policy page that describes it in prose. The reference page is the stronger source in one concrete way: it carries an Id member, key-default-1, that the prose does not mention.

Member Value
Version 2012-10-17
Id key-default-1
Statement[0].Sid Enable IAM User Permissions
Statement[0].Effect Allow
Statement[0].Principal.AWS arn:aws:iam::{account}:root
Statement[0].Action kms:*
Statement[0].Resource *

The account is the key's own, where AWS's example shows the documentation placeholder 111122223333 — the whole content of the statement is that this key's account controls it, so a document naming a foreign root would grant nothing to anyone who can reach the key. The document is synthesized at read time rather than written at create time: observationally identical, and it also means a key created by an earlier release reports the default rather than the old stand-in.

The refusals

Request Answer
Policy outside 1–32768 bytes, on CreateKey or PutKeyPolicy LimitExceededException/400
Policy that is not JSON, or is JSON but not an object MalformedPolicyDocumentException/400
PolicyName other than default, on GetKeyPolicy or PutKeyPolicy NotFoundException/400
KeyId naming no key, on either operation NotFoundException/400

LimitExceededException for a length violation is a transcription, not an inference from the error's gloss — CreateKey's Policy member names the code itself: "if the key policy exceeds the length constraint, AWS KMS returns a LimitExceededException". That is what settles an oversize document's otherwise plausible claim on MalformedPolicyDocumentException. The length is checked before the parse, so 40 KB of valid JSON hears about its size.

NotFoundException for a PolicyName is substrate's one-step reading: neither page publishes a code for that member, but both state default is the only valid value and both publish NotFoundException glossed "the specified entity or resource could not be found" — which a name that names no policy is exactly. It keeps the refusal among the page's own codes instead of reaching for CommonErrors.

Both handlers now call loadKey, closing a gap kms_error_status_test.go had recorded as unanswerable, and the member checks run before the lookup, following #991: a request wrong about both hears about the part it can fix without an AWS account.

Recorded rather than half-built

Four things AWS publishes here and substrate does not implement:

  • The lockout safety check, and therefore BypassPolicyLockoutSafetyCheck, which stays undecoded on both operations. The check is a policy evaluation against the calling principal; with no check to bypass, true and false are observationally identical, so decoding the member would create exactly the parameter-read-by-nobody defect kms: CreateKey decodes neither Origin, CustomKeyStoreId nor XksKeyId, so a key asked for with no key material comes back fully usable #984 just closed.
  • Statement-level validation, declined on AWS's own words: a statement missing Action or Resource "has no effect" while "the CreateKey and PutKeyPolicy API requests succeed", so MalformedPolicyDocumentException's "semantically correct" gloss is narrower than it reads.
  • The Policy character-class pattern, for which neither page publishes a code.
  • InvalidArnException — nothing walks the principals.

This reverses the release plan's fifth acceptance criterion

The issue asks PutKeyPolicy to refuse a key pending deletion, and the plan named kmsInvalidKeyState as the helper to use. The developer guide's key-state table settles it the other way: GetKeyPolicy and PutKeyPolicy carry a checkmark in all seven state columns — Enabled, Disabled, Pending deletion, Pending import, Unavailable, Creating, Updating — with no footnote on any of them, where TagResource two rows below is refused at pending deletion under footnote [3].

So no key state refuses either operation, adding one would be a divergence rather than fidelity, and neither kmsKeyStateError nor kmsInvalidKeyState is called from either handler. KMSInvalidStateException is recorded as published-but-unreachable, and TestKMSKeyPolicy_NeitherOperationRefusesAKeyForItsState asserts that a key pending deletion still takes and reports a policy — so a later sweep completing the key-state checks cannot read the published code as a missing refusal. Posted on the issue as a premise correction before this PR opened.

Verification

Eight new tests (twenty passing assertions with subtests), every call over the wire per #765 and every refusal pairing the code with the status per #923. TestKMSPlugin_KeyPolicy needed no change but gained a comment recording why it was previously vacuous: the document it writes is byte-identical to the old fallback, so until now it would have passed against a PutKeyPolicy that stored nothing.

Full gate green — gofmt/build/vet, golangci-lint, make test on both toolchains, coverage, the docs targets, the docs site build and e2e. Coverage 85.1% emulator / 84.7% total, holding the 85.1%/84.6% baseline.

Closes #983

#983)

`createKey` decoded no `Policy` member, so a caller that attached a policy at
creation time read back a document it had never sent; and `getKeyPolicy`
answered a hardcoded `{"Version":"2012-10-17","Statement":[]}` for any key that
had never had `PutKeyPolicy` called on it. A key policy is the only place a KMS
key's own permissions live, so an empty-statement document is not a placeholder
— it says the opposite of what AWS attaches.

The default document is transcribed from `API_GetKeyPolicy`'s own Example
Response, which carries an `Id` member the developer-guide prose does not
mention, and is synthesized at read time so keys created by earlier releases
report it too. The key's own account replaces AWS's `111122223333` placeholder.

Three refusals, each published on the page that answers it: a `Policy` outside
1-32768 bytes is `LimitExceededException`/400 (named for this condition by
`CreateKey`'s `Policy` member itself), a document that is not a JSON object is
`MalformedPolicyDocumentException`/400, and a `PolicyName` other than `default`
is `NotFoundException`/400. Both handlers now load the key, so both answer
`NotFoundException`, and the member checks run before the lookup per #991.

Four published behaviours are recorded rather than half-built: the lockout
safety check and hence `BypassPolicyLockoutSafetyCheck`, statement-level
validation (AWS states such requests succeed), the `Policy` pattern, and
`InvalidArnException`. A fifth, `KMSInvalidStateException`, is unreachable
because the key-state table gives both operations a footnote-free checkmark in
all seven columns — reversing this release's plan, which called for a key-state
refusal.

Closes #983
@scttfrdmn scttfrdmn added this to the v0.119.0 milestone Sep 18, 2026
@codecov

codecov Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.06173% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/kms_plugin.go 91.17% 3 Missing ⚠️
emulator/kms_key_policy.go 97.87% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 9631f51 into main Sep 18, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the feat/983-key-policy branch September 18, 2026 01:35
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.

kms: CreateKey drops Policy, and GetKeyPolicy answers an empty-statement document instead of AWS's default key policy

1 participant