Repository navigation
fix(kms): the deletion pair check a key state, and cancelling leaves it disabled (#963) - #967
Conversation
…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.
|
Filed while this was in CI, because it bears directly on the guarantee this PR adds: #968 — 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 It is deliberately not in this diff. Both still go through |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
CancelKeyDeletionandScheduleKeyDeletionare 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 forCancelKeyDeletion.The resulting state was
Enabled, where AWS leaves the keyDisabledAPI_CancelKeyDeletion's first sentence is explicit: "Cancels the deletion of a KMS key. When this operation succeeds, the key state of the KMS key isDisabled. To enable the KMS key, useEnableKey."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
EnableKeystep missing and then failed against AWS.The test asserts the state by encrypting, not by reading
KeyStateback.Encryptmust still refuse withDisabledException/400 after the cancel, and succeed only onceEnableKeyhas run. Reading the state back says it changed;Encryptrefusing says it means what it says — and a handler that wrote the string while leaving the key usable would satisfy the weaker assertion.API_KeyMetadatastates the invariant tying the two fields together — "Enabled: whenKeyStateisEnabledthis 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
CancelKeyDeletionPendingDeletiononly[4], "KMSInvalidStateException:<key ARN>is not pending deletion"ScheduleKeyDeletionPendingDeletion— footnote[3];Updating— footnote[15], unreachableCancelKeyDeletionis 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
EnableKeyunder another name — reachable by a caller holdingkms:CancelKeyDeletionand notkms:EnableKey.On
ScheduleKeyDeletionthe 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.DescribeKeynow reportsDeletionDate, which is what makes that refusal assertableThe 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_KeyMetadatapublishes the member and bounds when it appears — "this value is present only when the KMS key is scheduled for deletion, that is, when itsKeyStateisPendingDeletion" — 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.PendingDeletionWindowInDaysis deliberately absent. The page confines it toKeyStatePendingReplicaDeletion, which only a multi-Region primary with surviving replicas reaches and substrate never writes — and its range is 1–365, notScheduleKeyDeletion'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_ScheduleKeyDeletionpublishes four elements; substrate answered three, omittingPendingWindowInDays— which AWS's own sample response carries — and itsKeyIdwas the bare key ID where the page says "the Amazon Resource Name (key ARN)".API_CancelKeyDeletionpublishes exactly one element,KeyId, also the key ARN; substrate answered{}, so a consumer readingresponse["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,365and-5were all accepted, the last silently becoming 30.Substrate's reading, stated as such
DependencyTimeoutException,InvalidArnException,KMSInternalException,KMSInvalidStateException,NotFoundException— nothing describing a bad parameter value.ValidationError/400 comes fromCommonErrors, 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.ValidationErrorrather thanNotFoundException. 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:
PendingWindowInDaysdecodes into anint, so an omitted member and an explicit0are indistinguishable. AWS refuses0and defaults an absent value; substrate defaults both, since telling them apart needs a*intand 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.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.CancelKeyDeletionis refused against both an enabled and a disabled key, and both are read back.PendingWindowInDaysrather than by arithmetic onDeletionDate, which would be re-deriving the implementation.CancelKeyDeletionthat left the key permanently unschedulable, or aScheduleKeyDeletionthat 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.goassertedEnabledafter a cancel, andkms_error_status_test.go's nominal-paths loop calledCancelKeyDeletionagainst 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
CancelKeyDeletionleaves a keyDisabledwhere it left itEnabled, and answers{"KeyId": "<key ARN>"}where it answered{}; against a key that is not pending deletion it answersKMSInvalidStateException/400 where it answered 200 and enabled the key.ScheduleKeyDeletion'sKeyIdis now the key ARN rather than the key ID, its body carriesPendingWindowInDays, a value outside 7–30 answersValidationError/400 where it was accepted, and a key already pending deletion answersKMSInvalidStateException/400 rather than re-stamping the date.DescribeKeyreports aDeletionDatefor a pending key and, as before, none for any other.Closes #963.