fix(sns): require IAM role ARN for Firehose subscriptions (#4233)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-23 10:57:28 -04:00
1 parent b6d5d51bde
commit 97aa084209
5 files changed
+96 -7

No files matched your search

+2 -1
View File
@@ -85,7 +85,8 @@ Floci supports real SNS → SQS fan-out. When you publish to a topic, every SQS-
Supported subscription protocols:
- `sqs`: delivers to a Floci SQS queue
- `lambda`: invokes a Floci Lambda function
- `firehose`: puts records to a Floci Firehose delivery stream
- `firehose`: puts records to a Floci Firehose delivery stream; `Subscribe` requires a valid IAM
`SubscriptionRoleArn` attribute, which is returned by `GetSubscriptionAttributes`
- `http` / `https`: posts to an HTTP endpoint
- `application`: fans out to a mobile push platform endpoint (see [Mobile push](#mobile-push-mock))
@@ -57,6 +57,7 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@ApplicationScoped
public class SnsService implements Resettable, ResourceProvider {
@@ -71,6 +72,9 @@ public class SnsService implements Resettable, ResourceProvider {
private static final int MAX_MAX_MESSAGE_SIZE = 1_048_576;
/** The only protocols allowed on a topic above {@link #DEFAULT_MAX_MESSAGE_SIZE}. */
private static final Set<String> LARGE_PAYLOAD_PROTOCOLS = Set.of("sqs", "firehose", "lambda");
private static final String SUBSCRIPTION_ROLE_ARN = "SubscriptionRoleArn";
private static final Pattern IAM_ROLE_ARN = Pattern.compile(
"arn:" + AwsArnUtils.PARTITION_REGEX + ":iam::[0-9]{12}:role/[A-Za-z0-9+=,.@_/-]+");
/** How many subscriptions a topic above {@link #DEFAULT_MAX_MESSAGE_SIZE} may carry. */
private static final int LARGE_PAYLOAD_SUBSCRIPTION_LIMIT = 100;
private static final String MAXIMUM_MESSAGE_SIZE = "MaximumMessageSize";
@@ -437,6 +441,9 @@ public class SnsService implements Resettable, ResourceProvider {
throw new AwsException("InvalidParameter",
"Invalid parameter: Endpoint scheme does not match protocol '" + protocol + "'.", 400);
}
if ("firehose".equals(protocol)) {
requireFirehoseRoleArn(attributes == null ? null : attributes.get(SUBSCRIPTION_ROLE_ARN));
}
if (topicMaxMessageSize(topicArn, region) > DEFAULT_MAX_MESSAGE_SIZE) {
requireLargePayloadProtocol(protocol);
}
@@ -999,10 +1006,21 @@ public class SnsService implements Resettable, ResourceProvider {
String key = subKey(region, subscriptionArn);
Subscription sub = subscriptionStore.get(key)
.orElseThrow(() -> new AwsException("NotFound", "Subscription does not exist.", 404));
if ("firehose".equals(sub.getProtocol()) && SUBSCRIPTION_ROLE_ARN.equals(attributeName)) {
requireFirehoseRoleArn(attributeValue);
}
sub.getAttributes().put(attributeName, attributeValue);
subscriptionStore.put(key, sub);
}
private static void requireFirehoseRoleArn(String roleArn) {
if (roleArn == null || !IAM_ROLE_ARN.matcher(roleArn).matches()) {
throw new AwsException("InvalidParameter",
"Invalid parameter: SubscriptionRoleArn must be a valid IAM role ARN for firehose subscriptions.",
400);
}
}
public record BatchPublishResult(List<String[]> successful, List<String[]> failed) {
}
@@ -93,7 +93,7 @@ class SnsFirehoseIntegrationTest {
// Subscribe envelope delivery stream
String envelopeStreamArn = "arn:aws:firehose:us-east-1:000000000000:deliverystream/" + ENVELOPE_STREAM;
envelopeSubArn = given()
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "Subscribe")
.formParam("TopicArn", topicArn)
@@ -101,11 +101,36 @@ class SnsFirehoseIntegrationTest {
.formParam("Endpoint", envelopeStreamArn)
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("SubscriptionRoleArn"));
envelopeSubArn = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "Subscribe")
.formParam("TopicArn", topicArn)
.formParam("Protocol", "firehose")
.formParam("Endpoint", envelopeStreamArn)
.formParam("Attributes.entry.1.key", "SubscriptionRoleArn")
.formParam("Attributes.entry.1.value", "arn:aws:iam::000000000000:role/firehose-role")
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<SubscriptionArn>"))
.extract().xmlPath().getString("SubscribeResponse.SubscribeResult.SubscriptionArn");
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "GetSubscriptionAttributes")
.formParam("SubscriptionArn", envelopeSubArn)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("SubscriptionRoleArn"))
.body(containsString("arn:aws:iam::000000000000:role/firehose-role"));
// Subscribe raw delivery stream
String rawStreamArn = "arn:aws:firehose:us-east-1:000000000000:deliverystream/" + RAW_STREAM;
rawSubArn = given()
@@ -114,6 +139,8 @@ class SnsFirehoseIntegrationTest {
.formParam("TopicArn", topicArn)
.formParam("Protocol", "firehose")
.formParam("Endpoint", rawStreamArn)
.formParam("Attributes.entry.1.key", "SubscriptionRoleArn")
.formParam("Attributes.entry.1.value", "arn:aws:iam::000000000000:role/firehose-role")
.when()
.post("/")
.then()
@@ -257,7 +257,10 @@ class SnsMaximumMessageSizeTest {
String arn = largeTopic("allowed-protocols");
for (String protocol : List.of("sqs", "firehose", "lambda")) {
assertNotNull(snsService.subscribe(arn, protocol, "arn:aws:" + protocol
+ ":us-east-1:000000000000:target", REGION, Map.of()));
+ ":us-east-1:000000000000:target", REGION,
"firehose".equals(protocol)
? Map.of("SubscriptionRoleArn", "arn:aws:iam::000000000000:role/firehose-role")
: Map.of()));
}
}
@@ -30,6 +30,7 @@ class SnsServiceTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT = "000000000000";
private static final String BASE_URL = "http://localhost:4566";
private static final String FIREHOSE_ROLE_ARN = "arn:aws:iam::000000000000:role/firehose-role";
private SnsService snsService;
@@ -130,6 +131,43 @@ class SnsServiceTest {
assertEquals(Map.of("attr1", "value1", "attr2", "value2"), sub.getAttributes());
}
@Test
void subscribe_firehoseRequiresValidRoleArn() {
Topic topic = snsService.createTopic("firehose-topic", null, null, REGION);
String streamArn = "arn:aws:firehose:us-east-1:000000000000:deliverystream/test-stream";
for (Map<String, String> attributes : List.of(
Map.<String, String>of(),
Map.of("SubscriptionRoleArn", ""),
Map.of("SubscriptionRoleArn", "arn:aws:iam::000000000000:user/not-a-role"))) {
AwsException ex = assertThrows(AwsException.class, () -> snsService.subscribe(
topic.getTopicArn(), "firehose", streamArn, REGION, attributes));
assertEquals("InvalidParameter", ex.getErrorCode());
assertTrue(ex.getMessage().contains("SubscriptionRoleArn"));
}
assertTrue(snsService.listSubscriptions(REGION).isEmpty());
}
@Test
void subscribe_firehoseRoleArnIsReturnedAndCanBeUpdated() {
Topic topic = snsService.createTopic("firehose-topic", null, null, REGION);
Subscription sub = snsService.subscribe(topic.getTopicArn(), "firehose",
"arn:aws:firehose:us-east-1:000000000000:deliverystream/test-stream", REGION,
Map.of("SubscriptionRoleArn", FIREHOSE_ROLE_ARN));
assertEquals(FIREHOSE_ROLE_ARN,
snsService.getSubscriptionAttributes(sub.getSubscriptionArn(), REGION).get("SubscriptionRoleArn"));
AwsException ex = assertThrows(AwsException.class, () -> snsService.setSubscriptionAttribute(
sub.getSubscriptionArn(), "SubscriptionRoleArn", "not-an-arn", REGION));
assertEquals("InvalidParameter", ex.getErrorCode());
assertEquals(FIREHOSE_ROLE_ARN,
snsService.getSubscriptionAttributes(sub.getSubscriptionArn(), REGION).get("SubscriptionRoleArn"));
String updated = "arn:aws:iam::000000000000:role/updated-role";
snsService.setSubscriptionAttribute(sub.getSubscriptionArn(), "SubscriptionRoleArn", updated, REGION);
assertEquals(updated,
snsService.getSubscriptionAttributes(sub.getSubscriptionArn(), REGION).get("SubscriptionRoleArn"));
}
@Test
void subscribe_idempotent() {
Topic topic = snsService.createTopic("my-topic", null, null, REGION);
@@ -794,7 +832,8 @@ class SnsServiceTest {
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());
service.subscribe(topic.getTopicArn(), "firehose", streamArn, REGION,
Map.of("SubscriptionRoleArn", FIREHOSE_ROLE_ARN));
String messageId = service.publish(topic.getTopicArn(), null, "Hello Firehose", "Test Subject", REGION);
assertNotNull(messageId);
@@ -831,7 +870,7 @@ class SnsServiceTest {
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"));
Map.of("RawMessageDelivery", "true", "SubscriptionRoleArn", FIREHOSE_ROLE_ARN));
String message = "{\"raw\":\"payload\"}";
String messageId = service.publish(topic.getTopicArn(), null, message, null, REGION);
@@ -859,7 +898,7 @@ class SnsServiceTest {
Topic topic = service.createTopic("firehose-bare-topic", null, null, REGION);
service.subscribe(topic.getTopicArn(), "firehose", "bare-stream", REGION,
Map.of("RawMessageDelivery", "true"));
Map.of("RawMessageDelivery", "true", "SubscriptionRoleArn", FIREHOSE_ROLE_ARN));
service.publish(topic.getTopicArn(), null, "bare-msg", null, REGION);
@@ -886,7 +925,8 @@ class SnsServiceTest {
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());
"arn:aws:firehose:us-east-1:000000000000:deliverystream/failing-stream", REGION,
Map.of("SubscriptionRoleArn", FIREHOSE_ROLE_ARN));
// Delivery error is logged and tolerated; publisher receives message ID
String messageId = service.publish(topic.getTopicArn(), null, "msg", null, REGION);