fix(sqs): message processing from non-default account (#1406)

* fix(sqs): use account-aware queue lookup in doReceiveMessage

The Lambda SQS ESM poller runs on a Vert.x timer thread with no CDI
request scope. doReceiveMessage() used queueStore.get(storageKey) which
falls back to defaultAccountId via AccountAwareStorageBackend, causing
queues under non-default accounts to be invisible to the poller. Switch
to getQueueByUrl(storageKey, queueUrl) for URL-derived, context-free
account resolution, matching sendMessage/deleteMessage.

* fix(sqs): persist messages with account-aware storage in GuardedMessageQueue

GuardedMessageQueue.persist() called messageStore.put() which goes
through AccountAwareStorageBackend.put() and falls back to
defaultAccountId when invoked off the CDI request scope (Lambda ESM
poller on a Vert.x timer thread). This persisted visibility-timeout and
deletion state changes under the wrong account prefix, corrupting
message state for non-default-account queues.

Extract the 12-digit account from the storage key (region::/accountId/
queueName) and call putForAccount(accountId, ...) when the store is an
AccountAwareStorageBackend, with a safe fall-through to put() otherwise.

* test(lambda): add ESM multi-account poller regression test

End-to-end regression for the SQS ESM poller account-aware lookup fix
(M1+M2). Creates a queue, Lambda function, and event source mapping
under non-default account 000000000001 (via a 12-digit AKID SigV4
credential), sends a message, and asserts the poller claims it
(ApproximateNumberOfMessagesNotVisible > 0) within the poll interval.

Before the fix the poller resolved the queue under defaultAccountId and
never fired; this test would have failed. Self-contained with unique
resource names and best-effort cleanup in finally.
This commit is contained in:
alexjplant authored and GitHub committed 2026-06-18 23:32:17 -05:00
1 parent 9bb1db3a54
commit 630328547e
3 files changed
+210 -3

No files matched your search

@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.sqs;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.services.sqs.model.Message;
@@ -266,6 +267,33 @@ class GuardedMessageQueue {
if (closed || messageStore == null || storageKey == null) {
return;
}
if (messageStore instanceof AccountAwareStorageBackend<List<Message>> aware) {
String accountId = extractAccountFromStorageKey(storageKey);
if (accountId != null) {
aware.putForAccount(accountId, storageKey, new ArrayList<>(messages));
return;
}
}
messageStore.put(storageKey, new ArrayList<>(messages));
}
/**
* Extracts the 12-digit account ID from a storage key of the form
* {@code region::/accountId/queueName}.
*/
private static String extractAccountFromStorageKey(String storageKey) {
if (storageKey == null) {
return null;
}
// storageKey format: "us-east-1::/000000000001/my-queue"
int separator = storageKey.indexOf("::");
if (separator < 0) {
return null;
}
String path = storageKey.substring(separator + 2); // "/000000000001/my-queue"
String trimmed = path.startsWith("/") ? path.substring(1) : path;
int slash = trimmed.indexOf('/');
String candidate = slash > 0 ? trimmed.substring(0, slash) : trimmed;
return candidate.matches("\\d{12}") ? candidate : null;
}
}
@@ -611,7 +611,7 @@ public class SqsService {
// to the new queue's consumers.
return Collections.emptyList();
}
List<Message> result = doReceiveMessage(storageKey, maxMessages, visibilityTimeout, region);
List<Message> result = doReceiveMessage(storageKey, queueUrl, maxMessages, visibilityTimeout, region);
if (!result.isEmpty() || maxWait <= 0) {
if (!result.isEmpty() && LOG.isTraceEnabled()) {
for (Message m : result) {
@@ -657,8 +657,8 @@ public class SqsService {
});
}
private List<Message> doReceiveMessage(String storageKey, int maxMessages, int visibilityTimeout, String region) {
Queue queue = queueStore.get(storageKey).orElse(null);
private List<Message> doReceiveMessage(String storageKey, String queueUrl, int maxMessages, int visibilityTimeout, String region) {
Queue queue = getQueueByUrl(storageKey, queueUrl).orElse(null);
if (queue == null) {
return Collections.emptyList();
}