mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
@@ -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 {
|
||||
|
||||
@@ -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 QueueUrl
|
||||
|
||||
Reference in new issue
Block a user