refactor(core): unify service metadata behind descriptor-backed catalog (#357)

Replace scattered service-keyed switch statements in ServiceRegistry,
ServiceEnabledFilter, StorageFactory, and the Aws{Query,Json,Json11,JsonCbor}
controllers with a single immutable ServiceDescriptor catalog. EmulatorConfig
is snapshotted once at @Startup into ResolvedServiceCatalog, which exposes
O(1) lookups by external key, storage key, target prefix, credential scope,
resource class, and CBOR SDK service id. Adding a service now requires one
descriptor entry plus handler wiring instead of touching six to eight
unrelated switches.

Deliberate behavior fixes bundled with the refactor:

- ec2/ecs were present in getServices() but missing from getEnabledServices();
  both now iterate allStatusDescriptors() so they cannot drift again.
- ServiceEnabledFilter did not recognize CertificateManager. or
  AmazonEC2ContainerServiceV20141113. target prefixes, so disabling acm or
  ecs in config had no effect on targeted requests. Both are now gated.
- The disabled-service response format was chosen by header sniffing
  (Accept/Content-Type). Auth-only REST GETs to Lambda and OpenSearch
  received XML when the SDK expected JSON. The filter now resolves the
  protocol from the descriptor's defaultProtocol.
- floci.storage.services.acm.{mode,flushIntervalMs} was wired in
  EmulatorConfig but never read by StorageFactory. It now takes effect.

The default-true fallback in isServiceEnabled for unknown service names is
preserved and pinned by a test, since non-inventoried AWS calls rely on it.
SqsQueueUrlRouterFilter remains imperative (its SQS identity is load-bearing
for queue-URL POST routing). AwsNamespaces and per-controller target-prefix
constant cleanup are deferred to a follow-up.

New integration tests cover the regression floor: Query XML, JSON 1.0,
JSON 1.1, CBOR (target-prefix and smithy-path), and REST disabled responses;
default-true fallback; ec2/ecs status parity; iam/sts shared enablement;
sesv2 credential scope alias; and the ACM storage override.
This commit is contained in:
TiM authored and GitHub committed 2026-04-11 20:23:02 -05:00
1 parent 101d45e2b0
commit 88b7ed1bb0
21 files changed
+1044 -358

No files matched your search

@@ -0,0 +1,51 @@
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;
/**
* Regression coverage for the descriptor-backed target matcher. catalog.matchTarget
* is protocol-agnostic: it will return a descriptor for a JSON 1.1 target even when
* the request arrived at the JSON 1.0 controller (and vice versa). Each controller
* must map such mismatches to UnknownOperationException rather than dropping the
* request on a null switch branch.
*/
@QuarkusTest
class CrossProtocolTargetRoutingIntegrationTest {
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void json11ControllerRejectsJson10TargetAsUnknownOperation() {
given()
.contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "DynamoDB_20120810.ListTables")
.body("{}")
.when()
.post("/")
.then()
.statusCode(404)
.body("__type", equalTo("UnknownOperationException"));
}
@Test
void json10ControllerRejectsJson11TargetAsUnknownOperation() {
given()
.contentType("application/x-amz-json-1.0")
.header("X-Amz-Target", "AmazonSSM.ListDocuments")
.body("{}")
.when()
.post("/")
.then()
.statusCode(404)
.body("__type", equalTo("UnknownOperationException"));
}
}
@@ -0,0 +1,32 @@
package io.github.hectorvent.floci.core.common;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertFalse;
@QuarkusTest
@TestProfile(IamStsSharedEnablementIntegrationTest.IamDisabledProfile.class)
class IamStsSharedEnablementIntegrationTest {
@Inject
ServiceRegistry serviceRegistry;
@Test
void disablingIamAlsoDisablesSts() {
assertFalse(serviceRegistry.isServiceEnabled("iam"));
assertFalse(serviceRegistry.isServiceEnabled("sts"));
}
public static final class IamDisabledProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.services.iam.enabled", "false");
}
}
}
@@ -0,0 +1,50 @@
package io.github.hectorvent.floci.core.common;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class ServiceCatalogRoutingIntegrationTest {
@Inject
ResolvedServiceCatalog catalog;
@Test
void targetResolutionExtractsMatchingPrefixAndAction() {
ServiceCatalog.TargetMatch match = catalog.matchTarget("AWSEvents.PutEvents").orElseThrow();
assertEquals("events", match.descriptor().externalKey());
assertEquals("AWSEvents.", match.prefix());
assertEquals("PutEvents", match.action());
}
@Test
void dynamodbStreamsTargetUsesStreamsPrefix() {
ServiceCatalog.TargetMatch match = catalog.matchTarget("DynamoDBStreams_20120810.DescribeStream").orElseThrow();
assertEquals("dynamodb", match.descriptor().externalKey());
assertEquals("DynamoDBStreams_20120810.", match.prefix());
assertEquals("DescribeStream", match.action());
}
@Test
void cborSdkServiceIdsResolveThroughCatalog() {
assertEquals("states", catalog.byCborSdkServiceId("SFN").orElseThrow().externalKey());
assertEquals("monitoring", catalog.byCborSdkServiceId("GraniteServiceVersion20100801").orElseThrow().externalKey());
}
@Test
void queryProtocolAliasesAreDeclaredOnDescriptors() {
assertTrue(catalog.byCredentialScope("sesv2").orElseThrow().supportsProtocol(ServiceProtocol.QUERY));
assertTrue(catalog.byCredentialScope("cognito-idp").orElseThrow().supportsProtocol(ServiceProtocol.QUERY));
}
@Test
void unknownTargetsRemainUnresolved() {
assertTrue(catalog.matchTarget("UnknownService.DoThing").isEmpty());
}
}
@@ -0,0 +1,181 @@
package io.github.hectorvent.floci.core.common;
import com.fasterxml.jackson.databind.JsonNode;
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.quarkus.test.junit.QuarkusTestProfile;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
@QuarkusTest
@TestProfile(ServiceEnablementIntegrationTest.DisabledServicesProfile.class)
class ServiceEnablementIntegrationTest {
private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory());
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void acmTargetedRequestsAreRejected() {
given()
.contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "CertificateManager.ListCertificates")
.body("{}")
.when()
.post("/")
.then()
.statusCode(400)
.contentType("application/json")
.body("__type", equalTo("ServiceNotAvailableException"))
.body("message", equalTo("Service acm is not enabled."));
}
@Test
void ecsTargetedRequestsAreRejected() {
given()
.contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "AmazonEC2ContainerServiceV20141113.ListClusters")
.body("{}")
.when()
.post("/")
.then()
.statusCode(400)
.contentType("application/json")
.body("__type", equalTo("ServiceNotAvailableException"))
.body("message", equalTo("Service ecs is not enabled."));
}
@Test
void sqsQueueUrlJsonRequestsAreRejectedWhenServiceDisabled() {
given()
.contentType("application/x-amz-json-1.0")
.header("X-Amz-Target", "AmazonSQS.GetQueueAttributes")
.body("""
{"QueueUrl":"http://localhost:4566/000000000000/disabled-queue","AttributeNames":["All"]}
""")
.when()
.post("/000000000000/disabled-queue")
.then()
.statusCode(400)
.contentType("application/json")
.body("__type", equalTo("ServiceNotAvailableException"))
.body("message", equalTo("Service sqs is not enabled."));
}
@Test
void sqsQueryRequestsReturnXmlWhenServiceDisabled() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", authorization("sqs"))
.formParam("Action", "ListQueues")
.when()
.post("/")
.then()
.statusCode(400)
.contentType("application/xml")
.body(containsString("<Code>ServiceNotAvailableException</Code>"))
.body(containsString("<Message>Service sqs is not enabled.</Message>"));
}
@Test
void dynamodbTargetedCborRequestsReturnCborErrors() throws Exception {
JsonNode body = cborBody(
given()
.contentType("application/cbor")
.accept("application/cbor")
.header("X-Amz-Target", "DynamoDB_20120810.ListTables")
.body(CBOR_MAPPER.writeValueAsBytes(Map.of()))
.when()
.post("/")
.then()
.statusCode(400)
.contentType("application/cbor")
.extract().asByteArray()
);
assertEquals("ServiceNotAvailableException", body.get("__type").asText());
assertEquals("Service dynamodb is not enabled.", body.get("message").asText());
}
@Test
void dynamodbSmithyCborRequestsReturnCborErrors() throws Exception {
JsonNode body = cborBody(
given()
.contentType("application/cbor")
.accept("application/cbor")
.header("Authorization", authorization("dynamodb"))
.body(CBOR_MAPPER.writeValueAsBytes(Map.of()))
.when()
.post("/service/DynamoDB/operation/ListTables")
.then()
.statusCode(400)
.contentType("application/cbor")
.extract().asByteArray()
);
assertEquals("ServiceNotAvailableException", body.get("__type").asText());
assertEquals("Service dynamodb is not enabled.", body.get("message").asText());
}
@Test
void signedLambdaGetRequestsReturnJsonWhenServiceDisabled() {
given()
.header("Authorization", authorization("lambda"))
.when()
.get("/2015-03-31/functions")
.then()
.statusCode(400)
.contentType("application/json")
.body("__type", equalTo("ServiceNotAvailableException"))
.body("message", equalTo("Service lambda is not enabled."));
}
@Test
void signedOpenSearchGetRequestsReturnJsonWhenServiceDisabled() {
given()
.header("Authorization", authorization("es"))
.when()
.get("/2021-01-01/domain")
.then()
.statusCode(400)
.contentType("application/json")
.body("__type", equalTo("ServiceNotAvailableException"))
.body("message", equalTo("Service es is not enabled."));
}
private static JsonNode cborBody(byte[] body) throws Exception {
return CBOR_MAPPER.readTree(body);
}
private static String authorization(String service) {
return "AWS4-HMAC-SHA256 Credential=test/20260411/us-east-1/" + service
+ "/aws4_request, SignedHeaders=host;x-amz-date, Signature=deadbeef";
}
public static final class DisabledServicesProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"floci.services.acm.enabled", "false",
"floci.services.dynamodb.enabled", "false",
"floci.services.ecs.enabled", "false",
"floci.services.lambda.enabled", "false",
"floci.services.opensearch.enabled", "false",
"floci.services.sqs.enabled", "false"
);
}
}
}
@@ -0,0 +1,25 @@
package io.github.hectorvent.floci.core.common;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class ServiceRegistryIntegrationTest {
@Inject
ServiceRegistry serviceRegistry;
@Test
void enabledServicesIncludeEc2AndEcs() {
assertTrue(serviceRegistry.getEnabledServices().contains("ec2"));
assertTrue(serviceRegistry.getEnabledServices().contains("ecs"));
}
@Test
void unknownServicesDefaultToEnabled() {
assertTrue(serviceRegistry.isServiceEnabled("totally-unknown-service"));
}
}
@@ -0,0 +1,42 @@
package io.github.hectorvent.floci.core.storage;
import com.fasterxml.jackson.core.type.TypeReference;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@QuarkusTest
@TestProfile(StorageFactoryServiceCatalogIntegrationTest.AcmPersistentStorageProfile.class)
class StorageFactoryServiceCatalogIntegrationTest {
@Inject
StorageFactory storageFactory;
@Test
void acmStorageOverrideIsApplied() {
StorageBackend<String, String> backend = storageFactory.create(
"acm",
"acm-test.json",
new TypeReference<Map<String, String>>() {}
);
assertInstanceOf(PersistentStorage.class, backend);
}
public static final class AcmPersistentStorageProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"floci.storage.mode", "memory",
"floci.storage.services.acm.mode", "persistent",
"floci.storage.persistent-path", "/tmp/floci-service-registry-unification-tests"
);
}
}
}
@@ -16,12 +16,16 @@ import static org.hamcrest.Matchers.*;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SesIntegrationTest {
private static String authorization(String service) {
return "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/" + service + "/aws4_request";
}
@Test
@Order(1)
void verifyEmailIdentity() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request")
.header("Authorization", authorization("email"))
.formParam("Action", "VerifyEmailIdentity")
.formParam("EmailAddress", "sender@example.com")
.when()
@@ -37,7 +41,7 @@ class SesIntegrationTest {
void verifyEmailIdentity_second() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request")
.header("Authorization", authorization("email"))
.formParam("Action", "VerifyEmailIdentity")
.formParam("EmailAddress", "recipient@example.com")
.when()
@@ -51,7 +55,7 @@ class SesIntegrationTest {
void verifyDomainIdentity() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request")
.header("Authorization", authorization("email"))
.formParam("Action", "VerifyDomainIdentity")
.formParam("Domain", "example.com")
.when()
@@ -228,7 +232,21 @@ class SesIntegrationTest {
void getAccountSendingEnabled() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request")
.header("Authorization", authorization("email"))
.formParam("Action", "GetAccountSendingEnabled")
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<Enabled>true</Enabled>"));
}
@Test
@Order(14)
void getAccountSendingEnabled_acceptsSesv2CredentialScopeAlias() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", authorization("sesv2"))
.formParam("Action", "GetAccountSendingEnabled")
.when()
.post("/")
@@ -242,7 +260,7 @@ class SesIntegrationTest {
void getIdentityDkimAttributes() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request")
.header("Authorization", authorization("email"))
.formParam("Action", "GetIdentityDkimAttributes")
.formParam("Identities.member.1", "example.com")
.when()