Skip to content

fix(kms): the deletion pair check a key state, and cancelling leaves it disabled (#963) - #967

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/kms-deletion-pair
Sep 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/kms-deletion-pair

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

CancelKeyDeletion and ScheduleKeyDeletion are each other's inverse and shared one state-setting helper, which is what made six defects one change: that helper writes whatever state it is handed, answers an empty body, and checks nothing. All three are wrong for CancelKeyDeletion.

The resulting state was Enabled, where AWS leaves the key Disabled

API_CancelKeyDeletion's first sentence is explicit: "Cancels the deletion of a KMS key. When this operation succeeds, the key state of the KMS key is Disabled. To enable the KMS key, use EnableKey."

So recovery from a scheduled deletion is two calls, and substrate collapsed it to one — a consumer's recovery path written against substrate would have passed with its EnableKey step missing and then failed against AWS.

The test asserts the state by encrypting, not by reading KeyState back. Encrypt must still refuse with DisabledException/400 after the cancel, and succeed only once EnableKey has run. Reading the state back says it changed; Encrypt refusing says it means what it says — and a handler that wrote the string while leaving the key usable would satisfy the weaker assertion.

API_KeyMetadata states the invariant tying the two fields together — "Enabled: when KeyState is Enabled this value is true, otherwise it is false" — and a test asserts they agree.

Neither operation checked a key state, and the pair is where the table diverges most sharply

Operation Permitted from Refused from
CancelKeyDeletion PendingDeletion only Every other state — footnote [4], "KMSInvalidStateException: <key ARN> is not pending deletion"
ScheduleKeyDeletion Every state but the two at right PendingDeletion — footnote [3]; Updating — footnote [15], unreachable

CancelKeyDeletion is the only operation in the developer guide's Key states of AWS KMS keys table whose permitted set is a single state, and the only one whose footnote is a negation where every other footnote names the offending state. It therefore gets its own refusal helper: naming a state here would read as though some other state were the problem, when the problem is the absence of the one state the operation needs.

Without the check the operation enabled any key it was pointed at, which is EnableKey under another name — reachable by a caller holding kms:CancelKeyDeletion and not kms:EnableKey.

On ScheduleKeyDeletion the gap was quieter and no less real: a second call against an already-pending key recomputed the deletion date and saved it, silently moving a deadline the caller believed was fixed. Updating, that row's other refused state, is recorded as unreachable for the same reason #949's four states are — nothing in substrate writes it.

DescribeKey now reports DeletionDate, which is what makes that refusal assertable

The date was computed inside the handler and discarded, so a caller could learn when a key was due to be deleted exactly once, from the response to the call that set it. That also left the refusal above unassertable: a re-stamped deadline is invisible if the date is thrown away.

API_KeyMetadata publishes the member and bounds when it appears — "this value is present only when the KMS key is scheduled for deletion, that is, when its KeyState is PendingDeletion" — so it is rendered on the key state, not on the field being non-zero. A cancelled deletion leaves no date behind, and the zero value is never emitted as an epoch timestamp.

PendingDeletionWindowInDays is deliberately absent. The page confines it to KeyState PendingReplicaDeletion, which only a multi-Region primary with surviving replicas reaches and substrate never writes — and its range is 1–365, not ScheduleKeyDeletion's 7–30. The two are different members measuring different things, so reporting the waiting period under it would be wrong twice over.

Both response shapes were wrong, and the waiting period was unchecked

API_ScheduleKeyDeletion publishes four elements; substrate answered three, omitting PendingWindowInDays — which AWS's own sample response carries — and its KeyId was the bare key ID where the page says "the Amazon Resource Name (key ARN)". API_CancelKeyDeletion publishes exactly one element, KeyId, also the key ARN; substrate answered {}, so a consumer reading response["KeyId"] got nothing.

The waiting period is range-checked against the published 7–30, stated twice on that page. The previous guard — if days <= 0 { days = 30 } — got the default right and the range not at all: 1, 365 and -5 were all accepted, the last silently becoming 30.

Substrate's reading, stated as such

  • The refusal code. AWS publishes no error for a range violation here: its list is DependencyTimeoutException, InvalidArnException, KMSInternalException, KMSInvalidStateException, NotFoundException — nothing describing a bad parameter value. ValidationError/400 comes from CommonErrors, for the same reason an unparseable body does. The bound is named in the message, because a caller that sent 1 cannot discover 7–30 from a bare refusal.
  • Which check runs first. The range is checked before the key is resolved, so a bad window against an absent key answers ValidationError rather than NotFoundException. AWS does not publish the precedence; a request wrong on its face is refused without a lookup, as an unparseable body already is.

Recorded rather than fixed: PendingWindowInDays decodes into an int, so an omitted member and an explicit 0 are indistinguishable. AWS refuses 0 and defaults an absent value; substrate defaults both, since telling them apart needs a *int and defaulting is much the commoner intent.

Tests

emulator/kms_deletion_pair_test.go — nine tests, 17 cases, every call over the wire and every state reached through the operation a consumer would use, per #765.

  • Response bodies are asserted on raw JSON (map[string]json.RawMessage), because a decoded map cannot tell an absent member from a zero one — and two of the six defects are precisely an absent member.
  • Each refusal is followed by a read-back, so a handler that mutated and then returned the error would fail. CancelKeyDeletion is refused against both an enabled and a disabled key, and both are read back.
  • The waiting-period range is asserted on both sides of both bounds (−5, 6, 7, 30, 31, 365), so the guard cannot be off by one, and each refusal is shown to have scheduled nothing.
  • The default is asserted through the echoed PendingWindowInDays rather than by arithmetic on DeletionDate, which would be re-deriving the implementation.
  • A round-trip test schedules, cancels, enables and schedules again with a different window — without it, a CancelKeyDeletion that left the key permanently unschedulable, or a ScheduleKeyDeletion that refused any key it had seen, would pass the rest of the file.

Two existing tests recorded the old behaviour and are corrected with the citation: v016_extra_test.go asserted Enabled after a cancel, and kms_error_status_test.go's nominal-paths loop called CancelKeyDeletion against an enabled key — it now runs in the tail sequence, which became the actual lifecycle: DisableKey → ScheduleKeyDeletion → CancelKeyDeletion, each the precondition of the next.

Mutation-checked: eight mutations, one per defect fixed, each caught by the test that names it — 1 to 5 failures apiece, none passing. Restoring returns everything to green.

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

Compatibility

CancelKeyDeletion leaves a key Disabled where it left it Enabled, and answers {"KeyId": "<key ARN>"} where it answered {}; against a key that is not pending deletion it answers KMSInvalidStateException/400 where it answered 200 and enabled the key. ScheduleKeyDeletion's KeyId is now the key ARN rather than the key ID, its body carries PendingWindowInDays, a value outside 7–30 answers ValidationError/400 where it was accepted, and a key already pending deletion answers KMSInvalidStateException/400 rather than re-stamping the date. DescribeKey reports a DeletionDate for a pending key and, as before, none for any other.

Closes #963.

…it disabled (#963)

CancelKeyDeletion and ScheduleKeyDeletion are each other's inverse and shared one
state-setting helper, which is what made six defects one change: that helper writes
whatever state it is handed, answers an empty body, and checks nothing. All three are
wrong for CancelKeyDeletion.

The resulting state is now Disabled. API_CancelKeyDeletion's first sentence is
explicit — "when this operation succeeds, the key state of the KMS key is Disabled. To
enable the KMS key, use EnableKey" — so recovery from a scheduled deletion is two
calls, and substrate collapsed it to one. The test asserts it by encrypting rather
than by reading KeyState back: Encrypt still refuses with DisabledException/400 after
the cancel and succeeds only once EnableKey has run.

Both operations now check the key state. CancelKeyDeletion is the only operation in
the key-state table whose permitted set is a single state, every other row being
footnote [4], and that footnote is a negation — "is not pending deletion" — so it gets
its own refusal helper rather than naming a state. Without the check the operation
enabled any key it was pointed at, which is EnableKey under another name, reachable by
a caller holding kms:CancelKeyDeletion and not kms:EnableKey. On ScheduleKeyDeletion
the gap was quieter: a second call against an already-pending key recomputed the
deletion date and saved it, silently moving a deadline the caller believed was fixed.

DescribeKey now reports DeletionDate, which is what makes that refusal assertable. The
date was computed and discarded, so a caller could learn it exactly once.
API_KeyMetadata confines the member to KeyState PendingDeletion, so it is rendered on
the state rather than on the field being non-zero. PendingDeletionWindowInDays is
deliberately absent: the page confines it to PendingReplicaDeletion and gives it a
1-365 range, not ScheduleKeyDeletion's 7-30.

Both response shapes were wrong and the waiting period was unchecked.
ScheduleKeyDeletion omitted PendingWindowInDays and answered a bare key ID where the
page says key ARN; CancelKeyDeletion answered {} where the page publishes one element.
The previous window guard, "if days <= 0 { days = 30 }", got the default right and the
range not at all.

Two decisions are substrate's reading: the range violation answers ValidationError/400
from CommonErrors, since the operation publishes nothing describing a bad parameter
value, and the range is checked before the key is resolved, AWS not publishing the
precedence.

Mutation-checked: eight mutations, one per defect, each caught by the test that names
it — 1 to 5 failures apiece, none building green.

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

Closes #963.
@scttfrdmn

Copy link
Copy Markdown
Owner Author

Filed while this was in CI, because it bears directly on the guarantee this PR adds: #968 — EnableKey and DisableKey check no key state either, so EnableKey against a key in PendingDeletion answers 200 and enables it.

That is a one-call path from pending deletion to usable, which means the two-step recovery this PR establishes can be routed around by an operation in the same file. Verified from the key-state table: both operations refuse PendingDeletion with footnote [3], KMSInvalidStateException: <key ARN> is pending deletion (or pending replica deletion).

It is deliberately not in this diff. Both still go through setKeyState — the helper this PR moved cancelKeyDeletion off — so where the shared guard belongs is its own decision, and widening this change to two more operations would put a third wire change in a diff that already carries six.

@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.91525% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/kms_plugin.go 93.61% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 98bb0f5 into main Sep 17, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/kms-deletion-pair branch September 17, 2026 02:00
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: ScheduleKeyDeletion and CancelKeyDeletion check no key state, and CancelKeyDeletion leaves the key Enabled where AWS leaves it Disabled

1 participant