mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(kms): implement GenerateMac and VerifyMac (#927)
* feat(kms): implement GenerateMac and VerifyMac * fix(kms): remove unused MAC message type handling * test(kms): add MAC JSON handler integration coverage * fix(kms): avoid duplicate MAC key resolution
This commit is contained in:
1 parent
24bbd81988
commit
6819bce493
4 files changed
+343
No files matched your search
@@ -0,0 +1,111 @@
|
||||
package io.github.hectorvent.floci.services.kms;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
class KmsIntegrationTest {
|
||||
|
||||
private static final String KMS_CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateMacAndVerifyMacRoundTripThroughJsonHandler() {
|
||||
String keyId = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"Description": "integration-hmac",
|
||||
"KeyUsage": "GENERATE_VERIFY_MAC",
|
||||
"CustomerMasterKeySpec": "HMAC_256"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("KeyMetadata.KeyId", notNullValue())
|
||||
.body("KeyMetadata.Arn", startsWith("arn:aws:kms:"))
|
||||
.body("KeyMetadata.KeyUsage", equalTo("GENERATE_VERIFY_MAC"))
|
||||
.body("KeyMetadata.CustomerMasterKeySpec", equalTo("HMAC_256"))
|
||||
.extract().jsonPath().getString("KeyMetadata.KeyId");
|
||||
|
||||
String message = Base64.getEncoder().encodeToString(
|
||||
"kms integration mac message".getBytes(StandardCharsets.UTF_8));
|
||||
String mac = given()
|
||||
.header("X-Amz-Target", "TrentService.GenerateMac")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"KeyId": "%s",
|
||||
"Message": "%s",
|
||||
"MacAlgorithm": "HMAC_SHA_256"
|
||||
}
|
||||
""".formatted(keyId, message))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("KeyId", startsWith("arn:aws:kms:"))
|
||||
.body("Mac", notNullValue())
|
||||
.body("MacAlgorithm", equalTo("HMAC_SHA_256"))
|
||||
.extract().jsonPath().getString("Mac");
|
||||
|
||||
assertEquals(32, Base64.getDecoder().decode(mac).length);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.VerifyMac")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"KeyId": "%s",
|
||||
"Message": "%s",
|
||||
"Mac": "%s",
|
||||
"MacAlgorithm": "HMAC_SHA_256"
|
||||
}
|
||||
""".formatted(keyId, message, mac))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("KeyId", startsWith("arn:aws:kms:"))
|
||||
.body("MacAlgorithm", equalTo("HMAC_SHA_256"))
|
||||
.body("MacValid", equalTo(true));
|
||||
|
||||
String differentMessage = Base64.getEncoder().encodeToString(
|
||||
"different message".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.VerifyMac")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"KeyId": "%s",
|
||||
"Message": "%s",
|
||||
"Mac": "%s",
|
||||
"MacAlgorithm": "HMAC_SHA_256"
|
||||
}
|
||||
""".formatted(keyId, differentMessage, mac))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("KMSInvalidMacException"));
|
||||
}
|
||||
}
|
||||
@@ -657,6 +657,17 @@ class KmsServiceTest {
|
||||
assertEquals(spec, found.getCustomerMasterKeySpec());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"HMAC_224", "HMAC_256", "HMAC_384", "HMAC_512"})
|
||||
void generateMacSupportsAllAdvertisedHmacSpecs(String spec) {
|
||||
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", spec, null, Map.of(), REGION);
|
||||
byte[] message = "floci-mac-probe".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
byte[] mac = kmsService.generateMac(key.getKeyId(), message, KmsService.macAlgorithmFor(spec), REGION);
|
||||
|
||||
assertEquals(expectedMacByteLength(spec), mac.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createHmacKey_requiresGenerateVerifyMacUsage() {
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
@@ -678,4 +689,95 @@ class KmsServiceTest {
|
||||
kmsService.getPublicKey(key.getKeyId(), REGION));
|
||||
assertEquals("UnsupportedOperationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateMacWithHmacKeyReturnsDeterministicMac() {
|
||||
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", "HMAC_256", null, Map.of(), REGION);
|
||||
byte[] message = "floci-mac-probe".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
byte[] first = kmsService.generateMac(key.getKeyId(), message, "HMAC_SHA_256", REGION);
|
||||
byte[] second = kmsService.generateMac(key.getKeyId(), message, "HMAC_SHA_256", REGION);
|
||||
|
||||
assertEquals(32, first.length);
|
||||
assertArrayEquals(first, second);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateMacRejectsAlgorithmThatDoesNotMatchKeySpec() {
|
||||
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", "HMAC_256", null, Map.of(), REGION);
|
||||
byte[] message = "floci-mac-probe".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.generateMac(key.getKeyId(), message, "HMAC_SHA_512", REGION));
|
||||
|
||||
assertEquals("InvalidKeyUsageException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateMacRejectsMessageOutsideAwsLengthBounds() {
|
||||
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", "HMAC_256", null, Map.of(), REGION);
|
||||
|
||||
AwsException empty = assertThrows(AwsException.class, () ->
|
||||
kmsService.generateMac(key.getKeyId(), new byte[0], "HMAC_SHA_256", REGION));
|
||||
AwsException oversized = assertThrows(AwsException.class, () ->
|
||||
kmsService.generateMac(key.getKeyId(), new byte[4097], "HMAC_SHA_256", REGION));
|
||||
|
||||
assertEquals("ValidationException", empty.getErrorCode());
|
||||
assertEquals("ValidationException", oversized.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void verifyMacAcceptsMatchingMacAndRejectsMismatch() {
|
||||
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", "HMAC_256", null, Map.of(), REGION);
|
||||
byte[] message = "floci-mac-probe".getBytes(StandardCharsets.UTF_8);
|
||||
byte[] mac = kmsService.generateMac(key.getKeyId(), message, "HMAC_SHA_256", REGION);
|
||||
|
||||
assertDoesNotThrow(() -> kmsService.verifyMac(key.getKeyId(), message, mac, "HMAC_SHA_256", REGION));
|
||||
|
||||
byte[] tamperedMac = mac.clone();
|
||||
tamperedMac[0] ^= 1;
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.verifyMac(key.getKeyId(), message, tamperedMac, "HMAC_SHA_256", REGION));
|
||||
|
||||
assertEquals("KMSInvalidMacException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void verifyMacRejectsMacOutsideAwsLengthBounds() {
|
||||
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", "HMAC_256", null, Map.of(), REGION);
|
||||
byte[] message = "floci-mac-probe".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
AwsException empty = assertThrows(AwsException.class, () ->
|
||||
kmsService.verifyMac(key.getKeyId(), message, new byte[0], "HMAC_SHA_256", REGION));
|
||||
AwsException oversized = assertThrows(AwsException.class, () ->
|
||||
kmsService.verifyMac(key.getKeyId(), message, new byte[6145], "HMAC_SHA_256", REGION));
|
||||
|
||||
assertEquals("ValidationException", empty.getErrorCode());
|
||||
assertEquals("ValidationException", oversized.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void verifyMacRejectsMessageOutsideAwsLengthBounds() {
|
||||
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", "HMAC_256", null, Map.of(), REGION);
|
||||
byte[] validMessage = "floci-mac-probe".getBytes(StandardCharsets.UTF_8);
|
||||
byte[] mac = kmsService.generateMac(key.getKeyId(), validMessage, "HMAC_SHA_256", REGION);
|
||||
|
||||
AwsException empty = assertThrows(AwsException.class, () ->
|
||||
kmsService.verifyMac(key.getKeyId(), new byte[0], mac, "HMAC_SHA_256", REGION));
|
||||
AwsException oversized = assertThrows(AwsException.class, () ->
|
||||
kmsService.verifyMac(key.getKeyId(), new byte[4097], mac, "HMAC_SHA_256", REGION));
|
||||
|
||||
assertEquals("ValidationException", empty.getErrorCode());
|
||||
assertEquals("ValidationException", oversized.getErrorCode());
|
||||
}
|
||||
|
||||
private static int expectedMacByteLength(String spec) {
|
||||
return switch (spec) {
|
||||
case "HMAC_224" -> 28;
|
||||
case "HMAC_256" -> 32;
|
||||
case "HMAC_384" -> 48;
|
||||
case "HMAC_512" -> 64;
|
||||
default -> -1;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user