diff --git a/docs/services/swf.md b/docs/services/swf.md index b7f0ac851a..48c9110e46 100644 --- a/docs/services/swf.md +++ b/docs/services/swf.md @@ -3,6 +3,15 @@ **Protocol:** JSON 1.0 (`X-Amz-Target: SimpleWorkflowService.*`) **Endpoint:** `POST http://localhost:4566/` +The legacy SWF JSON format is also accepted: `Content-Type: application/json; charset=UTF-8` +with `X-Amz-Target: com.amazonaws.swf.service.model.SimpleWorkflowService.`. +It uses the same service state, validation and namespaced faults as JSON 1.0. This +plain-JSON compatibility path accepts both that prefix and `SimpleWorkflowService.`; it does +not enable plain-JSON target dispatch for other services. Request headers are preserved. +The contract is documented in +[AWS Making HTTP Requests](https://docs.aws.amazon.com/amazonswf/latest/developerguide/UsingJSON-swf.html) +and emitted by [Boto 2.49.0](https://github.com/boto/boto/blob/2.49.0/boto/swf/layer1.py). + Domains, workflow and activity type registration, and the workflow execution state machine: decision tasks, activity tasks, timers, signals, child workflows, and timeouts. 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 0396317742..74db04543b 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 @@ -393,7 +393,7 @@ public class AwsJsonCborController { } // 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 + // dispatch 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) { @@ -420,7 +420,7 @@ public class AwsJsonCborController { case "kinesis" -> kinesisJsonHandler.handle(action, request, region); case "states" -> sfnJsonHandler.handle(action, request, region); case "monitoring" -> cloudWatchMetricsJsonHandler.handle(action, request, region); - default -> null; + default -> throw new AwsException("UnknownOperationException", "Unknown operation: " + target, 404); }; if (delegated == null) { return null; 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 1ada2fa700..8398ebf400 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 @@ -93,6 +93,28 @@ public class AwsJsonController { this.timestreamInfluxDbJsonHandler = timestreamInfluxDbJsonHandler; } + /** + * Accept plain JSON only for catalog descriptors declaring that format. + * Keep the wire headers intact and delegate to the same JSON handler. + */ + @POST + @Consumes("application/json") + @Produces("application/json") + public Response handlePlainJsonRequest( + @HeaderParam("X-Amz-Target") String target, + @Context HttpHeaders httpHeaders, + String body) { + if (target == null) { + return JsonErrorResponseUtils.createMissingTargetErrorResponse(); + } + if (catalog.matchPlainJsonTarget(target).isEmpty()) { + return JsonErrorResponseUtils.createUnknownOperationErrorResponse(target); + } + return Response.fromResponse(handleJsonRequest(target, httpHeaders, body)) + .type("application/json") + .build(); + } + @POST @Consumes(CONTENT_TYPE_AWS_JSON_1_0) @Produces(CONTENT_TYPE_AWS_JSON_1_0) diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ProtocolClaimer.java b/src/main/java/io/github/hectorvent/floci/core/common/ProtocolClaimer.java index 7d08e863de..e371791187 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ProtocolClaimer.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ProtocolClaimer.java @@ -110,6 +110,9 @@ public class ProtocolClaimer { if (CONTENT_TYPE_JSON_1_1.equals(mediaType)) { return Optional.of(targetClaim(WireProtocol.AWS_JSON_1_1, target)); } + if ("application/json".equals(mediaType) && catalog.matchPlainJsonTarget(target).isPresent()) { + return Optional.of(targetClaim(WireProtocol.AWS_JSON_1_0, target)); + } if (isCbor(mediaType)) { return Optional.of(targetClaim(WireProtocol.AWS_CBOR_TARGET, target)); } diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java index e047555e03..abe1a2de7f 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java @@ -82,6 +82,7 @@ import io.github.hectorvent.floci.services.ses.SesTemplateController; import io.github.hectorvent.floci.services.ses.SesTenantController; import io.github.hectorvent.floci.services.ssooidc.SsoOidcController; import io.github.hectorvent.floci.services.ssoportal.SsoPortalController; +import io.github.hectorvent.floci.services.swf.model.SwfConstants; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -294,8 +295,9 @@ public class ResolvedServiceCatalog { Set.of("AWSStepFunctions."), Set.of("states"), Set.of("SFN"), Set.of()), descriptor("swf", "swf", config.services().swf().enabled(), true, "swf", config.storage().mode(), 5000L, null, ServiceProtocol.JSON, - protocols(ServiceProtocol.JSON), - Set.of("SimpleWorkflowService."), Set.of("swf"), Set.of(), Set.of()), + protocols(ServiceProtocol.JSON, ServiceProtocol.PLAIN_JSON), + Set.of(SwfConstants.TARGET_PREFIX, SwfConstants.LEGACY_TARGET_PREFIX), + Set.of("swf"), Set.of(), Set.of()), descriptor("cloudformation", "cloudformation", config.services().cloudformation().enabled(), true, null, null, 5000L, null, ServiceProtocol.QUERY, protocols(ServiceProtocol.QUERY), @@ -793,6 +795,10 @@ public class ResolvedServiceCatalog { return catalog.matchTarget(target); } + public Optional matchPlainJsonTarget(String target) { + return catalog.matchPlainJsonTarget(target); + } + public Optional byCredentialScope(String credentialScope) { return catalog.byCredentialScope(credentialScope); } diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ServiceCatalog.java index 92cac8deee..ebefd453e9 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ServiceCatalog.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ServiceCatalog.java @@ -111,6 +111,12 @@ public class ServiceCatalog { return matchTarget(target).map(TargetMatch::descriptor); } + /** Targeted application/json requests are accepted only by descriptors declaring that format. */ + public Optional matchPlainJsonTarget(String target) { + return matchTarget(target) + .filter(match -> match.descriptor().supportsProtocol(ServiceProtocol.PLAIN_JSON)); + } + public List all() { return all; } diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ServiceProtocol.java b/src/main/java/io/github/hectorvent/floci/core/common/ServiceProtocol.java index 38de960e39..4363fd13db 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ServiceProtocol.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ServiceProtocol.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common; public enum ServiceProtocol { QUERY, JSON, + PLAIN_JSON, CBOR, REST_JSON, REST_XML diff --git a/src/main/java/io/github/hectorvent/floci/services/swf/model/SwfConstants.java b/src/main/java/io/github/hectorvent/floci/services/swf/model/SwfConstants.java index cf83911889..b5e126e87f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/swf/model/SwfConstants.java +++ b/src/main/java/io/github/hectorvent/floci/services/swf/model/SwfConstants.java @@ -13,6 +13,9 @@ import java.util.Set; */ public final class SwfConstants { + public static final String TARGET_PREFIX = "SimpleWorkflowService."; + public static final String LEGACY_TARGET_PREFIX = "com.amazonaws.swf.service.model.SimpleWorkflowService."; + public static final String FAULT_NAMESPACE = "com.amazonaws.swf.base.model#"; public static final String VALIDATION_NAMESPACE = "com.amazon.coral.validate#"; @@ -92,4 +95,5 @@ public final class SwfConstants { private SwfConstants() { } + } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/ProtocolClaimerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/ProtocolClaimerIntegrationTest.java index f7d41ee523..649f3aeb9e 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/ProtocolClaimerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/ProtocolClaimerIntegrationTest.java @@ -122,6 +122,30 @@ class ProtocolClaimerIntegrationTest { null, "AmazonSSM.DescribeParameters", null).isEmpty()); } + @Test + void botoSwfJsonClaimsOnlyTheDocumentedRootTarget() { + String target = "com.amazonaws.swf.service.model.SimpleWorkflowService.RegisterActivityType"; + ProtocolClaim legacy = claim("POST", "/", "Application/JSON; charset=UTF-8", + null, target, null).orElseThrow(); + assertEquals(WireProtocol.AWS_JSON_1_0, legacy.protocol()); + assertEquals("swf", legacy.service().externalKey()); + assertEquals("RegisterActivityType", legacy.operation()); + assertEquals(target, legacy.target()); + + assertTrue(claim("POST", "/", "text/plain", null, target, null).isEmpty()); + assertTrue(claim("POST", "/", "application/json", "rpc-v2-json", target, null).isEmpty()); + assertEquals("swf", claim("POST", "/", "application/json", null, + "SimpleWorkflowService.RegisterActivityType", null).orElseThrow().service().externalKey()); + assertTrue(claim("POST", "/", "application/json", null, + "com.amazonaws.swf.service.model.SimpleWorkflowServiceOther.RegisterActivityType", null).isEmpty()); + assertTrue(claim("POST", "/", "application/json", null, + "AmazonSSM.DescribeParameters", null).isEmpty()); + assertEquals(WireProtocol.REST, claim("GET", "/", "application/json", + null, target, null).orElseThrow().protocol()); + assertEquals(WireProtocol.REST, claim("POST", "/other", "application/json", + null, target, null).orElseThrow().protocol()); + } + @Test void unknownTargetStillClaimsProtocolWithoutService() { ProtocolClaim claim = claim("POST", "/", "application/x-amz-json-1.1", diff --git a/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogTest.java b/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogTest.java new file mode 100644 index 0000000000..9eb7cb87b9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogTest.java @@ -0,0 +1,42 @@ +package io.github.hectorvent.floci.core.common; + +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ServiceCatalogTest { + + @Test + void plainJsonFormatIsDeclaredByTheDescriptor() { + ServiceDescriptor legacy = descriptor("example", Set.of("Example.", "LegacyExample."), + Set.of(ServiceProtocol.JSON, ServiceProtocol.PLAIN_JSON)); + ServiceDescriptor modern = descriptor("modern", Set.of("Modern."), Set.of(ServiceProtocol.JSON)); + ServiceCatalog catalog = new ServiceCatalog(List.of(legacy, modern)); + + assertEquals(legacy, catalog.matchPlainJsonTarget("Example.Read").orElseThrow().descriptor()); + assertEquals(legacy, catalog.matchPlainJsonTarget("LegacyExample.Read").orElseThrow().descriptor()); + assertTrue(catalog.matchPlainJsonTarget("Modern.Read").isEmpty()); + assertTrue(catalog.matchPlainJsonTarget("Unknown.Read").isEmpty()); + assertTrue(catalog.matchPlainJsonTarget(null).isEmpty()); + } + + @Test + void moreSpecificTargetOwnsItsAcceptedFormats() { + ServiceDescriptor legacy = descriptor("example", Set.of("Example."), + Set.of(ServiceProtocol.JSON, ServiceProtocol.PLAIN_JSON)); + ServiceDescriptor modern = descriptor("specific", Set.of("Example.Specific."), Set.of(ServiceProtocol.JSON)); + ServiceCatalog catalog = new ServiceCatalog(List.of(legacy, modern)); + + assertEquals(modern, catalog.matchTarget("Example.Specific.Read").orElseThrow().descriptor()); + assertTrue(catalog.matchPlainJsonTarget("Example.Specific.Read").isEmpty()); + } + + private static ServiceDescriptor descriptor(String name, Set prefixes, Set protocols) { + return new ServiceDescriptor(name, name, true, true, null, null, 0L, null, + ServiceProtocol.JSON, protocols, prefixes, Set.of(name), Set.of(), Set.of()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/core/common/ServiceEnablementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/ServiceEnablementIntegrationTest.java index 88165ed956..edcabfc04e 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/ServiceEnablementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/ServiceEnablementIntegrationTest.java @@ -58,6 +58,20 @@ class ServiceEnablementIntegrationTest { .body("message", equalTo("Service ecs is not enabled.")); } + @Test + void legacySwfJsonRequestsAreRejectedWhenServiceDisabled() { + given() + .contentType("application/json; charset=UTF-8") + .header("X-Amz-Target", "com.amazonaws.swf.service.model.SimpleWorkflowService.ListDomains") + .body("{\"registrationStatus\":\"REGISTERED\"}") + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ServiceNotAvailableException")) + .body("message", equalTo("Service swf is not enabled.")); + } + @Test void sqsQueueUrlJsonRequestsAreRejectedWhenServiceDisabled() { given() @@ -253,7 +267,8 @@ class ServiceEnablementIntegrationTest { "floci.services.lambda.enabled", "false", "floci.services.opensearch.enabled", "false", "floci.services.rds-data.enabled", "false", - "floci.services.sqs.enabled", "false" + "floci.services.sqs.enabled", "false", + "floci.services.swf.enabled", "false" ); } } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/SwfLegacyProtocolIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/SwfLegacyProtocolIntegrationTest.java new file mode 100644 index 0000000000..1a420e83ed --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/SwfLegacyProtocolIntegrationTest.java @@ -0,0 +1,128 @@ +package io.github.hectorvent.floci.core.common; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.cbor.CBORFactory; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.response.Response; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@QuarkusTest +@TestProfile(StrictProtocolClaimingIntegrationTest.StrictClaimingProfile.class) +class SwfLegacyProtocolIntegrationTest { + + private static final String LEGACY_PREFIX = "com.amazonaws.swf.service.model.SimpleWorkflowService."; + private static final String MODERN_PREFIX = "SimpleWorkflowService."; + private static final String LEGACY_JSON = "application/json; charset=UTF-8"; + private static final String MODERN_JSON = "application/x-amz-json-1.0"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @ParameterizedTest + @ValueSource(strings = {"application/json", LEGACY_JSON}) + void legacyRegistrationsShareStateWithModernClients(String mediaType) { + String domain = "legacy-swf-" + UUID.randomUUID(); + call(mediaType, LEGACY_PREFIX, "RegisterDomain", """ + {"name":"%s","workflowExecutionRetentionPeriodInDays":"7"} + """.formatted(domain)).then().statusCode(200).contentType("application/json"); + call(MODERN_JSON, MODERN_PREFIX, "DescribeDomain", "{\"name\":\"" + domain + "\"}") + .then().statusCode(200).body("domainInfo.name", equalTo(domain)); + + call(mediaType, LEGACY_PREFIX, "RegisterActivityType", """ + {"domain":"%s","name":"work","version":"1"} + """.formatted(domain)).then().statusCode(200); + call(MODERN_JSON, MODERN_PREFIX, "DescribeActivityType", """ + {"domain":"%s","activityType":{"name":"work","version":"1"}} + """.formatted(domain)).then().statusCode(200).body("typeInfo.status", equalTo("REGISTERED")); + + call(MODERN_JSON, MODERN_PREFIX, "RegisterWorkflowType", """ + {"domain":"%s","name":"flow","version":"1"} + """.formatted(domain)).then().statusCode(200); + call(mediaType, LEGACY_PREFIX, "DescribeWorkflowType", """ + {"domain":"%s","workflowType":{"name":"flow","version":"1"}} + """.formatted(domain)).then().statusCode(200).body("typeInfo.status", equalTo("REGISTERED")); + } + + @Test + void legacyDuplicateActivityRegistrationPreservesTypedBotoFaults() { + String domain = "legacy-duplicate-" + UUID.randomUUID(); + String domainBody = """ + {"name":"%s","workflowExecutionRetentionPeriodInDays":"7"} + """.formatted(domain); + call(MODERN_JSON, MODERN_PREFIX, "RegisterDomain", domainBody).then().statusCode(200); + String activity = "{\"domain\":\"" + domain + "\",\"name\":\"work\",\"version\":\"1\"}"; + call(MODERN_JSON, MODERN_PREFIX, "RegisterActivityType", activity).then().statusCode(200); + call(LEGACY_JSON, LEGACY_PREFIX, "RegisterActivityType", activity).then().statusCode(400) + .body("__type", equalTo("com.amazonaws.swf.base.model#TypeAlreadyExistsFault")); + } + + @ParameterizedTest + @CsvSource(textBlock = """ + SimpleWorkflowService., application/json + SimpleWorkflowService., application/json; charset=UTF-8 + SimpleWorkflowService., application/x-amz-json-1.0 + com.amazonaws.swf.service.model.SimpleWorkflowService., application/json + com.amazonaws.swf.service.model.SimpleWorkflowService., application/json; charset=UTF-8 + com.amazonaws.swf.service.model.SimpleWorkflowService., application/x-amz-json-1.0 + """) + void acceptedWireFormatsShareDomainStateAndTypedFaults(String prefix, String mediaType) { + String domain = "wire-swf-" + UUID.randomUUID(); + String registration = """ + {"name":"%s","workflowExecutionRetentionPeriodInDays":"7"} + """.formatted(domain); + call(MODERN_JSON, MODERN_PREFIX, "RegisterDomain", registration).then().statusCode(200); + call(mediaType, prefix, "DescribeDomain", "{\"name\":\"" + domain + "\"}") + .then().statusCode(200).body("domainInfo.name", equalTo(domain)); + call(mediaType, prefix, "RegisterDomain", registration).then().statusCode(400) + .body("__type", equalTo("com.amazonaws.swf.base.model#DomainAlreadyExistsFault")); + } + + @ParameterizedTest + @ValueSource(strings = {LEGACY_PREFIX, MODERN_PREFIX}) + void cborDoesNotClaimSwfTargets(String prefix) throws Exception { + byte[] response = given().contentType("application/x-amz-cbor-1.1") + .header("X-Amz-Target", prefix + "ListDomains") + .body(new byte[]{(byte) 0xbf, (byte) 0xff}) + .post("/").then().statusCode(404).contentType("application/x-amz-cbor-1.1") + .extract().asByteArray(); + + assertEquals("UnknownOperationException", + new ObjectMapper(new CBORFactory()).readTree(response).path("__type").asText()); + } + + @Test + void plainJsonWithoutTargetReportsTheMissingHeader() { + given().contentType(LEGACY_JSON).body("{}").post("/") + .then().statusCode(404).body("message", equalTo("Missing X-Amz-Target header.")); + } + + @ParameterizedTest + @ValueSource(strings = { + "com.amazonaws.swf.service.model.SimpleWorkflowServiceOther.ListDomains", + "AmazonSSM.DescribeParameters" + }) + void plainJsonDoesNotClaimOtherTargets(String target) { + call(LEGACY_JSON, "", target, "{}").then().statusCode(404); + } + + private static Response call(String mediaType, String prefix, String action, String body) { + return given().contentType(mediaType) + .header("X-Amz-Target", prefix + action) + .header("Content-Encoding", "amz-1.0") + .body(body).post("/"); + } +}