Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(rds): preserve snapshot partitions in custom regions (#5428)

David Phillipsad1fbbc9b93f
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
index 2eedccc686..04b4cfcc5b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
@@ -1080,9 +1080,10 @@ public class RdsService implements Resettable, ResourceProvider {
if (sourceIdentifier != null && sourceIdentifier.startsWith("arn:")) {
try {
AwsArnUtils.Arn parsed = AwsArnUtils.parse(sourceIdentifier);
- // A snapshot copies within its partition only, so a source ARN naming another
- // partition than the target region's is not a snapshot this copy can read.
- if (!AwsRegions.partitionFor(targetRegion).equals(parsed.partition())
+ // Both regions must belong to the ARN's partition; changing its prefix must
+ // not make a snapshot from another partition eligible for copying.
+ if (!regionResolver.partitionForRegion(targetRegion).equals(parsed.partition())
+ || !regionResolver.partitionForRegion(parsed.region()).equals(parsed.partition())
|| !"rds".equals(parsed.service())
|| !parsed.resource().startsWith("snapshot:")) {
throw new IllegalArgumentException("not an RDS snapshot ARN");
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
index 0c8061b754..c2031581f0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
@@ -4054,6 +4054,75 @@ class RdsServiceTest {
assertEquals("InvalidParameterValue", otherPartition.getErrorCode());
}
+ @ParameterizedTest
+ @CsvSource({"cn-north-1,xx-nowhere-9,xx-nowhere-9,aws-cn",
+ "cn-north-1,cn-north-1,xx-nowhere-9,aws-cn",
+ "cn-north-1,xx-nowhere-9,cn-north-1,aws-cn",
+ "us-gov-west-1,xx-nowhere-9,xx-nowhere-9,aws-us-gov"})
+ void copyDbSnapshotUsesDeploymentPartitionForCustomRegions(
+ String deploymentRegion, String sourceRegion, String targetRegion, String partition) {
+ regionResolver = new RegionResolver(deploymentRegion, "123456789012");
+ rdsService = newService(containerManager, proxyManager,
+ new InMemoryStorage<>(), new InMemoryStorage<>(),
+ new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
+ rdsService.createDbInstance("source-db", "postgres", "13",
+ "admin", "password", "dbname", "db.t3.micro",
+ 20, false, null, null, null, null, false,
+ false, null, Map.of(), sourceRegion);
+ when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA");
+ DbSnapshot source = rdsService.createDbSnapshot("source", "source-db", Map.of(), sourceRegion);
+ assertEquals("arn:" + partition + ":rds:" + sourceRegion + ":123456789012:snapshot:source",
+ source.getDbSnapshotArn());
+
+ String foreignArn = source.getDbSnapshotArn().replace("arn:" + partition + ":", "arn:aws:");
+ AwsException foreignPartition = assertThrows(AwsException.class, () ->
+ rdsService.copyDbSnapshot(foreignArn, "copy", false, Map.of(), null, null, targetRegion));
+ assertEquals("InvalidParameterValue", foreignPartition.getErrorCode());
+
+ DbSnapshot copy = rdsService.copyDbSnapshot(source.getDbSnapshotArn(), "copy", false,
+ Map.of(), null, null, targetRegion);
+ assertEquals("arn:" + partition + ":rds:" + targetRegion + ":123456789012:snapshot:copy",
+ copy.getDbSnapshotArn());
+ assertEquals("available", copy.getStatus());
+ }
+
+ @ParameterizedTest
+ @CsvSource({"cn-north-1,us-east-1,xx-nowhere-9,aws-cn",
+ "cn-north-1,us-east-1,cn-north-1,aws-cn",
+ "us-gov-west-1,us-east-1,xx-nowhere-9,aws-us-gov",
+ "us-east-1,cn-north-1,xx-nowhere-9,aws"})
+ void copyDbSnapshotRejectsRelabeledForeignSourceArn(
+ String deploymentRegion, String sourceRegion, String targetRegion, String targetPartition) {
+ regionResolver = new RegionResolver(deploymentRegion, "123456789012");
+ rdsService = newService(containerManager, proxyManager,
+ new InMemoryStorage<>(), new InMemoryStorage<>(),
+ new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
+ rdsService.createDbInstance("foreign-db", "postgres", "13",
+ "admin", "password", "dbname", "db.t3.micro",
+ 20, false, null, null, null, null, false,
+ false, null, Map.of(), sourceRegion);
+ when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA");
+ DbSnapshot foreignSource = rdsService.createDbSnapshot("source", "foreign-db", Map.of(), sourceRegion);
+ String relabeledArn = "arn:" + targetPartition + ":rds:" + sourceRegion
+ + ":123456789012:snapshot:source";
+ assertNotEquals(foreignSource.getDbSnapshotArn(), relabeledArn);
+
+ AwsException foreignPartition = assertThrows(AwsException.class, () ->
+ rdsService.copyDbSnapshot(relabeledArn, "copy", false, Map.of(), null, null, targetRegion));
+ assertEquals("InvalidParameterValue", foreignPartition.getErrorCode());
+
+ rdsService.createDbInstance("local-db", "postgres", "13",
+ "admin", "password", "dbname", "db.t3.micro",
+ 20, false, null, null, null, null, false,
+ false, null, Map.of(), targetRegion);
+ DbSnapshot localSource = rdsService.createDbSnapshot("local-source", "local-db", Map.of(), targetRegion);
+ DbSnapshot copy = rdsService.copyDbSnapshot(localSource.getDbSnapshotArn(), "copy", false,
+ Map.of(), null, null, targetRegion);
+ assertEquals("arn:" + targetPartition + ":rds:" + targetRegion + ":123456789012:snapshot:copy",
+ copy.getDbSnapshotArn());
+ assertEquals("available", copy.getStatus());
+ }
+
@Test
void copyDbSnapshotRejectsDuplicateTargetsAndUnavailableSources() {
rdsService.createDbInstance("mydb", "postgres", "13",