mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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
This commit is contained in:
1 parent
8d9ab570cd
commit
81f484fc18
4 files changed
+114
-3
No files matched your search
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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("/");
|
||||
|
||||
Reference in new issue
Block a user