Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3,sns): preserve root service-host routing (#1625)
TiM2a3652209485
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.javaindex 4ebd776474..f39c358e6d 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java@@ -2,11 +2,13 @@ package io.github.hectorvent.floci.services.s3;import static io.github.hectorvent.floci.services.s3.S3RequestParser.hasQueryParam;+import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.AwsNamespaces;import io.github.hectorvent.floci.core.common.XmlBuilder;import io.github.hectorvent.floci.core.common.XmlParser;import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.services.sns.SnsQueryHandler;import io.github.hectorvent.floci.services.s3.model.Bucket;import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesParts;import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesResult;@@ -29,6 +31,7 @@ import jakarta.ws.rs.*;import jakarta.ws.rs.core.Context;import jakarta.ws.rs.core.HttpHeaders;import jakarta.ws.rs.core.MediaType;+import jakarta.ws.rs.core.MultivaluedMap;import jakarta.ws.rs.core.Response;import jakarta.ws.rs.core.StreamingOutput;import jakarta.ws.rs.core.UriInfo;@@ -86,17 +89,20 @@ public class S3Controller {private final S3Service s3Service;private final S3SelectService s3SelectService;private final RegionResolver regionResolver;+ private final SnsQueryHandler snsQueryHandler;private final io.quarkus.vertx.http.runtime.CurrentVertxRequest currentVertxRequest;private final io.github.hectorvent.floci.services.floci.ui.UiPages uiPages;@Injectpublic S3Controller(S3Service s3Service, S3SelectService s3SelectService,RegionResolver regionResolver,+ SnsQueryHandler snsQueryHandler,io.quarkus.vertx.http.runtime.CurrentVertxRequest currentVertxRequest,io.github.hectorvent.floci.services.floci.ui.UiPages uiPages) {this.s3Service = s3Service;this.s3SelectService = s3SelectService;this.regionResolver = regionResolver;+ this.snsQueryHandler = snsQueryHandler;this.currentVertxRequest = currentVertxRequest;this.uiPages = uiPages;}@@ -106,10 +112,26 @@ public class S3Controller {@GET@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_HTML})public Response listBuckets(@HeaderParam("X-Amz-Target") String target,- @HeaderParam("Accept") String accept) {+ @HeaderParam("Accept") String accept,+ @Context HttpHeaders httpHeaders,+ @Context UriInfo uriInfo) {if (target != null) {return null;}+ MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();+ String action = queryParams.getFirst("Action");+ // SNS emits SubscribeURL/UnsubscribeURL as plain GET links against the+ // service root, which collides with the S3 ListBuckets endpoint. Delegate+ // those confirmation/unsubscribe GETs to SNS before falling through to S3.+ // The links are unsigned (no Authorization header), so the region must come+ // from the region-bearing ARN they carry, not the request headers.+ if ("ConfirmSubscription".equals(action) || "Unsubscribe".equals(action)) {+ String snsArn = "Unsubscribe".equals(action)+ ? queryParams.getFirst("SubscriptionArn")+ : queryParams.getFirst("TopicArn");+ String region = AwsArnUtils.regionOrDefault(snsArn, regionResolver.resolveRegion(httpHeaders));+ return snsQueryHandler.handle(action, queryParams, region);+ }// A browser hitting the root endpoint (Accept: text/html) gets the Floci// landing page; SDK/CLI callers (no Accept, */*, or an XML/JSON Accept) fall// through to the normal S3 ListBuckets behavior untouched.diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.javaindex a55577b778..c91f94e685 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java@@ -115,6 +115,10 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {String firstLabel = hostname.substring(0, firstDot);String remainder = hostname.substring(firstDot + 1);+ if (isS3ServiceEndpointHost(firstLabel, remainder, baseHostname)) {+ return null;+ }+// Primary: remainder must match the configured base hostname,// either directly or in the AWS region-qualified s3.<region>.<host> form.if (baseHostname != null && matchesEndpointHost(remainder, baseHostname)) {@@ -129,6 +133,19 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {return null;}+ private static boolean isS3ServiceEndpointHost(String firstLabel, String remainder, String baseHostname) {+ if (!"s3".equalsIgnoreCase(firstLabel)) {+ return false;+ }+ if (baseHostname != null && matchesEndpointHost(remainder, baseHostname)) {+ return true;+ }+ String lowerRemainder = remainder.toLowerCase();+ return "localhost".equals(lowerRemainder)+ || "localhost.localstack.cloud".equals(lowerRemainder)+ || "localhost.floci.io".equals(lowerRemainder);+ }+/*** Matches a hostname directly or its region-qualified s3.<region>.<hostname> variant.* Example: with hostname="localhost", both "localhost" and "s3.us-east-1.localhost" match.diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.javaindex 4449e26a1b..c33ef4adee 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java@@ -65,6 +65,13 @@ class S3VirtualHostFilterTest {"localhost:4566, localhost","localhost, localhost","plain-host, plain-host",+ // Bare S3 service hosts must NOT be treated as a bucket named s3+ "s3.localhost:4566, localhost",+ "s3.localhost, localhost",+ "s3.localhost.localstack.cloud, localhost",+ "s3.localhost.localstack.cloud:4566, localhost",+ "s3.localhost.floci.io, localhost",+ "s3.localhost.floci.io:4566, localhost",// K8s service hostname used as endpoint (path-style) — must NOT be rewritten"floci.default.svc.cluster.local, localhost","floci-service.namespace.svc.cluster.local, localhost",diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIntegrationTest.javaindex fc04f1dc92..ad25b8d522 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIntegrationTest.java@@ -247,6 +247,32 @@ class S3VirtualHostIntegrationTest {.body(equalTo("region-qualified content"));}+ @Test+ @Order(17)+ void listBucketsViaLocalStackS3ServiceHost() {+ given()+ .header("Host", "s3.localhost.localstack.cloud")+ .when()+ .get("/")+ .then()+ .statusCode(200)+ .body(containsString("ListAllMyBucketsResult"))+ .body(containsString(BUCKET));+ }++ @Test+ @Order(18)+ void listBucketsViaFlociS3ServiceHost() {+ given()+ .header("Host", "s3.localhost.floci.io")+ .when()+ .get("/")+ .then()+ .statusCode(200)+ .body(containsString("ListAllMyBucketsResult"))+ .body(containsString(BUCKET));+ }+@Test@Order(20)void cleanupAndDeleteBucket() {diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsHttpDeliveryIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsHttpDeliveryIntegrationTest.javaindex 0996b1d09c..6ee3649f7c 100644--- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsHttpDeliveryIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsHttpDeliveryIntegrationTest.java@@ -18,6 +18,7 @@ import java.util.Map;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.not;import static org.junit.jupiter.api.Assertions.*;/**@@ -31,6 +32,8 @@ class SnsHttpDeliveryIntegrationTest {private static int httpPort;private static final List<ReceivedRequest> receivedRequests = new CopyOnWriteArrayList<>();private static final ObjectMapper objectMapper = new ObjectMapper();+ private static final java.util.logging.Logger LOG =+ java.util.logging.Logger.getLogger(SnsHttpDeliveryIntegrationTest.class.getName());record ReceivedRequest(String body, Map<String, List<String>> headers) {}@BeforeAll@@ -45,6 +48,32 @@ class SnsHttpDeliveryIntegrationTest {exchange.sendResponseHeaders(200, 0);exchange.close();});+ httpServer.createContext("/confirming-webhook", exchange -> {+ byte[] bodyBytes = exchange.getRequestBody().readAllBytes();+ String body = new String(bodyBytes, StandardCharsets.UTF_8);+ Map<String, List<String>> headers = exchange.getRequestHeaders();+ ReceivedRequest received = new ReceivedRequest(body, headers);+ try {+ // Confirm the subscription BEFORE recording the request, so a test that+ // awaits this request observes it only once the subscription is actually+ // confirmed. Recording first would let a subsequent publish race ahead of+ // the confirmation and see a still-pending subscription (flaky delivery).+ if ("SubscriptionConfirmation".equals(firstHeader(headers, "X-amz-sns-message-type"))) {+ confirmSubscription(received);+ }+ receivedRequests.add(received);+ exchange.sendResponseHeaders(200, 0);+ } catch (Exception e) {+ // Record even on failure so awaitRequests unblocks and the assertion+ // reports the real state instead of timing out opaquely.+ receivedRequests.add(received);+ LOG.log(java.util.logging.Level.SEVERE,+ "confirming-webhook: subscription confirmation failed", e);+ exchange.sendResponseHeaders(500, 0);+ } finally {+ exchange.close();+ }+ });httpServer.start();}@@ -65,6 +94,11 @@ class SnsHttpDeliveryIntegrationTest {}}+ private static String firstHeader(Map<String, List<String>> headers, String name) {+ List<String> values = headers.get(name);+ return values == null || values.isEmpty() ? null : values.get(0);+ }+/*** Extracts the Token and TopicArn from the SubscriptionConfirmation message* and calls ConfirmSubscription via POST (matching how the SDK confirms).@@ -178,6 +212,194 @@ class SnsHttpDeliveryIntegrationTest {assertEquals("production", msgAttrs.get("env").get("Value").asText());}+ @Test+ void publish_toHttpSubscriber_confirmedDuringSuccessfulHandshake_deliversNotification() throws Exception {+ receivedRequests.clear();+ String endpoint = "http://localhost:" + httpPort + "/confirming-webhook";++ String topicArn = given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateTopic")+ .formParam("Name", "http-inline-confirm-test")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().xmlPath().getString("CreateTopicResponse.CreateTopicResult.TopicArn");++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "Subscribe")+ .formParam("TopicArn", topicArn)+ .formParam("Protocol", "http")+ .formParam("Endpoint", endpoint)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<SubscriptionArn>"));++ awaitRequests(1);+ assertEquals(1, receivedRequests.size());+ assertEquals("SubscriptionConfirmation", firstHeader(receivedRequests.get(0).headers(), "X-amz-sns-message-type"));+ receivedRequests.clear();++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "Publish")+ .formParam("TopicArn", topicArn)+ .formParam("Message", "delivered after inline confirm")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<MessageId>"));++ awaitRequests(1);+ assertEquals(1, receivedRequests.size());+ ReceivedRequest req = receivedRequests.get(0);+ assertEquals("Notification", firstHeader(req.headers(), "X-amz-sns-message-type"));+ JsonNode envelope = objectMapper.readTree(req.body());+ assertEquals("Notification", envelope.get("Type").asText());+ assertEquals("delivered after inline confirm", envelope.get("Message").asText());+ }++ @Test+ void subscribeUrl_getConfirmsSubscriptionAndAllowsDelivery() throws Exception {+ receivedRequests.clear();+ String endpoint = "http://localhost:" + httpPort + "/webhook";++ String topicArn = given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateTopic")+ .formParam("Name", "http-subscribe-url-get-test")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().xmlPath().getString("CreateTopicResponse.CreateTopicResult.TopicArn");++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "Subscribe")+ .formParam("TopicArn", topicArn)+ .formParam("Protocol", "http")+ .formParam("Endpoint", endpoint)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<SubscriptionArn>"));++ awaitRequests(1);+ assertEquals(1, receivedRequests.size());+ JsonNode confirmation = objectMapper.readTree(receivedRequests.get(0).body());++ given()+ .queryParam("Action", "ConfirmSubscription")+ .queryParam("TopicArn", confirmation.get("TopicArn").asText())+ .queryParam("Token", confirmation.get("Token").asText())+ .when()+ .get("/")+ .then()+ .statusCode(200)+ .body(containsString("<SubscriptionArn>"));++ receivedRequests.clear();++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "Publish")+ .formParam("TopicArn", topicArn)+ .formParam("Message", "delivered after SubscribeURL GET")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<MessageId>"));++ awaitRequests(1);+ assertEquals(1, receivedRequests.size());+ assertEquals("Notification", firstHeader(receivedRequests.get(0).headers(), "X-amz-sns-message-type"));+ }++ @Test+ void unsubscribeUrl_getRoutesToSnsAndRemovesSubscription() throws Exception {+ receivedRequests.clear();+ String endpoint = "http://localhost:" + httpPort + "/webhook";+ // Pin the subscription to a non-default region. The SubscribeURL/UnsubscribeURL+ // links SNS emits are unsigned, so the delegation must recover this region from+ // the ARN in the link, not from the (absent) request auth.+ String snsAuth = "AWS4-HMAC-SHA256 Credential=000000000000/20260101/us-west-2/sns/aws4_request";++ String topicArn = given()+ .header("Authorization", snsAuth)+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateTopic")+ .formParam("Name", "http-unsubscribe-url-get-test")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().xmlPath().getString("CreateTopicResponse.CreateTopicResult.TopicArn");+ assertTrue(topicArn.contains(":us-west-2:"), "topic should live in us-west-2");++ given()+ .header("Authorization", snsAuth)+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "Subscribe")+ .formParam("TopicArn", topicArn)+ .formParam("Protocol", "http")+ .formParam("Endpoint", endpoint)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<SubscriptionArn>"));++ awaitRequests(1);+ assertEquals(1, receivedRequests.size());+ JsonNode confirmation = objectMapper.readTree(receivedRequests.get(0).body());++ // Confirm via the SubscribeURL GET (unsigned) to obtain a real SubscriptionArn.+ // Region is recovered from the TopicArn, so the pending sub in us-west-2 is found.+ String subscriptionArn = given()+ .queryParam("Action", "ConfirmSubscription")+ .queryParam("TopicArn", confirmation.get("TopicArn").asText())+ .queryParam("Token", confirmation.get("Token").asText())+ .when()+ .get("/")+ .then()+ .statusCode(200)+ .body(containsString("<SubscriptionArn>"))+ .extract().xmlPath().getString("ConfirmSubscriptionResponse.ConfirmSubscriptionResult.SubscriptionArn");++ // Following the UnsubscribeURL is a plain GET on the service root. It must+ // reach SNS (UnsubscribeResponse), not fall through to S3 ListBuckets, and+ // its region must come from the SubscriptionArn (us-west-2), not the default.+ given()+ .queryParam("Action", "Unsubscribe")+ .queryParam("SubscriptionArn", subscriptionArn)+ .when()+ .get("/")+ .then()+ .statusCode(200)+ .body(containsString("UnsubscribeResponse"))+ .body(not(containsString("ListAllMyBucketsResult")));++ // The unsubscribe took effect: the subscription is no longer listed.+ given()+ .header("Authorization", snsAuth)+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ListSubscriptionsByTopic")+ .formParam("TopicArn", topicArn)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(not(containsString(subscriptionArn)));+ }+@Testvoid publish_toHttpSubscriber_rawMessageDelivery() throws Exception {receivedRequests.clear();