feat(dynamodb): restore backups across regions (#6349)

* feat(dynamodb): restore backups across regions

* fix(dynamodb): refuse restores across partitions
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-23 20:51:42 -04:00
1 parent a96921afad
commit 05d4786138
5 files changed
+127 -8

No files matched your search

@@ -11,6 +11,7 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.Digests;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestScopes;
@@ -2966,7 +2967,10 @@ public class DynamoDbService {
} catch (IllegalArgumentException e) {
throw invalidParameter("sourceTableArn is not a valid ARN");
}
if (!"dynamodb".equals(arn.service())) {
if (arn.partition().isEmpty()) {
throw invalidParameter("Partition must be non-empty: " + sourceTableArn);
}
if (!"dynamodb".equals(arn.service()) || !arn.partition().equals(AwsRegions.partitionFor(region))) {
throw invalidParameter("Invalid resource Arn: " + sourceTableArn);
}
if (!arn.resource().startsWith("table/")) {
@@ -2981,11 +2985,21 @@ public class DynamoDbService {
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) {
requireSseOverrideAcrossRegions(arn.region(), region, sseOverride);
return new DynamoDbTableNames.ResolvedTableRef(tableName, arn.region());
}
static void requireSseOverrideAcrossRegions(String sourceRegion, String region, boolean sseOverride) {
if (!sourceRegion.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());
}
static void requireSourceInPartition(String sourceRegion, String region) {
if (!AwsRegions.partitionFor(sourceRegion).equals(AwsRegions.partitionFor(region))) {
throw new AwsException("ValidationException", "Invalid Request: Table ARN is invalid.", 400);
}
}
private static AwsException invalidParameter(String message) {
@@ -5242,6 +5256,15 @@ public class DynamoDbService {
"Backup not found: " + backupArn, 400));
}
/** AWS fails with an internal error for a backup of another partition, after it checks the target. */
public TableBackup restoreSourceBackup(DynamoDbBackupArns.BackupArn arn, String targetTableName, String region) {
if (arn.isValid() && !arn.partition().equals(AwsRegions.partitionFor(region))) {
requireRestoreTarget(targetTableName, region);
throw new AwsException("InternalServerError", "Internal server error", 500);
}
return describeBackup(arn.arn(), arn.region());
}
public TableBackup deleteBackup(String backupArn, String region) {
TableBackup backup = describeBackup(backupArn, region);
if ("SYSTEM".equals(backup.getBackupType())) {
@@ -2015,11 +2015,13 @@ public class NativeDynamoDbJsonHandler {
*/
private Response handleRestoreTableFromBackup(JsonNode request, String region) {
String backupArn = textOrNull(request, "BackupArn");
dynamoDbService.requireOwnBackupArn(backupArn);
DynamoDbBackupArns.BackupArn arn = dynamoDbService.requireOwnBackupArn(backupArn);
String targetTableName = requireTableNameParameter(request, "TargetTableName");
throwValidationErrors(restoreViolations(request, targetTableName));
DynamoDbService.requireSseOverrideAcrossRegions(arn.region(), region,
request.path("SSESpecificationOverride").isObject());
TableBackup backup = dynamoDbService.describeBackup(backupArn, region);
TableBackup backup = dynamoDbService.restoreSourceBackup(arn, targetTableName, region);
TableDefinition table = tableService.restoreTable(DynamoDbService.snapshotOf(backup.getTable()),
targetTableName, parseRestoreOverrides(request), region);
ObjectNode description = tableToNode(table);
@@ -2063,6 +2065,7 @@ public class NativeDynamoDbJsonHandler {
throw new AwsException("ValidationException",
"Invalid Request: Must provide exactly one of: sourceTableArn, sourceTableName", 400);
}
DynamoDbService.requireSourceInPartition(source.region(), region);
boolean useLatestRestorableTime = request.path("UseLatestRestorableTime").asBoolean(false);
Instant restoreDateTime = timestampOrNull(request, "RestoreDateTime");
if (useLatestRestorableTime && restoreDateTime != null) {