mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(kms): support HMAC_* key specs in CreateKey (#544)
CreateKey with `--key-spec HMAC_{224,256,384,512} --key-usage
GENERATE_VERIFY_MAC` now succeeds instead of throwing
InvalidCustomerMasterKeySpecException. DescribeKey returns the matching
MacAlgorithms array (HMAC_SHA_*), and GetPublicKey correctly rejects
HMAC keys with UnsupportedOperationException.
Validation also rejects the two incompatible pairings:
- HMAC_* without GENERATE_VERIFY_MAC usage
- GENERATE_VERIFY_MAC usage with any non-HMAC spec
HMAC material is generated with SecureRandom and base64-stored on the
existing privateKeyEncoded field (same pattern as RSA/ECC private key
bytes). Generation sits outside the try block that wraps asymmetric
keypair creation, so ValidationExceptions aren't rewrapped as 500s.
Closes #497.
Out of scope (follow-up PRs):
- GenerateMac / VerifyMac operations
- HMAC key rotation support
- Catch-rewrap bug in generateKeyMaterial for asymmetric keys
This commit is contained in:
4 files changed
+125
-3
No files matched your search
@@ -367,4 +367,50 @@ class KmsServiceTest {
|
||||
assertThrows(AwsException.class, () ->
|
||||
kmsService.enableKeyRotation(key.getKeyId(), REGION));
|
||||
}
|
||||
|
||||
// ── Issue #497 — HMAC key specs ─────────────────────────────────────────
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"HMAC_224", "HMAC_256", "HMAC_384", "HMAC_512"})
|
||||
void createHmacKey_allSpecs(String spec) {
|
||||
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", spec, null, Map.of(), REGION);
|
||||
|
||||
assertEquals(spec, key.getCustomerMasterKeySpec());
|
||||
assertEquals("GENERATE_VERIFY_MAC", key.getKeyUsage());
|
||||
assertNotNull(key.getPrivateKeyEncoded());
|
||||
|
||||
int expectedBytes = switch (spec) {
|
||||
case "HMAC_224" -> 28;
|
||||
case "HMAC_256" -> 32;
|
||||
case "HMAC_384" -> 48;
|
||||
case "HMAC_512" -> 64;
|
||||
default -> -1;
|
||||
};
|
||||
assertEquals(expectedBytes, Base64.getDecoder().decode(key.getPrivateKeyEncoded()).length);
|
||||
|
||||
KmsKey found = kmsService.describeKey(key.getKeyId(), REGION);
|
||||
assertEquals(spec, found.getCustomerMasterKeySpec());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createHmacKey_requiresGenerateVerifyMacUsage() {
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.createKey("hmac key", "ENCRYPT_DECRYPT", "HMAC_256", null, Map.of(), REGION));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSymmetricKey_rejectsGenerateVerifyMacUsage() {
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.createKey("bad", "GENERATE_VERIFY_MAC", "SYMMETRIC_DEFAULT", null, Map.of(), REGION));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPublicKeyForHmacKey_throwsUnsupportedOperation() {
|
||||
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", "HMAC_256", null, Map.of(), REGION);
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.getPublicKey(key.getKeyId(), REGION));
|
||||
assertEquals("UnsupportedOperationException", ex.getErrorCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user