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
This commit is contained in:
XuJian authored and GitHub committed 2026-09-25 14:50:59 -04:00
1 parent a33c4e0b00
commit 0be4d07111
6 files changed
+163 -9

No files matched your search

@@ -1132,6 +1132,25 @@ class RdsQueryHandlerTest {
assertTrue(body.contains("<DBInstanceIdentifier>mydb</DBInstanceIdentifier>"));
assertTrue(body.contains("<SnapshotType>manual</SnapshotType>"));
assertTrue(body.contains("<SourceDBSnapshotIdentifier>arn:aws:rds:us-east-1:123456789012:snapshot:source</SourceDBSnapshotIdentifier>"));
assertTrue(body.contains("<Encrypted>false</Encrypted>"));
}
@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<String, String> 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("<Encrypted>true</Encrypted>"), body);
assertFalse(body.contains("<KmsKeyId>"), 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("<CopyDBSnapshotResult>"));
assertTrue(body.contains("<SnapshotType>manual</SnapshotType>"));
assertTrue(body.contains("<SourceDBSnapshotIdentifier>arn:aws:rds:us-east-1:123456789012:snapshot:source</SourceDBSnapshotIdentifier>"));
assertTrue(body.contains("<Encrypted>true</Encrypted>"));
verify(service).copyDbSnapshot("source", "copy", true,
Map.of("owner", "platform"), "custom-options", "kms-key", null);
}
@@ -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() {
@@ -105,8 +105,7 @@ class RdsSnapshotLifecycleIntegrationTest {
.body(containsString("<DBSnapshotIdentifier>" + copy + "</DBSnapshotIdentifier>"))
.body(containsString("<Status>available</Status>"))
.body(containsString("<SnapshotType>manual</SnapshotType>"))
// 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("<SourceDBSnapshotIdentifier>")))
.body(containsString("<Value>platform</Value>"))
.body(containsString("<Value>test</Value>"));