Skip to content

fix(kms): rotation checks the key type, and reports when the next one is (#972, #973) - #981

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/kms-rotation-key-type-and-next-rotation-date
Sep 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/kms-rotation-key-type-and-next-rotation-date

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #972.
Closes #973.

The rotation trio owed three conditions and checked one and a half. #964 added the period's range, #949 added the key state, and neither issue looked at the key type — nor at what GetKeyRotationStatus actually answers, which turned out to be wrong in one state rather than merely incomplete. Both halves touch the same two handlers and the same response builder, so they ship together.

#972 — a key type can forbid rotation permanently

EnableKeyRotation on an RSA key answered 200 and wrote RotationEnabled, after which GetKeyRotationStatus reported a rotation schedule for a key AWS will never rotate. AWS states the restriction twice on API_EnableKeyRotation and repeats it verbatim on API_DisableKeyRotation and API_GetKeyRotationStatus: "automatic key rotation is supported only on symmetric encryption KMS keys. You cannot enable automatic rotation of asymmetric KMS keys, HMAC KMS keys, KMS keys with imported key material, or KMS keys in a custom key store."

Both operations now answer UnsupportedOperationException/400 — the inadmissible-resource case whose gloss #964 declined to borrow for an out-of-range number. DisableKeyRotation refuses it too, although turning rotation off on a key that will never rotate looks harmless; that is the argument #949 rejected for the key state, and the pages carry the identical sentence and the identical seven-error list.

Three decisions worth naming:

  • KeySpec, not KeyUsage. One equality test against SYMMETRIC_DEFAULT covers every family AWS names and the two it implies — asymmetric encryption, asymmetric signing, SM2, HMAC, ML-DSA — where a KeyUsage test would let an ENCRYPT_DECRYPT RSA key through. The message names the spec it found, because the code is otherwise indistinguishable from the AWS-managed-key refusal a caller might expect.
  • The key type is answered before the key state, which AWS publishes no precedence for. Substrate's reading, argued from the remedy: a key state has one (EnableKey, or CancelKeyDeletion then EnableKey) and a key type has none, so telling a caller with a disabled RSA key to enable the key sends it round a loop that cannot terminate. Same request-before-resource principle as kms: EnableKeyRotation decodes only KeyId, so RotationPeriodInDays is accepted and discarded #964's range check.
  • GetKeyRotationStatus is deliberately not refused for such a key, and a test pins that. The sentence forbids enabling rotation, not asking whether it rotates; the operation's key-state row permits the read; and false for a key that will never rotate is a true answer.

Three of AWS's five restrictions stay unenforced because they are unreachable rather than unguarded — imported key material, a custom key store and an AWS managed key each need a KMSKey member substrate does not model, so no request can produce one. Every non-symmetric key in the tests is created through the gap #977 records (CreateKey validates KeySpec against nothing), which is why #977 has to re-verify these tests when it closes it.

#973 — NextRotationDate, and the answer for a key pending deletion

The member was missing entirely, and it is the one rotation-monitoring code reads. AWS defines it on API_EnableKeyRotation rather than where it is reported — "the rotation period defines the number of days after you enable automatic key rotation that AWS KMS will rotate your key material, and the number of days between each automatic rotation thereafter" — so it is the enable date plus the period, and substrate held only the period. KMSKey gained RotationEnabledDate for this and nothing else. It is reported as Unix seconds, matching what DescribeKey does with DeletionDate.

A second EnableKeyRotation overwrites the enable date, which AWS does not address. The alternative argues for it: a key enabled with 2560 days and re-enabled with 90 would otherwise report a next rotation date roughly six years in the past. DisableKeyRotation leaves both the period and the date alone; what changes is whether they are reported.

A key pending deletion reported the stored flag, and AWS publishes false: "while a KMS key is pending deletion, its key rotation status is false and AWS KMS does not rotate the key material. If you cancel the deletion, the original key rotation status returns to true." A caller polling a key it had scheduled for deletion was told rotation was still on. That was a wrong value rather than a missing refusal, which is the distinction that kept it out of #949 — that issue read the same two sentences as a reason not to guard the operation and stopped there.

The second sentence forces the shape of the fix. The reported status is derived from the stored flag and the key state at read time rather than written by ScheduleKeyDeletion, because there has to be something left to restore from. An implementation that cleared the flag on the way in would satisfy the first sentence and make the second unimplementable, and no state inspection would tell the two apart — so the test walks enable → schedule → cancel and asserts the original date comes back rather than a fresh one counted from the cancel. The period and the date follow the reported status, so a key pending deletion reports neither: answering a schedule beside a false status would describe a rotation AWS has just said will not happen.

OnDemandRotationStartDate stays absent, and every read in the new test file asserts that rather than one test doing so. It reports an in-progress RotateKeyOnDemand, which substrate does not implement, so no request can set it and a zero timestamp would report a rotation nobody started.

Two existing tests changed, and why that is the finding

kms_rotation_key_state_test.go was asserting the wrong one of the two documented values: it read the stored flag back in both states and required it unchanged in both, which is right for Disabled and wrong for pending deletion. Both assertions now follow the sentence for each state, and the DisableKeyRotation half goes one step further — it cancels the deletion and reads true back, so the stored flag is still shown to have survived the refusal in the state where the read-back would otherwise see only #973's derived false. Without that step the strongest half of #949's third criterion would be invisible in exactly the state where the guard is newest.

Provenance

  • Published by AWS: the symmetric-only sentence, verbatim on all three rotation pages; UnsupportedOperationException/400 and its gloss; the pending-deletion false and its restoration on cancel; NextRotationDate's definition, via API_EnableKeyRotation's definition of a rotation period; the 90–2560 range at both ends of the round trip.
  • Substrate's reading: the key type answered before the key state (AWS publishes no precedence); a second enable overwriting the enable date (AWS documents no answer); the period and date omitted while the reported status is off (the honest-empty behaviour cfn: Ref returns the physical ID for every type, but AWS returns an ARN for an ELBv2 load balancer and target group #827 established); Unix seconds for the timestamp — AWS's page disagrees with itself here, typing the member number in its Response Syntax and rendering "2024-02-14T18:14:33.587000+00:00" in its sample, so one KMS timestamp convention is a choice rather than a match.

Verification

Nine-command gate green. Coverage 84.4% emulator / 83.9% total, above the 84.3/83.8 baseline. Five mutants introduced and all five caught: swapping the two guards' order, reporting the stored flag instead of deriving it, keeping the first enable date on a re-enable, dropping the key-spec guard from DisableKeyRotation, and reporting the schedule while rotation is off.

… is (#972, #973)

EnableKeyRotation and DisableKeyRotation checked the period's range (#964) and the
key state (#949) and never checked what kind of key they were pointed at, so
EnableKeyRotation on an RSA key answered 200 and wrote the flag — after which
GetKeyRotationStatus reported a rotation schedule for a key AWS will never rotate.
Both now answer UnsupportedOperationException/400, with the key type checked before
the key state: a key state has a remedy and a key type has none, so answering the
state first would send a caller round a loop that cannot terminate. The
discriminator is KeySpec rather than KeyUsage, so one comparison covers every
family AWS names and the two it implies.

GetKeyRotationStatus answered two of its five published members, and one of the two
answers was wrong. NextRotationDate is now reported as the enable date plus the
period — KMSKey gained the enable date for this and nothing else — as Unix seconds,
matching DescribeKey's DeletionDate; AWS's own page types the member number and
renders it ISO-8601 in its sample, so keeping one convention is substrate's
reading. A key pending deletion now reports false, per the sentence AWS publishes
for that state, and the status is derived at read time rather than written by
ScheduleKeyDeletion, because the second half of the same sentence requires
CancelKeyDeletion to have something to restore. The period and the date follow the
reported status, so a key pending deletion reports neither.

GetKeyRotationStatus is deliberately not refused for a key that cannot rotate, and
a test pins that: the sentence forbids enabling rotation, not asking about it.
@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 da37824 into main Sep 17, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the feat/kms-rotation-key-type-and-next-rotation-date branch September 17, 2026 04: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

1 participant