mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sqs): opt-in AWS QueueUrl form via endpoint strategy (#5517)
* feat(sqs): opt-in AWS QueueUrl form via endpoint strategy FLOCI_SERVICES_SQS_ENDPOINT_STRATEGY=standard returns queue URLs in the AWS form, https://sqs.<region>.<dnsSuffix>/<account>/<name>. The default, path, keeps the current form. Every input form resolves under either setting. * fix(sqs): endpoint strategy accepts path or standard * refactor(sqs): queue URL forms are floci and aws, with shared round-trip tests * test(sqs): strategy names in tests and config comments read floci and aws * docs(sqs): list endpoint-strategy on the application.yml page The yml block and the env var table both list the setting, and the base-url comment names the aws strategy.
This commit is contained in:
1 parent
88b0522168
commit
691499d335
12 files changed
+489
-17
No files matched your search
+78
@@ -0,0 +1,78 @@
|
||||
package io.github.hectorvent.floci.services.sqs;
|
||||
|
||||
import io.github.hectorvent.floci.testing.PartitionMatrix;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
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;
|
||||
|
||||
/** {@code floci.services.sqs.endpoint-strategy=aws}: queue URLs take the AWS form. */
|
||||
@QuarkusTest
|
||||
@TestProfile(SqsAwsEndpointStrategyIntegrationTest.AwsEndpointStrategyProfile.class)
|
||||
class SqsAwsEndpointStrategyIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final String JSON_CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryProtocolReturnsTheAwsQueueUrl() {
|
||||
SqsQueueUrlRoundTrip.query("aws-url-queue",
|
||||
"https://sqs.us-east-1.amazonaws.com/" + ACCOUNT_ID + "/aws-url-queue",
|
||||
"http://localhost:4566/" + ACCOUNT_ID + "/aws-url-queue");
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonProtocolReturnsTheAwsQueueUrlForTheSignedRegion() {
|
||||
SqsQueueUrlRoundTrip.json(PartitionMatrix.sigV4Auth("eu-west-2", "sqs"), "eu-west-2",
|
||||
"json-aws-url-queue",
|
||||
"https://sqs.eu-west-2.amazonaws.com/" + ACCOUNT_ID + "/json-aws-url-queue",
|
||||
"http://localhost:4566/" + ACCOUNT_ID + "/json-aws-url-queue");
|
||||
}
|
||||
|
||||
@Test
|
||||
void queueUrlHostCarriesThePartitionDnsSuffix() {
|
||||
String auth = PartitionMatrix.sigV4Auth("cn-north-1", "sqs");
|
||||
String queueName = "cn-aws-url-queue";
|
||||
String expectedUrl = "https://sqs.cn-north-1.amazonaws.com.cn/" + ACCOUNT_ID + "/" + queueName;
|
||||
|
||||
given()
|
||||
.contentType(JSON_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.CreateQueue")
|
||||
.header("Authorization", auth)
|
||||
.body("{\"QueueName\":\"" + queueName + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("QueueUrl", equalTo(expectedUrl));
|
||||
|
||||
given()
|
||||
.contentType(JSON_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.DeleteQueue")
|
||||
.header("Authorization", auth)
|
||||
.body("{\"QueueUrl\":\"" + expectedUrl + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
public static final class AwsEndpointStrategyProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.sqs.endpoint-strategy", "aws");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1323,4 +1323,11 @@ class SqsIntegrationTest {
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
@Test
|
||||
void queueUrlsUseTheFlociFormByDefault() {
|
||||
SqsQueueUrlRoundTrip.query("floci-url-queue",
|
||||
"http://localhost:4566/000000000000/floci-url-queue",
|
||||
"https://sqs.us-east-1.amazonaws.com/000000000000/floci-url-queue");
|
||||
}
|
||||
}
|
||||
@@ -993,4 +993,11 @@ class SqsJsonProtocolTest {
|
||||
}
|
||||
return "{\"QueueUrl\":\"" + url + "\",\"Entries\":[" + entries + "]}";
|
||||
}
|
||||
|
||||
@Test
|
||||
void queueUrlsUseTheFlociFormByDefault() {
|
||||
SqsQueueUrlRoundTrip.json(AUTH_SQS_EU_WEST_2, "eu-west-2", "json-floci-url-queue",
|
||||
"http://localhost:4566/" + ACCOUNT_ID + "/json-floci-url-queue",
|
||||
"https://sqs.eu-west-2.amazonaws.com/" + ACCOUNT_ID + "/json-floci-url-queue");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package io.github.hectorvent.floci.services.sqs;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Queue URL round trips shared by the endpoint strategy tests: every call that returns a queue URL
|
||||
* returns {@code expectedUrl}, and the queue resolves from that URL, from {@code otherForm}, from its
|
||||
* bare name and from a request posted to its path.
|
||||
*/
|
||||
final class SqsQueueUrlRoundTrip {
|
||||
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";
|
||||
private static final String JSON_CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
|
||||
private SqsQueueUrlRoundTrip() {
|
||||
}
|
||||
|
||||
/** Query protocol, unsigned, so the queue lives in us-east-1. */
|
||||
static void query(String queueName, String expectedUrl, String otherForm) {
|
||||
String queueArn = "arn:aws:sqs:us-east-1:" + ACCOUNT_ID + ":" + queueName;
|
||||
|
||||
String createdUrl = given()
|
||||
.contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "CreateQueue")
|
||||
.formParam("QueueName", queueName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
|
||||
|
||||
try {
|
||||
assertEquals(expectedUrl, createdUrl);
|
||||
|
||||
given()
|
||||
.contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "GetQueueUrl")
|
||||
.formParam("QueueName", queueName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("GetQueueUrlResponse.GetQueueUrlResult.QueueUrl", equalTo(expectedUrl));
|
||||
|
||||
given()
|
||||
.contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "ListQueues")
|
||||
.formParam("QueueNamePrefix", queueName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<QueueUrl>" + expectedUrl + "</QueueUrl>"));
|
||||
|
||||
for (String input : List.of(expectedUrl, otherForm, queueName)) {
|
||||
given()
|
||||
.contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "GetQueueAttributes")
|
||||
.formParam("QueueUrl", input)
|
||||
.formParam("AttributeName.1", "QueueArn")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString(queueArn));
|
||||
}
|
||||
|
||||
given()
|
||||
.contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "GetQueueAttributes")
|
||||
.formParam("AttributeName.1", "QueueArn")
|
||||
.when()
|
||||
.post("/" + ACCOUNT_ID + "/" + queueName)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString(queueArn));
|
||||
} finally {
|
||||
given()
|
||||
.contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "DeleteQueue")
|
||||
.formParam("QueueUrl", expectedUrl)
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
}
|
||||
|
||||
/** JSON protocol, signed with {@code auth} for {@code region}. */
|
||||
static void json(String auth, String region, String queueName, String expectedUrl, String otherForm) {
|
||||
String queueArn = "arn:aws:sqs:" + region + ":" + ACCOUNT_ID + ":" + queueName;
|
||||
|
||||
given()
|
||||
.contentType(JSON_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.CreateQueue")
|
||||
.header("Authorization", auth)
|
||||
.body("{\"QueueName\":\"" + queueName + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("QueueUrl", equalTo(expectedUrl));
|
||||
|
||||
try {
|
||||
given()
|
||||
.contentType(JSON_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.GetQueueUrl")
|
||||
.header("Authorization", auth)
|
||||
.body("{\"QueueName\":\"" + queueName + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("QueueUrl", equalTo(expectedUrl));
|
||||
|
||||
given()
|
||||
.contentType(JSON_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.ListQueues")
|
||||
.header("Authorization", auth)
|
||||
.body("{\"QueueNamePrefix\":\"" + queueName + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("QueueUrls", contains(expectedUrl));
|
||||
|
||||
for (String input : List.of(expectedUrl, otherForm, queueName)) {
|
||||
given()
|
||||
.contentType(JSON_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.GetQueueAttributes")
|
||||
.header("Authorization", auth)
|
||||
.body("{\"QueueUrl\":\"" + input + "\",\"AttributeNames\":[\"QueueArn\"]}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Attributes.QueueArn", equalTo(queueArn));
|
||||
}
|
||||
|
||||
given()
|
||||
.contentType(JSON_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.GetQueueAttributes")
|
||||
.header("Authorization", auth)
|
||||
.body("{\"QueueUrl\":\"" + expectedUrl + "\",\"AttributeNames\":[\"QueueArn\"]}")
|
||||
.when()
|
||||
.post("/" + ACCOUNT_ID + "/" + queueName)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Attributes.QueueArn", equalTo(queueArn));
|
||||
} finally {
|
||||
given()
|
||||
.contentType(JSON_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.DeleteQueue")
|
||||
.header("Authorization", auth)
|
||||
.body("{\"QueueUrl\":\"" + expectedUrl + "\"}")
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.sqs;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig.SqsServiceConfig.EndpointStrategy;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
@@ -17,6 +18,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -390,6 +392,134 @@ class SqsServiceTest {
|
||||
assertEquals(BASE_URL + "/000000000000/my-queue", url);
|
||||
}
|
||||
|
||||
private SqsService serviceWithStrategy(InMemoryStorage<String, Queue> store, RegionResolver resolver,
|
||||
EndpointStrategy strategy) {
|
||||
return new SqsService(store, null, null, null, 30, 1048576, BASE_URL, resolver, false, null, clock,
|
||||
ReceiptHandle.DEFAULT_SECRET, null, null, strategy);
|
||||
}
|
||||
|
||||
private SqsService awsService() {
|
||||
return serviceWithStrategy(new InMemoryStorage<>(), new RegionResolver("us-east-1", "000000000000"),
|
||||
EndpointStrategy.AWS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultFlociStrategyReturnsTheEmulatorUrlForm() {
|
||||
String region = "eu-west-1";
|
||||
sqsService.createQueue("path-queue", null, region);
|
||||
sqsService.createQueue("path-source", Map.of("RedrivePolicy",
|
||||
"{\"deadLetterTargetArn\":\"arn:aws:sqs:eu-west-1:000000000000:path-queue\",\"maxReceiveCount\":\"3\"}"),
|
||||
region);
|
||||
|
||||
assertEquals(BASE_URL + "/000000000000/path-queue",
|
||||
sqsService.createQueue("path-queue", null, region).getQueueUrl());
|
||||
assertEquals(BASE_URL + "/000000000000/path-queue", sqsService.getQueueUrl("path-queue", region));
|
||||
assertEquals(List.of(BASE_URL + "/000000000000/path-queue", BASE_URL + "/000000000000/path-source"),
|
||||
sqsService.listQueues(null, region).stream().map(Queue::getQueueUrl).sorted().toList());
|
||||
assertEquals(List.of(BASE_URL + "/000000000000/path-source"),
|
||||
sqsService.listDeadLetterSourceQueues(BASE_URL + "/000000000000/path-queue", region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsStrategyReturnsTheAwsUrlForm() {
|
||||
SqsService service = awsService();
|
||||
String region = "eu-west-1";
|
||||
|
||||
assertEquals("https://sqs.eu-west-1.amazonaws.com/000000000000/std-queue",
|
||||
service.createQueue("std-queue", null, region).getQueueUrl());
|
||||
assertEquals("https://sqs.eu-west-1.amazonaws.com/000000000000/std-queue",
|
||||
service.getQueueUrl("std-queue", region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsStrategyListQueuesReturnsAwsUrls() {
|
||||
SqsService service = awsService();
|
||||
String region = "us-west-2";
|
||||
service.createQueue("listed-queue", null, region);
|
||||
|
||||
List<Queue> queues = service.listQueues(null, region);
|
||||
assertEquals(1, queues.size());
|
||||
assertEquals("https://sqs.us-west-2.amazonaws.com/000000000000/listed-queue",
|
||||
queues.getFirst().getQueueUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsStrategyCarriesThePartitionDnsSuffix() {
|
||||
SqsService service = awsService();
|
||||
assertEquals("https://sqs.cn-north-1.amazonaws.com.cn/000000000000/cn-queue",
|
||||
service.createQueue("cn-queue", null, "cn-north-1").getQueueUrl());
|
||||
service.createQueue("gov-queue", null, "us-gov-west-1");
|
||||
assertEquals("https://sqs.us-gov-west-1.amazonaws.com/000000000000/gov-queue",
|
||||
service.getQueueUrl("gov-queue", "us-gov-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsStrategyCreateQueueForAnExistingQueueReturnsTheAwsUrl() {
|
||||
SqsService service = awsService();
|
||||
String region = "eu-west-1";
|
||||
service.createQueue("again", null, region);
|
||||
assertEquals("https://sqs.eu-west-1.amazonaws.com/000000000000/again",
|
||||
service.createQueue("again", null, region).getQueueUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsStrategyListDeadLetterSourceQueuesReturnsAwsUrls() {
|
||||
SqsService service = awsService();
|
||||
String region = "eu-west-1";
|
||||
service.createQueue("dlq", null, region);
|
||||
service.createQueue("source", Map.of("RedrivePolicy",
|
||||
"{\"deadLetterTargetArn\":\"arn:aws:sqs:eu-west-1:000000000000:dlq\",\"maxReceiveCount\":\"3\"}"),
|
||||
region);
|
||||
|
||||
assertEquals(List.of("https://sqs.eu-west-1.amazonaws.com/000000000000/source"),
|
||||
service.listDeadLetterSourceQueues(BASE_URL + "/000000000000/dlq", region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsStrategyUsesTheQueueOwnerAccount() {
|
||||
SqsService service = serviceWithStrategy(new InMemoryStorage<>(),
|
||||
new RegionResolver("us-east-1", "111122223333"), EndpointStrategy.AWS);
|
||||
|
||||
assertEquals("https://sqs.ap-southeast-2.amazonaws.com/111122223333/owned",
|
||||
service.createQueue("owned", null, "ap-southeast-2").getQueueUrl());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(EndpointStrategy.class)
|
||||
void everyAcceptedQueueUrlFormResolvesUnderEitherStrategy(EndpointStrategy strategy) {
|
||||
SqsService service = serviceWithStrategy(new InMemoryStorage<>(),
|
||||
new RegionResolver("us-east-1", "000000000000"), strategy);
|
||||
String region = "eu-west-1";
|
||||
String url = service.createQueue("round-trip", null, region).getQueueUrl();
|
||||
|
||||
for (String input : List.of(url, BASE_URL + "/000000000000/round-trip",
|
||||
"https://sqs.eu-west-1.amazonaws.com/000000000000/round-trip",
|
||||
"http://localhost.localstack.cloud:4566/000000000000/round-trip", "round-trip")) {
|
||||
assertEquals("arn:aws:sqs:eu-west-1:000000000000:round-trip",
|
||||
service.getQueueAttributes(input, List.of("QueueArn"), region).get("QueueArn"), input);
|
||||
}
|
||||
service.sendMessage(url, "hello", 0, region);
|
||||
assertEquals(1, service.receiveMessage(
|
||||
"https://sqs.eu-west-1.amazonaws.com/000000000000/round-trip", 1, 30, 0, region).size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"FLOCI, http://localhost:4566/000000000000/legacy-queue",
|
||||
"AWS, https://sqs.eu-west-1.amazonaws.com/000000000000/legacy-queue"
|
||||
})
|
||||
void storedQueuesAreReportedInTheCurrentForm(EndpointStrategy strategy, String expectedUrl) {
|
||||
String region = "eu-west-1";
|
||||
InMemoryStorage<String, Queue> store = new InMemoryStorage<>();
|
||||
Queue legacy = new Queue("legacy-queue", "http://old-host:4566/000000000000/legacy-queue");
|
||||
legacy.setAccountId("000000000000");
|
||||
store.put(region + "::/000000000000/legacy-queue", legacy);
|
||||
SqsService service = serviceWithStrategy(store, new RegionResolver("us-east-1", "000000000000"), strategy);
|
||||
|
||||
assertEquals(expectedUrl, service.listQueues(null, region).getFirst().getQueueUrl());
|
||||
assertEquals(expectedUrl, service.getQueueUrl("legacy-queue", region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getQueueUrlNotFound() {
|
||||
assertThrows(AwsException.class, () ->
|
||||
|
||||
Reference in new issue
Block a user