Skip to content

fix(kms): EnableKeyRotation and DisableKeyRotation check the key state (#949) - #962

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/kms-rotation-key-state
Sep 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/kms-rotation-key-state

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Both operations carry the sentence "the KMS key that you use for this operation must be in a compatible key state" and publish an identical seven-error list, and substrate checked nothing: it wrote RotationEnabled and answered 200 whatever the key was. #923 recorded the gap while correcting DisabledException's status and named it as a missing refusal rather than folding it into a status change.

The two forbidding states answer different codes, and that is the whole of the fix

The developer guide's Key states of AWS KMS keys table gives these two operations one row each with identical contents:

Key state Answer Provenance
Enabled 200 Permitted
Disabled DisabledException/400 Footnote [1] — "DisabledException: <key ARN> is disabled"
PendingDeletion KMSInvalidStateException/400 Footnote [3] — "KMSInvalidStateException: <key ARN> is pending deletion (or pending replica deletion)"

Both states are reachable in substrate, because ScheduleKeyDeletion writes KeyState and clears Enabled in a single write. So a lone !key.Enabled test — the obvious reading of the issue title — would have answered DisabledException for a key pending deletion: the wrong one of the two codes AWS publishes, and one that points a caller at the wrong remedy (enable the key and retry versus cancel the deletion and retry). PendingDeletion is therefore checked first, and a new KMSInvalidStateException helper joins the four kms_errors.go already had.

The check runs before the write, so a refused call leaves RotationEnabled as it was — asserted by reading it back through GetKeyRotationStatus over the wire rather than by inspecting state, since a guard placed after the assignment would answer the right code while having already changed the value.

GetKeyRotationStatus is deliberately not guarded

Its row in the same table permits Enabled, Disabled and PendingDeletion alike, and API_GetKeyRotationStatus publishes neither code. A test pins that, so a later sweep guarding "every key-state-sensitive operation" for consistency with its two siblings cannot quietly introduce a refusal AWS does not have.

Recorded rather than implemented

The four remaining states AWS's table refuses — PendingImport, Unavailable, Creating, Updating — are unreachable: ScheduleKeyDeletion is substrate's only writer of a state other than Enabled or Disabled, so no caller can reach them and a guard for them could only be exercised by fabricating a key record, which proves nothing about the wire.

Out of scope, and filed: Encrypt, Decrypt and GenerateDataKey answer DisabledException for a key pending deletion, where their own pages publish KMSInvalidStateException at 400 beside it and their table rows are footnote [3] too. That is #961; widening this change to three cryptographic operations would put a separate wire change in the same diff.

kmsKeyStatePendingDeletion becomes a constant while Enabled and Disabled stay literals, because it is the one key state substrate compares against rather than only writes: a typo in either the writer or the guard would silently stop the guard from firing instead of failing to compile.

Tests

emulator/kms_rotation_key_state_test.go — every call over the wire, every state reached through the operation a consumer would use (DisableKey, ScheduleKeyDeletion) rather than by writing a key record, per #765.

  • Each of the two states asserted against its own code on both operations, with the status alongside the code per fix(kms): NotFoundException is HTTP 400, and InvalidRequest is not a KMS code #923's finding that a code alone let a wrong status stand. A single-state test could not tell the two codes apart.
  • The refusal wrote nothing, in both directions and not symmetrically: rotation is turned on first before DisableKeyRotation is refused, so a handler that assigned before guarding reports the wrong value where no ordering of a correct implementation does.
  • GetKeyRotationStatus answers 200 against a disabled and a pending-deletion key, and still reports the value set before the state changed.
  • The nominal path: an enabled key accepts both operations, in both directions, twice through (an idempotent repeat is what a guard reading RotationEnabled instead of the key state would refuse), and a key re-enabled after DisableKey rotates again — so the refusals are a function of the current state rather than of anything the key remembers. Without this, a handler refusing every key would satisfy every other assertion in the file.

Mutation-checked: dropping the PendingDeletion arm, and moving enableKeyRotation's guard to after the assignment, fail 9 of these tests between them; all pass against the change.

Coverage: 84.4% emulator / 83.9% total, unchanged.

Compatibility

EnableKeyRotation and DisableKeyRotation against a disabled key now answer DisabledException/400 where they answered 200, and against a key scheduled for deletion KMSInvalidStateException/400. A caller that turned rotation on or off before disabling or scheduling the key sees no change, and neither does GetKeyRotationStatus in any state.

Closes #949.

#949)

Both operations carry the sentence "the KMS key that you use for this operation
must be in a compatible key state" and publish an identical seven-error list, and
substrate checked nothing: it wrote RotationEnabled and answered 200 whatever the
key was. #923 recorded the gap while correcting DisabledException's status and
named it as a missing refusal rather than folding it into a status change.

The two forbidding states answer different codes, and that is the whole of the
fix. The developer guide's "Key states of AWS KMS keys" table gives these two
operations one row each with identical contents: a Disabled key is footnote [1],
"DisabledException: <key ARN> is disabled", while a key pending deletion is
footnote [3], "KMSInvalidStateException: <key ARN> is pending deletion". Both
states are reachable in substrate, because ScheduleKeyDeletion writes the state
and clears the enabled flag in a single write, so a lone !key.Enabled test would
have answered DisabledException for a key pending deletion — the wrong one of the
two codes AWS publishes, and one that points a caller at the wrong remedy: enable
the key and retry, versus cancel the deletion and retry. PendingDeletion is
checked first, and a new KMSInvalidStateException helper joins the four
kms_errors.go already had.

The check runs before the write, so a refused call leaves RotationEnabled as it
was. That is asserted by reading it back through GetKeyRotationStatus over the
wire rather than by inspecting state, since a guard placed after the assignment
would answer the right code while having already changed the value.

GetKeyRotationStatus is deliberately not guarded, and a test pins it. Its row in
the same table permits Enabled, Disabled and PendingDeletion alike and
API_GetKeyRotationStatus publishes neither code, so a later sweep guarding "every
key-state-sensitive operation" cannot quietly introduce a refusal AWS does not
have.

The four remaining states AWS's table refuses — PendingImport, Unavailable,
Creating, Updating — are recorded as unreachable rather than implemented, because
ScheduleKeyDeletion is substrate's only writer of a state other than Enabled or
Disabled: no caller can reach them, and a guard for them could only be exercised
by fabricating a key record. Left open by the same reading and filed as #961:
Encrypt, Decrypt and GenerateDataKey answer DisabledException for a key pending
deletion, where their pages publish KMSInvalidStateException beside it. Widening
this change to three cryptographic operations would put a separate wire change in
the same diff.

kmsKeyStatePendingDeletion becomes a constant while Enabled and Disabled stay
literals, because it is the one key state substrate compares against rather than
only writes: a typo in either the writer or the guard would silently stop the
guard from firing instead of failing to compile.

Compatibility: EnableKeyRotation and DisableKeyRotation against a disabled key now
answer DisabledException/400 where they answered 200, and against a key scheduled
for deletion KMSInvalidStateException/400. A caller that turned rotation on or off
before disabling or scheduling the key sees no change, and neither does
GetKeyRotationStatus in any state.

Mutation-checked: dropping the PendingDeletion arm, and moving enableKeyRotation's
guard to after the assignment, fail 9 of these tests between them; all pass
against the change.

Coverage: 84.4% emulator / 83.9% total, unchanged.

Closes #949.
@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 8a7f80d into main Sep 17, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/kms-rotation-key-state branch September 17, 2026 01:43
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: EnableKeyRotation and DisableKeyRotation succeed against a disabled key, where AWS publishes DisabledException/400

1 participant