diff --git a/src/main/java/io/floci/aws/services/cloudformation/provisioners/SnsCfnProvisioner.java b/src/main/java/io/floci/aws/services/cloudformation/provisioners/SnsCfnProvisioner.java index 0e592321a2..b0b98db178 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/provisioners/SnsCfnProvisioner.java +++ b/src/main/java/io/floci/aws/services/cloudformation/provisioners/SnsCfnProvisioner.java @@ -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. diff --git a/src/test/java/io/floci/aws/services/cloudformation/provisioners/SnsCfnProvisionerTest.java b/src/test/java/io/floci/aws/services/cloudformation/provisioners/SnsCfnProvisionerTest.java index c8c8aa06b8..4a4a63341a 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/provisioners/SnsCfnProvisionerTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/provisioners/SnsCfnProvisionerTest.java @@ -509,6 +509,58 @@ class SnsCfnProvisionerTest { anyString(), anyString()); } + @Test + void displayNameIsForwardedOnlyWhenSet() { + ArgumentCaptor> 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> 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 diff --git a/src/test/resources/cloudformation/writable-property-gaps.tsv b/src/test/resources/cloudformation/writable-property-gaps.tsv index a42f1c3706..c578de17bf 100644 --- a/src/test/resources/cloudformation/writable-property-gaps.tsv +++ b/src/test/resources/cloudformation/writable-property-gaps.tsv @@ -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