mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(sqs): honor AttributeNames/MessageSystemAttributeNames on ReceiveMessage (#784)
* fix(sqs): honor AttributeNames/MessageSystemAttributeNames on ReceiveMessage ReceiveMessage previously emitted a fixed set of system attributes (ApproximateReceiveCount, SentTimestamp, plus FIFO fields when present) regardless of what the caller asked for. AWS instead returns exactly the attributes the request selects: - empty list -> no system attributes - ["SenderId"] -> only SenderId - ["All"] -> SenderId, SentTimestamp, ApproximateReceiveCount, ApproximateFirstReceiveTimestamp (and FIFO MessageGroupId, SequenceNumber, MessageDeduplicationId when applicable) Both the legacy AttributeName.N / AttributeNames parameter and the modern MessageSystemAttributeName.N / MessageSystemAttributeNames parameter are honored. SenderId derives from the account ID parsed from the queue URL (falling back to the configured default account), matching the multi-tenancy isolation introduced in #753. A new firstReceiveTimestamp field on Message backs ApproximateFirstReceiveTimestamp and is set on first claim. Refs #768. * test(sqs): cover legacy AttributeNames and Query-protocol filtering Adds Query-protocol receive-with-filter coverage and a legacy AttributeNames case to the JSON test, and clarifies the senderIdFor Javadoc to reflect actual fallback order (queue URL → request context → default).
This commit is contained in:
1 parent
e62fd09b6d
commit
ffd7779c5c
8 files changed
+258
-29
No files matched your search
@@ -86,6 +86,9 @@ class GuardedMessageQueue {
|
||||
String deadLetterTargetArn, List<Message> claimed,
|
||||
List<Message> dlqCandidates) {
|
||||
msg.setReceiveCount(msg.getReceiveCount() + 1);
|
||||
if (msg.getFirstReceiveTimestamp() == null) {
|
||||
msg.setFirstReceiveTimestamp(Instant.now());
|
||||
}
|
||||
|
||||
if (maxReceiveCount > 0 && deadLetterTargetArn != null
|
||||
&& msg.getReceiveCount() > maxReceiveCount) {
|
||||
|
||||
@@ -81,6 +81,42 @@ public class SqsJsonHandler {
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private void writeSystemAttributesJson(ObjectNode msgNode, Message msg,
|
||||
java.util.Set<String> requested, String senderId) {
|
||||
if (requested.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
boolean all = requested.contains("All");
|
||||
ObjectNode attrs = objectMapper.createObjectNode();
|
||||
if ((all || requested.contains("SenderId")) && senderId != null) {
|
||||
attrs.put("SenderId", senderId);
|
||||
}
|
||||
if (all || requested.contains("SentTimestamp")) {
|
||||
attrs.put("SentTimestamp", String.valueOf(msg.getSentTimestamp().toEpochMilli()));
|
||||
}
|
||||
if (all || requested.contains("ApproximateReceiveCount")) {
|
||||
attrs.put("ApproximateReceiveCount", String.valueOf(msg.getReceiveCount()));
|
||||
}
|
||||
if ((all || requested.contains("ApproximateFirstReceiveTimestamp"))
|
||||
&& msg.getFirstReceiveTimestamp() != null) {
|
||||
attrs.put("ApproximateFirstReceiveTimestamp",
|
||||
String.valueOf(msg.getFirstReceiveTimestamp().toEpochMilli()));
|
||||
}
|
||||
if (msg.getMessageGroupId() != null && (all || requested.contains("MessageGroupId"))) {
|
||||
attrs.put("MessageGroupId", msg.getMessageGroupId());
|
||||
}
|
||||
if (msg.getSequenceNumber() > 0 && (all || requested.contains("SequenceNumber"))) {
|
||||
attrs.put("SequenceNumber", String.valueOf(msg.getSequenceNumber()));
|
||||
}
|
||||
if (msg.getMessageDeduplicationId() != null
|
||||
&& (all || requested.contains("MessageDeduplicationId"))) {
|
||||
attrs.put("MessageDeduplicationId", msg.getMessageDeduplicationId());
|
||||
}
|
||||
if (!attrs.isEmpty()) {
|
||||
msgNode.set("Attributes", attrs);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> jsonNodeToList(JsonNode node) {
|
||||
List<String> values = new ArrayList<>();
|
||||
if (node != null && node.isArray()) {
|
||||
@@ -199,8 +235,13 @@ public class SqsJsonHandler {
|
||||
int visibilityTimeout = request.path("VisibilityTimeout").asInt(-1);
|
||||
int waitTimeSeconds = request.path("WaitTimeSeconds").asInt(0);
|
||||
|
||||
java.util.Set<String> requestedAttrs = new java.util.LinkedHashSet<>();
|
||||
requestedAttrs.addAll(jsonNodeToList(request.path("AttributeNames")));
|
||||
requestedAttrs.addAll(jsonNodeToList(request.path("MessageSystemAttributeNames")));
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, maxMessages,
|
||||
visibilityTimeout, waitTimeSeconds, region);
|
||||
String senderId = sqsService.senderIdFor(queueUrl);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
// Match AWS: omit the Messages field entirely when no messages are
|
||||
@@ -220,18 +261,7 @@ public class SqsJsonHandler {
|
||||
}
|
||||
msgNode.put("Body", msg.getBody());
|
||||
|
||||
ObjectNode attrs = msgNode.putObject("Attributes");
|
||||
attrs.put("ApproximateReceiveCount", String.valueOf(msg.getReceiveCount()));
|
||||
attrs.put("SentTimestamp", String.valueOf(msg.getSentTimestamp().toEpochMilli()));
|
||||
if (msg.getMessageGroupId() != null) {
|
||||
attrs.put("MessageGroupId", msg.getMessageGroupId());
|
||||
}
|
||||
if (msg.getSequenceNumber() > 0) {
|
||||
attrs.put("SequenceNumber", String.valueOf(msg.getSequenceNumber()));
|
||||
}
|
||||
if (msg.getMessageDeduplicationId() != null) {
|
||||
attrs.put("MessageDeduplicationId", msg.getMessageDeduplicationId());
|
||||
}
|
||||
writeSystemAttributesJson(msgNode, msg, requestedAttrs, senderId);
|
||||
|
||||
if (msg.getMessageAttributes() != null && !msg.getMessageAttributes().isEmpty()) {
|
||||
ObjectNode msgAttrs = msgNode.putObject("MessageAttributes");
|
||||
|
||||
@@ -79,6 +79,46 @@ public class SqsQueryHandler {
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("RemovePermission", null)).build();
|
||||
}
|
||||
|
||||
private static void writeSystemAttributesXml(XmlBuilder xml, Message msg,
|
||||
java.util.Set<String> requested, String senderId) {
|
||||
if (requested.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
boolean all = requested.contains("All");
|
||||
if (all || requested.contains("SenderId")) {
|
||||
if (senderId != null) {
|
||||
xml.start("Attribute").elem("Name", "SenderId")
|
||||
.elem("Value", senderId).end("Attribute");
|
||||
}
|
||||
}
|
||||
if (all || requested.contains("SentTimestamp")) {
|
||||
xml.start("Attribute").elem("Name", "SentTimestamp")
|
||||
.elem("Value", String.valueOf(msg.getSentTimestamp().toEpochMilli())).end("Attribute");
|
||||
}
|
||||
if (all || requested.contains("ApproximateReceiveCount")) {
|
||||
xml.start("Attribute").elem("Name", "ApproximateReceiveCount")
|
||||
.elem("Value", String.valueOf(msg.getReceiveCount())).end("Attribute");
|
||||
}
|
||||
if (all || requested.contains("ApproximateFirstReceiveTimestamp")) {
|
||||
if (msg.getFirstReceiveTimestamp() != null) {
|
||||
xml.start("Attribute").elem("Name", "ApproximateFirstReceiveTimestamp")
|
||||
.elem("Value", String.valueOf(msg.getFirstReceiveTimestamp().toEpochMilli())).end("Attribute");
|
||||
}
|
||||
}
|
||||
if (msg.getMessageGroupId() != null && (all || requested.contains("MessageGroupId"))) {
|
||||
xml.start("Attribute").elem("Name", "MessageGroupId")
|
||||
.elem("Value", msg.getMessageGroupId()).end("Attribute");
|
||||
}
|
||||
if (msg.getSequenceNumber() > 0 && (all || requested.contains("SequenceNumber"))) {
|
||||
xml.start("Attribute").elem("Name", "SequenceNumber")
|
||||
.elem("Value", String.valueOf(msg.getSequenceNumber())).end("Attribute");
|
||||
}
|
||||
if (msg.getMessageDeduplicationId() != null && (all || requested.contains("MessageDeduplicationId"))) {
|
||||
xml.start("Attribute").elem("Name", "MessageDeduplicationId")
|
||||
.elem("Value", msg.getMessageDeduplicationId()).end("Attribute");
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> collectIndexed(MultivaluedMap<String, String> params, String prefix) {
|
||||
List<String> values = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
@@ -192,7 +232,12 @@ public class SqsQueryHandler {
|
||||
int visibilityTimeout = getIntParam(params, "VisibilityTimeout", -1);
|
||||
int waitTimeSeconds = getIntParam(params, "WaitTimeSeconds", 0);
|
||||
|
||||
java.util.Set<String> requestedAttrs = new java.util.LinkedHashSet<>();
|
||||
requestedAttrs.addAll(collectIndexed(params, "AttributeName."));
|
||||
requestedAttrs.addAll(collectIndexed(params, "MessageSystemAttributeName."));
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, maxMessages, visibilityTimeout, waitTimeSeconds, region);
|
||||
String senderId = sqsService.senderIdFor(queueUrl);
|
||||
|
||||
var xml = new XmlBuilder();
|
||||
for (Message msg : messages) {
|
||||
@@ -203,23 +248,8 @@ public class SqsQueryHandler {
|
||||
if (msg.getMd5OfMessageAttributes() != null) {
|
||||
xml.elem("MD5OfMessageAttributes", msg.getMd5OfMessageAttributes());
|
||||
}
|
||||
xml.elem("Body", msg.getBody())
|
||||
.start("Attribute").elem("Name", "ApproximateReceiveCount")
|
||||
.elem("Value", String.valueOf(msg.getReceiveCount())).end("Attribute")
|
||||
.start("Attribute").elem("Name", "SentTimestamp")
|
||||
.elem("Value", String.valueOf(msg.getSentTimestamp().toEpochMilli())).end("Attribute");
|
||||
if (msg.getMessageGroupId() != null) {
|
||||
xml.start("Attribute").elem("Name", "MessageGroupId")
|
||||
.elem("Value", msg.getMessageGroupId()).end("Attribute");
|
||||
}
|
||||
if (msg.getSequenceNumber() > 0) {
|
||||
xml.start("Attribute").elem("Name", "SequenceNumber")
|
||||
.elem("Value", String.valueOf(msg.getSequenceNumber())).end("Attribute");
|
||||
}
|
||||
if (msg.getMessageDeduplicationId() != null) {
|
||||
xml.start("Attribute").elem("Name", "MessageDeduplicationId")
|
||||
.elem("Value", msg.getMessageDeduplicationId()).end("Attribute");
|
||||
}
|
||||
xml.elem("Body", msg.getBody());
|
||||
writeSystemAttributesXml(xml, msg, requestedAttrs, senderId);
|
||||
if (msg.getMessageAttributes() != null && !msg.getMessageAttributes().isEmpty()) {
|
||||
for (var entry : msg.getMessageAttributes().entrySet()) {
|
||||
xml.start("MessageAttribute")
|
||||
|
||||
@@ -975,6 +975,18 @@ public class SqsService {
|
||||
return new java.util.LinkedHashMap<>(queue.getTags());
|
||||
}
|
||||
|
||||
/**
|
||||
* SQS reports SenderId as the principal that called SendMessage. Floci
|
||||
* has no per-call IAM context, so it falls back to the account that owns
|
||||
* the queue (parsed from the queue URL when present), otherwise the
|
||||
* account from the current request context, otherwise the configured
|
||||
* default account.
|
||||
*/
|
||||
public String senderIdFor(String queueUrl) {
|
||||
String fromUrl = accountFromQueueUrl(queueUrl);
|
||||
return fromUrl != null ? fromUrl : regionResolver.getAccountId();
|
||||
}
|
||||
|
||||
private static String regionKey(String region, String queueUrl) {
|
||||
return region + "::" + extractQueuePath(queueUrl);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ public class Message {
|
||||
private String body;
|
||||
private Map<String, MessageAttributeValue> messageAttributes;
|
||||
private Instant sentTimestamp;
|
||||
private Instant firstReceiveTimestamp;
|
||||
private int receiveCount;
|
||||
private String md5OfBody;
|
||||
private String md5OfMessageAttributes;
|
||||
@@ -57,6 +58,9 @@ public class Message {
|
||||
public Instant getSentTimestamp() { return sentTimestamp; }
|
||||
public void setSentTimestamp(Instant sentTimestamp) { this.sentTimestamp = sentTimestamp; }
|
||||
|
||||
public Instant getFirstReceiveTimestamp() { return firstReceiveTimestamp; }
|
||||
public void setFirstReceiveTimestamp(Instant firstReceiveTimestamp) { this.firstReceiveTimestamp = firstReceiveTimestamp; }
|
||||
|
||||
public int getReceiveCount() { return receiveCount; }
|
||||
public void setReceiveCount(int receiveCount) { this.receiveCount = receiveCount; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user