mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): refuse a DynamoDB TimeToLiveSpecification that is not an object (#7286)
A TimeToLiveSpecification given as a boolean, a string or a list was read as an absent block. A create reached CREATE_COMPLETE without TTL, and an update from a template that declared the block turned TTL off. CloudFormation fails both before the table is touched: "Property [TimeToLiveSpecification] expected type: JSONObject, found: String", or "found: JSONArray" for a list. Ref AWS::NoValue still leaves the property out.
This commit is contained in:
1 parent
f1c7098d9c
commit
10b1a1bf8d
3 files changed
+101
-1
No files matched your search
+5
-1
@@ -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");
|
||||
|
||||
+57
@@ -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("<ResourceStatusReason>Property [TimeToLiveSpecification]"
|
||||
+ " expected type: JSONObject, found: String</ResourceStatusReason>"));
|
||||
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("<ResourceStatusReason>Property [TimeToLiveSpecification]"
|
||||
+ " expected type: JSONObject, found: JSONArray</ResourceStatusReason>"));
|
||||
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")
|
||||
|
||||
+39
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user