feat(redshift): add ModifyClusterIamRoles, DescribeClusterVersions and DescribeOrderableClusterOptions (#3990)

ModifyClusterIamRoles adds and removes the IAM roles associated with a
cluster. The change is pushed to the running auth proxy, so COPY and
UNLOAD on new connections authorize against the updated role list.
Cluster IamRoles now report ApplyStatus in-sync.

DescribeClusterVersions and DescribeOrderableClusterOptions return static
data so IaC providers can validate node types before creating a cluster.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-21 11:32:09 -04:00
1 parent 992b80f8f7
commit d227ef9195
13 files changed
+390 -9

No files matched your search

@@ -0,0 +1,36 @@
package io.github.hectorvent.floci.services.redshift;
import java.util.List;
/**
* Static answers for the read-only "what can I order" APIs that IaC providers call before creating a
* cluster. Every emulated cluster is one PostgreSQL container, so these values only need to be
* accepted by SDK validation, not to describe real capacity.
*/
final class RedshiftClusterCatalog {
static final String CLUSTER_VERSION = "1.0";
static final String PARAMETER_GROUP_FAMILY = "redshift-1.0";
record OrderableOption(String clusterType, String nodeType) {}
private static final List<OrderableOption> ORDERABLE_OPTIONS = List.of(
new OrderableOption("single-node", "dc2.large"),
new OrderableOption("multi-node", "dc2.large"),
new OrderableOption("multi-node", "dc2.8xlarge"),
new OrderableOption("multi-node", "ra3.xlplus"),
new OrderableOption("multi-node", "ra3.4xlarge"),
new OrderableOption("multi-node", "ra3.16xlarge"));
private RedshiftClusterCatalog() {
}
static List<OrderableOption> orderableOptions(String clusterVersion, String nodeType) {
if (clusterVersion != null && !clusterVersion.isBlank() && !CLUSTER_VERSION.equals(clusterVersion)) {
return List.of();
}
return ORDERABLE_OPTIONS.stream()
.filter(option -> nodeType == null || nodeType.isBlank() || option.nodeType().equals(nodeType))
.toList();
}
}
@@ -514,6 +514,75 @@ public class RedshiftQueryHandler {
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "DescribeClusterVersions" -> {
String requested = params.getFirst("ClusterVersion");
XmlBuilder builder = new XmlBuilder()
.start("DescribeClusterVersionsResponse")
.start("DescribeClusterVersionsResult")
.start("ClusterVersions");
if (requested == null || requested.isBlank() || RedshiftClusterCatalog.CLUSTER_VERSION.equals(requested)) {
builder.start("ClusterVersion")
.elem("ClusterVersion", RedshiftClusterCatalog.CLUSTER_VERSION)
.elem("ClusterParameterGroupFamily", RedshiftClusterCatalog.PARAMETER_GROUP_FAMILY)
.elem("Description", "Amazon Redshift emulated engine")
.end("ClusterVersion");
}
String xml = builder
.end("ClusterVersions")
.end("DescribeClusterVersionsResult")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end("DescribeClusterVersionsResponse")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "DescribeOrderableClusterOptions" -> {
String zone = regionResolver.resolveRegionFromAuth(authorizationHeader) + "a";
XmlBuilder builder = new XmlBuilder()
.start("DescribeOrderableClusterOptionsResponse")
.start("DescribeOrderableClusterOptionsResult")
.start("OrderableClusterOptions");
for (RedshiftClusterCatalog.OrderableOption option : RedshiftClusterCatalog.orderableOptions(
params.getFirst("ClusterVersion"), params.getFirst("NodeType"))) {
builder.start("OrderableClusterOption")
.elem("ClusterVersion", RedshiftClusterCatalog.CLUSTER_VERSION)
.elem("ClusterType", option.clusterType())
.elem("NodeType", option.nodeType())
.start("AvailabilityZones")
.start("AvailabilityZone").elem("Name", zone).end("AvailabilityZone")
.end("AvailabilityZones")
.end("OrderableClusterOption");
}
String xml = builder
.end("OrderableClusterOptions")
.end("DescribeOrderableClusterOptionsResult")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end("DescribeOrderableClusterOptionsResponse")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "ModifyClusterIamRoles" -> {
String clusterIdentifier = params.getFirst("ClusterIdentifier");
if (clusterIdentifier == null || clusterIdentifier.isBlank()) {
throw new AwsException("InvalidParameterValue", "ClusterIdentifier is required", 400);
}
Cluster cluster = service.modifyClusterIamRoles(clusterIdentifier,
memberList(params, "AddIamRoles"), memberList(params, "RemoveIamRoles"));
String xml = new XmlBuilder()
.start("ModifyClusterIamRolesResponse")
.start("ModifyClusterIamRolesResult")
.raw(buildClusterXml(cluster))
.end("ModifyClusterIamRolesResult")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end("ModifyClusterIamRolesResponse")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "RebootCluster" -> {
String clusterIdentifier = params.getFirst("ClusterIdentifier");
if (clusterIdentifier == null || clusterIdentifier.isBlank()) {
@@ -655,7 +724,7 @@ public class RedshiftQueryHandler {
if (cluster.getIamRoleArns() != null && !cluster.getIamRoleArns().isEmpty()) {
builder.start("IamRoles");
for (String iamRoleArn : cluster.getIamRoleArns()) {
builder.start("IamRole").elem("IamRoleArn", iamRoleArn).end("IamRole");
builder.start("IamRole").elem("IamRoleArn", iamRoleArn).elem("ApplyStatus", "in-sync").end("IamRole");
}
builder.end("IamRoles");
}
@@ -886,7 +955,7 @@ public class RedshiftQueryHandler {
return switch (baseName) {
case "SubnetIds" -> quoted + "(\\.member|\\.SubnetIdentifier)?\\.\\d+";
case "VpcSecurityGroupIds" -> quoted + "(\\.member|\\.VpcSecurityGroupId)?\\.\\d+";
case "IamRoles" -> quoted + "(\\.member|\\.IamRoleArn)?\\.\\d+";
case "IamRoles", "AddIamRoles", "RemoveIamRoles" -> quoted + "(\\.member|\\.IamRoleArn)?\\.\\d+";
case "TagKeys" -> quoted + "(\\.member|\\.TagKey)?\\.\\d+";
case "DbGroups" -> quoted + "(\\.member|\\.DbGroup)?\\.\\d+";
default -> quoted + "(\\.member)?\\.\\d+";
@@ -42,6 +42,7 @@ import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -648,6 +649,38 @@ public class RedshiftService {
return cluster;
}
public synchronized Cluster modifyClusterIamRoles(String clusterIdentifier, List<String> addIamRoles,
List<String> removeIamRoles) {
Cluster cluster = clusters.get(clusterIdentifier)
.orElseThrow(() -> new AwsException("ClusterNotFound", "Cluster " + clusterIdentifier + " not found", 404));
// Validate every ARN before mutating: the cluster object is a live reference from HybridStorage.
addIamRoles.forEach(RedshiftService::requireIamRoleArn);
removeIamRoles.forEach(RedshiftService::requireIamRoleArn);
Set<String> roles = new LinkedHashSet<>(cluster.getIamRoleArns());
roles.addAll(addIamRoles);
roles.removeAll(removeIamRoles);
cluster.setIamRoleArns(List.copyOf(roles));
proxyManager.updateIamRoles(relayKey(clusters.accountId(), clusterIdentifier), cluster.getIamRoleArns());
clusters.put(clusterIdentifier, cluster);
clusters.flush();
return cluster;
}
private static void requireIamRoleArn(String arn) {
AwsArnUtils.Arn parsed;
try {
parsed = AwsArnUtils.parse(arn);
} catch (IllegalArgumentException e) {
throw new AwsException("InvalidParameterValue", "Invalid IAM role ARN: " + arn, 400);
}
if (!"iam".equals(parsed.service()) || !parsed.resource().startsWith("role/")) {
throw new AwsException("InvalidParameterValue", "Not an IAM role ARN: " + arn, 400);
}
}
private void updateManagedMasterSecret(Cluster cluster, String password) {
if (cluster.getMasterPasswordSecretArn() == null || secretsManagerService == null) {
return;
@@ -39,7 +39,7 @@ public class RedshiftAuthProxy {
private final S3Service s3Service;
private final IamService iamService;
private final String clusterAccountId;
private final List<String> iamRoleArns;
private volatile List<String> iamRoleArns;
private final int handshakeTimeoutMillis;
private final int backendConnectTimeoutMillis;
private final Semaphore connectionPermits;
@@ -141,6 +141,11 @@ public class RedshiftAuthProxy {
this.masterPassword = newPassword;
}
/** Swap the associated-role snapshot; new connections authorize COPY and UNLOAD against it. */
public void updateIamRoles(List<String> newIamRoleArns) {
this.iamRoleArns = newIamRoleArns == null ? List.of() : List.copyOf(newIamRoleArns);
}
public void stop() {
running = false;
try {
@@ -106,6 +106,14 @@ public class RedshiftProxyManager {
}
}
public synchronized void updateIamRoles(String relayKey, List<String> iamRoleArns) {
RedshiftAuthProxy proxy = proxies.get(relayKey);
if (proxy != null) {
proxy.updateIamRoles(iamRoleArns);
LOG.infov("Updated Redshift proxy IAM roles for cluster {0}", relayKey);
}
}
public synchronized void stopProxy(String relayKey) {
// Retry any listener a previous failed start/replace could not close. If it still
// cannot be closed this throws, and the entry stays for the next retry.