fix(sns): honor RawMessageDelivery attribute for SQS subscriptions (#54)

* fix(sns): honor RawMessageDelivery attribute for SQS subscriptions
* fix(sns): address PR review feedback on RawMessageDelivery

- Preserve original SNS attribute DataType (Number, Binary, etc.) by
  changing the internal messageAttributes type from Map<String,String>
  to Map<String,MessageAttributeValue> throughout the publish pipeline
- Update SnsQueryHandler and SnsJsonHandler to parse DataType from
  incoming requests and construct typed MessageAttributeValue objects
- Return Collections.emptyMap() from toSqsMessageAttributes instead
  of null for safety on non-raw delivery paths
- Use UUID-based queue names in raw delivery tests for isolation
- Add test asserting message attributes (including Number type) are
  forwarded correctly to SQS in raw delivery mode
This commit is contained in:
Roberto Perez Alcolea authored and GitHub committed 2026-03-31 23:18:06 -05:00
1 parent a4af506c15
commit b762becaea
4 files changed
+232 -21

No files matched your search

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.sns;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.services.sns.model.Subscription;
import io.github.hectorvent.floci.services.sns.model.Topic;
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -154,11 +155,13 @@ public class SnsJsonHandler {
String message = request.path("Message").asText(null);
String subject = request.path("Subject").asText(null);
Map<String, String> attributes = new HashMap<>();
Map<String, MessageAttributeValue> attributes = new HashMap<>();
JsonNode attrsNode = request.path("MessageAttributes");
if (attrsNode.isObject()) {
attrsNode.fields().forEachRemaining(entry -> {
attributes.put(entry.getKey(), entry.getValue().path("StringValue").asText());
String dataType = entry.getValue().path("DataType").asText("String");
String stringValue = entry.getValue().path("StringValue").asText();
attributes.put(entry.getKey(), new MessageAttributeValue(stringValue, dataType));
});
}
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.sns;
import io.github.hectorvent.floci.core.common.*;
import io.github.hectorvent.floci.services.sns.model.Subscription;
import io.github.hectorvent.floci.services.sns.model.Topic;
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.MultivaluedMap;
@@ -181,12 +182,13 @@ public class SnsQueryHandler {
String messageGroupId = getParam(params, "MessageGroupId");
String messageDeduplicationId = getParam(params, "MessageDeduplicationId");
Map<String, String> attributes = new HashMap<>();
Map<String, MessageAttributeValue> attributes = new HashMap<>();
for (int i = 1; ; i++) {
String name = params.getFirst("MessageAttributes.entry." + i + ".Name");
if (name == null) break;
String value = params.getFirst("MessageAttributes.entry." + i + ".Value.StringValue");
if (value != null) attributes.put(name, value);
String dataType = params.getFirst("MessageAttributes.entry." + i + ".Value.DataType");
if (value != null) attributes.put(name, new MessageAttributeValue(value, dataType != null ? dataType : "String"));
}
try {
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.sns.model.Subscription;
import io.github.hectorvent.floci.services.sns.model.Topic;
import io.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -27,6 +28,7 @@ import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@@ -247,12 +249,12 @@ public class SnsService {
}
public String publish(String topicArn, String targetArn, String phoneNumber, String message,
String subject, Map<String, String> messageAttributes, String region) {
String subject, Map<String, MessageAttributeValue> messageAttributes, String region) {
return publish(topicArn, targetArn, phoneNumber, message, subject, messageAttributes, null, null, region);
}
public String publish(String topicArn, String targetArn, String phoneNumber, String message,
String subject, Map<String, String> messageAttributes,
String subject, Map<String, MessageAttributeValue> messageAttributes,
String messageGroupId, String messageDeduplicationId, String region) {
// Send SMS
if (phoneNumber != null) {
@@ -361,7 +363,7 @@ public class SnsService {
String messageId = UUID.randomUUID().toString();
@SuppressWarnings("unchecked")
Map<String, String> attrs = (Map<String, String>) entry.get("MessageAttributes");
Map<String, MessageAttributeValue> attrs = (Map<String, MessageAttributeValue>) entry.get("MessageAttributes");
for (Subscription sub : subscriptionsByTopic(topicArn, region)) {
if ("true".equals(sub.getAttributes().get("PendingConfirmation"))) continue;
if (!matchesFilterPolicy(sub, attrs)) continue;
@@ -411,7 +413,7 @@ public class SnsService {
* All keys in the policy must match (AND logic). Within each key's rule array,
* any matching element is sufficient (OR logic).
*/
private boolean matchesFilterPolicy(Subscription sub, Map<String, String> messageAttributes) {
private boolean matchesFilterPolicy(Subscription sub, Map<String, MessageAttributeValue> messageAttributes) {
String filterPolicyJson = sub.getAttributes().get("FilterPolicy");
if (filterPolicyJson == null || filterPolicyJson.isBlank()) {
return true;
@@ -426,13 +428,14 @@ public class SnsService {
LOG.warnv("Invalid FilterPolicy (not a JSON object) for {0}", sub.getSubscriptionArn());
return false;
}
Map<String, String> attrs = messageAttributes != null ? messageAttributes : Map.of();
Map<String, MessageAttributeValue> attrs = messageAttributes != null ? messageAttributes : Map.of();
var fields = filterPolicy.fields();
while (fields.hasNext()) {
var entry = fields.next();
String key = entry.getKey();
JsonNode rules = entry.getValue();
String actualValue = attrs.get(key);
MessageAttributeValue attr = attrs.get(key);
String actualValue = attr != null ? attr.getStringValue() : null;
if (!matchesAttributeRules(actualValue, rules)) {
return false;
}
@@ -559,7 +562,7 @@ public class SnsService {
}
private void deliverMessage(Subscription sub, String message, String subject,
Map<String, String> messageAttributes, String messageId,
Map<String, MessageAttributeValue> messageAttributes, String messageId,
String topicArn, String messageGroupId) {
try {
switch (sub.getProtocol()) {
@@ -569,10 +572,15 @@ public class SnsService {
region = extractRegionFromArn(topicArn);
}
String queueUrl = sqsArnToUrl(sub.getEndpoint());
String envelope = buildSnsEnvelope(message, subject, messageAttributes, topicArn, messageId);
sqsService.sendMessage(queueUrl, envelope, 0, messageGroupId, null, region);
LOG.debugv("Delivered SNS message to SQS: {0} ({1}) in {2}",
sub.getEndpoint(), queueUrl, region);
boolean rawDelivery = "true".equalsIgnoreCase(sub.getAttributes().get("RawMessageDelivery"));
String body = rawDelivery
? message
: buildSnsEnvelope(message, subject, messageAttributes, topicArn, messageId);
Map<String, MessageAttributeValue> sqsAttributes = rawDelivery
? toSqsMessageAttributes(messageAttributes)
: Collections.emptyMap();
sqsService.sendMessage(queueUrl, body, 0, messageGroupId, null, sqsAttributes, region);
LOG.debugv("Delivered SNS message to SQS: {0} ({1}) raw={2}", sub.getEndpoint(), queueUrl, rawDelivery);
}
case "lambda" -> {
String fnName = extractFunctionName(sub.getEndpoint());
@@ -596,7 +604,7 @@ public class SnsService {
}
private String buildSnsLambdaEvent(String topicArn, String messageId, String message,
String subject, Map<String, String> messageAttributes,
String subject, Map<String, MessageAttributeValue> messageAttributes,
String subscriptionArn) {
try {
String timestamp = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
@@ -619,8 +627,8 @@ public class SnsService {
if (messageAttributes != null) {
for (var entry : messageAttributes.entrySet()) {
ObjectNode attr = attrs.putObject(entry.getKey());
attr.put("Type", "String");
attr.put("Value", entry.getValue());
attr.put("Type", entry.getValue().getDataType());
attr.put("Value", entry.getValue().getStringValue());
}
}
ObjectNode record = objectMapper.createObjectNode();
@@ -647,8 +655,19 @@ public class SnsService {
return parts.length >= 4 ? parts[3] : null;
}
/**
* Forwards SNS message attributes as SQS MessageAttributeValue objects
* when RawMessageDelivery is enabled, preserving the original DataType.
*/
private Map<String, MessageAttributeValue> toSqsMessageAttributes(Map<String, MessageAttributeValue> snsAttributes) {
if (snsAttributes == null || snsAttributes.isEmpty()) {
return Collections.emptyMap();
}
return new java.util.HashMap<>(snsAttributes);
}
private String buildSnsEnvelope(String message, String subject,
Map<String, String> messageAttributes,
Map<String, MessageAttributeValue> messageAttributes,
String topicArn, String messageId) {
try {
ObjectNode node = objectMapper.createObjectNode();
@@ -663,8 +682,8 @@ public class SnsService {
if (messageAttributes != null) {
for (var entry : messageAttributes.entrySet()) {
ObjectNode attr = attrs.putObject(entry.getKey());
attr.put("Type", "String");
attr.put("Value", entry.getValue());
attr.put("Type", entry.getValue().getDataType());
attr.put("Value", entry.getValue().getStringValue());
}
}
return objectMapper.writeValueAsString(node);