Repository navigation
feat(kms): the default key policy, and the policy operations' refusals (#983) - #1048
Merged
Merged
Conversation
#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
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two defects with one cause:
createKeydecoded noPolicymember at all, andgetKeyPolicyanswered a hardcoded{"Version":"2012-10-17","Statement":[]}for any key that had never hadPutKeyPolicycalled 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 anIdmember,key-default-1, that the prose does not mention.Version2012-10-17Idkey-default-1Statement[0].SidEnable IAM User PermissionsStatement[0].EffectAllowStatement[0].Principal.AWSarn:aws:iam::{account}:rootStatement[0].Actionkms:*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
Policyoutside 1–32768 bytes, onCreateKeyorPutKeyPolicyLimitExceededException/400Policythat is not JSON, or is JSON but not an objectMalformedPolicyDocumentException/400PolicyNameother thandefault, onGetKeyPolicyorPutKeyPolicyNotFoundException/400KeyIdnaming no key, on either operationNotFoundException/400LimitExceededExceptionfor a length violation is a transcription, not an inference from the error's gloss —CreateKey'sPolicymember names the code itself: "if the key policy exceeds the length constraint, AWS KMS returns aLimitExceededException". That is what settles an oversize document's otherwise plausible claim onMalformedPolicyDocumentException. The length is checked before the parse, so 40 KB of valid JSON hears about its size.NotFoundExceptionfor aPolicyNameis substrate's one-step reading: neither page publishes a code for that member, but both statedefaultis the only valid value and both publishNotFoundExceptionglossed "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 forCommonErrors.Both handlers now call
loadKey, closing a gapkms_error_status_test.gohad 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:
BypassPolicyLockoutSafetyCheck, which stays undecoded on both operations. The check is a policy evaluation against the calling principal; with no check to bypass,trueandfalseare 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.ActionorResource"has no effect" while "theCreateKeyandPutKeyPolicyAPI requests succeed", soMalformedPolicyDocumentException's "semantically correct" gloss is narrower than it reads.Policycharacter-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
PutKeyPolicyto refuse a key pending deletion, and the plan namedkmsInvalidKeyStateas the helper to use. The developer guide's key-state table settles it the other way:GetKeyPolicyandPutKeyPolicycarry a checkmark in all seven state columns — Enabled, Disabled, Pending deletion, Pending import, Unavailable, Creating, Updating — with no footnote on any of them, whereTagResourcetwo 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
kmsKeyStateErrornorkmsInvalidKeyStateis called from either handler.KMSInvalidStateExceptionis recorded as published-but-unreachable, andTestKMSKeyPolicy_NeitherOperationRefusesAKeyForItsStateasserts 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_KeyPolicyneeded 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 aPutKeyPolicythat stored nothing.Full gate green —
gofmt/build/vet,golangci-lint,make teston 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