Light, fluffy, and always free - The AWS Local Emulator alternative
fix(core): treat a request no protocol claims as the call its route runs (#6200)
Priamo Germosen2f57441b8457
diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.mdindex 25b376b978..dc5ac781dd 100644--- a/docs/configuration/environment-variables.md+++ b/docs/configuration/environment-variables.md@@ -71,7 +71,7 @@ See [TLS / HTTPS](./tls.md) for SDK configuration examples and WebSocket (`wss:/| Variable | Default | Description ||---|---|---|| `FLOCI_PROTOCOLS_MAX_REQUEST_SIZE` | `2048` | Maximum HTTP request body size in megabytes (feeds `quarkus.http.limits.max-body-size`). Legacy name `FLOCI_MAX_REQUEST_SIZE` still works |-| `FLOCI_PROTOCOLS_STRICT_CLAIMING` | `false` | Reject RPC-signaled requests that no supported wire protocol claims, per the [Smithy wire-protocol-selection guide](https://smithy.io/2.0/guides/wire-protocol-selection.html) (e.g. an unknown `Smithy-Protocol` header value or an unimplemented `rpc-v2-json` request). When disabled such requests are logged and pass through |+| `FLOCI_PROTOCOLS_STRICT_CLAIMING` | `false` | Reject RPC-signaled requests that no supported wire protocol claims, per the [Smithy wire-protocol-selection guide](https://smithy.io/2.0/guides/wire-protocol-selection.html) (e.g. an unknown `Smithy-Protocol` header value or an unimplemented `rpc-v2-json` request). When disabled such requests are logged and pass through, and are handled as the protocol of the route that serves them: one served by the JSON, CBOR or Query endpoint is an RPC call, and one served by a service's route is a REST request || `FLOCI_PROTOCOLS_REJECT_UNKNOWN_SERVICE_SCOPE` | `true` | Reject REST requests whose SigV4 credential scope names a service Floci does not implement, with `UnknownOperationException` instead of letting them fall through to S3's path-style routes and return a misleading `NoSuchBucket`. Set to `false` if Floci serves a route whose signing scope is not yet enumerated: the request then falls through as before instead of failing with a 404 |---diff --git a/docs/services/iam.md b/docs/services/iam.mdindex 1adcea9303..5261be1d90 100644--- a/docs/services/iam.md+++ b/docs/services/iam.md@@ -1246,10 +1246,10 @@ requests. REST-JSON requests return HTTP 403 `AccessDeniedException`; AWS Queryrequests retain HTTP 403 XML `AccessDenied` responses. The routed protocol determinesthe status, not just the request's content type.-A REST request is authorized as the operation of the route it reached. An `X-Amz-Target` header or-a Query `Action` field on it does not change the action it is checked as, and its path is matched-still percent-encoded, as the router matches it, so an encoded `/` inside a parameter cannot make-the route's rule miss.+A REST request is authorized as the operation of the route it reached. An `X-Amz-Target` header, a+Query `Action` field or a `smithy-protocol` header on it does not change the action it is checked+as, and its path is matched still percent-encoded, as the router matches it, so an encoded `/`+inside a parameter cannot make the route's rule miss.The REST routes mapped to an action are those of S3, Lambda, API Gateway, SES v2, RDS Data,Kinesis, EventBridge Scheduler and EventBridge Pipes. In Lambda they are the function, alias,@@ -1936,7 +1936,9 @@ lazily-resolved `ScpProvider` exists to avoid.With enforcement on, a request carrying no `Authorization` header is refused with`403 MissingAuthenticationToken` when its wire shape names a management operation: a JSON, CBOR orQuery request, identified by `X-Amz-Target`, an rpcv2 path or an `Action` parameter. A call routed-to the rpcv2 endpoint is one whatever its `smithy-protocol` header says.+to the JSON, CBOR, rpcv2 or Query endpoint is one whatever its `smithy-protocol` header or content+type says: with `FLOCI_PROTOCOLS_STRICT_CLAIMING` off (the default), a request whose protocol+those signals leave unclear is still served, and is judged by the endpoint that serves it.Two carve-outs, both deliberate.diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex 2ba42ec79c..48f74cb775 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -156,7 +156,8 @@ public interface EmulatorConfig {* wire-protocol-selection guide (e.g. an unknown Smithy-Protocol header* value, a recognized-but-unimplemented rpc-v2-json request, or an* X-Amz-Target post with a foreign content type). When disabled such- * requests are only logged and pass through to JAX-RS matching.+ * requests are logged and pass through to JAX-RS matching, and are then+ * handled as the protocol of the route that serves them.*/@WithDefault("false")boolean strictClaiming();diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsProtocolClaimFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsProtocolClaimFilter.javaindex 30aca98105..0d355b2c1d 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsProtocolClaimFilter.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsProtocolClaimFilter.java@@ -68,6 +68,7 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {ctx.abortWith(unknownOperationResponse(404,"Unable to determine the wire protocol for this request"));}+ // Otherwise the route it reaches claims it: see AwsRouteClaimFilter.return;}@@ -78,22 +79,8 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {.replacePath(AwsQueryController.QUERY_GET_PATH).build());}if (resolved.protocol() == WireProtocol.REST) {- Optional<String> scope = SigV4CredentialScope.serviceName(signals.authorization());- if (scope.isPresent() && catalog.byCredentialScope(scope.get()).isEmpty()) {- // Gated like the other rejections in this filter. The guard is only as good as- // the catalog's scope list: if Floci serves a route whose signing scope is not- // enumerated, rejecting is a 404 with no workaround, so leave users a switch.- if (!configProvider.get().protocols().rejectUnknownServiceScope()) {- LOG.debugv("Unsupported service scope {0} on {1} {2}, rejection disabled by config",- scope.get(), signals.method(), signals.path());- return;- }- LOG.infov("Rejecting request signed for unsupported service scope {0}: {1} {2}",- scope.get(), signals.method(), signals.path());- ctx.abortWith(unknownOperationResponse(404,- "Unknown operation: " + signals.method() + " " + signals.path()));- return;- }+ refuseUnknownServiceScope(ctx, signals, catalog, configProvider.get());+ return;}if (resolved.protocol() == WireProtocol.RPCV2_JSON) {LOG.warnv("Received rpc-v2-json request for service {0} operation {1} — protocol not implemented",@@ -111,6 +98,32 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {}}+ /**+ * Refuses a REST request whose SigV4 credential scope names a service absent from the catalog,+ * which would otherwise fall through to S3's path-style routes. {@link AwsRouteClaimFilter} also+ * applies it to a request it claims as REST. Returns whether it refused the request.+ */+ static boolean refuseUnknownServiceScope(ContainerRequestContext ctx, ClaimSignals signals,+ ResolvedServiceCatalog catalog, EmulatorConfig config) {+ Optional<String> scope = SigV4CredentialScope.serviceName(signals.authorization());+ if (scope.isEmpty() || catalog.byCredentialScope(scope.get()).isPresent()) {+ return false;+ }+ // Gated like the other rejections in this filter. The guard is only as good as the+ // catalog's scope list: if Floci serves a route whose signing scope is not enumerated,+ // rejecting is a 404 with no workaround, so leave users a switch.+ if (!config.protocols().rejectUnknownServiceScope()) {+ LOG.debugv("Unsupported service scope {0} on {1} {2}, rejection disabled by config",+ scope.get(), signals.method(), signals.path());+ return false;+ }+ LOG.infov("Rejecting request signed for unsupported service scope {0}: {1} {2}",+ scope.get(), signals.method(), signals.path());+ ctx.abortWith(unknownOperationResponse(404,+ "Unknown operation: " + signals.method() + " " + signals.path()));+ return true;+ }+static Response unknownOperationResponse(int status, String message) {return errorResponse(status, "UnknownOperationException", message);}diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsRestJsonErrorHeaderFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsRestJsonErrorHeaderFilter.javaindex f954851bb6..8beb71f828 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsRestJsonErrorHeaderFilter.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsRestJsonErrorHeaderFilter.java@@ -51,8 +51,9 @@ public class AwsRestJsonErrorHeaderFilter implements ContainerResponseFilter {if (claimValue instanceof ProtocolClaim claim && claim.service() != null) {return Optional.of(claim.service());}- if (resourceInfo != null && resourceInfo.getResourceClass() != null) {- Optional<ServiceDescriptor> descriptor = catalog.byResourceClass(resourceInfo.getResourceClass());+ Class<?> resourceClass = RoutedResource.resourceClass(resourceInfo);+ if (resourceClass != null) {+ Optional<ServiceDescriptor> descriptor = catalog.byResourceClass(resourceClass);if (descriptor.isPresent()) {return descriptor;}diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsRouteClaimFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsRouteClaimFilter.javanew file mode 100644index 0000000000..d4f003392b--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsRouteClaimFilter.java@@ -0,0 +1,67 @@+package io.github.hectorvent.floci.core.common;++import io.github.hectorvent.floci.config.EmulatorConfig;+import jakarta.annotation.Priority;+import jakarta.inject.Inject;+import jakarta.ws.rs.Priorities;+import jakarta.ws.rs.container.ContainerRequestContext;+import jakarta.ws.rs.container.ContainerRequestFilter;+import jakarta.ws.rs.container.ResourceInfo;+import jakarta.ws.rs.core.Context;+import jakarta.ws.rs.ext.Provider;+import org.jboss.logging.Logger;++/**+ * Claims, by its route, a request {@link AwsProtocolClaimFilter} served without a claim. With+ * {@code floci.protocols.strict-claiming} off, a request no protocol claims, such as one carrying an+ * unrecognised {@code Smithy-Protocol} value or an {@code X-Amz-Target} with a content type no+ * protocol uses, is logged and passes on to route matching, which serves it. Every check that reads+ * the claim would otherwise see a request of no protocol: the unsigned refusal let it through, and+ * IAM enforcement checked a REST request as the action its {@code X-Amz-Target} named rather than+ * its route's. A request claimed as REST also gets the check a claimed one gets before routing: one+ * signed for a service Floci does not serve is refused rather than answered by S3's routes.+ * <p>+ * Runs after route matching, so {@link ResourceInfo} names the route, and before every filter that+ * reads the claim: {@link AccountContextFilter}, which answers a request signed for an unknown Region+ * in the claim's error format, runs at {@code AUTHENTICATION - 100}, and the signature check at+ * {@link Priorities#AUTHENTICATION}.+ */+@Provider+@Priority(Priorities.AUTHENTICATION - 200)+public class AwsRouteClaimFilter implements ContainerRequestFilter {++ private static final Logger LOG = Logger.getLogger(AwsRouteClaimFilter.class);++ @Context+ ResourceInfo resourceInfo;++ private final ProtocolClaimer claimer;+ private final ResolvedServiceCatalog catalog;+ // Lazily resolved: JAX-RS providers are instantiated before runtime config mappings exist.+ private final jakarta.inject.Provider<EmulatorConfig> configProvider;++ @Inject+ public AwsRouteClaimFilter(ProtocolClaimer claimer, ResolvedServiceCatalog catalog,+ jakarta.inject.Provider<EmulatorConfig> configProvider) {+ this.claimer = claimer;+ this.catalog = catalog;+ this.configProvider = configProvider;+ }++ @Override+ public void filter(ContainerRequestContext ctx) {+ Class<?> resourceClass = RoutedResource.resourceClass(resourceInfo);+ if (ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY) instanceof ProtocolClaim || resourceClass == null) {+ return;+ }+ ClaimSignals signals = ClaimSignals.from(ctx);+ ProtocolClaim claim = claimer.routeClaim(resourceClass, signals, ctx.getUriInfo().getPathParameters());+ LOG.debugv("Claimed {0} {1} by its route: {2} service={3} operation={4}", ctx.getMethod(),+ ctx.getUriInfo().getPath(), claim.protocol(),+ claim.service() != null ? claim.service().externalKey() : null, claim.operation());+ ctx.setProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY, claim);+ if (claim.protocol() == WireProtocol.REST) {+ AwsProtocolClaimFilter.refuseUnknownServiceScope(ctx, signals, catalog, configProvider.get());+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.javaindex b772fffa18..1abe91b86f 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java@@ -770,14 +770,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {if (resourceInfo == null || !AwsJsonCborController.class.equals(resourceInfo.getResourceClass())) {return null;}- MultivaluedMap<String, String> parameters = ctx.getUriInfo().getPathParameters();- String serviceId = parameters.getFirst(AwsJsonCborController.RPC_V2_SERVICE_PARAMETER);- String operation = parameters.getFirst(AwsJsonCborController.RPC_V2_OPERATION_PARAMETER);- if (serviceId == null || operation == null) {- return null;- }- return new ProtocolClaim(WireProtocol.RPCV2_CBOR, catalog.byCborSdkServiceId(serviceId).orElse(null),- operation, null);+ return ProtocolClaimer.rpcV2RouteClaim(catalog, ctx.getUriInfo().getPathParameters());}/**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.javaindex c7ab12c473..5efdbbd723 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@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import jakarta.ws.rs.core.MediaType;+import jakarta.ws.rs.core.MultivaluedMap;import java.util.Locale;import java.util.Optional;@@ -28,7 +29,8 @@ import java.util.regex.Pattern;* <p>* An empty result means the request carries an RPC signal that no supported* protocol claims — per the guide such input must be rejected (enforced only- * when {@code floci.protocols.strict-claiming} is enabled).+ * when {@code floci.protocols.strict-claiming} is enabled). With it disabled the+ * request is served, and {@link #routeClaim} claims it by the route it reaches.* <p>* Intentional deviation from pure precedence iteration: a present* Smithy-Protocol header is treated as an exclusive rpcv2 signal. If its value@@ -123,29 +125,78 @@ public class ProtocolClaimer {return Optional.empty();}if (CONTENT_TYPE_FORM_URLENCODED.equals(mediaType)) {- ServiceDescriptor service = SigV4CredentialScope.serviceName(signals.authorization())- .flatMap(catalog::byCredentialScope)- .orElse(null);- return Optional.of(new ProtocolClaim(WireProtocol.AWS_QUERY, service, null, null));+ return Optional.of(queryClaim(signals));}}return Optional.of(ProtocolClaim.rest());}- private ProtocolClaim rpcV2Claim(WireProtocol protocol, Matcher rpcV2Path) {- String serviceName = rpcV2Path.group(1);- String operation = rpcV2Path.group(2);+ /**+ * The claim of a request {@link #claim} could not claim, made from the route JAX-RS matched it+ * to. With strict claiming off such a request is still served, by a route that dispatches it the+ * same way whatever its {@code Smithy-Protocol} header or content type says: the JSON, CBOR and+ * Query endpoints on {@code X-Amz-Target}, the rpcv2 path and the form, and every other route as+ * its REST operation. Claimed this way, every check that reads the claim sees the call that runs.+ */+ public ProtocolClaim routeClaim(Class<?> resourceClass, ClaimSignals signals,+ MultivaluedMap<String, String> pathParameters) {+ if (resourceClass == AwsJsonController.class) {+ return targetClaim(WireProtocol.AWS_JSON_1_0, signals.xAmzTarget());+ }+ if (resourceClass == AwsJson11Controller.class) {+ return targetClaim(WireProtocol.AWS_JSON_1_1, signals.xAmzTarget());+ }+ if (resourceClass == AwsJsonCborController.class) {+ ProtocolClaim rpcV2 = rpcV2RouteClaim(catalog, pathParameters);+ return rpcV2 != null ? rpcV2 : targetClaim(WireProtocol.AWS_CBOR_TARGET, signals.xAmzTarget());+ }+ if (resourceClass == AwsQueryController.class && "POST".equalsIgnoreCase(signals.method())) {+ return queryClaim(signals);+ }+ // The Query endpoint's GET route serves only the Query GET that claim() rewrites onto it;+ // reached directly it is no Query call, as claim() would say without the header.+ return ProtocolClaim.rest();+ }++ /**+ * The claim of a call routed to the Smithy RPC v2 endpoint, made from the service and operation+ * in the route's path parameters, which are what that endpoint dispatches. Null when the+ * parameters do not name both, as on the endpoint's {@code X-Amz-Target} route.+ */+ static ProtocolClaim rpcV2RouteClaim(ResolvedServiceCatalog catalog,+ MultivaluedMap<String, String> pathParameters) {+ String serviceId = pathParameters.getFirst(AwsJsonCborController.RPC_V2_SERVICE_PARAMETER);+ String operation = pathParameters.getFirst(AwsJsonCborController.RPC_V2_OPERATION_PARAMETER);+ if (serviceId == null || operation == null) {+ return null;+ }+ return rpcV2Claim(catalog, WireProtocol.RPCV2_CBOR, serviceId, operation);+ }++ private static ProtocolClaim rpcV2Claim(ResolvedServiceCatalog catalog, WireProtocol protocol,+ String serviceName, String operation) {ServiceDescriptor service = catalog.byCborSdkServiceId(serviceName).orElse(null);return new ProtocolClaim(protocol, service, operation, null);}+ private ProtocolClaim rpcV2Claim(WireProtocol protocol, Matcher rpcV2Path) {+ return rpcV2Claim(catalog, protocol, rpcV2Path.group(1), rpcV2Path.group(2));+ }+private ProtocolClaim targetClaim(WireProtocol protocol, String target) {return catalog.matchTarget(target).map(match -> new ProtocolClaim(protocol, match.descriptor(), match.action(), target)).orElseGet(() -> new ProtocolClaim(protocol, null, null, target));}+ private ProtocolClaim queryClaim(ClaimSignals signals) {+ ServiceDescriptor service = SigV4CredentialScope.serviceName(signals.authorization())+ .flatMap(catalog::byCredentialScope)+ .orElse(null);+ return new ProtocolClaim(WireProtocol.AWS_QUERY, service, null, null);+ }+static boolean isRpcV2Path(String path) {return path != null && RPC_V2_PATH.matcher(path).matches();}diff --git a/src/main/java/io/github/hectorvent/floci/core/common/RoutedResource.java b/src/main/java/io/github/hectorvent/floci/core/common/RoutedResource.javanew file mode 100644index 0000000000..82a8aca523--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/RoutedResource.java@@ -0,0 +1,31 @@+package io.github.hectorvent.floci.core.common;++import jakarta.enterprise.inject.IllegalProductException;+import jakarta.ws.rs.container.ResourceInfo;++/**+ * Reads the resource a request was routed to from a response filter, which also runs for requests+ * routing turned away. For most of those {@link ResourceInfo} names no class, but not for all: when+ * several resource methods share a path and HTTP method and differ only in what they consume,+ * RESTEasy Reactive routes to a placeholder with no method of its own, which picks one by content+ * type. When none consumes the request's content type it answers 415 from that placeholder, the+ * {@code ResourceInfo} producer returns its missing method, and CDI refuses the null with+ * {@link IllegalProductException} on the first call, turning the 415 into a 500.+ */+public final class RoutedResource {++ private RoutedResource() {+ }++ /** The class of the resource method the request was routed to, or null when routing chose none. */+ public static Class<?> resourceClass(ResourceInfo resourceInfo) {+ if (resourceInfo == null) {+ return null;+ }+ try {+ return resourceInfo.getResourceClass();+ } catch (IllegalProductException noMethodChosen) {+ return null;+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3SignatureFilterScope.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3SignatureFilterScope.javaindex d30ba9fe78..ece1bbaa8e 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3SignatureFilterScope.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3SignatureFilterScope.java@@ -1,5 +1,6 @@package io.github.hectorvent.floci.services.s3;+import io.github.hectorvent.floci.core.common.RoutedResource;import jakarta.ws.rs.container.ResourceInfo;final class S3SignatureFilterScope {@@ -12,7 +13,8 @@ final class S3SignatureFilterScope {|| (presignGenerator != null && presignGenerator.shouldValidateSignatures());}+ /** Safe from a response filter too, which also sees requests routing turned away. */static boolean routedToS3(ResourceInfo resourceInfo) {- return resourceInfo != null && S3Controller.class.equals(resourceInfo.getResourceClass());+ return S3Controller.class.equals(RoutedResource.resourceClass(resourceInfo));}}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsRestJsonErrorHeaderFilterTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsRestJsonErrorHeaderFilterTest.javaindex de22191292..f4a28c8eab 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/AwsRestJsonErrorHeaderFilterTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsRestJsonErrorHeaderFilterTest.java@@ -1,7 +1,9 @@package io.github.hectorvent.floci.core.common;+import jakarta.enterprise.inject.IllegalProductException;import jakarta.ws.rs.container.ContainerRequestContext;import jakarta.ws.rs.container.ContainerResponseContext;+import jakarta.ws.rs.container.ResourceInfo;import org.junit.jupiter.api.Test;import static io.github.hectorvent.floci.testing.InlineMocks.inlineMock;@@ -31,4 +33,20 @@ class AwsRestJsonErrorHeaderFilterTest {verify(response, never()).getEntity();verify(response, never()).getHeaders();}++ @Test+ void leavesTheErrorOfARequestRoutingTurnedAwayAlone() {+ // A 415 from RESTEasy's media-type dispatch leaves a ResourceInfo whose producer CDI refuses.+ ResourceInfo noMethodChosen = mock(ResourceInfo.class);+ when(noMethodChosen.getResourceClass()).thenThrow(new IllegalProductException("no method chosen"));+ ContainerRequestContext request = mock(ContainerRequestContext.class);+ ContainerResponseContext response = mock(ContainerResponseContext.class);+ when(response.getStatus()).thenReturn(415);++ AwsRestJsonErrorHeaderFilter filter = new AwsRestJsonErrorHeaderFilter(mock(ResolvedServiceCatalog.class));+ filter.resourceInfo = noMethodChosen;+ filter.filter(request, response);++ verify(response, never()).getHeaders();+ }}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionIntegrationTest.javaindex 4b93fc8c19..3858212e7d 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionIntegrationTest.java@@ -1,19 +1,26 @@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.PartitionCleanup;import io.github.hectorvent.floci.testing.PartitionMatrix;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.junit.QuarkusTest;+import io.restassured.specification.RequestSpecification;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.extension.RegisterExtension;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;import java.nio.charset.StandardCharsets;+import java.util.Map;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.containsString;import static org.hamcrest.Matchers.equalTo;import static org.hamcrest.Matchers.not;+import static org.junit.jupiter.api.Assertions.assertEquals;/*** A SigV4 scope region that no partition publishes or admits by its region pattern is refused,@@ -62,6 +69,27 @@ class PartitionUnknownRegionIntegrationTest {}/** A presigned POST's credential sits in the form body, out of the ingress filter's sight. */+ @ParameterizedTest+ @ValueSource(booleans = {false, true})+ void anUnclaimedCborRequestSignedForAnUnknownRegionGetsTheCborSignatureError(boolean rpcV2) throws Exception {+ // An unrecognised Smithy-Protocol leaves it to its route to claim, which has to happen before+ // the Region check picks the error format.+ ObjectMapper cbor = new ObjectMapper(new CBORFactory());+ String mediaType = rpcV2 ? "application/cbor" : "application/x-amz-cbor-1.1";+ RequestSpecification request = given().contentType(mediaType).accept(mediaType)+ .header("Authorization", PartitionMatrix.sigV4Auth(UNKNOWN, "monitoring"))+ .header("smithy-protocol", "unrecognised")+ .body(cbor.writeValueAsBytes(Map.of()));+ if (!rpcV2) {+ request.header("X-Amz-Target", "GraniteServiceVersion20100801.ListMetrics");+ }+ byte[] response = request.when().post(rpcV2+ ? "/service/GraniteServiceVersion20100801/operation/ListMetrics" : "/").then().statusCode(400)+ .contentType(mediaType).header("x-amzn-query-error", equalTo("InvalidSignatureException;Sender"))+ .extract().asByteArray();+ assertEquals("InvalidSignatureException", cbor.readTree(response).path("__type").asText());+ }+@Testvoid aPresignedPostSignedForAnUnknownRegionGetsS3sMalformedHeaderError() {given()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.javaindex 649f3aeb9e..8a61f22ee1 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@@ -1,5 +1,6 @@package io.github.hectorvent.floci.core.common;+import io.github.hectorvent.floci.services.s3.S3Controller;import io.quarkus.test.junit.QuarkusTest;import jakarta.inject.Inject;import jakarta.ws.rs.core.MultivaluedHashMap;@@ -221,4 +222,55 @@ class ProtocolClaimerIntegrationTest {assertEquals(WireProtocol.REST, claim.protocol());}++ @Test+ void anUnclaimedRequestIsClaimedAsTheEndpointItReachedServesIt() {+ ClaimSignals signals = new ClaimSignals("POST", "/", "application/x-amz-json-1.0", "unrecognised",+ "AmazonSQS.ListQueues", SQS_AUTH, null, new MultivaluedHashMap<>());+ MultivaluedHashMap<String, String> noPathParameters = new MultivaluedHashMap<>();++ ProtocolClaim json = claimer.routeClaim(AwsJsonController.class, signals, noPathParameters);+ assertEquals(WireProtocol.AWS_JSON_1_0, json.protocol());+ assertEquals("sqs", json.service().externalKey());+ assertEquals("ListQueues", json.operation());++ assertEquals(WireProtocol.AWS_JSON_1_1,+ claimer.routeClaim(AwsJson11Controller.class, signals, noPathParameters).protocol());+ assertEquals(WireProtocol.AWS_CBOR_TARGET,+ claimer.routeClaim(AwsJsonCborController.class, signals, noPathParameters).protocol());++ ProtocolClaim query = claimer.routeClaim(AwsQueryController.class, signals, noPathParameters);+ assertEquals(WireProtocol.AWS_QUERY, query.protocol());+ assertEquals("sqs", query.service().externalKey());+ }++ @Test+ void anUnclaimedRequestOnTheRpcV2RouteIsClaimedByThePathItDispatches() {+ MultivaluedHashMap<String, String> pathParameters = new MultivaluedHashMap<>();+ pathParameters.putSingle(AwsJsonCborController.RPC_V2_SERVICE_PARAMETER, "DynamoDB_20120810");+ pathParameters.putSingle(AwsJsonCborController.RPC_V2_OPERATION_PARAMETER, "ListTables");++ ProtocolClaim claim = claimer.routeClaim(AwsJsonCborController.class,+ new ClaimSignals("POST", "/service/DynamoDB_20120810/operation/ListTables", "application/cbor",+ "unrecognised", "AmazonSQS.ListQueues", null, null, new MultivaluedHashMap<>()),+ pathParameters);++ assertEquals(WireProtocol.RPCV2_CBOR, claim.protocol());+ assertEquals("dynamodb", claim.service().externalKey());+ assertEquals("ListTables", claim.operation());+ }++ @Test+ void anUnclaimedRequestOnAnyOtherRouteIsRest() {+ MultivaluedHashMap<String, String> noPathParameters = new MultivaluedHashMap<>();+ ClaimSignals delete = new ClaimSignals("DELETE", "/bucket/key", null, "unrecognised",+ "AmazonS3.GetObject", null, null, new MultivaluedHashMap<>());+ assertEquals(WireProtocol.REST, claimer.routeClaim(S3Controller.class, delete, noPathParameters).protocol());++ // The Query endpoint's GET route is reached as a Query call only through the Query GET+ // rewrite, which only a claimed request gets.+ ClaimSignals get = new ClaimSignals("GET", AwsQueryController.QUERY_GET_PATH, null, "unrecognised",+ null, null, null, new MultivaluedHashMap<>());+ assertEquals(WireProtocol.REST, claimer.routeClaim(AwsQueryController.class, get, noPathParameters).protocol());+ }}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.javaindex fd5d8eb927..6f6af47450 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@@ -43,6 +43,23 @@ class ServiceEnablementIntegrationTest {.body("message", equalTo("Service acm is not enabled."));}+ @Test+ void aTargetedRequestWithAnUnrecognisedSmithyProtocolIsStillRejected() {+ // No protocol claims it, so it is served as its route is: as a call to acm, which is disabled.+ given()+ .contentType("application/x-amz-json-1.1")+ .header("X-Amz-Target", "CertificateManager.ListCertificates")+ .header("smithy-protocol", "unrecognised")+ .body("{}")+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .contentType("application/json")+ .body("__type", equalTo("ServiceNotAvailableException"))+ .body("message", equalTo("Service acm is not enabled."));+ }+@Testvoid ecsTargetedRequestsAreRejected() {given()@@ -154,6 +171,27 @@ class ServiceEnablementIntegrationTest {assertEquals("Service dynamodb is not enabled.", body.get("message").asText());}+ @Test+ void anRpcV2CallWithAnUnrecognisedSmithyProtocolIsStillRejected() throws Exception {+ // Unsigned, so only the path it is routed by names the service.+ JsonNode body = cborBody(+ given()+ .contentType("application/cbor")+ .accept("application/cbor")+ .header("smithy-protocol", "unrecognised")+ .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());+ }+@Testvoid signedLambdaGetRequestsReturnJsonWhenServiceDisabled() {given()diff --git a/src/test/java/io/github/hectorvent/floci/core/common/UnknownServiceScopeGuardDisabledIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/UnknownServiceScopeGuardDisabledIntegrationTest.javaindex 2fc269a495..32d032a1eb 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/UnknownServiceScopeGuardDisabledIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/UnknownServiceScopeGuardDisabledIntegrationTest.java@@ -40,6 +40,19 @@ class UnknownServiceScopeGuardDisabledIntegrationTest {.body(containsString("<Code>NoSuchBucket</Code>"));}+ @Test+ void unsupportedScopeWithAnUnrecognisedSmithyProtocolFallsThroughWhenRejectionDisabled() {+ given()+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260707/us-east-1/account"+ + "/aws4_request, SignedHeaders=host;x-amz-date, Signature=deadbeef")+ .header("smithy-protocol", "unrecognised")+ .when()+ .get("/guard-disabled-no-such-bucket")+ .then()+ .statusCode(404)+ .body(containsString("<Code>NoSuchBucket</Code>"));+ }+@Testvoid knownRestJsonScopeFallsThroughWhenRejectionDisabled() {given()diff --git a/src/test/java/io/github/hectorvent/floci/core/common/UnknownServiceScopeGuardIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/UnknownServiceScopeGuardIntegrationTest.javaindex 7807b88157..9c2dce3462 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/UnknownServiceScopeGuardIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/UnknownServiceScopeGuardIntegrationTest.java@@ -45,6 +45,23 @@ class UnknownServiceScopeGuardIntegrationTest {.body("__type", equalTo("UnknownOperationException"));}+ @Test+ void unsupportedRestServiceScopeWithAnUnrecognisedSmithyProtocolGetsUnknownOperation() {+ // No protocol claims it before routing; the REST route it reaches claims it, and the guard+ // still applies.+ given()+ .header("Authorization", authorization("support"))+ .header("smithy-protocol", "unrecognised")+ .contentType("application/json")+ .body("{}")+ .when()+ .post("/listRegions")+ .then()+ .statusCode(404)+ .body("__type", equalTo("UnknownOperationException"))+ .body("message", equalTo("Unknown operation: POST /listRegions"));+ }+@Testvoid knownRestJsonScopeCannotFallThroughToS3Wildcard() {given()@@ -61,6 +78,22 @@ class UnknownServiceScopeGuardIntegrationTest {.body("message", equalTo("Unknown operation: POST /prompts"));}+ @Test+ void aKnownRestJsonScopeWithAnUnrecognisedSmithyProtocolCannotFallThroughToS3Wildcard() {+ // No protocol claims it, so it is judged as the S3 route it reached, which bedrock is not.+ given()+ .header("Authorization", authorization("bedrock"))+ .header("smithy-protocol", "unrecognised")+ .contentType("application/x-amz-json-1.1")+ .body("{\"name\":\"probe\"}")+ .when()+ .post("/prompts")+ .then()+ .statusCode(404)+ .body("__type", equalTo("UnknownOperationException"))+ .body("message", equalTo("Unknown operation: POST /prompts"));+ }+@Testvoid bedrockScopeStillReachesBedrockRuntimeRoutes() {given()diff --git a/src/test/java/io/github/hectorvent/floci/core/common/UnroutableRequestIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/UnroutableRequestIntegrationTest.javanew file mode 100644index 0000000000..58727bb556--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/core/common/UnroutableRequestIntegrationTest.java@@ -0,0 +1,38 @@+package io.github.hectorvent.floci.core.common;++import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;++/**+ * A request no resource method consumes is answered with routing's own status. The response filters+ * also run for it, and reading the routed resource there used to fail with a 500.+ */+@QuarkusTest+class UnroutableRequestIntegrationTest {++ @Test+ void aRootPostNoEndpointConsumesGetsUnsupportedMediaType() {+ given()+ .contentType("text/plain")+ .body("{}")+ .when()+ .post("/")+ .then()+ .statusCode(415);+ }++ @Test+ void aTargetedCallInAContentTypeNoEndpointConsumesGetsUnsupportedMediaType() {+ // No protocol claims it either: an X-Amz-Target post with a content type no protocol uses.+ given()+ .contentType("text/plain")+ .header("X-Amz-Target", "AmazonSQS.ListQueues")+ .body("{}")+ .when()+ .post("/")+ .then()+ .statusCode(415);+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/UnclaimedRequestEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/UnclaimedRequestEnforcementIntegrationTest.javanew file mode 100644index 0000000000..1589793024--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/iam/UnclaimedRequestEnforcementIntegrationTest.java@@ -0,0 +1,189 @@+package io.github.hectorvent.floci.services.iam;++import io.github.hectorvent.floci.testing.EnforcementFixtures;+import io.github.hectorvent.floci.testing.IamEnforcementProfile;+import io.github.hectorvent.floci.testing.PartitionCleanup;+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.common.http.TestHTTPResource;+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.api.extension.RegisterExtension;++import java.io.IOException;+import java.net.URI;+import java.net.http.HttpClient;+import java.net.http.HttpRequest;+import java.net.http.HttpResponse;+import java.nio.charset.StandardCharsets;+import java.util.UUID;++import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;+import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertTrue;++/**+ * A request no wire protocol claims, such as one carrying a {@code Smithy-Protocol} value Floci does+ * not serve it by, is still served while strict claiming is off, the default. It is checked as the+ * call its route runs: unsigned, a call to the JSON, CBOR or Query endpoint is refused, and a REST+ * request is checked as its route's action, whatever an {@code X-Amz-Target} header on it names.+ */+@QuarkusTest+@TestProfile(IamEnforcementProfile.class)+class UnclaimedRequestEnforcementIntegrationTest {++ private static final String ACCOUNT_ID = "000000000000";+ private static final byte[] EMPTY_CBOR_MAP = {(byte) 0xa0};++ @TestHTTPResource("/")+ static URI baseUri;++ @RegisterExtension+ final PartitionCleanup cleanup = new PartitionCleanup();++ @BeforeAll+ static void configureRestAssured() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ @Test+ void anUnsignedJsonCallWithAnUnrecognisedSmithyProtocolIsRefused() {+ given()+ .contentType("application/x-amz-json-1.0")+ .header("X-Amz-Target", "AmazonSQS.ListQueues")+ .header("smithy-protocol", "unrecognised")+ .body("{}")+ .when()+ .post("/")+ .then()+ .statusCode(403)+ .body("__type", equalTo("MissingAuthenticationTokenException"));+ }++ @Test+ void anUnsignedJsonCallCarryingTheRpcV2HeaderOffItsPathIsRefused() {+ // The header names a protocol Floci serves, but only on the rpcv2 path; at the root the JSON+ // endpoint serves the call.+ given()+ .contentType("application/x-amz-json-1.0")+ .header("X-Amz-Target", "AmazonSQS.ListQueues")+ .header("smithy-protocol", "rpc-v2-cbor")+ .body("{}")+ .when()+ .post("/")+ .then()+ .statusCode(403)+ .body("__type", equalTo("MissingAuthenticationTokenException"));+ }++ @Test+ void anUnsignedQueryCallWithAnUnrecognisedSmithyProtocolIsRefused() {+ given()+ .contentType("application/x-www-form-urlencoded")+ .header("smithy-protocol", "unrecognised")+ .formParam("Action", "ListQueues")+ .formParam("Version", "2012-11-05")+ .when()+ .post("/")+ .then()+ .statusCode(403)+ .body(containsString("<Code>MissingAuthenticationToken</Code>"));+ }++ @Test+ void anUnsignedCborCallWithAnUnrecognisedSmithyProtocolIsRefused() {+ byte[] body = given()+ .contentType("application/x-amz-cbor-1.1")+ .header("X-Amz-Target", "Kinesis_20131202.ListStreams")+ .header("smithy-protocol", "unrecognised")+ .body(EMPTY_CBOR_MAP)+ .when()+ .post("/")+ .then()+ .statusCode(403)+ .extract().asByteArray();++ assertTrue(new String(body, StandardCharsets.ISO_8859_1).contains("MissingAuthenticationToken"));+ }++ @Test+ void anUnsignedTargetedCallWithNoContentTypeIsRefused() throws IOException, InterruptedException {+ // No protocol claims an X-Amz-Target call without a content type, yet a route serves it. Rest+ // Assured always sends a content type with a body, so this one goes out by hand.+ HttpResponse<String> response = HttpClient.newHttpClient().send(+ HttpRequest.newBuilder(baseUri)+ .header("X-Amz-Target", "Kinesis_20131202.ListStreams")+ .POST(HttpRequest.BodyPublishers.ofByteArray(EMPTY_CBOR_MAP))+ .build(),+ HttpResponse.BodyHandlers.ofString(StandardCharsets.ISO_8859_1));++ assertEquals(403, response.statusCode());+ assertTrue(response.body().contains("MissingAuthenticationToken"), response.body());+ }++ @Test+ void aSignedJsonCallWithAnUnrecognisedSmithyProtocolIsCheckedAsItsTarget() {+ String lister = userWithPolicy(allow("sqs:ListQueues", "*"));+ String other = userWithPolicy(allow("sns:ListTopics", "*"));++ listQueues(lister).then().statusCode(200);+ listQueues(other).then()+ .statusCode(400)+ .body("__type", equalTo("AccessDeniedException"))+ .body("message", containsString("sqs:ListQueues"));+ }++ @Test+ void aRestRequestIsCheckedAsItsRouteWhateverTargetItCarries() {+ String bucket = "unclaimed-" + UUID.randomUUID().toString().substring(0, 8);+ given().header("Authorization", auth(ACCOUNT_ID, "s3")).when().put("/" + bucket)+ .then().statusCode(200);+ given().header("Authorization", auth(ACCOUNT_ID, "s3")).body("kept").when().put("/" + bucket + "/doc")+ .then().statusCode(200);+ String reader = userWithPolicy(allow("s3:GetObject", "arn:aws:s3:::" + bucket + "/*"));++ // Unclaimed, the request was checked as the s3:GetObject its target named, and the delete ran.+ given()+ .header("Authorization", auth(reader, "s3"))+ .header("X-Amz-Target", "AmazonS3.GetObject")+ .header("smithy-protocol", "unrecognised")+ .when()+ .delete("/" + bucket + "/doc")+ .then()+ .statusCode(403)+ .body(containsString("s3:DeleteObject"));++ given().header("Authorization", auth(reader, "s3")).when().get("/" + bucket + "/doc")+ .then().statusCode(200).body(equalTo("kept"));+ }++ private static Response listQueues(String accessKeyId) {+ return given()+ .header("Authorization", auth(accessKeyId, "sqs"))+ .contentType("application/x-amz-json-1.0")+ .header("X-Amz-Target", "AmazonSQS.ListQueues")+ .header("smithy-protocol", "unrecognised")+ .body("{}")+ .when()+ .post("/");+ }++ private static String allow(String action, String resource) {+ return """+ {"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"%s","Resource":"%s"}]}"""+ .formatted(action, resource);+ }++ private String userWithPolicy(String document) {+ String userName = "unclaimed-" + UUID.randomUUID().toString().substring(0, 8);+ String accessKeyId = EnforcementFixtures.createUser(cleanup, baseUri.getPort(),+ auth(ACCOUNT_ID, "iam"), userName).accessKeyId();+ EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ACCOUNT_ID, "iam"), userName, "only", document);+ return accessKeyId;+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3CorsFilterTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3CorsFilterTest.javanew file mode 100644index 0000000000..ba6939d2a6--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3CorsFilterTest.java@@ -0,0 +1,32 @@+package io.github.hectorvent.floci.services.s3;++import jakarta.enterprise.inject.IllegalProductException;+import jakarta.ws.rs.container.ContainerRequestContext;+import jakarta.ws.rs.container.ContainerResponseContext;+import jakarta.ws.rs.container.ResourceInfo;+import org.junit.jupiter.api.Test;++import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;+import static org.mockito.Mockito.verifyNoInteractions;+import static org.mockito.Mockito.when;++class S3CorsFilterTest {++ @Test+ void leavesTheResponseToARequestRoutingTurnedAwayAlone() {+ // A 415 from RESTEasy's media-type dispatch leaves a ResourceInfo whose producer CDI refuses.+ ResourceInfo noMethodChosen = mock(ResourceInfo.class);+ when(noMethodChosen.getResourceClass()).thenThrow(new IllegalProductException("no method chosen"));+ S3Service s3Service = mock(S3Service.class);+ ContainerRequestContext request = mock(ContainerRequestContext.class);+ when(request.getHeaderString("Origin")).thenReturn("https://example.com");+ ContainerResponseContext response = mock(ContainerResponseContext.class);++ new S3CorsFilter(s3Service, noMethodChosen).filter(request, response);++ verifyNoInteractions(s3Service);+ verify(response, never()).getHeaders();+ }+}