fix(sns): forward CloudFormation subscription roles (#7683)

Resolve SubscriptionRoleArn for SNS subscriptions and update the role without replacing the subscription. Remove the covered CloudFormation property gap.

Validate RawMessageDelivery and FilterPolicy when Subscribe reuses an existing subscription; preserve omitted/default attributes and explicit updates.

Update the stack subscription by its prior physical identity when topic, protocol and endpoint are unchanged. Preserve Subscribe validation for creates and target replacements, reset dropped declared attributes and validate firehose roles before other attribute writes. Cover FilterPolicy and raw delivery updates/removal through CloudFormation and SNS SDKs.

Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-06 20:12:45 -04:00
1 parent feb9364760
commit 765647268b
7 files changed
+458 -4

No files matched your search

@@ -198,13 +198,50 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
if (props.has("RawMessageDelivery")) {
attributes.put("RawMessageDelivery", ctx.engine().resolve(props.path("RawMessageDelivery")));
}
if (props.hasNonNull("SubscriptionRoleArn")) {
attributes.put("SubscriptionRoleArn", ctx.engine().resolve(props.path("SubscriptionRoleArn")));
}
if (props.has("RedrivePolicy") && !props.path("RedrivePolicy").isNull()) {
attributes.put("RedrivePolicy", ctx.engine().resolveJsonAttribute(props.path("RedrivePolicy")));
}
Subscription sub = snsService.subscribe(topicArn, protocol, endpoint, ctx.region(), attributes);
r.setPhysicalId(sub.getSubscriptionArn());
r.getAttributes().put("Arn", sub.getSubscriptionArn());
String subscriptionArn;
if (ctx.isUpdate() && matchesPriorSubscription(topicArn, protocol, endpoint, ctx)) {
subscriptionArn = ctx.priorPhysicalId();
// Validate the firehose role before changing other attributes, including a removed role.
if (attributes.containsKey("SubscriptionRoleArn") || ctx.previouslyDeclared("SubscriptionRoleArn")) {
snsService.setSubscriptionAttribute(subscriptionArn, "SubscriptionRoleArn",
attributes.getOrDefault("SubscriptionRoleArn", ""), ctx.region());
}
Map<String, String> desired = new HashMap<>(attributes);
desired.remove("SubscriptionRoleArn");
for (String policy : List.of("FilterPolicy", "RedrivePolicy")) {
if (!desired.containsKey(policy) && ctx.previouslyDeclared(policy)) {
desired.put(policy, "");
}
}
if (!desired.containsKey("RawMessageDelivery") && ctx.previouslyDeclared("RawMessageDelivery")) {
desired.put("RawMessageDelivery", "false");
}
if (!desired.containsKey("FilterPolicyScope") && ctx.previouslyDeclared("FilterPolicyScope")) {
desired.put("FilterPolicyScope", "MessageAttributes");
}
desired.forEach((name, value) ->
snsService.setSubscriptionAttribute(subscriptionArn, name, value, ctx.region()));
} else {
Subscription sub = snsService.subscribe(topicArn, protocol, endpoint, ctx.region(), attributes);
subscriptionArn = sub.getSubscriptionArn();
}
r.setPhysicalId(subscriptionArn);
r.getAttributes().put("Arn", subscriptionArn);
}
private boolean matchesPriorSubscription(String topicArn, String protocol, String endpoint,
ProvisionContext ctx) {
Map<String, String> prior = snsService.getSubscriptionAttributes(ctx.priorPhysicalId(), ctx.region());
return Objects.equals(topicArn, prior.get("TopicArn"))
&& Objects.equals(protocol, prior.get("Protocol"))
&& Objects.equals(endpoint, prior.get("Endpoint"));
}
/**
@@ -960,6 +960,7 @@ public class SnsService implements Resettable, ResourceProvider {
for (Subscription existing : subscriptionsByTopic(topicArn, region)) {
if (protocol.equals(existing.getProtocol())
&& Objects.equals(endpoint, existing.getEndpoint())) {
requireMatchingSubscriptionAttributes(existing, attributes);
return new SubscriptionCreation(existing, false);
}
}
@@ -1486,6 +1487,28 @@ public class SnsService implements Resettable, ResourceProvider {
return "endpoint::" + region + "::" + arn;
}
private static void requireMatchingSubscriptionAttributes(Subscription existing, Map<String, String> requested) {
if (requested == null) {
return;
}
for (String attribute : List.of("RawMessageDelivery", "FilterPolicy")) {
if (!requested.containsKey(attribute)) {
continue;
}
String defaultValue = "RawMessageDelivery".equals(attribute) ? "false" : "{}";
String current = existing.getAttributes().getOrDefault(attribute, defaultValue);
String desired = requested.get(attribute);
if ("FilterPolicy".equals(attribute)) {
current = current == null || current.isEmpty() ? defaultValue : current;
desired = desired != null && desired.isEmpty() ? defaultValue : desired;
}
if (!Objects.equals(current, desired)) {
throw new AwsException("InvalidParameter",
"Invalid parameter: Attributes Reason: Subscription already exists with different attributes", 400);
}
}
}
public Map<String, String> getSubscriptionAttributes(String subscriptionArn, String region) {
String key = subKey(region, subscriptionArn);
Subscription sub = subscriptionStore.get(key)