mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(eventbridge): deliver events to Firehose delivery stream targets (#1739)
* feat(eventbridge): deliver events to Firehose delivery stream targets EventBridgeInvoker dispatched Lambda, SQS, SNS, and Batch targets but silently dropped Firehose-targeted events with an unsupported-ARN warning. Add the :firehose: branch: the (input-transformed) event JSON becomes the record Data verbatim, matching AWS, which appends no newline; the stream's NDJSON flush handles separation. Covered by invoker unit tests and an end-to-end PutEvents to rule to Firehose to S3 integration test. Refs #1043 * refactor(eventbridge): use the Record constructor in the Firehose branch
This commit is contained in:
1 parent
070b43d0f7
commit
94a2ff3993
3 files changed
+175
-1
No files matched your search
+121
@@ -0,0 +1,121 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import io.github.hectorvent.floci.services.firehose.FirehoseService;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* End to end EventBridge → Firehose target delivery: rule matches a PutEvents event,
|
||||
* the invoker puts the event JSON as a Firehose record, and the stream's S3 flush
|
||||
* lands it in the destination bucket.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class EventBridgeFirehoseTargetIntegrationTest {
|
||||
|
||||
private static final String BUCKET = "eb-firehose-target-bucket";
|
||||
private static final String STREAM = "eb-firehose-target-stream";
|
||||
private static final String RULE = "eb-firehose-target-rule";
|
||||
private static final String JSON_CT = "application/x-amz-json-1.1";
|
||||
|
||||
@Inject
|
||||
FirehoseService firehoseService;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void setupBucketStreamAndRule() {
|
||||
given().when().put("/" + BUCKET).then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType(JSON_CT)
|
||||
.header("X-Amz-Target", "Firehose_20150804.CreateDeliveryStream")
|
||||
.body("""
|
||||
{
|
||||
"DeliveryStreamName": "%s",
|
||||
"ExtendedS3DestinationConfiguration": {
|
||||
"RoleARN": "arn:aws:iam::000000000000:role/firehose-delivery-role",
|
||||
"BucketARN": "arn:aws:s3:::%s",
|
||||
"Prefix": "events/"
|
||||
}
|
||||
}
|
||||
""".formatted(STREAM, BUCKET))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType(JSON_CT)
|
||||
.header("X-Amz-Target", "AWSEvents.PutRule")
|
||||
.body("""
|
||||
{
|
||||
"Name": "%s",
|
||||
"EventPattern": "{\\"source\\": [\\"local.orders\\"]}"
|
||||
}
|
||||
""".formatted(RULE))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType(JSON_CT)
|
||||
.header("X-Amz-Target", "AWSEvents.PutTargets")
|
||||
.body("""
|
||||
{
|
||||
"Rule": "%s",
|
||||
"Targets": [{
|
||||
"Id": "firehose-target",
|
||||
"Arn": "arn:aws:firehose:us-east-1:000000000000:deliverystream/%s"
|
||||
}]
|
||||
}
|
||||
""".formatted(RULE, STREAM))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("FailedEntryCount", equalTo(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void putEventIsDeliveredToS3ThroughFirehose() {
|
||||
given()
|
||||
.contentType(JSON_CT)
|
||||
.header("X-Amz-Target", "AWSEvents.PutEvents")
|
||||
.body("""
|
||||
{
|
||||
"Entries": [{
|
||||
"Source": "local.orders",
|
||||
"DetailType": "OrderPlaced",
|
||||
"Detail": "{\\"orderId\\": \\"order-eb-fh-1\\"}"
|
||||
}]
|
||||
}
|
||||
""")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("FailedEntryCount", equalTo(0));
|
||||
|
||||
// The record sits in the stream buffer until size/interval flush; force it so
|
||||
// the assertion is deterministic (same mechanism the scheduled flusher uses).
|
||||
firehoseService.flush(STREAM);
|
||||
|
||||
String key = given().when().get("/" + BUCKET + "?prefix=events/")
|
||||
.then().statusCode(200)
|
||||
.extract().xmlPath().getString("ListBucketResult.Contents[0].Key");
|
||||
|
||||
given().when().get("/" + BUCKET + "/" + key)
|
||||
.then().statusCode(200)
|
||||
.body(containsString("order-eb-fh-1"))
|
||||
.body(containsString("\"detail-type\":\"OrderPlaced\""));
|
||||
}
|
||||
}
|
||||
+36
@@ -6,14 +6,19 @@ import io.github.hectorvent.floci.services.batch.BatchService;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.BatchParameters;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.InputTransformer;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.firehose.FirehoseService;
|
||||
import io.github.hectorvent.floci.services.firehose.model.Record;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
@@ -23,6 +28,7 @@ class EventBridgeInvokerTest {
|
||||
private EventBridgeInvoker invoker;
|
||||
private SqsService sqsService;
|
||||
private BatchService batchService;
|
||||
private FirehoseService firehoseService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
@@ -30,11 +36,13 @@ class EventBridgeInvokerTest {
|
||||
sqsService = mock(SqsService.class);
|
||||
SnsService snsService = mock(SnsService.class);
|
||||
batchService = mock(BatchService.class);
|
||||
firehoseService = mock(FirehoseService.class);
|
||||
invoker = new EventBridgeInvoker(
|
||||
lambdaService,
|
||||
sqsService,
|
||||
snsService,
|
||||
batchService,
|
||||
firehoseService,
|
||||
new ObjectMapper(),
|
||||
mock(io.github.hectorvent.floci.config.EmulatorConfig.class)
|
||||
);
|
||||
@@ -123,6 +131,34 @@ class EventBridgeInvokerTest {
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeTarget_firehoseTarget_putsEventJsonAsRecordData() {
|
||||
Target target = new Target("id1",
|
||||
"arn:aws:firehose:us-east-1:000000000000:deliverystream/my-stream", null, null);
|
||||
String event = "{\"source\":\"local.test\",\"detail\":{\"orderId\":\"o-1\"}}";
|
||||
|
||||
invoker.invokeTarget(target, event, "us-east-1");
|
||||
|
||||
ArgumentCaptor<Record> captor = ArgumentCaptor.forClass(Record.class);
|
||||
verify(firehoseService).putRecord(eq("my-stream"), captor.capture());
|
||||
// The event JSON is the record Data verbatim: no wrapping, no trailing newline.
|
||||
assertEquals(event, new String(captor.getValue().getData(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeTarget_firehoseTarget_deliversInputOverrideNotEnvelope() {
|
||||
Target target = new Target("id1",
|
||||
"arn:aws:firehose:us-east-1:000000000000:deliverystream/my-stream",
|
||||
"{\"custom\":\"payload\"}", null);
|
||||
|
||||
invoker.invokeTarget(target, "{\"ignored\":true}", "us-east-1");
|
||||
|
||||
ArgumentCaptor<Record> captor = ArgumentCaptor.forClass(Record.class);
|
||||
verify(firehoseService).putRecord(eq("my-stream"), captor.capture());
|
||||
assertEquals("{\"custom\":\"payload\"}",
|
||||
new String(captor.getValue().getData(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractJsonPath_topLevelField() {
|
||||
String event = "{\"source\":\"aws.s3\",\"detail-type\":\"Object Created\"}";
|
||||
|
||||
Reference in new issue
Block a user