fix(cloudformation): apply DisplayName on AWS::SNS::Topic (#8693)

AWS::SNS::Topic now forwards DisplayName to the topic, so GetTopicAttributes reports it once the stack is created. An update that drops a DisplayName the previous template declared resets it to empty; one the stack never declared is left alone, in case it is set outside the stack. A value over the documented 100-character limit is rejected with a ValidationError before anything is created, and the "accepted and ignored" row for DisplayName leaves writable-property-gaps.tsv.

Closes #8666
This commit is contained in:
Daniël van Ziel authored and GitHub committed 2026-11-16 23:57:22 -04:00
1 parent 15519216c3
commit bf77705ace
3 files changed
+72 -2

No files matched your search

@@ -29,6 +29,7 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
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 static final int DISPLAY_NAME_MAX_LENGTH = 100;
private static final String FIFO_SUFFIX = ".fifo";
private static final String INLINE_SUBSCRIPTION_PREFIX = "__FlociInlineSubscription:";
private static final String PRIOR_INLINE_ATTR = CfnRollback.SNS_PRIOR_INLINE_SUBSCRIPTIONS_ATTR;
@@ -121,6 +122,7 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
String contentBasedDedupFlag = ctx.resolveOptional(props, "ContentBasedDeduplication");
String throughputScope = ctx.resolveOptional(props, "FifoThroughputScope");
String maximumMessageSize = ctx.resolveOptional(props, "MaximumMessageSize");
String displayName = ctx.resolveOptional(props, "DisplayName");
// The .fifo suffix is what makes SnsService treat a topic as FIFO, so an explicitly named
// one is FIFO with or without the flag, which only has to steer a generated name.
boolean fifo = "true".equalsIgnoreCase(ctx.resolveOptional(props, "FifoTopic"))
@@ -162,6 +164,13 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
if (maximumMessageSize != null && !maximumMessageSize.isBlank()) {
attributes.put("MaximumMessageSize", maximumMessageSize);
}
if (displayName != null) {
if (displayName.length() > DISPLAY_NAME_MAX_LENGTH) {
throw new AwsException("ValidationError",
"DisplayName must be at most " + DISPLAY_NAME_MAX_LENGTH + " characters.", 400);
}
attributes.put("DisplayName", displayName);
}
JsonNode subscriptions = props == null ? null
: ctx.engine().resolveNodeOmittingNoValue(props.path("Subscription"));
@@ -198,6 +207,11 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
desired.put("MaximumMessageSize",
String.valueOf(SnsService.DEFAULT_MAX_MESSAGE_SIZE));
}
// A DisplayName the template dropped goes back to the empty default GetTopicAttributes
// reports; one the template never declared may be managed outside the stack.
if (ctx.changes(TOPIC, props).dropped().contains("DisplayName")) {
desired.put("DisplayName", "");
}
}
// A replacing update that adopted an existing topic changes something the stack did not
// create, so what it writes (the prior values, null for absent) is kept for the undo.
@@ -509,6 +509,58 @@ class SnsCfnProvisionerTest {
anyString(), anyString());
}
@Test
void displayNameIsForwardedOnlyWhenSet() {
ArgumentCaptor<Map<String, String>> attrs = ArgumentCaptor.forClass(Map.class);
when(sns.createTopic(anyString(), attrs.capture(), anyMap(), anyString())).thenReturn(topic(TOPIC_ARN));
provision("AWS::SNS::Topic", """
{"TopicName": "events", "DisplayName": "Expected Name"}
""");
assertEquals("Expected Name", attrs.getValue().get("DisplayName"));
setUp();
ArgumentCaptor<Map<String, String>> plain = ArgumentCaptor.forClass(Map.class);
when(sns.createTopic(anyString(), plain.capture(), anyMap(), anyString())).thenReturn(topic(TOPIC_ARN));
provision("AWS::SNS::Topic", """
{"TopicName": "events"}
""");
assertFalse(plain.getValue().containsKey("DisplayName"));
}
@Test
void anUpdateThatDropsDisplayNameClearsItButContinuedOmissionDoesNot() {
when(sns.createTopic(eq("events"), anyMap(), anyMap(), eq(REGION))).thenReturn(topic(TOPIC_ARN));
provisionUpdate("""
{"TopicName": "events", "DisplayName": "Expected Name"}
""", """
{"TopicName": "events"}
""");
verify(sns).setTopicAttributes(TOPIC_ARN, "DisplayName", "", REGION);
setUp();
when(sns.createTopic(eq("events"), anyMap(), anyMap(), eq(REGION))).thenReturn(topic(TOPIC_ARN));
provisionUpdate("""
{"TopicName": "events"}
""", """
{"TopicName": "events"}
""");
verify(sns, never()).setTopicAttributes(anyString(), eq("DisplayName"), anyString(), anyString());
}
@Test
void displayNameOverOneHundredCharactersIsRejectedBeforeAnythingIsCreated() {
String atLimit = "x".repeat(100);
when(sns.createTopic(anyString(), anyMap(), anyMap(), anyString())).thenReturn(topic(TOPIC_ARN));
provision("AWS::SNS::Topic", "{\"TopicName\": \"events\", \"DisplayName\": \"" + atLimit + "\"}");
setUp();
assertThrows(AwsException.class, () -> provision("AWS::SNS::Topic",
"{\"TopicName\": \"events\", \"DisplayName\": \"" + atLimit + "x\"}"));
verify(sns, never()).createTopic(anyString(), anyMap(), anyMap(), anyString());
}
/** Real SNS rejects both attributes on a standard topic, so an update must not invent them. */
@Test
void aStandardTopicUpdateWritesNoFifoDefaults() {
@@ -523,6 +575,10 @@ class SnsCfnProvisionerTest {
/** Provisions an existing topic again, the way UpdateStack re-invokes the provisioner. */
private void provisionUpdate(String json) {
provisionUpdate(null, json);
}
private void provisionUpdate(String priorJson, String json) {
StackResource r = new StackResource();
r.setLogicalId("Topic");
r.setResourceType("AWS::SNS::Topic");
@@ -530,8 +586,9 @@ class SnsCfnProvisionerTest {
String priorName = resolved.path("TopicName").asText(null);
r.setAttributes(new HashMap<>(Map.of("TopicName", priorName)));
r.setPhysicalId(TOPIC_ARN);
JsonNode prior = priorJson == null ? null : props(priorJson);
provisioner.provision(r, resolved,
new ProvisionContext(ctx.engine(), REGION, "000000000000", "my-stack", TOPIC_ARN));
new ProvisionContext(ctx.engine(), REGION, "000000000000", "my-stack", TOPIC_ARN, null, prior));
}
@Test
@@ -644,7 +644,6 @@ AWS::SNS::Subscription ReplayPolicy accepted and ignored
AWS::SNS::Topic ArchivePolicy accepted and ignored
AWS::SNS::Topic DataProtectionPolicy accepted and ignored
AWS::SNS::Topic DeliveryStatusLogging accepted and ignored
AWS::SNS::Topic DisplayName accepted and ignored
AWS::SNS::Topic KmsMasterKeyId accepted and ignored
AWS::SNS::Topic SignatureVersion accepted and ignored
AWS::SNS::Topic Tags accepted and ignored
1 AWS::ApiGateway::ApiKey StageKeys accepted and ignored, as the ApiGatewayApiKeyCfnProvisioner javadoc records
644 AWS::SNS::Topic ArchivePolicy accepted and ignored
645 AWS::SNS::Topic DataProtectionPolicy accepted and ignored
646 AWS::SNS::Topic DeliveryStatusLogging accepted and ignored
AWS::SNS::Topic DisplayName accepted and ignored
647 AWS::SNS::Topic KmsMasterKeyId accepted and ignored
648 AWS::SNS::Topic SignatureVersion accepted and ignored
649 AWS::SNS::Topic Tags accepted and ignored