diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java index b943824fc0..06f4ec9814 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java @@ -266,7 +266,7 @@ public class AwsJson11Controller { String body) { if (target == null) { - return null; + return JsonErrorResponseUtils.createMissingTargetErrorResponse(); } ServiceCatalog.TargetMatch targetMatch = catalog.matchTarget(target).orElse(null); diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java index b8d5b4b7cc..b7d97a00a7 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java @@ -385,16 +385,15 @@ public class AwsJsonCborController { byte[] body) { if (target == null) { - return null; + return cborUnknownOperationError("Missing X-Amz-Target header.", httpHeaders); } - // Upstream CBOR behavior is to return null for targets this controller - // does not dispatch (JAX-RS then serves 204). The JSON 1.0/1.1 - // controllers return UnknownOperationException instead; CBOR stays on - // null here to preserve pre-refactor semantics. + // Symmetric with the JSON 1.0/1.1 controllers: a target this controller cannot + // resolve is an unknown operation, not an empty 204. The 204 fallback now applies + // only to a resolved service whose action this handler does not dispatch. ServiceCatalog.TargetMatch targetMatch = catalog.matchTarget(target).orElse(null); if (targetMatch == null) { - return null; + return cborUnknownOperationError("Unknown operation: " + target, httpHeaders); } String serviceKey = targetMatch.descriptor().externalKey(); @@ -491,6 +490,12 @@ public class AwsJsonCborController { }; } + private Response cborUnknownOperationError(String message, HttpHeaders httpHeaders) { + return cborErrorResponse( + new AwsException("UnknownOperationException", message, 404), + "smithy-protocol", responseContentType(httpHeaders)); + } + private Response cborErrorResponse(AwsException e, String protocolHeader, String mediaType) { try { byte[] errBytes = CBOR_MAPPER.writeValueAsBytes( diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java index 9b39ae53cd..005c60c101 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java @@ -101,7 +101,7 @@ public class AwsJsonController { String body) { if (target == null) { - return null; + return JsonErrorResponseUtils.createMissingTargetErrorResponse(); } ServiceCatalog.TargetMatch targetMatch = catalog.matchTarget(target).orElse(null); diff --git a/src/main/java/io/github/hectorvent/floci/core/common/JsonErrorResponseUtils.java b/src/main/java/io/github/hectorvent/floci/core/common/JsonErrorResponseUtils.java index 83ac997162..1274024575 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/JsonErrorResponseUtils.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/JsonErrorResponseUtils.java @@ -57,6 +57,18 @@ public class JsonErrorResponseUtils { "Unknown operation: " + target, null); } + /** + * A JSON request with no X-Amz-Target names no operation, so there is nothing to route on. + * It fails as an unknown operation but with a message about the missing header rather than + * concatenating a literal {@code null} into {@link #createUnknownOperationErrorResponse}. + */ + public static Response createMissingTargetErrorResponse() { + return createErrorResponse(404, + "UnknownOperationException", + "UnknownOperationException", + "Missing X-Amz-Target header.", null); + } + /** * A request body that is not valid JSON is a client (deserialization) error, not a server * fault. AWS's JSON protocols reject it with 400 SerializationException; without this the diff --git a/src/test/java/io/github/hectorvent/floci/core/common/UnknownOperationMissingTargetIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/UnknownOperationMissingTargetIntegrationTest.java new file mode 100644 index 0000000000..746f8bd723 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/UnknownOperationMissingTargetIntegrationTest.java @@ -0,0 +1,144 @@ +package io.github.hectorvent.floci.core.common; + +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 static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; + +/** + * A JSON POST without an X-Amz-Target header must fail with + * UnknownOperationException (404) rather than be served as an empty success. + * The status and error name follow the AWS Common Errors entry for an + * unrecognized operation; Smithy requires the header but does not specify a + * server response when it is absent, so this pins Floci's chosen behavior. + * Both the JSON 1.0 and JSON 1.1 controllers share the null-target branch. + */ +@QuarkusTest +class UnknownOperationMissingTargetIntegrationTest { + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void json10RequestWithoutTargetReturnsUnknownOperation() { + given() + .contentType("application/x-amz-json-1.0") + .body("{}") + .when() + .post("/") + .then() + .statusCode(404) + .header("x-amzn-query-error", equalTo("UnknownOperationException;Sender")) + .body("__type", equalTo("UnknownOperationException")) + .body("message", equalTo("Missing X-Amz-Target header.")); + } + + @Test + void json10RequestWithEmptyBodyAndNoTargetReturnsUnknownOperation() { + given() + .contentType("application/x-amz-json-1.0") + .when() + .post("/") + .then() + .statusCode(404) + .body("__type", equalTo("UnknownOperationException")) + .body("message", equalTo("Missing X-Amz-Target header.")); + } + + @Test + void json11RequestWithoutTargetReturnsUnknownOperation() { + given() + .contentType("application/x-amz-json-1.1") + .body("{}") + .when() + .post("/") + .then() + .statusCode(404) + .header("x-amzn-query-error", equalTo("UnknownOperationException;Sender")) + .body("__type", equalTo("UnknownOperationException")) + .body("message", equalTo("Missing X-Amz-Target header.")); + } + + @Test + void json11RequestWithEmptyBodyAndNoTargetReturnsUnknownOperation() { + given() + .contentType("application/x-amz-json-1.1") + .when() + .post("/") + .then() + .statusCode(404) + .body("__type", equalTo("UnknownOperationException")) + .body("message", equalTo("Missing X-Amz-Target header.")); + } + + @Test + void unknownTargetReturnsUnknownOperation() { + given() + .contentType("application/x-amz-json-1.0") + .header("X-Amz-Target", "Nope.Nope") + .body("{}") + .when() + .post("/") + .then() + .statusCode(404) + .body("__type", equalTo("UnknownOperationException")) + .body("message", equalTo("Unknown operation: Nope.Nope")); + } + + @Test + void json10ValidTargetStillReachesTheHandler() { + given() + .contentType("application/x-amz-json-1.0") + .header("X-Amz-Target", "DynamoDB_20120810.ListTables") + .body("{}") + .when() + .post("/") + .then() + .statusCode(200) + .body("__type", not(equalTo("UnknownOperationException"))); + } + + @Test + void json11ValidTargetStillReachesTheHandler() { + given() + .contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "AmazonSSM.DescribeParameters") + .body("{}") + .when() + .post("/") + .then() + .statusCode(200) + .body("__type", not(equalTo("UnknownOperationException"))); + } + + @Test + void cborRequestWithoutTargetReturnsUnknownOperation() { + given() + .contentType("application/x-amz-cbor-1.1") + .when() + .post("/") + .then() + .statusCode(404) + .header("x-amzn-query-error", equalTo("UnknownOperationException;Sender")) + .header("smithy-protocol", equalTo("rpc-v2-cbor")); + } + + @Test + void cborRequestWithUnknownTargetReturnsUnknownOperation() { + given() + .contentType("application/x-amz-cbor-1.1") + .header("X-Amz-Target", "Nope.Nope") + .when() + .post("/") + .then() + .statusCode(404) + .header("x-amzn-query-error", equalTo("UnknownOperationException;Sender")) + .header("smithy-protocol", equalTo("rpc-v2-cbor")); + } +}