mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(dynamodb): restore backups across regions (#6349)
* feat(dynamodb): restore backups across regions * fix(dynamodb): refuse restores across partitions
This commit is contained in:
1 parent
a96921afad
commit
05d4786138
5 files changed
+127
-8
No files matched your search
+85
-2
@@ -25,6 +25,9 @@ class DynamoDbBackupIntegrationTest {
|
||||
private static final String OTHER_REGION_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260215/eu-west-2/dynamodb/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
private static final String OTHER_PARTITION_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260215/cn-north-1/dynamodb/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
private static final String OTHER_ACCOUNT_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=000000000001/20260215/us-east-1/dynamodb/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
@@ -563,6 +566,69 @@ class DynamoDbBackupIntegrationTest {
|
||||
.body("message", equalTo("Invalid Request: BackupArn is not valid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreAcrossRegionsNeedsAnSseOverride() {
|
||||
String table = "BackupIT-cross-region";
|
||||
createPlainTable(table);
|
||||
putItem(table, "k1");
|
||||
String backupArn = createBackup(table, "cross-region");
|
||||
String sse = ", \"SSESpecificationOverride\": {\"Enabled\": true, \"SSEType\": \"KMS\"}";
|
||||
String missing = TABLE_ARN_PREFIX + table + "/backup/01700000000000-abcdef12";
|
||||
String badId = TABLE_ARN_PREFIX + table + "/backup/1700000000000-abcdef12";
|
||||
|
||||
for (String request : List.of(restoreRequest(backupArn, ""), restoreRequest(missing, ""),
|
||||
restoreRequest(badId, ""), restoreRequest(backupArn, ", \"SSESpecificationOverride\": null"))) {
|
||||
signedWith(OTHER_REGION_AUTH, "RestoreTableFromBackup", request)
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(
|
||||
"Invalid Request: sseSpecificationOverride must be provided for cross-region restores"));
|
||||
}
|
||||
signedWith(OTHER_REGION_AUTH, "RestoreTableFromBackup", restoreRequest(badId, sse))
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Invalid Request: BackupArn is not valid"));
|
||||
signedWith(OTHER_REGION_AUTH, "RestoreTableFromBackup", restoreRequest(missing, sse))
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("BackupNotFoundException"));
|
||||
|
||||
signedWith(OTHER_REGION_AUTH, "RestoreTableFromBackup", """
|
||||
{"BackupArn": "%s", "TargetTableName": "BackupIT-cross-region-copy"%s}
|
||||
""".formatted(backupArn, sse))
|
||||
.statusCode(200)
|
||||
.body("TableDescription.TableArn",
|
||||
equalTo("arn:aws:dynamodb:eu-west-2:000000000000:table/BackupIT-cross-region-copy"))
|
||||
.body("TableDescription.RestoreSummary.SourceBackupArn", equalTo(backupArn))
|
||||
.body("TableDescription.RestoreSummary.SourceTableArn", equalTo(TABLE_ARN_PREFIX + table))
|
||||
.body("TableDescription.SSEDescription.KMSMasterKeyArn", startsWith("arn:aws:kms:eu-west-2:"));
|
||||
signedWith(OTHER_REGION_AUTH, "Scan", "{\"TableName\": \"BackupIT-cross-region-copy\"}")
|
||||
.statusCode(200)
|
||||
.body("Items.pk.S", contains("k1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreOfABackupInAnotherPartitionFails() {
|
||||
String table = "BackupIT-other-partition";
|
||||
signedWith(OTHER_PARTITION_AUTH, "CreateTable", plainTableBody(table)).statusCode(200);
|
||||
String backupArn = signedWith(OTHER_PARTITION_AUTH, "CreateBackup",
|
||||
"{\"TableName\": \"" + table + "\", \"BackupName\": \"other-partition\"}")
|
||||
.statusCode(200)
|
||||
.extract().path("BackupDetails.BackupArn");
|
||||
String target = "BackupIT-other-partition-target";
|
||||
createPlainTable(target);
|
||||
String sse = "\"SSESpecificationOverride\": {\"Enabled\": true}";
|
||||
|
||||
ddb("RestoreTableFromBackup", restoreRequest(backupArn, ""))
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(
|
||||
"Invalid Request: sseSpecificationOverride must be provided for cross-region restores"));
|
||||
restore(backupArn, target, sse)
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("TableAlreadyExistsException"));
|
||||
ddb("RestoreTableFromBackup", restoreRequest(backupArn, ", " + sse))
|
||||
.statusCode(500)
|
||||
.body("__type", containsString("InternalServerError"))
|
||||
.body("message", equalTo("Internal server error"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreReportsEveryViolationBeforeTheBackupLookup() {
|
||||
String arn = TABLE_ARN_PREFIX + "BackupIT-none/backup/01700000000000-abcdef12";
|
||||
@@ -865,14 +931,18 @@ class DynamoDbBackupIntegrationTest {
|
||||
}
|
||||
|
||||
private void createPlainTable(String table) {
|
||||
ddb("CreateTable", """
|
||||
ddb("CreateTable", plainTableBody(table)).statusCode(200);
|
||||
}
|
||||
|
||||
private static String plainTableBody(String table) {
|
||||
return """
|
||||
{
|
||||
"TableName": "%s",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"ProvisionedThroughput": {"ReadCapacityUnits": 1, "WriteCapacityUnits": 1}
|
||||
}
|
||||
""".formatted(table)).statusCode(200);
|
||||
""".formatted(table);
|
||||
}
|
||||
|
||||
private void putItem(String table, String pk) {
|
||||
@@ -899,6 +969,19 @@ class DynamoDbBackupIntegrationTest {
|
||||
""".formatted(backupArn, target, overrides));
|
||||
}
|
||||
|
||||
private static String restoreRequest(String backupArn, String overrides) {
|
||||
return "{\"BackupArn\": \"" + backupArn + "\", \"TargetTableName\": \"BackupIT-never\"" + overrides + "}";
|
||||
}
|
||||
|
||||
private static ValidatableResponse signedWith(String authorization, String action, String body) {
|
||||
return given().header("Authorization", authorization)
|
||||
.header("X-Amz-Target", "DynamoDB_20120810." + action)
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body(body)
|
||||
.when().post("/")
|
||||
.then();
|
||||
}
|
||||
|
||||
private static ValidatableResponse listBackupsBetween(String lowerBound, String upperBound) {
|
||||
return ddb("ListBackups", """
|
||||
{"TimeRangeLowerBound": %s, "TimeRangeUpperBound": %s}
|
||||
|
||||
+10
@@ -277,6 +277,12 @@ class DynamoDbPointInTimeRecoveryIntegrationTest {
|
||||
"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");
|
||||
String emptyPartition = "arn::dynamodb:us-east-1:000000000000:table/" + table;
|
||||
expectError(restore("\"SourceTableArn\": \"" + emptyPartition + "\"" + rest), "ValidationException",
|
||||
"One or more parameter values were invalid: Partition must be non-empty: " + emptyPartition);
|
||||
String otherPartition = "arn:aws-cn:dynamodb:cn-north-1:000000000000:table/" + table;
|
||||
expectError(restore("\"SourceTableArn\": \"" + otherPartition + "\"" + rest), "ValidationException",
|
||||
"One or more parameter values were invalid: Invalid resource Arn: " + otherPartition);
|
||||
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",
|
||||
@@ -294,6 +300,10 @@ class DynamoDbPointInTimeRecoveryIntegrationTest {
|
||||
+ "\"TargetTableName\": \"PitrIT-never\", \"UseLatestRestorableTime\": true, "
|
||||
+ "\"SSESpecificationOverride\": {\"Enabled\": true, \"SSEType\": \"KMS\"}"),
|
||||
"TableNotFoundException", "Table not found");
|
||||
expectError(restore("\"SourceTableArn\": \"arn:aws:dynamodb:cn-north-1:000000000000:table/PitrIT-missing\", "
|
||||
+ "\"TargetTableName\": \"PitrIT-never\", \"UseLatestRestorableTime\": true, "
|
||||
+ "\"SSESpecificationOverride\": {\"Enabled\": true}"),
|
||||
"ValidationException", "Invalid Request: Table ARN is invalid.");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user