mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(rds): persist DB cluster DeletionProtection and refuse to delete a protected cluster (#8985)
This commit is contained in:
1 parent
fca7dfdd22
commit
30503c4c2e
14 files changed
+346
-77
No files matched your search
@@ -1450,6 +1450,7 @@ public class RdsQueryHandler {
|
||||
List<DbRoleAssociation> associatedRoles = parseAssociatedRoles(params);
|
||||
service.validateClusterRoleAssociations(id, engine, associatedRoles);
|
||||
boolean enableHttpEndpoint = Boolean.TRUE.equals(parseOptionalBoolean(params, "EnableHttpEndpoint"));
|
||||
Boolean deletionProtection = parseOptionalBoolean(params, "DeletionProtection");
|
||||
String globalCluster = params.getFirst("GlobalClusterIdentifier");
|
||||
if (enableHttpEndpoint && (globalCluster == null || globalCluster.isBlank())) {
|
||||
// A global cluster's members take its Aurora engine; any other create names its own.
|
||||
@@ -1461,33 +1462,22 @@ public class RdsQueryHandler {
|
||||
params, "ServerlessV2ScalingConfiguration.SecondsUntilAutoPause");
|
||||
Integer requestedPort = parseIntegerParam(params, "Port");
|
||||
String globalClusterIdentifier = params.getFirst("GlobalClusterIdentifier");
|
||||
boolean protectedCluster = Boolean.TRUE.equals(deletionProtection);
|
||||
DbCluster cluster;
|
||||
if (globalClusterIdentifier != null && !globalClusterIdentifier.isBlank()) {
|
||||
cluster = requestedPort == null
|
||||
? service.createDbClusterInGlobalCluster(globalClusterIdentifier, id, engine,
|
||||
params.getFirst("EngineVersion"), masterUsername, masterPassword, databaseName,
|
||||
iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted)
|
||||
: service.createDbClusterInGlobalCluster(globalClusterIdentifier, id, engine,
|
||||
params.getFirst("EngineVersion"), masterUsername, masterPassword, databaseName,
|
||||
iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted,
|
||||
requestedPort);
|
||||
cluster = service.createDbClusterInGlobalCluster(globalClusterIdentifier, id, engine,
|
||||
params.getFirst("EngineVersion"), masterUsername, masterPassword, databaseName,
|
||||
iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted,
|
||||
requestedPort, protectedCluster);
|
||||
} else {
|
||||
cluster = requestedPort == null
|
||||
? service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted)
|
||||
: service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted,
|
||||
requestedPort);
|
||||
cluster = service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted,
|
||||
requestedPort, protectedCluster);
|
||||
}
|
||||
Map<String, String> tags = parseTags(params);
|
||||
String kmsKeyId = params.getFirst("KmsKeyId");
|
||||
@@ -1503,6 +1493,9 @@ public class RdsQueryHandler {
|
||||
try {
|
||||
cluster = service.addRolesToNewDbCluster(region, id, associatedRoles);
|
||||
} catch (AwsException e) {
|
||||
if (protectedCluster) {
|
||||
service.setDeletionProtection(region, id, false);
|
||||
}
|
||||
service.deleteDbCluster(id, region);
|
||||
throw e;
|
||||
}
|
||||
@@ -1632,6 +1625,7 @@ public class RdsQueryHandler {
|
||||
if (modifyBackupRetentionPeriod != null) {
|
||||
RdsService.validateBackupRetentionPeriod(modifyBackupRetentionPeriod);
|
||||
}
|
||||
Boolean deletionProtection = parseOptionalBoolean(params, "DeletionProtection");
|
||||
if (Boolean.TRUE.equals(enableHttpEndpoint)) {
|
||||
RdsService.requireDataApiEngine(service.getDbCluster(id, region).getEngineIdentifier());
|
||||
}
|
||||
@@ -1650,6 +1644,9 @@ public class RdsQueryHandler {
|
||||
if (copyTagsToSnapshot != null || exportChanges != null) {
|
||||
cluster = service.setClusterSnapshotAndLogSettings(region, id, copyTagsToSnapshot, null, exportChanges);
|
||||
}
|
||||
if (deletionProtection != null) {
|
||||
cluster = service.setDeletionProtection(region, id, deletionProtection);
|
||||
}
|
||||
String result = dbClusterXml(cluster);
|
||||
return Response.ok(AwsQueryResponse.envelope("ModifyDBCluster", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -3691,6 +3688,7 @@ public class RdsQueryHandler {
|
||||
.elem("MultiAZ", c.isMultiAz())
|
||||
.elem("StorageEncrypted", c.isStorageEncrypted())
|
||||
.elem("BackupRetentionPeriod", c.getBackupRetentionPeriod())
|
||||
.elem("DeletionProtection", c.isDeletionProtection())
|
||||
.elem("CopyTagsToSnapshot", c.isCopyTagsToSnapshot())
|
||||
.elem("KmsKeyId", c.getKmsKeyId())
|
||||
.elem("AvailabilityZone", c.getAvailabilityZone() != null ? c.getAvailabilityZone() : config.defaultAvailabilityZone())
|
||||
|
||||
@@ -2798,7 +2798,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
parameterGroupName, dbSubnetGroupName, availabilityZone, false, effectiveRegion,
|
||||
null, null, null, false, null,
|
||||
engineMode != null && !engineMode.isBlank() ? engineMode : snapshot.getEngineMode(),
|
||||
snapshot.isStorageEncrypted(), port);
|
||||
snapshot.isStorageEncrypted(), port, false);
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
cluster.getTags().putAll(tags);
|
||||
putClusterForScope(accountId, effectiveRegion, clusterId, cluster);
|
||||
@@ -2897,7 +2897,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
parameterGroupName, dbSubnetGroupName, null, false, effectiveRegion,
|
||||
null, null, null, false, null,
|
||||
!isBlank(engineMode) ? engineMode : source.getEngineMode(),
|
||||
source.isStorageEncrypted(), port);
|
||||
source.isStorageEncrypted(), port, false);
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
cluster.getTags().putAll(tags);
|
||||
putClusterForScope(accountId, effectiveRegion, clusterId, cluster);
|
||||
@@ -5769,11 +5769,28 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort) {
|
||||
return createDbCluster(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone,
|
||||
multiAz, region, serverlessV2MinCapacity, serverlessV2MaxCapacity,
|
||||
serverlessV2SecondsUntilAutoPause, manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
engineMode, storageEncrypted, requestedPort, false);
|
||||
}
|
||||
|
||||
public DbCluster createDbCluster(String id, String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String databaseName, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String availabilityZone, boolean multiAz, String region,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort,
|
||||
boolean deletionProtection) {
|
||||
return createDbClusterWithPort(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone,
|
||||
multiAz, region, serverlessV2MinCapacity, serverlessV2MaxCapacity,
|
||||
serverlessV2SecondsUntilAutoPause, manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
engineMode, storageEncrypted, requestedPort);
|
||||
engineMode, storageEncrypted, requestedPort, deletionProtection);
|
||||
}
|
||||
|
||||
private DbCluster createDbClusterWithPort(String id, String engineParam, String engineVersion,
|
||||
@@ -5784,7 +5801,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort) {
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort,
|
||||
boolean deletionProtection) {
|
||||
String provisioningKey = "cluster:" + currentAccountId() + ":"
|
||||
+ dbResourceKey(effectiveRegion(region), id);
|
||||
if (!provisioningIds.add(provisioningKey)) {
|
||||
@@ -5796,7 +5814,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone,
|
||||
multiAz, region, serverlessV2MinCapacity, serverlessV2MaxCapacity,
|
||||
serverlessV2SecondsUntilAutoPause, manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
engineMode, storageEncrypted, requestedPort);
|
||||
engineMode, storageEncrypted, requestedPort, deletionProtection);
|
||||
} finally {
|
||||
provisioningIds.remove(provisioningKey);
|
||||
}
|
||||
@@ -5810,7 +5828,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort) {
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort,
|
||||
boolean deletionProtection) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String clusterResourceId = "cluster-" + UUID.randomUUID().toString()
|
||||
.replace("-", "").substring(0, 24).toUpperCase();
|
||||
@@ -5848,6 +5867,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
cluster.setEngineIdentifier(effectiveEngineName(engineParam).toLowerCase(Locale.ROOT));
|
||||
cluster.setEngineMode(engineMode != null && !engineMode.isBlank() ? engineMode : "provisioned");
|
||||
cluster.setStorageEncrypted(storageEncrypted);
|
||||
cluster.setDeletionProtection(deletionProtection);
|
||||
cluster.setContainerStorageResourceId(clusterResourceId);
|
||||
if (!mock) {
|
||||
String clusterVolumeId;
|
||||
@@ -6541,6 +6561,10 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
new AwsException("DBClusterNotFoundFault",
|
||||
"DB cluster " + id + " not found.", 404));
|
||||
|
||||
if (cluster.isDeletionProtection()) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Cannot delete protected Cluster, please disable deletion protection and try again.", 400);
|
||||
}
|
||||
if (!cluster.getDbClusterMembers().isEmpty()) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"DB cluster " + id + " still has DB instances.", 400);
|
||||
@@ -6704,7 +6728,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
masterUsername, masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region, serverlessV2MinCapacity,
|
||||
serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause, manageMasterUserPassword,
|
||||
masterUserSecretKmsKeyId, engineMode, storageEncrypted, null);
|
||||
masterUserSecretKmsKeyId, engineMode, storageEncrypted, null, false);
|
||||
}
|
||||
|
||||
public DbCluster createDbClusterInGlobalCluster(String globalClusterIdentifier, String id,
|
||||
@@ -6716,7 +6740,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort) {
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort,
|
||||
boolean deletionProtection) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
GlobalCluster global = requireGlobalCluster(accountId, globalClusterIdentifier);
|
||||
@@ -6737,7 +6762,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
paramGroupName, dbSubnetGroupName, availabilityZone, multiAz, effectiveRegion,
|
||||
serverlessV2MinCapacity, serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode,
|
||||
storageEncrypted || global.isStorageEncrypted(), requestedPort);
|
||||
storageEncrypted || global.isStorageEncrypted(), requestedPort, deletionProtection);
|
||||
} else {
|
||||
if (hasText(masterUsername) || hasText(masterPassword) || manageMasterUserPassword) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
@@ -6772,7 +6797,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
primary.getDatabaseName(), iamEnabled, paramGroupName, dbSubnetGroupName,
|
||||
availabilityZone, multiAz, effectiveRegion, serverlessV2MinCapacity,
|
||||
serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause, false, null,
|
||||
engineMode, primary.isStorageEncrypted(), requestedPort);
|
||||
engineMode, primary.isStorageEncrypted(), requestedPort, deletionProtection);
|
||||
}
|
||||
try {
|
||||
attachToGlobalCluster(accountId, global.getGlobalClusterIdentifier(), cluster, primary == null);
|
||||
@@ -6785,6 +6810,9 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
if (deletionProtection) {
|
||||
setDeletionProtection(effectiveRegion, id, false);
|
||||
}
|
||||
deleteDbCluster(id, effectiveRegion);
|
||||
} catch (RuntimeException cleanupError) {
|
||||
e.addSuppressed(cleanupError);
|
||||
@@ -11999,6 +12027,14 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
return RESPONSE_COPIER.convertValue(cluster, DbCluster.class);
|
||||
}
|
||||
|
||||
public synchronized DbCluster setDeletionProtection(String region, String clusterId, boolean deletionProtection) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
DbCluster cluster = getDbCluster(clusterId, effectiveRegion);
|
||||
cluster.setDeletionProtection(deletionProtection);
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, cluster.getDbClusterIdentifier(), cluster);
|
||||
return RESPONSE_COPIER.convertValue(cluster, DbCluster.class);
|
||||
}
|
||||
|
||||
public synchronized DbCluster setClusterSnapshotAndLogSettings(String region, String clusterId,
|
||||
Boolean copyTagsToSnapshot, List<String> exports, LogExportChanges changes) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
|
||||
@@ -48,6 +48,7 @@ public class DbCluster {
|
||||
private String engineMode;
|
||||
private boolean storageEncrypted;
|
||||
private int backupRetentionPeriod = 1;
|
||||
private boolean deletionProtection;
|
||||
private boolean copyTagsToSnapshot;
|
||||
private List<String> enabledCloudwatchLogsExports = new ArrayList<>();
|
||||
private String kmsKeyId;
|
||||
@@ -212,6 +213,9 @@ public class DbCluster {
|
||||
public int getBackupRetentionPeriod() { return backupRetentionPeriod; }
|
||||
public void setBackupRetentionPeriod(int backupRetentionPeriod) { this.backupRetentionPeriod = backupRetentionPeriod; }
|
||||
|
||||
public boolean isDeletionProtection() { return deletionProtection; }
|
||||
public void setDeletionProtection(boolean deletionProtection) { this.deletionProtection = deletionProtection; }
|
||||
|
||||
public String getKmsKeyId() { return kmsKeyId; }
|
||||
public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user