Light, fluffy, and always free - The AWS Local Emulator alternative
feat(sqs): opt-in AWS QueueUrl form via endpoint strategy (#5517)
lex00691499d3354c
diff --git a/docs/configuration/advanced/application-yml.md b/docs/configuration/advanced/application-yml.mdindex df8952e0d3..e34fe0afc6 100644--- a/docs/configuration/advanced/application-yml.md+++ b/docs/configuration/advanced/application-yml.md@@ -36,7 +36,7 @@ The block below mirrors `src/main/resources/application.yml`, it's the effective```yamlfloci:- base-url: "http://localhost:4566" # Used to build response URLs (SQS QueueUrl, SNS endpoints, etc.)+ base-url: "http://localhost:4566" # Used to build response URLs (SQS QueueUrl unless sqs.endpoint-strategy is aws, SNS endpoints, etc.)# hostname: "" # When set, overrides the host in base-url for multi-container Dockerdefault-region: us-east-1 # Also selects the partition (cn-north-1 -> aws-cn)default-account-id: "000000000000"@@ -136,6 +136,7 @@ floci:max-message-size: 1048576 # Bytes (1 MiB, the AWS maximum)clear-fifo-deduplication-cache-on-purge: false # When true, PurgeQueue clears SQS FIFO dedup and SNS FIFO topic dedup for topics subscribed to that queuereceipt-handle-secret: local-emulator-secret # HMAC secret that signs receipt handles+ endpoint-strategy: floci # QueueUrl shape: floci is <base-url>/<account>/<name>, aws is https://sqs.<region>.<dns-suffix>/<account>/<name>s3:enabled: true@@ -347,6 +348,7 @@ All keys in this table are declared on `EmulatorConfig` and accept environment v| `FLOCI_SERVICES_SQS_MAX_MESSAGE_SIZE` | `1048576` | Max message size (bytes) || `FLOCI_SERVICES_SQS_CLEAR_FIFO_DEDUPLICATION_CACHE_ON_PURGE` | `false` | When `true`, `PurgeQueue` clears the FIFO 5-minute deduplication cache for the target queue and matching SNS FIFO topic dedup entries || `FLOCI_SERVICES_SQS_RECEIPT_HANDLE_SECRET` | `local-emulator-secret` | HMAC secret that signs receipt handles, so a handle that was edited or built by hand is rejected |+| `FLOCI_SERVICES_SQS_ENDPOINT_STRATEGY` | `floci` | Shape of the returned `QueueUrl`. `floci` gives `<FLOCI_BASE_URL>/<account>/<name>` and `aws` gives `https://sqs.<region>.<dns-suffix>/<account>/<name>`. Both forms are accepted as input || `FLOCI_SERVICES_S3_DEFAULT_PRESIGN_EXPIRY_SECONDS` | `3600` | Pre-signed URL expiry || `FLOCI_SERVICES_DOCKER_NETWORK` | *(unset)* | Shared Docker network for Lambda, RDS, ElastiCache containers || `FLOCI_SERVICES_RDS_DATA_ENABLED` | `true` | Enable the RDS Data API service |diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.mdindex 2c6d6a23c2..6621184e94 100644--- a/docs/configuration/environment-variables.md+++ b/docs/configuration/environment-variables.md@@ -8,7 +8,7 @@ Floci is configured exclusively through environment variables. Every option belo| Variable | Default | Description ||---|---|---|-| `FLOCI_BASE_URL` | `http://localhost:4566` | Base URL embedded in response fields (SQS `QueueUrl`, pre-signed URLs, etc.) |+| `FLOCI_BASE_URL` | `http://localhost:4566` | Base URL embedded in response fields (SQS `QueueUrl` unless `FLOCI_SERVICES_SQS_ENDPOINT_STRATEGY=aws`, pre-signed URLs, etc.) || `FLOCI_HOSTNAME` | _(none)_ | Overrides only the hostname part of `FLOCI_BASE_URL`. Set to the Compose service name (e.g. `floci`) so other containers can reach Floci by DNS || `FLOCI_DEFAULT_REGION` | `us-east-1` | AWS region used in ARNs and API responses when a request names none. Also selects the deployment's partition (`cn-north-1` means `aws-cn`), see [AWS Partitions](./partitions.md) || `FLOCI_PARTITIONS_ID` | _(derived)_ | Pins the partition (`aws`, `aws-cn`, `aws-us-gov`, `aws-iso`, `aws-iso-b`, `aws-iso-e`, `aws-iso-f`, `aws-eusc`). Startup refuses a value that contradicts `FLOCI_DEFAULT_REGION` |@@ -208,6 +208,7 @@ See [Initialization Hooks](./initialization-hooks.md) for lifecycle phases and s| `FLOCI_SERVICES_SQS_MAX_MESSAGE_SIZE` | `1048576` | Maximum message body size in bytes (1 MiB) || `FLOCI_SERVICES_SQS_CLEAR_FIFO_DEDUPLICATION_CACHE_ON_PURGE` | `false` | Reset the deduplication cache when a FIFO queue is purged || `FLOCI_SERVICES_SQS_RECEIPT_HANDLE_SECRET` | `local-emulator-secret` | HMAC secret that signs receipt handles |+| `FLOCI_SERVICES_SQS_ENDPOINT_STRATEGY` | `floci` | Shape of the returned `QueueUrl`. `floci` gives `<FLOCI_BASE_URL>/<account>/<name>` and `aws` gives the AWS form `https://sqs.<region>.<dns-suffix>/<account>/<name>`. Both forms are accepted as input |### SNSdiff --git a/docs/services/sqs.md b/docs/services/sqs.mdindex 32086e1818..0a3aeed630 100644--- a/docs/services/sqs.md+++ b/docs/services/sqs.md@@ -115,6 +115,7 @@ A message older than the queue's `MessageRetentionPeriod` is no longer returned| `FLOCI_SERVICES_SQS_MAX_MESSAGE_SIZE` | `1048576` | Maximum message size in bytes (1 MiB) || `FLOCI_SERVICES_SQS_CLEAR_FIFO_DEDUPLICATION_CACHE_ON_PURGE` | `false` | When `true`, `PurgeQueue` also clears the FIFO deduplication cache for the queue and any SNS FIFO topics subscribed to it || `FLOCI_SERVICES_SQS_RECEIPT_HANDLE_SECRET` | `local-emulator-secret` | HMAC secret that signs receipt handles, so a handle that was edited or built by hand is rejected |+| `FLOCI_SERVICES_SQS_ENDPOINT_STRATEGY` | `floci` | Shape of the returned `QueueUrl`, see [Queue URL Format](#queue-url-format) |## Examples@@ -167,4 +168,16 @@ aws sqs set-queue-attributes \```http://localhost:4566/000000000000/<queue-name>-```\ No newline at end of file+```++`CreateQueue`, `GetQueueUrl`, `ListQueues` and `ListDeadLetterSourceQueues` return the queue URL under Floci's own endpoint by default, `<FLOCI_BASE_URL>/<account>/<queue-name>`.++Set `FLOCI_SERVICES_SQS_ENDPOINT_STRATEGY=aws` to get the form AWS returns instead.++```+https://sqs.<region>.amazonaws.com/000000000000/<queue-name>+```++The region is the one the request was signed for, and the host carries that region's partition DNS suffix, so a queue in `cn-north-1` is reported as `https://sqs.cn-north-1.amazonaws.com.cn/...`. Use it for tooling that parses or compares queue URLs against the AWS form. Most SDKs send requests to the endpoint you configured and take only the path from the URL. Clients that send requests to the host in the returned URL, such as the AWS SDK for Java 1.x, need the default `floci` setting or DNS that points the `sqs.<region>` host at Floci.++Under either setting every form is accepted as input. That covers the AWS URL, the Floci URL, any other host with the same `/<account>/<queue-name>` path, and the bare queue name. Existing queues, persisted ones included, are reported in the form the current setting and base URL give.diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex 00cbae5bef..e107c35a9c 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -1349,6 +1349,25 @@ public interface EmulatorConfig {@WithDefault("local-emulator-secret")String receiptHandleSecret();++ /**+ * Shape of the QueueUrl returned by CreateQueue, GetQueueUrl, ListQueues and+ * ListDeadLetterSourceQueues.+ * <ul>+ * <li>{@code floci}: floci's own endpoint form, {@code <base-url>/<account>/<name>}.</li>+ * <li>{@code aws}: the AWS form, {@code https://sqs.<region>.<dnsSuffix>/<account>/<name>},+ * with the DNS suffix of the region's partition.</li>+ * </ul>+ * Either form is accepted on input under either setting, since queue lookups key off the+ * URL path and ignore the host.+ */+ @WithDefault("floci")+ EndpointStrategy endpointStrategy();++ enum EndpointStrategy {+ FLOCI,+ AWS+ }}interface S3ServiceConfig {diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.javaindex 36f8368ce4..34b5f22ecb 100644--- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java@@ -6,7 +6,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.config.EmulatorConfig.SqsServiceConfig.EndpointStrategy;import io.github.hectorvent.floci.core.common.AwsArnUtils;+import io.github.hectorvent.floci.core.common.AwsEndpoints;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.AwsRegions;import io.github.hectorvent.floci.core.common.RegionResolver;@@ -271,6 +273,7 @@ public class SqsService implements Resettable, ResourceProvider {* the AWS limit widens this bound so the configured default stays settable explicitly. */private final int maxAllowedMessageSize;private final String baseUrl;+ private final EndpointStrategy endpointStrategy;private final RegionResolver regionResolver;private final boolean clearFifoDeduplicationCacheOnPurge;private final SnsService snsService;@@ -316,7 +319,8 @@ public class SqsService implements Resettable, ResourceProvider {clock,config.services().sqs().receiptHandleSecret(),requestContextInstance,- iamService+ iamService,+ config.services().sqs().endpointStrategy());}@@ -398,6 +402,19 @@ public class SqsService implements Resettable, ResourceProvider {RegionResolver regionResolver, boolean clearFifoDeduplicationCacheOnPurge,SnsService snsService, Clock clock, String receiptHandleSecret,Instance<RequestContext> requestContextInstance, IamService iamService) {+ this(queueStore, messageStore, dedupStore, dedupIdentityStore, defaultVisibilityTimeout, maxMessageSize,+ baseUrl, regionResolver, clearFifoDeduplicationCacheOnPurge, snsService, clock,+ receiptHandleSecret, requestContextInstance, iamService, EndpointStrategy.FLOCI);+ }++ SqsService(StorageBackend<String, Queue> queueStore, StorageBackend<String, List<Message>> messageStore,+ StorageBackend<String, Map<String, Long>> dedupStore,+ StorageBackend<String, Map<String, Map<String, String>>> dedupIdentityStore,+ int defaultVisibilityTimeout, int maxMessageSize, String baseUrl,+ RegionResolver regionResolver, boolean clearFifoDeduplicationCacheOnPurge,+ SnsService snsService, Clock clock, String receiptHandleSecret,+ Instance<RequestContext> requestContextInstance, IamService iamService,+ EndpointStrategy endpointStrategy) {this.queueStore = queueStore;this.messageStore = messageStore;this.dedupStore = dedupStore;@@ -406,6 +423,7 @@ public class SqsService implements Resettable, ResourceProvider {this.maxMessageSize = maxMessageSize;this.maxAllowedMessageSize = Math.max(AWS_MAXIMUM_MESSAGE_SIZE, maxMessageSize);this.baseUrl = baseUrl;+ this.endpointStrategy = endpointStrategy;this.regionResolver = regionResolver;this.clearFifoDeduplicationCacheOnPurge = clearFifoDeduplicationCacheOnPurge;this.snsService = snsService;@@ -629,7 +647,7 @@ public class SqsService implements Resettable, ResourceProvider {attributes = validateMessageRetentionPeriod(attributes);String accountId = regionResolver.getAccountId();- String queueUrl = baseUrl + "/" + accountId + "/" + queueName;+ String queueUrl = buildQueueUrl(region, accountId, queueName);String storageKey = regionKey(region, queueUrl);// If queue already exists with same name, check for attribute conflicts@@ -662,7 +680,7 @@ public class SqsService implements Resettable, ResourceProvider {}}}- return existing;+ return withCurrentQueueUrl(existing, region);}Queue queue = new Queue(queueName, queueUrl);@@ -727,22 +745,26 @@ public class SqsService implements Resettable, ResourceProvider {public List<Queue> listQueues(String namePrefix, String region) {String prefix = region + "::";+ List<Queue> queues;if (namePrefix == null || namePrefix.isEmpty()) {- return queueStore.scan(key -> key.startsWith(prefix));+ queues = queueStore.scan(key -> key.startsWith(prefix));+ } else {+ queues = queueStore.scan(key -> {+ if (!key.startsWith(prefix)) {+ return false;+ }+ String queueUrl = key.substring(prefix.length());+ String name = queueUrl.substring(queueUrl.lastIndexOf('/') + 1);+ return name.startsWith(namePrefix);+ });}- return queueStore.scan(key -> {- if (!key.startsWith(prefix)) {- return false;- }- String queueUrl = key.substring(prefix.length());- String name = queueUrl.substring(queueUrl.lastIndexOf('/') + 1);- return name.startsWith(namePrefix);- });+ queues.forEach(q -> withCurrentQueueUrl(q, region));+ return queues;}public String getQueueUrl(String queueName, String region) {String accountId = regionResolver.getAccountId();- String queueUrl = baseUrl + "/" + accountId + "/" + queueName;+ String queueUrl = buildQueueUrl(region, accountId, queueName);String storageKey = regionKey(region, queueUrl);if (queueStore.get(storageKey).isEmpty()) {throw new AwsException("AWS.SimpleQueueService.NonExistentQueue",@@ -1520,7 +1542,7 @@ public class SqsService implements Resettable, ResourceProvider {String prefix = region + "::";for (Queue q : queueStore.scan(k -> k.startsWith(prefix))) {if (targetArn.equals(redriveDlqArn(q))) {- sourceQueues.add(q.getQueueUrl());+ sourceQueues.add(withCurrentQueueUrl(q, region).getQueueUrl());}}return sourceQueues;@@ -1970,6 +1992,30 @@ public class SqsService implements Resettable, ResourceProvider {return fromUrl != null ? fromUrl : regionResolver.getAccountId();}+ /**+ * The QueueUrl handed back to clients. The {@code floci} strategy, the default, returns floci's+ * own endpoint form, {@code <base-url>/<account>/<name>}. The {@code aws} strategy returns+ * the AWS form, {@code https://sqs.<region>.<dnsSuffix>/<account>/<name>}, for tooling that+ * parses queue URLs. Lookups key off the URL path, so either form is accepted on input.+ */+ private String buildQueueUrl(String region, String accountId, String queueName) {+ if (endpointStrategy != EndpointStrategy.AWS) {+ return baseUrl + "/" + accountId + "/" + queueName;+ }+ String effectiveRegion = region != null && !region.isBlank() ? region : regionResolver.getDefaultRegion();+ return "https://" + AwsEndpoints.host("sqs", effectiveRegion) + "/" + accountId + "/" + queueName;+ }++ /**+ * Reports a stored queue under the URL a fresh CreateQueue would return, so queues persisted+ * by an earlier version, or under the other endpoint strategy, list consistently.+ */+ private Queue withCurrentQueueUrl(Queue queue, String region) {+ String accountId = queue.getAccountId() != null ? queue.getAccountId() : regionResolver.getAccountId();+ queue.setQueueUrl(buildQueueUrl(region, accountId, queue.getQueueName()));+ return queue;+ }+/*** Accepts a full queue URL or a bare queue name and returns a canonical* full URL. AWS SQS allows clients to pass either form in the QueueUrldiff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex 1a5500178a..78b5009a01 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -380,6 +380,9 @@ floci:max-message-size: 1048576clear-fifo-deduplication-cache-on-purge: falsereceipt-handle-secret: local-emulator-secret+ # QueueUrl shape. floci is <base-url>/<account>/<name>,+ # aws is https://sqs.<region>.<partition dns suffix>/<account>/<name>+ endpoint-strategy: flocis3:enabled: trueenforce-auth: falsediff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsAwsEndpointStrategyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsAwsEndpointStrategyIntegrationTest.javanew file mode 100644index 0000000000..6efef94fbf--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsAwsEndpointStrategyIntegrationTest.java@@ -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");+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.javaindex dc250db4e2..cede28626a 100644--- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java@@ -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");+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.javaindex 65a63a2e3e..5ae9ae3050 100644--- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java@@ -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");+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsQueueUrlRoundTrip.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsQueueUrlRoundTrip.javanew file mode 100644index 0000000000..f5197a3738--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsQueueUrlRoundTrip.java@@ -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("/");+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.javaindex 8fe9043d68..c0f711a267 100644--- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java@@ -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));+ }+@Testvoid getQueueUrlNotFound() {assertThrows(AwsException.class, () ->diff --git a/src/test/resources/application.yml b/src/test/resources/application.ymlindex 83522ed9ab..53fb554a35 100644--- a/src/test/resources/application.yml+++ b/src/test/resources/application.yml@@ -134,6 +134,7 @@ floci:enabled: truedefault-visibility-timeout: 30max-message-size: 1048576+ endpoint-strategy: flocis3:enabled: trueenforce-auth: false