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

@@ -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() : "")
@@ -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<String, String> 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);
}
@@ -32,6 +32,7 @@ public class DbSnapshot {
private String dbName;
private String dbInstanceClass;
private String optionGroupName;
private Boolean storageEncrypted;
private String kmsKeyId;
private Map<String, String> 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; }
}