mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 02:07:58 -05:00
feat(pipes): apply EventBridge Pipe enrichment before target delivery (#1658)
* feat(pipes): apply EventBridge Pipe enrichment before target delivery
PipesPoller delivered the raw source record straight to the target; pipe.getEnrichment() was
stored but never invoked. Implement the AWS Pipes source->filter->ENRICHMENT->target flow: when an
enrichment Lambda is configured, invoke it once with the filtered events (bare JSON array) and
forward its response to the target (null/empty response skips the target, per AWS). Also resolve
qualified Lambda ARNs (:$LATEST) in PipesTargetInvoker. Covered by PipesTargetInvokerTest.
* fix(pipes): deliver enrichment result to the target as an event array
EventBridge Pipes delivers events to a target as a batch (JSON array). The
enrichment path forwarded a single-object enrichment response verbatim, so a
target expecting an array (e.g. a Step Functions state machine with
InputPath "$.[0]") received an object and resolved null. Wrap a non-array
enrichment response in a one-element array before target delivery. Covered by
PipesPollerTest.asEventArray*.
(cherry picked from commit f160f66857c2362e3f1bad3bbfdb025396b281a0)
* fix(pipes): treat a target/enrichment Lambda FunctionError as failed delivery
invokeLambda (the Lambda target path) discarded the InvokeResult, so a target
Lambda that returned a FunctionError (unhandled exception, throttle) was treated
as a successful delivery and the source record was consumed. Surface it (throw)
so the caller routes the record to the DLQ. Also switch applyEnrichment's
FunctionError failure from a bare RuntimeException to AwsException, per the
service-layer convention. Covered by PipesTargetInvokerTest.invoke_lambdaTargetFunctionErrorThrows.
* fix(pipes): only array-wrap the enrichment response for Lambda targets
deliverEnrichedBatch wrapped the enrichment response in a JSON batch array before invoking
the target unconditionally. That is correct for a Lambda target (which expects an
SQSRecord[]-style batch) but corrupts every other target — a Step Functions execution, SQS
message, SNS publish or EventBridge event must receive the raw enrichment response. Gate the
wrapping on isLambdaTarget, matching the non-enrichment delivery path. Adds pollSqs tests for
both a non-Lambda (raw) and a Lambda (wrapped) target.
* fix(pipes): align enrichment empty/unsupported handling with AWS + document SQS-only wiring
Review feedback:
- applyEnrichment now also skips the target when the Lambda enrichment returns an
empty object {} or empty array [] (whitespace variants included), matching AWS;
a non-empty array such as [{}] still invokes the target.
- A non-Lambda enrichment type (API destination, API Gateway, SFN Express) now
fails the batch to the DLQ instead of silently delivering the unenriched payload.
- Documented that enrichment is currently applied only on the SQS source path
(Kinesis/DynamoDB Streams/Kafka bypass it) in code and docs/services/pipes.md.
- Generalized a comment that named a specific downstream consumer.
- Tests: empty {}/[] skip, [{}] invokes, unsupported type throws.
* docs: regenerate action tables
This commit is contained in:
1 parent
45ed00ebc7
commit
0e85ac92c7
5 files changed
+383
-3
No files matched your search
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.dynamodb.DynamoDbStreamService;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import io.github.hectorvent.floci.services.pipes.model.Pipe;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import io.vertx.core.Vertx;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||
@@ -27,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -57,6 +59,91 @@ class PipesPollerTest {
|
||||
kafkaConsumerManager, targetInvoker, new PipesFilterMatcher(MAPPER), MAPPER, config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void asEventArrayWrapsSingleObjectInBatchArray() throws Exception {
|
||||
// Pipes delivers events to a target as a batch array; a single-object enrichment response
|
||||
// must become a one-element array so a target like "InputPath": "$.[0]" can unwrap it.
|
||||
String wrapped = PipesPoller.asEventArray(MAPPER, "{\"systemId\":\"S1\",\"solutions\":[1]}");
|
||||
JsonNode node = MAPPER.readTree(wrapped);
|
||||
assertTrue(node.isArray());
|
||||
assertEquals(1, node.size());
|
||||
assertEquals("S1", node.get(0).path("systemId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void asEventArrayLeavesArrayResponseUnchanged() throws Exception {
|
||||
String out = PipesPoller.asEventArray(MAPPER, "[{\"a\":1},{\"a\":2}]");
|
||||
JsonNode node = MAPPER.readTree(out);
|
||||
assertTrue(node.isArray());
|
||||
assertEquals(2, node.size());
|
||||
assertEquals(2, node.get(1).path("a").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void asEventArrayWrapsNonJsonAsSingleStringEvent() throws Exception {
|
||||
String out = PipesPoller.asEventArray(MAPPER, "not-json");
|
||||
JsonNode node = MAPPER.readTree(out);
|
||||
assertTrue(node.isArray());
|
||||
assertEquals(1, node.size());
|
||||
assertEquals("not-json", node.get(0).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pollSqs_enrichmentToNonLambdaTargetForwardsRawResponse() throws Exception {
|
||||
// A non-Lambda target (here Step Functions) must receive the raw enrichment response, not a
|
||||
// one-element batch array — array-wrapping would start the execution with [{...}] instead of {...}.
|
||||
Pipe pipe = new Pipe();
|
||||
pipe.setName("enrich-sfn");
|
||||
pipe.setSource("arn:aws:sqs:us-east-1:000000000000:src-queue");
|
||||
pipe.setEnrichment("arn:aws:lambda:us-east-1:000000000000:function:enrich");
|
||||
pipe.setTarget("arn:aws:states:us-east-1:000000000000:stateMachine:tgt");
|
||||
|
||||
Message msg = new Message("{\"orderId\":\"o1\"}");
|
||||
msg.setMessageId("m1");
|
||||
msg.setReceiptHandle("rh1");
|
||||
when(sqsService.receiveMessage(anyString(), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
|
||||
.thenReturn(List.of(msg));
|
||||
when(targetInvoker.applyEnrichment(eq(pipe), anyString(), eq("us-east-1")))
|
||||
.thenReturn("{\"systemId\":\"S1\"}");
|
||||
|
||||
poller.pollSqs(pipe, "us-east-1");
|
||||
|
||||
ArgumentCaptor<String> payload = ArgumentCaptor.forClass(String.class);
|
||||
verify(targetInvoker).invoke(eq(pipe), payload.capture(), eq("us-east-1"));
|
||||
JsonNode delivered = MAPPER.readTree(payload.getValue());
|
||||
assertFalse(delivered.isArray(),
|
||||
"non-Lambda target must receive the raw enrichment response, not a batch array");
|
||||
assertEquals("S1", delivered.path("systemId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pollSqs_enrichmentToLambdaTargetWrapsResponseInBatchArray() throws Exception {
|
||||
// A Lambda target expects the SQSRecord[]-style batch, so a single-object enrichment response
|
||||
// is wrapped in a one-element array.
|
||||
Pipe pipe = new Pipe();
|
||||
pipe.setName("enrich-lambda");
|
||||
pipe.setSource("arn:aws:sqs:us-east-1:000000000000:src-queue");
|
||||
pipe.setEnrichment("arn:aws:lambda:us-east-1:000000000000:function:enrich");
|
||||
pipe.setTarget("arn:aws:lambda:us-east-1:000000000000:function:tgt");
|
||||
|
||||
Message msg = new Message("{\"orderId\":\"o1\"}");
|
||||
msg.setMessageId("m1");
|
||||
msg.setReceiptHandle("rh1");
|
||||
when(sqsService.receiveMessage(anyString(), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
|
||||
.thenReturn(List.of(msg));
|
||||
when(targetInvoker.applyEnrichment(eq(pipe), anyString(), eq("us-east-1")))
|
||||
.thenReturn("{\"systemId\":\"S1\"}");
|
||||
|
||||
poller.pollSqs(pipe, "us-east-1");
|
||||
|
||||
ArgumentCaptor<String> payload = ArgumentCaptor.forClass(String.class);
|
||||
verify(targetInvoker).invoke(eq(pipe), payload.capture(), eq("us-east-1"));
|
||||
JsonNode delivered = MAPPER.readTree(payload.getValue());
|
||||
assertTrue(delivered.isArray(), "Lambda target must receive the batch array shape");
|
||||
assertEquals(1, delivered.size());
|
||||
assertEquals("S1", delivered.get(0).path("systemId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pollKafka_filtersUsingDecodedPayloadButDeliversOriginalRecord() throws Exception {
|
||||
Pipe pipe = selfManagedKafkaPipe();
|
||||
|
||||
@@ -248,4 +248,113 @@ class PipesTargetInvokerTest {
|
||||
"{\"message\": \"user registered\", \"messageType\": \"REGISTRATION\"}",
|
||||
captor.getValue());
|
||||
}
|
||||
|
||||
// --- Enrichment: AWS Pipes invokes the enrichment Lambda with the events and forwards its response ---
|
||||
|
||||
private Pipe enrichmentPipe(String region, String enrichmentArn) {
|
||||
Pipe pipe = createPipe("arn:aws:states:" + region + ":000000000000:stateMachine:target", null);
|
||||
pipe.setEnrichment(enrichmentArn);
|
||||
return pipe;
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyEnrichment_invokesLambdaAndReturnsResponse() {
|
||||
String region = "us-east-1";
|
||||
Pipe pipe = enrichmentPipe(region, "arn:aws:lambda:" + region + ":000000000000:function:enrich-fn");
|
||||
byte[] resp = "{\"systemId\":\"S1\"}".getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
when(lambdaService.invoke(eq(region), eq("enrich-fn"), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new io.github.hectorvent.floci.services.lambda.model.InvokeResult(200, null, resp, null, "req"));
|
||||
|
||||
String out = invoker.applyEnrichment(pipe, "[{\"body\":\"{}\"}]", region);
|
||||
|
||||
assertEquals("{\"systemId\":\"S1\"}", out);
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(lambdaService).invoke(eq(region), eq("enrich-fn"), payloadCaptor.capture(), eq(InvocationType.RequestResponse));
|
||||
assertEquals("[{\"body\":\"{}\"}]",
|
||||
new String(payloadCaptor.getValue(), java.nio.charset.StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyEnrichment_qualifiedArnResolvesFunctionName() {
|
||||
String region = "us-east-1";
|
||||
Pipe pipe = enrichmentPipe(region, "arn:aws:lambda:" + region + ":000000000000:function:enrich-fn:$LATEST");
|
||||
when(lambdaService.invoke(eq(region), eq("enrich-fn"), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new io.github.hectorvent.floci.services.lambda.model.InvokeResult(200, null, "{}".getBytes(), null, "req"));
|
||||
|
||||
invoker.applyEnrichment(pipe, "[]", region);
|
||||
|
||||
verify(lambdaService).invoke(eq(region), eq("enrich-fn"), any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyEnrichment_noEnrichmentReturnsPayloadUnchanged() {
|
||||
Pipe pipe = createPipe("arn:aws:states:us-east-1:000000000000:stateMachine:target", null);
|
||||
String out = invoker.applyEnrichment(pipe, "[{\"x\":1}]", "us-east-1");
|
||||
assertEquals("[{\"x\":1}]", out);
|
||||
verifyNoInteractions(lambdaService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyEnrichment_emptyResponseReturnsNull() {
|
||||
String region = "us-east-1";
|
||||
Pipe pipe = enrichmentPipe(region, "arn:aws:lambda:" + region + ":000000000000:function:enrich-fn");
|
||||
when(lambdaService.invoke(eq(region), eq("enrich-fn"), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new io.github.hectorvent.floci.services.lambda.model.InvokeResult(200, null, new byte[0], null, "req"));
|
||||
assertNull(invoker.applyEnrichment(pipe, "[]", region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyEnrichment_emptyObjectOrArrayResponseSkipsTarget() {
|
||||
// AWS skips the target when the enrichment returns {} or [] (whitespace variants included).
|
||||
String region = "us-east-1";
|
||||
Pipe pipe = enrichmentPipe(region, "arn:aws:lambda:" + region + ":000000000000:function:enrich-fn");
|
||||
for (String body : new String[]{"{}", "[]", " { } ", "[ ]"}) {
|
||||
when(lambdaService.invoke(eq(region), eq("enrich-fn"), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new io.github.hectorvent.floci.services.lambda.model.InvokeResult(
|
||||
200, null, body.getBytes(java.nio.charset.StandardCharsets.UTF_8), null, "req"));
|
||||
assertNull(invoker.applyEnrichment(pipe, "[]", region),
|
||||
"enrichment response " + body + " should skip the target");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyEnrichment_singleElementArrayResponseInvokesTarget() {
|
||||
// [{}] is the explicit "invoke the target with an empty-payload element" form — not skipped.
|
||||
String region = "us-east-1";
|
||||
Pipe pipe = enrichmentPipe(region, "arn:aws:lambda:" + region + ":000000000000:function:enrich-fn");
|
||||
when(lambdaService.invoke(eq(region), eq("enrich-fn"), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new io.github.hectorvent.floci.services.lambda.model.InvokeResult(
|
||||
200, null, "[{}]".getBytes(java.nio.charset.StandardCharsets.UTF_8), null, "req"));
|
||||
assertEquals("[{}]", invoker.applyEnrichment(pipe, "[]", region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyEnrichment_unsupportedTypeThrows() {
|
||||
// Non-Lambda enrichment (e.g. an API destination) is not emulated — applyEnrichment must
|
||||
// fail so the batch is routed to the DLQ rather than delivered unenriched.
|
||||
String region = "us-east-1";
|
||||
Pipe pipe = enrichmentPipe(region, "arn:aws:events:" + region + ":000000000000:api-destination/foo");
|
||||
assertThrows(RuntimeException.class, () -> invoker.applyEnrichment(pipe, "[{\"x\":1}]", region));
|
||||
verifyNoInteractions(lambdaService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyEnrichment_lambdaFunctionErrorThrows() {
|
||||
String region = "us-east-1";
|
||||
Pipe pipe = enrichmentPipe(region, "arn:aws:lambda:" + region + ":000000000000:function:enrich-fn");
|
||||
when(lambdaService.invoke(eq(region), eq("enrich-fn"), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new io.github.hectorvent.floci.services.lambda.model.InvokeResult(200, "Unhandled", "boom".getBytes(), null, "req"));
|
||||
assertThrows(RuntimeException.class, () -> invoker.applyEnrichment(pipe, "[]", region));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invoke_lambdaTargetFunctionErrorThrows() {
|
||||
// A target Lambda that returns a FunctionError is a failed delivery — invoke must throw so the
|
||||
// poller routes the source record to the DLQ instead of silently deleting it.
|
||||
String region = "us-east-1";
|
||||
Pipe pipe = createPipe("arn:aws:lambda:" + region + ":000000000000:function:my-fn", null);
|
||||
when(lambdaService.invoke(eq(region), eq("my-fn"), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new io.github.hectorvent.floci.services.lambda.model.InvokeResult(200, "Unhandled", "boom".getBytes(), null, "req"));
|
||||
assertThrows(RuntimeException.class, () -> invoker.invoke(pipe, "{}", region));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user