diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md index 5828867463..ff8fffd0ec 100644 --- a/docs/services/cloudformation.md +++ b/docs/services/cloudformation.md @@ -147,7 +147,7 @@ cross-resource references. | S3 | `Bucket`, `BucketPolicy` (document stored on the bucket; S3 does not evaluate it) | | SQS | `Queue`, `QueuePolicy` (accepted; policy not enforced) | | SNS | `Topic`, `Subscription`, `TopicPolicy` | -| DynamoDB | `Table`, `GlobalTable` | +| DynamoDB | `Table` (`TimeToLiveSpecification` applied on create and update; to rename an enabled TTL attribute, disable TTL in one update and enable the new attribute in a later one), `GlobalTable` | | Lambda | `Function` (Zip via S3/inline `ZipFile`, Image, and the `hot-reload` bind-mount bucket), `LayerVersion`, `EventSourceMapping` (SQS, Kinesis, DynamoDB Streams; `ParallelizationFactor` and `TumblingWindowInSeconds` are ignored), `Version`, `Alias` (also what SAM's `AutoPublishAlias` expands into), `Permission`, `EventInvokeConfig` (also what a SAM function's `EventInvokeConfig` expands into), `MicrovmImage`, `NetworkConnector`, `Url`. Inline `ZipFile` packages include the `cfn-response` (Node.js) / `cfnresponse` (Python) module AWS injects for that code path, so Solutions-style custom-resource handlers work. | | IAM | `Role`, `User` (template `LoginProfile` and `PermissionsBoundary` are ignored; an API-created login profile is removed on delete), `AccessKey`, `Policy`, `ManagedPolicy`, `InstanceProfile` | | Organizations | `Organization`, `OrganizationalUnit`, `Account`, `Policy`, `ResourcePolicy` | diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java index 61c1cf9021..c2f08f6604 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java @@ -196,6 +196,14 @@ public final class CfnRollback { */ public static final String REPLACEMENT_CLEANUP_ATTR = "__FlociReplacementCleanup"; + /** + * Holds the tags, stream setting and time-to-live setting a DynamoDB table carried before an + * in-place update changed them, with the account and region that address it, so a failed + * stack update can put all three back. Written by {@code DynamoDbCfnProvisioner} before its + * first mutating call and spent by its {@code rollbackUpdate}. + */ + public static final String DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR = "__FlociDynamoDbTableUpdateSnapshot"; + /** * Holds the attributes and tags a queue carried before an in-place update changed them, * so a failed stack update can restore them. 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 477fe2edeb..7158765324 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 @@ -1,11 +1,15 @@ 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.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; import io.github.hectorvent.floci.services.dynamodb.DynamoDbFacade; import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Scope; +import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbTableAccess.TimeToLive; import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition; import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex; import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; @@ -17,6 +21,7 @@ import org.jboss.logging.Logger; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -35,6 +40,11 @@ import java.util.Set; * applied directly against the DynamoDB service rather than through the async CDK Provider * framework, and its delete removes the replica the same way instead of invoking the handler. * {@code Ref} of a replica follows CDK's {@code -} format. + * + *

A table's {@code TimeToLiveSpecification} is reconciled against what DescribeTimeToLive + * reports, so UpdateTimeToLive is called only when the setting differs. An in-place update of an + * {@code AWS::DynamoDB::Table} keeps the tags, stream and TTL setting it replaces, so a failed + * stack update can put all three back. */ @ApplicationScoped public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { @@ -44,6 +54,8 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { static final String REPLICA = "Custom::DynamoDBReplica"; private static final Logger LOG = Logger.getLogger(DynamoDbCfnProvisioner.class); + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final TimeToLive TIME_TO_LIVE_OFF = new TimeToLive(false, null); private static final String REPLICA_TABLE_NAME_ATTR = "TableName"; private static final String REPLICA_REGION_ATTR = "__FlociDynamoDbReplicaRegion"; @@ -102,6 +114,8 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { } private void provisionTable(StackResource r, JsonNode props, ProvisionContext ctx) { + // A snapshot describes the update in flight; one an earlier update left behind is stale. + r.getAttributes().remove(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR); CloudFormationTemplateEngine engine = ctx.engine(); String region = ctx.region(); // TableName is create-only, so an update without one must keep the name generated on @@ -109,6 +123,12 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { // UpdateStack and re-pointed Ref at it. 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(); + // 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"); + TimeToLive desiredTtl = ttlDeclared ? declaredTimeToLive(ttlSpec, engine) : TIME_TO_LIVE_OFF; List keySchema = new ArrayList<>(); List attrDefs = new ArrayList<>(); @@ -184,8 +204,10 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { Scope scope = new Scope(ctx.accountId(), region); TableDefinition table; + boolean created = false; try { table = dynamoDb.tables().createTable(scope, tableName, keySchema, attrDefs, null, null, gsis, lsis); + created = true; } catch (AwsException e) { if (!"ResourceInUseException".equals(e.getErrorCode())) { throw e; @@ -193,48 +215,103 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { table = dynamoDb.tables().describeTable(scope, tableName); } - Map tags = parseCfnTags(props != null ? props.get("Tags") : null, engine); - List staleTags = ProvisionContext.staleTagKeys( - dynamoDb.tables().listTagsOfResource(scope, table.getTableArn()), tags); - if (!staleTags.isEmpty()) { - dynamoDb.tables().untagResource(scope, table.getTableArn(), staleTags); - } - if (!tags.isEmpty()) { - dynamoDb.tables().tagResource(scope, table.getTableArn(), tags); - } + try { + // TTL is read when the template declares it, and on an in-place update for the rollback + // snapshot, so UpdateTimeToLive is called only when something differs. A rename of an + // enabled TTL attribute fails here, and the snapshot is taken here, before anything changes. + // A table this attempt created has no prior state to snapshot; a failure deletes it instead. + boolean inPlaceUpdate = !created && ctx.reusesPriorEntity(tableName); + TimeToLive currentTtl = null; + if (ttlDeclared || inPlaceUpdate) { + currentTtl = dynamoDb.tables().timeToLive(scope, tableName); + requireTimeToLiveChangeAllowed(currentTtl, desiredTtl); + } + Map currentTags = dynamoDb.tables().listTagsOfResource(scope, table.getTableArn()); + if (inPlaceUpdate && TABLE.equals(r.getResourceType())) { + snapshotBeforeUpdate(r, scope, tableName, table, currentTags, currentTtl); + } - // A template that declares StreamSpecification wants a stream. Unlike the DynamoDB API, - // the CloudFormation property carries no StreamEnabled flag: declaring the block IS the - // request, so its presence alone turns the stream on. Without this the table is created - // streamless and an event source mapping polls its ARN forever. - // - // Removing the block on an update is the inverse request: the stream is reconciled off, - // or a table updated out of streaming would keep emitting records to whatever still holds - // its ARN. - JsonNode streamSpec = props != null ? props.path("StreamSpecification") : null; - if (streamSpec != null && streamSpec.isObject()) { - String viewType = streamSpec.has("StreamViewType") - ? engine.resolve(streamSpec.get("StreamViewType")) - : null; - table = dynamoDb.tables().enableStream(scope, tableName, viewType); - } else if (table.isStreamEnabled()) { - table = dynamoDb.tables().disableStream(scope, tableName); - } + reconcileTags(scope, table.getTableArn(), currentTags, + parseCfnTags(props != null ? props.get("Tags") : null, engine)); - r.setPhysicalId(tableName); - r.getAttributes().put("Arn", table.getTableArn()); - // Only the global table's registry schema declares TableId read-only; a plain table exposes - // Arn and StreamArn alone, so publishing it there would accept a Fn::GetAtt CloudFormation - // rejects. DescribeTable reports the same value, so Fn::GetAtt and the API agree. - if (GLOBAL_TABLE.equals(r.getResourceType())) { - r.getAttributes().put("TableId", table.getTableId()); - reconcileGlobalTableReplicas(tableName, props, ctx); + // A template that declares StreamSpecification wants a stream. Unlike the DynamoDB API, + // the CloudFormation property carries no StreamEnabled flag: declaring the block IS the + // request, so its presence alone turns the stream on. Without this the table is created + // streamless and an event source mapping polls its ARN forever. + // + // Removing the block on an update is the inverse request: the stream is reconciled off, + // or a table updated out of streaming would keep emitting records to whatever still holds + // its ARN. + JsonNode streamSpec = props != null ? props.path("StreamSpecification") : null; + if (streamSpec != null && streamSpec.isObject()) { + String viewType = streamSpec.has("StreamViewType") + ? engine.resolve(streamSpec.get("StreamViewType")) + : null; + table = dynamoDb.tables().enableStream(scope, tableName, viewType); + } else if (table.isStreamEnabled()) { + table = dynamoDb.tables().disableStream(scope, tableName); + } + + // TimeToLiveSpecification is reconciled like the stream: declaring it enabled turns TTL on + // for the attribute, and Enabled false or a block removed on an update turns it off. + if (ttlManaged && currentTtl != null) { + reconcileTimeToLive(scope, tableName, currentTtl, desiredTtl); + } + + r.setPhysicalId(tableName); + r.getAttributes().put("Arn", table.getTableArn()); + // Only the global table's registry schema declares TableId read-only; a plain table exposes + // Arn and StreamArn alone, so publishing it there would accept a Fn::GetAtt CloudFormation + // rejects. DescribeTable reports the same value, so Fn::GetAtt and the API agree. + if (GLOBAL_TABLE.equals(r.getResourceType())) { + r.getAttributes().put("TableId", table.getTableId()); + reconcileGlobalTableReplicas(tableName, props, ctx); + } + // Only a live stream has an ARN worth handing to Fn::GetAtt. Publishing one unconditionally + // resolved to nothing on a streamless table; publishing the retained ARN of a stream that + // has since been switched off would resolve to something no longer running. An update + // starts from the previous attributes, so the stale entry has to be removed rather than + // merely left unwritten. + publishStreamArn(r, table); + } catch (RuntimeException failure) { + // A table this attempt created would outlive the failure otherwise: the create rollback + // deletes only a resource that carries its physical id, and a replacing update puts the + // previous resource back. An adopted table, or one an in-place update changes, is kept. + // DeletionPolicy Retain does not keep it: AWS validates the tags and the stream inside + // CreateTable, so on AWS such a failure leaves no table to retain. + if (created) { + deleteCreatedTable(r, scope, tableName, ctx.isUpdate(), failure); + } + throw failure; } - // Only a live stream has an ARN worth handing to Fn::GetAtt. Publishing one unconditionally - // resolved to nothing on a streamless table; publishing the retained ARN of a stream that - // has since been switched off would resolve to something no longer running. An update - // starts from the previous attributes, so the stale entry has to be removed rather than - // merely left unwritten. + } + + /** + * Deletes the table a failed provision created. A delete that fails on create leaves the table + * owned by the resource, so the stack's create rollback retries it. On a replacing update the + * engine puts the previous resource back and never learns this table, so the failure is + * reported as a rollback failure that names it. + */ + private void deleteCreatedTable(StackResource r, Scope scope, String tableName, boolean update, + RuntimeException failure) { + try { + CfnDeletes.safeDelete("DynamoDB table", tableName, + () -> dynamoDb.tables().deleteTable(scope, tableName), "ResourceNotFoundException"); + } catch (RuntimeException cleanupFailure) { + failure.addSuppressed(cleanupFailure); + String reason = "Could not delete DynamoDB table " + tableName + ", created for " + + r.getLogicalId() + " by a failed provision: " + cleanupFailure.getMessage(); + LOG.warnv("{0}", reason); + if (update) { + r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR, reason); + } else { + r.setPhysicalId(tableName); + r.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true"); + } + } + } + + private static void publishStreamArn(StackResource r, TableDefinition table) { if (table.isStreamEnabled() && table.getStreamArn() != null) { r.getAttributes().put("StreamArn", table.getStreamArn()); } else { @@ -242,6 +319,141 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { } } + private void reconcileTags(Scope scope, String tableArn, Map current, + Map desired) { + List staleTags = ProvisionContext.staleTagKeys(current, desired); + if (!staleTags.isEmpty()) { + dynamoDb.tables().untagResource(scope, tableArn, staleTags); + } + if (!desired.isEmpty()) { + dynamoDb.tables().tagResource(scope, tableArn, desired); + } + } + + /** + * {@code Enabled} is required and may arrive as the string {@code "true"}, so it goes through + * the engine. Both checks run before the table is created, as AWS validates the template + * before any resource exists. The failures carry the error code AWS CloudFormation reports. + */ + private static TimeToLive declaredTimeToLive(JsonNode spec, CloudFormationTemplateEngine engine) { + JsonNode enabledNode = spec.get("Enabled"); + if (enabledNode == null || enabledNode.isNull()) { + throw new AwsException("InvalidRequest", + "Model validation failed (#/TimeToLiveSpecification: required key [Enabled] not found)", 400); + } + String enabledText = engine.resolve(enabledNode); + if (!"true".equalsIgnoreCase(enabledText) && !"false".equalsIgnoreCase(enabledText)) { + throw new AwsException("InvalidRequest", "Model validation failed (#/TimeToLiveSpecification/Enabled:" + + " expected type: Boolean, found: String)", 400); + } + boolean enabled = Boolean.parseBoolean(enabledText); + String attributeName = spec.has("AttributeName") ? engine.resolve(spec.get("AttributeName")) : null; + if (enabled && (attributeName == null || attributeName.isBlank())) { + throw new AwsException("InvalidRequest", "Invalid request provided: AttributeName property of" + + " TimeToLiveSpecification is required when TTL status is enabled or when enabling TTL.", 400); + } + return new TimeToLive(enabled, attributeName); + } + + /** + * DynamoDB refuses to enable a second attribute while one is enabled, and CloudFormation + * renames one by disabling TTL in an update and enabling the new attribute in a later one. The + * failure carries the handler message and error code AWS CloudFormation reports. + */ + private static void requireTimeToLiveChangeAllowed(TimeToLive current, TimeToLive desired) { + if (desired.enabled() && current.enabled() && !desired.attributeName().equals(current.attributeName())) { + throw new AwsException("InvalidRequest", "Invalid request provided: Cannot change time-to-live attribute" + + " name. To update this property, you must first disable TTL then enable TTL with the new" + + " attribute name.", 400); + } + } + + /** Calls UpdateTimeToLive only when the table's setting differs; disabling names the current attribute. */ + private void reconcileTimeToLive(Scope scope, String tableName, TimeToLive current, TimeToLive desired) { + if (desired.enabled() && !current.enabled()) { + dynamoDb.tables().updateTimeToLive(scope, tableName, desired.attributeName(), true); + } else if (!desired.enabled() && current.enabled()) { + dynamoDb.tables().updateTimeToLive(scope, tableName, current.attributeName(), false); + } + } + + /** + * Keeps the tags, stream setting and TTL setting the table has before an in-place update + * changes them, with the account and region that address it, so {@link #rollbackUpdate} can + * put all three back. + */ + private static void snapshotBeforeUpdate(StackResource r, Scope scope, String tableName, TableDefinition table, + Map tags, TimeToLive ttl) { + ObjectNode snapshot = MAPPER.createObjectNode(); + snapshot.put("accountId", scope.accountId()); + snapshot.put("region", scope.region()); + snapshot.put("tableName", tableName); + ObjectNode tagsNode = snapshot.putObject("tags"); + tags.forEach(tagsNode::put); + snapshot.put("streamEnabled", table.isStreamEnabled()); + snapshot.put("streamViewType", table.getStreamViewType()); + snapshot.put("ttlEnabled", ttl.enabled()); + snapshot.put("ttlAttributeName", ttl.attributeName()); + r.getAttributes().put(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR, snapshot.toString()); + } + + /** + * An in-place update that failed after its snapshot may already have changed the table, so the + * engine keeps this attempt for {@link #rollbackUpdate} instead of putting the previous resource + * back over it. A failure before the snapshot changed nothing and is left to the engine. + */ + @Override + public boolean retainsFailedUpdateState(StackResource resource) { + return resource.getAttributes().containsKey(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR); + } + + /** + * Puts back the tags, stream setting and TTL setting an in-place table update changed. All + * three go back or none is claimed: restoring one alone would report a rollback while the + * others stayed changed. Only an in-place {@code AWS::DynamoDB::Table} update takes the + * snapshot, so a global table or a replacing update still reports that rollback is not + * implemented. The snapshot is spent only once the restore succeeded: a restore that throws + * leaves it in place for the next attempt. + */ + @Override + public boolean rollbackUpdate(StackResource resource) { + String raw = resource.getAttributes().get(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR); + if (raw == null) { + return false; + } + JsonNode snapshot; + try { + snapshot = MAPPER.readTree(raw); + } catch (JsonProcessingException e) { + throw new IllegalStateException("Could not read the DynamoDB table update snapshot for " + + resource.getLogicalId(), e); + } + Scope scope = new Scope(snapshot.path("accountId").asText(), snapshot.path("region").asText()); + String tableName = snapshot.path("tableName").asText(); + TableDefinition table = dynamoDb.tables().describeTable(scope, tableName); + + Map tags = new LinkedHashMap<>(); + snapshot.path("tags").properties().forEach(tag -> tags.put(tag.getKey(), tag.getValue().asText())); + reconcileTags(scope, table.getTableArn(), + dynamoDb.tables().listTagsOfResource(scope, table.getTableArn()), tags); + + if (snapshot.path("streamEnabled").asBoolean()) { + table = dynamoDb.tables().enableStream(scope, tableName, snapshot.path("streamViewType").textValue()); + } else if (table.isStreamEnabled()) { + table = dynamoDb.tables().disableStream(scope, tableName); + } + + TimeToLive priorTtl = new TimeToLive(snapshot.path("ttlEnabled").asBoolean(), + snapshot.path("ttlAttributeName").textValue()); + TimeToLive currentTtl = dynamoDb.tables().timeToLive(scope, tableName); + requireTimeToLiveChangeAllowed(currentTtl, priorTtl); + reconcileTimeToLive(scope, tableName, currentTtl, priorTtl); + + publishStreamArn(resource, table); + resource.getAttributes().remove(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR); + return true; + } + /** * Reconciles a global table's tracked replica regions to its declared {@code Replicas} property. * Serves create and update: declared regions absent from the table are added and tracked regions diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java index d0ab1b53ca..b85b3a3874 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java @@ -476,6 +476,20 @@ public class DynamoDbLocalBackend implements DynamoDbBackend { return table(call(scope, "UpdateTable", body).path("TableDescription")); } + @Override + public TimeToLive timeToLive(Scope scope, String tableName) { + JsonNode description = call(scope, "DescribeTimeToLive", body(tableName)).path("TimeToLiveDescription"); + return new TimeToLive("ENABLED".equals(text(description, "TimeToLiveStatus")), + text(description, "AttributeName")); + } + + @Override + public void updateTimeToLive(Scope scope, String tableName, String attributeName, boolean enabled) { + ObjectNode body = body(tableName); + body.putObject("TimeToLiveSpecification").put("AttributeName", attributeName).put("Enabled", enabled); + call(scope, "UpdateTimeToLive", body); + } + private TableDefinition table(JsonNode description) { TableDefinition table = aws.convertValue(description, TableDefinition.class); JsonNode spec = description.path("StreamSpecification"); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbTableAccess.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbTableAccess.java index 7f03bae5f2..04986d8271 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbTableAccess.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbTableAccess.java @@ -15,6 +15,10 @@ import java.util.Optional; /** Typed table operations for Java consumers that manage DynamoDB tables directly. */ public interface DynamoDbTableAccess { + /** A table's time-to-live setting, as DescribeTimeToLive reports it. */ + record TimeToLive(boolean enabled, String attributeName) { + } + TableDefinition createTable(Scope scope, String tableName, List keySchema, List attributeDefinitions, Long readCapacity, Long writeCapacity, List globalSecondaryIndexes, @@ -34,6 +38,10 @@ public interface DynamoDbTableAccess { TableDefinition disableStream(Scope scope, String tableName); + TimeToLive timeToLive(Scope scope, String tableName); + + void updateTimeToLive(Scope scope, String tableName, String attributeName, boolean enabled); + Map listTagsOfResource(Scope scope, String resourceArn); void tagResource(Scope scope, String resourceArn, Map tags); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/NativeDynamoDbBackend.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/NativeDynamoDbBackend.java index 6d374daaff..0dc67a5aac 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/NativeDynamoDbBackend.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/NativeDynamoDbBackend.java @@ -193,6 +193,20 @@ public class NativeDynamoDbBackend implements DynamoDbBackend { return RequestScopes.callAs(scope.accountId(), () -> dynamoDbService.disableStream(tableName, scope.region())); } + // Mirrors the native DescribeTimeToLive: no attribute name is reported while TTL is off. + @Override + public TimeToLive timeToLive(Scope scope, String tableName) { + TableDefinition table = describeTable(scope, tableName); + boolean enabled = table.isTtlEnabled() && table.getTtlAttributeName() != null; + return new TimeToLive(enabled, enabled ? table.getTtlAttributeName() : null); + } + + @Override + public void updateTimeToLive(Scope scope, String tableName, String attributeName, boolean enabled) { + RequestScopes.runAs(scope.accountId(), + () -> dynamoDbService.updateTimeToLive(tableName, attributeName, enabled, scope.region())); + } + @Override public Map listTagsOfResource(Scope scope, String resourceArn) { return RequestScopes.callAs(scope.accountId(), 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 new file mode 100644 index 0000000000..2e5e603185 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbTimeToLiveIntegrationTest.java @@ -0,0 +1,341 @@ +package io.github.hectorvent.floci.services.cloudformation; + +import io.github.hectorvent.floci.services.dynamodb.DynamoDbService; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectSpy; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyString; + +/** + * {@code TimeToLiveSpecification} on {@code AWS::DynamoDB::Table}: the setting a stack declares is + * what DescribeTimeToLive reports after create, after an update that disables it and after an + * update that removes the block, and a failed update puts the previous setting back. A failure + * inside the table's own provision is injected through a spy on the DynamoDB service. + */ +@QuarkusTest +class CloudFormationDynamoDbTimeToLiveIntegrationTest { + + private static final String CFN_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request"; + private static final String DDB_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/dynamodb/aws4_request"; + + private static final String TEMPLATE = """ + { + "Resources": { + "Orders": { + "Type": "AWS::DynamoDB::Table", + "Properties": { + "TableName": "%s", + "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}], + "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}], + "BillingMode": "PAY_PER_REQUEST", + "Tags": [{"Key": "env", "Value": "%s"}]%s + } + }%s + } + } + """; + + private static final String TTL_ENABLED = + ",\n\"TimeToLiveSpecification\": {\"AttributeName\": \"expiresAt\", \"Enabled\": true}"; + private static final String TTL_DISABLED = + ",\n\"TimeToLiveSpecification\": {\"AttributeName\": \"expiresAt\", \"Enabled\": false}"; + private static final String TTL_RENAMED = + ",\n\"TimeToLiveSpecification\": {\"AttributeName\": \"deleteAfter\", \"Enabled\": true}"; + private static final String TTL_WITHOUT_ATTRIBUTE = + ",\n\"TimeToLiveSpecification\": {\"Enabled\": true}"; + private static final String TTL_WITHOUT_ENABLED = + ",\n\"TimeToLiveSpecification\": {\"AttributeName\": \"expiresAt\"}"; + private static final String STREAMED = + ",\n\"StreamSpecification\": {\"StreamViewType\": \"NEW_AND_OLD_IMAGES\"}"; + + /** A resource that fails after the table, so the update that changed the table rolls back. */ + private static final String FAILING_RESOURCE = """ + , + "BadSecret": { + "Type": "AWS::SecretsManager::Secret", + "DependsOn": "Orders", + "Properties": { + "Name": "ttl-rollback-%s", + "SecretString": "explicit", + "GenerateSecretString": {"PasswordLength": 32} + } + }"""; + + @InjectSpy + DynamoDbService dynamoDbService; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void createEnablesTheDeclaredTimeToLiveAndEnabledFalseDisablesIt() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-" + suffix; + String table = "ttl-table-" + suffix; + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", TTL_ENABLED, "")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + describeTimeToLive(table) + .body("TimeToLiveDescription.TimeToLiveStatus", equalTo("ENABLED")) + .body("TimeToLiveDescription.AttributeName", equalTo("expiresAt")); + + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(table, "dev", TTL_DISABLED, "")); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + describeTimeToLive(table) + .body("TimeToLiveDescription.TimeToLiveStatus", equalTo("DISABLED")) + .body("TimeToLiveDescription.AttributeName", nullValue()); + + deleteStack(stack); + } + + @Test + void removingTheTimeToLiveBlockDisablesIt() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-rm-" + suffix; + String table = "ttl-rm-table-" + suffix; + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", TTL_ENABLED, "")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + describeTimeToLive(table).body("TimeToLiveDescription.TimeToLiveStatus", equalTo("ENABLED")); + + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(table, "dev", "", "")); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + describeTimeToLive(table).body("TimeToLiveDescription.TimeToLiveStatus", equalTo("DISABLED")); + + deleteStack(stack); + } + + @Test + void anUpdateThatFailsOnALaterResourcePutsTheTableBack() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-rb-" + suffix; + String table = "ttl-rb-table-" + suffix; + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", TTL_ENABLED, "")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + String tableArn = dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}") + .statusCode(200) + .extract().path("Table.TableArn"); + + cloudFormation(stack, "UpdateStack", + TEMPLATE.formatted(table, "prod", TTL_DISABLED, FAILING_RESOURCE.formatted(suffix))); + assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + + describeTimeToLive(table) + .body("TimeToLiveDescription.TimeToLiveStatus", equalTo("ENABLED")) + .body("TimeToLiveDescription.AttributeName", equalTo("expiresAt")); + dynamoDb("ListTagsOfResource", "{\"ResourceArn\": \"" + tableArn + "\"}") + .statusCode(200) + .body("Tags.Value", contains("dev")); + + deleteStack(stack); + } + + /** + * The table's own update changes the tags and switches the stream off, then its TTL call fails. + * The rollback must put the tags and the stream back, not just report that it did. + */ + @Test + void anUpdateThatFailsInsideTheTablePutsItsTagsAndStreamBack() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-own-" + suffix; + String table = "ttl-own-table-" + suffix; + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", STREAMED, "")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + String tableArn = dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}") + .statusCode(200) + .extract().path("Table.TableArn"); + + failNextUpdateTimeToLive(); + try { + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(table, "prod", TTL_ENABLED, "")); + assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + } finally { + Mockito.doCallRealMethod().when(dynamoDbService) + .updateTimeToLive(anyString(), anyString(), anyBoolean(), anyString()); + } + + dynamoDb("ListTagsOfResource", "{\"ResourceArn\": \"" + tableArn + "\"}") + .statusCode(200) + .body("Tags.Value", contains("dev")); + dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}") + .statusCode(200) + .body("Table.StreamSpecification.StreamEnabled", equalTo(true)) + .body("Table.StreamSpecification.StreamViewType", equalTo("NEW_AND_OLD_IMAGES")); + describeTimeToLive(table).body("TimeToLiveDescription.TimeToLiveStatus", equalTo("DISABLED")); + + deleteStack(stack); + } + + /** A create that fails after the table exists rolls back to no table, not to an orphan. */ + @Test + void aCreateThatFailsAfterTheTableExistsLeavesNoTable() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-orphan-" + suffix; + String table = "ttl-orphan-table-" + suffix; + + failNextUpdateTimeToLive(); + try { + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", TTL_ENABLED, "")); + assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + } finally { + Mockito.doCallRealMethod().when(dynamoDbService) + .updateTimeToLive(anyString(), anyString(), anyBoolean(), anyString()); + } + + dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}") + .statusCode(400) + .body("__type", containsString("ResourceNotFoundException")); + + deleteStack(stack); + } + + /** As measured on AWS: the rename fails the table before any DynamoDB change and the update rolls back. */ + @Test + void renamingAnEnabledAttributeFailsWithTheAwsReasonAndRollsBack() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-rename-" + suffix; + String table = "ttl-rename-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", TTL_RENAMED, "")); + assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + + describeStackEvents(stack).body(containsString("Invalid request provided: Cannot change" + + " time-to-live attribute name. To update this property, you must first disable TTL then enable" + + " TTL with the new attribute name.")); + describeTimeToLive(table) + .body("TimeToLiveDescription.TimeToLiveStatus", equalTo("ENABLED")) + .body("TimeToLiveDescription.AttributeName", equalTo("expiresAt")); + + deleteStack(stack); + } + + /** As measured on AWS: the create fails with the reason below and leaves no table. */ + @Test + void enablingWithoutAnAttributeNameFailsTheCreateWithTheAwsReason() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-noattr-" + suffix; + String table = "ttl-noattr-table-" + suffix; + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", TTL_WITHOUT_ATTRIBUTE, "")); + assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + + describeStackEvents(stack).body(containsString("Invalid request provided: AttributeName" + + " property of TimeToLiveSpecification is required when TTL status is enabled or when enabling" + + " TTL.")); + dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}") + .statusCode(400) + .body("__type", containsString("ResourceNotFoundException")); + + deleteStack(stack); + } + + /** As measured on AWS: an update leaves alone TTL enabled outside a stack that never declared it. */ + @Test + void timeToLiveSetOutsideAStackThatNeverDeclaredItSurvivesATagOnlyUpdate() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-oob-" + suffix; + String table = "ttl-oob-table-" + suffix; + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", "", "")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + dynamoDb("UpdateTimeToLive", "{\"TableName\": \"" + table + "\", \"TimeToLiveSpecification\":" + + " {\"AttributeName\": \"expiresAt\", \"Enabled\": true}}").statusCode(200); + + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(table, "prod", "", "")); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + describeTimeToLive(table) + .body("TimeToLiveDescription.TimeToLiveStatus", equalTo("ENABLED")) + .body("TimeToLiveDescription.AttributeName", equalTo("expiresAt")); + + deleteStack(stack); + } + + /** The registry schema requires Enabled, so the block is rejected before a table exists. */ + @Test + void aTimeToLiveBlockWithoutEnabledFailsTheCreateAndLeavesNoTable() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-noen-" + suffix; + String table = "ttl-noen-table-" + suffix; + + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", TTL_WITHOUT_ENABLED, "")); + assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + + describeStackEvents(stack).body(containsString("Model validation failed" + + " (#/TimeToLiveSpecification: required key [Enabled] not found)")); + dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}") + .statusCode(400) + .body("__type", containsString("ResourceNotFoundException")); + + deleteStack(stack); + } + + private ValidatableResponse describeStackEvents(String stack) { + return given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", "DescribeStackEvents") + .formParam("StackName", stack) + .when().post("/").then().statusCode(200); + } + + private void failNextUpdateTimeToLive() { + Mockito.doThrow(new IllegalStateException("simulated UpdateTimeToLive failure")) + .doCallRealMethod() + .when(dynamoDbService) + .updateTimeToLive(anyString(), anyString(), anyBoolean(), anyString()); + } + + private void cloudFormation(String stack, String action, String template) { + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", action) + .formParam("StackName", stack) + .formParam("TemplateBody", template) + .when().post("/").then().statusCode(200); + } + + private void deleteStack(String stack) { + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", "DeleteStack") + .formParam("StackName", stack) + .when().post("/").then().statusCode(200); + CfnStackWaits.awaitStackDeleted(stack, CFN_AUTH); + } + + private ValidatableResponse describeTimeToLive(String table) { + return dynamoDb("DescribeTimeToLive", "{\"TableName\": \"" + table + "\"}").statusCode(200); + } + + private ValidatableResponse dynamoDb(String action, String body) { + return given() + .contentType("application/x-amz-json-1.0") + .header("Authorization", DDB_AUTH) + .header("X-Amz-Target", "DynamoDB_20120810." + action) + .body(body) + .when().post("/").then(); + } +} 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 0059ec46a7..1f0bfc888b 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 @@ -19,11 +19,15 @@ import java.util.Map; import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -33,7 +37,9 @@ import static org.mockito.Mockito.when; /** * The DynamoDB CFN provisioner in isolation: the exact {@code Fn::GetAtt} keys the registry - * schema declares, and the delete contract for each of its three types. The stream and replica + * schema declares, the delete contract for each of its three types, the + * {@code TimeToLiveSpecification} reconcile, and the snapshot a failed table update rolls back + * from. The stream and replica * behaviour keeps its own coverage in {@code DynamoDbStreamSpecificationCfnProvisionerTest} and * {@code DynamoDbReplicaCfnProvisionerTest}, which reach this class through the fixture. */ @@ -43,6 +49,8 @@ class DynamoDbCfnProvisionerTest { private static final String TABLE_ARN = "arn:aws:dynamodb:us-east-1:000000000000:table/" + TABLE_NAME; private static final String TABLE_ID = "2d4c5c3a-1b6e-4a1d-9f0a-7b1c2d3e4f50"; private static final String STREAM_ARN = TABLE_ARN + "/stream/2026-01-01T00:00:00.000"; + private static final String TTL_PROPS = "{\"TableName\":\"orders\",\"TimeToLiveSpecification\":" + + "{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}"; private final DynamoDbService dynamoDb = mock(DynamoDbService.class); private final ObjectMapper mapper = new ObjectMapper(); @@ -67,6 +75,13 @@ class DynamoDbCfnProvisionerTest { create.stackName(), priorPhysicalId); } + /** The update path, with the properties the previous template declared. */ + private ProvisionContext updateCtx(String priorPhysicalId, String priorProperties) throws Exception { + ProvisionContext create = ctx(); + return new ProvisionContext(create.engine(), create.region(), create.accountId(), + create.stackName(), priorPhysicalId, event -> { }, props(priorProperties)); + } + private StackResource resource(String type, String logicalId) { StackResource r = new StackResource(); r.setLogicalId(logicalId); @@ -310,7 +325,7 @@ class DynamoDbCfnProvisionerTest { assertEquals(priorName, r.getPhysicalId()); verify(dynamoDb).createTable(eq(priorName), anyList(), anyList(), any(), any(), anyList(), anyList(), eq("us-east-1")); - verify(dynamoDb).describeTable(priorName, "us-east-1"); + verify(dynamoDb, atLeastOnce()).describeTable(priorName, "us-east-1"); } @Test @@ -326,4 +341,393 @@ class DynamoDbCfnProvisionerTest { verify(dynamoDb).untagResource(TABLE_ARN, List.of("old"), "us-east-1"); verify(dynamoDb).tagResource(TABLE_ARN, Map.of("env", "prod"), "us-east-1"); } + + private TableDefinition withTimeToLive(TableDefinition table, String attributeName) { + table.setTtlEnabled(true); + table.setTtlAttributeName(attributeName); + return table; + } + + /** The update path: the table already exists and reports {@code current}. */ + private void existingTable(TableDefinition current) { + when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString())) + .thenThrow(new AwsException("ResourceInUseException", "Table already exists", 400)); + when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(current); + } + + @Test + void createWithTimeToLiveEnablesItOnce() throws Exception { + when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString())) + .thenReturn(table(false)); + when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false)); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + + provisioner.provision(r, props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":" + + "{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}"), ctx()); + + verify(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", true, "us-east-1"); + } + + @Test + void enabledGivenAsAStringIsResolvedThroughTheEngine() throws Exception { + when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString())) + .thenReturn(table(false)); + when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false)); + ProvisionContext ctx = ctx(); + when(ctx.engine().resolve(any())).thenAnswer(inv -> { + JsonNode node = inv.getArgument(0); + if (node != null && node.has("Ref")) { + return "true"; + } + return node == null ? null : node.asText(); + }); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + + provisioner.provision(r, props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":" + + "{\"AttributeName\":\"expiresAt\",\"Enabled\":{\"Ref\":\"TtlOn\"}}}"), ctx); + + verify(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", true, "us-east-1"); + } + + @Test + void updateWithTheSameTimeToLiveMakesNoTimeToLiveCall() throws Exception { + existingTable(withTimeToLive(table(false), "expiresAt")); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + + provisioner.provision(r, props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":" + + "{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}"), updateCtx(TABLE_NAME)); + + verify(dynamoDb, never()).updateTimeToLive(anyString(), any(), anyBoolean(), anyString()); + } + + @Test + void updateWithEnabledFalseDisablesTheCurrentAttribute() throws Exception { + existingTable(withTimeToLive(table(false), "expiresAt")); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + + provisioner.provision(r, props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":{\"Enabled\":false}}"), + updateCtx(TABLE_NAME)); + + verify(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", false, "us-east-1"); + } + + @Test + void updateThatRemovesTheTimeToLiveBlockDisablesIt() throws Exception { + existingTable(withTimeToLive(table(false), "expiresAt")); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + + provisioner.provision(r, props("{\"TableName\":\"orders\"}"), updateCtx(TABLE_NAME, TTL_PROPS)); + + verify(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", false, "us-east-1"); + } + + /** As measured on AWS: TTL enabled outside a stack that never declared it survives a tag-only update. */ + @Test + void updateOfAStackThatNeverDeclaredTimeToLiveLeavesItAlone() throws Exception { + existingTable(withTimeToLive(table(false), "expiresAt")); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + + provisioner.provision(r, props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]}"), + updateCtx(TABLE_NAME, "{\"TableName\":\"orders\"}")); + + verify(dynamoDb, never()).updateTimeToLive(anyString(), any(), anyBoolean(), anyString()); + } + + @Test + void timeToLiveWithoutEnabledFailsModelValidationBeforeTheTableIsCreated() throws Exception { + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":{\"AttributeName\":\"exp\"}}"); + ProvisionContext ctx = ctx(); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertEquals("InvalidRequest", e.getErrorCode()); + assertEquals("Model validation failed (#/TimeToLiveSpecification: required key [Enabled] not found)", + e.getMessage()); + verifyNoInteractions(dynamoDb); + } + + @Test + void timeToLiveWithANonBooleanEnabledFailsModelValidationBeforeTheTableIsCreated() throws Exception { + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":" + + "{\"AttributeName\":\"exp\",\"Enabled\":\"tru\"}}"); + ProvisionContext ctx = ctx(); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertEquals("InvalidRequest", e.getErrorCode()); + assertEquals("Model validation failed (#/TimeToLiveSpecification/Enabled: expected type: Boolean," + + " found: String)", e.getMessage()); + verifyNoInteractions(dynamoDb); + } + + /** 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 { + existingTable(withTimeToLive(table(false), "expiresAt")); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + // Left by an earlier successful update; it must not make the engine skip its own restore. + r.getAttributes().put(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR, "{}"); + JsonNode props = props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]," + + "\"TimeToLiveSpecification\":{\"AttributeName\":\"deleteAfter\",\"Enabled\":true}}"); + ProvisionContext ctx = updateCtx(TABLE_NAME); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertEquals("InvalidRequest", e.getErrorCode()); + assertEquals("Invalid request provided: Cannot change time-to-live attribute name. To update this property," + + " you must first disable TTL then enable TTL with the new attribute name.", e.getMessage()); + assertEquals(400, e.getHttpStatus()); + verify(dynamoDb, never()).updateTimeToLive(anyString(), any(), anyBoolean(), anyString()); + verify(dynamoDb, never()).tagResource(anyString(), any(), anyString()); + assertFalse(provisioner.retainsFailedUpdateState(r)); + verify(dynamoDb, never()).deleteTable(anyString(), anyString()); + } + + /** + * The table's own update fails after it changed the tags and the stream. The engine must keep + * this attempt, snapshot included, and hand it to rollbackUpdate rather than put the previous + * resource back and report a rollback that never happened. + */ + @Test + void inPlaceUpdateThatFailsAfterItsSnapshotIsRetainedAndRolledBack() throws Exception { + existingTable(table(true)); + when(dynamoDb.listTagsOfResource(TABLE_ARN, "us-east-1")).thenReturn(Map.of("env", "dev")); + when(dynamoDb.disableStream(TABLE_NAME, "us-east-1")).thenReturn(table(false)); + doThrow(new AwsException("InternalServerError", "boom", 500)) + .when(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", true, "us-east-1"); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + r.getAttributes().put("Arn", TABLE_ARN); + r.getAttributes().put("StreamArn", STREAM_ARN); + JsonNode props = props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]," + + "\"TimeToLiveSpecification\":{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}"); + ProvisionContext ctx = updateCtx(TABLE_NAME); + + assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertTrue(provisioner.retainsFailedUpdateState(r)); + assertEquals(TABLE_NAME, r.getPhysicalId()); + assertEquals(TABLE_ARN, r.getAttributes().get("Arn")); + verify(dynamoDb, never()).deleteTable(anyString(), anyString()); + + when(dynamoDb.listTagsOfResource(TABLE_ARN, "us-east-1")).thenReturn(Map.of("env", "prod")); + when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false)); + when(dynamoDb.enableStream(TABLE_NAME, "NEW_AND_OLD_IMAGES", "us-east-1")).thenReturn(table(true)); + + assertTrue(provisioner.rollbackUpdate(r)); + verify(dynamoDb).tagResource(TABLE_ARN, Map.of("env", "dev"), "us-east-1"); + verify(dynamoDb).enableStream(TABLE_NAME, "NEW_AND_OLD_IMAGES", "us-east-1"); + verify(dynamoDb, never()).updateTimeToLive(TABLE_NAME, "expiresAt", false, "us-east-1"); + assertFalse(provisioner.retainsFailedUpdateState(r)); + } + + private void createdTableWhoseTimeToLiveCallFails() { + when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString())) + .thenReturn(table(false)); + when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false)); + doThrow(new AwsException("LimitExceededException", "boom", 400)) + .when(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", true, "us-east-1"); + } + + @Test + void createThatFailsAfterCreatingTheTableDeletesIt() throws Exception { + createdTableWhoseTimeToLiveCallFails(); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + JsonNode props = props(TTL_PROPS); + ProvisionContext ctx = ctx(); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertEquals("LimitExceededException", e.getErrorCode()); + verify(dynamoDb).deleteTable(TABLE_NAME, "us-east-1"); + assertFalse(r.getAttributes().containsKey(CfnRollback.ROLLBACK_OWNED_ATTR)); + } + + @Test + void replacingUpdateThatFailsDeletesTheNewTableAndLeavesTheOldOne() throws Exception { + createdTableWhoseTimeToLiveCallFails(); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId("orders-v1"); + JsonNode props = props(TTL_PROPS); + ProvisionContext ctx = updateCtx("orders-v1"); + + assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + verify(dynamoDb).deleteTable(TABLE_NAME, "us-east-1"); + verify(dynamoDb, never()).deleteTable("orders-v1", "us-east-1"); + assertFalse(r.getAttributes().containsKey(CfnRollback.ROLLBACK_OWNED_ATTR)); + } + + /** A table this attempt did not create, the update target or one adopted on create, is never deleted. */ + @Test + void failureOnATableThisAttemptDidNotCreateDeletesNothing() throws Exception { + existingTable(table(false)); + doThrow(new AwsException("LimitExceededException", "boom", 400)) + .when(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", true, "us-east-1"); + JsonNode props = props(TTL_PROPS); + + for (ProvisionContext ctx : List.of(updateCtx(TABLE_NAME), ctx())) { + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(ctx.priorPhysicalId()); + + assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertFalse(r.getAttributes().containsKey(CfnRollback.ROLLBACK_OWNED_ATTR)); + } + verify(dynamoDb, never()).deleteTable(anyString(), anyString()); + } + + /** A cleanup that fails leaves the table to the engine's create rollback, which retries the delete. */ + @Test + void createWhoseCleanupFailsLeavesTheTableOwnedForTheStackRollback() throws Exception { + createdTableWhoseTimeToLiveCallFails(); + doThrow(new AwsException("InternalServerError", "delete failed", 500)) + .when(dynamoDb).deleteTable(TABLE_NAME, "us-east-1"); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + JsonNode props = props(TTL_PROPS); + ProvisionContext ctx = ctx(); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertEquals("LimitExceededException", e.getErrorCode()); + assertEquals(TABLE_NAME, r.getPhysicalId()); + assertEquals("true", r.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR)); + } + + /** + * The engine puts the previous resource back after a failed update and never learns the new + * table, so a cleanup that fails there is reported as a rollback failure naming it. + */ + @Test + void replacingUpdateWhoseCleanupFailsReportsTheTableAsARollbackFailure() throws Exception { + createdTableWhoseTimeToLiveCallFails(); + doThrow(new AwsException("InternalServerError", "delete failed", 500)) + .when(dynamoDb).deleteTable(TABLE_NAME, "us-east-1"); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId("orders-v1"); + JsonNode props = props(TTL_PROPS); + ProvisionContext ctx = updateCtx("orders-v1"); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertEquals("LimitExceededException", e.getErrorCode()); + assertEquals(1, e.getSuppressed().length); + String reason = r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR); + assertTrue(reason != null && reason.contains(TABLE_NAME) && reason.contains("delete failed"), reason); + assertFalse(r.getAttributes().containsKey(CfnRollback.ROLLBACK_OWNED_ATTR)); + } + + /** + * The update target was deleted outside the stack, so this attempt created it again. That table + * is this attempt's to delete, and there is no prior state to snapshot for a rollback. + */ + @Test + void updateThatRecreatesAMissingTableAndFailsDeletesItAndKeepsNoSnapshot() throws Exception { + createdTableWhoseTimeToLiveCallFails(); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + JsonNode props = props(TTL_PROPS); + ProvisionContext ctx = updateCtx(TABLE_NAME); + + assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + verify(dynamoDb).deleteTable(TABLE_NAME, "us-east-1"); + assertFalse(provisioner.retainsFailedUpdateState(r)); + } + + @Test + void successfulCreateLeavesNoOwnershipMarker() throws Exception { + when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString())) + .thenReturn(table(false)); + when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false)); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + + provisioner.provision(r, props(TTL_PROPS), ctx()); + + assertFalse(r.getAttributes().containsKey(CfnRollback.ROLLBACK_OWNED_ATTR)); + verify(dynamoDb, never()).deleteTable(anyString(), anyString()); + } + + @Test + void enabledTimeToLiveWithoutAnAttributeNameFailsBeforeTheTableIsCreated() throws Exception { + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":{\"Enabled\":true}}"); + ProvisionContext ctx = ctx(); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx)); + + assertEquals("InvalidRequest", e.getErrorCode()); + assertEquals("Invalid request provided: AttributeName property of TimeToLiveSpecification is required" + + " when TTL status is enabled or when enabling TTL.", e.getMessage()); + assertEquals(400, e.getHttpStatus()); + verifyNoInteractions(dynamoDb); + } + + @Test + void rollbackUpdateRestoresTagsStreamAndTimeToLive() throws Exception { + existingTable(table(true)); + when(dynamoDb.listTagsOfResource(TABLE_ARN, "us-east-1")).thenReturn(Map.of("env", "dev")); + when(dynamoDb.disableStream(TABLE_NAME, "us-east-1")).thenReturn(table(false)); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + r.getAttributes().put("StreamArn", STREAM_ARN); + provisioner.provision(r, props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]," + + "\"TimeToLiveSpecification\":{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}"), + updateCtx(TABLE_NAME)); + // The table as the update left it: retagged, streamless and expiring. + when(dynamoDb.listTagsOfResource(TABLE_ARN, "us-east-1")).thenReturn(Map.of("env", "prod")); + when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(withTimeToLive(table(false), "expiresAt")); + when(dynamoDb.enableStream(TABLE_NAME, "NEW_AND_OLD_IMAGES", "us-east-1")).thenReturn(table(true)); + + boolean rolledBack = provisioner.rollbackUpdate(r); + + assertTrue(rolledBack); + verify(dynamoDb).tagResource(TABLE_ARN, Map.of("env", "dev"), "us-east-1"); + verify(dynamoDb).enableStream(TABLE_NAME, "NEW_AND_OLD_IMAGES", "us-east-1"); + verify(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", false, "us-east-1"); + assertEquals(STREAM_ARN, r.getAttributes().get("StreamArn")); + assertFalse(r.getAttributes().containsKey(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR)); + } + + @Test + void rollbackUpdateThatFailsKeepsTheSnapshotForTheNextAttempt() throws Exception { + existingTable(table(false)); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(TABLE_NAME); + provisioner.provision(r, props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]}"), + updateCtx(TABLE_NAME)); + when(dynamoDb.listTagsOfResource(TABLE_ARN, "us-east-1")).thenReturn(Map.of("env", "prod")); + doThrow(new AwsException("InternalServerError", "boom", 500)) + .when(dynamoDb).untagResource(TABLE_ARN, List.of("env"), "us-east-1"); + + assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r)); + + assertTrue(r.getAttributes().containsKey(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR)); + } + + /** + * A snapshot describes the update in flight. One left by an earlier update is dropped, so a + * replacing update, which takes none, cannot roll the new table back to the old one's state. + */ + @Test + void replacingUpdateDropsAStaleSnapshotAndReportsNoRollback() throws Exception { + when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString())) + .thenReturn(table(false)); + when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false)); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId("orders-v1"); + r.getAttributes().put(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR, "{}"); + + provisioner.provision(r, props("{\"TableName\":\"orders\"}"), updateCtx("orders-v1")); + + assertFalse(r.getAttributes().containsKey(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR)); + assertFalse(provisioner.rollbackUpdate(r)); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackendIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackendIntegrationTest.java index 25835b1f08..01d2d2c812 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackendIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackendIntegrationTest.java @@ -14,6 +14,7 @@ import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTestProfile; import io.quarkus.test.junit.TestProfile; import io.restassured.response.ValidatableResponse; +import io.restassured.specification.RequestSpecification; import jakarta.inject.Inject; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -32,6 +33,7 @@ import static io.restassured.RestAssured.given; import static org.awaitility.Awaitility.await; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -58,8 +60,12 @@ class DynamoDbLocalBackendIntegrationTest { private record Caller(String account, String region) { String authorization() { + return authorization("dynamodb"); + } + + String authorization(String service) { return "AWS4-HMAC-SHA256 Credential=" + account + "/20260215/" + region - + "/dynamodb/aws4_request, SignedHeaders=host, Signature=abc"; + + "/" + service + "/aws4_request, SignedHeaders=host, Signature=abc"; } } @@ -252,6 +258,39 @@ class DynamoDbLocalBackendIntegrationTest { .body("__type", endsWith("ResourceNotFoundException")); } + @Test + void theTimeToLiveAStackEnablesIsWhatLocalReports() { + String table = tableName("ttl"); + String stack = "local-it-ttl-" + UUID.randomUUID(); + String template = """ + { + "Resources": { + "Orders": { + "Type": "AWS::DynamoDB::Table", + "Properties": { + "TableName": "%s", + "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}], + "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}], + "BillingMode": "PAY_PER_REQUEST", + "TimeToLiveSpecification": {"AttributeName": "expiresAt", "Enabled": true} + } + } + } + } + """.formatted(table); + + cloudFormation(A_EAST, "CreateStack", stack, template); + // The stack stays in Floci; its table is removed from Local with the others. + created.add(new CreatedTable(A_EAST, table)); + cloudFormation(A_EAST, "DescribeStacks", stack, null) + .body(containsString("CREATE_COMPLETE")); + + dynamoDb(A_EAST, "DescribeTimeToLive", "{\"TableName\": \"" + table + "\"}") + .statusCode(200) + .body("TimeToLiveDescription.TimeToLiveStatus", equalTo("ENABLED")) + .body("TimeToLiveDescription.AttributeName", equalTo("expiresAt")); + } + @Test void resetIsRefused() { given().when().post("/_floci/state/reset").then().statusCode(409); @@ -296,6 +335,18 @@ class DynamoDbLocalBackendIntegrationTest { return dynamoDb(caller, "Scan", "{\"TableName\": \"" + table + "\"}").statusCode(200); } + private static ValidatableResponse cloudFormation(Caller caller, String action, String stack, String template) { + RequestSpecification request = given() + .header("Authorization", caller.authorization("cloudformation")) + .contentType("application/x-www-form-urlencoded") + .formParam("Action", action) + .formParam("StackName", stack); + if (template != null) { + request.formParam("TemplateBody", template); + } + return request.when().post("/").then().statusCode(200); + } + private static ValidatableResponse dynamoDb(Caller caller, String action, String body) { return send(caller, "DynamoDB_20120810." + action, body); } diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackendTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackendTest.java index 3d64822d74..8826a1afb4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackendTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackendTest.java @@ -10,6 +10,7 @@ import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.A import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Call; import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Reply; import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Scope; +import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbTableAccess.TimeToLive; import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition; import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex; import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; @@ -629,6 +630,35 @@ class DynamoDbLocalBackendTest { neverSent("UpdateTable"); } + @Test + void timeToLiveReadsDescribeTimeToLive() throws Exception { + answer("DescribeTimeToLive", 200, "{\"TimeToLiveDescription\":" + + "{\"TimeToLiveStatus\":\"ENABLED\",\"AttributeName\":\"expiresAt\"}}"); + + TimeToLive ttl = backend.timeToLive(SCOPE, "orders"); + + assertEquals(new TimeToLive(true, "expiresAt"), ttl); + assertEquals(json("{\"TableName\":\"orders\"}"), sent("DescribeTimeToLive")); + } + + @Test + void timeToLiveDisabledIsOff() throws Exception { + answer("DescribeTimeToLive", 200, "{\"TimeToLiveDescription\":{\"TimeToLiveStatus\":\"DISABLED\"}}"); + + assertEquals(new TimeToLive(false, null), backend.timeToLive(SCOPE, "orders")); + } + + @Test + void updateTimeToLiveSendsTheSpecification() throws Exception { + answer("UpdateTimeToLive", 200, "{\"TimeToLiveSpecification\":" + + "{\"AttributeName\":\"expiresAt\",\"Enabled\":false}}"); + + backend.updateTimeToLive(SCOPE, "orders", "expiresAt", false); + + assertEquals(json("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":" + + "{\"AttributeName\":\"expiresAt\",\"Enabled\":false}}"), sent("UpdateTimeToLive")); + } + @Test void updateItemReturnsOnlyImages() throws Exception { answer("UpdateItem", 200, "{\"Attributes\":{\"pk\":{\"S\":\"a\"},\"n\":{\"N\":\"2\"}}}"); diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbNativeBackendTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbNativeBackendTest.java index 53c209449c..fbdbb04871 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbNativeBackendTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbNativeBackendTest.java @@ -22,6 +22,7 @@ import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.A import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Call; import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Reply; import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Scope; +import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbTableAccess.TimeToLive; import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition; import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; import io.github.hectorvent.floci.services.kinesis.KinesisService; @@ -179,6 +180,29 @@ class DynamoDbNativeBackendTest { assertEquals(json("{\"id\":{\"S\":\"k\"},\"n\":{\"N\":\"5\"}}"), backend.getItem(SCOPE, TABLE, key)); } + @Test + void typedTimeToLiveIsWhatDescribeTimeToLiveReportsUnderTheScopesAccount() throws Exception { + Scope owner = new Scope("111122223333", REGION); + backend.createTable(owner, TABLE, List.of(new KeySchemaElement("id", "HASH")), + List.of(new AttributeDefinition("id", "S")), 5L, 5L, List.of(), List.of()); + JsonNode describe = json("{\"TableName\":\"" + TABLE + "\"}"); + + assertEquals(new TimeToLive(false, null), backend.timeToLive(owner, TABLE)); + + backend.updateTimeToLive(owner, TABLE, "expiresAt", true); + + assertEquals(new TimeToLive(true, "expiresAt"), backend.timeToLive(owner, TABLE)); + JsonNode enabled = backend.execute(new Call(owner, Api.DYNAMODB, "DescribeTimeToLive", describe)).body(); + assertEquals("ENABLED", enabled.path("TimeToLiveDescription").path("TimeToLiveStatus").asText()); + assertEquals("expiresAt", enabled.path("TimeToLiveDescription").path("AttributeName").asText()); + + backend.updateTimeToLive(owner, TABLE, "expiresAt", false); + + assertEquals(new TimeToLive(false, null), backend.timeToLive(owner, TABLE)); + JsonNode disabled = backend.execute(new Call(owner, Api.DYNAMODB, "DescribeTimeToLive", describe)).body(); + assertEquals("DISABLED", disabled.path("TimeToLiveDescription").path("TimeToLiveStatus").asText()); + } + @Test void unknownActionRepliesWithAJsonErrorBody() throws Exception { Reply reply = call(Api.DYNAMODB, "NoSuchAction", mapper.createObjectNode()); diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/RecordingDynamoDbBackend.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/RecordingDynamoDbBackend.java index 05f110463d..55de707505 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/RecordingDynamoDbBackend.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/RecordingDynamoDbBackend.java @@ -116,6 +116,17 @@ public final class RecordingDynamoDbBackend implements DynamoDbItemAccess, Dynam return record("disableStream", scope); } + @Override + public TimeToLive timeToLive(Scope scope, String tableName) { + record("timeToLive", scope); + return new TimeToLive(false, null); + } + + @Override + public void updateTimeToLive(Scope scope, String tableName, String attributeName, boolean enabled) { + record("updateTimeToLive", scope); + } + @Override public Map listTagsOfResource(Scope scope, String resourceArn) { record("listTagsOfResource", scope); diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yaml index 21fcf85b91..647d625b5b 100644 --- a/tools/docs/cfn_resource_types.yaml +++ b/tools/docs/cfn_resource_types.yaml @@ -194,6 +194,7 @@ notes: AWS::IoT::DomainConfiguration: '`ServerCertificates` resolves to a JSON string' AWS::IoT::Policy: 'deleted after detaching it from its principals; on AWS the delete fails with `DeleteConflictException` while the policy is attached' Custom::DynamoDBReplica: applied natively against DynamoDB, not via a provider Lambda + AWS::DynamoDB::Table: '`TimeToLiveSpecification` applied on create and update; to rename an enabled TTL attribute, disable TTL in one update and enable the new attribute in a later one' AWS::Cognito::UserPool: '`ProviderURL` is the local issuer of the tokens Floci mints, `/`' AWS::Events::Archive: '`KmsKeyIdentifier` is ignored' AWS::CloudFront::Function: '`KeyValueStoreAssociations` is ignored'