fix(redshift): refuse deleting the default parameter group, and a parameter or subnet group a cluster uses (#9233)

This commit is contained in:
mrfelek authored and GitHub committed 2026-11-23 06:40:42 -04:00
1 parent 29e262e783
commit 272f3f8435
5 files changed
+262 -10

No files matched your search

@@ -115,6 +115,12 @@ public class RedshiftQueryHandler {
String region = regionResolver.resolveRegionFromAuth(authorizationHeader);
String defaultIamRoleArn = params.getFirst("DefaultIamRoleArn");
Integer automatedRetention = parseOptionalInteger(params, "AutomatedSnapshotRetentionPeriod");
String parameterGroupName = params.getFirst("ClusterParameterGroupName");
boolean hasParameterGroup = parameterGroupName != null && !parameterGroupName.isBlank();
if (hasParameterGroup) {
// Refused before the cluster is created, as Redshift refuses a CreateCluster naming no group.
service.describeClusterParameterGroups(parameterGroupName);
}
Cluster cluster;
if (manageMasterPassword) {
cluster = automatedRetention == null
@@ -131,12 +137,20 @@ public class RedshiftQueryHandler {
: service.createCluster(identifier, nodeType, masterUsername, masterUserPassword,
clusterSubnetGroupName, vpcSecurityGroupIds, iamRoleArns, defaultIamRoleArn, automatedRetention);
}
String parameterGroupName = params.getFirst("ClusterParameterGroupName");
String multiAz = params.getFirst("MultiAZ");
boolean hasParameterGroup = parameterGroupName != null && !parameterGroupName.isBlank();
if (hasParameterGroup || multiAz != null) {
cluster = service.modifyCluster(identifier, null, null, null, parameterGroupName, null,
multiAz == null ? null : Boolean.parseBoolean(multiAz));
try {
cluster = service.modifyCluster(identifier, null, null, null, parameterGroupName, null,
multiAz == null ? null : Boolean.parseBoolean(multiAz));
} catch (AwsException refused) {
// The group was deleted after the check above: the create fails as a whole, leaving no cluster.
try {
service.deleteCluster(identifier);
} catch (RuntimeException cleanupFailure) {
refused.addSuppressed(cleanupFailure);
}
throw refused;
}
}
String xml = new XmlBuilder()
.start("CreateClusterResponse")
@@ -1388,9 +1388,12 @@ public class RedshiftService implements ManagedSecretRotator {
rejectPausedCluster(cluster);
validateAutomatedRetention(nodeType == null ? cluster.getNodeType() : nodeType,
automatedRetention == null ? cluster.getAutomatedSnapshotRetentionPeriod() : automatedRetention);
boolean parameterGroupChanged = clusterParameterGroupName != null && !clusterParameterGroupName.isBlank();
if (parameterGroupChanged) {
requireParameterGroup(clusterParameterGroupName);
}
boolean passwordChanged = masterUserPassword != null && !masterUserPassword.isBlank();
boolean parameterGroupChanged = clusterParameterGroupName != null && !clusterParameterGroupName.isBlank();
boolean securityGroupsChanged = vpcSecurityGroupIds != null && !vpcSecurityGroupIds.isEmpty();
// alterUserPassword runs before any mutation of the cluster object (a live reference
// from HybridStorage, not a copy); if it throws, no metadata has been changed yet.
@@ -1410,7 +1413,7 @@ public class RedshiftService implements ManagedSecretRotator {
if (nodeType != null && !nodeType.isBlank()) {
cluster.setNodeType(nodeType);
}
if (clusterParameterGroupName != null && !clusterParameterGroupName.isBlank()) {
if (parameterGroupChanged) {
cluster.setClusterParameterGroupName(clusterParameterGroupName);
cluster.setUserActivityLoggingEnabled(userActivityLoggingEnabled(clusterParameterGroupName));
}
@@ -2740,7 +2743,16 @@ public class RedshiftService implements ManagedSecretRotator {
Cluster cluster = restoreSnapshot(clusterIdentifier, snapshotIdentifier, null, nodeType,
clusters.accountId());
try {
cluster.setClusterSubnetGroupName(clusterSubnetGroupName);
// Under the lock the subnet group's delete takes, so the group is either refused here or
// seen by the delete's in-use check once the cluster is stored with it.
mutate(() -> {
if (clusterSubnetGroupName != null && !clusterSubnetGroupName.isBlank()) {
requireSubnetGroup(clusterSubnetGroupName);
}
cluster.setClusterSubnetGroupName(clusterSubnetGroupName);
storeCluster(cluster);
return cluster;
});
if (manageMasterPassword) {
String password = RandomPasswordGenerator.generate(objectMapper.createObjectNode());
modifyCluster(clusterIdentifier, null, null, password, null, List.of());
@@ -3072,6 +3084,24 @@ public class RedshiftService implements ManagedSecretRotator {
}
}
/**
* Refuses a parameter group name this Region holds no group of, as Redshift refuses a cluster naming one; the
* default group, which Describe lists, is always there. Callers check under {@link #mutate}, the lock the
* group's delete takes, so a group deleted meanwhile is either refused here or kept by the delete's in-use check.
*/
private void requireParameterGroup(String name) {
if (!DEFAULT_PARAMETER_GROUP_NAME.equals(name) && parameterGroups.get(regionalKey(name)).isEmpty()) {
throw new AwsException("ClusterParameterGroupNotFound", "Cluster parameter group " + name + " not found", 404);
}
}
/** As {@link #requireParameterGroup}, for a subnet group. */
private void requireSubnetGroup(String name) {
if (subnetGroups.get(regionalKey(name)).isEmpty()) {
throw new AwsException("ClusterSubnetGroupNotFoundFault", "Cluster subnet group " + name + " not found", 400);
}
}
private void requireMutableParameterGroup(String name) {
if (name == null || name.isBlank()) {
throw new AwsException("InvalidParameterValue", "ParameterGroupName is required", 400);
@@ -3101,9 +3131,21 @@ public class RedshiftService implements ManagedSecretRotator {
// resetClusterParameterGroup hold from their read to their write, so a delete can't land in between and be
// undone by their write; the event is emitted after it.
ClusterParameterGroup group = mutate(() -> {
ClusterParameterGroup found = parameterGroups.get(regionalKey(parameterGroupName))
.orElseThrow(() -> new AwsException("ClusterParameterGroupNotFound",
"Cluster parameter group " + parameterGroupName + " not found", 404));
// As Redshift answers (measured on AWS): the default group, which is answered for without being stored,
// can't be deleted (a custom group an older Floci stored under its name stays deletable, as it stays
// modifiable), and a group a cluster of the Region still uses is refused, checked under the lock cluster
// changes hold too.
Optional<ClusterParameterGroup> stored = parameterGroups.get(regionalKey(parameterGroupName));
if (stored.isEmpty() && DEFAULT_PARAMETER_GROUP_NAME.equals(parameterGroupName)) {
throw new AwsException("InvalidParameterValue",
"The Cluster Parameter Group cannot be deleted: " + parameterGroupName, 400);
}
ClusterParameterGroup found = stored.orElseThrow(() -> new AwsException("ClusterParameterGroupNotFound",
"Cluster parameter group " + parameterGroupName + " not found", 404));
if (regionalClusters().stream().anyMatch(c -> parameterGroupName.equals(c.getClusterParameterGroupName()))) {
throw new AwsException("InvalidClusterParameterGroupState", "One or more Clusters are still members of "
+ "this parameter group " + parameterGroupName + ", so the group cannot be deleted", 400);
}
parameterGroups.delete(regionalKey(parameterGroupName));
parameterGroups.flush();
return found;
@@ -3272,6 +3314,13 @@ public class RedshiftService implements ManagedSecretRotator {
ClusterSubnetGroup group = subnetGroups.get(regionalKey(name))
.orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFoundFault",
"Cluster subnet group " + name + " not found", 400));
// A group a cluster of the Region still uses is refused, naming one such cluster, as Redshift answers
// (measured on AWS).
regionalClusters().stream().filter(c -> name.equals(c.getClusterSubnetGroupName()))
.map(Cluster::getClusterIdentifier).sorted().findFirst().ifPresent(cluster -> {
throw new AwsException("InvalidClusterSubnetGroupStateFault", "Cannot delete the subnet group '"
+ name + "' because at least one Cluster: " + cluster + " is still using it.", 400);
});
subnetGroups.delete(regionalKey(name));
subnetGroups.flush();
return group;