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 e04d061c68..bf1962578a 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 @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; +import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.ecs.EcsService; import io.github.hectorvent.floci.services.ecs.model.LaunchType; import io.github.hectorvent.floci.services.eventbridge.EventBridgeService; @@ -20,6 +21,7 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; +import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -180,13 +182,47 @@ public class ScheduleInvoker { String body = text(params, "MessageBody"); String messageGroupId = text(params, "MessageGroupId"); String messageDeduplicationId = text(params, "MessageDeduplicationId"); - sqsService.sendMessage(queueUrl, body, 0, messageGroupId, messageDeduplicationId, region); + Map messageAttributes = + parseUniversalSqsMessageAttributes(params.path("MessageAttributes")); + sqsService.sendMessage(queueUrl, body, 0, messageGroupId, messageDeduplicationId, + messageAttributes, region); LOG.debugv("Scheduler delivered to SQS (universal target): {0}", queueUrl); } default -> LOG.warnv("Scheduler: unsupported universal target action: {0}", serviceAction); } } + private static Map parseUniversalSqsMessageAttributes(JsonNode attrsNode) { + Map attributes = new HashMap<>(); + if (attrsNode == null || !attrsNode.isObject()) { + return attributes; + } + attrsNode.fields().forEachRemaining(entry -> { + 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); + 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); + } + attributes.put(entry.getKey(), new MessageAttributeValue(binaryValue, dataType)); + } else if (stringValue != null) { + attributes.put(entry.getKey(), new MessageAttributeValue( + stringValue, dataType)); + } + }); + return attributes; + } + private static String text(JsonNode node, String field) { JsonNode value = node.get(field); return value != null && !value.isNull() ? value.asText() : null; 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 bf18943a2c..9e0b3a1169 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 @@ -20,10 +20,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; @@ -143,17 +147,83 @@ class ScheduleInvokerTest { } @Test - void universalSqsSendMessageReadsQueueUrlBodyAndGroupIdFromInput() { + void universalSqsSendMessageReadsQueueUrlBodyAndFifoIdsFromInputWithoutAttributes() { Target target = new Target(); target.setArn("arn:aws:scheduler:::aws-sdk:sqs:sendMessage"); target.setRoleArn("arn:aws:iam::000000000000:role/x"); target.setInput("{\"QueueUrl\":\"http://localhost:4566/000000000000/q.fifo\"," - + "\"MessageBody\":\"hi\",\"MessageGroupId\":\"g1\"}"); + + "\"MessageBody\":\"hi\",\"MessageGroupId\":\"g1\"," + + "\"MessageDeduplicationId\":\"dedup-1\"}"); invoker.invoke(target, "us-east-1"); verify(sqsService).sendMessage(eq("http://localhost:4566/000000000000/q.fifo"), - eq("hi"), eq(0), eq("g1"), isNull(), eq("us-east-1")); + eq("hi"), eq(0), eq("g1"), eq("dedup-1"), + argThat(attrs -> attrs == null || attrs.isEmpty()), eq("us-east-1")); + } + + @Test + void universalSqsSendMessageForwardsStringAndBase64BinaryMessageAttributes() { + String queueUrl = "http://localhost:4566/000000000000/q.fifo"; + String binaryValueBase64 = "aGVsbG8="; + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:sqs:sendMessage"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"QueueUrl\":\"" + queueUrl + "\"," + + "\"MessageBody\":\"hi\"," + + "\"MessageGroupId\":\"g1\"," + + "\"MessageDeduplicationId\":\"dedup-1\"," + + "\"MessageAttributes\":{" + + "\"StringAttr\":{\"DataType\":\"String.Custom\",\"StringValue\":\"value\"}," + + "\"BinaryAttr\":{\"DataType\":\"Binary.Custom\",\"BinaryValue\":\"" + + binaryValueBase64 + "\"}}}"); + + invoker.invoke(target, "us-east-1"); + + ArgumentCaptor> attributesCaptor = ArgumentCaptor.captor(); + verify(sqsService).sendMessage(eq(queueUrl), eq("hi"), eq(0), eq("g1"), eq("dedup-1"), + attributesCaptor.capture(), eq("us-east-1")); + + Map attributes = attributesCaptor.getValue(); + assertEquals(2, attributes.size()); + + MessageAttributeValue stringAttribute = attributes.get("StringAttr"); + assertNotNull(stringAttribute); + assertEquals("String.Custom", stringAttribute.getDataType()); + assertEquals("value", stringAttribute.getStringValue()); + assertNull(stringAttribute.getBinaryValue()); + + MessageAttributeValue binaryAttribute = attributes.get("BinaryAttr"); + assertNotNull(binaryAttribute); + assertEquals("Binary.Custom", binaryAttribute.getDataType()); + assertArrayEquals("hello".getBytes(StandardCharsets.UTF_8), binaryAttribute.getBinaryValue()); + assertNull(binaryAttribute.getStringValue()); + } + + @Test + void universalSqsSendMessageSkipsAttributesWithoutDataType() { + String queueUrl = "http://localhost:4566/000000000000/q.fifo"; + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:sqs:sendMessage"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"QueueUrl\":\"" + queueUrl + "\"," + + "\"MessageBody\":\"hi\"," + + "\"MessageGroupId\":\"g1\"," + + "\"MessageDeduplicationId\":\"dedup-1\"," + + "\"MessageAttributes\":{" + + "\"MissingType\":{\"StringValue\":\"ignored\"}," + + "\"Valid\":{\"DataType\":\"String\",\"StringValue\":\"value\"}" + + "}}"); + + invoker.invoke(target, "us-east-1"); + + ArgumentCaptor> attributesCaptor = ArgumentCaptor.captor(); + verify(sqsService).sendMessage(eq(queueUrl), eq("hi"), eq(0), eq("g1"), eq("dedup-1"), + attributesCaptor.capture(), eq("us-east-1")); + + Map attributes = attributesCaptor.getValue(); + assertEquals(1, attributes.size()); + assertEquals("value", attributes.get("Valid").getStringValue()); } @Test