Skip to content

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

Closed

Description

@scttfrdmn

CreateKey's Policy parameter is decoded by nobody, and GetKeyPolicy answers {"Version":"2012-10-17","Statement":[]} for any key that has never had PutKeyPolicy called on it. So no key created through substrate has a key policy that grants anything, and a caller that attached one at creation time cannot read it back.

Found while verifying API_CreateKey for #977.

What substrate does today

createKey (emulator/kms_plugin.go) decodes five members — Description, KeyUsage, KeySpec, MultiRegion, Tags — and Policy is not among them. Nothing writes kmsPolicyKey(...) except putKeyPolicy.

getKeyPolicy (emulator/kms_plugin.go:653-681) falls back to an empty-statement document:

policy := `{"Version":"2012-10-17","Statement":[]}`
if data != nil {
	policy = string(data)
}

So there are two defects with one cause:

  1. CreateKey's Policy is silently dropped. A caller that creates a key with a policy and then reads it back gets a document it did not send — the response-member analogue of cfn: the aws:cloudformation:* stamp covers EC2 only, so the rest of a stack is unstamped #765, aimed at a request parameter this time.
  2. A key with no explicit policy reports a policy that grants nothing, where AWS attaches a default that grants the account root full access to the key.

What AWS publishes

API_CreateKey's Policy parameter, verbatim: "The key policy to attach to the KMS key. … If you do not provide a key policy, AWS KMS attaches a default key policy to the KMS key." Type String, length 1–32768, pattern [\u0009\u000A\u000D\u0020-\u00FF]+.

API_CreateKey publishes MalformedPolicyDocumentException/400 — "the request was rejected because the specified policy is not syntactically or semantically correct" — and LimitExceededException/400 for a length constraint, with the parameter's own note: "if the key policy exceeds the length constraint, AWS KMS returns a LimitExceededException."

The Default key policy page states what the default document contains: a single statement with Sid Enable IAM User Permissions, Principal the account root ARN, Action kms:* and Resource *. Verify the exact document against that page before implementing, since the empty-statement stand-in is currently the only thing distinguishing a substrate key policy from AWS's.

API_CreateKey also notes a substrate-relevant asymmetry worth recording rather than implementing: "If either of the required Resource or Action elements are missing from a key policy statement, the policy statement has no effect. When a key policy statement is missing one of these elements, the AWS KMS console correctly reports an error, but the CreateKey and PutKeyPolicy API requests succeed." So the semantic check AWS advertises is narrower than it looks.

Why the default document 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. Substrate's authz work (#671 and the authzref data) reads IAM; if a key policy is ever consulted, an empty-statement default would deny every principal, so the stand-in is not a neutral placeholder. It is a document that says the opposite of AWS's default.

putKeyPolicy (:683-702) has the same class of gap and belongs in the same PR: it validates no length, refuses no malformed document, checks no key state, and ignores PolicyName (AWS supports only default, and getKeyPolicy already hardcodes that value in its response).

Acceptance criteria

  • CreateKey records the Policy it was given, readable through GetKeyPolicy over the wire.
  • A key created with no Policy reports AWS's default key policy, with the exact document verified against the Default key policy page at implementation time and any deviation recorded as substrate's reading.
  • Policy length outside 1–32768 answers LimitExceededException/400 on both CreateKey and PutKeyPolicy, per the parameter's own note.
  • A document that is not syntactically valid JSON answers MalformedPolicyDocumentException/400. Semantic validation beyond that is decided and recorded — AWS's own page says a statement missing Action or Resource still succeeds, so "semantically correct" is narrower than it reads.
  • PutKeyPolicy refuses a key pending deletion, per the key-state table, or the omission is recorded with the footnote that permits it.
  • PolicyName handling is decided: AWS supports only default, and getKeyPolicy already answers that string unconditionally.
  • Tests go over the wire (cfn: the aws:cloudformation:* stamp covers EC2 only, so the rest of a stack is unstamped #765) and assert the status alongside the code (fix(kms): NotFoundException is HTTP 400, and InvalidRequest is not a KMS code #923).

Activity

  1. added this to the v0.119.0 milestone on Sep 17, 2026
  2. added
    type: bug
    on Sep 17, 2026
  3. scttfrdmn commented on Sep 18, 2026

    @scttfrdmn
    OwnerAuthor

    Premise corrections before the PR opens, all verified against the tree at bd0e338 and against the AWS pages today.

    The cited lines have drifted, and one of them names the wrong count. createKey decodes six members, not the five listed — #985 added CustomerMasterKeySpec and #984 added Origin, CustomKeyStoreId and XksKeyId, so the current set is Description, KeyUsage, KeySpec, CustomerMasterKeySpec, Origin, CustomKeyStoreId, XksKeyId, MultiRegion, Tags. Policy is still absent from it, which is the defect. getKeyPolicy is at kms_plugin.go:689-717 (not :653-681) and putKeyPolicy at :719-738 (not :683-702).

    The default document does not need a developer-guide trip. API_GetKeyPolicy's own Example Response carries it in full, which is a stronger source than the Default key policy page the issue points at:

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

    Note the "Id" : "key-default-1" member, which the developer-guide prose does not mention and which the issue's summary of the document therefore omits.

    Two gaps the issue does not name.

    1. Neither handler calls loadKey, so neither answers NotFoundException for a key that does not exist — both pages publish it at 400. This is already recorded in the tree, at kms_error_status_test.go:102-103: "GetKeyPolicy and PutKeyPolicy are absent because substrate does not check the key exists on either — a separate gap, not a status." That table entry and its comment change with this issue.
    2. BypassPolicyLockoutSafetyCheck is decoded nowhere, on either CreateKey or PutKeyPolicy. It only matters if the lockout check exists to be bypassed, which decides the scope question below.

    Correction to the fifth acceptance criterion: there is no key-state refusal to add. The criterion asks for PutKeyPolicy to refuse a key pending deletion "per the key-state table, or the omission recorded with the footnote that permits it". The key-state table permits it: the PutKeyPolicy and GetKeyPolicy rows are a green checkmark in all seven state columns — Enabled, Disabled, Pending deletion / Pending replica deletion, Pending import, Unavailable, Creating, Updating — with no footnote marker on any of them. Compare TagResource, two rows down, which is an X at Pending deletion carrying footnote [3]. So the correct outcome is that substrate adds no key-state check here, and KMSInvalidStateException stays unreachable on both operations even though both publish it. That is a published-but-unreachable code rather than an omission, and it is recorded rather than implemented.

    This also disposes of the trap the release plan flagged: since no key-state refusal is added, neither kmsKeyStateError nor kmsInvalidKeyState is called from either handler. (kmsKeyStateError would have been wrong regardless — it can answer DisabledException, which PutKeyPolicy does not publish.)

    PolicyName for a value other than default resolves to NotFoundException/400. Both pages state "The only valid value is default" and both publish NotFoundException, glossed "the specified entity or resource could not be found" — a policy name that names no policy is squarely that, and it keeps the refusal on the page rather than reaching for CommonErrors. Recording it as a one-step reading.

    Scope: the lockout safety check is not implemented, so BypassPolicyLockoutSafetyCheck stays undecoded. The check AWS describes is "the key policy must allow the calling principal to make a subsequent PutKeyPolicy request on the KMS key" — that is a policy evaluation against the caller's principal, which is #671's authorization machinery and not something this issue can reach. With no lockout check, true and false produce identical observable behaviour, so decoding the member would add a value read by nobody: exactly the defect class #984 just closed. It is recorded as an omission on both operations instead.

    Semantic validation is syntax plus "the document is a JSON object". The issue is right that AWS's own page narrows "semantically correct" almost to nothing — "if either of the required Resource or Action elements are missing from a key policy statement … the CreateKey and PutKeyPolicy API requests succeed, even though the policy statement is ineffective" — so no statement-level check is defensible. Substrate refuses a body that is not valid JSON and a body that is valid JSON but not an object ([], "x", 5), on the IAM JSON Policy Reference's terms, and records that Policy's published pattern (the printable-ASCII-plus-Latin-1 character class, tab/LF/CR included) stays unenforced because neither page publishes a code for a pattern violation. InvalidArnException — "a specified ARN, or an ARN in a key policy, is not valid" — likewise stays unreachable, since nothing walks the principals.

    The default is synthesised at read time rather than written at create time. Observationally identical for a key created after this ships, and it also means every key created before it reports the default rather than the empty-statement stand-in — no migration, and no state write on a path AWS describes as attaching a document.

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

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions