diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftOperationsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftOperationsTest.java index baa2454c9b..77b45fd550 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftOperationsTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftOperationsTest.java @@ -30,7 +30,10 @@ import software.amazon.awssdk.services.redshift.model.DescribeClusterSnapshotsRe import software.amazon.awssdk.services.redshift.model.DescribeClusterSubnetGroupsRequest; import software.amazon.awssdk.services.redshift.model.DescribeClustersRequest; import software.amazon.awssdk.services.redshift.model.DescribeClustersResponse; +import software.amazon.awssdk.services.redshift.model.DescribeOrderableClusterOptionsRequest; import software.amazon.awssdk.services.redshift.model.DescribeTagsRequest; +import software.amazon.awssdk.services.redshift.model.ModifyClusterIamRolesRequest; +import software.amazon.awssdk.services.redshift.model.ModifyClusterIamRolesResponse; import software.amazon.awssdk.services.redshift.model.ModifyClusterRequest; import software.amazon.awssdk.services.redshift.model.RebootClusterRequest; import software.amazon.awssdk.services.redshift.model.RestoreFromClusterSnapshotRequest; @@ -309,6 +312,27 @@ class RedshiftOperationsTest { .build()); assertThat(modified.cluster().nodeType()).isEqualTo("ra3.xlplus"); + String roleArn = "arn:aws:iam::000000000000:role/rs-copy-role"; + ModifyClusterIamRolesResponse withRole = client.modifyClusterIamRoles(ModifyClusterIamRolesRequest.builder() + .clusterIdentifier(clusterId) + .addIamRoles(roleArn) + .build()); + assertThat(withRole.cluster().iamRoles()) + .anyMatch(r -> roleArn.equals(r.iamRoleArn()) && "in-sync".equals(r.applyStatus())); + ModifyClusterIamRolesResponse withoutRole = client.modifyClusterIamRoles(ModifyClusterIamRolesRequest.builder() + .clusterIdentifier(clusterId) + .removeIamRoles(roleArn) + .build()); + assertThat(withoutRole.cluster().iamRoles()).isEmpty(); + + assertThat(client.describeClusterVersions().clusterVersions()) + .anyMatch(v -> "redshift-1.0".equals(v.clusterParameterGroupFamily())); + assertThat(client.describeOrderableClusterOptions(DescribeOrderableClusterOptionsRequest.builder() + .nodeType("ra3.xlplus") + .build()).orderableClusterOptions()) + .isNotEmpty() + .allMatch(o -> "ra3.xlplus".equals(o.nodeType())); + var rebooted = client.rebootCluster(RebootClusterRequest.builder() .clusterIdentifier(clusterId) .build()); diff --git a/docs/services/index.md b/docs/services/index.md index 5be2607342..3f2e8ab923 100644 --- a/docs/services/index.md +++ b/docs/services/index.md @@ -72,7 +72,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac | [Lake Formation](lakeformation.md) | `POST /` | REST JSON | 16 | | [Neptune](neptune.md) | `POST /` with `Action=` param + Gremlin TCP proxy | Query + WebSocket | 14 | | [DocumentDB](docdb.md) | `POST /` with `Action=` param + MongoDB container | Query + MongoDB wire | 8 | -| [Redshift](redshift.md) | `POST /` with `Action=` param + PostgreSQL container | Query + PostgreSQL wire (+ CFN) | 23 | +| [Redshift](redshift.md) | `POST /` with `Action=` param + PostgreSQL container | Query + PostgreSQL wire (+ CFN) | 29 | | [Redshift Data API](redshift-data.md) | `POST /` + `X-Amz-Target: RedshiftData.*` | JSON 1.1 | 11 | | [EMR](emr.md) | `POST /` + `X-Amz-Target: ElasticMapReduce.*` | JSON 1.1 | 24 | | [EMR Serverless](emr-serverless.md) | `/applications/*` | REST JSON | 7 | diff --git a/docs/services/redshift.md b/docs/services/redshift.md index d996043b4f..acfcf4000b 100644 --- a/docs/services/redshift.md +++ b/docs/services/redshift.md @@ -40,6 +40,9 @@ For running SQL without a PostgreSQL wire connection (the way Lambda and Step Fu | `ModifyClusterSubnetGroup` | Update a subnet group's description or subnet list | | `DeleteClusterSubnetGroup` | Remove a subnet group | | `ModifyCluster` | Update node type, parameter group, security groups, or the master password | +| `DescribeClusterVersions` | Return the single emulated engine version and its parameter group family | +| `DescribeOrderableClusterOptions` | Return the static node types and cluster types, optionally filtered by `NodeType` or `ClusterVersion` | +| `ModifyClusterIamRoles` | Add or remove the IAM roles associated with a cluster; COPY and UNLOAD see the change on new connections. `DefaultIamRoleArn` is ignored | | `RebootCluster` | Restart a cluster's container | | `GetClusterCredentials` | Issue a short-lived DbUser / DbPassword pair the auth proxy and Data API accept for a non-master user | | `GetClusterCredentialsWithIAM` | Issue short-lived credentials with the DbUser derived from the caller's IAM identity | @@ -238,7 +241,9 @@ order) through its own S3 service and streams the rows into the backing PostgreS bucket as the manifest file itself; cross-bucket manifest entries are rejected as an intentional deviation. - `IGNOREHEADER` and `HEADER` skip lines from the first resolved object only. - `GZIP` is the only input compression recognized; `BZIP2`, `LZOP` and `ZSTD` are not. -- `IAM_ROLE ''` is supported. The role must be associated with the cluster, exist in +- `IAM_ROLE ''` is supported. The role must be associated with the cluster (at + `CreateCluster` or through `ModifyClusterIamRoles`; connections opened before a change keep the + roles they started with), exist in the local IAM service, and trust Redshift to assume it. With `FLOCI_SERVICES_S3_ENFORCE_AUTH` off, S3 policy checks are skipped. With it on, the role's identity policy must allow the required S3 actions, and any bucket policy must not deny the diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftClusterCatalog.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftClusterCatalog.java new file mode 100644 index 0000000000..a15e42a374 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftClusterCatalog.java @@ -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 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 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(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java index 8dafed5def..6a13d58ae8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java @@ -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+"; diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java index 516b9d543e..8876548bb2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java @@ -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 addIamRoles, + List 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 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; diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java index ac06e346d3..88445600ea 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java @@ -39,7 +39,7 @@ public class RedshiftAuthProxy { private final S3Service s3Service; private final IamService iamService; private final String clusterAccountId; - private final List iamRoleArns; + private volatile List 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 newIamRoleArns) { + this.iamRoleArns = newIamRoleArns == null ? List.of() : List.copyOf(newIamRoleArns); + } + public void stop() { running = false; try { diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManager.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManager.java index f45f026d8f..644b949701 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManager.java @@ -106,6 +106,14 @@ public class RedshiftProxyManager { } } + public synchronized void updateIamRoles(String relayKey, List 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. diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java index a7ecb616ac..58ce508562 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java @@ -15,6 +15,7 @@ import java.util.Optional; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; @@ -167,6 +168,54 @@ public class RedshiftOperationsTest { .statusCode(200) .body(containsString("ra3.xlplus")); + // 1d. ModifyClusterIamRoles adds a role, then removes it + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "ModifyClusterIamRoles") + .formParam("ClusterIdentifier", "cluster-src") + .formParam("AddIamRoles.IamRoleArn.1", "arn:aws:iam::000000000000:role/redshift-copy") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("arn:aws:iam::000000000000:role/redshift-copy")) + .body(containsString("in-sync")); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "ModifyClusterIamRoles") + .formParam("ClusterIdentifier", "cluster-src") + .formParam("RemoveIamRoles.IamRoleArn.1", "arn:aws:iam::000000000000:role/redshift-copy") + .when() + .post("/") + .then() + .statusCode(200) + .body(not(containsString("redshift-copy"))); + + // 1e. Static discovery APIs + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "DescribeClusterVersions") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("redshift-1.0")); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "DescribeOrderableClusterOptions") + .formParam("NodeType", "ra3.xlplus") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("ra3.xlplus")); + // 2. CreateClusterSnapshot given() .contentType("application/x-www-form-urlencoded") diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandlerTest.java index 1e55a597a1..7b938d8f12 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandlerTest.java @@ -428,6 +428,74 @@ class RedshiftQueryHandlerTest { assertEquals("InvalidParameterValue", ex.getErrorCode()); } + @Test + void modifyClusterIamRolesAcceptsNamedMemberForm() { + // Real Redshift SDK sends "AddIamRoles.IamRoleArn.N" and "RemoveIamRoles.IamRoleArn.N". + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "c1"); + params.putSingle("AddIamRoles.IamRoleArn.1", "arn:aws:iam::111111111111:role/a"); + params.putSingle("RemoveIamRoles.IamRoleArn.1", "arn:aws:iam::111111111111:role/b"); + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("c1"); + cluster.setIamRoleArns(List.of("arn:aws:iam::111111111111:role/a")); + when(service.modifyClusterIamRoles(eq("c1"), any(), any())).thenReturn(cluster); + + Response response = handler.handle("ModifyClusterIamRoles", params); + + assertEquals(200, response.getStatus()); + verify(service).modifyClusterIamRoles("c1", + List.of("arn:aws:iam::111111111111:role/a"), List.of("arn:aws:iam::111111111111:role/b")); + String body = response.getEntity().toString(); + assertTrue(body.contains("")); + assertTrue(body.contains("arn:aws:iam::111111111111:role/a")); + assertTrue(body.contains("in-sync")); + } + + @Test + void describeClusterVersionsListsTheEmulatedVersion() { + Response response = handler.handle("DescribeClusterVersions", new MultivaluedHashMap<>()); + + assertEquals(200, response.getStatus()); + String body = response.getEntity().toString(); + assertTrue(body.contains("")); + assertTrue(body.contains("1.0")); + assertTrue(body.contains("redshift-1.0")); + } + + @Test + void describeClusterVersionsFiltersByVersion() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterVersion", "9.9"); + + String body = handler.handle("DescribeClusterVersions", params).getEntity().toString(); + + assertFalse(body.contains("1.0")); + } + + @Test + void describeOrderableClusterOptionsFiltersByNodeType() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("NodeType", "ra3.xlplus"); + when(regionResolver.resolveRegionFromAuth("auth")).thenReturn("eu-west-1"); + + Response response = handler.handle("DescribeOrderableClusterOptions", params, "auth"); + + assertEquals(200, response.getStatus()); + String body = response.getEntity().toString(); + assertTrue(body.contains("eu-west-1a")); + assertTrue(body.contains("")); + assertTrue(body.contains("ra3.xlplus")); + assertFalse(body.contains("dc2.large")); + assertTrue(body.contains("multi-node")); + } + + @Test + void modifyClusterIamRolesRequiresClusterIdentifier() { + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("ModifyClusterIamRoles", new MultivaluedHashMap<>())); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + } + @Test void testBuildClusterXmlIncludesParameterGroupAndTags() { MultivaluedMap params = new MultivaluedHashMap<>(); diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java index fbb41fee35..dda3f10593 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java @@ -807,6 +807,62 @@ class RedshiftServiceTest { service.modifyCluster("missing", "ra3.xlplus", null, null, null, null)); } + @Test + void modifyClusterIamRolesAddsAndRemovesRolesAndUpdatesTheProxy() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("c1"); + cluster.setIamRoleArns(List.of("arn:aws:iam::111111111111:role/old", "arn:aws:iam::111111111111:role/keep")); + when(clusterBackend.get("c1")).thenReturn(Optional.of(cluster)); + when(clusterBackend.accountId()).thenReturn("111111111111"); + + Cluster updated = service.modifyClusterIamRoles("c1", + List.of("arn:aws:iam::111111111111:role/new", "arn:aws:iam::111111111111:role/keep"), + List.of("arn:aws:iam::111111111111:role/old")); + + assertEquals(List.of("arn:aws:iam::111111111111:role/keep", "arn:aws:iam::111111111111:role/new"), + updated.getIamRoleArns()); + verify(proxyManager).updateIamRoles("111111111111:c1", updated.getIamRoleArns()); + verify(clusterBackend).put(eq("c1"), any(Cluster.class)); + verify(clusterBackend).flush(); + } + + @Test + void modifyClusterIamRolesRejectsMalformedRoleArnWithoutChangingTheCluster() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("c1"); + cluster.setIamRoleArns(List.of("arn:aws:iam::111111111111:role/keep")); + when(clusterBackend.get("c1")).thenReturn(Optional.of(cluster)); + + AwsException ex = assertThrows(AwsException.class, () -> + service.modifyClusterIamRoles("c1", List.of("not-an-arn"), List.of())); + + assertEquals("InvalidParameterValue", ex.getErrorCode()); + assertEquals(List.of("arn:aws:iam::111111111111:role/keep"), cluster.getIamRoleArns()); + verify(proxyManager, never()).updateIamRoles(any(), any()); + } + + @Test + void modifyClusterIamRolesRejectsNonRoleArn() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("c1"); + when(clusterBackend.get("c1")).thenReturn(Optional.of(cluster)); + + AwsException ex = assertThrows(AwsException.class, () -> + service.modifyClusterIamRoles("c1", List.of("arn:aws:iam::111111111111:user/bob"), List.of())); + + assertEquals("InvalidParameterValue", ex.getErrorCode()); + } + + @Test + void modifyClusterIamRolesNotFound() { + when(clusterBackend.get("missing")).thenReturn(Optional.empty()); + + AwsException ex = assertThrows(AwsException.class, () -> + service.modifyClusterIamRoles("missing", List.of(), List.of())); + + assertEquals("ClusterNotFound", ex.getErrorCode()); + } + @Test void testDescribeSnapshots() { Snapshot s = new Snapshot("snap-1", "my-cluster", "available", 5439, "admin"); diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManagerTest.java index 7bee189f13..58869aa73f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManagerTest.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.lang.reflect.Field; import java.net.InetSocketAddress; import java.net.ServerSocket; +import java.util.List; import java.util.concurrent.ConcurrentHashMap; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -99,6 +100,26 @@ class RedshiftProxyManagerTest { } + @Test + void updateIamRolesSwapsTheRunningSnapshotWithoutARestart() throws Exception { + RedshiftProxyManager manager = newManager(); + int port = availablePort(); + try { + start(manager, KEY, port); + manager.updateIamRoles(KEY, List.of("arn:aws:iam::111111111111:role/copy")); + assertEquals(List.of("arn:aws:iam::111111111111:role/copy"), iamRoleArns(registry(manager).get(KEY))); + assertPortUnavailable(port); + } finally { + manager.stopAll(); + } + } + + @Test + void updateIamRolesForUnknownKeyIsANoOp() { + RedshiftProxyManager manager = newManager(); + assertDoesNotThrow(() -> manager.updateIamRoles("missing", List.of())); + } + @Test void updateMasterPasswordForUnknownKeyIsANoOp() { RedshiftProxyManager manager = newManager(); @@ -267,6 +288,13 @@ class RedshiftProxyManagerTest { return (String) field.get(proxy); } + @SuppressWarnings("unchecked") + private static List iamRoleArns(RedshiftAuthProxy proxy) throws Exception { + Field field = RedshiftAuthProxy.class.getDeclaredField("iamRoleArns"); + field.setAccessible(true); + return (List) field.get(proxy); + } + private static int availablePort() throws IOException { try (ServerSocket socket = new ServerSocket(0)) { return socket.getLocalPort(); diff --git a/tools/docs/services.yaml b/tools/docs/services.yaml index bc46c62091..cbc027996a 100644 --- a/tools/docs/services.yaml +++ b/tools/docs/services.yaml @@ -281,10 +281,10 @@ services: doc: docs/services/redshift.md sources: - { path: src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java, mode: switch } - # SubnetIds / VpcSecurityGroupIds / IamRoles / TagKeys / DbGroups are labels of an inner - # member-name switch (memberKeyRegex), not actions, same shape as the rds - # exclusions above. - exclude_actions: [SubnetIds, VpcSecurityGroupIds, IamRoles, TagKeys, DbGroups] + # SubnetIds / VpcSecurityGroupIds / IamRoles / AddIamRoles / RemoveIamRoles / TagKeys / DbGroups + # are labels of an inner member-name switch (memberKeyRegex), not actions, same shape as the + # rds exclusions above. + exclude_actions: [SubnetIds, VpcSecurityGroupIds, IamRoles, AddIamRoles, RemoveIamRoles, TagKeys, DbGroups] - service: redshiftdata doc: docs/services/redshift-data.md sources: