diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbPointInTimeRecoveryTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbPointInTimeRecoveryTest.java new file mode 100644 index 0000000000..2ddcaabd54 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DynamoDbPointInTimeRecoveryTest.java @@ -0,0 +1,132 @@ +package com.floci.test; + +import org.junit.jupiter.api.*; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.dynamodb.model.*; + +import java.time.Instant; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.*; + +@DisplayName("DynamoDB Point-in-Time Recovery") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class DynamoDbPointInTimeRecoveryTest { + + private static DynamoDbClient ddb; + private static final String TABLE_NAME = "sdk-pitr-test-table"; + private static final String RESTORED_TABLE_NAME = "sdk-pitr-test-restored"; + private static final String SYSTEM_RESTORED_TABLE_NAME = "sdk-pitr-test-system-restored"; + private static Instant beforeSecondWrite; + + @BeforeAll + static void setup() { + ddb = TestFixtures.dynamoDbClient(); + ddb.createTable(CreateTableRequest.builder() + .tableName(TABLE_NAME) + .attributeDefinitions(AttributeDefinition.builder() + .attributeName("pk").attributeType(ScalarAttributeType.S).build()) + .keySchema(KeySchemaElement.builder().attributeName("pk").keyType(KeyType.HASH).build()) + .billingMode(BillingMode.PAY_PER_REQUEST) + .build()); + } + + @AfterAll + static void cleanup() { + for (String table : new String[] {TABLE_NAME, RESTORED_TABLE_NAME, SYSTEM_RESTORED_TABLE_NAME}) { + try { + ddb.deleteTable(DeleteTableRequest.builder().tableName(table).build()); + } catch (ResourceNotFoundException ignored) { + // The test that creates or deletes it did not run. + } + } + if (ddb != null) { + ddb.close(); + } + } + + @Test + @Order(1) + void updateContinuousBackups_opensTheRestoreWindow() { + PointInTimeRecoveryDescription pitr = ddb.updateContinuousBackups(UpdateContinuousBackupsRequest.builder() + .tableName(TABLE_NAME) + .pointInTimeRecoverySpecification(PointInTimeRecoverySpecification.builder() + .pointInTimeRecoveryEnabled(true).build()) + .build()).continuousBackupsDescription().pointInTimeRecoveryDescription(); + + assertThat(pitr.pointInTimeRecoveryStatus()).isEqualTo(PointInTimeRecoveryStatus.ENABLED); + assertThat(pitr.recoveryPeriodInDays()).isEqualTo(35); + assertThat(pitr.earliestRestorableDateTime()).isNotNull(); + assertThat(pitr.latestRestorableDateTime()).isAfterOrEqualTo(pitr.earliestRestorableDateTime()); + } + + @Test + @Order(2) + void restoreTableToPointInTime_restoresTheItemsOfThatTime() throws InterruptedException { + putItem("first"); + Thread.sleep(50); + beforeSecondWrite = Instant.now(); + Thread.sleep(50); + putItem("second"); + + TableDescription description = ddb.restoreTableToPointInTime(RestoreTableToPointInTimeRequest.builder() + .sourceTableName(TABLE_NAME) + .targetTableName(RESTORED_TABLE_NAME) + .restoreDateTime(beforeSecondWrite) + .build()).tableDescription(); + + assertThat(description.restoreSummary().restoreInProgress()).isTrue(); + assertThat(description.restoreSummary().sourceTableArn()).endsWith(":table/" + TABLE_NAME); + ddb.waiter().waitUntilTableExists(DescribeTableRequest.builder().tableName(RESTORED_TABLE_NAME).build()); + assertThat(keys(RESTORED_TABLE_NAME)).containsExactly("first"); + } + + @Test + @Order(3) + void restoreTableToPointInTime_refusesAFutureTime() { + assertThatThrownBy(() -> ddb.restoreTableToPointInTime(RestoreTableToPointInTimeRequest.builder() + .sourceTableName(TABLE_NAME) + .targetTableName("sdk-pitr-test-never") + .restoreDateTime(Instant.now().plusSeconds(3600)) + .build())) + .isInstanceOf(InvalidRestoreTimeException.class); + } + + @Test + @Order(4) + void deleteTable_leavesASystemBackup() { + ddb.deleteTable(DeleteTableRequest.builder().tableName(TABLE_NAME).build()); + + List backups = ddb.listBackups(ListBackupsRequest.builder() + .tableName(TABLE_NAME).backupType(BackupTypeFilter.SYSTEM).build()).backupSummaries(); + assertThat(backups).isNotEmpty(); + BackupSummary backup = backups.get(backups.size() - 1); + assertThat(backup.backupName()).isEqualTo(TABLE_NAME + "$DeletedTableBackup"); + assertThat(backup.backupType()).isEqualTo(BackupType.SYSTEM); + assertThat(backup.backupExpiryDateTime()).isAfter(backup.backupCreationDateTime()); + assertThatThrownBy(() -> ddb.deleteBackup(DeleteBackupRequest.builder() + .backupArn(backup.backupArn()).build())) + .isInstanceOf(DynamoDbException.class) + .hasMessageContaining("not allowed to delete the system backup"); + + ddb.restoreTableFromBackup(RestoreTableFromBackupRequest.builder() + .backupArn(backup.backupArn()).targetTableName(SYSTEM_RESTORED_TABLE_NAME).build()); + ddb.waiter().waitUntilTableExists(DescribeTableRequest.builder() + .tableName(SYSTEM_RESTORED_TABLE_NAME).build()); + assertThat(keys(SYSTEM_RESTORED_TABLE_NAME)).containsExactlyInAnyOrder("first", "second"); + } + + private static void putItem(String pk) { + ddb.putItem(PutItemRequest.builder() + .tableName(TABLE_NAME) + .item(Map.of("pk", AttributeValue.fromS(pk))) + .build()); + } + + private static List keys(String table) { + return ddb.scan(ScanRequest.builder().tableName(table).build()).items().stream() + .map(item -> item.get("pk").s()) + .toList(); + } +} diff --git a/docs/services/dynamodb.md b/docs/services/dynamodb.md index f36e15365d..110b8c7d8d 100644 --- a/docs/services/dynamodb.md +++ b/docs/services/dynamodb.md @@ -28,8 +28,9 @@ | `TagResource` | Tag a table | | `UntagResource` | Remove tags | | `ListTagsOfResource` | List tags | -| `DescribeContinuousBackups` | Get PITR backup configuration | -| `UpdateContinuousBackups` | Enable/disable PITR | +| `DescribeContinuousBackups` | Get the point-in-time recovery status and restore window | +| `UpdateContinuousBackups` | Enable or disable point-in-time recovery | +| `RestoreTableToPointInTime` | Create a new table from a table as it was at a point in time | | `CreateBackup` | Take an on-demand backup of a table | | `DescribeBackup` | Get a backup and the table it was taken from | | `ListBackups` | List backups by table, type and creation time | @@ -231,6 +232,7 @@ region is rejected. | `FLOCI_STORAGE_SERVICES_DYNAMODB_FLUSH_INTERVAL_MS` | `5000` | Flush interval for `hybrid`/`wal` storage modes (milliseconds) | | `FLOCI_SERVICES_DYNAMODB_VECTOR_INDEX_ALLOCATION_SECONDS` | `4` | Seconds a vector index added by `UpdateTable` spends in resource allocation | | `FLOCI_SERVICES_DYNAMODB_VECTOR_INDEX_BACKFILL_SECONDS` | `10` | Seconds that index then spends backfilling before it goes `ACTIVE` | +| `FLOCI_SERVICES_DYNAMODB_LATEST_RESTORABLE_LAG_SECONDS` | `300` | Seconds `LatestRestorableDateTime` of point-in-time recovery trails the current time. Set `0` to restore the latest writes with `UseLatestRestorableTime` | | `FLOCI_SERVICES_DYNAMODB_BACKEND` | `native` | Engine behind the DynamoDB API: `native` or `local`, case-insensitive. Any other value fails startup. See [DynamoDB Local backend](#dynamodb-local-backend) | | `FLOCI_SERVICES_DYNAMODB_LOCAL_ENDPOINT` | *(none)* | Base URL of DynamoDB Local, for example `http://dynamodb-local:8000`. Required when the backend is `local`. Must be `http` or `https` with a host. AWS endpoints (hosts under `amazonaws.com`, `amazonaws.com.cn` or `api.aws`) are rejected | | `FLOCI_SERVICES_DYNAMODB_LOCAL_CONNECT_TIMEOUT_SECONDS` | `2` | Seconds to wait for a connection to DynamoDB Local | @@ -468,6 +470,24 @@ aws dynamodb restore-table-from-backup --backup-arn $BACKUP_ARN \ Deviations from AWS: a restore is done before the response is sent. The response still says `CREATING` with `RestoreInProgress: true`, as on AWS, but the table is `ACTIVE` right away. So `BackupInUseException` and `TableInUseException` for a restore in progress never happen. `BackupSizeBytes` is always 0. Backups made by AWS Backup (`BackupType` `AWS_BACKUP`) are not emulated. +## Point-in-time recovery + +While point-in-time recovery is enabled, Floci keeps every item change of the table. `RestoreTableToPointInTime` creates a new table with the items as they were at `RestoreDateTime`, or at `LatestRestorableDateTime` with `UseLatestRestorableTime`. As on AWS, the new table takes the current settings of the source table, not the settings at that time. The overrides are the same as for `RestoreTableFromBackup`, and a source in another region needs `SSESpecificationOverride`. + +```bash +aws dynamodb update-continuous-backups --table-name Users \ + --point-in-time-recovery-specification PointInTimeRecoveryEnabled=true --endpoint-url $AWS_ENDPOINT_URL + +aws dynamodb restore-table-to-point-in-time --source-table-name Users \ + --target-table-name UsersAnHourAgo --restore-date-time $(($(date +%s) - 3600)) --endpoint-url $AWS_ENDPOINT_URL +``` + +`EarliestRestorableDateTime` is the time point-in-time recovery was enabled, or the start of the recovery period once that has passed. `LatestRestorableDateTime` trails the current time by `FLOCI_SERVICES_DYNAMODB_LATEST_RESTORABLE_LAG_SECONDS`, 5 minutes by default like AWS. As on AWS, a `RestoreDateTime` after it is still accepted up to the current time. Disabling point-in-time recovery drops the change history. The history survives a restart in the persistent storage modes. + +Deleting a table with point-in-time recovery enabled leaves a `SYSTEM` backup named `$DeletedTableBackup`. It holds the table as it was at deletion, cannot be deleted, and expires after 35 days. `ListBackups` shows it with `BackupType` `SYSTEM` or `ALL`, and `RestoreTableFromBackup` restores it. + +Deviations from AWS: enabling point-in-time recovery right after `CreateTable` is accepted at once. AWS sometimes refuses it for a few seconds with `ContinuousBackupsUnavailableException`. + ## Kinesis change data capture (CDC) diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 2d30c5bf46..9b9d743e50 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -1461,6 +1461,14 @@ public interface EmulatorConfig { @WithDefault("10") int vectorIndexBackfillSeconds(); + /** + * Seconds LatestRestorableDateTime of point-in-time recovery trails the current time. AWS + * keeps it about five minutes behind. + * Env: FLOCI_SERVICES_DYNAMODB_LATEST_RESTORABLE_LAG_SECONDS + */ + @WithDefault("300") + int latestRestorableLagSeconds(); + /** The engine behind the DynamoDB API: {@code native} or {@code local}. Env: FLOCI_SERVICES_DYNAMODB_BACKEND. */ @WithDefault("native") String backend(); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbBackupArns.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbBackupArns.java index 3770ffe070..b2e8c55198 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbBackupArns.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbBackupArns.java @@ -54,6 +54,11 @@ final class DynamoDbBackupArns { + "-" + UUID.randomUUID().toString().substring(0, 8); } + /** AWS ends the id of a system backup with zeros instead of random hex digits. */ + static String mintSystem(String tableArn, Instant creationDateTime) { + return tableArn + "/backup/" + String.format("%014d", creationDateTime.toEpochMilli()) + "-00000000"; + } + private static AwsException invalidBackupArn() { return new AwsException("ValidationException", "Invalid Backup ARN", 400); } diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPointInTimeRecovery.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPointInTimeRecovery.java new file mode 100644 index 0000000000..24059fb2ba --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPointInTimeRecovery.java @@ -0,0 +1,171 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import com.fasterxml.jackson.databind.JsonNode; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.services.dynamodb.model.PointInTimeChange; +import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition; + +import java.time.Clock; +import java.time.Duration; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * The change history behind point-in-time recovery. A change keeps what it replaced, so a table at + * a time is the live table with every later change undone. Each change is stored on its own, under + * {@code #}. + */ +final class DynamoDbPointInTimeRecovery { + + record RestoreWindow(Instant earliest, Instant latest) {} + + /** The changes of one table, oldest first. */ + private static final class Changes { + private final List list = new ArrayList<>(); + private long nextSequence; + private boolean dropped; + } + + private final StorageBackend store; + private final Duration latestLag; + private final Clock clock; + // Keyed like DynamoDbService.itemsByTable: "/::
". + private final ConcurrentHashMap changesByTable = new ConcurrentHashMap<>(); + + DynamoDbPointInTimeRecovery(StorageBackend store, String defaultAccountId, + Duration latestLag, Clock clock) { + this.store = store; + this.latestLag = latestLag; + this.clock = clock; + Map stored = new HashMap<>(); + if (store instanceof AccountAwareStorageBackend aware) { + stored.putAll(aware.scanAllAccountsRaw()); + } else { + for (String key : store.keys()) { + store.get(key).ifPresent(change -> stored.put(defaultAccountId + "/" + key, change)); + } + } + stored.forEach((key, change) -> changesByTable + .computeIfAbsent(key.substring(0, key.lastIndexOf('#')), ignored -> new Changes()) + .list.add(change)); + for (Changes changes : changesByTable.values()) { + changes.list.sort(Comparator.comparingLong(PointInTimeChange::getSequence)); + changes.nextSequence = changes.list.getLast().getSequence() + 1; + } + } + + void clear() { + changesByTable.clear(); + } + + /** Call after point-in-time recovery of the table is turned off, so no writer records again. */ + void drop(String accountId, String storageKey) { + Changes changes = changesByTable.remove(key(accountId, storageKey)); + if (changes == null) { + return; + } + synchronized (changes) { + changes.dropped = true; + for (PointInTimeChange change : changes.list) { + delete(accountId, storageKey, change); + } + } + } + + /** Call before the item is written. */ + void recordItem(String accountId, String storageKey, TableDefinition table, String itemKey, JsonNode oldItem) { + if (!table.isPointInTimeRecoveryEnabled()) { + return; + } + Changes changes = changesByTable.computeIfAbsent(key(accountId, storageKey), ignored -> new Changes()); + synchronized (changes) { + if (changes.dropped || !table.isPointInTimeRecoveryEnabled()) { + return; + } + Instant now = clock.instant().truncatedTo(ChronoUnit.MILLIS); + PointInTimeChange change = new PointInTimeChange(changes.nextSequence++, now, itemKey, + oldItem != null ? oldItem.deepCopy() : null); + changes.list.add(change); + put(accountId, storageKey, change); + Instant cutoff = now.minus(recoveryPeriod(table)); + int expired = 0; + while (expired < changes.list.size() && changes.list.get(expired).getTime().isBefore(cutoff)) { + delete(accountId, storageKey, changes.list.get(expired)); + expired++; + } + changes.list.subList(0, expired).clear(); + } + } + + /** AWS words an InvalidRestoreTimeException with the latest time to the nanosecond. */ + RestoreWindow window(TableDefinition table) { + Instant now = clock.instant(); + Instant earliest = later(table.getPointInTimeRecoveryStartDateTime(), now.minus(recoveryPeriod(table))); + return new RestoreWindow(earliest, later(earliest, now.minus(latestLag))); + } + + /** + * An item changed after {@code time} takes the value that its first change after it replaced. + * Writers record a change before they write the item, so the live items are copied first. + */ + List itemsAt(String accountId, String storageKey, Map liveItems, Instant time) { + Map items = new HashMap<>(liveItems); + Changes changes = changesByTable.get(key(accountId, storageKey)); + if (changes != null) { + Set undone = new HashSet<>(); + synchronized (changes) { + for (PointInTimeChange change : changes.list) { + String itemKey = change.getItemKey(); + if (!change.getTime().isAfter(time) || !undone.add(itemKey)) { + continue; + } + if (change.getOldItem() == null) { + items.remove(itemKey); + } else { + items.put(itemKey, change.getOldItem()); + } + } + } + } + return new ArrayList<>(items.values()); + } + + private void put(String accountId, String storageKey, PointInTimeChange change) { + String key = storageKey + "#" + change.getSequence(); + if (store instanceof AccountAwareStorageBackend aware) { + aware.putForAccount(accountId, key, change); + } else { + store.put(key, change); + } + } + + private void delete(String accountId, String storageKey, PointInTimeChange change) { + String key = storageKey + "#" + change.getSequence(); + if (store instanceof AccountAwareStorageBackend aware) { + aware.deleteForAccount(accountId, key); + } else { + store.delete(key); + } + } + + private static Duration recoveryPeriod(TableDefinition table) { + return Duration.ofDays(table.getPointInTimeRecoveryRecoveryPeriodInDays()); + } + + private static Instant later(Instant a, Instant b) { + return a.isAfter(b) ? a : b; + } + + private static String key(String accountId, String storageKey) { + return accountId + "/" + storageKey; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index cc94d92d32..2f2f4de9e2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -29,6 +29,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.ImportSummary; import io.github.hectorvent.floci.services.dynamodb.model.ImportTableDescription; import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex; +import io.github.hectorvent.floci.services.dynamodb.model.PointInTimeChange; import io.github.hectorvent.floci.services.dynamodb.model.StreamDescription; import io.github.hectorvent.floci.services.dynamodb.model.TableBackup; import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition; @@ -47,6 +48,7 @@ import java.math.BigDecimal; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -103,6 +105,8 @@ public class DynamoDbService { private final StorageBackend exportStore; private final StorageBackend importStore; private final StorageBackend backupStore; + private final DynamoDbPointInTimeRecovery pointInTimeRecovery; + private final Clock clock; // Items stored per table: storageKey -> Map // itemKey is "pk" or "pk#sk" depending on table schema private final ConcurrentHashMap> itemsByTable = new ConcurrentHashMap<>(); @@ -170,9 +174,12 @@ public class DynamoDbService { new TypeReference>() {}), storageFactory.create("dynamodb", "dynamodb-backups.json", new TypeReference>() {}), + storageFactory.create("dynamodb", "dynamodb-point-in-time.json", + new TypeReference>() {}), regionResolver, streamService, kinesisForwarder, s3Service, objectMapper, config.services().dynamodb().vectorIndexAllocationSeconds(), - config.services().dynamodb().vectorIndexBackfillSeconds()); + config.services().dynamodb().vectorIndexBackfillSeconds(), + config.services().dynamodb().latestRestorableLagSeconds(), Clock.systemUTC()); } /** Package-private constructor for testing. */ @@ -239,11 +246,34 @@ public class DynamoDbService { ObjectMapper objectMapper, int vectorIndexAllocationSeconds, int vectorIndexBackfillSeconds) { + this(tableStore, itemStore, exportStore, importStore, backupStore, new InMemoryStorage<>(), regionResolver, + streamService, kinesisForwarder, s3Service, objectMapper, + vectorIndexAllocationSeconds, vectorIndexBackfillSeconds, 0, Clock.systemUTC()); + } + + DynamoDbService(StorageBackend tableStore, + StorageBackend> itemStore, + StorageBackend exportStore, + StorageBackend importStore, + StorageBackend backupStore, + StorageBackend pointInTimeStore, + RegionResolver regionResolver, + DynamoDbStreamService streamService, + KinesisStreamingForwarder kinesisForwarder, + S3Service s3Service, + ObjectMapper objectMapper, + int vectorIndexAllocationSeconds, + int vectorIndexBackfillSeconds, + int latestRestorableLagSeconds, + Clock clock) { this.tableStore = tableStore; this.itemStore = itemStore; this.exportStore = exportStore; this.importStore = importStore; this.backupStore = backupStore; + this.pointInTimeRecovery = new DynamoDbPointInTimeRecovery(pointInTimeStore, regionResolver.getAccountId(), + Duration.ofSeconds(latestRestorableLagSeconds), clock); + this.clock = clock; this.regionResolver = regionResolver; this.streamService = streamService; this.kinesisForwarder = kinesisForwarder; @@ -260,6 +290,7 @@ public class DynamoDbService { itemsByTable.clear(); itemLocks.clear(); txIdempotency.clear(); + pointInTimeRecovery.clear(); if (streamService != null) { streamService.clear(); } @@ -859,6 +890,12 @@ public class DynamoDbService { "Attempt to change a resource which is still in use: Cannot delete table while " + "indexes are being created, updated, or deleted.", 400); } + if (table.isPointInTimeRecoveryEnabled()) { + createSystemBackup(storageKey, table, region); + } + // A write still in flight on this table object stops recording once the flag is off. + table.setPointInTimeRecoveryEnabled(false); + pointInTimeRecovery.drop(regionResolver.getAccountId(), storageKey); tableStore.delete(storageKey); itemsByTable.remove(scopedItemsKey(storageKey)); itemLocks.remove(scopedItemsKey(storageKey)); @@ -980,6 +1017,9 @@ public class DynamoDbService { } requireItemNestingWithinLimit(normalizedItem); + if (stagedItems == null) { + pointInTimeRecovery.recordItem(regionResolver.getAccountId(), storageKey, table, itemKey, existing); + } tableItems.put(itemKey, normalizedItem); if (shouldPersist) { persistItems(storageKey); @@ -1078,6 +1118,10 @@ public class DynamoDbService { evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues, returnValuesOnConditionCheckFailure); } + JsonNode current = items.get(itemKey); + if (stagedItems == null && current != null) { + pointInTimeRecovery.recordItem(regionResolver.getAccountId(), storageKey, table, itemKey, current); + } JsonNode removed = items.remove(itemKey); if (shouldPersist) { persistItems(storageKey); @@ -1291,6 +1335,9 @@ public class DynamoDbService { requireUpdatedItemWithinSizeLimit(item); } + if (stagedItems == null) { + pointInTimeRecovery.recordItem(regionResolver.getAccountId(), storageKey, table, itemKey, existing); + } items.put(itemKey, item); if (shouldPersist) { persistItems(storageKey); @@ -1881,6 +1928,12 @@ public class DynamoDbService { scopedItemsKey(participant.storageKey()), ignored -> new ConcurrentSkipListMap<>()); ConcurrentSkipListMap stagedTable = staged.get(participant.storageKey()); JsonNode value = stagedTable.get(participant.itemKey()); + JsonNode before = live.get(participant.itemKey()); + if (value != before) { + tableStore.get(participant.storageKey()).ifPresent(participantTable -> pointInTimeRecovery.recordItem( + regionResolver.getAccountId(), participant.storageKey(), participantTable, + participant.itemKey(), before)); + } if (value == null) { live.remove(participant.itemKey()); } else { @@ -2683,21 +2736,149 @@ public class DynamoDbService { LOG.infov("Updated TTL for table {0}: enabled={1}, attr={2}", canonicalTableName, enabled, ttlAttributeName); } - public TableDefinition updateContinuousBackups(String tableName, boolean enabled, - Integer recoveryPeriodInDays, String region) { + /** The restore window is null while point-in-time recovery is disabled. */ + public record ContinuousBackups(TableDefinition table, DynamoDbPointInTimeRecovery.RestoreWindow window) {} + + /** The items of a source table as they were at {@code restoreDateTime}, with its current settings, as on AWS. */ + public record PointInTimeState(TableDefinition table, List items, Instant restoreDateTime) {} + + public ContinuousBackups describeContinuousBackups(String tableName, String region) { String canonicalTableName = canonicalTableName(region, tableName); String storageKey = regionKey(region, canonicalTableName); - TableDefinition table = tableStore.get(storageKey) - .orElseThrow(() -> resourceNotFoundException(canonicalTableName)); + return continuousBackups(storageKey, requireBackupSource(storageKey, canonicalTableName)); + } + + /** Enabling a disabled table starts the restore window over, as on AWS. */ + public ContinuousBackups updateContinuousBackups(String tableName, boolean enabled, + Integer recoveryPeriodInDays, String region) { + String canonicalTableName = canonicalTableName(region, tableName); + String storageKey = regionKey(region, canonicalTableName); + TableDefinition table = requireBackupSource(storageKey, canonicalTableName); + if (enabled && !table.isPointInTimeRecoveryEnabled()) { + pointInTimeRecovery.drop(regionResolver.getAccountId(), storageKey); + table.setPointInTimeRecoveryStartDateTime(clock.instant().truncatedTo(ChronoUnit.SECONDS)); + table.setPointInTimeRecoveryRecoveryPeriodInDays(DEFAULT_RECOVERY_PERIOD_DAYS); + } else if (enabled && recoveryPeriodInDays != null) { + // AWS keeps the earliest time when the period grows, until the longer period has passed. + table.setPointInTimeRecoveryStartDateTime( + pointInTimeRecovery.window(withPointInTimeStart(storageKey, table)).earliest()); + } table.setPointInTimeRecoveryEnabled(enabled); - table.setPointInTimeRecoveryRecoveryPeriodInDays( - recoveryPeriodInDays != null ? recoveryPeriodInDays : table.getPointInTimeRecoveryRecoveryPeriodInDays()); + if (!enabled) { + table.setPointInTimeRecoveryStartDateTime(null); + pointInTimeRecovery.drop(regionResolver.getAccountId(), storageKey); + } + if (recoveryPeriodInDays != null) { + table.setPointInTimeRecoveryRecoveryPeriodInDays(recoveryPeriodInDays); + } tableStore.put(storageKey, table); LOG.infov("Updated PITR for table {0}: enabled={1}, recoveryPeriodInDays={2}", canonicalTableName, enabled, table.getPointInTimeRecoveryRecoveryPeriodInDays()); + return continuousBackups(storageKey, table); + } + + /** + * The checks run in the AWS order: the source table, the presence of a restore time, point-in-time + * recovery on the source, then the restore time. AWS restores any time up to now, although it + * reports a LatestRestorableDateTime minutes earlier and names only that one in the error. + */ + public PointInTimeState pointInTimeState(String sourceTableName, String sourceRegion, String region, + boolean useLatestRestorableTime, Instant restoreDateTime) { + String canonicalTableName = canonicalTableName(sourceRegion, sourceTableName); + String storageKey = regionKey(sourceRegion, canonicalTableName); + TableDefinition table = tableStore.get(storageKey) + .orElseThrow(() -> new AwsException("TableNotFoundException", sourceRegion.equals(region) + ? "Table not found: " + canonicalTableName : "Table not found", 400)); + if ("CREATING".equals(table.getTableStatus())) { + throw new AwsException("TableNotFoundException", "Table not found", 400); + } + if (!useLatestRestorableTime && restoreDateTime == null) { + throw new AwsException("ValidationException", "Invalid Request: Either one of RestoreDateTime or " + + "UseLatestRestorableTime must be specified, but not both", 400); + } + if (!table.isPointInTimeRecoveryEnabled()) { + throw new AwsException("PointInTimeRecoveryUnavailableException", + "Point in time recovery is not enabled for table '" + canonicalTableName + "'", 400); + } + String accountId = regionResolver.getAccountId(); + DynamoDbPointInTimeRecovery.RestoreWindow window = + pointInTimeRecovery.window(withPointInTimeStart(storageKey, table)); + Instant time = useLatestRestorableTime ? window.latest() : restoreDateTime; + if (time.isBefore(window.earliest()) || clock.instant().isBefore(time)) { + throw new AwsException("InvalidRestoreTimeException", "RestoreDateTime '" + time + + "' must be within EarliestRestorableDateTime '" + window.earliest() + + "' and LatestRestorableDateTime '" + window.latest() + "' for table '" + + canonicalTableName + "'", 400); + } + ConcurrentSkipListMap liveItems = itemsByTable.get(scopedItemsKey(storageKey)); + List items = pointInTimeRecovery.itemsAt(accountId, storageKey, + liveItems != null ? liveItems : Map.of(), time); + return new PointInTimeState(snapshotOf(table), items, time); + } + + /** + * The table a SourceTableArn names. AWS restores across regions only with an + * SSESpecificationOverride, and treats an unknown region as another region. + */ + public DynamoDbTableNames.ResolvedTableRef pointInTimeSourceArn(String sourceTableArn, String region, + boolean sseOverride) { + if (!sourceTableArn.startsWith("arn:")) { + throw invalidParameter("ARNs must start with 'arn:': " + sourceTableArn); + } + AwsArnUtils.Arn arn; + try { + arn = AwsArnUtils.parse(sourceTableArn); + } catch (IllegalArgumentException e) { + throw invalidParameter("sourceTableArn is not a valid ARN"); + } + if (!"dynamodb".equals(arn.service())) { + throw invalidParameter("Invalid resource Arn: " + sourceTableArn); + } + if (!arn.resource().startsWith("table/")) { + throw invalidParameter("sourceTableArn is not a valid ARN"); + } + String tableName = arn.resource().substring("table/".length()); + if (tableName.length() < 3 || 255 < tableName.length() + || !DynamoDbTableNames.TABLE_NAME_CHARS.matcher(tableName).matches()) { + throw new AwsException("ValidationException", "Invalid Request: Invalid TableArn: Invalid Table ARN", 400); + } + if (!arn.accountId().equals(regionResolver.getAccountId())) { + throw new AwsException("ValidationException", + "This action is only supported by accounts that match the resource owner’s account.", 400); + } + if (!arn.region().equals(region) && !sseOverride) { + throw new AwsException("ValidationException", + "Invalid Request: sseSpecificationOverride must be provided for cross-region restores", 400); + } + return new DynamoDbTableNames.ResolvedTableRef(tableName, arn.region()); + } + + private static AwsException invalidParameter(String message) { + return new AwsException("ValidationException", "One or more parameter values were invalid: " + message, 400); + } + + private ContinuousBackups continuousBackups(String storageKey, TableDefinition table) { + return new ContinuousBackups(table, table.isPointInTimeRecoveryEnabled() + ? pointInTimeRecovery.window(withPointInTimeStart(storageKey, table)) + : null); + } + + /** A table that had point-in-time recovery enabled before Floci kept a history starts one now. */ + private TableDefinition withPointInTimeStart(String storageKey, TableDefinition table) { + if (table.getPointInTimeRecoveryStartDateTime() == null) { + table.setPointInTimeRecoveryStartDateTime(clock.instant().truncatedTo(ChronoUnit.SECONDS)); + tableStore.put(storageKey, table); + } return table; } + /** The backup operations do not see a table that a restore is still creating. */ + private TableDefinition requireBackupSource(String storageKey, String tableName) { + return tableStore.get(storageKey) + .filter(t -> !"CREATING".equals(t.getTableStatus())) + .orElseThrow(() -> new AwsException("TableNotFoundException", "Table not found: " + tableName, 400)); + } + static boolean isExpired(JsonNode item, TableDefinition table) { if (!table.isTtlEnabled() || table.getTtlAttributeName() == null) return false; JsonNode attr = item.get(table.getTtlAttributeName()); @@ -2713,8 +2894,10 @@ public class DynamoDbService { TableDefinition table, List itemKeys, ConcurrentSkipListMap items) {} + /** The TTL sweep also removes the system backups past their expiry. */ void deleteExpiredItems() { deleteScannedItems(scanExpiredItems()); + deleteExpiredBackups(); } List scanExpiredItems() { @@ -2771,6 +2954,7 @@ public class DynamoDbService { if (current == null || !isExpired(current, scan.table())) { return null; } + pointInTimeRecovery.recordItem(scan.accountId(), scan.storageKey(), scan.table(), itemKey, current); return items.remove(itemKey); }); if (removed == null) { @@ -4811,6 +4995,9 @@ public class DynamoDbService { // The live table object is shared and later mutated in place, so a backup keeps its own copy. private static final ObjectMapper SNAPSHOT_MAPPER = new ObjectMapper().registerModule(new JavaTimeModule()); + private static final int DEFAULT_RECOVERY_PERIOD_DAYS = 35; + private static final int SYSTEM_BACKUP_RETENTION_DAYS = 35; + private static final Comparator BACKUP_ORDER = Comparator .comparing(TableBackup::getCreationDateTime) .thenComparing(TableBackup::getBackupArn); @@ -4824,6 +5011,29 @@ public class DynamoDbService { .filter(t -> !"CREATING".equals(t.getTableStatus())) .orElseThrow(() -> new AwsException("TableNotFoundException", "Table not found: " + canonicalTableName, 400)); + Instant now = clock.instant().truncatedTo(ChronoUnit.MILLIS); + TableBackup backup = backupOf(storageKey, table, DynamoDbBackupArns.mint(table.getTableArn(), now), now); + backup.setBackupName(backupName); + backup.setBackupType("USER"); + backup.setRegion(region); + backupStore.put(backup.getBackupArn(), backup); + LOG.infov("Created backup {0} of table {1}", backup.getBackupArn(), canonicalTableName); + return backup; + } + + /** AWS keeps the backup it takes of a deleted table with point-in-time recovery for 35 days. */ + private void createSystemBackup(String storageKey, TableDefinition table, String region) { + Instant now = clock.instant().truncatedTo(ChronoUnit.MILLIS); + TableBackup backup = backupOf(storageKey, table, DynamoDbBackupArns.mintSystem(table.getTableArn(), now), now); + backup.setBackupName(table.getTableName() + "$DeletedTableBackup"); + backup.setBackupType("SYSTEM"); + backup.setExpiryDateTime(now.plus(Duration.ofDays(SYSTEM_BACKUP_RETENTION_DAYS))); + backup.setRegion(region); + backupStore.put(backup.getBackupArn(), backup); + LOG.infov("Created system backup {0} of deleted table {1}", backup.getBackupArn(), table.getTableName()); + } + + private TableBackup backupOf(String storageKey, TableDefinition table, String backupArn, Instant now) { ConcurrentSkipListMap items = itemsByTable.get(scopedItemsKey(storageKey)); List snapshot = new ArrayList<>(); if (items != null) { @@ -4832,18 +5042,12 @@ public class DynamoDbService { TableDefinition source = snapshotOf(table); source.setItemCount(snapshot.size()); - Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS); TableBackup backup = new TableBackup(); - backup.setBackupArn(DynamoDbBackupArns.mint(table.getTableArn(), now)); - backup.setBackupName(backupName); - backup.setBackupType("USER"); + backup.setBackupArn(backupArn); backup.setCreationDateTime(now); backup.setSizeBytes(source.getTableSizeBytes()); - backup.setRegion(region); backup.setTable(source); backup.setItems(snapshot); - backupStore.put(backup.getBackupArn(), backup); - LOG.infov("Created backup {0} of table {1}", backup.getBackupArn(), canonicalTableName); return backup; } @@ -4854,12 +5058,18 @@ public class DynamoDbService { } return backupStore.get(backupArn) .filter(backup -> backup.getRegion().equals(region)) + .filter(this::isUnexpired) .orElseThrow(() -> new AwsException("BackupNotFoundException", "Backup not found: " + backupArn, 400)); } public TableBackup deleteBackup(String backupArn, String region) { TableBackup backup = describeBackup(backupArn, region); + if ("SYSTEM".equals(backup.getBackupType())) { + throw new AwsException("ValidationException", "Invalid Request: User is not allowed to delete the " + + "system backup with arn " + backupArn + ". It will automatically expire on " + + backup.getExpiryDateTime(), 400); + } backupStore.delete(backupArn); LOG.infov("Deleted backup {0}", backupArn); return backup; @@ -4876,6 +5086,7 @@ public class DynamoDbService { Set types = "ALL".equals(backupType) ? Set.of("USER", "SYSTEM") : Set.of(backupType); List matching = backupStore.scan(key -> true).stream() .filter(backup -> backup.getRegion().equals(region)) + .filter(this::isUnexpired) .filter(backup -> tableName == null || tableName.equals(backup.getTable().getTableName())) .filter(backup -> types.contains(backup.getBackupType())) .filter(backup -> lowerBound == null || !backup.getCreationDateTime().isBefore(lowerBound)) @@ -4919,6 +5130,21 @@ public class DynamoDbService { LOG.infov("Restored {0} items into table {1}", items.size(), tableName); } + private void deleteExpiredBackups() { + if (backupStore instanceof AccountAwareStorageBackend aware) { + aware.deleteAllAccountsMatching(backup -> !isUnexpired(backup)); + return; + } + for (String backupArn : new ArrayList<>(backupStore.keys())) { + backupStore.get(backupArn).filter(backup -> !isUnexpired(backup)) + .ifPresent(backup -> backupStore.delete(backupArn)); + } + } + + private boolean isUnexpired(TableBackup backup) { + return backup.getExpiryDateTime() == null || clock.instant().isBefore(backup.getExpiryDateTime()); + } + private static boolean isAfter(TableBackup backup, DynamoDbBackupArns.BackupArn start) { int byTime = Long.compare(backup.getCreationDateTime().toEpochMilli(), start.creationMillis()); return 0 < byTime || (byTime == 0 && 0 < backup.getBackupArn().compareTo(start.arn())); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java index d14f56176a..cd05844e54 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java @@ -12,7 +12,7 @@ import java.util.regex.Pattern; */ public final class DynamoDbTableNames { - private static final Pattern TABLE_NAME_CHARS = Pattern.compile("[a-zA-Z0-9_.\\-]+"); + static final Pattern TABLE_NAME_CHARS = Pattern.compile("[a-zA-Z0-9_.\\-]+"); private static final Pattern ACCOUNT_PATTERN = Pattern.compile("\\d{12}"); private DynamoDbTableNames() {} diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java index 791dc03bbd..46cbff0310 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java @@ -60,6 +60,7 @@ public class NativeDynamoDbJsonHandler { // The order AWS prints the enum constraint in. private static final List BACKUP_TYPE_FILTERS = List.of("ALL", "AWS_BACKUP", "USER", "SYSTEM"); private static final int MAX_LIST_BACKUPS_LIMIT = 100; + private static final int MAX_RECOVERY_PERIOD_DAYS = 35; private final DynamoDbService dynamoDbService; private final NativeDynamoDbTableService tableService; @@ -108,6 +109,7 @@ public class NativeDynamoDbJsonHandler { case "ListBackups" -> handleListBackups(request, region); case "DeleteBackup" -> handleDeleteBackup(request, region); case "RestoreTableFromBackup" -> handleRestoreTableFromBackup(request, region); + case "RestoreTableToPointInTime" -> handleRestoreTableToPointInTime(request, region); case "TransactWriteItems" -> handleTransactWriteItems(request, region); case "TransactGetItems" -> handleTransactGetItems(request, region); case "TagResource" -> handleTagResource(request, region); @@ -1864,31 +1866,50 @@ public class NativeDynamoDbJsonHandler { } private Response handleDescribeContinuousBackups(JsonNode request, String region) { - String tableName = request.path("TableName").asText(); - TableDefinition table = dynamoDbService.describeTable(tableName, region); + String tableName = requireTableNameParameter(request, "TableName"); + DynamoDbTableNames.resolveWithRegion(tableName, region); + DynamoDbService.ContinuousBackups backups = dynamoDbService.describeContinuousBackups(tableName, region); ObjectNode response = objectMapper.createObjectNode(); - response.set("ContinuousBackupsDescription", continuousBackupsDescriptionNode(table)); + response.set("ContinuousBackupsDescription", continuousBackupsDescriptionNode(backups)); return Response.ok(response).build(); } private Response handleUpdateContinuousBackups(JsonNode request, String region) { - String tableName = request.path("TableName").asText(); + String tableName = requireTableNameParameter(request, "TableName"); + DynamoDbTableNames.resolveWithRegion(tableName, region); JsonNode spec = request.path("PointInTimeRecoverySpecification"); - boolean enabled = spec.path("PointInTimeRecoveryEnabled").asBoolean(false); - Integer recoveryPeriodInDays = spec.has("RecoveryPeriodInDays") - ? spec.path("RecoveryPeriodInDays").asInt() - : null; - if (recoveryPeriodInDays != null && (recoveryPeriodInDays < 1 || recoveryPeriodInDays > 35)) { + if (spec.isMissingNode() || spec.isNull()) { + throwValidationErrors(List.of("Value null at 'pointInTimeRecoverySpecification' failed to satisfy " + + "constraint: Member must not be null")); + } + List violations = new ArrayList<>(); + JsonNode period = spec.path("RecoveryPeriodInDays"); + String periodConstraint = "Value '" + period.asText() + "' at 'pointInTimeRecoverySpecification." + + "recoveryPeriodInDays' failed to satisfy constraint: "; + if (period.isNumber() && period.asLong() < 1) { + violations.add(periodConstraint + "Member must have value greater than or equal to 1"); + } + if (period.isNumber() && MAX_RECOVERY_PERIOD_DAYS < period.asLong()) { + violations.add(periodConstraint + "Member must have value less than or equal to " + MAX_RECOVERY_PERIOD_DAYS); + } + JsonNode enabled = spec.path("PointInTimeRecoveryEnabled"); + if (enabled.isMissingNode() || enabled.isNull()) { + violations.add("Value null at 'pointInTimeRecoverySpecification.pointInTimeRecoveryEnabled' failed to " + + "satisfy constraint: Member must not be null"); + } + throwValidationErrors(violations); + Integer recoveryPeriodInDays = period.isNumber() ? period.asInt() : null; + if (!enabled.asBoolean() && recoveryPeriodInDays != null) { throw new AwsException("ValidationException", - "RecoveryPeriodInDays must be between 1 and 35", 400); + "Invalid Request: Cannot specify RecoveryPeriodInDays when disabling point-in-time recovery.", 400); } - TableDefinition table = dynamoDbService.updateContinuousBackups( - tableName, enabled, recoveryPeriodInDays, region); + DynamoDbService.ContinuousBackups backups = dynamoDbService.updateContinuousBackups( + tableName, enabled.asBoolean(), recoveryPeriodInDays, region); ObjectNode response = objectMapper.createObjectNode(); - response.set("ContinuousBackupsDescription", continuousBackupsDescriptionNode(table)); + response.set("ContinuousBackupsDescription", continuousBackupsDescriptionNode(backups)); return Response.ok(response).build(); } @@ -1975,6 +1996,76 @@ public class NativeDynamoDbJsonHandler { String backupArn = textOrNull(request, "BackupArn"); DynamoDbBackupArns.parse(backupArn); String targetTableName = requireTableNameParameter(request, "TargetTableName"); + throwValidationErrors(restoreViolations(request, targetTableName)); + + TableBackup backup = dynamoDbService.describeBackup(backupArn, region); + TableDefinition table = tableService.restoreTable(DynamoDbService.snapshotOf(backup.getTable()), + targetTableName, parseRestoreOverrides(request), region); + ObjectNode description = tableToNode(table); + description.set("RestoreSummary", restoreSummaryNode(backup.getBackupArn(), + backup.getTable().getTableArn(), backup.getCreationDateTime())); + dynamoDbService.finishRestore(targetTableName, backup.getItems(), region); + + ObjectNode response = objectMapper.createObjectNode(); + response.set("TableDescription", description); + return Response.ok(response).build(); + } + + /** + * Restores synchronously, like {@link #handleRestoreTableFromBackup}. AWS checks a source table + * name for its length before the validation list, and a source table ARN before both. + */ + private Response handleRestoreTableToPointInTime(JsonNode request, String region) { + String targetTableName = requireTableNameParameter(request, "TargetTableName"); + String sourceTableName = textOrNull(request, "SourceTableName"); + String sourceTableArn = textOrNull(request, "SourceTableArn"); + if (sourceTableName == null && sourceTableArn == null) { + requireTableNameParameter(request, "SourceTableName"); + } + DynamoDbTableNames.ResolvedTableRef source; + if (sourceTableArn != null) { + source = dynamoDbService.pointInTimeSourceArn(sourceTableArn, region, + request.path("SSESpecificationOverride").isObject()); + } else if (sourceTableName.length() < 3 || 255 < sourceTableName.length()) { + throw new AwsException("ValidationException", + "TableName must be at least 3 characters long and at most 255 characters long", 400); + } else { + source = new DynamoDbTableNames.ResolvedTableRef(sourceTableName, region); + } + List violations = new ArrayList<>(); + if (sourceTableName != null) { + violations.addAll(nameViolations(sourceTableName, "sourceTableName")); + } + violations.addAll(restoreViolations(request, targetTableName)); + throwValidationErrors(violations); + if (sourceTableName != null && sourceTableArn != null) { + throw new AwsException("ValidationException", + "Invalid Request: Must provide exactly one of: sourceTableArn, sourceTableName", 400); + } + boolean useLatestRestorableTime = request.path("UseLatestRestorableTime").asBoolean(false); + Instant restoreDateTime = timestampOrNull(request, "RestoreDateTime"); + if (useLatestRestorableTime && restoreDateTime != null) { + throw new AwsException("ValidationException", "Invalid Request: Both RestoreDateTime and " + + "UseLatestRestorableTime cannot be set for point-in-time restore request. Please specify " + + "either RestoreDateTime time or UseLatestRestorableTime", 400); + } + + DynamoDbService.PointInTimeState state = dynamoDbService.pointInTimeState(source.name(), source.region(), + region, useLatestRestorableTime, restoreDateTime); + TableDefinition table = tableService.restoreTable(state.table(), targetTableName, + parseRestoreOverrides(request), region); + ObjectNode description = tableToNode(table); + description.set("RestoreSummary", restoreSummaryNode(null, state.table().getTableArn(), + state.restoreDateTime())); + dynamoDbService.finishRestore(targetTableName, state.items(), region); + + ObjectNode response = objectMapper.createObjectNode(); + response.set("TableDescription", description); + return Response.ok(response).build(); + } + + /** The validation list both restores share, in the AWS order. */ + private static List restoreViolations(JsonNode request, String targetTableName) { List violations = new ArrayList<>(throughputViolations(request.path("ProvisionedThroughputOverride"), "provisionedThroughputOverride")); JsonNode billingModeOverride = request.path("BillingModeOverride"); @@ -1995,19 +2086,7 @@ public class NativeDynamoDbJsonHandler { member + ".provisionedThroughput")); } } - throwValidationErrors(violations); - - TableBackup backup = dynamoDbService.describeBackup(backupArn, region); - TableDefinition table = tableService.restoreTable(DynamoDbService.snapshotOf(backup.getTable()), - targetTableName, parseRestoreOverrides(request), region); - ObjectNode description = tableToNode(table); - description.set("RestoreSummary", restoreSummaryNode(backup.getBackupArn(), - backup.getTable().getTableArn(), backup.getCreationDateTime())); - dynamoDbService.finishRestore(targetTableName, backup.getItems(), region); - - ObjectNode response = objectMapper.createObjectNode(); - response.set("TableDescription", description); - return Response.ok(response).build(); + return violations; } private static RestoreOverrides parseRestoreOverrides(JsonNode request) { @@ -3280,15 +3359,20 @@ public class NativeDynamoDbJsonHandler { return vectorIndexNode; } - private ObjectNode continuousBackupsDescriptionNode(TableDefinition table) { + /** AWS also answers PointInTimeRecoveryWindowDays, a member its API model does not list. */ + private ObjectNode continuousBackupsDescriptionNode(DynamoDbService.ContinuousBackups backups) { ObjectNode node = objectMapper.createObjectNode(); node.put("ContinuousBackupsStatus", "ENABLED"); ObjectNode pitrNode = objectMapper.createObjectNode(); + TableDefinition table = backups.table(); pitrNode.put("PointInTimeRecoveryStatus", table.isPointInTimeRecoveryEnabled() ? "ENABLED" : "DISABLED"); - if (table.isPointInTimeRecoveryEnabled()) { + if (backups.window() != null) { + pitrNode.put("EarliestRestorableDateTime", epochSeconds(backups.window().earliest())); + pitrNode.put("LatestRestorableDateTime", epochSeconds(backups.window().latest())); pitrNode.put("RecoveryPeriodInDays", table.getPointInTimeRecoveryRecoveryPeriodInDays()); + pitrNode.put("PointInTimeRecoveryWindowDays", table.getPointInTimeRecoveryRecoveryPeriodInDays()); } node.set("PointInTimeRecoveryDescription", pitrNode); return node; diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableService.java index a9ab0402ab..f9e695ffb2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbTableService.java @@ -181,10 +181,10 @@ public class NativeDynamoDbTableService { } /** - * Creates the target of a restore in CREATING from {@code source}, a copy of the table the - * backup holds, with the request's overrides applied. Like AWS, the new table gets the keys, - * indexes, capacity and encryption, and none of the stream, TTL, point-in-time recovery, tags, - * deletion protection or table class. + * Creates the target of a restore in CREATING from {@code source}, a copy of the table a backup + * holds or of the source of a point-in-time restore, with the request's overrides applied. Like + * AWS, the new table gets the keys, indexes, capacity and encryption, and none of the stream, + * TTL, point-in-time recovery, tags, deletion protection or table class. */ TableDefinition restoreTable(TableDefinition source, String targetTableName, RestoreOverrides overrides, String region) { diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/PointInTimeChange.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/PointInTimeChange.java new file mode 100644 index 0000000000..785266a500 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/PointInTimeChange.java @@ -0,0 +1,44 @@ +package io.github.hectorvent.floci.services.dynamodb.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.databind.JsonNode; +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.time.Instant; + +/** + * One item change to a table with point-in-time recovery enabled. {@code oldItem} is what the change + * replaced, null when it created the item. + */ +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class PointInTimeChange { + + private long sequence; + private Instant time; + private String itemKey; + private JsonNode oldItem; + + public PointInTimeChange() { + } + + public PointInTimeChange(long sequence, Instant time, String itemKey, JsonNode oldItem) { + this.sequence = sequence; + this.time = time; + this.itemKey = itemKey; + this.oldItem = oldItem; + } + + public long getSequence() { return sequence; } + public void setSequence(long sequence) { this.sequence = sequence; } + + public Instant getTime() { return time; } + public void setTime(Instant time) { this.time = time; } + + public String getItemKey() { return itemKey; } + public void setItemKey(String itemKey) { this.itemKey = itemKey; } + + public JsonNode getOldItem() { return oldItem; } + // Jackson reads a stored null into a JsonNode as NullNode. + public void setOldItem(JsonNode oldItem) { this.oldItem = oldItem != null && oldItem.isNull() ? null : oldItem; } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java index 22f6b1a138..4f88c55466 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java @@ -37,6 +37,7 @@ public class TableDefinition { private boolean ttlEnabled; private boolean pointInTimeRecoveryEnabled; private int pointInTimeRecoveryRecoveryPeriodInDays; + private Instant pointInTimeRecoveryStartDateTime; private boolean deletionProtectionEnabled; private boolean streamEnabled; private String streamArn; @@ -169,6 +170,11 @@ public class TableDefinition { this.pointInTimeRecoveryRecoveryPeriodInDays = pointInTimeRecoveryRecoveryPeriodInDays; } + public Instant getPointInTimeRecoveryStartDateTime() { return pointInTimeRecoveryStartDateTime; } + public void setPointInTimeRecoveryStartDateTime(Instant pointInTimeRecoveryStartDateTime) { + this.pointInTimeRecoveryStartDateTime = pointInTimeRecoveryStartDateTime; + } + public boolean isDeletionProtectionEnabled() { return deletionProtectionEnabled; } public void setDeletionProtectionEnabled(boolean deletionProtectionEnabled) { this.deletionProtectionEnabled = deletionProtectionEnabled; } diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java index b8aeb09a84..ba6b42d056 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java @@ -977,6 +977,19 @@ public class ResourceArnBuilder { return List.of(importArn); } } + // AWS authorizes RestoreTableToPointInTime on its source table. + if (json.hasNonNull("SourceTableArn")) { + String sourceTableArn = json.get("SourceTableArn").asText().trim(); + if (!sourceTableArn.isEmpty()) { + return List.of(sourceTableArn); + } + } + if (json.hasNonNull("SourceTableName")) { + String sourceTableName = json.get("SourceTableName").asText().trim(); + if (!sourceTableName.isEmpty()) { + return List.of(toDynamoDbTableArn(sourceTableName, region, accountId)); + } + } if (json.hasNonNull("RequestItems") && json.get("RequestItems").isObject()) { Set arns = new LinkedHashSet<>(); Iterator fieldNames = json.get("RequestItems").fieldNames(); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 294b306c1e..5d365c8ded 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -400,6 +400,7 @@ floci: enabled: true vector-index-allocation-seconds: 4 # FLOCI_SERVICES_DYNAMODB_VECTOR_INDEX_ALLOCATION_SECONDS. Resource allocation phase of a vector index added by UpdateTable. vector-index-backfill-seconds: 10 # FLOCI_SERVICES_DYNAMODB_VECTOR_INDEX_BACKFILL_SECONDS. Backfilling phase that follows it. + latest-restorable-lag-seconds: 300 # FLOCI_SERVICES_DYNAMODB_LATEST_RESTORABLE_LAG_SECONDS. How far LatestRestorableDateTime trails now. backend: native # FLOCI_SERVICES_DYNAMODB_BACKEND: native or local # local-endpoint: http://dynamodb-local:8000 # FLOCI_SERVICES_DYNAMODB_LOCAL_ENDPOINT local-connect-timeout-seconds: 2 # FLOCI_SERVICES_DYNAMODB_LOCAL_CONNECT_TIMEOUT_SECONDS diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbBackupIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbBackupIntegrationTest.java index 4a8578cfd6..8d9300ea8d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbBackupIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbBackupIntegrationTest.java @@ -1,7 +1,5 @@ package io.github.hectorvent.floci.services.dynamodb; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.ValidatableResponse; @@ -11,6 +9,9 @@ import org.junit.jupiter.api.Test; import java.math.BigDecimal; import java.util.List; +import static io.github.hectorvent.floci.testing.DynamoDbRequests.CONTENT_TYPE; +import static io.github.hectorvent.floci.testing.DynamoDbRequests.ddb; +import static io.github.hectorvent.floci.testing.DynamoDbRequests.decimalAt; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.*; @@ -18,9 +19,6 @@ import static org.junit.jupiter.api.Assertions.*; @QuarkusTest class DynamoDbBackupIntegrationTest { - private static final String DYNAMODB_CONTENT_TYPE = "application/x-amz-json-1.0"; - // JsonPath reads numbers as float, which drops whole seconds at epoch magnitude. - private static final ObjectMapper EXACT = new ObjectMapper().enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); private static final String TABLE_ARN_PREFIX = "arn:aws:dynamodb:us-east-1:000000000000:table/"; private static final String BACKUP_ARN_PATTERN = "arn:aws:dynamodb:us-east-1:000000000000:table/%s/backup/\\d{14}-[0-9a-f]{8}"; @@ -178,7 +176,7 @@ class DynamoDbBackupIntegrationTest { given().header("Authorization", OTHER_REGION_AUTH) .header("X-Amz-Target", "DynamoDB_20120810.DescribeBackup") - .contentType(DYNAMODB_CONTENT_TYPE) + .contentType(CONTENT_TYPE) .body("{\"BackupArn\": \"" + backupArn + "\"}") .when().post("/") .then() @@ -186,7 +184,7 @@ class DynamoDbBackupIntegrationTest { .body("__type", containsString("BackupNotFoundException")); given().header("Authorization", OTHER_REGION_AUTH) .header("X-Amz-Target", "DynamoDB_20120810.ListBackups") - .contentType(DYNAMODB_CONTENT_TYPE) + .contentType(CONTENT_TYPE) .body("{\"TableName\": \"" + table + "\"}") .when().post("/") .then() @@ -194,7 +192,7 @@ class DynamoDbBackupIntegrationTest { .body("BackupSummaries", empty()); given().header("Authorization", OTHER_ACCOUNT_AUTH) .header("X-Amz-Target", "DynamoDB_20120810.DescribeBackup") - .contentType(DYNAMODB_CONTENT_TYPE) + .contentType(CONTENT_TYPE) .body("{\"BackupArn\": \"" + backupArn + "\"}") .when().post("/") .then() @@ -757,17 +755,4 @@ class DynamoDbBackupIntegrationTest { {"BackupArn": "%s", "TargetTableName": "%s", %s} """.formatted(backupArn, target, overrides)); } - - private static BigDecimal decimalAt(ValidatableResponse response, String pointer) throws Exception { - return EXACT.readTree(response.extract().asString()).at(pointer).decimalValue(); - } - - private ValidatableResponse ddb(String action, String body) { - return given() - .header("X-Amz-Target", "DynamoDB_20120810." + action) - .contentType(DYNAMODB_CONTENT_TYPE) - .body(body) - .when().post("/") - .then(); - } } diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPointInTimeRecoveryIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPointInTimeRecoveryIntegrationTest.java new file mode 100644 index 0000000000..8368ad6398 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPointInTimeRecoveryIntegrationTest.java @@ -0,0 +1,433 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.time.Duration; +import java.time.Instant; +import java.util.List; + +import static io.github.hectorvent.floci.testing.DynamoDbRequests.CONTENT_TYPE; +import static io.github.hectorvent.floci.testing.DynamoDbRequests.ddb; +import static io.github.hectorvent.floci.testing.DynamoDbRequests.decimalAt; +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.*; + +@QuarkusTest +class DynamoDbPointInTimeRecoveryIntegrationTest { + + private static final String TABLE_ARN_PREFIX = "arn:aws:dynamodb:us-east-1:000000000000:table/"; + private static final String PITR = "ContinuousBackupsDescription.PointInTimeRecoveryDescription"; + private static final String OTHER_REGION_AUTH = + "AWS4-HMAC-SHA256 Credential=AKID/20260215/eu-west-2/dynamodb/aws4_request, " + + "SignedHeaders=host, Signature=abc"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void enablingStartsTheRestoreWindowAndDisablingEndsIt() throws Exception { + String table = "PitrIT-window"; + createTable(table); + ddb("DescribeContinuousBackups", "{\"TableName\": \"" + table + "\"}") + .statusCode(200) + .body("ContinuousBackupsDescription.ContinuousBackupsStatus", equalTo("ENABLED")) + .body(PITR + ".PointInTimeRecoveryStatus", equalTo("DISABLED")) + .body(PITR + ".keySet()", contains("PointInTimeRecoveryStatus")); + + long before = System.currentTimeMillis(); + ValidatableResponse enabled = updateContinuousBackups(table, "true, \"RecoveryPeriodInDays\": 10") + .statusCode(200) + .body(PITR + ".PointInTimeRecoveryStatus", equalTo("ENABLED")) + .body(PITR + ".RecoveryPeriodInDays", equalTo(10)) + .body(PITR + ".PointInTimeRecoveryWindowDays", equalTo(10)); + BigDecimal earliest = decimalAt(enabled, "/ContinuousBackupsDescription/PointInTimeRecoveryDescription/EarliestRestorableDateTime"); + assertEquals(0, earliest.remainder(BigDecimal.ONE).signum(), "whole seconds: " + earliest); + assertTrue(before / 1000 <= earliest.longValue() && earliest.longValue() <= System.currentTimeMillis() / 1000); + BigDecimal latest = decimalAt(enabled, "/ContinuousBackupsDescription/PointInTimeRecoveryDescription/LatestRestorableDateTime"); + assertTrue(earliest.compareTo(latest) <= 0, earliest + " <= " + latest); + + ValidatableResponse again = updateContinuousBackups(table, "true") + .statusCode(200) + .body(PITR + ".RecoveryPeriodInDays", equalTo(10)); + assertEquals(earliest, decimalAt(again, + "/ContinuousBackupsDescription/PointInTimeRecoveryDescription/EarliestRestorableDateTime")); + + updateContinuousBackups(table, "false") + .statusCode(200) + .body(PITR + ".keySet()", contains("PointInTimeRecoveryStatus")) + .body(PITR + ".PointInTimeRecoveryStatus", equalTo("DISABLED")); + updateContinuousBackups(table, "true") + .statusCode(200) + .body(PITR + ".RecoveryPeriodInDays", equalTo(35)) + .body(PITR + ".PointInTimeRecoveryWindowDays", equalTo(35)); + } + + @Test + void updateContinuousBackupsChecksInTheAwsOrder() { + String table = "PitrIT-update-checks"; + createTable(table); + String spec = "\"PointInTimeRecoverySpecification\": "; + expectError(ddb("UpdateContinuousBackups", "{" + spec + "{\"PointInTimeRecoveryEnabled\": true}}"), + "ValidationException", "The parameter 'TableName' is required but was not present in the request"); + expectError(ddb("UpdateContinuousBackups", "{\"TableName\": \"a b\"}"), "ValidationException", + "1 validation error detected: Value 'a b' at 'tableName' failed to satisfy constraint: " + + "Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+"); + expectError(ddb("UpdateContinuousBackups", "{\"TableName\": \"" + table + "\"}"), "ValidationException", + "1 validation error detected: Value null at 'pointInTimeRecoverySpecification' failed to satisfy " + + "constraint: Member must not be null"); + expectError(ddb("UpdateContinuousBackups", "{\"TableName\": \"" + table + "\", " + spec + + "{\"RecoveryPeriodInDays\": 0}}"), "ValidationException", + "2 validation errors detected: Value '0' at 'pointInTimeRecoverySpecification.recoveryPeriodInDays' " + + "failed to satisfy constraint: Member must have value greater than or equal to 1; Value null at " + + "'pointInTimeRecoverySpecification.pointInTimeRecoveryEnabled' failed to satisfy constraint: " + + "Member must not be null"); + expectError(updateContinuousBackups(table, "true, \"RecoveryPeriodInDays\": 36"), "ValidationException", + "1 validation error detected: Value '36' at 'pointInTimeRecoverySpecification.recoveryPeriodInDays' " + + "failed to satisfy constraint: Member must have value less than or equal to 35"); + expectError(updateContinuousBackups("PitrIT-missing", "false, \"RecoveryPeriodInDays\": 7"), + "ValidationException", + "Invalid Request: Cannot specify RecoveryPeriodInDays when disabling point-in-time recovery."); + expectError(updateContinuousBackups("PitrIT-missing", "true"), "TableNotFoundException", + "Table not found: PitrIT-missing"); + expectError(ddb("DescribeContinuousBackups", "{}"), "ValidationException", + "The parameter 'TableName' is required but was not present in the request"); + expectError(ddb("DescribeContinuousBackups", "{\"TableName\": \"ab\"}"), "ValidationException", + "1 validation error detected: Value 'ab' at 'tableName' failed to satisfy constraint: " + + "Member must have length greater than or equal to 3"); + expectError(ddb("DescribeContinuousBackups", "{\"TableName\": \"PitrIT-missing\"}"), "TableNotFoundException", + "Table not found: PitrIT-missing"); + } + + @Test + void restoreTableToPointInTimeRestoresTheItemsOfThatTime() { + String table = "PitrIT-items"; + createTable(table); + updateContinuousBackups(table, "true").statusCode(200); + putItem(table, "k1", "v1"); + BigDecimal t1 = pause(); + ddb("UpdateItem", """ + {"TableName": "%s", "Key": {"pk": {"S": "k1"}}, "UpdateExpression": "SET v = :v", + "ExpressionAttributeValues": {":v": {"S": "v2"}}} + """.formatted(table)).statusCode(200); + ddb("BatchWriteItem", """ + {"RequestItems": {"%s": [{"PutRequest": {"Item": {"pk": {"S": "k2"}, "v": {"S": "v1"}}}}]}} + """.formatted(table)).statusCode(200); + BigDecimal t2 = pause(); + ddb("TransactWriteItems", """ + {"TransactItems": [ + {"Delete": {"TableName": "%1$s", "Key": {"pk": {"S": "k1"}}}}, + {"Put": {"TableName": "%1$s", "Item": {"pk": {"S": "k3"}, "v": {"S": "v1"}}}} + ]} + """.formatted(table)).statusCode(200); + ddb("ExecuteStatement", "{\"Statement\": \"UPDATE \\\"" + table + "\\\" SET v = 'v2' WHERE pk = 'k3'\"}") + .statusCode(200); + + restoreToTime(table, "PitrIT-items-t1", t1) + .statusCode(200) + .body("TableDescription.TableName", equalTo("PitrIT-items-t1")) + .body("TableDescription.TableStatus", equalTo("CREATING")) + .body("TableDescription.RestoreSummary.SourceTableArn", equalTo(TABLE_ARN_PREFIX + table)) + .body("TableDescription.RestoreSummary.RestoreInProgress", equalTo(true)) + .body("TableDescription.RestoreSummary", not(hasKey("SourceBackupArn"))); + assertEquals(List.of("k1=v1"), items("PitrIT-items-t1")); + restoreToTime(table, "PitrIT-items-t2", t2).statusCode(200); + assertEquals(List.of("k1=v2", "k2=v1"), items("PitrIT-items-t2")); + ddb("RestoreTableToPointInTime", """ + {"SourceTableArn": "%s", "TargetTableName": "PitrIT-items-latest", "UseLatestRestorableTime": true} + """.formatted(TABLE_ARN_PREFIX + table)).statusCode(200); + assertEquals(List.of("k2=v1", "k3=v2"), items("PitrIT-items-latest")); + + ddb("DescribeTable", "{\"TableName\": \"PitrIT-items-t1\"}") + .statusCode(200) + .body("Table.TableStatus", equalTo("ACTIVE")) + .body("Table", not(hasKey("RestoreSummary"))); + ddb("DescribeContinuousBackups", "{\"TableName\": \"PitrIT-items-t1\"}") + .statusCode(200) + .body(PITR + ".PointInTimeRecoveryStatus", equalTo("DISABLED")); + } + + @Test + void restoreSummaryNamesTheRequestedTime() throws Exception { + String table = "PitrIT-summary"; + createTable(table); + updateContinuousBackups(table, "true").statusCode(200); + BigDecimal t1 = pause(); + + ValidatableResponse restored = restoreToTime(table, "PitrIT-summary-t1", t1).statusCode(200); + assertEquals(0, t1.compareTo(decimalAt(restored, "/TableDescription/RestoreSummary/RestoreDateTime"))); + } + + @Test + void restoreRefusesATimeOutsideTheWindow() throws Exception { + String table = "PitrIT-window-checks"; + createTable(table); + BigDecimal earliest = decimalAt(updateContinuousBackups(table, "true").statusCode(200), + "/ContinuousBackupsDescription/PointInTimeRecoveryDescription/EarliestRestorableDateTime"); + + String message = restoreToTime(table, "PitrIT-never", earliest.subtract(BigDecimal.ONE)) + .statusCode(400) + .body("__type", containsString("InvalidRestoreTimeException")) + .extract().path("message"); + String earliestText = Instant.ofEpochSecond(earliest.longValue()).toString(); + String requested = Instant.ofEpochSecond(earliest.longValue() - 1).toString(); + assertTrue(message.matches("RestoreDateTime '" + requested + "' must be within EarliestRestorableDateTime '" + + earliestText + "' and LatestRestorableDateTime '[0-9T:.\\-]+Z' for table '" + table + "'"), message); + restoreToTime(table, "PitrIT-never", earliest.add(BigDecimal.valueOf(Duration.ofHours(1).toSeconds()))) + .statusCode(400) + .body("__type", containsString("InvalidRestoreTimeException")); + restoreToTime(table, "PitrIT-window-checks-earliest", earliest).statusCode(200); + } + + @Test + void restoreTableToPointInTimeChecksInTheAwsOrder() { + String table = "PitrIT-checks"; + String off = "PitrIT-checks-off"; + createTable(table); + createTable(off); + updateContinuousBackups(table, "true").statusCode(200); + String latest = "\"UseLatestRestorableTime\": true"; + + expectError(restore("\"SourceTableName\": \"" + table + "\", " + latest), "ValidationException", + "The parameter 'TableName' is required but was not present in the request"); + expectError(restore("\"TargetTableName\": \"c d\", " + latest), "ValidationException", + "The parameter 'TableName' is required but was not present in the request"); + expectError(restore("\"SourceTableName\": \"ab\", \"TargetTableName\": \"c d\", " + latest), + "ValidationException", "TableName must be at least 3 characters long and at most 255 characters long"); + expectError(restore(""" + "SourceTableName": "a b", "TargetTableName": "c d", "BillingModeOverride": "BAD", + "ProvisionedThroughputOverride": {}, + "GlobalSecondaryIndexOverride": [{"IndexName": "a", "KeySchema": [{"AttributeName": "g", "KeyType": "HASH"}], + "Projection": {"ProjectionType": "ALL"}, "ProvisionedThroughput": {"ReadCapacityUnits": 0}}], + """ + latest), "ValidationException", + "8 validation errors detected: " + + "Value 'a b' at 'sourceTableName' failed to satisfy constraint: Member must satisfy regular " + + "expression pattern: [a-zA-Z0-9_.-]+; " + + "Value null at 'provisionedThroughputOverride.writeCapacityUnits' failed to satisfy constraint: " + + "Member must not be null; " + + "Value null at 'provisionedThroughputOverride.readCapacityUnits' failed to satisfy constraint: " + + "Member must not be null; " + + "Value 'BAD' at 'billingModeOverride' failed to satisfy constraint: Member must satisfy enum value " + + "set: [PROVISIONED, PAY_PER_REQUEST]; " + + "Value 'c d' at 'targetTableName' failed to satisfy constraint: Member must satisfy regular " + + "expression pattern: [a-zA-Z0-9_.-]+; " + + "Value 'a' at 'globalSecondaryIndexOverride.1.member.indexName' failed to satisfy constraint: " + + "Member must have length greater than or equal to 3; " + + "Value null at 'globalSecondaryIndexOverride.1.member.provisionedThroughput.writeCapacityUnits' " + + "failed to satisfy constraint: Member must not be null; " + + "Value '0' at 'globalSecondaryIndexOverride.1.member.provisionedThroughput.readCapacityUnits' " + + "failed to satisfy constraint: Member must have value greater than or equal to 1"); + String both = "\"SourceTableName\": \"%s\", \"SourceTableArn\": \"%s\", ".formatted(table, TABLE_ARN_PREFIX + table); + expectError(restore(both + "\"TargetTableName\": \"c d\", " + latest), "ValidationException", + "1 validation error detected: Value 'c d' at 'targetTableName' failed to satisfy constraint: " + + "Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+"); + expectError(restore(both + "\"TargetTableName\": \"PitrIT-never\", " + latest), "ValidationException", + "Invalid Request: Must provide exactly one of: sourceTableArn, sourceTableName"); + expectError(restore("\"SourceTableName\": \"PitrIT-missing\", \"TargetTableName\": \"PitrIT-never\", " + + latest + ", \"RestoreDateTime\": 1"), "ValidationException", + "Invalid Request: Both RestoreDateTime and UseLatestRestorableTime cannot be set for point-in-time " + + "restore request. Please specify either RestoreDateTime time or UseLatestRestorableTime"); + expectError(restore("\"SourceTableName\": \"PitrIT-missing\", \"TargetTableName\": \"PitrIT-never\""), + "TableNotFoundException", "Table not found: PitrIT-missing"); + expectError(restore("\"SourceTableName\": \"" + off + "\", \"TargetTableName\": \"PitrIT-never\", " + + "\"UseLatestRestorableTime\": false"), "ValidationException", + "Invalid Request: Either one of RestoreDateTime or UseLatestRestorableTime must be specified, but not both"); + expectError(restore("\"SourceTableName\": \"" + off + "\", \"TargetTableName\": \"PitrIT-never\", " + + "\"BillingModeOverride\": \"PROVISIONED\", " + latest), "PointInTimeRecoveryUnavailableException", + "Point in time recovery is not enabled for table '" + off + "'"); + expectError(restore("\"SourceTableName\": \"" + table + "\", \"TargetTableName\": \"" + off + "\", " + + "\"BillingModeOverride\": \"PROVISIONED\", " + latest), "ValidationException", + "Invalid Request: Must override ProvisionedThroughput if BillingMode is overridden to PROVISIONED"); + expectError(restore("\"SourceTableName\": \"" + table + "\", \"TargetTableName\": \"" + table + "\", " + latest), + "TableAlreadyExistsException", "Table already exists: " + table); + } + + @Test + void sourceTableArnIsCheckedBeforeTheValidationList() { + String table = "PitrIT-arn"; + createTable(table); + updateContinuousBackups(table, "true").statusCode(200); + String rest = ", \"TargetTableName\": \"c d\", \"UseLatestRestorableTime\": true"; + + expectError(restore("\"SourceTableArn\": \"abc\"" + rest), "ValidationException", + "One or more parameter values were invalid: ARNs must start with 'arn:': abc"); + expectError(restore("\"SourceTableArn\": \"arn:aws:s3:::bucket\"" + rest), "ValidationException", + "One or more parameter values were invalid: Invalid resource Arn: arn:aws:s3:::bucket"); + expectError(restore("\"SourceTableArn\": \"arn:aws:dynamodb:us-east-1:000000000000:foo/bar\"" + rest), + "ValidationException", "One or more parameter values were invalid: sourceTableArn is not a valid ARN"); + expectError(restore("\"SourceTableArn\": \"" + TABLE_ARN_PREFIX + "ab\"" + rest), "ValidationException", + "Invalid Request: Invalid TableArn: Invalid Table ARN"); + expectError(restore("\"SourceTableArn\": \"arn:aws:dynamodb:us-east-1:123456789012:table/" + table + "\"" + + rest), "ValidationException", + "This action is only supported by accounts that match the resource owner’s account."); + expectError(restore("\"SourceTableArn\": \"arn:aws:dynamodb:eu-west-2:000000000000:table/" + table + "\"" + + rest), "ValidationException", + "Invalid Request: sseSpecificationOverride must be provided for cross-region restores"); + expectError(restore("\"SourceTableArn\": \"" + TABLE_ARN_PREFIX + "PitrIT-missing\", " + + "\"TargetTableName\": \"PitrIT-never\", \"UseLatestRestorableTime\": true"), + "TableNotFoundException", "Table not found: PitrIT-missing"); + expectError(restore("\"SourceTableArn\": \"arn:aws:dynamodb:eu-west-2:000000000000:table/PitrIT-missing\", " + + "\"TargetTableName\": \"PitrIT-never\", \"UseLatestRestorableTime\": true, " + + "\"SSESpecificationOverride\": {\"Enabled\": true, \"SSEType\": \"KMS\"}"), + "TableNotFoundException", "Table not found"); + } + + @Test + void restoreAcrossRegionsNeedsAnSseOverride() { + String table = "PitrIT-cross-region"; + given() + .header("X-Amz-Target", "DynamoDB_20120810.CreateTable") + .header("Authorization", OTHER_REGION_AUTH) + .contentType(CONTENT_TYPE) + .body(createTableBody(table)) + .when().post("/") + .then().statusCode(200); + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateContinuousBackups") + .header("Authorization", OTHER_REGION_AUTH) + .contentType(CONTENT_TYPE) + .body("{\"TableName\": \"" + table + "\", \"PointInTimeRecoverySpecification\": " + + "{\"PointInTimeRecoveryEnabled\": true}}") + .when().post("/") + .then().statusCode(200); + given() + .header("X-Amz-Target", "DynamoDB_20120810.PutItem") + .header("Authorization", OTHER_REGION_AUTH) + .contentType(CONTENT_TYPE) + .body("{\"TableName\": \"" + table + "\", \"Item\": {\"pk\": {\"S\": \"k1\"}, \"v\": {\"S\": \"v1\"}}}") + .when().post("/") + .then().statusCode(200); + + String sourceArn = "arn:aws:dynamodb:eu-west-2:000000000000:table/" + table; + restore(""" + "SourceTableArn": "%s", "TargetTableName": "PitrIT-cross-region-copy", "UseLatestRestorableTime": true, + "SSESpecificationOverride": {"Enabled": true, "SSEType": "KMS"} + """.formatted(sourceArn)) + .statusCode(200) + .body("TableDescription.TableArn", equalTo(TABLE_ARN_PREFIX + "PitrIT-cross-region-copy")) + .body("TableDescription.RestoreSummary.SourceTableArn", equalTo(sourceArn)) + .body("TableDescription.SSEDescription.SSEType", equalTo("KMS")); + assertEquals(List.of("k1=v1"), items("PitrIT-cross-region-copy")); + } + + @Test + void deletingATableWithPointInTimeRecoveryLeavesASystemBackup() throws Exception { + String table = "PitrIT-deleted"; + createTable(table); + updateContinuousBackups(table, "true, \"RecoveryPeriodInDays\": 7").statusCode(200); + putItem(table, "k1", "v1"); + ddb("DeleteTable", "{\"TableName\": \"" + table + "\"}").statusCode(200); + + ddb("ListBackups", "{\"TableName\": \"" + table + "\"}") + .statusCode(200) + .body("BackupSummaries", empty()); + ValidatableResponse listed = ddb("ListBackups", "{\"TableName\": \"" + table + "\", \"BackupType\": \"SYSTEM\"}") + .statusCode(200) + .body("BackupSummaries", hasSize(1)) + .body("BackupSummaries[0].BackupName", equalTo(table + "$DeletedTableBackup")) + .body("BackupSummaries[0].BackupType", equalTo("SYSTEM")) + .body("BackupSummaries[0].BackupStatus", equalTo("AVAILABLE")) + .body("BackupSummaries[0].TableArn", equalTo(TABLE_ARN_PREFIX + table)); + String backupArn = listed.extract().path("BackupSummaries[0].BackupArn"); + assertTrue(backupArn.matches(TABLE_ARN_PREFIX + table + "/backup/\\d{14}-00000000"), backupArn); + BigDecimal created = decimalAt(listed, "/BackupSummaries/0/BackupCreationDateTime"); + BigDecimal expires = decimalAt(listed, "/BackupSummaries/0/BackupExpiryDateTime"); + assertEquals(Duration.ofDays(35).toMillis(), expires.subtract(created).movePointRight(3).longValueExact()); + ddb("ListBackups", "{\"TableName\": \"" + table + "\", \"BackupType\": \"ALL\"}") + .statusCode(200) + .body("BackupSummaries.BackupArn", contains(backupArn)); + + ddb("DescribeBackup", "{\"BackupArn\": \"" + backupArn + "\"}") + .statusCode(200) + .body("BackupDescription.BackupDetails.BackupType", equalTo("SYSTEM")) + .body("BackupDescription.SourceTableDetails.ItemCount", equalTo(1)); + String expiry = Instant.ofEpochMilli(expires.movePointRight(3).longValue()).toString(); + expectError(ddb("DeleteBackup", "{\"BackupArn\": \"" + backupArn + "\"}"), "ValidationException", + "Invalid Request: User is not allowed to delete the system backup with arn " + backupArn + + ". It will automatically expire on " + expiry); + + ddb("RestoreTableFromBackup", """ + {"BackupArn": "%s", "TargetTableName": "PitrIT-deleted-back"} + """.formatted(backupArn)).statusCode(200); + assertEquals(List.of("k1=v1"), items("PitrIT-deleted-back")); + expectError(ddb("DescribeContinuousBackups", "{\"TableName\": \"" + table + "\"}"), "TableNotFoundException", + "Table not found: " + table); + } + + @Test + void deletingATableWithoutPointInTimeRecoveryLeavesNoBackup() { + String table = "PitrIT-deleted-plain"; + createTable(table); + ddb("DeleteTable", "{\"TableName\": \"" + table + "\"}").statusCode(200); + + ddb("ListBackups", "{\"TableName\": \"" + table + "\", \"BackupType\": \"ALL\"}") + .statusCode(200) + .body("BackupSummaries", empty()); + } + + /** Returns a time between the writes before and after the call, in epoch seconds with milliseconds. */ + private static BigDecimal pause() { + long now = System.currentTimeMillis(); + while (System.currentTimeMillis() <= now) { + Thread.onSpinWait(); + } + return BigDecimal.valueOf(now, 3); + } + + private void createTable(String table) { + ddb("CreateTable", createTableBody(table)).statusCode(200); + } + + private static String createTableBody(String table) { + return """ + { + "TableName": "%s", + "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}], + "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(table); + } + + private void putItem(String table, String pk, String value) { + ddb("PutItem", """ + {"TableName": "%s", "Item": {"pk": {"S": "%s"}, "v": {"S": "%s"}}} + """.formatted(table, pk, value)).statusCode(200); + } + + private List items(String table) { + List pks = ddb("Scan", "{\"TableName\": \"" + table + "\"}") + .statusCode(200) + .extract().path("Items.collect { it.pk.S + '=' + it.v.S }"); + return pks.stream().sorted().toList(); + } + + private ValidatableResponse updateContinuousBackups(String table, String spec) { + return ddb("UpdateContinuousBackups", """ + {"TableName": "%s", "PointInTimeRecoverySpecification": {"PointInTimeRecoveryEnabled": %s}} + """.formatted(table, spec)); + } + + private ValidatableResponse restoreToTime(String source, String target, BigDecimal time) { + return restore(""" + "SourceTableName": "%s", "TargetTableName": "%s", "RestoreDateTime": %s + """.formatted(source, target, time.toPlainString())); + } + + private ValidatableResponse restore(String members) { + return ddb("RestoreTableToPointInTime", "{" + members + "}"); + } + + private static void expectError(ValidatableResponse response, String type, String message) { + response.statusCode(400) + .body("__type", containsString(type)) + .body("message", equalTo(message)); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java index 785435c017..ff6229bb8d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java @@ -18,10 +18,12 @@ import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex; import io.github.hectorvent.floci.services.dynamodb.model.ImportTableDescription; import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex; +import io.github.hectorvent.floci.services.dynamodb.model.PointInTimeChange; import io.github.hectorvent.floci.services.dynamodb.model.TableBackup; import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition; import io.github.hectorvent.floci.services.s3.S3Service; import io.github.hectorvent.floci.services.s3.model.S3Object; +import io.github.hectorvent.floci.testing.MutableClock; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; @@ -31,6 +33,8 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -60,6 +64,8 @@ class DynamoDbServiceTest { private DynamoDbService service; private ObjectMapper mapper; + // Moves on by a millisecond each time it is read, so a read falls between two writes. + private final MutableClock clock = new MutableClock(); @BeforeEach void setUp() { @@ -4622,4 +4628,149 @@ class DynamoDbServiceTest { assertEquals(1, reloaded.getTable().getItemCount()); assertFalse(reloaded.getTable().isTtlEnabled()); } + + @Test + void pointInTimeStateUndoesATtlDeletionAfterTheRestoreTime() { + String region = "us-east-1"; + DynamoDbService pitr = pointInTimeService(new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), 0); + pitr.createTable("Users", List.of(new KeySchemaElement("userId", "HASH")), + List.of(new AttributeDefinition("userId", "S")), 5L, 5L, region); + pitr.updateTimeToLive("Users", "expiresAt", true, region); + pitr.updateContinuousBackups("Users", true, null, region); + ObjectNode expired = item("userId", "u1"); + expired.set("expiresAt", attributeValue("N", "1")); + pitr.putItem("Users", expired, null, null, null, region, null); + Instant beforeSweep = clock.instant(); + pitr.deleteExpiredItems(); + + assertEquals(List.of("u1"), userIds(pitr.pointInTimeState("Users", region, region, false, beforeSweep))); + assertEquals(List.of(), userIds(pitr.pointInTimeState("Users", region, region, true, null))); + } + + @Test + void pointInTimeHistorySurvivesARestart(@TempDir Path dir) { + StorageBackend tables = new InMemoryStorage<>(); + StorageBackend> items = new InMemoryStorage<>(); + Path historyFile = dir.resolve("dynamodb-point-in-time.json"); + TypeReference> historyType = new TypeReference<>() {}; + DynamoDbService before = pointInTimeService(tables, items, new InMemoryStorage<>(), + new PersistentStorage<>(historyFile, historyType), 0); + before.createTable("Users", List.of(new KeySchemaElement("userId", "HASH")), + List.of(new AttributeDefinition("userId", "S")), 5L, 5L, "us-east-1"); + before.updateContinuousBackups("Users", true, null, "us-east-1"); + before.putItem("Users", item("userId", "u1"), null, null, null, "us-east-1", null); + Instant t1 = clock.instant(); + before.putItem("Users", item("userId", "u2"), null, null, null, "us-east-1", null); + Instant earliest = before.describeContinuousBackups("Users", "us-east-1").window().earliest(); + + PersistentStorage reloaded = new PersistentStorage<>(historyFile, historyType); + reloaded.load(); + DynamoDbService restarted = pointInTimeService(tables, items, new InMemoryStorage<>(), reloaded, 0); + + assertEquals(earliest, restarted.describeContinuousBackups("Users", "us-east-1").window().earliest()); + assertEquals(List.of("u1"), userIds(restarted.pointInTimeState("Users", "us-east-1", "us-east-1", false, t1))); + assertEquals(List.of("u1", "u2"), + userIds(restarted.pointInTimeState("Users", "us-east-1", "us-east-1", true, null))); + } + + @Test + void pointInTimeRestoreTakesTimesAfterTheReportedLatestUpToNow() { + String region = "us-east-1"; + DynamoDbService lagged = pointInTimeService(new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), 300); + lagged.createTable("Users", List.of(new KeySchemaElement("userId", "HASH")), + List.of(new AttributeDefinition("userId", "S")), 5L, 5L, region); + DynamoDbPointInTimeRecovery.RestoreWindow window = + lagged.updateContinuousBackups("Users", true, null, region).window(); + assertEquals(window.earliest(), window.latest()); + lagged.putItem("Users", item("userId", "u1"), null, null, null, region, null); + Instant afterWrite = clock.instant(); + + assertEquals(List.of("u1"), userIds(lagged.pointInTimeState("Users", region, region, false, afterWrite))); + assertEquals(List.of(), userIds(lagged.pointInTimeState("Users", region, region, true, null))); + AwsException future = assertThrows(AwsException.class, () -> + lagged.pointInTimeState("Users", region, region, false, clock.instant().plusSeconds(60))); + assertEquals("InvalidRestoreTimeException", future.getErrorCode()); + } + + @Test + void pointInTimeChangesAreStoredOneByOneAndDroppedWithTheirTable() { + String region = "us-east-1"; + StorageBackend changes = new InMemoryStorage<>(); + DynamoDbService pitr = pointInTimeService(new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), changes, 0); + pitr.createTable("Users", List.of(new KeySchemaElement("userId", "HASH")), + List.of(new AttributeDefinition("userId", "S")), 5L, 5L, region); + pitr.updateContinuousBackups("Users", true, null, region); + pitr.putItem("Users", item("userId", "u1"), null, null, null, region, null); + pitr.putItem("Users", item("userId", "u1", "name", "one"), null, null, null, region, null); + pitr.deleteItem("Users", item("userId", "u1"), region); + assertEquals(3, changes.keys().size()); + + pitr.updateContinuousBackups("Users", false, null, region); + assertEquals(Set.of(), changes.keys()); + pitr.updateContinuousBackups("Users", true, null, region); + pitr.putItem("Users", item("userId", "u2"), null, null, null, region, null); + assertEquals(1, changes.keys().size()); + pitr.deleteTable("Users", region); + assertEquals(Set.of(), changes.keys()); + } + + @Test + void aTableEnabledBeforeTheHistoryExistedKeepsItsEarliestTimeAcrossARestart(@TempDir Path dir) { + String region = "us-east-1"; + Path tableFile = dir.resolve("dynamodb-tables.json"); + TypeReference> tableType = new TypeReference<>() {}; + PersistentStorage tables = new PersistentStorage<>(tableFile, tableType); + DynamoDbService before = pointInTimeService(tables, new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), 0); + before.createTable("Users", List.of(new KeySchemaElement("userId", "HASH")), + List.of(new AttributeDefinition("userId", "S")), 5L, 5L, region); + TableDefinition legacy = tables.get(region + "::Users").orElseThrow(); + legacy.setPointInTimeRecoveryEnabled(true); + tables.put(region + "::Users", legacy); + Instant earliest = before.describeContinuousBackups("Users", region).window().earliest(); + clock.advance(Duration.ofSeconds(1)); + + PersistentStorage reloaded = new PersistentStorage<>(tableFile, tableType); + reloaded.load(); + DynamoDbService restarted = pointInTimeService(reloaded, new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), 0); + + assertEquals(earliest, restarted.describeContinuousBackups("Users", region).window().earliest()); + } + + @Test + void theTtlSweepDeletesAnExpiredSystemBackup() { + String region = "us-east-1"; + StorageBackend backups = new InMemoryStorage<>(); + DynamoDbService swept = pointInTimeService(new InMemoryStorage<>(), new InMemoryStorage<>(), backups, + new InMemoryStorage<>(), 0); + for (String table : List.of("Expired", "Kept")) { + swept.createTable(table, List.of(new KeySchemaElement("userId", "HASH")), + List.of(new AttributeDefinition("userId", "S")), 5L, 5L, region); + swept.updateContinuousBackups(table, true, null, region); + swept.deleteTable(table, region); + clock.advance(Duration.ofDays(20)); + } + + swept.deleteExpiredItems(); + + assertEquals(List.of("Kept"), backups.scan(key -> true).stream().map(b -> b.getTable().getTableName()).toList()); + } + + private DynamoDbService pointInTimeService(StorageBackend tables, + StorageBackend> items, + StorageBackend backups, + StorageBackend changes, + int latestRestorableLagSeconds) { + return new DynamoDbService(tables, items, null, null, backups, changes, + new RegionResolver("us-east-1", "000000000000"), null, null, null, mapper, 0, 0, + latestRestorableLagSeconds, clock); + } + + private static List userIds(DynamoDbService.PointInTimeState state) { + return state.items().stream().map(i -> i.path("userId").path("S").asText()).sorted().toList(); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java index 81464a8aa2..c4874d3c43 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java @@ -820,6 +820,16 @@ class ResourceArnBuilderTest { assertEquals(exportArn, arn); } + @Test + void dynamoDbBuildsArnFromTheSourceOfAPointInTimeRestore() { + setJsonBody("{\"SourceTableName\":\"Music\",\"TargetTableName\":\"MusicCopy\"}"); + assertEquals("arn:aws:dynamodb:us-east-1:000000000000:table/Music", + builder.build("dynamodb", ctx, "us-east-1", "000000000000")); + String sourceArn = "arn:aws:dynamodb:eu-west-2:000000000000:table/Music"; + setJsonBody("{\"SourceTableArn\":\"" + sourceArn + "\",\"TargetTableName\":\"MusicCopy\"}"); + assertEquals(sourceArn, builder.build("dynamodb", ctx, "us-east-1", "000000000000")); + } + @Test void dynamoDbBuildsArnFromSingleTableBatchRequest() { setJsonBody("{\"RequestItems\":{\"MyTable\":{\"Keys\":[]}}}"); diff --git a/src/test/java/io/github/hectorvent/floci/testing/DynamoDbRequests.java b/src/test/java/io/github/hectorvent/floci/testing/DynamoDbRequests.java new file mode 100644 index 0000000000..308c3b0ef3 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/DynamoDbRequests.java @@ -0,0 +1,31 @@ +package io.github.hectorvent.floci.testing; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.restassured.response.ValidatableResponse; + +import java.math.BigDecimal; + +import static io.restassured.RestAssured.given; + +public final class DynamoDbRequests { + + public static final String CONTENT_TYPE = "application/x-amz-json-1.0"; + // JsonPath reads numbers as float, which drops whole seconds at epoch magnitude. + private static final ObjectMapper EXACT = new ObjectMapper().enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); + + private DynamoDbRequests() {} + + public static ValidatableResponse ddb(String action, String body) { + return given() + .header("X-Amz-Target", "DynamoDB_20120810." + action) + .contentType(CONTENT_TYPE) + .body(body) + .when().post("/") + .then(); + } + + public static BigDecimal decimalAt(ValidatableResponse response, String pointer) throws Exception { + return EXACT.readTree(response.extract().asString()).at(pointer).decimalValue(); + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index ae85494a1f..5138a49247 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -151,6 +151,7 @@ floci: enabled: true vector-index-allocation-seconds: 0 vector-index-backfill-seconds: 0 + latest-restorable-lag-seconds: 0 backend: native sns: enabled: true