Skip to content

fix(kms): EnableKey and DisableKey refuse a key pending deletion (#968) - #976

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

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

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #968.

EnableKey and DisableKey both delegated to a helper that resolved the key, refused only a missing
one, and then assigned whatever state it was handed. So EnableKey against a key in PendingDeletion
answered 200 and wrote KeyState: Enabled, Enabled: true — a one-call path from pending deletion
to usable
, which AWS does not have.

This is the last member of the key-state class in this package: #949 fixed the rotation pair, #961 the
five cryptographic operations, #963 the deletion pair, and these two are what remained.

Why it mattered more than a missing refusal usually does

It defeated a guarantee another operation in the same file had just established. #963 made
CancelKeyDeletion leave a key Disabled, because API_CancelKeyDeletion says recovery from a
scheduled deletion is two calls — "when this operation succeeds, the key state of the KMS key is
Disabled. To enable the KMS key, use EnableKey"
— and an unguarded EnableKey skipped the first
one. It also abandoned the deletion silently: the shared helper cleared the deletion date on its
way through, so nothing was left to say a deletion had ever been scheduled.

What AWS publishes

EnableKey and DisableKey have identical rows in the developer guide's Key states of AWS KMS keys
table:

Key state Answered Provenance
Enabled 200 Permitted — neither operation refuses a redundant call
Disabled 200 Permitted too, which is why the rotation rule could not be reused: an enabled-key check would refuse a call AWS accepts
PendingDeletion KMSInvalidStateException/400 Footnote [3], which names the state — so kmsInvalidKeyState serves, with no sibling needed as footnote [4]'s negation required for CancelKeyDeletion
Unavailable — Permitted, not refused. Footnote [12]: "the operation succeeds, but the key state of the KMS key does not change until it becomes available" — a success with a deferred effect, unreachable here and recorded so a later sweep does not read it as a refusal
PendingImport, Creating, Updating — Refused by AWS, unreachable: ScheduleKeyDeletion remains substrate's only writer of a state other than Enabled or Disabled

The one design decision, and why #963 is what settles it

The guard lives in the shared helper rather than in each handler. Both callers refuse the same
single state, so one condition serves both — and #963's counter-example is what makes that worth
stating rather than assuming: CancelKeyDeletion was deliberately moved off that helper, because it
requires the opposite state and nothing else, phrases the absence of it as a negation, and returns
a body. Two callers wanting one refusal belong together; a third wanting the inverse does not.

The date-clearing is removed rather than kept as a safety net. The helper used to clear
DeletionDate, on the reasoning that a key reported Enabled must not still remember when it is due
to be deleted. With the refusal in place, the only state that carries a date cannot reach that line,
and CancelKeyDeletion clears it on the one exit AWS documents — so keeping it would be code no
request can run, which this package records in a comment rather than guards. That is the same
disposition the four unreachable states already have.

Tests

emulator/kms_enable_disable_key_state_test.go, three tests, every state reached through the operation
a consumer would use (#765) and every refusal pairing code with status (#923):

  • RefusesAKeyPendingDeletion — a table over both operations: KMSInvalidStateException/400 with
    the state named, and the refusal wrote nothing — KeyState still PendingDeletion, Enabled still
    false, and the DeletionDate unchanged, read back through DescribeKey. DisableKey is the less
    obvious half and is why the table has two rows: its effect on a pending key looks harmless, but
    allowing it would clear the date and report a state the key is not in.
  • CannotShortCircuitTheTwoStepRecovery — one sequence rather than two tests, because "the short
    cut is refused" and "cancel then enable works" would each pass against a version that refused every
    EnableKey, or against one that let CancelKeyDeletion leave the key Enabled. Encrypt is the
    witness at each step: refused with KMSInvalidStateException while pending, refused with
    DisabledException after the cancel, and succeeding only after EnableKey — so the two codes are
    also shown to carry the two different remedies.
  • PermittedTransitionsAreUnaffected — the half a guard is most likely to get wrong. Both rows
    permit Enabled and Disabled, so a redundant call in either direction still succeeds, and the cycle
    runs repeatedly so the guard cannot depend on how the key arrived at its current state. Also asserts
    no DeletionDate is reported for a key that is not pending deletion.

Mutation-tested: removing the guard, moving it after the write, and widening it to refuse a disabled key
are each caught.

Verification

The nine-command gate passes. Coverage 84.4% emulator / 83.9% total, holding the baseline.

Compatibility

EnableKey and DisableKey against a key pending deletion now answer KMSInvalidStateException/400
where they previously answered 200. A caller recovering from a scheduled deletion must call
CancelKeyDeletion first, as AWS requires. Nothing else changes: both operations still succeed from
Enabled and from Disabled, in either direction.

Both operations delegated to a helper that resolved the key, refused only a
missing one, and then assigned whatever state it was handed, so EnableKey against
a key in PendingDeletion answered 200 and wrote Enabled. That is a one-call path
from pending deletion to usable, which AWS refuses with KMSInvalidStateException
under footnote [3], and it routed around the two-call recovery #963 established
from API_CancelKeyDeletion's own first sentence.

The guard is in the shared helper, because both callers refuse the same single
state — unlike CancelKeyDeletion, which #963 moved off that helper because it
requires the opposite state. It precedes the write, so a refused call leaves the
state, the enabled flag and the deletion date unchanged.
@scttfrdmn scttfrdmn added this to the v0.118.0 milestone Sep 17, 2026
@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 20dbbc5 into main Sep 17, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/kms-enable-disable-key-state branch September 17, 2026 03:24
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: EnableKey and DisableKey check no key state, so EnableKey revives a key pending deletion in one call

1 participant