Repository navigation
fix(kms): EnableKey and DisableKey refuse a key pending deletion (#968) - #976
Merged
Merged
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 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.
Closes #968.
EnableKeyandDisableKeyboth delegated to a helper that resolved the key, refused only a missingone, and then assigned whatever state it was handed. So
EnableKeyagainst a key inPendingDeletionanswered
200and wroteKeyState: Enabled,Enabled: true— a one-call path from pending deletionto 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
CancelKeyDeletionleave a keyDisabled, becauseAPI_CancelKeyDeletionsays recovery from ascheduled deletion is two calls — "when this operation succeeds, the key state of the KMS key is
Disabled. To enable the KMS key, useEnableKey" — and an unguardedEnableKeyskipped the firstone. 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
EnableKeyandDisableKeyhave identical rows in the developer guide's Key states of AWS KMS keystable:
EnabledDisabledPendingDeletionKMSInvalidStateException/400[3], which names the state — sokmsInvalidKeyStateserves, with no sibling needed as footnote[4]'s negation required forCancelKeyDeletionUnavailable[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 refusalPendingImport,Creating,UpdatingScheduleKeyDeletionremains substrate's only writer of a state other thanEnabledorDisabledThe 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:
CancelKeyDeletionwas deliberately moved off that helper, because itrequires 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 reportedEnabledmust not still remember when it is dueto be deleted. With the refusal in place, the only state that carries a date cannot reach that line,
and
CancelKeyDeletionclears it on the one exit AWS documents — so keeping it would be code norequest 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 operationa consumer would use (#765) and every refusal pairing code with status (#923):
RefusesAKeyPendingDeletion— a table over both operations:KMSInvalidStateException/400 withthe state named, and the refusal wrote nothing —
KeyStatestillPendingDeletion,Enabledstillfalse, and the
DeletionDateunchanged, read back throughDescribeKey.DisableKeyis the lessobvious 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 shortcut is refused" and "cancel then enable works" would each pass against a version that refused every
EnableKey, or against one that letCancelKeyDeletionleave the keyEnabled.Encryptis thewitness at each step: refused with
KMSInvalidStateExceptionwhile pending, refused withDisabledExceptionafter the cancel, and succeeding only afterEnableKey— so the two codes arealso shown to carry the two different remedies.
PermittedTransitionsAreUnaffected— the half a guard is most likely to get wrong. Both rowspermit
EnabledandDisabled, so a redundant call in either direction still succeeds, and the cycleruns repeatedly so the guard cannot depend on how the key arrived at its current state. Also asserts
no
DeletionDateis 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
EnableKeyandDisableKeyagainst a key pending deletion now answerKMSInvalidStateException/400where they previously answered
200. A caller recovering from a scheduled deletion must callCancelKeyDeletionfirst, as AWS requires. Nothing else changes: both operations still succeed fromEnabledand fromDisabled, in either direction.