Repository navigation
feat(kms): validate KeySpec and KeyUsage, and pair them (#977) - #986
Merged
Merged
Conversation
CreateKey accepted any string for either member and paired them however a caller asked. Both members are immutable, so an unvalidated value is not something a later call can correct — it is a key that will never behave as its own metadata says. Three refusals: a value outside its published enum, and an absent KeyUsage for any spec but SYMMETRIC_DEFAULT, answer ValidationError/400; a well-formed pair the spec does not admit answers UnsupportedOperationException/400, which CreateKey publishes. The two codes say different things — that is not a key spec, versus that is a key spec and not with that usage — and the first is reported before the second. The pairing table is derived rather than written: a spec admits a usage exactly when its algorithm list for that usage is non-empty, read from the four maps #974 built for DescribeKey. The metadata builder and the validator now read them through one accessor, so a key cannot be accepted for a usage whose algorithm list it would then not carry. The test transcribes AWS's seven pairing bullets by hand across all 17 specs x 4 usages. Also implements InvalidKeyUsageException's first gloss bullet, where #969 implemented the second: the five cryptographic operations refuse a key whose usage is not ENCRYPT_DECRYPT, checked before the key state and before the encryption algorithm.
Codecov Report❌ Patch coverage is
📢 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 #977.
CreateKeyaccepted any string forKeySpecandKeyUsageand paired them however a caller asked, so substrate could hold an ECC key withKeyUsageENCRYPT_DECRYPT, or a key whose spec wasrsa2048. Both members are immutable — "you can't change theKeySpecafter the KMS key is created", and the same sentence forKeyUsage— so an unvalidated value is not a member a later call can correct. It is a key that will never behave as its own metadata says, and every operation reading either member reports a value AWS would never have stored.Three defects, three answers
KeySpecorKeyUsageoutside its published enum (17 specs, 4 usages)ValidationError/400, listing the enumAPI_CreateKeypublishes 13 errors andValidationExceptionis not among them, so a malformed enum member comes fromCommonErrors.html, as it does for #969's encryption algorithmKeyUsageabsent for any spec butSYMMETRIC_DEFAULTValidationError/400, naming the spec and what it admitsUnsupportedOperationException/400, naming both and what the spec admitsCreateKey, glossed "a specified parameter is not supported or a specified resource is not valid for this operation"The two codes are load-bearing together rather than one code twice: the first says that is not a key spec, the second says that is a key spec, and not with that usage. A caller that misspelled a value and one that chose an impossible combination have different fixes, and the first is reported before the second is considered.
The default fires for exactly one spec, which AWS's HMAC guidance settles: "you must set the key usage even though
GENERATE_VERIFY_MACis the only valid key usage value for HMAC KMS keys." SoKeyUsageis not defaulted to the single admissible value wherever there is one — only forSYMMETRIC_DEFAULT.The pairing table is derived, not written
A spec admits a usage exactly when its algorithm list for that usage is non-empty, read from the four maps #974 already built for
DescribeKey's algorithm members.kmsKeyMetadata's four-arm switch is replaced by a lookup in the same table the validator uses, so the renderer and the validator cannot disagree — a key cannot be accepted for a usage whose algorithm list it would then not carry.Writing AWS's seven pairing bullets out a second time would have created the drift class this repo has recorded before: two tables that must agree, with nothing making them. Instead the test transcribes those bullets by hand — all 17 specs × 4 usages, 25 admissible pairs and 43 inadmissible — so the derivation and the page disagree unless both match.
InvalidKeyUsageException's first bullet#969 implemented that code's second gloss bullet (algorithm incompatible with the key spec). This implements the first — "the
KeyUsagevalue of the KMS key is incompatible with the API operation" — per AWS's requirement that "for encrypting, decrypting, re-encrypting, and generating data keys, theKeyUsagemust beENCRYPT_DECRYPT".Encrypt,Decrypt,GenerateDataKey,GenerateDataKeyWithoutPlaintextand both ends ofReEncryptrefuse a key whose usage is anything else.Ordered before the key-state check, because a key usage is permanent where a state is not — telling a caller to enable a
SIGN_VERIFYkey sends it round a loop that cannot terminate — and before the encryption-algorithm check, so one condition yields one message: aSIGN_VERIFYRSA key addressed byEncrypthears about its usage, not aboutSYMMETRIC_DEFAULTbeing inadmissible for RSA.Consequence recorded rather than hidden: #974's empty-algorithm-list branch is now unreachable through substrate's own API. It is kept as the guard on a fifth algorithm member arriving without its pairing being thought through, and a test asserts that wording does not appear.
CloudFormation
AWS::KMS::KeydocumentsKeySpec's default asSYMMETRIC_DEFAULTandKeyUsage's asENCRYPT_DECRYPT, anddeployKMSKeysends both unconditionally — so a template naming an asymmetric or HMACKeySpecwithout aKeyUsageis now refused where it previously created a key AWS would not have. The defaults stay: they are the resource type's own, and that template is invalid at AWS too, where the property "is required for asymmetric KMS keys and HMAC KMS keys". A test pins all three cases.Test rework
Every existing KMS test that created a non-symmetric key did so through the gap this closes, and 29 subtests failed once the validation was wired — each one a
CreateKeyAWS itself would refuse, so a rework list rather than a defect.createKMSKeySpecmoved into the new file and now names an admissible usage; the metadata file's rows gained akeyUsagecolumn; andTestKMSKeyMetadata_AnImpossiblePairReportsNoAlgorithmListAtAllwas re-pointed at the refusal rather than deleted, since its five pairs are exactly the fiveCreateKeynow rejects.Verification
The nine-command gate passes. Coverage holds the baseline at 84.4% emulator / 83.9% total.