mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(rds): preserve snapshot partitions in custom regions (#5428)
* fix(rds): preserve snapshot partitions in custom regions * fix(rds): reject relabeled cross-partition snapshot sources
This commit is contained in:
1 parent
fe51d2996d
commit
ad1fbbc9b9
2 files changed
+73
-3
No files matched your search
@@ -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");
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user