From 0be4d071113c65e2e3959d374d018f29d8ca2eb2 Mon Sep 17 00:00:00 2001 From: XuJian <265865031+xujiantop-crypto@users.noreply.github.com> Date: Sat, 26 Sep 2026 03:50:59 +0900 Subject: [PATCH] fix(rds): align snapshot copy and encryption responses (#4355) * fix(rds): align snapshot copy and encryption responses * fix(rds): clean failed restores in target regionfix(rds): clean failed restores in target region * fix(rds): allow cross-region snapshot copies by ARN * fix(rds): require target KMS key for encrypted cross-region copies --- .../floci/services/rds/RdsQueryHandler.java | 1 + .../floci/services/rds/RdsService.java | 24 +++- .../floci/services/rds/model/DbSnapshot.java | 9 ++ .../services/rds/RdsQueryHandlerTest.java | 22 ++++ .../floci/services/rds/RdsServiceTest.java | 113 +++++++++++++++++- .../RdsSnapshotLifecycleIntegrationTest.java | 3 +- 6 files changed, 163 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java index 742c0bd297..f1ad268625 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java @@ -2238,6 +2238,7 @@ public class RdsQueryHandler { if (s.getKmsKeyId() != null && !s.getKmsKeyId().isBlank()) { xml.elem("KmsKeyId", s.getKmsKeyId()); } + xml.elem("Encrypted", s.isStorageEncrypted()); if (s.getAvailabilityZone() != null) xml.elem("AvailabilityZone", s.getAvailabilityZone()); if (s.getVpcId() != null) xml.elem("VpcId", s.getVpcId()); xml.elem("InstanceCreateTime", s.getInstanceCreateTime() != null ? s.getInstanceCreateTime().toString() : "") 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 f148c0a997..18a49cec33 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 @@ -842,6 +842,7 @@ public class RdsService implements Resettable, ResourceProvider { snapshot.setDbName(instance.getDbName()); snapshot.setSnapshotType("manual"); snapshot.setOptionGroupName(instance.getOptionGroupName()); + snapshot.setStorageEncrypted(instance.isStorageEncrypted()); snapshot.setKmsKeyId(instance.getKmsKeyId()); snapshot.setTags(tags != null ? new java.util.LinkedHashMap<>(tags) : new java.util.LinkedHashMap<>()); snapshot.setDbSnapshotArn(regionResolver.buildArn("rds", effectiveRegion, "snapshot:" + snapshotId)); @@ -897,14 +898,22 @@ public class RdsService implements Resettable, ResourceProvider { String accountId = currentAccountId(); SnapshotReference sourceReference = resolveSnapshotReference(sourceIdentifier, targetRegion); DbSnapshot source = sourceReference.snapshot(); + boolean crossRegion = !Objects.equals(sourceReference.region(), targetRegion); if (!"available".equalsIgnoreCase(source.getStatus())) { throw new AwsException("InvalidDBSnapshotState", "DBSnapshot " + source.getDbSnapshotIdentifier() + " is not in an available state.", 400); } + if (crossRegion && source.isStorageEncrypted() + && (kmsKeyId == null || kmsKeyId.isBlank())) { + throw new AwsException("InvalidParameterCombination", + "KmsKeyId is required when copying an encrypted DBSnapshot across Regions.", 400); + } if (findSnapshotForScope(accountId, targetRegion, targetIdentifier) != null) { throw new AwsException("DBSnapshotAlreadyExists", "DBSnapshot " + targetIdentifier + " already exists.", 400); } + String targetKmsKeyId = crossRegion && kmsKeyId != null && !kmsKeyId.isBlank() + ? resolveKmsKeyArn(kmsKeyId, targetRegion) : kmsKeyId; String sourceData = getSnapshotDataForScope( sourceReference.accountId(), sourceReference.region(), source.getDbSnapshotIdentifier()) .orElseThrow(() -> new AwsException("DBSnapshotNotFound", @@ -916,10 +925,13 @@ public class RdsService implements Resettable, ResourceProvider { copy.setSnapshotCreateTime(Instant.now()); copy.setStatus("available"); copy.setSnapshotType("manual"); - copy.setSourceDbSnapshotIdentifier(source.getDbSnapshotArn()); + copy.setSourceDbSnapshotIdentifier(crossRegion ? source.getDbSnapshotArn() : null); copy.setOptionGroupName(optionGroupName != null && !optionGroupName.isBlank() ? optionGroupName : source.getOptionGroupName()); - copy.setKmsKeyId(kmsKeyId != null && !kmsKeyId.isBlank() ? kmsKeyId : source.getKmsKeyId()); + copy.setStorageEncrypted(source.isStorageEncrypted() + || (targetKmsKeyId != null && !targetKmsKeyId.isBlank())); + copy.setKmsKeyId(targetKmsKeyId != null && !targetKmsKeyId.isBlank() + ? targetKmsKeyId : source.getKmsKeyId()); copy.setRestoreAccountIds(new ArrayList<>()); Map copiedTags = new LinkedHashMap<>(); if (copyTags) { @@ -989,6 +1001,7 @@ public class RdsService implements Resettable, ResourceProvider { copy.setDbName(source.getDbName()); copy.setDbInstanceClass(source.getDbInstanceClass()); copy.setOptionGroupName(source.getOptionGroupName()); + copy.setStorageEncrypted(source.isStorageEncrypted()); copy.setKmsKeyId(source.getKmsKeyId()); copy.setTags(new LinkedHashMap<>(source.getTags())); copy.setRestoreAccountIds(new ArrayList<>(source.getRestoreAccountIds())); @@ -1075,17 +1088,20 @@ public class RdsService implements Resettable, ResourceProvider { targetClass = "db.t3.micro"; } // Use the parameters from the snapshot + DbInstanceSettings restoreSettings = new DbInstanceSettings( + snapshot.isStorageEncrypted(), snapshot.getKmsKeyId(), null, null, null, null); DbInstance instance = createDbInstance(instanceId, snapshot.getEngine().name().toLowerCase(), snapshot.getEngineVersion(), snapshot.getMasterUsername(), snapshot.getMasterPassword(), snapshot.getDbName(), targetClass, snapshot.getAllocatedStorage(), snapshot.isIamDatabaseAuthenticationEnabled(), - null, dbSubnetGroupName, null, availabilityZone, multiAz, false, null, tags, vpcSecurityGroupIds); + null, dbSubnetGroupName, null, availabilityZone, multiAz, false, null, tags, + vpcSecurityGroupIds, null, effectiveRegion, true, restoreSettings); if (!config.services().rds().mock()) { try { containerManager.restorePostgresSnapshot(instance.getContainerId(), instance.getMasterUsername(), sqlDump); } catch (Exception e) { try { - deleteDbInstance(instanceId); + deleteDbInstance(instanceId, effectiveRegion); } catch (Exception cleanupError) { e.addSuppressed(cleanupError); } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbSnapshot.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbSnapshot.java index 37d0163574..eef7a44375 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbSnapshot.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbSnapshot.java @@ -32,6 +32,7 @@ public class DbSnapshot { private String dbName; private String dbInstanceClass; private String optionGroupName; + private Boolean storageEncrypted; private String kmsKeyId; private Map tags = new LinkedHashMap<>(); /** Account IDs authorized to copy/restore this snapshot; the "restore" DBSnapshotAttribute. */ @@ -134,6 +135,14 @@ public class DbSnapshot { public String getOptionGroupName() { return optionGroupName; } public void setOptionGroupName(String optionGroupName) { this.optionGroupName = optionGroupName; } + public boolean isStorageEncrypted() { + if (storageEncrypted != null) { + return storageEncrypted; + } + return kmsKeyId != null && !kmsKeyId.isBlank(); + } + public void setStorageEncrypted(boolean storageEncrypted) { this.storageEncrypted = storageEncrypted; } + public String getKmsKeyId() { return kmsKeyId; } public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; } } diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java index 610dffde3b..2d637ea5fd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java @@ -1132,6 +1132,25 @@ class RdsQueryHandlerTest { assertTrue(body.contains("mydb")); assertTrue(body.contains("manual")); assertTrue(body.contains("arn:aws:rds:us-east-1:123456789012:snapshot:source")); + assertTrue(body.contains("false")); + } + + @Test + void describeDbSnapshotsReportsEncryptionWithoutExplicitKmsKey() { + DbSnapshot snapshot = new DbSnapshot(); + snapshot.setDbSnapshotIdentifier("encrypted-snapshot"); + snapshot.setStorageEncrypted(true); + when(service.describeDbSnapshots(eq("encrypted-snapshot"), isNull(), isNull())) + .thenReturn(List.of(snapshot)); + + MultivaluedMap p = params(); + p.add("DBSnapshotIdentifier", "encrypted-snapshot"); + Response response = handler.handle("DescribeDBSnapshots", p); + + assertEquals(200, response.getStatus()); + String body = (String) response.getEntity(); + assertTrue(body.contains("true"), body); + assertFalse(body.contains(""), body); } @Test @@ -1873,6 +1892,8 @@ class RdsQueryHandlerTest { snapshot.setSnapshotType("manual"); snapshot.setSourceDbSnapshotIdentifier( "arn:aws:rds:us-east-1:123456789012:snapshot:source"); + snapshot.setStorageEncrypted(true); + snapshot.setKmsKeyId("kms-key"); when(service.copyDbSnapshot(eq("source"), eq("copy"), eq(true), eq(Map.of("owner", "platform")), eq("custom-options"), eq("kms-key"), isNull())) .thenReturn(snapshot); @@ -1892,6 +1913,7 @@ class RdsQueryHandlerTest { assertTrue(body.contains("")); assertTrue(body.contains("manual")); assertTrue(body.contains("arn:aws:rds:us-east-1:123456789012:snapshot:source")); + assertTrue(body.contains("true")); verify(service).copyDbSnapshot("source", "copy", true, Map.of("owner", "platform"), "custom-options", "kms-key", null); } 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 6af033a7ab..b7861114dc 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 @@ -3293,6 +3293,32 @@ class RdsServiceTest { verify(containerManager).createPostgresSnapshot(any(), eq("admin")); } + @Test + void createCopyAndRestoreDbSnapshotPreserveEncryptionWithoutExplicitKmsKey() { + DbInstance sourceInstance = rdsService.createDbInstance("mydb", "postgres", "13", + "admin", "password", "dbname", "db.t3.micro", + 20, false, null, null, null, null, false, false, null, + Map.of(), List.of(), null, null, true, + new DbInstanceSettings(true, null, null, null, null, null)); + when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA"); + + DbSnapshot snapshot = rdsService.createDbSnapshot("mysnap", "mydb"); + DbSnapshot copy = rdsService.copyDbSnapshot( + "mysnap", "copy", false, Map.of(), null, null, "us-east-1"); + DbInstance restored = rdsService.restoreDbInstanceFromDbSnapshot( + "restored", "copy", "db.t3.micro", null, false, + null, null, Map.of(), "us-east-1"); + + assertTrue(sourceInstance.isStorageEncrypted()); + assertNull(sourceInstance.getKmsKeyId()); + assertTrue(snapshot.isStorageEncrypted()); + assertNull(snapshot.getKmsKeyId()); + assertTrue(copy.isStorageEncrypted()); + assertNull(copy.getKmsKeyId()); + assertTrue(restored.isStorageEncrypted()); + assertNull(restored.getKmsKeyId()); + } + @Test void deleteDbSnapshotReturnsDeletedSnapshotAndRemovesItsData() { rdsService.createDbInstance("mydb", "postgres", "13", @@ -3313,7 +3339,7 @@ class RdsServiceTest { } @Test - void copyDbSnapshotCopiesDataTagsAndSourceArn() { + void copyDbSnapshotCopiesDataAndTagsWithoutSameRegionSourceArn() { rdsService.createDbInstance("mydb", "postgres", "13", "admin", "password", "dbname", "db.t3.micro", 20, false, null, null, null, null, false); @@ -3328,15 +3354,18 @@ class RdsServiceTest { assertEquals("copy", copy.getDbSnapshotIdentifier()); assertEquals("manual", copy.getSnapshotType()); - assertEquals(source.getDbSnapshotArn(), copy.getSourceDbSnapshotIdentifier()); + assertNull(copy.getSourceDbSnapshotIdentifier()); assertEquals(Map.of("owner", "platform", "Name", "copy"), copy.getTags()); assertEquals("custom-options", copy.getOptionGroupName()); assertEquals("kms-key", copy.getKmsKeyId()); + assertTrue(copy.isStorageEncrypted()); assertTrue(copy.getRestoreAccountIds().isEmpty()); - rdsService.restoreDbInstanceFromDbSnapshot( + knownKey("kms-key"); + DbInstance restored = rdsService.restoreDbInstanceFromDbSnapshot( "restored", "copy", "db.t3.micro", null, false, null, null, Map.of(), "us-east-1"); + assertEquals(KEY_ARN, restored.getKmsKeyId()); verify(containerManager).restorePostgresSnapshot(any(), eq("admin"), eq("MOCK_DUMP_DATA")); } @@ -3361,6 +3390,58 @@ class RdsServiceTest { assertEquals("InvalidDBSnapshotState", unavailable.getErrorCode()); } + @Test + void copyDbSnapshotFromAnotherRegionNeedsOnlyTheSourceArn() { + rdsService.createDbInstance("source-db", "postgres", "13", + "admin", "password", "dbname", "db.t3.micro", + 20, false, null, null, null, null, false, + false, null, Map.of(), "us-west-2"); + when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA"); + DbSnapshot source = rdsService.createDbSnapshot( + "source", "source-db", Map.of(), "us-west-2"); + + DbSnapshot copy = rdsService.copyDbSnapshot( + source.getDbSnapshotArn(), "copy", false, Map.of(), + null, null, "us-east-1"); + assertEquals(source.getDbSnapshotArn(), copy.getSourceDbSnapshotIdentifier()); + } + + @Test + void copyEncryptedDbSnapshotAcrossRegionsRequiresDestinationKmsKey() { + KmsKey sourceKey = new KmsKey(); + sourceKey.setArn("arn:aws:kms:us-west-2:123456789012:key/source"); + sourceKey.setEnabled(true); + sourceKey.setKeyState("Enabled"); + doReturn(sourceKey).when(kmsService).describeKey("source-key", "us-west-2"); + rdsService.createDbInstance("source-db", "postgres", "13", + "admin", "password", "dbname", "db.t3.micro", + 20, false, null, null, null, null, false, false, null, + Map.of(), List.of(), null, "us-west-2", true, + new DbInstanceSettings(true, "source-key", null, null, null, null)); + when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA"); + DbSnapshot source = rdsService.createDbSnapshot("source", "source-db", Map.of(), "us-west-2"); + + AwsException missingKey = assertThrows(AwsException.class, () -> + rdsService.copyDbSnapshot(source.getDbSnapshotArn(), "copy", false, + Map.of(), null, null, "us-east-1")); + assertEquals("InvalidParameterCombination", missingKey.getErrorCode()); + + AwsException wrongRegionKey = assertThrows(AwsException.class, () -> + rdsService.copyDbSnapshot(source.getDbSnapshotArn(), "copy", false, + Map.of(), null, source.getKmsKeyId(), "us-east-1")); + assertEquals("KMSKeyNotAccessibleFault", wrongRegionKey.getErrorCode()); + + knownKey("destination-key", KEY_ARN); + DbSnapshot copy = rdsService.copyDbSnapshot(source.getDbSnapshotArn(), "copy", false, + Map.of(), null, "destination-key", "us-east-1"); + assertTrue(copy.isStorageEncrypted()); + assertEquals(KEY_ARN, copy.getKmsKeyId()); + DbInstance restored = rdsService.restoreDbInstanceFromDbSnapshot( + "restored", "copy", "db.t3.micro", null, false, + null, null, Map.of(), "us-east-1"); + assertEquals(KEY_ARN, restored.getKmsKeyId()); + } + @Test void modifyDbSnapshotUpdatesEngineVersionAndOptionGroup() { rdsService.createDbInstance("mydb", "postgres", "13", @@ -3583,6 +3664,32 @@ class RdsServiceTest { assertEquals("InvalidDBSnapshotState", exception.getErrorCode()); assertTrue(exception.getMessage().contains("Failed to restore snapshot: failed restore")); } + @Test + void restoreDbInstanceFromDbSnapshotCleansUpTheTargetRegionOnFailure() { + rdsService.createDbInstance("source", "postgres", "13", + "admin", "password", "dbname", "db.t3.micro", + 20, false, null, null, null, null, false, + false, null, Map.of(), "eu-west-1"); + rdsService.createDbInstance("restored-db", "postgres", "13", + "admin", "password", "defaultdb", "db.t3.micro", + 20, false, null, null, null, null, false); + when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA"); + rdsService.createDbSnapshot("mysnap", "source", Map.of(), "eu-west-1"); + doThrow(new RuntimeException("failed restore")) + .when(containerManager).restorePostgresSnapshot(any(), eq("admin"), eq("MOCK_DUMP_DATA")); + + AwsException exception = assertThrows(AwsException.class, () -> + rdsService.restoreDbInstanceFromDbSnapshot( + "restored-db", "mysnap", "db.t3.large", null, false, + null, null, Map.of(), "eu-west-1")); + + assertEquals("InvalidDBSnapshotState", exception.getErrorCode()); + assertEquals("defaultdb", rdsService.getDbInstance("restored-db").getDbName()); + AwsException missingTarget = assertThrows(AwsException.class, + () -> rdsService.getDbInstance("restored-db", "eu-west-1")); + assertEquals("DBInstanceNotFound", missingTarget.getErrorCode()); + } + @Test void describeDbSnapshotsFiltersCorrectly() { diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsSnapshotLifecycleIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsSnapshotLifecycleIntegrationTest.java index 1d10c25dcc..aea585f80f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsSnapshotLifecycleIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsSnapshotLifecycleIntegrationTest.java @@ -105,8 +105,7 @@ class RdsSnapshotLifecycleIntegrationTest { .body(containsString("" + copy + "")) .body(containsString("available")) .body(containsString("manual")) - // SourceDBSnapshotIdentifier is not asserted: the reference says it "only has a - // value in the case of a cross-account or cross-Region copy", which this is not. + .body(not(containsString(""))) .body(containsString("platform")) .body(containsString("test"));