mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sns): deliver published messages to firehose subscriptions (#4079)
Deliver published messages to Firehose delivery stream subscriptions (sns:Publish -> firehose:PutRecord). Support RawMessageDelivery attribute to switch between raw payload and JSON envelope. Tolerates subscriber delivery errors without failing publisher. Closes #4068
This commit is contained in:
1 parent
d6682cf452
commit
ccb6b63d00
4 files changed
+382
-8
No files matched your search
+178
@@ -0,0 +1,178 @@
|
||||
package io.github.hectorvent.floci.services.sns;
|
||||
|
||||
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.not;
|
||||
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class SnsFirehoseIntegrationTest {
|
||||
|
||||
private static final String FIREHOSE_JSON_CT = "application/x-amz-json-1.1";
|
||||
private static final String ENVELOPE_BUCKET = "sns-fh-envelope-bucket";
|
||||
private static final String ENVELOPE_STREAM = "sns-fh-envelope-stream";
|
||||
private static final String RAW_BUCKET = "sns-fh-raw-bucket";
|
||||
private static final String RAW_STREAM = "sns-fh-raw-stream";
|
||||
|
||||
private static String topicArn;
|
||||
private static String envelopeSubArn;
|
||||
private static String rawSubArn;
|
||||
|
||||
@Inject
|
||||
FirehoseService firehoseService;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void setupBucketsAndStreams() {
|
||||
// Create destination S3 buckets
|
||||
given().when().put("/" + ENVELOPE_BUCKET).then().statusCode(200);
|
||||
given().when().put("/" + RAW_BUCKET).then().statusCode(200);
|
||||
|
||||
// Create Firehose delivery streams
|
||||
given()
|
||||
.contentType(FIREHOSE_JSON_CT)
|
||||
.header("X-Amz-Target", "Firehose_20150804.CreateDeliveryStream")
|
||||
.body("""
|
||||
{
|
||||
"DeliveryStreamName": "%s",
|
||||
"ExtendedS3DestinationConfiguration": {
|
||||
"RoleARN": "arn:aws:iam::000000000000:role/firehose-role",
|
||||
"BucketARN": "arn:aws:s3:::%s",
|
||||
"Prefix": "envelope/"
|
||||
}
|
||||
}
|
||||
""".formatted(ENVELOPE_STREAM, ENVELOPE_BUCKET))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType(FIREHOSE_JSON_CT)
|
||||
.header("X-Amz-Target", "Firehose_20150804.CreateDeliveryStream")
|
||||
.body("""
|
||||
{
|
||||
"DeliveryStreamName": "%s",
|
||||
"ExtendedS3DestinationConfiguration": {
|
||||
"RoleARN": "arn:aws:iam::000000000000:role/firehose-role",
|
||||
"BucketARN": "arn:aws:s3:::%s",
|
||||
"Prefix": "raw/"
|
||||
}
|
||||
}
|
||||
""".formatted(RAW_STREAM, RAW_BUCKET))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void createTopicAndSubscriptions() {
|
||||
// Create SNS topic
|
||||
topicArn = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateTopic")
|
||||
.formParam("Name", "sns-to-firehose-topic")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("CreateTopicResponse.CreateTopicResult.TopicArn");
|
||||
|
||||
// Subscribe envelope delivery stream
|
||||
String envelopeStreamArn = "arn:aws:firehose:us-east-1:000000000000:deliverystream/" + ENVELOPE_STREAM;
|
||||
envelopeSubArn = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "Subscribe")
|
||||
.formParam("TopicArn", topicArn)
|
||||
.formParam("Protocol", "firehose")
|
||||
.formParam("Endpoint", envelopeStreamArn)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<SubscriptionArn>"))
|
||||
.extract().xmlPath().getString("SubscribeResponse.SubscribeResult.SubscriptionArn");
|
||||
|
||||
// Subscribe raw delivery stream
|
||||
String rawStreamArn = "arn:aws:firehose:us-east-1:000000000000:deliverystream/" + RAW_STREAM;
|
||||
rawSubArn = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "Subscribe")
|
||||
.formParam("TopicArn", topicArn)
|
||||
.formParam("Protocol", "firehose")
|
||||
.formParam("Endpoint", rawStreamArn)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<SubscriptionArn>"))
|
||||
.extract().xmlPath().getString("SubscribeResponse.SubscribeResult.SubscriptionArn");
|
||||
|
||||
// Enable RawMessageDelivery on the second subscription
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SetSubscriptionAttributes")
|
||||
.formParam("SubscriptionArn", rawSubArn)
|
||||
.formParam("AttributeName", "RawMessageDelivery")
|
||||
.formParam("AttributeValue", "true")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void publishMessage_deliversToFirehoseSubscriptions() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "Publish")
|
||||
.formParam("TopicArn", topicArn)
|
||||
.formParam("Message", "{\"event\":\"order_created\",\"id\":12345}")
|
||||
.formParam("Subject", "Order Notification")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<MessageId>"));
|
||||
|
||||
// Flush both firehose streams so buffered records land in S3
|
||||
firehoseService.flush(ENVELOPE_STREAM);
|
||||
firehoseService.flush(RAW_STREAM);
|
||||
|
||||
// Verify envelope delivery in ENVELOPE_BUCKET
|
||||
String envelopeKey = given().when().get("/" + ENVELOPE_BUCKET + "?prefix=envelope/")
|
||||
.then().statusCode(200)
|
||||
.extract().xmlPath().getString("ListBucketResult.Contents[0].Key");
|
||||
|
||||
given().when().get("/" + ENVELOPE_BUCKET + "/" + envelopeKey)
|
||||
.then().statusCode(200)
|
||||
.body(containsString("\"Type\":\"Notification\""))
|
||||
.body(containsString("\"Subject\":\"Order Notification\""))
|
||||
.body(containsString("order_created"))
|
||||
.body(containsString("12345"));
|
||||
|
||||
// Verify raw delivery in RAW_BUCKET
|
||||
String rawKey = given().when().get("/" + RAW_BUCKET + "?prefix=raw/")
|
||||
.then().statusCode(200)
|
||||
.extract().xmlPath().getString("ListBucketResult.Contents[0].Key");
|
||||
|
||||
given().when().get("/" + RAW_BUCKET + "/" + rawKey)
|
||||
.then().statusCode(200)
|
||||
.body(containsString("{\"event\":\"order_created\",\"id\":12345}"))
|
||||
.body(not(containsString("\"Type\":\"Notification\"")));
|
||||
}
|
||||
}
|
||||
@@ -5,16 +5,25 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.firehose.FirehoseService;
|
||||
import io.github.hectorvent.floci.services.firehose.model.Record;
|
||||
import io.github.hectorvent.floci.services.sns.model.Subscription;
|
||||
import io.github.hectorvent.floci.services.sns.model.Topic;
|
||||
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
|
||||
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.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
class SnsServiceTest {
|
||||
|
||||
@@ -721,4 +730,118 @@ class SnsServiceTest {
|
||||
assertTrue(snsService.matchesFilterPolicy(nested, body("{\"detail\":{\"b\":\"2\"}}"), null));
|
||||
assertFalse(snsService.matchesFilterPolicy(nested, body("{\"detail\":{\"b\":\"3\"}}"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_deliversToFirehoseSubscription_withJsonEnvelope() throws Exception {
|
||||
FirehoseService firehoseService = mock(FirehoseService.class);
|
||||
RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT);
|
||||
SnsService service = new SnsService(
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
regionResolver,
|
||||
null,
|
||||
null,
|
||||
firehoseService
|
||||
);
|
||||
|
||||
Topic topic = service.createTopic("firehose-topic", null, null, REGION);
|
||||
String streamArn = "arn:aws:firehose:us-east-1:000000000000:deliverystream/test-stream";
|
||||
service.subscribe(topic.getTopicArn(), "firehose", streamArn, REGION, Map.of());
|
||||
|
||||
String messageId = service.publish(topic.getTopicArn(), null, "Hello Firehose", "Test Subject", REGION);
|
||||
assertNotNull(messageId);
|
||||
|
||||
ArgumentCaptor<Record> recordCaptor = ArgumentCaptor.forClass(Record.class);
|
||||
verify(firehoseService).putRecord(eq(ACCOUNT), eq(REGION), eq("test-stream"), recordCaptor.capture());
|
||||
|
||||
Record captured = recordCaptor.getValue();
|
||||
assertNotNull(captured);
|
||||
assertNotNull(captured.getData());
|
||||
String payload = new String(captured.getData(), StandardCharsets.UTF_8);
|
||||
|
||||
JsonNode json = new ObjectMapper().readTree(payload);
|
||||
assertEquals("Notification", json.get("Type").asText());
|
||||
assertEquals(messageId, json.get("MessageId").asText());
|
||||
assertEquals(topic.getTopicArn(), json.get("TopicArn").asText());
|
||||
assertEquals("Test Subject", json.get("Subject").asText());
|
||||
assertEquals("Hello Firehose", json.get("Message").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_deliversToFirehoseSubscription_rawMessageDelivery() {
|
||||
FirehoseService firehoseService = mock(FirehoseService.class);
|
||||
RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT);
|
||||
SnsService service = new SnsService(
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
regionResolver,
|
||||
null,
|
||||
null,
|
||||
firehoseService
|
||||
);
|
||||
|
||||
Topic topic = service.createTopic("firehose-topic-raw", null, null, REGION);
|
||||
String streamArn = "arn:aws:firehose:us-east-1:000000000000:deliverystream/test-stream-raw";
|
||||
service.subscribe(topic.getTopicArn(), "firehose", streamArn, REGION,
|
||||
Map.of("RawMessageDelivery", "true"));
|
||||
|
||||
String message = "{\"raw\":\"payload\"}";
|
||||
String messageId = service.publish(topic.getTopicArn(), null, message, null, REGION);
|
||||
assertNotNull(messageId);
|
||||
|
||||
ArgumentCaptor<Record> recordCaptor = ArgumentCaptor.forClass(Record.class);
|
||||
verify(firehoseService).putRecord(eq(ACCOUNT), eq(REGION), eq("test-stream-raw"), recordCaptor.capture());
|
||||
|
||||
Record captured = recordCaptor.getValue();
|
||||
assertEquals(message, new String(captured.getData(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_deliversToFirehoseSubscription_bareStreamNameEndpoint() {
|
||||
FirehoseService firehoseService = mock(FirehoseService.class);
|
||||
RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT);
|
||||
SnsService service = new SnsService(
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
regionResolver,
|
||||
null,
|
||||
null,
|
||||
firehoseService
|
||||
);
|
||||
|
||||
Topic topic = service.createTopic("firehose-bare-topic", null, null, REGION);
|
||||
service.subscribe(topic.getTopicArn(), "firehose", "bare-stream", REGION,
|
||||
Map.of("RawMessageDelivery", "true"));
|
||||
|
||||
service.publish(topic.getTopicArn(), null, "bare-msg", null, REGION);
|
||||
|
||||
ArgumentCaptor<Record> recordCaptor = ArgumentCaptor.forClass(Record.class);
|
||||
verify(firehoseService).putRecord(eq(ACCOUNT), eq(REGION), eq("bare-stream"), recordCaptor.capture());
|
||||
assertEquals("bare-msg", new String(recordCaptor.getValue().getData(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publish_firehoseDeliveryFailure_doesNotFailPublisher() {
|
||||
FirehoseService firehoseService = mock(FirehoseService.class);
|
||||
doThrow(new RuntimeException("stream not found"))
|
||||
.when(firehoseService).putRecord(any(), any(), any(), any());
|
||||
|
||||
RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT);
|
||||
SnsService service = new SnsService(
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
regionResolver,
|
||||
null,
|
||||
null,
|
||||
firehoseService
|
||||
);
|
||||
|
||||
Topic topic = service.createTopic("firehose-fail-topic", null, null, REGION);
|
||||
service.subscribe(topic.getTopicArn(), "firehose",
|
||||
"arn:aws:firehose:us-east-1:000000000000:deliverystream/failing-stream", REGION, Map.of());
|
||||
|
||||
// Delivery error is logged and tolerated; publisher receives message ID
|
||||
String messageId = service.publish(topic.getTopicArn(), null, "msg", null, REGION);
|
||||
assertNotNull(messageId);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user