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 9ec8fb7469..0a42c6fa5f 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 @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.sns; import io.github.hectorvent.floci.core.common.AwsErrorResponse; +import io.github.hectorvent.floci.core.common.AwsException; 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; @@ -13,6 +14,7 @@ import jakarta.inject.Inject; import jakarta.ws.rs.core.Response; import java.util.ArrayList; +import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -156,15 +158,7 @@ public class SnsJsonHandler { String message = request.path("Message").asText(null); String subject = request.path("Subject").asText(null); - Map attributes = new HashMap<>(); - JsonNode attrsNode = request.path("MessageAttributes"); - if (attrsNode.isObject()) { - attrsNode.fields().forEachRemaining(entry -> { - String dataType = entry.getValue().path("DataType").asText("String"); - String stringValue = entry.getValue().path("StringValue").asText(); - attributes.put(entry.getKey(), new MessageAttributeValue(stringValue, dataType)); - }); - } + Map attributes = parseMessageAttributes(request.path("MessageAttributes")); String messageId = snsService.publish(topicArn, targetArn, phoneNumber, message, subject, attributes, region); ObjectNode response = objectMapper.createObjectNode(); @@ -228,13 +222,7 @@ public class SnsJsonHandler { JsonNode attrsNode = entryNode.path("MessageAttributes"); if (attrsNode.isObject()) { - Map messageAttributes = new HashMap<>(); - attrsNode.fields().forEachRemaining(field -> { - String dataType = field.getValue().path("DataType").asText("String"); - String stringValue = field.getValue().path("StringValue").asText(); - messageAttributes.put(field.getKey(), new MessageAttributeValue(stringValue, dataType)); - }); - entry.put("MessageAttributes", messageAttributes); + entry.put("MessageAttributes", parseMessageAttributes(attrsNode)); } entries.add(entry); @@ -302,6 +290,33 @@ public class SnsJsonHandler { return node; } + private Map parseMessageAttributes(JsonNode attrsNode) { + Map attributes = new HashMap<>(); + if (!attrsNode.isObject()) { + return attributes; + } + attrsNode.fields().forEachRemaining(entry -> { + JsonNode valueNode = entry.getValue(); + String binaryValueBase64 = valueNode.path("BinaryValue").asText(null); + String defaultDataType = binaryValueBase64 != null ? "Binary" : "String"; + String dataType = valueNode.path("DataType").asText(defaultDataType); + if (binaryValueBase64 != null) { + byte[] binaryValue; + try { + binaryValue = Base64.getDecoder().decode(binaryValueBase64); + } catch (IllegalArgumentException e) { + throw new AwsException("InvalidParameterValue", + "Invalid binary value for message attribute '" + entry.getKey() + "': not valid base64.", 400); + } + attributes.put(entry.getKey(), new MessageAttributeValue(binaryValue, dataType)); + } else { + String stringValue = valueNode.path("StringValue").asText(); + attributes.put(entry.getKey(), new MessageAttributeValue(stringValue, dataType)); + } + }); + return attributes; + } + private Map jsonNodeToMap(JsonNode node) { Map map = new HashMap<>(); if (node != null && node.isObject()) { 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 182ce63766..7a7a57060f 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 @@ -11,6 +11,7 @@ import jakarta.ws.rs.core.Response; import org.jboss.logging.Logger; import java.util.ArrayList; +import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -183,13 +184,11 @@ public class SnsQueryHandler { String messageGroupId = getParam(params, "MessageGroupId"); String messageDeduplicationId = getParam(params, "MessageDeduplicationId"); - 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"); - String dataType = params.getFirst("MessageAttributes.entry." + i + ".Value.DataType"); - if (value != null) attributes.put(name, new MessageAttributeValue(value, dataType != null ? dataType : "String")); + Map attributes; + try { + attributes = parseMessageAttributes(params, "MessageAttributes.entry."); + } catch (AwsException e) { + return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); } try { @@ -217,14 +216,11 @@ public class SnsQueryHandler { entry.put("MessageGroupId", getParam(params, entryPrefix + ".MessageGroupId")); entry.put("MessageDeduplicationId", getParam(params, entryPrefix + ".MessageDeduplicationId")); - Map attributes = new HashMap<>(); - for (int j = 1; ; j++) { - String attrPrefix = entryPrefix + ".MessageAttributes.entry." + j; - String name = params.getFirst(attrPrefix + ".Name"); - if (name == null) break; - String value = params.getFirst(attrPrefix + ".Value.StringValue"); - String dataType = params.getFirst(attrPrefix + ".Value.DataType"); - if (value != null) attributes.put(name, new MessageAttributeValue(value, dataType != null ? dataType : "String")); + Map attributes; + try { + attributes = parseMessageAttributes(params, entryPrefix + ".MessageAttributes.entry."); + } catch (AwsException e) { + return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus()); } if (!attributes.isEmpty()) entry.put("MessageAttributes", attributes); entries.add(entry); @@ -361,6 +357,31 @@ public class SnsQueryHandler { return params.getFirst(name); } + private Map parseMessageAttributes( + MultivaluedMap params, String prefix) { + Map attributes = new HashMap<>(); + for (int i = 1; ; i++) { + String name = params.getFirst(prefix + i + ".Name"); + if (name == null) break; + String value = params.getFirst(prefix + i + ".Value.StringValue"); + String binaryValueBase64 = params.getFirst(prefix + i + ".Value.BinaryValue"); + String dataType = params.getFirst(prefix + i + ".Value.DataType"); + if (binaryValueBase64 != null) { + byte[] binaryValue; + try { + binaryValue = Base64.getDecoder().decode(binaryValueBase64); + } catch (IllegalArgumentException e) { + throw new AwsException("InvalidParameterValue", + "Invalid binary value for message attribute '" + name + "': not valid base64.", 400); + } + attributes.put(name, new MessageAttributeValue(binaryValue, dataType != null ? dataType : "Binary")); + } else if (value != null) { + attributes.put(name, new MessageAttributeValue(value, dataType != null ? dataType : "String")); + } + } + return attributes; + } + Response xmlErrorResponse(String code, String message, int status) { return AwsQueryResponse.error(code, message, AwsNamespaces.SNS, status); } 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 23d7c00a9d..c000198b2d 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 @@ -32,6 +32,7 @@ import java.time.Duration; import java.time.Instant; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.Base64; import java.util.Collections; import java.util.HexFormat; import java.util.List; @@ -955,7 +956,12 @@ public class SnsService { for (var entry : messageAttributes.entrySet()) { ObjectNode attr = attrs.putObject(entry.getKey()); attr.put("Type", entry.getValue().getDataType()); - attr.put("Value", entry.getValue().getStringValue()); + if (entry.getValue().getBinaryValue() != null) { + attr.put("Value", Base64.getEncoder() + .encodeToString(entry.getValue().getBinaryValue())); + } else { + attr.put("Value", entry.getValue().getStringValue()); + } } } ObjectNode record = objectMapper.createObjectNode(); @@ -1011,7 +1017,12 @@ public class SnsService { for (var entry : messageAttributes.entrySet()) { ObjectNode attr = attrs.putObject(entry.getKey()); attr.put("Type", entry.getValue().getDataType()); - attr.put("Value", entry.getValue().getStringValue()); + if (entry.getValue().getBinaryValue() != null) { + attr.put("Value", Base64.getEncoder() + .encodeToString(entry.getValue().getBinaryValue())); + } else { + attr.put("Value", entry.getValue().getStringValue()); + } } } return objectMapper.writeValueAsString(node); @@ -1044,7 +1055,7 @@ public class SnsService { ObjectNode attr = attrs.putObject(entry.getKey()); attr.put("Type", entry.getValue().getDataType()); if (entry.getValue().getBinaryValue() != null) { - attr.put("Value", java.util.Base64.getEncoder() + attr.put("Value", Base64.getEncoder() .encodeToString(entry.getValue().getBinaryValue())); } else { attr.put("Value", entry.getValue().getStringValue()); 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 47d50c46a4..40390e056e 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 @@ -211,6 +211,68 @@ class SnsIntegrationTest { assertTrue(jsonBodyInResponse.contains("\"Timestamp\"")); } + @Test + @Order(18) + void publish_fanOutPreservesBinaryMessageAttribute() { + drainQueue(sqsQueueUrl); + + // "somebinarydata" base64-encoded -> c29tZWJpbmFyeWRhdGE= + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "Publish") + .formParam("TopicArn", topicArn) + .formParam("Message", "hello") + .formParam("MessageAttributes.entry.1.Name", "content") + .formParam("MessageAttributes.entry.1.Value.DataType", "String") + .formParam("MessageAttributes.entry.1.Value.StringValue", "somecontent") + .formParam("MessageAttributes.entry.2.Name", "binarycontent") + .formParam("MessageAttributes.entry.2.Value.DataType", "Binary") + .formParam("MessageAttributes.entry.2.Value.BinaryValue", "c29tZWJpbmFyeWRhdGE=") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("")); + + String jsonBodyInResponse = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", sqsQueueUrl) + .formParam("MaxNumberOfMessages", "1") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("hello")) + .extract().xmlPath().getString( + "ReceiveMessageResponse.ReceiveMessageResult.Message.Body"); + + assertTrue(jsonBodyInResponse.contains("\"binarycontent\""), + "Binary attribute should be present in delivered envelope: " + jsonBodyInResponse); + assertTrue(jsonBodyInResponse.contains("\"Binary\""), + "Binary attribute Type should be present: " + jsonBodyInResponse); + assertTrue(jsonBodyInResponse.contains("c29tZWJpbmFyeWRhdGE="), + "Binary attribute Value should be base64-encoded: " + jsonBodyInResponse); + } + + @Test + @Order(19) + void publish_invalidBinaryAttributeReturns400() { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "Publish") + .formParam("TopicArn", topicArn) + .formParam("Message", "hello") + .formParam("MessageAttributes.entry.1.Name", "binarycontent") + .formParam("MessageAttributes.entry.1.Value.DataType", "Binary") + .formParam("MessageAttributes.entry.1.Value.BinaryValue", "not valid base64!!!") + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("InvalidParameterValue")); + } + @Test @Order(10) void publishBatch() {