mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(sqs): route requests posted to queue URL paths to SQS as AWS does (#7525)
SqsQueueUrlRouterFilter only matched /{account}/{queue}, so SQS requests posted to the queue path with a trailing slash or to a /queue/{region}/ prefixed path fell through to the S3 object handler and failed with InvalidArgument. Like AWS, route JSON requests on either form, since they name the queue in QueueUrl, and route Query requests only on /{account}/{queue}, with or without a trailing slash. Never reroute S3 object POSTs, which always carry uploads, uploadId, restore or select.
Closes #7521
This commit is contained in:
1 parent
f7cf6fef6e
commit
8fa33c710b
2 files changed
+182
-8
No files matched your search
@@ -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.
|
||||
* <p>
|
||||
* 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;
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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<String> 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<String, String> 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
|
||||
|
||||
+149
@@ -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("<UploadId>"));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user