From a3f3bf8fe1b998e065aea19f35b3f62e06a55746 Mon Sep 17 00:00:00 2001 From: Konda Balaji Rao Date: Thu, 25 Jun 2026 10:07:03 +0530 Subject: [PATCH] fix(lambda): populate SQS messageAttributes in Lambda event payload (#1422) * fix: return XML error for missing action and add volume action fallbacks * fix(eventbridge): sync EventBridge tags to ResourceGroupsTaggingService EventBridge stored tags on rules, buses, and archives in its own stores but never propagated them to the central ResourceGroupsTaggingService. This caused GetResources with --resource-type-filters events:rule to return an empty list. Inject ResourceGroupsTaggingService into EventBridgeService (following the existing GlueService pattern) and sync tags during putRule, tagResource, untagResource, and deleteRule operations. Closes #1343 * fix(lambda): populate SQS messageAttributes in Lambda event payload buildSqsEvent() was calling record.putObject("messageAttributes") which creates an empty JSON object node and discards msg.getMessageAttributes() entirely. Lambda functions receiving SQS events always saw an empty messageAttributes map regardless of what was sent with the message. Fix serializes each MessageAttributeValue into the AWS-specified shape: { dataType, stringValue, binaryValue, stringListValues, binaryListValues } Binary values are base64-encoded per the Lambda SQS event contract. md5OfMessageAttributes is now forwarded to the record when present. Added four new unit tests covering: String, Number, and Binary attribute types, multiple messages each with independent attributes, and the no-attributes case (backward compat: empty object, no md5 field). Fixes #1411 * fix: serialize only populated SQS message attribute values * fix: omit null scalar fields from SQS message attributes in Lambda event payload - Stop emitting explicit JSON nulls for absent stringValue/binaryValue fields in SqsEventSourcePoller.buildSqsEvent(); AWS Lambda omits these entirely rather than sending null. - Align serialization order with PipesPoller: check binaryValue first, then stringValue via else-if. - Update test assertions to expect key-absent (assertNull) instead of null-JSON-node (isNull) for the unpopulated scalar field. --- .../services/lambda/SqsEventSourcePoller.java | 20 ++- .../floci/services/pipes/PipesPoller.java | 3 +- .../lambda/SqsEventSourcePollerTest.java | 116 ++++++++++++++++++ 3 files changed, 137 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java index 6061706840..ca2d5c47e1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java @@ -285,8 +285,26 @@ public class SqsEventSourcePoller { attrs.put("SentTimestamp", String.valueOf(msg.getSentTimestamp().toEpochMilli())); attrs.put("SenderId", AwsArnUtils.accountOrDefault(esm.getEventSourceArn(), "000000000000")); attrs.put("ApproximateFirstReceiveTimestamp", String.valueOf(System.currentTimeMillis())); - record.putObject("messageAttributes"); + // Populate messageAttributes from the message model + ObjectNode msgAttrs = record.putObject("messageAttributes"); + if (msg.getMessageAttributes() != null) { + msg.getMessageAttributes().forEach((name, val) -> { + ObjectNode attrNode = msgAttrs.putObject(name); + attrNode.put("dataType", val.getDataType() != null ? val.getDataType() : "String"); + if (val.getBinaryValue() != null) { + attrNode.put("binaryValue", + java.util.Base64.getEncoder().encodeToString(val.getBinaryValue())); + } else if (val.getStringValue() != null) { + attrNode.put("stringValue", val.getStringValue()); + } + attrNode.putArray("stringListValues"); + attrNode.putArray("binaryListValues"); + }); + } record.put("md5OfBody", msg.getMd5OfBody() != null ? msg.getMd5OfBody() : ""); + if (msg.getMd5OfMessageAttributes() != null) { + record.put("md5OfMessageAttributes", msg.getMd5OfMessageAttributes()); + } record.put("eventSource", "aws:sqs"); record.put("eventSourceARN", esm.getEventSourceArn()); record.put("awsRegion", esm.getRegion()); diff --git a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesPoller.java b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesPoller.java index 856cefcd25..112b441a0e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesPoller.java +++ b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesPoller.java @@ -415,9 +415,10 @@ public class PipesPoller { for (Map.Entry entry : msg.getMessageAttributes().entrySet()) { ObjectNode attrNode = msgAttrs.putObject(entry.getKey()); MessageAttributeValue val = entry.getValue(); - attrNode.put("stringValue", val.getStringValue()); if (val.getBinaryValue() != null) { attrNode.put("binaryValue", Base64.getEncoder().encodeToString(val.getBinaryValue())); + } else if (val.getStringValue() != null) { + attrNode.put("stringValue", val.getStringValue()); } attrNode.putArray("stringListValues"); attrNode.putArray("binaryListValues"); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java index 96559e5fbe..c791dcaacd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePollerTest.java @@ -87,6 +87,122 @@ class SqsEventSourcePollerTest { assertEquals("aws:sqs", record.get("eventSource").asText()); assertEquals("arn:aws:sqs:us-east-1:123456789012:my-queue", record.get("eventSourceARN").asText()); assertEquals("us-east-1", record.get("awsRegion").asText()); + + // message with no attributes — messageAttributes must be an empty object, not absent + assertTrue(record.get("messageAttributes").isObject()); + assertEquals(0, record.get("messageAttributes").size()); + assertNull(record.get("md5OfMessageAttributes")); + } + + @Test + void buildSqsEventPopulatesStringMessageAttribute() throws Exception { + Message msg = new Message(); + msg.setBody("hello"); + msg.setSentTimestamp(Instant.now()); + + io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue attr = + new io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue("red", "String"); + msg.getMessageAttributes().put("color", attr); + msg.updateMd5OfMessageAttributes(); + + EventSourceMapping esm = new EventSourceMapping(); + esm.setEventSourceArn("arn:aws:sqs:us-east-1:123456789012:test-queue"); + esm.setRegion("us-east-1"); + + String event = poller.buildSqsEvent(List.of(msg), esm); + JsonNode record = OBJECT_MAPPER.readTree(event).get("Records").get(0); + JsonNode msgAttrs = record.get("messageAttributes"); + + assertTrue(msgAttrs.isObject(), "messageAttributes must be an object"); + assertEquals(1, msgAttrs.size()); + + JsonNode color = msgAttrs.get("color"); + assertNotNull(color, "color attribute must be present"); + assertEquals("String", color.get("dataType").asText()); + assertEquals("red", color.get("stringValue").asText()); + assertNull(color.get("binaryValue")); + assertTrue(color.get("stringListValues").isArray()); + assertTrue(color.get("binaryListValues").isArray()); + + // md5OfMessageAttributes must be propagated to the record + assertNotNull(record.get("md5OfMessageAttributes"), + "md5OfMessageAttributes must be present when attributes exist"); + } + + @Test + void buildSqsEventPopulatesNumberMessageAttribute() throws Exception { + Message msg = new Message(); + msg.setBody("order"); + msg.setSentTimestamp(Instant.now()); + + io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue attr = + new io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue("42", "Number"); + msg.getMessageAttributes().put("count", attr); + + EventSourceMapping esm = new EventSourceMapping(); + esm.setEventSourceArn("arn:aws:sqs:us-east-1:123456789012:num-queue"); + esm.setRegion("us-east-1"); + + String event = poller.buildSqsEvent(List.of(msg), esm); + JsonNode color = OBJECT_MAPPER.readTree(event) + .get("Records").get(0).get("messageAttributes").get("count"); + + assertEquals("Number", color.get("dataType").asText()); + assertEquals("42", color.get("stringValue").asText()); + } + + @Test + void buildSqsEventPopulatesBinaryMessageAttribute() throws Exception { + Message msg = new Message(); + msg.setBody("bin"); + msg.setSentTimestamp(Instant.now()); + + byte[] raw = {1, 2, 3}; + io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue attr = + new io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue(raw, "Binary"); + msg.getMessageAttributes().put("data", attr); + + EventSourceMapping esm = new EventSourceMapping(); + esm.setEventSourceArn("arn:aws:sqs:us-east-1:123456789012:bin-queue"); + esm.setRegion("us-east-1"); + + String event = poller.buildSqsEvent(List.of(msg), esm); + JsonNode data = OBJECT_MAPPER.readTree(event) + .get("Records").get(0).get("messageAttributes").get("data"); + + assertEquals("Binary", data.get("dataType").asText()); + assertNull(data.get("stringValue")); + assertEquals( + java.util.Base64.getEncoder().encodeToString(raw), + data.get("binaryValue").asText()); + } + + @Test + void buildSqsEventHandlesMultipleAttributesAcrossMultipleMessages() throws Exception { + io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue a1 = + new io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue("v1", "String"); + io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue a2 = + new io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue("v2", "String"); + + Message msg1 = new Message(); + msg1.setBody("m1"); + msg1.setSentTimestamp(Instant.now()); + msg1.getMessageAttributes().put("attr1", a1); + + Message msg2 = new Message(); + msg2.setBody("m2"); + msg2.setSentTimestamp(Instant.now()); + msg2.getMessageAttributes().put("attr2", a2); + + EventSourceMapping esm = new EventSourceMapping(); + esm.setEventSourceArn("arn:aws:sqs:us-east-1:123456789012:multi-queue"); + esm.setRegion("us-east-1"); + + String event = poller.buildSqsEvent(List.of(msg1, msg2), esm); + JsonNode records = OBJECT_MAPPER.readTree(event).get("Records"); + + assertEquals("v1", records.get(0).get("messageAttributes").get("attr1").get("stringValue").asText()); + assertEquals("v2", records.get(1).get("messageAttributes").get("attr2").get("stringValue").asText()); } @Test