From 81f484fc18640097a37b554be657feec05c1bc30 Mon Sep 17 00:00:00 2001 From: Fernando Farias Date: Tue, 17 Nov 2026 01:23:48 -0300 Subject: [PATCH] fix(kms): accept application/x-amz-json-1.0 requests (#8658) * fix(kms): accept application/x-amz-json-1.0 requests * test(kms): declare JsonPath explicitly instead of var * test(kms): reuse awsAction with an explicit content type --- .../aws/core/common/AwsJsonController.java | 7 +- .../AwsJsonControllerSerializationTest.java | 2 +- .../kms/KmsJson10IntegrationTest.java | 102 ++++++++++++++++++ .../aws/testing/RestAssuredJsonUtils.java | 6 +- 4 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/kms/KmsJson10IntegrationTest.java diff --git a/src/main/java/io/floci/aws/core/common/AwsJsonController.java b/src/main/java/io/floci/aws/core/common/AwsJsonController.java index 6c3000656a..aed3d734e8 100644 --- a/src/main/java/io/floci/aws/core/common/AwsJsonController.java +++ b/src/main/java/io/floci/aws/core/common/AwsJsonController.java @@ -12,6 +12,7 @@ import io.floci.aws.services.dynamodb.DynamoDbJsonHandler; import io.floci.aws.services.dynamodb.DynamoDbRequestShapes; import io.floci.aws.services.dynamodb.DynamoDbResponses; import io.floci.aws.services.dynamodb.DynamoDbStreamsJsonHandler; +import io.floci.aws.services.kms.KmsJsonHandler; import io.floci.aws.services.marketplace.MarketplaceJsonHandler; import io.floci.aws.services.networkfirewall.NetworkFirewallJsonHandler; import io.floci.aws.services.paymentcryptography.PaymentCryptographyJsonHandler; @@ -68,6 +69,7 @@ public class AwsJsonController { private final VerifiedPermissionsJsonHandler verifiedPermissionsJsonHandler; private final BcmPricingCalculatorJsonHandler bcmPricingCalculatorJsonHandler; private final TimestreamInfluxDbJsonHandler timestreamInfluxDbJsonHandler; + private final KmsJsonHandler kmsJsonHandler; @Inject public AwsJsonController(ObjectMapper objectMapper, ResolvedServiceCatalog catalog, @@ -84,7 +86,8 @@ public class AwsJsonController { MarketplaceJsonHandler marketplaceJsonHandler, VerifiedPermissionsJsonHandler verifiedPermissionsJsonHandler, BcmPricingCalculatorJsonHandler bcmPricingCalculatorJsonHandler, - TimestreamInfluxDbJsonHandler timestreamInfluxDbJsonHandler) { + TimestreamInfluxDbJsonHandler timestreamInfluxDbJsonHandler, + KmsJsonHandler kmsJsonHandler) { this.objectMapper = objectMapper; this.strictBodyReader = objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); this.catalog = catalog; @@ -103,6 +106,7 @@ public class AwsJsonController { this.verifiedPermissionsJsonHandler = verifiedPermissionsJsonHandler; this.bcmPricingCalculatorJsonHandler = bcmPricingCalculatorJsonHandler; this.timestreamInfluxDbJsonHandler = timestreamInfluxDbJsonHandler; + this.kmsJsonHandler = kmsJsonHandler; } /** @@ -199,6 +203,7 @@ public class AwsJsonController { case "verifiedpermissions" -> verifiedPermissionsJsonHandler.handle(action, request, region); case "bcm-pricing-calculator" -> bcmPricingCalculatorJsonHandler.handle(action, request, region); case "timestream-influxdb" -> timestreamInfluxDbJsonHandler.handle(action, request, region); + case "kms" -> kmsJsonHandler.handle(action, request, region); default -> null; }; // catalog.matchTarget is protocol-agnostic: a JSON 1.1 target diff --git a/src/test/java/io/floci/aws/core/common/AwsJsonControllerSerializationTest.java b/src/test/java/io/floci/aws/core/common/AwsJsonControllerSerializationTest.java index 451adaf995..da5a56d5e5 100644 --- a/src/test/java/io/floci/aws/core/common/AwsJsonControllerSerializationTest.java +++ b/src/test/java/io/floci/aws/core/common/AwsJsonControllerSerializationTest.java @@ -44,7 +44,7 @@ class AwsJsonControllerSerializationTest { AwsJsonController controller = new AwsJsonController(mapper, catalog, regionResolver, null, null, sqs, null, null, null, - null, null, null, null, null, null, null, null); + null, null, null, null, null, null, null, null, null); Response result = controller.handleJsonRequest("AmazonSQS.Test", headers, "{}"); assertSame(original, result); diff --git a/src/test/java/io/floci/aws/services/kms/KmsJson10IntegrationTest.java b/src/test/java/io/floci/aws/services/kms/KmsJson10IntegrationTest.java new file mode 100644 index 0000000000..248ed17400 --- /dev/null +++ b/src/test/java/io/floci/aws/services/kms/KmsJson10IntegrationTest.java @@ -0,0 +1,102 @@ +package io.floci.aws.services.kms; + +import io.floci.aws.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.json.JsonPath; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +import static io.floci.aws.core.common.AwsJson11Controller.CONTENT_TYPE_AWS_JSON_1_1; +import static io.floci.aws.core.common.AwsJsonController.CONTENT_TYPE_AWS_JSON_1_0; +import static io.floci.aws.testing.RestAssuredJsonUtils.awsAction; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; + +@QuarkusTest +class KmsJson10IntegrationTest { + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void generateDataKeyAndDecryptOverJson10() { + String keyId = call(CONTENT_TYPE_AWS_JSON_1_0, "CreateKey", "{}") + .statusCode(200) + .body("KeyMetadata.KeyId", notNullValue()) + .extract().path("KeyMetadata.KeyId"); + + String context = "{\"record_id\":\"42\"}"; + JsonPath dataKey = call(CONTENT_TYPE_AWS_JSON_1_0, "GenerateDataKey", """ + {"KeyId":"%s","KeySpec":"AES_256","EncryptionContext":%s} + """.formatted(keyId, context)) + .statusCode(200) + .extract().jsonPath(); + String plaintext = dataKey.getString("Plaintext"); + String blob = dataKey.getString("CiphertextBlob"); + + call(CONTENT_TYPE_AWS_JSON_1_0, "Decrypt", """ + {"CiphertextBlob":"%s","EncryptionContext":%s} + """.formatted(blob, context)) + .statusCode(200) + .body("Plaintext", equalTo(plaintext)) + .body("KeyId", notNullValue()); + + call(CONTENT_TYPE_AWS_JSON_1_1, "Decrypt", """ + {"CiphertextBlob":"%s","EncryptionContext":%s} + """.formatted(blob, context)) + .statusCode(200) + .body("Plaintext", equalTo(plaintext)); + } + + @Test + void encryptOverJson11DecryptsOverJson10() { + String keyId = call(CONTENT_TYPE_AWS_JSON_1_1, "CreateKey", "{}") + .statusCode(200) + .extract().path("KeyMetadata.KeyId"); + String message = Base64.getEncoder().encodeToString("hello".getBytes(StandardCharsets.UTF_8)); + + String blob = call(CONTENT_TYPE_AWS_JSON_1_1, "Encrypt", """ + {"KeyId":"%s","Plaintext":"%s"} + """.formatted(keyId, message)) + .statusCode(200) + .extract().path("CiphertextBlob"); + + call(CONTENT_TYPE_AWS_JSON_1_0, "Decrypt", "{\"CiphertextBlob\":\"" + blob + "\"}") + .statusCode(200) + .body("Plaintext", equalTo(message)); + } + + @Test + void json10KeepsKmsErrorShapes() { + String keyId = call(CONTENT_TYPE_AWS_JSON_1_0, "CreateKey", "{}") + .statusCode(200) + .extract().path("KeyMetadata.KeyId"); + String blob = call(CONTENT_TYPE_AWS_JSON_1_0, "GenerateDataKey", """ + {"KeyId":"%s","KeySpec":"AES_256","EncryptionContext":{"record_id":"1"}} + """.formatted(keyId)) + .statusCode(200) + .extract().path("CiphertextBlob"); + + call(CONTENT_TYPE_AWS_JSON_1_0, "Decrypt", """ + {"CiphertextBlob":"%s","EncryptionContext":{"record_id":"2"}} + """.formatted(blob)) + .statusCode(400) + .body("__type", equalTo("InvalidCiphertextException")); + + call(CONTENT_TYPE_AWS_JSON_1_0, "GenerateDataKey", """ + {"KeyId":"alias/does-not-exist","KeySpec":"AES_256"} + """) + .statusCode(400) + .body("__type", equalTo("NotFoundException")); + } + + private static ValidatableResponse call(String contentType, String action, String body) { + return awsAction("TrentService", action, body, contentType).then(); + } +} diff --git a/src/test/java/io/floci/aws/testing/RestAssuredJsonUtils.java b/src/test/java/io/floci/aws/testing/RestAssuredJsonUtils.java index c28b4d4dfe..ff98bbbc6c 100644 --- a/src/test/java/io/floci/aws/testing/RestAssuredJsonUtils.java +++ b/src/test/java/io/floci/aws/testing/RestAssuredJsonUtils.java @@ -49,9 +49,13 @@ public class RestAssuredJsonUtils { } public static Response awsAction(String target, String action, String body) { + return awsAction(target, action, body, CONTENT_TYPE_AWS_JSON_1_1); + } + + public static Response awsAction(String target, String action, String body, String contentType) { return given() .header("X-Amz-Target", target + "." + action) - .contentType(CONTENT_TYPE_AWS_JSON_1_1) + .contentType(contentType) .body(body) .when() .post("/");