From b762becaea3795d2a6320e10dd290a84088e9b2b Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Tue, 31 Mar 2026 21:18:06 -0700 Subject: [PATCH] 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 to Map 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 --- .../floci/services/sns/SnsJsonHandler.java | 7 +- .../floci/services/sns/SnsQueryHandler.java | 6 +- .../floci/services/sns/SnsService.java | 53 +++-- .../services/sns/SnsIntegrationTest.java | 187 ++++++++++++++++++ 4 files changed, 232 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java index 508f18b52f..eb3b22e1a2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsJsonHandler.java @@ -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 attributes = new HashMap<>(); + Map 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)); }); } diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java index 39ffaef05e..e8c7b3ba68 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsQueryHandler.java @@ -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 attributes = new HashMap<>(); + Map 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 { diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java index c2d5da6ac7..b71c4c8f36 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java @@ -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 messageAttributes, String region) { + String subject, Map 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 messageAttributes, + String subject, Map 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 attrs = (Map) entry.get("MessageAttributes"); + Map attrs = (Map) 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 messageAttributes) { + private boolean matchesFilterPolicy(Subscription sub, Map 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 attrs = messageAttributes != null ? messageAttributes : Map.of(); + Map 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 messageAttributes, String messageId, + Map 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 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 messageAttributes, + String subject, Map 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 toSqsMessageAttributes(Map snsAttributes) { + if (snsAttributes == null || snsAttributes.isEmpty()) { + return Collections.emptyMap(); + } + return new java.util.HashMap<>(snsAttributes); + } + private String buildSnsEnvelope(String message, String subject, - Map messageAttributes, + Map 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); diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java index a41dd90586..5169791066 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import java.util.UUID; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.*; @@ -32,6 +34,10 @@ class SnsIntegrationTest { private static String topicArn; private static String subscriptionArn; private static String sqsQueueUrl; + private static String rawDeliveryQueueUrl; + private static String rawDeliverySubArn; + private static String envelopeQueueUrl; + private static String envelopeSubArn; @Test @Order(1) @@ -587,6 +593,187 @@ class SnsIntegrationTest { .when().post("/"); } + @Test + @Order(50) + void rawDelivery_createQueuesAndSubscribe() { + String suffix = UUID.randomUUID().toString().substring(0, 8); + + rawDeliveryQueueUrl = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", "sns-raw-delivery-" + suffix) + .when() + .post("/") + .then() + .statusCode(200) + .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl"); + + envelopeQueueUrl = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", "sns-envelope-delivery-" + suffix) + .when() + .post("/") + .then() + .statusCode(200) + .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl"); + + rawDeliverySubArn = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "Subscribe") + .formParam("TopicArn", topicArn) + .formParam("Protocol", "sqs") + .formParam("Endpoint", rawDeliveryQueueUrl) + .when() + .post("/") + .then() + .statusCode(200) + .extract().xmlPath().getString("SubscribeResponse.SubscribeResult.SubscriptionArn"); + + envelopeSubArn = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "Subscribe") + .formParam("TopicArn", topicArn) + .formParam("Protocol", "sqs") + .formParam("Endpoint", envelopeQueueUrl) + .when() + .post("/") + .then() + .statusCode(200) + .extract().xmlPath().getString("SubscribeResponse.SubscribeResult.SubscriptionArn"); + } + + @Test + @Order(51) + void rawDelivery_setSubscriptionAttribute() { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "SetSubscriptionAttributes") + .formParam("SubscriptionArn", rawDeliverySubArn) + .formParam("AttributeName", "RawMessageDelivery") + .formParam("AttributeValue", "true") + .when() + .post("/") + .then() + .statusCode(200); + } + + @Test + @Order(52) + void rawDelivery_publishAndVerifyRawMessage() { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "Publish") + .formParam("TopicArn", topicArn) + .formParam("Message", "Raw delivery test message") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("")); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", rawDeliveryQueueUrl) + .formParam("MaxNumberOfMessages", "1") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("Raw delivery test message")) + .body(not(containsString("Notification"))); + } + + @Test + @Order(53) + void rawDelivery_defaultSubscriptionWrapsInEnvelope() { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", envelopeQueueUrl) + .formParam("MaxNumberOfMessages", "1") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("Raw delivery test message")) + .body(containsString("Notification")); + } + + @Test + @Order(54) + void rawDelivery_messageAttributesForwardedOnRawDelivery() { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "Publish") + .formParam("TopicArn", topicArn) + .formParam("Message", "Attribute forwarding test") + .formParam("MessageAttributes.entry.1.Name", "color") + .formParam("MessageAttributes.entry.1.Value.DataType", "String") + .formParam("MessageAttributes.entry.1.Value.StringValue", "blue") + .formParam("MessageAttributes.entry.2.Name", "count") + .formParam("MessageAttributes.entry.2.Value.DataType", "Number") + .formParam("MessageAttributes.entry.2.Value.StringValue", "42") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("")); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", rawDeliveryQueueUrl) + .formParam("MaxNumberOfMessages", "1") + .formParam("MessageAttributeNames.member.1", "All") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("Attribute forwarding test")) + .body(containsString("color")) + .body(containsString("blue")) + .body(containsString("count")) + .body(containsString("Number")); + } + + @Test + @Order(55) + void rawDelivery_cleanup() { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "Unsubscribe") + .formParam("SubscriptionArn", rawDeliverySubArn) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "Unsubscribe") + .formParam("SubscriptionArn", envelopeSubArn) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteQueue") + .formParam("QueueUrl", rawDeliveryQueueUrl) + .when() + .post("/"); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteQueue") + .formParam("QueueUrl", envelopeQueueUrl) + .when() + .post("/"); + } + @Test @Order(100) void unsubscribe() {