diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SqsQueueUrlRouterFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/SqsQueueUrlRouterFilter.java
index 82800e511d..d209940e6e 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/SqsQueueUrlRouterFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/SqsQueueUrlRouterFilter.java
@@ -4,6 +4,7 @@ import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.PreMatching;
import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.Provider;
import java.io.ByteArrayInputStream;
@@ -12,11 +13,14 @@ import java.io.SequenceInputStream;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
- * Pre-matching filter that rewrites SQS requests sent to the queue URL path
- * (/{accountId}/{queueName}) to POST / so they are handled by the correct controller.
+ * Pre-matching filter that rewrites SQS requests sent to a queue URL path to POST / so they are handled
+ * by the correct controller. The path is /{accountId}/{queueName}, optionally behind a /queue/{region}/
+ * prefix, with an optional trailing slash.
*
* Newer AWS SDKs (e.g. aws-sdk-sqs Ruby gem >= 1.71) route operations to the queue URL
* rather than POST /. Without this filter, those requests match S3Controller's
@@ -24,7 +28,10 @@ import java.util.regex.Pattern;
*
* For the query (form-encoded) protocol, AWS SDK v1 omits QueueUrl from the body and
* uses the queue URL as the HTTP path instead. This filter appends it back into the
- * entity stream so SqsQueryHandler can look up the queue normally.
+ * entity stream so SqsQueryHandler can look up the queue normally. Like AWS, a query request
+ * is only rewritten on the unprefixed path, because the path names the queue.
+ *
+ * S3 object POSTs, which always carry uploads, uploadId, restore or select, are never rewritten.
*/
@Provider
@PreMatching
@@ -37,7 +44,14 @@ public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
public static final String APPENDED_BODY_BYTES_PROPERTY =
SqsQueueUrlRouterFilter.class.getName() + ".appendedBodyBytes";
- private static final Pattern QUEUE_PATH = Pattern.compile("^/(\\d+)/([^/]+)$");
+ // The queue URL path (/{account}/{queue}), optionally behind a /queue/{region}/ prefix, with an optional
+ // trailing slash, as the AWS SDK v2 may append one. Like AWS, a JSON request is routed on either form because
+ // it names the queue in its QueueUrl body field, while a Query request is named by its path and is routed
+ // only without the prefix.
+ private static final Pattern QUEUE_PATH = Pattern.compile("^/(queue/[a-z0-9-]+/)?(\\d+)/([^/]+)/?$");
+
+ // S3 POSTs to /{bucket}/{key} always carry one of these parameters, and SQS never sends them.
+ private static final List S3_OBJECT_POST_PARAMS = List.of("uploads", "uploadId", "restore", "select");
@Override
public void filter(ContainerRequestContext ctx) {
@@ -47,9 +61,18 @@ public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
}
String path = ctx.getUriInfo().getPath();
- if (!QUEUE_PATH.matcher(path).matches()) {
+ Matcher matcher = QUEUE_PATH.matcher(path);
+ if (!matcher.matches()) {
return;
}
+ boolean hasRegionPrefix = matcher.group(1) != null;
+
+ MultivaluedMap queryParams = ctx.getUriInfo().getQueryParameters();
+ for (String param : S3_OBJECT_POST_PARAMS) {
+ if (queryParams.containsKey(param)) {
+ return;
+ }
+ }
MediaType mt = ctx.getMediaType();
if (mt == null) {
@@ -60,9 +83,10 @@ public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
&& "x-amz-json-1.0".equals(mt.getSubtype())
&& isSqsTarget(ctx.getHeaderString("X-Amz-Target"));
- // S3 never receives form-encoded POSTs to /{bucket}/{key} paths —
+ // A form-encoded S3 POST to /{bucket}/{key} carries one of S3_OBJECT_POST_PARAMS, handled above.
// S3 presigned POST always goes to /{bucket}, not /{bucket}/{key}.
- boolean isSqsQuery = "application".equals(mt.getType())
+ boolean isSqsQuery = !hasRegionPrefix
+ && "application".equals(mt.getType())
&& "x-www-form-urlencoded".equals(mt.getSubtype());
if (!isSqsJson && !isSqsQuery) {
@@ -71,7 +95,8 @@ public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
// Reconstruct the queue URL from the original path.
URI reqUri = ctx.getUriInfo().getRequestUri();
- String queueUrl = reqUri.getScheme() + "://" + reqUri.getAuthority() + path;
+ String queuePath = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
+ String queueUrl = reqUri.getScheme() + "://" + reqUri.getAuthority() + queuePath;
if (isSqsQuery) {
// AWS SDK v1 omits QueueUrl from the form body and uses the queue URL as the
diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsQueueUrlPathRoutingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsQueueUrlPathRoutingIntegrationTest.java
new file mode 100644
index 0000000000..9e397a039e
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsQueueUrlPathRoutingIntegrationTest.java
@@ -0,0 +1,149 @@
+package io.github.hectorvent.floci.services.sqs;
+
+import io.github.hectorvent.floci.testing.AwsTestClients;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
+import io.restassured.response.Response;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.sqs.SqsClient;
+import software.amazon.awssdk.services.sqs.model.QueueAttributeName;
+
+import java.net.URI;
+
+import static io.restassured.RestAssured.given;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.not;
+
+/**
+ * SDKs that use the queue URL as the request endpoint post SQS calls to the queue path instead of "/". Routing
+ * follows AWS: a JSON request names the queue in its QueueUrl body field, so it reaches SQS on the queue path, on
+ * a /queue/{region}/ prefixed path and on either with a trailing slash. A Query request without a QueueUrl is
+ * named by its path, so it reaches SQS only on the queue path, with or without a trailing slash.
+ */
+@QuarkusTest
+class SqsQueueUrlPathRoutingIntegrationTest {
+
+ private static final String JSON_CONTENT_TYPE = "application/x-amz-json-1.0";
+ private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";
+ private static final String QUEUE_NAME = "path-routing-queue";
+ private static final String QUEUE_ARN = "arn:aws:sqs:us-east-1:000000000000:" + QUEUE_NAME;
+
+ @BeforeAll
+ static void configureRestAssured() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @BeforeEach
+ void createQueue() {
+ given()
+ .contentType(FORM_CONTENT_TYPE)
+ .formParam("Action", "CreateQueue")
+ .formParam("QueueName", QUEUE_NAME)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "/000000000000/" + QUEUE_NAME,
+ "/000000000000/" + QUEUE_NAME + "/",
+ "/queue/us-east-1/000000000000/" + QUEUE_NAME,
+ "/queue/us-east-1/000000000000/" + QUEUE_NAME + "/"
+ })
+ void jsonRequestPostedToQueuePathReachesSqs(String path) {
+ // The SDK sends the queue URL without the trailing slash in the body, only the HTTP path carries it.
+ String queueUrlPath = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
+ given()
+ .contentType(JSON_CONTENT_TYPE)
+ .header("X-Amz-Target", "AmazonSQS.GetQueueAttributes")
+ .body("{\"QueueUrl\":\"http://localhost:4566" + queueUrlPath + "\",\"AttributeNames\":[\"QueueArn\"]}")
+ .when()
+ .post(path)
+ .then()
+ .statusCode(200)
+ .body("Attributes.QueueArn", equalTo(QUEUE_ARN));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "/000000000000/" + QUEUE_NAME,
+ "/000000000000/" + QUEUE_NAME + "/"
+ })
+ void queryRequestPostedToQueuePathWithoutQueueUrlReachesSqs(String path) {
+ postQueryWithoutQueueUrl(path)
+ .then()
+ .statusCode(200)
+ .body(containsString(QUEUE_ARN));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "/queue/us-east-1/000000000000/" + QUEUE_NAME,
+ "/queue/us-east-1/000000000000/" + QUEUE_NAME + "/"
+ })
+ void queryRequestPostedToPrefixedQueuePathWithoutQueueUrlIsNotRoutedToSqs(String path) {
+ // AWS rejects these with 404 InvalidAddress.
+ postQueryWithoutQueueUrl(path)
+ .then()
+ .statusCode(not(equalTo(200)))
+ .body(not(containsString(QUEUE_ARN)));
+ }
+
+ @ParameterizedTest
+ @CsvSource({
+ "queue, us-east-1/000000000000/form",
+ "000000000001, form",
+ "000000000002, dir/"
+ })
+ void formEncodedS3UploadOnQueueLikePathStaysWithS3(String bucket, String key) {
+ // The Content-Type of CreateMultipartUpload describes the object, so it can be form-encoded.
+ given()
+ .when()
+ .put("/" + bucket)
+ .then()
+ .statusCode(200);
+
+ given()
+ .contentType(FORM_CONTENT_TYPE)
+ .queryParam("uploads", "")
+ .when()
+ .post("/" + bucket + "/" + key)
+ .then()
+ .statusCode(200)
+ .body(containsString(""));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "/000000000000/" + QUEUE_NAME + "/",
+ "/queue/us-east-1/000000000000/" + QUEUE_NAME + "/"
+ })
+ void sdkRequestOnQueueUrlEndpointWithTrailingSlashReachesSqs(String path) {
+ String queueUrl = "http://localhost:" + RestAssured.port + path.substring(0, path.length() - 1);
+ URI endpoint = URI.create(queueUrl + "/");
+ try (SqsClient sqs = AwsTestClients.create(SqsClient.builder(), "000000000000", Region.US_EAST_1, endpoint)) {
+ assertThat(sqs.getQueueAttributes(r -> r.queueUrl(queueUrl)
+ .attributeNames(QueueAttributeName.QUEUE_ARN)).attributes())
+ .containsEntry(QueueAttributeName.QUEUE_ARN, QUEUE_ARN);
+ }
+ }
+
+ private static Response postQueryWithoutQueueUrl(String path) {
+ return given()
+ .contentType(FORM_CONTENT_TYPE)
+ .formParam("Action", "GetQueueAttributes")
+ .formParam("AttributeName.1", "QueueArn")
+ .when()
+ .post(path);
+ }
+}