diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java index 7158765324..181400e3da 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.JsonNodeType; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine; @@ -124,7 +125,10 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { String tableName = ctx.stablePhysicalName(ctx.resolveOptional(props, "TableName"), r.getLogicalId(), TABLE_NAME_MAX_LENGTH, false); JsonNode ttlSpec = props != null ? engine.resolveNode(props.get("TimeToLiveSpecification")) : null; - boolean ttlDeclared = ttlSpec != null && ttlSpec.isObject(); + boolean ttlDeclared = ttlSpec != null && !ttlSpec.isNull(); + if (ttlDeclared) { + CfnPropertyTypes.requireType("TimeToLiveSpecification", ttlSpec, JsonNodeType.OBJECT); + } // A stack that never declared TTL leaves it alone, as AWS does, so TTL set outside the stack // survives an update; removing a block the previous template declared turns TTL off. boolean ttlManaged = ttlDeclared || ctx.previouslyDeclared("TimeToLiveSpecification"); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbTimeToLiveIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbTimeToLiveIntegrationTest.java index d4a6c12a79..f8d14846d4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbTimeToLiveIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbTimeToLiveIntegrationTest.java @@ -291,6 +291,63 @@ class CloudFormationDynamoDbTimeToLiveIntegrationTest { deleteStack(stack); } + /** As measured on AWS: a block that is not an object fails the create, and no table is left. */ + @Test + void aTimeToLiveBlockThatIsNotAnObjectFailsTheCreateAndLeavesNoTable() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-scalar-" + suffix; + String table = "ttl-scalar-table-" + suffix; + + cloudFormation(stack, "CreateStack", + TEMPLATE.formatted(table, "dev", ",\n\"TimeToLiveSpecification\": false", "")); + assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + + describeStackEvents(stack).body(containsString("Property [TimeToLiveSpecification]" + + " expected type: JSONObject, found: String")); + dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}") + .statusCode(400) + .body("__type", containsString("ResourceNotFoundException")); + + deleteStack(stack); + } + + /** A block that is not an object is refused, not read as a removed block that turns TTL off. */ + @Test + void anUpdateWithATimeToLiveBlockThatIsNotAnObjectKeepsTimeToLiveEnabled() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-scalar-up-" + suffix; + String table = "ttl-scalar-up-table-" + suffix; + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", TTL_ENABLED, "")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + + cloudFormation(stack, "UpdateStack", + TEMPLATE.formatted(table, "dev", ",\n\"TimeToLiveSpecification\": [\"expiresAt\"]", "")); + assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + + describeStackEvents(stack).body(containsString("Property [TimeToLiveSpecification]" + + " expected type: JSONObject, found: JSONArray")); + describeTimeToLive(table) + .body("TimeToLiveDescription.TimeToLiveStatus", equalTo("ENABLED")) + .body("TimeToLiveDescription.AttributeName", equalTo("expiresAt")); + + deleteStack(stack); + } + + @Test + void aTimeToLiveBlockLeftOutWithNoValueIsNotRefused() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-novalue-" + suffix; + String table = "ttl-novalue-table-" + suffix; + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", + ",\n\"TimeToLiveSpecification\": {\"Ref\": \"AWS::NoValue\"}", "")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + describeTimeToLive(table).body("TimeToLiveDescription.TimeToLiveStatus", equalTo("DISABLED")); + + deleteStack(stack); + } + private ValidatableResponse describeStackEvents(String stack) { return given() .contentType("application/x-www-form-urlencoded") diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java index 1f0bfc888b..34f87498a2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java @@ -466,6 +466,45 @@ class DynamoDbCfnProvisionerTest { verifyNoInteractions(dynamoDb); } + @Test + void aBooleanTimeToLiveFailsTemplateValidationBeforeTheTableIsCreated() throws Exception { + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":false}"); + ProvisionContext ctx = ctx(); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertEquals("ValidationError", e.getErrorCode()); + assertEquals("Property [TimeToLiveSpecification] expected type: JSONObject, found: String", e.getMessage()); + verifyNoInteractions(dynamoDb); + } + + @Test + void aListTimeToLiveFailsTemplateValidationBeforeTheTableIsCreated() throws Exception { + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":[]}"); + ProvisionContext ctx = ctx(); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertEquals("Property [TimeToLiveSpecification] expected type: JSONObject, found: JSONArray", + e.getMessage()); + verifyNoInteractions(dynamoDb); + } + + @Test + void anUpdateWithANonObjectTimeToLiveLeavesTheDeclaredOneEnabled() throws Exception { + existingTable(withTimeToLive(table(false), "expiresAt")); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":\"off\"}"); + ProvisionContext ctx = updateCtx(TABLE_NAME, TTL_PROPS); + + assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + verify(dynamoDb, never()).updateTimeToLive(anyString(), any(), anyBoolean(), anyString()); + } + /** CloudFormation renames a TTL attribute by disabling it in one update and enabling the new one in a later one. */ @Test void updateThatRenamesAnEnabledTimeToLiveAttributeFailsBeforeChangingAnything() throws Exception {