Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(scheduler): do not base64-decode BinaryValue in universal sqs target (#5314)

Mathieu Charpeneld0c2774564e4
diff --git a/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java b/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java
index bb40c06264..82671421b2 100644
--- a/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java
+++ b/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java
@@ -31,7 +31,6 @@ import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
-import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@@ -531,19 +530,15 @@ public class ScheduleInvoker {
JsonNode valueNode = entry.getValue();
String dataType = valueNode.path("DataType").asText(null);
String stringValue = valueNode.path("StringValue").asText(null);
- String binaryValueBase64 = valueNode.path("BinaryValue").asText(null);
+ String binaryValueText = valueNode.path("BinaryValue").asText(null);
if (dataType == null) {
return;
}
- 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);
- }
+ if (binaryValueText != null) {
+ // Unlike the SQS JSON protocol, AWS does not base64-decode BinaryValue in a universal
+ // target Input: the attribute bytes are the UTF-8 encoding of the JSON string, and a
+ // base64 string is delivered as its own text.
+ byte[] binaryValue = binaryValueText.getBytes(StandardCharsets.UTF_8);
attributes.put(entry.getKey(), new MessageAttributeValue(binaryValue, dataType));
} else if (stringValue != null) {
attributes.put(entry.getKey(), new MessageAttributeValue(
diff --git a/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java b/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java
index fe5319b8f1..74c3b41a3d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java
@@ -336,8 +336,9 @@ class ScheduleInvokerTest {
}
@Test
- void universalSqsSendMessageForwardsStringAndBase64BinaryMessageAttributes() {
+ void universalSqsSendMessageForwardsStringAndBinaryMessageAttributesWithoutBase64Decoding() {
String queueUrl = "http://localhost:4566/000000000000/q.fifo";
+ // AWS delivers a base64-looking BinaryValue as its own text, it does not decode it
String binaryValueBase64 = "aGVsbG8=";
Target target = new Target();
target.setArn("arn:aws:scheduler:::aws-sdk:sqs:sendMessage");
@@ -369,7 +370,7 @@ class ScheduleInvokerTest {
MessageAttributeValue binaryAttribute = attributes.get("BinaryAttr");
assertNotNull(binaryAttribute);
assertEquals("Binary.Custom", binaryAttribute.getDataType());
- assertArrayEquals("hello".getBytes(StandardCharsets.UTF_8), binaryAttribute.getBinaryValue());
+ assertArrayEquals(binaryValueBase64.getBytes(StandardCharsets.UTF_8), binaryAttribute.getBinaryValue());
assertNull(binaryAttribute.getStringValue());
}
@@ -504,6 +505,29 @@ class ScheduleInvokerTest {
verifyNoInteractions(redshiftDataService);
}
+ @Test
+ void universalSqsSendMessageUsesUtf8BytesOfRawBinaryValue() {
+ String queueUrl = "http://localhost:4566/000000000000/q";
+ Target target = new Target();
+ target.setArn("arn:aws:scheduler:::aws-sdk:sqs:sendMessage");
+ target.setRoleArn("arn:aws:iam::000000000000:role/x");
+ // Protobuf-like payload with control characters plus U+0080 and U+00FF, as JSON escapes
+ target.setInput("{\"QueueUrl\":\"" + queueUrl + "\","
+ + "\"MessageBody\":\"hi\","
+ + "\"MessageAttributes\":{"
+ + "\"BinaryAttr\":{\"DataType\":\"Binary\",\"BinaryValue\":\"\\nA\\u0080\\u0001\\u00ff\\u0000B\"}}}");
+
+ invoke(target, "us-east-1");
+
+ ArgumentCaptor<Map<String, MessageAttributeValue>> attributesCaptor = ArgumentCaptor.captor();
+ verify(sqsService).sendMessage(eq(queueUrl), eq("hi"), eq(0), isNull(), isNull(),
+ attributesCaptor.capture(), eq("us-east-1"));
+
+ // Same bytes real AWS delivers for this Input: the UTF-8 encoding of the decoded JSON string
+ byte[] expected = {0x0a, 0x41, (byte) 0xc2, (byte) 0x80, 0x01, (byte) 0xc3, (byte) 0xbf, 0x00, 0x42};
+ assertArrayEquals(expected, attributesCaptor.getValue().get("BinaryAttr").getBinaryValue());
+ }
+
@Test
void universalSqsSendMessageSkipsAttributesWithoutDataType() {
String queueUrl = "http://localhost:4566/000000000000/q.fifo";