mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 02:07:58 -05:00
feat(rds): honor DeletionProtection on cluster restores and AWS::RDS::DBCluster (#9057)
RestoreDBClusterFromSnapshot and RestoreDBClusterToPointInTime now read DeletionProtection and set it on the new cluster before it is stored. The AWS::RDS::DBCluster provisioner reads DeletionProtection, creates the cluster with it, and applies a changed value on stack update. A stack delete of a protected cluster is refused by DeleteDBCluster and reported as the resource's DELETE_FAILED, as on AWS. A restore, or a CloudFormation role attach, that fails after creating the cluster goes through one cleanup that clears the flag and always attempts the delete, adding any failure to the original error. Closes #9056
This commit is contained in:
1 parent
3e1fe3c8b6
commit
3f51fdec34
10 files changed
+218
-82
No files matched your search
+6
-3
@@ -655,6 +655,7 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
|
||||
// Refused before a new cluster exists, rather than leaving one behind.
|
||||
RdsService.requireDataApiEngine(resolveOptional(props, "Engine", engine));
|
||||
}
|
||||
boolean deletionProtection = parseStrictBoolProp(props, "DeletionProtection", engine);
|
||||
boolean created = cluster == null;
|
||||
if (cluster != null) {
|
||||
cluster = rdsService.modifyDbCluster(
|
||||
@@ -680,7 +681,7 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
|
||||
cluster = rdsService.createDbCluster(id, engineName, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, parameterGroup, null, null, false, region,
|
||||
serverlessV2MinCapacity, serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause,
|
||||
false, null, engineMode, storageEncrypted);
|
||||
false, null, engineMode, storageEncrypted, null, deletionProtection);
|
||||
}
|
||||
cluster = reconcileClusterRoles(r, cluster, roles, created, region);
|
||||
ASSOCIATED_ROLES.record(r, ctx, attributesBefore, associatedRolesRecord(roles));
|
||||
@@ -688,6 +689,9 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (httpEndpoint != cluster.isHttpEndpointEnabled()) {
|
||||
cluster = rdsService.setHttpEndpoint(region, cluster.getDbClusterIdentifier(), httpEndpoint);
|
||||
}
|
||||
if (deletionProtection != cluster.isDeletionProtection()) {
|
||||
cluster = rdsService.setDeletionProtection(region, cluster.getDbClusterIdentifier(), deletionProtection);
|
||||
}
|
||||
r.setPhysicalId(cluster.getDbClusterIdentifier());
|
||||
r.getAttributes().put("DBClusterIdentifier", cluster.getDbClusterIdentifier());
|
||||
if (cluster.getEndpoint() != null) {
|
||||
@@ -758,8 +762,7 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
|
||||
try {
|
||||
cluster = rdsService.addRolesToNewDbCluster(region, id, roles);
|
||||
} catch (AwsException e) {
|
||||
CfnDeletes.safeDelete("DB cluster", id, () -> rdsService.deleteDbCluster(id, region),
|
||||
"DBClusterNotFoundFault");
|
||||
rdsService.discardCreatedCluster(region, id, cluster.isDeletionProtection(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3059,7 +3059,9 @@ public class RdsQueryHandler {
|
||||
optionalInt(params.getFirst("Port")), params.getFirst("DBSubnetGroupName"),
|
||||
params.getFirst("DBClusterParameterGroupName"),
|
||||
parseOptionalBoolean(params, "EnableIAMDatabaseAuthentication"),
|
||||
params.getFirst("EngineMode"), parseTags(params), region);
|
||||
params.getFirst("EngineMode"),
|
||||
Boolean.TRUE.equals(parseOptionalBoolean(params, "DeletionProtection")),
|
||||
parseTags(params), region);
|
||||
return Response.ok(AwsQueryResponse.envelope("RestoreDBClusterToPointInTime", AwsNamespaces.RDS,
|
||||
dbClusterXml(cluster))).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -3084,7 +3086,9 @@ public class RdsQueryHandler {
|
||||
params.getFirst("DBClusterParameterGroupName"),
|
||||
params.getFirst("AvailabilityZones.member.1"),
|
||||
parseOptionalBoolean(params, "EnableIAMDatabaseAuthentication"),
|
||||
params.getFirst("EngineMode"), parseTags(params), region);
|
||||
params.getFirst("EngineMode"),
|
||||
Boolean.TRUE.equals(parseOptionalBoolean(params, "DeletionProtection")),
|
||||
parseTags(params), region);
|
||||
return Response.ok(AwsQueryResponse.envelope("RestoreDBClusterFromSnapshot", AwsNamespaces.RDS,
|
||||
dbClusterXml(cluster))).build();
|
||||
}
|
||||
|
||||
@@ -2772,7 +2772,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
String engineVersion, Integer port, String databaseName,
|
||||
String dbSubnetGroupName, String parameterGroupName,
|
||||
String availabilityZone, Boolean iamEnabled, String engineMode,
|
||||
Map<String, String> tags, String region) {
|
||||
boolean deletionProtection, Map<String, String> tags,
|
||||
String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
// SnapshotIdentifier takes the snapshot's name or its ARN (API reference).
|
||||
@@ -2798,7 +2799,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, false);
|
||||
snapshot.isStorageEncrypted(), port, deletionProtection);
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
cluster.getTags().putAll(tags);
|
||||
putClusterForScope(accountId, effectiveRegion, clusterId, cluster);
|
||||
@@ -2807,11 +2808,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
try {
|
||||
containerManager.restorePostgresSnapshot(cluster.getContainerId(), cluster.getMasterUsername(), sqlDump);
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
deleteDbCluster(clusterId, effectiveRegion);
|
||||
} catch (Exception cleanupError) {
|
||||
e.addSuppressed(cleanupError);
|
||||
}
|
||||
discardCreatedCluster(effectiveRegion, clusterId, deletionProtection, e);
|
||||
AwsException awsEx = new AwsException("InvalidDBClusterSnapshotStateFault",
|
||||
"Failed to restore snapshot: " + e.getMessage(), 400);
|
||||
awsEx.initCause(e);
|
||||
@@ -2832,7 +2829,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
String sourceClusterResourceId, String restoreType,
|
||||
Instant restoreToTime, Boolean useLatestRestorableTime,
|
||||
Integer port, String dbSubnetGroupName, String parameterGroupName,
|
||||
Boolean iamEnabled, String engineMode,
|
||||
Boolean iamEnabled, String engineMode, boolean deletionProtection,
|
||||
Map<String, String> tags, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
@@ -2897,7 +2894,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, false);
|
||||
source.isStorageEncrypted(), port, deletionProtection);
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
cluster.getTags().putAll(tags);
|
||||
putClusterForScope(accountId, effectiveRegion, clusterId, cluster);
|
||||
@@ -2916,11 +2913,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
containerManager.restorePostgresSnapshot(
|
||||
cluster.getContainerId(), cluster.getMasterUsername(), sqlDump);
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
deleteDbCluster(clusterId, effectiveRegion);
|
||||
} catch (Exception cleanupError) {
|
||||
e.addSuppressed(cleanupError);
|
||||
}
|
||||
discardCreatedCluster(effectiveRegion, clusterId, deletionProtection, e);
|
||||
AwsException failure = new AwsException("InvalidDBClusterStateFault",
|
||||
"Failed to restore DB cluster " + sourceId + " to " + clusterId + ": "
|
||||
+ e.getMessage(), 400);
|
||||
@@ -2932,6 +2925,21 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
return cluster;
|
||||
}
|
||||
|
||||
public void discardCreatedCluster(String region, String clusterId, boolean deletionProtection, Exception cause) {
|
||||
if (deletionProtection) {
|
||||
try {
|
||||
setDeletionProtection(region, clusterId, false);
|
||||
} catch (Exception unprotectError) {
|
||||
cause.addSuppressed(unprotectError);
|
||||
}
|
||||
}
|
||||
try {
|
||||
deleteDbCluster(clusterId, region);
|
||||
} catch (Exception deleteError) {
|
||||
cause.addSuppressed(deleteError);
|
||||
}
|
||||
}
|
||||
|
||||
private DbClusterSnapshot clusterSnapshotOf(DbCluster cluster, String snapshotId, String region) {
|
||||
DbClusterSnapshot snapshot = new DbClusterSnapshot();
|
||||
snapshot.setDbClusterSnapshotIdentifier(snapshotId);
|
||||
|
||||
Reference in new issue
Block a user