diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SqsMd5Test.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SqsMd5Test.java index 6497bf19c3..f4386fdf2b 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SqsMd5Test.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SqsMd5Test.java @@ -57,6 +57,40 @@ class SqsMd5Test { sqs.deleteQueue(DeleteQueueRequest.builder().queueUrl(queueUrl).build()); } + @Test + void receiveMessageWithSelectedAttributePassesSdkMd5Validation() { + String queueUrl = sqs.createQueue(CreateQueueRequest.builder() + .queueName("md5-receive-" + UUID.randomUUID()).build()).queueUrl(); + try { + sqs.sendMessage(SendMessageRequest.builder() + .queueUrl(queueUrl) + .messageBody("filter-test") + .messageAttributes(Map.of( + "color.primary", MessageAttributeValue.builder() + .dataType("String").stringValue("red").build(), + "color", MessageAttributeValue.builder() + .dataType("String").stringValue("blue").build(), + "colorful", MessageAttributeValue.builder() + .dataType("String").stringValue("bright").build(), + "secret", MessageAttributeValue.builder() + .dataType("String").stringValue("private").build())) + .build()); + + ReceiveMessageResponse response = sqs.receiveMessage(ReceiveMessageRequest.builder() + .queueUrl(queueUrl) + .messageAttributeNames("color.*") + .build()); + + assertThat(response.messages()).hasSize(1); + assertThat(response.messages().get(0).messageAttributes()) + .containsOnlyKeys("color", "colorful", "color.primary"); + assertThat(response.messages().get(0).md5OfMessageAttributes()) + .isEqualTo("46db9885b8f221ea04082f03e3a63d14"); + } finally { + sqs.deleteQueue(DeleteQueueRequest.builder().queueUrl(queueUrl).build()); + } + } + @Test void fifoQueueSendMessageWithAttributesPassesSdkMd5Validation() { String queueName = "md5-fifo-" + UUID.randomUUID() + ".fifo"; diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java index 7ad1e0dd1a..626f17565e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java @@ -259,6 +259,7 @@ public class SqsJsonHandler { Set requestedAttrs = new LinkedHashSet<>(); requestedAttrs.addAll(jsonNodeToList(request.path("AttributeNames"))); requestedAttrs.addAll(jsonNodeToList(request.path("MessageSystemAttributeNames"))); + Set requestedMessageAttrs = new LinkedHashSet<>(jsonNodeToList(request.path("MessageAttributeNames"))); List messages = sqsService.receiveMessage(queueUrl, maxMessages, visibilityTimeout, waitTimeSeconds, region); @@ -273,20 +274,23 @@ public class SqsJsonHandler { } ArrayNode messagesArray = response.putArray("Messages"); for (Message msg : messages) { + Map selectedMessageAttrs = SqsMessageAttributeSelector.select( + msg.getMessageAttributes(), requestedMessageAttrs); ObjectNode msgNode = objectMapper.createObjectNode(); msgNode.put("MessageId", msg.getMessageId()); msgNode.put("ReceiptHandle", msg.getReceiptHandle()); msgNode.put("MD5OfBody", msg.getMd5OfBody()); - if (msg.getMd5OfMessageAttributes() != null) { - msgNode.put("MD5OfMessageAttributes", msg.getMd5OfMessageAttributes()); + String messageAttrsMd5 = Message.computeMessageAttributesMd5(selectedMessageAttrs); + if (messageAttrsMd5 != null) { + msgNode.put("MD5OfMessageAttributes", messageAttrsMd5); } msgNode.put("Body", msg.getBody()); writeSystemAttributesJson(msgNode, msg, requestedAttrs, senderId); - if (msg.getMessageAttributes() != null && !msg.getMessageAttributes().isEmpty()) { - ObjectNode msgAttrs = msgNode.putObject("MessageAttributes"); - for (Map.Entry entry : msg.getMessageAttributes().entrySet()) { + if (!selectedMessageAttrs.isEmpty()) { + ObjectNode msgAttrs = objectMapper.createObjectNode(); + for (Map.Entry entry : selectedMessageAttrs.entrySet()) { ObjectNode valNode = msgAttrs.putObject(entry.getKey()); valNode.put("DataType", entry.getValue().getDataType()); if (entry.getValue().getBinaryValue() != null) { @@ -295,6 +299,7 @@ public class SqsJsonHandler { valNode.put("StringValue", entry.getValue().getStringValue()); } } + msgNode.set("MessageAttributes", msgAttrs); } messagesArray.add(msgNode); diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsMessageAttributeSelector.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsMessageAttributeSelector.java new file mode 100644 index 0000000000..8a46a44745 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsMessageAttributeSelector.java @@ -0,0 +1,39 @@ +package io.github.hectorvent.floci.services.sqs; + +import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +final class SqsMessageAttributeSelector { + + private SqsMessageAttributeSelector() { + } + + static Map select(Map attributes, + Set requestedNames) { + if (attributes == null || attributes.isEmpty() || requestedNames.isEmpty()) { + return Map.of(); + } + Map selected = new LinkedHashMap<>(); + for (Map.Entry entry : attributes.entrySet()) { + if (includes(requestedNames, entry.getKey())) { + selected.put(entry.getKey(), entry.getValue()); + } + } + return selected; + } + + private static boolean includes(Set requestedNames, String name) { + if (requestedNames.contains("All") || requestedNames.contains(".*") || requestedNames.contains(name)) { + return true; + } + for (String requestedName : requestedNames) { + if (requestedName.endsWith(".*") && name.startsWith(requestedName.substring(0, requestedName.length() - 2))) { + return true; + } + } + return false; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsQueryHandler.java index 5e9ba9c5c5..3d1720553d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsQueryHandler.java @@ -273,23 +273,27 @@ public class SqsQueryHandler { Set requestedAttrs = new LinkedHashSet<>(); requestedAttrs.addAll(collectIndexed(params, "AttributeName.")); requestedAttrs.addAll(collectIndexed(params, "MessageSystemAttributeName.")); + Set requestedMessageAttrs = new LinkedHashSet<>(collectIndexed(params, "MessageAttributeName.")); List messages = sqsService.receiveMessage(queueUrl, maxMessages, visibilityTimeout, waitTimeSeconds, region); String senderId = sqsService.senderIdFor(queueUrl); XmlBuilder xml = new XmlBuilder(); for (Message msg : messages) { + Map selectedMessageAttrs = SqsMessageAttributeSelector.select( + msg.getMessageAttributes(), requestedMessageAttrs); xml.start("Message") .elem("MessageId", msg.getMessageId()) .elem("ReceiptHandle", msg.getReceiptHandle()) .elem("MD5OfBody", msg.getMd5OfBody()); - if (msg.getMd5OfMessageAttributes() != null) { - xml.elem("MD5OfMessageAttributes", msg.getMd5OfMessageAttributes()); + String messageAttrsMd5 = Message.computeMessageAttributesMd5(selectedMessageAttrs); + if (messageAttrsMd5 != null) { + xml.elem("MD5OfMessageAttributes", messageAttrsMd5); } xml.elem("Body", msg.getBody()); writeSystemAttributesXml(xml, msg, requestedAttrs, senderId); - if (msg.getMessageAttributes() != null && !msg.getMessageAttributes().isEmpty()) { - for (Map.Entry entry : msg.getMessageAttributes().entrySet()) { + if (!selectedMessageAttrs.isEmpty()) { + for (Map.Entry entry : selectedMessageAttrs.entrySet()) { xml.start("MessageAttribute") .elem("Name", entry.getKey()) .start("Value") diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java b/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java index f698e25d74..b68ee7c91f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java @@ -151,9 +151,12 @@ public class Message { } public void updateMd5OfMessageAttributes() { + this.md5OfMessageAttributes = computeMessageAttributesMd5(messageAttributes); + } + + public static String computeMessageAttributesMd5(Map messageAttributes) { if (messageAttributes == null || messageAttributes.isEmpty()) { - this.md5OfMessageAttributes = null; - return; + return null; } try { MessageDigest md = MessageDigest.getInstance("MD5"); @@ -190,10 +193,10 @@ public class Message { for (byte b : digest) { sb.append(String.format("%02x", b)); } - this.md5OfMessageAttributes = sb.toString(); + return sb.toString(); } catch (Exception ignored) { // Attribute serialization failure falls back to null MD5 - this.md5OfMessageAttributes = null; + return null; } } @@ -211,4 +214,4 @@ public class Message { return ""; } } -} \ No newline at end of file +} 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 1f98c5a027..c63a13485d 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 @@ -861,7 +861,7 @@ class SnsIntegrationTest { .formParam("Action", "ReceiveMessage") .formParam("QueueUrl", rawDeliveryQueueUrl) .formParam("MaxNumberOfMessages", "1") - .formParam("MessageAttributeNames.member.1", "All") + .formParam("MessageAttributeName.1", "All") .when() .post("/") .then() diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java index 360fcf48f0..be8238ebc4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java @@ -107,6 +107,91 @@ class SqsIntegrationTest { SqsIntegrationTest.receiptHandle = receiptHandle; } + @Test + void receiveMessage_messageAttributeNamesFiltersOutput() { + String filterQueueUrl = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", "query-message-attribute-filter-queue") + .when().post("/").then().statusCode(200) + .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl"); + + try { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "SendMessage") + .formParam("QueueUrl", filterQueueUrl) + .formParam("MessageBody", "filter-test") + .formParam("MessageAttribute.1.Name", "color.primary") + .formParam("MessageAttribute.1.Value.DataType", "String") + .formParam("MessageAttribute.1.Value.StringValue", "red") + .formParam("MessageAttribute.2.Name", "secret") + .formParam("MessageAttribute.2.Value.DataType", "String") + .formParam("MessageAttribute.2.Value.StringValue", "private") + .formParam("MessageAttribute.3.Name", "color") + .formParam("MessageAttribute.3.Value.DataType", "String") + .formParam("MessageAttribute.3.Value.StringValue", "blue") + .formParam("MessageAttribute.4.Name", "colorful") + .formParam("MessageAttribute.4.Value.DataType", "String") + .formParam("MessageAttribute.4.Value.StringValue", "bright") + .when().post("/").then().statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", filterQueueUrl) + .formParam("VisibilityTimeout", "0") + .when().post("/").then().statusCode(200) + .body(containsString("filter-test")) + .body(not(containsString(""))) + .body(not(containsString(""))); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", filterQueueUrl) + .formParam("VisibilityTimeout", "0") + .formParam("MessageAttributeName.1", "color.primary") + .when().post("/").then().statusCode(200) + .body(containsString("color.primary")) + .body(not(containsString("color"))) + .body(not(containsString("colorful"))) + .body(not(containsString("secret"))) + .body(containsString("d1cf84dfbac1cbe5c78ed09a1768b0d9")); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", filterQueueUrl) + .formParam("VisibilityTimeout", "0") + .formParam("MessageAttributeName.1", "color.*") + .when().post("/").then().statusCode(200) + .body(containsString("color.primary")) + .body(containsString("color")) + .body(containsString("colorful")) + .body(not(containsString("secret"))) + .body(containsString("46db9885b8f221ea04082f03e3a63d14")); + + for (String allSelector : new String[]{"All", ".*"}) { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", filterQueueUrl) + .formParam("VisibilityTimeout", "0") + .formParam("MessageAttributeName.1", allSelector) + .when().post("/").then().statusCode(200) + .body(containsString("color.primary")) + .body(containsString("secret")); + } + } finally { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteQueue") + .formParam("QueueUrl", filterQueueUrl) + .when().post("/"); + } + } + private static String receiptHandle; @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java index 61a0f6c8a1..3ffc2e46c2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java @@ -359,6 +359,78 @@ class SqsJsonProtocolTest { .body("Messages[0].Attributes.ApproximateFirstReceiveTimestamp", notNullValue()); } + @Test + void receiveMessage_messageAttributeNamesFiltersOutput() { + String filterQueueUrl = given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.CreateQueue") + .body("{\"QueueName\":\"json-message-attribute-filter-queue\"}") + .when().post("/").then().statusCode(200) + .extract().jsonPath().getString("QueueUrl"); + + try { + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.SendMessage") + .body("{\"QueueUrl\":\"" + filterQueueUrl + "\"," + + "\"MessageBody\":\"filter-test\"," + + "\"MessageAttributes\":{" + + "\"color.primary\":{\"DataType\":\"String\",\"StringValue\":\"red\"}," + + "\"color\":{\"DataType\":\"String\",\"StringValue\":\"blue\"}," + + "\"colorful\":{\"DataType\":\"String\",\"StringValue\":\"bright\"}," + + "\"secret\":{\"DataType\":\"String\",\"StringValue\":\"private\"}}}") + .when().post("/").then().statusCode(200); + + String receiveBody = "{\"QueueUrl\":\"" + filterQueueUrl + "\",\"VisibilityTimeout\":0"; + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.ReceiveMessage") + .body(receiveBody + "}") + .when().post("/").then().statusCode(200) + .body("Messages", hasSize(1)) + .body("Messages[0]", not(hasKey("MessageAttributes"))) + .body("Messages[0]", not(hasKey("MD5OfMessageAttributes"))); + + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.ReceiveMessage") + .body(receiveBody + ",\"MessageAttributeNames\":[\"color.primary\"]}") + .when().post("/").then().statusCode(200) + .body("Messages[0].MessageAttributes", hasKey("color.primary")) + .body("Messages[0].MessageAttributes", not(hasKey("color"))) + .body("Messages[0].MessageAttributes", not(hasKey("colorful"))) + .body("Messages[0].MessageAttributes", not(hasKey("secret"))) + .body("Messages[0].MD5OfMessageAttributes", equalTo("d1cf84dfbac1cbe5c78ed09a1768b0d9")); + + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.ReceiveMessage") + .body(receiveBody + ",\"MessageAttributeNames\":[\"color.*\"]}") + .when().post("/").then().statusCode(200) + .body("Messages[0].MessageAttributes", hasKey("color.primary")) + .body("Messages[0].MessageAttributes", hasKey("color")) + .body("Messages[0].MessageAttributes", hasKey("colorful")) + .body("Messages[0].MessageAttributes", not(hasKey("secret"))) + .body("Messages[0].MD5OfMessageAttributes", equalTo("46db9885b8f221ea04082f03e3a63d14")); + + for (String allSelector : new String[]{"All", ".*"}) { + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.ReceiveMessage") + .body(receiveBody + ",\"MessageAttributeNames\":[\"" + allSelector + "\"]}") + .when().post("/").then().statusCode(200) + .body("Messages[0].MessageAttributes", hasKey("color.primary")) + .body("Messages[0].MessageAttributes.secret.StringValue", equalTo("private")); + } + } finally { + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.DeleteQueue") + .body("{\"QueueUrl\":\"" + filterQueueUrl + "\"}") + .when().post("/"); + } + } + @Test @Order(9) void sendMessageToStandardQueueRetainsMessageGroupId() {