refactor(cloudformation): move AWS::SNS::* into its own provisioner (#2759)

Migrates AWS::SNS::Topic and AWS::SNS::Subscription out of the switch,
completing the Wave 1 leaf slices. Behaviour-preserving.

The care here is the subscription's attribute encoding. FilterPolicy and
RedrivePolicy are JSON documents that SNS expects as JSON strings, so they
go through resolveJsonAttribute, while FilterPolicyScope and
RawMessageDelivery are scalars that go through resolve. Using the scalar
resolver on a document flattens it to an empty string and silently breaks
message filtering, which is the failure this type has had before, so the
test stubs the two resolvers with distinct semantics and asserts the
serialized form rather than only that an attribute was set.

The topic's physical id is the ARN rather than the name, because Ref on an
SNS topic returns the ARN.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-08-31 00:06:05 -04:00
1 parent 6f123e83b8
commit 9f7b7117f1
4 files changed
+314 -54

No files matched your search

@@ -267,8 +267,6 @@ public class CloudFormationResourceProvisioner {
"AWS::RDS::DBSubnetGroup",
"AWS::Route53::HostedZone",
"AWS::Route53::RecordSet",
"AWS::SNS::Subscription",
"AWS::SNS::Topic",
"AWS::SecretsManager::Secret",
"AWS::SecretsManager::SecretTargetAttachment",
"AWS::StepFunctions::StateMachine",
@@ -285,7 +283,6 @@ public class CloudFormationResourceProvisioner {
private static final Duration CR_RESPONSE_TIMEOUT = Duration.ofSeconds(10);
private final S3Service s3Service;
private final SnsService snsService;
private final DynamoDbService dynamoDbService;
private final LambdaService lambdaService;
private final IamService iamService;
@@ -350,7 +347,6 @@ public class CloudFormationResourceProvisioner {
EmulatorConfig config) {
this.config = config;
this.s3Service = s3Service;
this.snsService = snsService;
this.dynamoDbService = dynamoDbService;
this.lambdaService = lambdaService;
this.iamService = iamService;
@@ -413,8 +409,6 @@ public class CloudFormationResourceProvisioner {
return resource;
}
switch (resourceType) {
case "AWS::SNS::Topic" -> provisionSnsTopic(resource, properties, engine, region, accountId, stackName);
case "AWS::SNS::Subscription" -> provisionSnsSubscription(resource, properties, engine, region);
case "AWS::DynamoDB::Table", "AWS::DynamoDB::GlobalTable" ->
provisionDynamoTable(resource, properties, engine, region, accountId, stackName);
case "AWS::Lambda::Function" -> provisionLambda(resource, properties, engine, region, accountId, stackName);
@@ -694,8 +688,6 @@ public class CloudFormationResourceProvisioner {
return;
}
switch (resourceType) {
case "AWS::SNS::Topic" -> snsService.deleteTopic(physicalId, region);
case "AWS::SNS::Subscription" -> snsService.unsubscribe(physicalId, region);
case "AWS::DynamoDB::Table" -> deleteDynamoTableSafe(physicalId, region);
case "AWS::Lambda::Function" -> deleteLambdaFunctionSafe(physicalId, region);
// AWS::IAM::Policy is inline: it is removed together with its owning principal (see
@@ -1804,50 +1796,6 @@ public class CloudFormationResourceProvisioner {
// ── SNS ───────────────────────────────────────────────────────────────────
private void provisionSnsTopic(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String accountId, String stackName) {
String topicName = resolveOptional(props, "TopicName", engine);
String contentBasedDedupFlag = resolveOptional(props, "ContentBasedDeduplication", engine);
if (topicName == null || topicName.isBlank()) {
topicName = generatePhysicalName(stackName, r.getLogicalId(), 256, false);
}
Map<String, String> attributes = new HashMap<>();
if (contentBasedDedupFlag != null && !contentBasedDedupFlag.isBlank()) {
attributes.put("ContentBasedDeduplication", contentBasedDedupFlag);
}
var topic = snsService.createTopic(topicName, attributes, Map.of(), region);
r.setPhysicalId(topic.getTopicArn());
r.getAttributes().put("Arn", topic.getTopicArn());
r.getAttributes().put("TopicName", topicName);
}
private void provisionSnsSubscription(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, String region) {
String topicArn = engine.resolve(props.path("TopicArn"));
String protocol = engine.resolve(props.path("Protocol"));
String endpoint = engine.resolve(props.path("Endpoint"));
Map<String, String> attributes = new HashMap<>();
if (props.has("FilterPolicy") && !props.path("FilterPolicy").isNull()) {
attributes.put("FilterPolicy", engine.resolveJsonAttribute(props.path("FilterPolicy")));
}
if (props.has("FilterPolicyScope")) {
attributes.put("FilterPolicyScope", engine.resolve(props.path("FilterPolicyScope")));
}
if (props.has("RawMessageDelivery")) {
attributes.put("RawMessageDelivery", engine.resolve(props.path("RawMessageDelivery")));
}
if (props.has("RedrivePolicy") && !props.path("RedrivePolicy").isNull()) {
attributes.put("RedrivePolicy", engine.resolveJsonAttribute(props.path("RedrivePolicy")));
}
var sub = snsService.subscribe(topicArn, protocol, endpoint, region, attributes);
r.setPhysicalId(sub.getSubscriptionArn());
r.getAttributes().put("Arn", sub.getSubscriptionArn());
}
// ── DynamoDB ──────────────────────────────────────────────────────────────
private void provisionDynamoTable(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
@@ -0,0 +1,96 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.sns.SnsService;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/** Provisions {@code AWS::SNS::Topic} and {@code AWS::SNS::Subscription}. */
@ApplicationScoped
public class SnsCfnProvisioner implements CfnResourceProvisioner {
private static final String TOPIC = "AWS::SNS::Topic";
private static final String SUBSCRIPTION = "AWS::SNS::Subscription";
private static final int TOPIC_NAME_MAX_LENGTH = 256;
private final SnsService snsService;
public SnsCfnProvisioner(SnsService snsService) {
this.snsService = snsService;
}
@Override
public Set<String> resourceTypes() {
return Set.of(TOPIC, SUBSCRIPTION);
}
@Override
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
switch (r.getResourceType()) {
case TOPIC -> provisionTopic(r, props, ctx);
case SUBSCRIPTION -> provisionSubscription(r, props, ctx);
default -> throw new IllegalStateException(
"SnsCfnProvisioner cannot provision " + r.getResourceType());
}
}
private void provisionTopic(StackResource r, JsonNode props, ProvisionContext ctx) {
String topicName = ctx.resolveOptional(props, "TopicName");
String contentBasedDedupFlag = ctx.resolveOptional(props, "ContentBasedDeduplication");
if (topicName == null || topicName.isBlank()) {
topicName = ctx.generatePhysicalName(r.getLogicalId(), TOPIC_NAME_MAX_LENGTH, false);
}
Map<String, String> attributes = new HashMap<>();
if (contentBasedDedupFlag != null && !contentBasedDedupFlag.isBlank()) {
attributes.put("ContentBasedDeduplication", contentBasedDedupFlag);
}
var topic = snsService.createTopic(topicName, attributes, Map.of(), ctx.region());
// Ref returns the topic ARN, which is why the physical id is the ARN and not the name.
r.setPhysicalId(topic.getTopicArn());
r.getAttributes().put("Arn", topic.getTopicArn());
r.getAttributes().put("TopicName", topicName);
}
private void provisionSubscription(StackResource r, JsonNode props, ProvisionContext ctx) {
String topicArn = ctx.engine().resolve(props.path("TopicArn"));
String protocol = ctx.engine().resolve(props.path("Protocol"));
String endpoint = ctx.engine().resolve(props.path("Endpoint"));
Map<String, String> attributes = new HashMap<>();
// FilterPolicy and RedrivePolicy are JSON documents, so they go through
// resolveJsonAttribute, which serializes the resolved node once. Passing them through the
// scalar resolve() instead would hand SNS a re-encoded string and break filtering.
if (props.has("FilterPolicy") && !props.path("FilterPolicy").isNull()) {
attributes.put("FilterPolicy", ctx.engine().resolveJsonAttribute(props.path("FilterPolicy")));
}
if (props.has("FilterPolicyScope")) {
attributes.put("FilterPolicyScope", ctx.engine().resolve(props.path("FilterPolicyScope")));
}
if (props.has("RawMessageDelivery")) {
attributes.put("RawMessageDelivery", ctx.engine().resolve(props.path("RawMessageDelivery")));
}
if (props.has("RedrivePolicy") && !props.path("RedrivePolicy").isNull()) {
attributes.put("RedrivePolicy", ctx.engine().resolveJsonAttribute(props.path("RedrivePolicy")));
}
var sub = snsService.subscribe(topicArn, protocol, endpoint, ctx.region(), attributes);
r.setPhysicalId(sub.getSubscriptionArn());
r.getAttributes().put("Arn", sub.getSubscriptionArn());
}
@Override
public void delete(String resourceType, String physicalId, String region) {
switch (resourceType) {
case TOPIC -> snsService.deleteTopic(physicalId, region);
case SUBSCRIPTION -> snsService.unsubscribe(physicalId, region);
default -> throw new IllegalStateException(
"SnsCfnProvisioner cannot delete " + resourceType);
}
}
}