feat(cloudformation): provision AWS::SNS::TopicPolicy (#3126)

Adds the type to SnsCfnProvisioner beside Topic and Subscription. A topic
policy is not an entity of its own: it writes the Policy attribute of every
topic the template lists, so re-applying on UpdateStack is what an update
means. The document goes through resolveJsonAttribute like FilterPolicy, so a
Ref to a topic inside Resource resolves to the ARN. The physical id is
synthetic and stays put across updates, as AWS::SQS::QueuePolicy does, and Ref
and Fn::GetAtt Id both return it. An empty Topics list or a missing
PolicyDocument is refused. Delete touches nothing, as with QueuePolicy.

Ported from lex00/floci#118. Tracked in lex00/floci#192.
This commit is contained in:
lex00 authored and GitHub committed 2026-09-06 15:11:42 -05:00
1 parent 024bfa0a5c
commit ba3dd657b6
5 files changed
+282 -5

No files matched your search

@@ -1,20 +1,24 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsException;
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.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
/** Provisions {@code AWS::SNS::Topic} and {@code AWS::SNS::Subscription}. */
/** Provisions {@code AWS::SNS::Topic}, {@code AWS::SNS::Subscription} and {@code AWS::SNS::TopicPolicy}. */
@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 String TOPIC_POLICY = "AWS::SNS::TopicPolicy";
private static final int TOPIC_NAME_MAX_LENGTH = 256;
private final SnsService snsService;
@@ -25,7 +29,7 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
@Override
public Set<String> resourceTypes() {
return Set.of(TOPIC, SUBSCRIPTION);
return Set.of(TOPIC, SUBSCRIPTION, TOPIC_POLICY);
}
@Override
@@ -33,6 +37,7 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
switch (r.getResourceType()) {
case TOPIC -> provisionTopic(r, props, ctx);
case SUBSCRIPTION -> provisionSubscription(r, props, ctx);
case TOPIC_POLICY -> provisionTopicPolicy(r, props, ctx);
default -> throw new IllegalStateException(
"SnsCfnProvisioner cannot provision " + r.getResourceType());
}
@@ -93,11 +98,42 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
r.getAttributes().put("Arn", sub.getSubscriptionArn());
}
/**
* A topic policy is not an entity of its own. It writes the Policy attribute of every topic the
* template lists, so re-applying on UpdateStack is what an update means, and the physical id
* only has to stay put across updates, as with {@code AWS::SQS::QueuePolicy}. {@code Ref} and
* {@code Fn::GetAtt Id} both return that id.
*/
private void provisionTopicPolicy(StackResource r, JsonNode props, ProvisionContext ctx) {
List<String> topics = ctx.resolveStringList(props, "Topics");
if (topics.isEmpty()) {
throw new AwsException("ValidationError",
"AWS::SNS::TopicPolicy requires at least one topic ARN in Topics.", 400);
}
if (props == null || !props.hasNonNull("PolicyDocument")) {
throw new AwsException("ValidationError", "AWS::SNS::TopicPolicy requires a PolicyDocument.", 400);
}
// The document is JSON with intrinsics inside (topic ARNs in Resource, typically), so it
// goes through resolveJsonAttribute like FilterPolicy above rather than the scalar resolve.
String policy = ctx.engine().resolveJsonAttribute(props.path("PolicyDocument"));
for (String topicArn : topics) {
snsService.setTopicAttributes(topicArn, "Policy", policy, ctx.region());
}
String id = ctx.isUpdate()
? ctx.priorPhysicalId()
: "topic-policy-" + UUID.randomUUID().toString().substring(0, 8);
r.setPhysicalId(id);
r.getAttributes().put("Id", id);
}
@Override
public void delete(String resourceType, String physicalId, String region) {
switch (resourceType) {
case TOPIC -> snsService.deleteTopic(physicalId, region);
case SUBSCRIPTION -> snsService.unsubscribe(physicalId, region);
// No entity to remove. The topics keep the last policy written, the same choice
// SqsCfnProvisioner makes for AWS::SQS::QueuePolicy.
case TOPIC_POLICY -> { }
default -> throw new IllegalStateException(
"SnsCfnProvisioner cannot delete " + resourceType);
}