Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(redshift-serverless): add running workgroups and Data API WorkgroupName support (#5012)

Vũ Thế Huy99a6b4050c97
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java
index e91a527e6e..168aa7c2a4 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RedshiftServerlessTest.java
@@ -3,19 +3,28 @@ package com.floci.test;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
+import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient;
+import software.amazon.awssdk.services.redshiftdata.model.DescribeStatementResponse;
+import software.amazon.awssdk.services.redshiftdata.model.ExecuteStatementResponse;
+import software.amazon.awssdk.services.redshiftdata.model.StatusString;
import software.amazon.awssdk.services.redshiftserverless.RedshiftServerlessClient;
+import software.amazon.awssdk.services.redshiftserverless.model.GetCredentialsResponse;
import software.amazon.awssdk.services.redshiftserverless.model.ConflictException;
import software.amazon.awssdk.services.redshiftserverless.model.Namespace;
import software.amazon.awssdk.services.redshiftserverless.model.NamespaceStatus;
import software.amazon.awssdk.services.redshiftserverless.model.ResourceNotFoundException;
import software.amazon.awssdk.services.redshiftserverless.model.Tag;
+import software.amazon.awssdk.services.redshiftserverless.model.Workgroup;
+import software.amazon.awssdk.services.redshiftserverless.model.WorkgroupStatus;
+
+import java.time.Instant;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
-@DisplayName("Redshift Serverless namespace lifecycle")
+@DisplayName("Redshift Serverless namespace and workgroup lifecycle")
class RedshiftServerlessTest {
private static final Logger LOG = Logger.getLogger(RedshiftServerlessTest.class);
@@ -70,6 +79,96 @@ class RedshiftServerlessTest {
}
}
+ @Test
+ void workgroupLifecycleUsesAwsSdk() {
+ assumeFalse(TestFixtures.isRealAws(), "Creates a workgroup and asserts emulator-local defaults");
+
+ try (RedshiftServerlessClient client = TestFixtures.redshiftServerlessClient();
+ RedshiftDataClient dataClient = TestFixtures.redshiftDataClient()) {
+ String namespaceName = "floci-compat-wg-ns";
+ String workgroupName = "floci-compat-wg";
+ try {
+ client.createNamespace(request -> request
+ .namespaceName(namespaceName)
+ .adminUsername("admin")
+ .adminUserPassword("Secret123!"));
+
+ Workgroup created = client.createWorkgroup(request -> request
+ .workgroupName(workgroupName)
+ .namespaceName(namespaceName)
+ .baseCapacity(32)
+ .tags(Tag.builder().key("env").value("dev").build())).workgroup();
+
+ assertThat(created.workgroupName()).isEqualTo(workgroupName);
+ assertThat(created.namespaceName()).isEqualTo(namespaceName);
+ assertThat(created.status()).isEqualTo(WorkgroupStatus.AVAILABLE);
+ assertThat(created.baseCapacity()).isEqualTo(32);
+ assertThat(created.port()).isEqualTo(5439);
+ assertThat(created.trackName()).isEqualTo("current");
+ assertThat(created.workgroupArn()).contains(":redshift-serverless:").contains(":workgroup/");
+ assertThat(created.creationDate()).isNotNull();
+ assertThat(created.endpoint().address()).isNotBlank();
+ assertThat(created.endpoint().port()).isPositive();
+
+ Workgroup fetched = client.getWorkgroup(request -> request.workgroupName(workgroupName)).workgroup();
+ assertThat(fetched.workgroupId()).isEqualTo(created.workgroupId());
+
+ assertThat(client.listWorkgroups(request -> {}).workgroups())
+ .extracting(Workgroup::workgroupName)
+ .contains(workgroupName);
+
+ Workgroup updated = client.updateWorkgroup(request -> request
+ .workgroupName(workgroupName)
+ .baseCapacity(64)).workgroup();
+ assertThat(updated.baseCapacity()).isEqualTo(64);
+
+ assertThatThrownBy(() -> client.createWorkgroup(request -> request
+ .workgroupName(workgroupName)
+ .namespaceName(namespaceName)))
+ .isInstanceOf(ConflictException.class);
+ assertThatThrownBy(() -> client.deleteNamespace(request -> request.namespaceName(namespaceName)))
+ .isInstanceOf(ConflictException.class);
+
+ GetCredentialsResponse credentials = client.getCredentials(request -> request
+ .workgroupName(workgroupName)
+ .dbName("dev"));
+ assertThat(credentials.dbUser()).isNotBlank();
+ assertThat(credentials.dbPassword()).isNotBlank();
+ assertThat(credentials.expiration()).isAfter(Instant.now());
+
+ ExecuteStatementResponse executed = dataClient.executeStatement(request -> request
+ .workgroupName(workgroupName)
+ .database("dev")
+ .sql("select 1"));
+ assertThat(executed.workgroupName()).isEqualTo(workgroupName);
+ assertThat(executed.clusterIdentifier()).isNull();
+ DescribeStatementResponse described = dataClient.describeStatement(request -> request.id(executed.id()));
+ assertThat(described.status()).isEqualTo(StatusString.FINISHED);
+
+ String arn = created.workgroupArn();
+ client.tagResource(request -> request.resourceArn(arn)
+ .tags(Tag.builder().key("team").value("data").build()));
+ assertThat(client.listTagsForResource(request -> request.resourceArn(arn)).tags())
+ .extracting(Tag::key)
+ .contains("env", "team");
+ } finally {
+ deleteWorkgroupBestEffort(client, workgroupName);
+ deleteBestEffort(client, namespaceName);
+ }
+
+ assertThatThrownBy(() -> client.getWorkgroup(request -> request.workgroupName(workgroupName)))
+ .isInstanceOf(ResourceNotFoundException.class);
+ }
+ }
+
+ private static void deleteWorkgroupBestEffort(RedshiftServerlessClient client, String workgroupName) {
+ try {
+ client.deleteWorkgroup(request -> request.workgroupName(workgroupName));
+ } catch (Exception cleanupError) {
+ LOG.warnf(cleanupError, "Best-effort cleanup failed for workgroupName=%s", workgroupName);
+ }
+ }
+
private static void deleteBestEffort(RedshiftServerlessClient client, String namespaceName) {
try {
client.deleteNamespace(request -> request.namespaceName(namespaceName));
diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md
index ee7223b0ed..3cc85769c2 100644
--- a/docs/services/cloudformation.md
+++ b/docs/services/cloudformation.md
@@ -139,6 +139,7 @@ cross-resource references.
| EKS | `Cluster`, `Nodegroup` |
| RDS | `DBInstance` (starts a real container), `DBCluster` (starts a real container), `DBSubnetGroup`, `DBParameterGroup`, `DBClusterParameterGroup`, `DBProxy`, `DBProxyTargetGroup` |
| Redshift | `Cluster` (single-node container; Port and non-dev DBName ignored; ManageMasterPassword unsupported), `ClusterParameterGroup`, `ClusterSubnetGroup`, `ClusterSecurityGroup` (accepted; no EC2-Classic security group model) |
+| Redshift Serverless | `Namespace` (ManageAdminPassword is accepted and not applied; GetAtt uses the dotted Namespace.* form), `Workgroup` (starts a real container; one workgroup per namespace; capacity, VPC and price-performance settings are stored only; snapshot restore properties are ignored; GetAtt uses the dotted Workgroup.* form) |
| EC2 | `VPC`, `Subnet`, `SecurityGroup` (inline `SecurityGroupIngress`/`SecurityGroupEgress` supported), `SecurityGroupIngress`, `SecurityGroupEgress`, `InternetGateway`, `RouteTable`, `SubnetRouteTableAssociation`, `Route`, `NatGateway`, `EIP`, `Instance`, `LaunchTemplate`, `VPCGatewayAttachment`, `VPCEndpoint`, `NetworkAcl`, `NetworkAclEntry`, `SubnetNetworkAclAssociation`, `FlowLog` |
| Elastic Load Balancing v2 | `LoadBalancer`, `TargetGroup`, `Listener`, `ListenerRule` |
| Auto Scaling | `LaunchConfiguration`, `AutoScalingGroup`, `LifecycleHook`, `ScalingPolicy` |
diff --git a/docs/services/index.md b/docs/services/index.md
index 50b095e2d9..cf9eb99a9f 100644
--- a/docs/services/index.md
+++ b/docs/services/index.md
@@ -76,7 +76,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [DMS](dms.md) | `POST /` + `X-Amz-Target: AmazonDMSv20160101.*` | JSON 1.1 | 6 |
| [Redshift](redshift.md) | `POST /` with `Action=` param + PostgreSQL container | Query + PostgreSQL wire (+ CFN) | 35 |
| [Redshift Data API](redshift-data.md) | `POST /` + `X-Amz-Target: RedshiftData.*` | JSON 1.1 | 11 |
-| [Redshift Serverless](redshift-serverless.md) | `POST /` + `X-Amz-Target: RedshiftServerless.*` | JSON 1.1 | 8 |
+| [Redshift Serverless](redshift-serverless.md) | `POST /` + `X-Amz-Target: RedshiftServerless.*` | JSON 1.1 | 14 |
| [EMR](emr.md) | `POST /` + `X-Amz-Target: ElasticMapReduce.*` | JSON 1.1 | 37 |
| [EMR Serverless](emr-serverless.md) | `/applications/*`, `/tags/*` | REST JSON | 10 |
| [Data Firehose](firehose.md) | `POST /` + `X-Amz-Target: Firehose_20150804.*` | JSON 1.1 | 12 |
diff --git a/docs/services/redshift-data.md b/docs/services/redshift-data.md
index 7be0078a2c..49b2d40f86 100644
--- a/docs/services/redshift-data.md
+++ b/docs/services/redshift-data.md
@@ -34,12 +34,14 @@ For the upstream API shape, see the AWS documentation:
## Authentication modes
-A request identifies its target cluster one of two ways:
+A request identifies its target cluster or serverless workgroup one of three ways:
- **`ClusterIdentifier` + `DbUser` + `Database`.** The `DbUser` is the cluster master, or the prefixed name returned by `GetClusterCredentials` / `GetClusterCredentialsWithIAM` (for example `IAM:analyst`) while that credential is unexpired. Floci connects to the container as the cluster master in both cases. Any other `DbUser` returns `ValidationException`.
- **`SecretArn` + `ClusterIdentifier` + `Database`.** The secret must be a local Secrets Manager secret holding JSON credentials (`username` or `user`, plus `password`). A cross-region `SecretArn` is rejected.
-`WorkgroupName` (Amazon Redshift Serverless) is rejected with `ValidationException`. Redshift Serverless is not emulated.
+- **`WorkgroupName` + `Database`, with or without `SecretArn`.** `WorkgroupName` is a workgroup name or workgroup ARN created through [Redshift Serverless](redshift-serverless.md). `Database` must be the namespace `dbName`. Without a `SecretArn`, Floci connects as the namespace admin; AWS derives the database user from the signing identity (`IAM:<name>`), and `DbUser` is not used. With a `SecretArn`, the secret must hold credentials the workgroup backend accepts. Sending `ClusterIdentifier` as well returns `ValidationException`, and so does a workgroup whose runtime is not running.
+
+A statement run against a workgroup reports `WorkgroupName` and omits `ClusterIdentifier`, and the reverse holds for a cluster statement. `ListDatabases`, `ListSchemas`, `ListTables` and `DescribeTable` accept `WorkgroupName` the same way.
## Compatibility Notes
diff --git a/docs/services/redshift-serverless.md b/docs/services/redshift-serverless.md
index 8b335d7bab..f4397a94c3 100644
--- a/docs/services/redshift-serverless.md
+++ b/docs/services/redshift-serverless.md
@@ -3,7 +3,7 @@
**Protocol:** JSON 1.1
**Endpoint:** `POST http://localhost:4566/` with `X-Amz-Target: RedshiftServerless.<Operation>` and `Content-Type: application/x-amz-json-1.1`
-Floci emulates the namespace lifecycle of Amazon Redshift Serverless: the account and Region scoped container that holds a serverless database, its admin credentials, and its IAM roles. This is the surface Terraform's `aws_redshiftserverless_namespace` drives.
+Floci emulates the namespace and workgroup lifecycle of Amazon Redshift Serverless: the account and Region scoped namespace that holds a serverless database, its admin credentials, and its IAM roles, and the workgroup that carries its compute settings and endpoint. This is the surface Terraform's `aws_redshiftserverless_namespace` and `aws_redshiftserverless_workgroup` drive, and the surface the `AWS::RedshiftServerless::Namespace` and `AWS::RedshiftServerless::Workgroup` CloudFormation types provision.
For the upstream API shape, see the [Amazon Redshift Serverless API Reference](https://docs.aws.amazon.com/redshift-serverless/latest/APIReference/Welcome.html).
@@ -17,16 +17,25 @@ For the upstream API shape, see the [Amazon Redshift Serverless API Reference](h
| `ListNamespaces` | Page through the namespaces in the account and Region |
| `UpdateNamespace` | Apply the supplied fields to an existing namespace and return it |
| `DeleteNamespace` | Remove a namespace and return it with `status` `DELETING` |
-| `ListTagsForResource` | Return the tags on the namespace named by `resourceArn` |
-| `TagResource` | Merge tags into the namespace named by `resourceArn` |
-| `UntagResource` | Remove tags by key from the namespace named by `resourceArn` |
+| `CreateWorkgroup` | Create a workgroup in a namespace that has none, applying AWS defaults, starting its container and proxy, and returning its `endpoint` |
+| `GetWorkgroup` | Return a workgroup by name |
+| `ListWorkgroups` | Page through the workgroups in the account and Region |
+| `UpdateWorkgroup` | Apply the supplied fields to an existing workgroup and return it |
+| `DeleteWorkgroup` | Stop a workgroup's container and proxy, release its endpoint, and return it with `status` `DELETING` |
+| `GetCredentials` | Mint a temporary database user and password for a workgroup, valid on its endpoint until `expiration` |
+| `ListTagsForResource` | Return the tags on the namespace or workgroup named by `resourceArn` |
+| `TagResource` | Merge tags into the namespace or workgroup named by `resourceArn` |
+| `UntagResource` | Remove tags by key from the namespace or workgroup named by `resourceArn` |
<!-- floci:actions:end -->
-Namespace state is account and Region scoped and persisted through `StorageFactory`.
+Namespace and workgroup state is account and Region scoped and persisted through `StorageFactory`.
## Compatibility Notes
-- **Namespaces only.** Workgroups, snapshots, recovery points, usage limits, and endpoint access are not emulated. A namespace has no compute attached and no endpoint to connect to, so the Redshift Data API still rejects `WorkgroupName`.
+- **A workgroup runs a real PostgreSQL container.** `CreateWorkgroup` starts the container (this needs Docker and uses `floci.services.redshift.image-version`) and an authenticating proxy on the `endpoint`, whose port comes from the same pool as provisioned Redshift clusters (`floci.services.redshift.proxy-base-port` to `proxy-max-port`). Connect with any PostgreSQL client or JDBC driver using the namespace admin user and password, to the namespace `dbName`. The Redshift Data API accepts `WorkgroupName` ([details](redshift-data.md)). Only the 14 operations in the Supported Actions table are served. The rest of the Redshift Serverless API is not emulated and answers `UnknownOperationException`: snapshots and snapshot copy configurations, recovery points, table restore and restore status, usage limits, endpoint access, resource policies, custom domain associations, scheduled actions, reservations and reservation offerings, tracks, managed workgroups, the Identity Center token, and lakehouse configuration.
+- **Admin credentials come from the namespace.** The backend role is the namespace `adminUsername` (default `admin`) and the password is the `adminUserPassword` the namespace was created with. A namespace created without a password gets a generated one that is kept internally and never returned, so such a workgroup is reachable through `GetCredentials` and the Data API only. `manageAdminPassword` still creates no secret. Changing `adminUserPassword` with `UpdateNamespace` takes effect for new connections and is refused with `ValidationException` if the backend rejects it (AWS password rules: 8 to 64 characters with upper case, lower case and a digit, and none of `' " / @`). Changing `adminUsername` on a running workgroup does not rename the backend role; the new name is accepted at the proxy, which opens the backend as the original role.
+- **`GetCredentials` returns a master-equivalent password.** `dbUser` is derived from the signing identity (`IAM:<user>` or `IAMR:<role>`, falling back to `IAMR:floci` when the caller cannot be resolved), `dbPassword` is random, and `expiration` and `nextRefreshTime` are epoch seconds (the same instant). `durationSeconds` defaults to 900 and must be 900 to 3600. `dbName`, when given, must be the namespace `dbName`. `customDomainName` is not supported. A returned credential stays valid until it expires or the workgroup is deleted.
+- **The runtime survives a restart when its container does.** On startup Floci re-attaches to the container and restarts the proxy on the same port, so the endpoint is stable. If the container is gone the workgroup comes back with an empty database; if neither can be started, the workgroup stays `AVAILABLE`, the Data API reports that its runtime is not available, and the failure is logged. Containers have no Docker volume, as for provisioned clusters.
- **`adminUserPassword` is accepted and never returned**, matching AWS. No secret is created for `manageAdminPassword`.
- **Defaults follow AWS.** `dbName` defaults to `dev`, `kmsKeyId` to `AWS_OWNED_KMS_KEY`, `logExports` to an empty list, and `status` to `AVAILABLE` immediately: there is no `MODIFYING` or `CREATING` phase to poll through.
- **`creationDate` is an ISO-8601 string**, for example `2026-09-11T18:12:55.433Z`, not the
@@ -38,11 +47,17 @@ Namespace state is account and Region scoped and persisted through `StorageFacto
- **`namespaceId` is a generated UUID** and `namespaceArn` is `arn:aws:redshift-serverless:<region>:<account>:namespace/<namespaceId>`.
- **`DeleteNamespace` returns the deleted namespace with `status` `DELETING`** and removes it in the same call, so the next `GetNamespace` returns `ResourceNotFoundException`.
- **`UpdateNamespace` applies only the fields present in the request.** An omitted field keeps its stored value; an explicitly empty `iamRoles` or `logExports` array clears it.
-- **Tagging is keyed by `resourceArn`, not by namespace name**, and the namespace is the only taggable Redshift Serverless resource in Floci. `TagResource` merges into the existing tags rather than replacing them, `UntagResource` removes by key, and any ARN that does not name a known namespace in the caller's Region returns `ResourceNotFoundException`. Tags supplied to `CreateNamespace` are readable through `ListTagsForResource` and survive an `UpdateNamespace`.
+- **A namespace has at most one workgroup, and a workgroup belongs to exactly one namespace.** A second `CreateWorkgroup` for a namespace returns `ConflictException`, and so does `DeleteNamespace` while the namespace still has a workgroup. AWS documents the one-to-one relationship; the `DeleteNamespace` refusal is the one rule here that is inferred rather than documented, because the API reference lists `ConflictException` for that call without naming its trigger.
+- **Workgroup defaults follow AWS.** `baseCapacity` defaults to 128 RPUs, `port` to 5439, `ipAddressType` to `ipv4`, `trackName` to `current`, `pricePerformanceTarget` to `{"status":"DISABLED"}`, and `enhancedVpcRouting`, `publiclyAccessible` and `extraComputeForAutomaticOptimization` to `false`. `status` is `AVAILABLE` as soon as the call returns; `DeleteWorkgroup` returns the workgroup with `DELETING` and removes it in the same call.
+- **Capacity, network and security settings are stored and echoed, not enforced.** `baseCapacity`, `maxCapacity`, `subnetIds`, `securityGroupIds`, `enhancedVpcRouting`, `publiclyAccessible` and `configParameters` have no effect on the emulator. `configParameters` returns only what the caller supplied: AWS also adds a set of default parameters that the API reference does not enumerate, and Terraform treats the attribute as computed, so the difference does not produce a plan diff.
+- **`port` is validated and echoed.** It must be in 5431-5455 or 8191-8215. The `endpoint.port` is the port Floci allocated, which is what a client connects to.
+- **`UpdateWorkgroup` applies only the fields present.** AWS documents that several parameters cannot be changed in one request; Floci accepts any combination. A `trackName` different from the current one is recorded as `pendingTrackName` and `trackName` is unchanged, as AWS switches tracks on the next release. `workgroupName` and `namespaceName` cannot be changed.
+- **`workgroupArn` is `arn:aws:redshift-serverless:<region>:<account>:workgroup/<workgroupId>`**, and `creationDate` is an ISO-8601 string like the namespace one. `workgroupVersion`, `patchVersion`, `crossAccountVpcs`, the custom domain members and `endpoint.vpcEndpoints` are omitted.
+- **Tagging is keyed by `resourceArn`, not by name**, and namespaces and workgroups are the taggable Redshift Serverless resources in Floci. `TagResource` merges into the existing tags rather than replacing them, `UntagResource` removes by key, and any ARN that does not name a known namespace or workgroup in the caller's Region returns `ResourceNotFoundException`. Tags supplied at create are readable through `ListTagsForResource` and survive an update.
## AWS-compatible failures
-Namespace names, database names, and log export values are validated. Floci returns `ValidationException` for a malformed request, `ConflictException` when the namespace name is already taken, and `ResourceNotFoundException` for an unknown namespace.
+Namespace and workgroup names, database names, log export values, ports and workgroup enum members are validated. Floci returns `ValidationException` for a malformed request, `ConflictException` when a namespace or workgroup name is already taken, when a namespace already has a workgroup, or when a namespace being deleted still has one, and `ResourceNotFoundException` for an unknown namespace or workgroup.
AWS also models `AccessDeniedException`, `InternalServerException`, and `ThrottlingException`. Floci does not inject provider-side failures that cannot be derived from the request or emulator state.
@@ -52,6 +67,8 @@ AWS also models `AccessDeniedException`, `InternalServerException`, and `Throttl
|---|---|---|
| `FLOCI_SERVICES_REDSHIFT_SERVERLESS_ENABLED` | `true` | Enable or disable Redshift Serverless |
+Workgroup containers and proxies use the `floci.services.redshift.*` settings: `image-version`, `proxy-base-port` and `proxy-max-port`, `endpoint-host`, `docker-network` and the proxy connection limits.
+
## Example
```bash
@@ -66,5 +83,17 @@ aws redshift-serverless create-namespace \
aws redshift-serverless get-namespace --namespace-name analytics
aws redshift-serverless list-namespaces
aws redshift-serverless update-namespace --namespace-name analytics --log-exports userlog
+aws redshift-serverless create-workgroup \
+ --workgroup-name analytics-wg \
+ --namespace-name analytics \
+ --base-capacity 32
+
+aws redshift-serverless get-workgroup --workgroup-name analytics-wg
+aws redshift-serverless get-credentials --workgroup-name analytics-wg --db-name dev
+aws redshift-data execute-statement --workgroup-name analytics-wg --database dev --sql "select 1"
+aws redshift-serverless list-workgroups
+aws redshift-serverless update-workgroup --workgroup-name analytics-wg --base-capacity 64
+
+aws redshift-serverless delete-workgroup --workgroup-name analytics-wg
aws redshift-serverless delete-namespace --namespace-name analytics
```
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java
index 6f54535c64..2f32444e62 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java
@@ -329,7 +329,8 @@ public class AwsJson11Controller {
case "glue" -> glueJsonHandler.handle(action, request, region);
case "athena" -> athenaJsonHandler.handle(action, request, region);
case "redshift-data" -> redshiftDataJsonHandler.handle(action, request, region);
- case "redshift-serverless" -> redshiftServerlessJsonHandler.handle(action, request, region);
+ case "redshift-serverless" -> redshiftServerlessJsonHandler.handle(
+ action, request, region, httpHeaders.getHeaderString("Authorization"));
case "firehose" -> firehoseJsonHandler.handle(action, request, region);
case "tagging" -> resourceGroupsTaggingJsonHandler.handle(action, request, region);
case "codebuild" -> codeBuildJsonHandler.handle(action, request, region, regionResolver.getAccountId());
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftServerlessCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftServerlessCfnProvisioner.java
new file mode 100644
index 0000000000..7594e4843a
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftServerlessCfnProvisioner.java
@@ -0,0 +1,303 @@
+package io.github.hectorvent.floci.services.cloudformation.provisioners;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
+import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessService;
+import io.github.hectorvent.floci.services.redshiftserverless.WorkgroupSettings;
+import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
+import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
+import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
+import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Provisions {@code AWS::RedshiftServerless::Namespace} and {@code AWS::RedshiftServerless::Workgroup}.
+ * Both types use their name as the physical id, which is what {@code Ref} returns.
+ *
+ * <p>The registry schema declares every read-only attribute under a nested pointer
+ * ({@code /properties/Namespace/NamespaceArn}), so {@code Fn::GetAtt} takes the dotted form
+ * ({@code Namespace.NamespaceArn}, {@code Workgroup.Endpoint.Address}) and each is set under that
+ * exact key.
+ */
+@ApplicationScoped
+public class RedshiftServerlessCfnProvisioner implements CfnResourceProvisioner {
+
+ private static final Logger LOG = Logger.getLogger(RedshiftServerlessCfnProvisioner.class);
+
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+ private static final String NAMESPACE = "AWS::RedshiftServerless::Namespace";
+ private static final String WORKGROUP = "AWS::RedshiftServerless::Workgroup";
+ private static final int NAME_MAX_LENGTH = 64;
+ private static final List<String> UNSUPPORTED_WORKGROUP_PROPERTIES =
+ List.of("SnapshotArn", "SnapshotName", "SnapshotOwnerAccount", "RecoveryPointId");
+
+ private final RedshiftServerlessService service;
+
+ @Inject
+ public RedshiftServerlessCfnProvisioner(RedshiftServerlessService service) {
+ this.service = service;
+ }
+
+ @Override
+ public Set<String> resourceTypes() {
+ return Set.of(NAMESPACE, WORKGROUP);
+ }
+
+ @Override
+ public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
+ Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
+ switch (r.getResourceType()) {
+ case NAMESPACE -> provisionNamespace(r, props, ctx);
+ case WORKGROUP -> provisionWorkgroup(r, props, ctx);
+ default -> throw new IllegalStateException(
+ "RedshiftServerlessCfnProvisioner cannot provision " + r.getResourceType());
+ }
+ ReplacementCleanup.record(r, ctx, attributesBefore);
+ }
+
+ @Override
+ public void delete(String resourceType, String physicalId, String region) {
+ switch (resourceType) {
+ case NAMESPACE -> CfnDeletes.safeDelete("Redshift Serverless namespace", physicalId,
+ () -> service.deleteNamespace(physicalId, region), "ResourceNotFoundException");
+ case WORKGROUP -> CfnDeletes.safeDelete("Redshift Serverless workgroup", physicalId,
+ () -> service.deleteWorkgroup(physicalId, region), "ResourceNotFoundException");
+ default -> throw new IllegalStateException(
+ "RedshiftServerlessCfnProvisioner cannot delete " + resourceType);
+ }
+ }
+
+ @Override
+ public boolean hasReplacementUpdate(StackResource resource) {
+ return ReplacementCleanup.hasReplacement(resource);
+ }
+
+ @Override
+ public String updateCleanupPhysicalId(StackResource resource) {
+ return ReplacementCleanup.cleanupPhysicalId(resource);
+ }
+
+ @Override
+ public UpdateCleanupResult completeUpdate(StackResource resource) {
+ return ReplacementCleanup.complete(resource, this::delete);
+ }
+
+ @Override
+ public void clearUpdate(StackResource resource) {
+ ReplacementCleanup.clear(resource);
+ }
+
+ @Override
+ public boolean rollbackUpdate(StackResource resource) {
+ return ReplacementCleanup.rollback(resource, this::delete);
+ }
+
+ private void provisionNamespace(StackResource r, JsonNode props, ProvisionContext ctx) {
+ String name = ctx.stablePhysicalName(ctx.resolveOptional(props, "NamespaceName"),
+ r.getLogicalId(), NAME_MAX_LENGTH, true);
+ String adminUsername = ctx.resolveOptional(props, "AdminUsername");
+ String adminUserPassword = ctx.resolveOptional(props, "AdminUserPassword");
+ String kmsKeyId = ctx.resolveOptional(props, "KmsKeyId");
+ String defaultIamRoleArn = ctx.resolveOptional(props, "DefaultIamRoleArn");
+ List<String> iamRoles = ctx.resolveStringList(props, "IamRoles");
+ List<String> logExports = ctx.resolveStringList(props, "LogExports");
+ Map<String, String> tags = ctx.resolveTags(props, "Tags");
+
+ Namespace namespace;
+ if (ctx.reusesPriorEntity(name)) {
+ String dbName = ctx.resolveOptional(props, "DbName");
+ String currentDbName = service.getNamespace(name, ctx.region()).getDbName();
+ if (dbName != null && !dbName.equals(currentDbName)) {
+ throw new AwsException("ValidationException",
+ "DbName cannot be changed in place; it is fixed when the namespace is created, so "
+ + "remove the namespace from the template and add it back",
+ 400);
+ }
+ namespace = service.reconcileNamespace(name, adminUsername, adminUserPassword, kmsKeyId,
+ defaultIamRoleArn, iamRoles, logExports, ctx.region());
+ reconcileTags(namespace.getNamespaceArn(), tags, ctx);
+ } else {
+ namespace = service.createNamespace(name, adminUsername, adminUserPassword,
+ ctx.resolveOptional(props, "DbName"), kmsKeyId, defaultIamRoleArn, iamRoles, logExports, tags,
+ ctx.region());
+ }
+
+ r.setPhysicalId(name);
+ r.getAttributes().put("Namespace.NamespaceName", namespace.getNamespaceName());
+ r.getAttributes().put("Namespace.NamespaceId", namespace.getNamespaceId());
+ r.getAttributes().put("Namespace.NamespaceArn", namespace.getNamespaceArn());
+ r.getAttributes().put("Namespace.DbName", namespace.getDbName());
+ r.getAttributes().put("Namespace.KmsKeyId", namespace.getKmsKeyId());
+ r.getAttributes().put("Namespace.Status", namespace.getStatus());
+ r.getAttributes().put("Namespace.CreationDate", namespace.getCreationDate().toString());
+ r.getAttributes().put("Namespace.IamRoles", String.join(",", namespace.getIamRoles()));
+ r.getAttributes().put("Namespace.LogExports", String.join(",", namespace.getLogExports()));
+ if (namespace.getAdminUsername() != null) {
+ r.getAttributes().put("Namespace.AdminUsername", namespace.getAdminUsername());
+ }
+ if (namespace.getDefaultIamRoleArn() != null) {
+ r.getAttributes().put("Namespace.DefaultIamRoleArn", namespace.getDefaultIamRoleArn());
+ } else {
+ // An update carries the prior attributes forward, so a role the template dropped must be removed.
+ r.getAttributes().remove("Namespace.DefaultIamRoleArn");
+ }
+ }
+
+ private void provisionWorkgroup(StackResource r, JsonNode props, ProvisionContext ctx) {
+ for (String unsupported : UNSUPPORTED_WORKGROUP_PROPERTIES) {
+ if (ctx.resolveOptional(props, unsupported) != null) {
+ LOG.warnv("{0} is not supported on AWS::RedshiftServerless::Workgroup {1}; restoring from a "
+ + "snapshot is not emulated and the property is ignored", unsupported, r.getLogicalId());
+ }
+ }
+ String name = ctx.stablePhysicalName(ctx.resolveOptional(props, "WorkgroupName"),
+ r.getLogicalId(), NAME_MAX_LENGTH, true);
+ String namespaceName = ctx.resolveOptional(props, "NamespaceName");
+ WorkgroupSettings settings = settings(props, ctx);
+ Map<String, String> tags = ctx.resolveTags(props, "Tags");
+
+ Workgroup workgroup;
+ if (ctx.reusesPriorEntity(name)) {
+ Workgroup current = service.getWorkgroup(name, ctx.region());
+ if (namespaceName != null && !namespaceName.equals(current.getNamespaceName())) {
+ throw new AwsException("ValidationException",
+ "NamespaceName cannot be changed in place; a workgroup belongs to one namespace for its "
+ + "lifetime, so remove the workgroup from the template and add it back",
+ 400);
+ }
+ workgroup = service.updateWorkgroup(name, settings, ctx.region());
+ reconcileTags(workgroup.getWorkgroupArn(), tags, ctx);
+ } else {
+ workgroup = service.createWorkgroup(name, namespaceName, settings, tags, ctx.region());
+ }
+
+ r.setPhysicalId(name);
+ r.getAttributes().put("Workgroup.WorkgroupName", workgroup.getWorkgroupName());
+ r.getAttributes().put("Workgroup.WorkgroupId", workgroup.getWorkgroupId());
+ r.getAttributes().put("Workgroup.WorkgroupArn", workgroup.getWorkgroupArn());
+ r.getAttributes().put("Workgroup.NamespaceName", workgroup.getNamespaceName());
+ r.getAttributes().put("Workgroup.Status", workgroup.getStatus());
+ r.getAttributes().put("Workgroup.BaseCapacity", String.valueOf(workgroup.getBaseCapacity()));
+ r.getAttributes().put("Workgroup.EnhancedVpcRouting", String.valueOf(workgroup.isEnhancedVpcRouting()));
+ r.getAttributes().put("Workgroup.PubliclyAccessible", String.valueOf(workgroup.isPubliclyAccessible()));
+ r.getAttributes().put("Workgroup.TrackName", workgroup.getTrackName());
+ r.getAttributes().put("Workgroup.CreationDate", workgroup.getCreationDate().toString());
+ r.getAttributes().put("Workgroup.SecurityGroupIds", String.join(",", workgroup.getSecurityGroupIds()));
+ r.getAttributes().put("Workgroup.SubnetIds", String.join(",", workgroup.getSubnetIds()));
+ r.getAttributes().put("Workgroup.ConfigParameters", configParametersJson(workgroup.getConfigParameters()));
+ r.getAttributes().put("Workgroup.Endpoint.Address", workgroup.getEndpoint().getAddress());
+ r.getAttributes().put("Workgroup.Endpoint.Port", String.valueOf(workgroup.getEndpoint().getPort()));
+ if (workgroup.getMaxCapacity() != null) {
+ r.getAttributes().put("Workgroup.MaxCapacity", String.valueOf(workgroup.getMaxCapacity()));
+ }
+ }
+
+ private static String configParametersJson(List<ConfigParameter> parameters) {
+ ArrayNode array = MAPPER.createArrayNode();
+ for (ConfigParameter parameter : parameters) {
+ array.addObject()
+ .put("ParameterKey", parameter.getParameterKey())
+ .put("ParameterValue", parameter.getParameterValue());
+ }
+ return array.toString();
+ }
+
+ private static JsonNode parseConfigParametersAttribute(String text) {
+ try {
+ JsonNode parsed = MAPPER.readTree(text);
+ if (parsed != null && parsed.isArray()) {
+ return parsed;
+ }
+ } catch (JsonProcessingException e) {
+ LOG.debugv(e, "ConfigParameters is not valid JSON: {0}", text);
+ }
+ throw new AwsException("ValidationException",
+ "ConfigParameters must be a list of ParameterKey/ParameterValue pairs, got: " + text, 400);
+ }
+
+ /**
+ * CloudFormation drives a resource to the template, so a property the template omits means its
+ * default, not "keep what is stored". Every setting that has a default is therefore sent with it,
+ * which also resets a value an earlier template set. {@code MaxCapacity} has no default and the
+ * API cannot unset it, so removing it from a template leaves the stored value.
+ */
+ private WorkgroupSettings settings(JsonNode props, ProvisionContext ctx) {
+ List<ConfigParameter> configParameters = new ArrayList<>();
+ JsonNode configNode = props != null ? ctx.engine().resolveNode(props.get("ConfigParameters")) : null;
+ if (configNode != null && configNode.isTextual() && !configNode.textValue().isBlank()) {
+ configNode = parseConfigParametersAttribute(configNode.textValue());
+ }
+ if (configNode != null && configNode.isArray()) {
+ for (JsonNode entry : configNode) {
+ configParameters.add(new ConfigParameter(ctx.engine().resolve(entry.path("ParameterKey")),
+ ctx.engine().resolve(entry.path("ParameterValue"))));
+ }
+ }
+ PricePerformanceTarget target = null;
+ JsonNode targetNode = props != null ? ctx.engine().resolveNode(props.get("PricePerformanceTarget")) : null;
+ if (targetNode != null && targetNode.isObject()) {
+ String level = ctx.engine().resolve(targetNode.path("Level"));
+ target = new PricePerformanceTarget(ctx.engine().resolve(targetNode.path("Status")),
+ level == null || level.isBlank() ? null : Integer.valueOf(level.trim()));
+ } else {
+ target = new PricePerformanceTarget("DISABLED", null);
+ }
+ return new WorkgroupSettings(
+ orDefault(integer(props, "BaseCapacity", ctx), RedshiftServerlessService.DEFAULT_BASE_CAPACITY),
+ integer(props, "MaxCapacity", ctx),
+ orDefault(bool(props, "EnhancedVpcRouting", ctx), Boolean.FALSE),
+ orDefault(bool(props, "PubliclyAccessible", ctx), Boolean.FALSE),
+ null,
+ configParameters,
+ ctx.resolveStringList(props, "SecurityGroupIds"),
+ ctx.resolveStringList(props, "SubnetIds"),
+ orDefault(integer(props, "Port", ctx), RedshiftServerlessService.DEFAULT_PORT),
+ target,
+ null,
+ ctx.resolveOrDefault(props, "TrackName", RedshiftServerlessService.DEFAULT_TRACK_NAME));
+ }
+
+ private static <T> T orDefault(T value, T fallback) {
+ return value != null ? value : fallback;
+ }
+
+ private static Integer integer(JsonNode props, String name, ProvisionContext ctx) {
+ String value = ctx.resolveOptional(props, name);
+ if (value == null || value.isBlank()) {
+ return null;
+ }
+ try {
+ return Integer.valueOf(value.trim());
+ } catch (NumberFormatException e) {
+ throw new AwsException("ValidationException", name + " must be an integer, got: " + value, 400);
+ }
+ }
+
+ private static Boolean bool(JsonNode props, String name, ProvisionContext ctx) {
+ String value = ctx.resolveOptional(props, name);
+ return value == null || value.isBlank() ? null : Boolean.valueOf(value.trim());
+ }
+
+ /** Drives the stored tags to the template's, since TagResource alone never removes a key. */
+ private void reconcileTags(String resourceArn, Map<String, String> desired, ProvisionContext ctx) {
+ Map<String, String> current = service.listTagsForResource(resourceArn, ctx.region());
+ List<String> stale = ProvisionContext.staleTagKeys(current, desired);
+ if (!stale.isEmpty()) {
+ service.untagResource(resourceArn, stale, ctx.region());
+ }
+ if (!desired.isEmpty()) {
+ service.tagResource(resourceArn, desired, ctx.region());
+ }
+ }
+}
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 fca7dcf4bb..798e321d93 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
@@ -1574,12 +1574,31 @@ public class RedshiftService {
}
}
- private void releaseProxyPort(int port) {
+ public void releaseProxyPort(int port) {
if (port > 0) {
usedPorts.remove(port);
}
}
+ /**
+ * Hands out a proxy port from the shared pool. Redshift Serverless workgroups draw from the same
+ * range as provisioned clusters, so both must reserve through this service or they could be given
+ * the same port.
+ */
+ public int reserveProxyPort() {
+ return allocateProxyPort();
+ }
+
+ /** Reserves a specific port, e.g. one persisted before a restart. Returns false if it is taken. */
+ public boolean reserveProxyPort(int port) {
+ return port > 0 && usedPorts.add(port);
+ }
+
+ /** The address and port clients use to reach a proxy bound to {@code proxyPort}. */
+ public Endpoint advertisedEndpoint(int proxyPort) {
+ return proxyEndpoint(proxyPort);
+ }
+
private Endpoint proxyEndpoint(int proxyPort) {
String host = config.services().redshift().endpointHost()
.filter(h -> !h.isBlank())
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java
index 4a8d1f1e22..f79294160f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java
@@ -38,6 +38,7 @@ import java.util.concurrent.TimeUnit;
public class RedshiftContainerManager {
private static final Logger LOG = Logger.getLogger(RedshiftContainerManager.class);
+ private static final String DEFAULT_DATABASE = "dev";
private final ContainerBuilder containerBuilder;
private final ContainerLifecycleManager lifecycleManager;
@@ -60,6 +61,12 @@ public class RedshiftContainerManager {
}
public RedshiftContainerHandle start(String accountId, String clusterIdentifier, String masterUsername, String masterPassword) {
+ return start(accountId, clusterIdentifier, masterUsername, masterPassword, DEFAULT_DATABASE);
+ }
+
+ /** Starts the container with {@code databaseName} as its one database, instead of the default {@code dev}. */
+ public RedshiftContainerHandle start(String accountId, String clusterIdentifier, String masterUsername,
+ String masterPassword, String databaseName) {
String image = config.services().redshift().imageVersion();
String containerName = containerName(accountId, clusterIdentifier);
// Only the current name: no volume backs this container, so removing a legacy-named survivor
@@ -69,7 +76,7 @@ public class RedshiftContainerManager {
List<String> envVars = List.of(
"POSTGRES_USER=" + masterUsername,
"POSTGRES_PASSWORD=" + masterPassword,
- "POSTGRES_DB=dev"
+ "POSTGRES_DB=" + databaseName
);
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
@@ -88,7 +95,8 @@ public class RedshiftContainerManager {
ContainerSpec spec = specBuilder.build();
ContainerInfo info = lifecycleManager.createAndStart(spec);
- return initializeAndRegisterHandle(info, accountId, clusterIdentifier, masterUsername, containerName, enginePort);
+ return initializeAndRegisterHandle(info, accountId, clusterIdentifier, masterUsername, containerName, enginePort,
+ databaseName);
}
/**
@@ -102,19 +110,26 @@ public class RedshiftContainerManager {
* project's decision not to back Redshift containers with a Docker volume.
*/
public RedshiftContainerHandle adoptOrStart(String accountId, String clusterIdentifier, String masterUsername, String masterPassword) {
+ return adoptOrStart(accountId, clusterIdentifier, masterUsername, masterPassword, DEFAULT_DATABASE);
+ }
+
+ /** As {@link #adoptOrStart(String, String, String, String)} for a container whose database is not {@code dev}. */
+ public RedshiftContainerHandle adoptOrStart(String accountId, String clusterIdentifier, String masterUsername,
+ String masterPassword, String databaseName) {
String containerName = containerName(accountId, clusterIdentifier);
Optional<Container> existing = lifecycleManager.findByName(containerName);
if (existing.isEmpty()) {
LOG.warnv("No surviving container for cluster {0}; starting a fresh empty one: the previous"
+ " contents are not recoverable (no Docker volume backs Redshift containers)", clusterIdentifier);
- return start(accountId, clusterIdentifier, masterUsername, masterPassword);
+ return start(accountId, clusterIdentifier, masterUsername, masterPassword, databaseName);
}
LOG.infov("Adopting existing container {0} for cluster {1} to avoid discarding its data",
containerName, clusterIdentifier);
int enginePort = 5432;
ContainerInfo info = lifecycleManager.adopt(existing.get().getId(), List.of(enginePort));
- return initializeAndRegisterHandle(info, accountId, clusterIdentifier, masterUsername, containerName, enginePort);
+ return initializeAndRegisterHandle(info, accountId, clusterIdentifier, masterUsername, containerName, enginePort,
+ databaseName);
}
private RedshiftContainerHandle initializeAndRegisterHandle(
@@ -123,7 +138,8 @@ public class RedshiftContainerManager {
String clusterIdentifier,
String masterUsername,
String containerName,
- int enginePort) {
+ int enginePort,
+ String databaseName) {
EndpointInfo endpoint = info.getEndpoint(enginePort);
RedshiftContainerHandle handle = new RedshiftContainerHandle(
info.containerId(), clusterIdentifier, endpoint.host(), endpoint.port());
@@ -140,8 +156,8 @@ public class RedshiftContainerManager {
LOG.warnv("Failed to stream logs for {0}", containerName);
}
- waitForReady(containerName, info.containerId(), masterUsername, "dev");
- bootstrapCatalog(info.containerId(), masterUsername, "dev");
+ waitForReady(containerName, info.containerId(), masterUsername, databaseName);
+ bootstrapCatalog(info.containerId(), masterUsername, databaseName);
containers.put(containerKey(accountId, clusterIdentifier), handle);
return handle;
@@ -200,6 +216,12 @@ public class RedshiftContainerManager {
}
public void alterUserPassword(String accountId, String clusterIdentifier, String username, String newPassword) {
+ alterUserPassword(accountId, clusterIdentifier, username, newPassword, DEFAULT_DATABASE);
+ }
+
+ /** As the four-argument form, connecting to {@code databaseName} instead of {@code dev}. */
+ public void alterUserPassword(String accountId, String clusterIdentifier, String username, String newPassword,
+ String databaseName) {
RedshiftContainerHandle handle = containers.get(containerKey(accountId, clusterIdentifier));
if (handle == null) {
throw new AwsException("ClusterNotFound", "Cluster container for " + clusterIdentifier + " not found", 404);
@@ -234,7 +256,7 @@ public class RedshiftContainerManager {
}
}
String sql = "ALTER USER " + effectiveUser + " PASSWORD '" + newPassword + "'";
- String[] cmd = new String[]{"psql", "-U", effectiveUser, "-d", "dev", "-c", sql};
+ String[] cmd = new String[]{"psql", "-U", effectiveUser, "-d", databaseName, "-c", sql};
try {
ContainerExec.Result result = execInContainer(handle.getContainerId(), cmd, 15);
if (result.exitCode() != 0) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java
index 92fafa397f..e2029564d5 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.redshift.RedshiftCredentialBroker;
import io.github.hectorvent.floci.services.redshift.RedshiftService;
import io.github.hectorvent.floci.services.redshift.model.Cluster;
+import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessService;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
import jakarta.enterprise.context.ApplicationScoped;
@@ -24,25 +25,28 @@ class RedshiftDataResourceResolver {
private final ObjectMapper objectMapper;
private final RedshiftCredentialBroker credentialBroker;
private final RegionResolver regionResolver;
+ private final RedshiftServerlessService serverlessService;
@Inject
RedshiftDataResourceResolver(RedshiftService redshiftService,
SecretsManagerService secretsManagerService,
ObjectMapper objectMapper,
RedshiftCredentialBroker credentialBroker,
- RegionResolver regionResolver) {
+ RegionResolver regionResolver,
+ RedshiftServerlessService serverlessService) {
this.redshiftService = redshiftService;
this.secretsManagerService = secretsManagerService;
this.objectMapper = objectMapper;
this.credentialBroker = credentialBroker;
this.regionResolver = regionResolver;
+ this.serverlessService = serverlessService;
}
DatabaseTarget resolve(JsonNode request, String region) {
+ String database = requiredText(request, "Database");
if (hasText(request, "WorkgroupName")) {
- throw validation("Redshift Serverless (WorkgroupName) is not emulated by Floci.");
+ return resolveWorkgroup(request, region, database);
}
- String database = requiredText(request, "Database");
if (hasText(request, "SecretArn")) {
return resolveViaSecret(request, region, database);
@@ -50,18 +54,32 @@ class RedshiftDataResourceResolver {
return resolveViaDbUser(request, database);
}
- private DatabaseTarget resolveViaSecret(JsonNode request, String region, String database) {
- String secretArn = request.get("SecretArn").asText();
- try {
- AwsArnUtils.Arn arn = AwsArnUtils.parse(secretArn);
- if (region != null && !region.isBlank() && !region.equals(arn.region())) {
- throw validation("SecretArn is outside the request region.");
- }
- } catch (IllegalArgumentException e) {
- throw validation("SecretArn is not a valid ARN: " + secretArn);
+ /**
+ * A workgroup is reached either with a secret holding credentials, or as an IAM identity whose
+ * database user AWS derives from the signing identity. Floci connects the IAM path as the
+ * namespace admin, the same stand-in GetClusterCredentialsWithIAM uses for a cluster.
+ */
+ private DatabaseTarget resolveWorkgroup(JsonNode request, String region, String database) {
+ if (hasText(request, "ClusterIdentifier")) {
+ throw validation("ClusterIdentifier and WorkgroupName cannot both be specified.");
}
- String clusterId = requiredText(request, "ClusterIdentifier");
- Cluster cluster = cluster(clusterId);
+ RedshiftServerlessService.WorkgroupTarget workgroup =
+ serverlessService.getWorkgroupTarget(request.get("WorkgroupName").asText(), region);
+ if (!database.equals(workgroup.database())) {
+ throw validation("Database " + database + " does not exist in workgroup " + workgroup.workgroupName()
+ + "; the only database is " + workgroup.database() + ".");
+ }
+ if (!hasText(request, "SecretArn")) {
+ return new DatabaseTarget(workgroup.arn(), workgroup.host(), workgroup.port(), database,
+ workgroup.masterUsername(), workgroup.masterPassword());
+ }
+ Credentials creds = secretCredentials(request.get("SecretArn").asText(), region);
+ return new DatabaseTarget(workgroup.arn(), workgroup.host(), workgroup.port(), database,
+ creds.username(), creds.password());
+ }
+
+ private Credentials secretCredentials(String secretArn, String region) {
+ validateSecretRegion(secretArn, region);
SecretVersion secret;
try {
secret = secretsManagerService.getSecretValue(secretArn, null, null, region);
@@ -72,6 +90,26 @@ class RedshiftDataResourceResolver {
if (creds == null) {
throw validation("Secret " + secretArn + " does not contain username and password fields.");
}
+ return creds;
+ }
+
+ private static void validateSecretRegion(String secretArn, String region) {
+ try {
+ AwsArnUtils.Arn arn = AwsArnUtils.parse(secretArn);
+ if (region != null && !region.isBlank() && !region.equals(arn.region())) {
+ throw validation("SecretArn is outside the request region.");
+ }
+ } catch (IllegalArgumentException e) {
+ throw validation("SecretArn is not a valid ARN: " + secretArn);
+ }
+ }
+
+ private DatabaseTarget resolveViaSecret(JsonNode request, String region, String database) {
+ String secretArn = request.get("SecretArn").asText();
+ validateSecretRegion(secretArn, region);
+ String clusterId = requiredText(request, "ClusterIdentifier");
+ Cluster cluster = cluster(clusterId);
+ Credentials creds = secretCredentials(secretArn, region);
return target(clusterArn(clusterId, region), cluster, database, creds.username(), creds.password());
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java
index ff1492939b..5c0382d39a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java
@@ -63,6 +63,7 @@ public class RedshiftDataService implements Resettable {
stored.sql = sql;
stored.sqls = List.of(sql);
stored.clusterIdentifier = clusterIdentifierOrNull(request);
+ stored.workgroupName = workgroupNameOrNull(request);
stored.dbUser = target.user();
stored.database = target.database();
stored.resultFormat = request.path("ResultFormat").asText("JSON");
@@ -133,7 +134,7 @@ public class RedshiftDataService implements Resettable {
response.put("Id", stored.id);
if (stored.clusterIdentifier != null) {
response.put("ClusterIdentifier", stored.clusterIdentifier);
- } else {
+ } else if (stored.workgroupName == null) {
response.putNull("ClusterIdentifier");
}
response.put("CreatedAt", epochSeconds(stored.createdAt));
@@ -142,7 +143,7 @@ public class RedshiftDataService implements Resettable {
response.put("DbUser", stored.dbUser);
}
response.putArray("DbGroups");
- response.putNull("WorkgroupName");
+ putWorkgroupName(response, stored);
response.put("HasResultSet", stored.hasResultSet);
response.putNull("SessionId");
return response;
@@ -175,7 +176,7 @@ public class RedshiftDataService implements Resettable {
if (stored.dbUser != null) {
response.put("DbUser", stored.dbUser);
}
- response.putNull("WorkgroupName");
+ putWorkgroupName(response, stored);
response.put("ResultFormat", stored.resultFormat);
if (stored.batch && stored.subStatements != null) {
ArrayNode subs = response.putArray("SubStatements");
@@ -300,6 +301,7 @@ public class RedshiftDataService implements Resettable {
parent.sql = String.join("; ", sqls);
parent.sqls = sqls;
parent.clusterIdentifier = clusterIdentifierOrNull(request);
+ parent.workgroupName = workgroupNameOrNull(request);
parent.dbUser = target.user();
parent.database = target.database();
parent.resultFormat = request.path("ResultFormat").asText("JSON");
@@ -315,6 +317,7 @@ public class RedshiftDataService implements Resettable {
sub.sql = sqls.get(n);
sub.sqls = List.of(sqls.get(n));
sub.clusterIdentifier = parent.clusterIdentifier;
+ sub.workgroupName = parent.workgroupName;
sub.database = parent.database;
sub.dbUser = parent.dbUser;
sub.resultFormat = parent.resultFormat;
@@ -672,6 +675,19 @@ public class RedshiftDataService implements Resettable {
return textOrNull(request, "ClusterIdentifier");
}
+ private String workgroupNameOrNull(JsonNode request) {
+ return textOrNull(request, "WorkgroupName");
+ }
+
+ /** A cluster statement keeps the null member it always had; a workgroup statement names its workgroup. */
+ private static void putWorkgroupName(ObjectNode response, RedshiftDataStatementStore.StoredStatement stored) {
+ if (stored.workgroupName != null) {
+ response.put("WorkgroupName", stored.workgroupName);
+ } else {
+ response.putNull("WorkgroupName");
+ }
+ }
+
private static String requiredText(JsonNode request, String name) {
String value = textOrNull(request, name);
if (value == null || value.isBlank()) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStore.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStore.java
index a88273791c..41bbaf8f9d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStore.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataStatementStore.java
@@ -116,6 +116,8 @@ class RedshiftDataStatementStore {
boolean batch;
String statementName;
String clusterIdentifier;
+ /** Set instead of {@code clusterIdentifier} for a statement run against a serverless workgroup. */
+ String workgroupName;
String database;
String dbUser;
String resultFormat = "JSON";
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpoints.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpoints.java
new file mode 100644
index 0000000000..d36bc82fc3
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpoints.java
@@ -0,0 +1,43 @@
+package io.github.hectorvent.floci.services.redshiftserverless;
+
+import io.github.hectorvent.floci.services.redshift.RedshiftService;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+
+/**
+ * Allocates the endpoint advertised for a workgroup from the pool Redshift proxies share, so that a
+ * workgroup and a provisioned cluster can never be given the same port.
+ */
+@ApplicationScoped
+public class RedshiftServerlessEndpoints {
+
+ private final RedshiftService redshiftService;
+
+ @Inject
+ public RedshiftServerlessEndpoints(RedshiftService redshiftService) {
+ this.redshiftService = redshiftService;
+ }
+
+ public Endpoint allocate() {
+ return redshiftService.advertisedEndpoint(redshiftService.reserveProxyPort());
+ }
+
+ /**
+ * Re-reserves a persisted endpoint after a restart and rebuilds its address from the current
+ * configuration, so a changed {@code endpoint-host} is picked up. Allocates a fresh endpoint if
+ * the persisted port is taken.
+ */
+ public Endpoint restore(Endpoint persisted) {
+ if (persisted != null && redshiftService.reserveProxyPort(persisted.getPort())) {
+ return redshiftService.advertisedEndpoint(persisted.getPort());
+ }
+ return allocate();
+ }
+
+ public void release(Endpoint endpoint) {
+ if (endpoint != null) {
+ redshiftService.releaseProxyPort(endpoint.getPort());
+ }
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java
index 6a7caca238..50e655832c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java
@@ -8,7 +8,12 @@ import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
import io.github.hectorvent.floci.core.common.PaginatedResult;
+import io.github.hectorvent.floci.services.redshift.RedshiftIamDbUserResolver;
+import io.github.hectorvent.floci.services.redshift.TempCredential;
+import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
+import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
+import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
@@ -43,14 +48,21 @@ public class RedshiftServerlessJsonHandler {
private final RedshiftServerlessService service;
private final ObjectMapper objectMapper;
+ private final RedshiftIamDbUserResolver iamDbUserResolver;
@Inject
- public RedshiftServerlessJsonHandler(RedshiftServerlessService service, ObjectMapper objectMapper) {
+ public RedshiftServerlessJsonHandler(RedshiftServerlessService service, ObjectMapper objectMapper,
+ RedshiftIamDbUserResolver iamDbUserResolver) {
this.service = service;
this.objectMapper = objectMapper;
+ this.iamDbUserResolver = iamDbUserResolver;
}
public Response handle(String action, JsonNode request, String region) {
+ return handle(action, request, region, null);
+ }
+
+ public Response handle(String action, JsonNode request, String region, String authorizationHeader) {
LOG.debugv("Redshift Serverless action: {0}", action);
try {
return switch (action) {
@@ -59,6 +71,12 @@ public class RedshiftServerlessJsonHandler {
case "ListNamespaces" -> handleListNamespaces(request, region);
case "UpdateNamespace" -> handleUpdateNamespace(request, region);
case "DeleteNamespace" -> handleDeleteNamespace(request, region);
+ case "CreateWorkgroup" -> handleCreateWorkgroup(request, region);
+ case "GetWorkgroup" -> handleGetWorkgroup(request, region);
+ case "ListWorkgroups" -> handleListWorkgroups(request, region);
+ case "UpdateWorkgroup" -> handleUpdateWorkgroup(request, region);
+ case "DeleteWorkgroup" -> handleDeleteWorkgroup(request, region);
+ case "GetCredentials" -> handleGetCredentials(request, region, authorizationHeader);
case "ListTagsForResource" -> handleListTagsForResource(request, region);
case "TagResource" -> handleTagResource(request, region);
case "UntagResource" -> handleUntagResource(request, region);
@@ -79,6 +97,7 @@ public class RedshiftServerlessJsonHandler {
Namespace namespace = service.createNamespace(
text(request, "namespaceName"),
text(request, "adminUsername"),
+ text(request, "adminUserPassword"),
text(request, "dbName"),
text(request, "kmsKeyId"),
text(request, "defaultIamRoleArn"),
@@ -109,6 +128,7 @@ public class RedshiftServerlessJsonHandler {
Namespace namespace = service.updateNamespace(
text(request, "namespaceName"),
text(request, "adminUsername"),
+ text(request, "adminUserPassword"),
text(request, "kmsKeyId"),
text(request, "defaultIamRoleArn"),
parseStringList(request.path("iamRoles"), "iamRoles"),
@@ -121,6 +141,64 @@ public class RedshiftServerlessJsonHandler {
return namespaceResponse(service.deleteNamespace(text(request, "namespaceName"), region));
}
+ private Response handleCreateWorkgroup(JsonNode request, String region) {
+ Workgroup workgroup = service.createWorkgroup(
+ text(request, "workgroupName"),
+ text(request, "namespaceName"),
+ parseWorkgroupSettings(request),
+ parseTagList(request.path("tags"), "tags"),
+ region);
+ return workgroupResponse(workgroup);
+ }
+
+ private Response handleGetWorkgroup(JsonNode request, String region) {
+ return workgroupResponse(service.getWorkgroup(text(request, "workgroupName"), region));
+ }
+
+ private Response handleListWorkgroups(JsonNode request, String region) {
+ PaginatedResult<Workgroup> page = service.listWorkgroups(
+ region, parseMaxResults(request), text(request, "nextToken"));
+ ObjectNode response = objectMapper.createObjectNode();
+ ArrayNode items = response.putArray("workgroups");
+ page.items().forEach(workgroup -> items.add(workgroupNode(workgroup)));
+ if (page.nextToken() != null) {
+ response.put("nextToken", page.nextToken());
+ }
+ return Response.ok(response).build();
+ }
+
+ private Response handleUpdateWorkgroup(JsonNode request, String region) {
+ return workgroupResponse(service.updateWorkgroup(
+ text(request, "workgroupName"), parseWorkgroupSettings(request), region));
+ }
+
+ private Response handleDeleteWorkgroup(JsonNode request, String region) {
+ return workgroupResponse(service.deleteWorkgroup(text(request, "workgroupName"), region));
+ }
+
+ /**
+ * {@code expiration} and {@code nextRefreshTime} carry no timestamp format trait in the API
+ * model, so they use the awsJson1.1 default of epoch seconds as a JSON number. Floci never
+ * refreshes an authorization early, so the refresh time is the expiry.
+ */
+ private Response handleGetCredentials(JsonNode request, String region, String authorizationHeader) {
+ if (request.hasNonNull("customDomainName")) {
+ throw validation("customDomainName is not supported; use workgroupName.");
+ }
+ TempCredential credential = service.getCredentials(
+ text(request, "workgroupName"),
+ text(request, "dbName"),
+ parseInteger(request, "durationSeconds"),
+ iamDbUserResolver.resolveDbUser(authorizationHeader),
+ region);
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("dbUser", credential.dbUser());
+ response.put("dbPassword", credential.password());
+ response.put("expiration", credential.expiresAt().getEpochSecond());
+ response.put("nextRefreshTime", credential.expiresAt().getEpochSecond());
+ return Response.ok(response).build();
+ }
+
private Response handleListTagsForResource(JsonNode request, String region) {
Map<String, String> tags = service.listTagsForResource(text(request, "resourceArn"), region);
ObjectNode response = objectMapper.createObjectNode();
@@ -182,6 +260,133 @@ public class RedshiftServerlessJsonHandler {
return node;
}
+ private Response workgroupResponse(Workgroup workgroup) {
+ ObjectNode response = objectMapper.createObjectNode();
+ response.set("workgroup", workgroupNode(workgroup));
+ return Response.ok(response).build();
+ }
+
+ /**
+ * Members the emulator does not model ({@code workgroupVersion}, {@code patchVersion},
+ * {@code crossAccountVpcs}, the custom domain members and {@code endpoint.vpcEndpoints}) are
+ * omitted rather than invented; all of them are optional in the API model.
+ */
+ private ObjectNode workgroupNode(Workgroup workgroup) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("workgroupName", workgroup.getWorkgroupName());
+ node.put("workgroupId", workgroup.getWorkgroupId());
+ node.put("workgroupArn", workgroup.getWorkgroupArn());
+ node.put("namespaceName", workgroup.getNamespaceName());
+ node.put("status", workgroup.getStatus());
+ if (workgroup.getBaseCapacity() != null) {
+ node.put("baseCapacity", workgroup.getBaseCapacity());
+ }
+ if (workgroup.getMaxCapacity() != null) {
+ node.put("maxCapacity", workgroup.getMaxCapacity());
+ }
+ node.put("enhancedVpcRouting", workgroup.isEnhancedVpcRouting());
+ node.put("publiclyAccessible", workgroup.isPubliclyAccessible());
+ node.put("extraComputeForAutomaticOptimization", workgroup.isExtraComputeForAutomaticOptimization());
+ node.put("port", workgroup.getPort());
+ node.put("ipAddressType", workgroup.getIpAddressType());
+ node.put("trackName", workgroup.getTrackName());
+ if (workgroup.getPendingTrackName() != null) {
+ node.put("pendingTrackName", workgroup.getPendingTrackName());
+ }
+ ArrayNode configParameters = node.putArray("configParameters");
+ for (ConfigParameter parameter : workgroup.getConfigParameters()) {
+ ObjectNode entry = configParameters.addObject();
+ entry.put("parameterKey", parameter.getParameterKey());
+ entry.put("parameterValue", parameter.getParameterValue());
+ }
+ ArrayNode securityGroupIds = node.putArray("securityGroupIds");
+ workgroup.getSecurityGroupIds().forEach(securityGroupIds::add);
+ ArrayNode subnetIds = node.putArray("subnetIds");
+ workgroup.getSubnetIds().forEach(subnetIds::add);
+ PricePerformanceTarget target = workgroup.getPricePerformanceTarget();
+ if (target != null) {
+ ObjectNode targetNode = node.putObject("pricePerformanceTarget");
+ targetNode.put("status", target.getStatus());
+ if (target.getLevel() != null) {
+ targetNode.put("level", target.getLevel());
+ }
+ }
+ if (workgroup.getEndpoint() != null) {
+ ObjectNode endpointNode = node.putObject("endpoint");
+ endpointNode.put("address", workgroup.getEndpoint().getAddress());
+ endpointNode.put("port", workgroup.getEndpoint().getPort());
+ }
+ if (workgroup.getCreationDate() != null) {
+ node.put("creationDate", CREATION_DATE_FORMAT.format(workgroup.getCreationDate()));
+ }
+ return node;
+ }
+
+ private WorkgroupSettings parseWorkgroupSettings(JsonNode request) {
+ return new WorkgroupSettings(
+ parseInteger(request, "baseCapacity"),
+ parseInteger(request, "maxCapacity"),
+ parseBoolean(request, "enhancedVpcRouting"),
+ parseBoolean(request, "publiclyAccessible"),
+ parseBoolean(request, "extraComputeForAutomaticOptimization"),
+ parseConfigParameters(request.path("configParameters")),
+ parseStringList(request.path("securityGroupIds"), "securityGroupIds"),
+ parseStringList(request.path("subnetIds"), "subnetIds"),
+ parseInteger(request, "port"),
+ parsePricePerformanceTarget(request.path("pricePerformanceTarget")),
+ text(request, "ipAddressType"),
+ text(request, "trackName"));
+ }
+
+ private Integer parseInteger(JsonNode request, String field) {
+ JsonNode node = request.path(field);
+ if (node.isMissingNode() || node.isNull()) {
+ return null;
+ }
+ if (!node.isIntegralNumber() || !node.canConvertToInt()) {
+ throw validation(field + " must be an integer.");
+ }
+ return node.asInt();
+ }
+
+ private Boolean parseBoolean(JsonNode request, String field) {
+ JsonNode node = request.path(field);
+ if (node.isMissingNode() || node.isNull()) {
+ return null;
+ }
+ if (!node.isBoolean()) {
+ throw validation(field + " must be a boolean.");
+ }
+ return node.asBoolean();
+ }
+
+ private List<ConfigParameter> parseConfigParameters(JsonNode node) {
+ if (node == null || node.isMissingNode() || node.isNull()) {
+ return null;
+ }
+ if (!node.isArray()) {
+ throw validation("configParameters must be an array of parameters.");
+ }
+ List<ConfigParameter> parameters = new ArrayList<>();
+ for (JsonNode element : node) {
+ if (!element.isObject()) {
+ throw validation("configParameters must contain only parameter objects.");
+ }
+ parameters.add(new ConfigParameter(text(element, "parameterKey"), text(element, "parameterValue")));
+ }
+ return parameters;
+ }
+
+ private PricePerformanceTarget parsePricePerformanceTarget(JsonNode node) {
+ if (node == null || node.isMissingNode() || node.isNull()) {
+ return null;
+ }
+ if (!node.isObject()) {
+ throw validation("pricePerformanceTarget must be an object.");
+ }
+ return new PricePerformanceTarget(text(node, "status"), parseInteger(node, "level"));
+ }
+
private Integer parseMaxResults(JsonNode request) {
JsonNode node = request.path("maxResults");
if (node.isMissingNode() || node.isNull()) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntime.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntime.java
new file mode 100644
index 0000000000..b1c7076ad8
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntime.java
@@ -0,0 +1,189 @@
+package io.github.hectorvent.floci.services.redshiftserverless;
+
+import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;
+import io.github.hectorvent.floci.services.redshift.RedshiftCredentialBroker;
+import io.github.hectorvent.floci.services.redshift.TempCredential;
+import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerHandle;
+import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerManager;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
+import io.github.hectorvent.floci.services.redshift.proxy.RedshiftProxyManager;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+import java.security.SecureRandom;
+import java.util.List;
+import java.util.Optional;
+import java.util.function.Supplier;
+
+/**
+ * Owns the PostgreSQL container and the auth proxy behind a workgroup endpoint.
+ *
+ * <p>The container, proxy and credential managers are keyed by an {@code (account, identifier)}
+ * string and know nothing about provisioned clusters, so a workgroup reuses them under the
+ * identifier {@code serverless_<region>_<workgroup>}. The underscore cannot appear in a cluster
+ * identifier, so the two never collide and the workgroup stays out of {@code DescribeClusters}.
+ */
+@ApplicationScoped
+public class RedshiftServerlessRuntime {
+
+ private static final Logger LOG = Logger.getLogger(RedshiftServerlessRuntime.class);
+ private static final String PASSWORD_ALPHABET =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+ private static final int GENERATED_PASSWORD_LENGTH = 32;
+
+ /** The namespace admin pair as it stands now; read on every login so a password change applies at once. */
+ public record AdminCredentials(String username, String password) {
+ }
+
+ /** Where the workgroup's PostgreSQL container listens. */
+ public record Backend(String host, int port) {
+ }
+
+ private final RedshiftContainerManager containerManager;
+ private final RedshiftProxyManager proxyManager;
+ private final RedshiftCredentialBroker credentialBroker;
+ private final SecureRandom random = new SecureRandom();
+
+ @Inject
+ public RedshiftServerlessRuntime(RedshiftContainerManager containerManager, RedshiftProxyManager proxyManager,
+ RedshiftCredentialBroker credentialBroker) {
+ this.containerManager = containerManager;
+ this.proxyManager = proxyManager;
+ this.credentialBroker = credentialBroker;
+ }
+
+ public static String backendId(String region, String workgroupName) {
+ return "serverless_" + region + "_" + workgroupName;
+ }
+
+ /**
+ * Starts (or, when {@code adopt} is set, re-attaches to) the container and fronts it with the
+ * auth proxy on {@code endpoint}. Anything started before a failure is torn down again, so a
+ * failed call leaves neither a container nor a bound port behind.
+ */
+ public Backend start(String accountId, String region, String workgroupName, String masterUsername,
+ String masterPassword, String dbName, Endpoint endpoint, boolean adopt,
+ Supplier<Optional<AdminCredentials>> admin) {
+ String backendId = backendId(region, workgroupName);
+ try {
+ RedshiftContainerHandle handle = adopt
+ ? containerManager.adoptOrStart(accountId, backendId, masterUsername, masterPassword, dbName)
+ : containerManager.start(accountId, backendId, masterUsername, masterPassword, dbName);
+ proxyManager.startProxy(relayKey(accountId, backendId), endpoint.getPort(),
+ handle.getHost(), handle.getPort(), endpoint.getAddress(),
+ masterUsername, masterPassword, dbName,
+ validator(accountId, backendId, admin), List.of());
+ return new Backend(handle.getHost(), handle.getPort());
+ } catch (RuntimeException e) {
+ rollback(accountId, backendId);
+ throw e;
+ }
+ }
+
+ /**
+ * Stops the proxy first and lets a failure propagate: if the listener cannot be closed the
+ * port must stay reserved and the workgroup must stay on record, so the caller can retry.
+ */
+ public void stop(String accountId, String region, String workgroupName) {
+ String backendId = backendId(region, workgroupName);
+ proxyManager.stopProxy(relayKey(accountId, backendId));
+ containerManager.stop(accountId, backendId);
+ credentialBroker.revokeCluster(accountId, backendId);
+ }
+
+ /**
+ * Applies a new master password to the PostgreSQL role and to the proxy's backend leg. When the
+ * proxy step fails the role is put back to {@code oldPassword}, so the backend never requires a
+ * password the stored namespace does not hold.
+ */
+ public void changeMasterPassword(String accountId, String region, String workgroupName,
+ String masterUsername, String dbName, String oldPassword,
+ String newPassword) {
+ String backendId = backendId(region, workgroupName);
+ containerManager.alterUserPassword(accountId, backendId, masterUsername, newPassword, dbName);
+ try {
+ proxyManager.updateMasterPassword(relayKey(accountId, backendId), newPassword);
+ } catch (RuntimeException e) {
+ if (oldPassword != null) {
+ try {
+ containerManager.alterUserPassword(accountId, backendId, masterUsername, oldPassword, dbName);
+ } catch (RuntimeException restoreFailure) {
+ e.addSuppressed(restoreFailure);
+ }
+ }
+ throw e;
+ }
+ }
+
+ /**
+ * Puts the role and the proxy back to {@code password} after a rotation the caller could not
+ * store. The proxy is only reset once the role is, so a failed role restore never leaves the two
+ * disagreeing; the failure propagates instead.
+ */
+ public void restoreMasterPassword(String accountId, String region, String workgroupName,
+ String masterUsername, String dbName, String password) {
+ String backendId = backendId(region, workgroupName);
+ containerManager.alterUserPassword(accountId, backendId, masterUsername, password, dbName);
+ proxyManager.updateMasterPassword(relayKey(accountId, backendId), password);
+ }
+
+ public TempCredential issueCredential(String accountId, String region, String workgroupName,
+ String dbUser, int durationSeconds) {
+ return credentialBroker.issue(accountId, backendId(region, workgroupName), dbUser, List.of(),
+ durationSeconds);
+ }
+
+ public String generatePassword() {
+ StringBuilder password = new StringBuilder(GENERATED_PASSWORD_LENGTH);
+ for (int i = 0; i < GENERATED_PASSWORD_LENGTH; i++) {
+ password.append(PASSWORD_ALPHABET.charAt(random.nextInt(PASSWORD_ALPHABET.length())));
+ }
+ return password.toString();
+ }
+
+ private void rollback(String accountId, String backendId) {
+ try {
+ proxyManager.stopProxy(relayKey(accountId, backendId));
+ } catch (RuntimeException e) {
+ LOG.warnv(e, "Could not stop the proxy while rolling back workgroup backend {0}", backendId);
+ }
+ try {
+ containerManager.stop(accountId, backendId);
+ } catch (RuntimeException e) {
+ LOG.warnv(e, "Could not remove the container while rolling back workgroup backend {0}", backendId);
+ }
+ credentialBroker.revokeCluster(accountId, backendId);
+ }
+
+ /**
+ * The namespace admin pair and any live {@code GetCredentials} credential run the backend leg as
+ * the master role, a known broker user with a stale password is refused, and everyone else is
+ * passed through for PostgreSQL to judge. A namespace that no longer exists vouches for nothing:
+ * falling through to the broker would classify the admin name as unknown and let the wire proxy
+ * open the backend as master.
+ */
+ private PasswordValidator validator(String accountId, String backendId,
+ Supplier<Optional<AdminCredentials>> admin) {
+ return (user, password) -> {
+ Optional<AdminCredentials> current = admin.get();
+ if (current.isEmpty()) {
+ return PasswordValidator.AuthResult.REJECT;
+ }
+ if (user.equals(current.get().username())) {
+ return password.equals(current.get().password())
+ ? PasswordValidator.AuthResult.MASTER_EQUIVALENT
+ : PasswordValidator.AuthResult.REJECT;
+ }
+ return switch (credentialBroker.classify(accountId, backendId, user, password)) {
+ case MASTER_EQUIVALENT -> PasswordValidator.AuthResult.MASTER_EQUIVALENT;
+ case REJECT -> PasswordValidator.AuthResult.REJECT;
+ case PASSTHROUGH -> PasswordValidator.AuthResult.PASSTHROUGH;
+ };
+ };
+ }
+
+ private static String relayKey(String accountId, String backendId) {
+ return accountId + ":" + backendId;
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java
index 8a352f4921..883ace9243 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java
@@ -8,21 +8,35 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.redshift.TempCredential;
+import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
+import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
+import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
+import io.quarkus.runtime.StartupEvent;
+import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
+import jakarta.interceptor.Interceptor;
+import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.UUID;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
import java.util.regex.Pattern;
@ApplicationScoped
public class RedshiftServerlessService implements Resettable {
+ private static final Logger LOG = Logger.getLogger(RedshiftServerlessService.class);
+
public static final String DEFAULT_DB_NAME = "dev";
public static final String AWS_OWNED_KMS_KEY = "AWS_OWNED_KMS_KEY";
@@ -64,19 +78,126 @@ public class RedshiftServerlessService implements Resettable {
"union", "unique", "unnest", "unpivot", "user", "using",
"verbose", "wallet", "when", "where", "with", "without");
+ public static final int DEFAULT_BASE_CAPACITY = 128;
+ public static final int DEFAULT_PORT = 5439;
+ public static final String DEFAULT_TRACK_NAME = "current";
+ public static final String DEFAULT_IP_ADDRESS_TYPE = "ipv4";
+ public static final String DEFAULT_ADMIN_USERNAME = "admin";
+ public static final int DEFAULT_CREDENTIAL_DURATION_SECONDS = 900;
+ public static final int MIN_CREDENTIAL_DURATION_SECONDS = 900;
+ public static final int MAX_CREDENTIAL_DURATION_SECONDS = 3600;
+
+ private static final Pattern WORKGROUP_NAME = Pattern.compile("[a-z0-9-]+");
+ private static final Pattern TRACK_NAME = Pattern.compile("[a-zA-Z0-9_]+");
+ private static final Set<String> IP_ADDRESS_TYPES = Set.of("ipv4", "dualstack");
+ private static final Set<String> PERFORMANCE_TARGET_STATUSES = Set.of("ENABLED", "DISABLED");
+
private final AccountAwareStorageBackend<Namespace> namespaces;
+ private final AccountAwareStorageBackend<Workgroup> workgroups;
private final RegionResolver regionResolver;
+ private final RedshiftServerlessEndpoints endpoints;
+ private final RedshiftServerlessRuntime runtime;
@Inject
- public RedshiftServerlessService(StorageFactory storageFactory, RegionResolver regionResolver) {
+ public RedshiftServerlessService(StorageFactory storageFactory, RegionResolver regionResolver,
+ RedshiftServerlessEndpoints endpoints, RedshiftServerlessRuntime runtime) {
this.namespaces = storageFactory.create("redshiftserverless", "redshiftserverless-namespaces.json",
new TypeReference<Map<String, Namespace>>() {});
+ this.workgroups = storageFactory.create("redshiftserverless", "redshiftserverless-workgroups.json",
+ new TypeReference<Map<String, Workgroup>>() {});
this.regionResolver = regionResolver;
+ this.endpoints = endpoints;
+ this.runtime = runtime;
+ }
+
+ /**
+ * A persisted workgroup keeps its endpoint across restarts. The port is claimed again in the
+ * shared pool; if a provisioned cluster already took it, the workgroup gets a new endpoint and the
+ * change is stored. The priority places this after the default-priority observers, which includes
+ * {@code RedshiftService}, so every persisted cluster has reserved its own port by then and the
+ * two services cannot be handed the same one.
+ */
+ void onStart(@Observes @Priority(Interceptor.Priority.APPLICATION + 600) StartupEvent event) {
+ for (AccountAwareStorageBackend.AccountEntry<Workgroup> entry : workgroups.scanAllAccountEntries(key -> true)) {
+ Workgroup workgroup = new Workgroup(entry.value());
+ workgroup.setEndpoint(endpoints.restore(workgroup.getEndpoint()));
+ recoverRuntime(entry.accountId(), regionOf(entry.key()), workgroup);
+ workgroups.putForAccount(entry.accountId(), entry.key(), workgroup);
+ }
+ }
+
+ /**
+ * Re-attaches to the workgroup's container, or starts a fresh one when it did not survive, and
+ * restarts the proxy on the persisted port. A failure is logged and the runtime coordinates are
+ * cleared rather than the status changed: the status enum has no failed member, and a Data API
+ * call against a workgroup with no runtime reports that clearly.
+ */
+ private void recoverRuntime(String accountId, String region, Workgroup workgroup) {
+ Optional<Namespace> namespace = namespaces.getForAccount(accountId,
+ storageKey(region, workgroup.getNamespaceName()));
+ if (namespace.isEmpty() || namespace.get().getAdminUserPassword() == null) {
+ clearRuntime(workgroup);
+ LOG.warnv("Workgroup {0} has no namespace admin credentials to restore its runtime with", workgroup.getWorkgroupName());
+ return;
+ }
+ try {
+ startRuntime(accountId, region, workgroup, namespace.get(), true);
+ } catch (RuntimeException e) {
+ clearRuntime(workgroup);
+ LOG.warnv(e, "Failed to recover the runtime of workgroup {0}", workgroup.getWorkgroupName());
+ }
+ }
+
+ private static void clearRuntime(Workgroup workgroup) {
+ workgroup.setRuntimeHost(null);
+ workgroup.setRuntimePort(0);
+ }
+
+ /**
+ * Starts the workgroup's container and proxy with the namespace admin pair and records where the
+ * container listens. The master role is the namespace admin user, or a role it was originally
+ * created with when the namespace was renamed since.
+ */
+ private void startRuntime(String accountId, String region, Workgroup workgroup, Namespace namespace,
+ boolean adopt) {
+ String masterUsername = workgroup.getMasterUsername() != null
+ ? workgroup.getMasterUsername() : adminUserOf(namespace);
+ RedshiftServerlessRuntime.Backend backend = runtime.start(accountId, region, workgroup.getWorkgroupName(),
+ masterUsername, namespace.getAdminUserPassword(), namespace.getDbName(), workgroup.getEndpoint(),
+ adopt, adminSupplier(accountId, region, namespace.getNamespaceName()));
+ workgroup.setMasterUsername(masterUsername);
+ workgroup.setRuntimeHost(backend.host());
+ workgroup.setRuntimePort(backend.port());
+ }
+
+ private Supplier<Optional<RedshiftServerlessRuntime.AdminCredentials>> adminSupplier(
+ String accountId, String region, String namespaceName) {
+ return () -> namespaces.getForAccount(accountId, storageKey(region, namespaceName))
+ .filter(namespace -> namespace.getAdminUserPassword() != null)
+ .map(namespace -> new RedshiftServerlessRuntime.AdminCredentials(
+ adminUserOf(namespace), namespace.getAdminUserPassword()));
+ }
+
+ private static String adminUserOf(Namespace namespace) {
+ String adminUsername = namespace.getAdminUsername();
+ return adminUsername == null || adminUsername.isBlank() ? DEFAULT_ADMIN_USERNAME : adminUsername;
+ }
+
+ private static String regionOf(String storageKey) {
+ return storageKey.substring(0, storageKey.indexOf("::"));
}
public synchronized Namespace createNamespace(String namespaceName, String adminUsername, String dbName,
String kmsKeyId, String defaultIamRoleArn, List<String> iamRoles,
List<String> logExports, Map<String, String> tags, String region) {
+ return createNamespace(namespaceName, adminUsername, null, dbName, kmsKeyId, defaultIamRoleArn, iamRoles,
+ logExports, tags, region);
+ }
+
+ public synchronized Namespace createNamespace(String namespaceName, String adminUsername,
+ String adminUserPassword, String dbName, String kmsKeyId,
+ String defaultIamRoleArn, List<String> iamRoles,
+ List<String> logExports, Map<String, String> tags, String region) {
validateNamespaceName(namespaceName);
String key = storageKey(region, namespaceName);
if (namespaces.get(key).isPresent()) {
@@ -90,6 +211,7 @@ public class RedshiftServerlessService implements Resettable {
namespace.setNamespaceArn(regionResolver.buildArn("redshift-serverless", region,
"namespace/" + namespace.getNamespaceId()));
namespace.setAdminUsername(adminUsername);
+ namespace.setAdminUserPassword(blankToNull(adminUserPassword));
namespace.setDbName(validateDbName(dbName));
namespace.setKmsKeyId(kmsKeyId == null || kmsKeyId.isBlank() ? AWS_OWNED_KMS_KEY : kmsKeyId);
namespace.setDefaultIamRoleArn(defaultIamRoleArn);
@@ -114,23 +236,60 @@ public class RedshiftServerlessService implements Resettable {
100, 100, "ValidationException");
}
+ public synchronized Namespace updateNamespace(String namespaceName, String adminUsername, String kmsKeyId,
+ String defaultIamRoleArn, List<String> iamRoles,
+ List<String> logExports, String region) {
+ return updateNamespace(namespaceName, adminUsername, null, kmsKeyId, defaultIamRoleArn, iamRoles,
+ logExports, region);
+ }
+
/**
- * Builds the new state on a copy and stores that, rather than mutating the stored instance.
- * Reads do not take the monitor this method holds, so an in-place mutation lets a concurrent
- * GetNamespace or ListNamespaces observe a torn object: some fields updated, some not.
+ * A new {@code adminUserPassword} is applied to the running workgroup backend last, once every
+ * other field has validated, and rolled back if the namespace cannot be stored. The backend
+ * therefore never holds a password the stored namespace does not.
*/
- public synchronized Namespace updateNamespace(String namespaceName, String adminUsername, String kmsKeyId,
+ public synchronized Namespace updateNamespace(String namespaceName, String adminUsername,
+ String adminUserPassword, String kmsKeyId,
String defaultIamRoleArn, List<String> iamRoles,
List<String> logExports, String region) {
+ return applyNamespaceUpdate(namespaceName, adminUsername, adminUserPassword, kmsKeyId,
+ defaultIamRoleArn, iamRoles, logExports, region, false);
+ }
+
+ /**
+ * As {@link #updateNamespace} for CloudFormation, which drives a namespace to the template: an
+ * omitted {@code kmsKeyId} returns to the AWS-owned key and an omitted {@code defaultIamRoleArn}
+ * is cleared, where the API call would keep the stored value.
+ */
+ public synchronized Namespace reconcileNamespace(String namespaceName, String adminUsername,
+ String adminUserPassword, String kmsKeyId,
+ String defaultIamRoleArn, List<String> iamRoles,
+ List<String> logExports, String region) {
+ return applyNamespaceUpdate(namespaceName, adminUsername, adminUserPassword, kmsKeyId,
+ defaultIamRoleArn, iamRoles, logExports, region, true);
+ }
+
+ /**
+ * Builds the new state on a copy and stores that, rather than mutating the stored instance.
+ * Reads do not take the monitor the callers hold, so an in-place mutation lets a concurrent
+ * GetNamespace or ListNamespaces observe a torn object: some fields updated, some not.
+ */
+ private Namespace applyNamespaceUpdate(String namespaceName, String adminUsername,
+ String adminUserPassword, String kmsKeyId,
+ String defaultIamRoleArn, List<String> iamRoles,
+ List<String> logExports, String region, boolean resetOmitted) {
String key = storageKey(region, namespaceName);
- Namespace updated = new Namespace(getNamespace(namespaceName, region));
+ Namespace current = getNamespace(namespaceName, region);
+ Namespace updated = new Namespace(current);
if (adminUsername != null) {
updated.setAdminUsername(adminUsername);
}
if (kmsKeyId != null && !kmsKeyId.isBlank()) {
updated.setKmsKeyId(kmsKeyId);
+ } else if (resetOmitted) {
+ updated.setKmsKeyId(AWS_OWNED_KMS_KEY);
}
- if (defaultIamRoleArn != null) {
+ if (defaultIamRoleArn != null || resetOmitted) {
updated.setDefaultIamRoleArn(defaultIamRoleArn);
}
if (iamRoles != null) {
@@ -139,60 +298,373 @@ public class RedshiftServerlessService implements Resettable {
if (logExports != null) {
updated.setLogExports(validateLogExports(logExports));
}
- namespaces.put(key, updated);
+ boolean rotated = adminUserPassword != null && !adminUserPassword.isBlank();
+ if (rotated) {
+ applyNewAdminPassword(current, adminUserPassword, region);
+ updated.setAdminUserPassword(adminUserPassword);
+ }
+ try {
+ namespaces.put(key, updated);
+ } catch (RuntimeException e) {
+ if (rotated) {
+ try {
+ restoreAdminPassword(current, region);
+ } catch (RuntimeException restoreFailure) {
+ LOG.errorv(restoreFailure, "Could not restore the admin password of namespace {0}; "
+ + "the backend may reject the stored password", namespaceName);
+ e.addSuppressed(restoreFailure);
+ }
+ }
+ throw e;
+ }
return updated;
}
public synchronized Namespace deleteNamespace(String namespaceName, String region) {
Namespace deleted = new Namespace(getNamespace(namespaceName, region));
+ // A workgroup cannot outlive its namespace; the model lists ConflictException for this call
+ // but not its trigger, so this guard is the single place to relax if AWS turns out to differ.
+ if (workgroupOf(namespaceName, region).isPresent()) {
+ throw new AwsException("ConflictException",
+ "The namespace " + namespaceName + " still has a workgroup. Delete the workgroup first.", 409);
+ }
namespaces.delete(storageKey(region, namespaceName));
deleted.setStatus("DELETING");
return deleted;
}
public Map<String, String> listTagsForResource(String resourceArn, String region) {
- return new LinkedHashMap<>(resolveByArn(resourceArn, region).getTags());
+ requireResourceArn(resourceArn);
+ Optional<Namespace> namespace = namespaceByArn(resourceArn, region);
+ if (namespace.isPresent()) {
+ return new LinkedHashMap<>(namespace.get().getTags());
+ }
+ return new LinkedHashMap<>(workgroupByArn(resourceArn, region).orElseThrow(
+ () -> resourceNotFound(resourceArn)).getTags());
}
public synchronized Map<String, String> tagResource(String resourceArn, Map<String, String> tags, String region) {
- Namespace updated = new Namespace(resolveByArn(resourceArn, region));
- if (tags != null) {
- updated.getTags().putAll(tags);
- }
- namespaces.put(storageKey(region, updated.getNamespaceName()), updated);
- return new LinkedHashMap<>(updated.getTags());
+ return retag(resourceArn, region, tagMap -> {
+ if (tags != null) {
+ tagMap.putAll(tags);
+ }
+ });
}
public synchronized Map<String, String> untagResource(String resourceArn, List<String> tagKeys, String region) {
- Namespace resolved = resolveByArn(resourceArn, region);
+ requireResourceArn(resourceArn);
+ if (namespaceByArn(resourceArn, region).isEmpty() && workgroupByArn(resourceArn, region).isEmpty()) {
+ throw resourceNotFound(resourceArn);
+ }
if (tagKeys == null) {
throw validation("tagKeys is required.");
}
- Namespace updated = new Namespace(resolved);
- tagKeys.forEach(updated.getTags()::remove);
- namespaces.put(storageKey(region, updated.getNamespaceName()), updated);
- return new LinkedHashMap<>(updated.getTags());
+ return retag(resourceArn, region, tagMap -> tagKeys.forEach(tagMap::remove));
}
/**
- * Redshift Serverless tags whatever the ARN names, so the lookup is by ARN rather than by
- * namespace name. Only namespaces are taggable in Floci, so any other Redshift Serverless ARN
- * resolves to nothing and is reported as absent rather than as an unsupported resource type.
+ * Redshift Serverless tags whatever the ARN names, so the lookup is by ARN rather than by name.
+ * Namespaces and workgroups are the taggable resources in Floci, so any other Redshift Serverless
+ * ARN resolves to nothing and is reported as absent rather than as an unsupported resource type.
+ * The change is applied to a copy and stored, never to the instance readers may hold.
*/
- private Namespace resolveByArn(String resourceArn, String region) {
- if (resourceArn == null || resourceArn.isBlank()) {
- throw validation("resourceArn is required.");
+ private Map<String, String> retag(String resourceArn, String region, Consumer<Map<String, String>> change) {
+ requireResourceArn(resourceArn);
+ Optional<Namespace> namespace = namespaceByArn(resourceArn, region);
+ if (namespace.isPresent()) {
+ Namespace updated = new Namespace(namespace.get());
+ change.accept(updated.getTags());
+ namespaces.put(storageKey(region, updated.getNamespaceName()), updated);
+ return new LinkedHashMap<>(updated.getTags());
}
+ Workgroup workgroup = workgroupByArn(resourceArn, region).orElseThrow(() -> resourceNotFound(resourceArn));
+ Workgroup updated = new Workgroup(workgroup);
+ change.accept(updated.getTags());
+ workgroups.put(storageKey(region, updated.getWorkgroupName()), updated);
+ return new LinkedHashMap<>(updated.getTags());
+ }
+
+ private Optional<Namespace> namespaceByArn(String resourceArn, String region) {
return namespaces.scan(key -> key.startsWith(region + "::")).stream()
.filter(namespace -> resourceArn.equals(namespace.getNamespaceArn()))
- .findFirst()
- .orElseThrow(() -> new AwsException("ResourceNotFoundException",
- "The resource " + resourceArn + " was not found.", 404));
+ .findFirst();
+ }
+
+ private Optional<Workgroup> workgroupByArn(String resourceArn, String region) {
+ return workgroups.scan(key -> key.startsWith(region + "::")).stream()
+ .filter(workgroup -> resourceArn.equals(workgroup.getWorkgroupArn()))
+ .findFirst();
+ }
+
+ private static void requireResourceArn(String resourceArn) {
+ if (resourceArn == null || resourceArn.isBlank()) {
+ throw validation("resourceArn is required.");
+ }
+ }
+
+ private static AwsException resourceNotFound(String resourceArn) {
+ return new AwsException("ResourceNotFoundException",
+ "The resource " + resourceArn + " was not found.", 404);
+ }
+
+ public synchronized Workgroup createWorkgroup(String workgroupName, String namespaceName,
+ WorkgroupSettings settings, Map<String, String> tags,
+ String region) {
+ validateWorkgroupName(workgroupName);
+ validateSettings(settings);
+ Namespace namespace = getNamespace(namespaceName, region);
+ String key = storageKey(region, workgroupName);
+ if (workgroups.get(key).isPresent()) {
+ throw new AwsException("ConflictException",
+ "The workgroup " + workgroupName + " already exists.", 409);
+ }
+ if (workgroupOf(namespaceName, region).isPresent()) {
+ throw new AwsException("ConflictException",
+ "The namespace " + namespaceName + " already has a workgroup.", 409);
+ }
+
+ Workgroup workgroup = new Workgroup();
+ workgroup.setWorkgroupName(workgroupName);
+ workgroup.setWorkgroupId(UUID.randomUUID().toString());
+ workgroup.setWorkgroupArn(regionResolver.buildArn("redshift-serverless", region,
+ "workgroup/" + workgroup.getWorkgroupId()));
+ workgroup.setNamespaceName(namespaceName);
+ workgroup.setBaseCapacity(orDefault(settings.baseCapacity(), DEFAULT_BASE_CAPACITY));
+ workgroup.setMaxCapacity(settings.maxCapacity());
+ workgroup.setEnhancedVpcRouting(Boolean.TRUE.equals(settings.enhancedVpcRouting()));
+ workgroup.setPubliclyAccessible(Boolean.TRUE.equals(settings.publiclyAccessible()));
+ workgroup.setExtraComputeForAutomaticOptimization(
+ Boolean.TRUE.equals(settings.extraComputeForAutomaticOptimization()));
+ workgroup.setConfigParameters(copyOf(settings.configParameters()));
+ workgroup.setSecurityGroupIds(copyOf(settings.securityGroupIds()));
+ workgroup.setSubnetIds(copyOf(settings.subnetIds()));
+ workgroup.setPort(orDefault(settings.port(), DEFAULT_PORT));
+ workgroup.setIpAddressType(orDefault(settings.ipAddressType(), DEFAULT_IP_ADDRESS_TYPE));
+ workgroup.setTrackName(orDefault(settings.trackName(), DEFAULT_TRACK_NAME));
+ workgroup.setPricePerformanceTarget(settings.pricePerformanceTarget() != null
+ ? settings.pricePerformanceTarget() : new PricePerformanceTarget("DISABLED", null));
+ workgroup.setStatus("AVAILABLE");
+ workgroup.setCreationDate(Instant.now());
+ workgroup.setTags(tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags));
+ workgroup.setEndpoint(endpoints.allocate());
+ try {
+ startRuntime(workgroups.accountId(), region, workgroup, ensureAdminPassword(namespace, region), false);
+ workgroups.put(key, workgroup);
+ } catch (RuntimeException e) {
+ // The runtime may have started before the failure (a failed put), so tear it down; a
+ // runtime that failed to start has already rolled itself back and this is then a no-op.
+ try {
+ runtime.stop(workgroups.accountId(), region, workgroupName);
+ } catch (RuntimeException stopFailure) {
+ LOG.warnv(stopFailure, "Could not stop the runtime of workgroup {0} after a failed create",
+ workgroupName);
+ e.addSuppressed(stopFailure);
+ }
+ endpoints.release(workgroup.getEndpoint());
+ throw e;
+ }
+ return workgroup;
+ }
+
+ public Workgroup getWorkgroup(String workgroupName, String region) {
+ validateWorkgroupName(workgroupName);
+ return workgroups.get(storageKey(region, workgroupName))
+ .orElseThrow(() -> workgroupNotFound(workgroupName));
+ }
+
+ public PaginatedResult<Workgroup> listWorkgroups(String region, Integer maxResults, String nextToken) {
+ List<Workgroup> all = workgroups.scan(key -> key.startsWith(region + "::"));
+ return Pagination.paginate(all, Workgroup::getWorkgroupName, maxResults, nextToken,
+ 100, 100, "ValidationException");
+ }
+
+ /**
+ * Applies the supplied settings to a copy and stores that. A {@code trackName} that differs from
+ * the current one is recorded as {@code pendingTrackName} and the current track is left alone,
+ * as AWS switches tracks only on the next release. {@code port} updates the configured value
+ * that is echoed; the endpoint keeps the port the emulator allocated.
+ */
+ public synchronized Workgroup updateWorkgroup(String workgroupName, WorkgroupSettings settings, String region) {
+ validateSettings(settings);
+ Workgroup updated = new Workgroup(getWorkgroup(workgroupName, region));
+ if (settings.baseCapacity() != null) {
+ updated.setBaseCapacity(settings.baseCapacity());
+ }
+ if (settings.maxCapacity() != null) {
+ updated.setMaxCapacity(settings.maxCapacity());
+ }
+ if (settings.enhancedVpcRouting() != null) {
+ updated.setEnhancedVpcRouting(settings.enhancedVpcRouting());
+ }
+ if (settings.publiclyAccessible() != null) {
+ updated.setPubliclyAccessible(settings.publiclyAccessible());
+ }
+ if (settings.extraComputeForAutomaticOptimization() != null) {
+ updated.setExtraComputeForAutomaticOptimization(settings.extraComputeForAutomaticOptimization());
+ }
+ if (settings.configParameters() != null) {
+ updated.setConfigParameters(copyOf(settings.configParameters()));
+ }
+ if (settings.securityGroupIds() != null) {
+ updated.setSecurityGroupIds(copyOf(settings.securityGroupIds()));
+ }
+ if (settings.subnetIds() != null) {
+ updated.setSubnetIds(copyOf(settings.subnetIds()));
+ }
+ if (settings.port() != null) {
+ updated.setPort(settings.port());
+ }
+ if (settings.ipAddressType() != null) {
+ updated.setIpAddressType(settings.ipAddressType());
+ }
+ if (settings.pricePerformanceTarget() != null) {
+ updated.setPricePerformanceTarget(settings.pricePerformanceTarget());
+ }
+ if (settings.trackName() != null) {
+ if (settings.trackName().equals(updated.getTrackName())) {
+ updated.setPendingTrackName(null);
+ } else {
+ updated.setPendingTrackName(settings.trackName());
+ }
+ }
+ workgroups.put(storageKey(region, workgroupName), updated);
+ return updated;
+ }
+
+ public synchronized Workgroup deleteWorkgroup(String workgroupName, String region) {
+ Workgroup deleted = new Workgroup(getWorkgroup(workgroupName, region));
+ // The proxy goes first and its failure aborts the delete: a listener that could not be closed
+ // keeps its port, so the workgroup must stay on record and the call can be retried.
+ try {
+ runtime.stop(workgroups.accountId(), region, workgroupName);
+ } catch (RuntimeException e) {
+ LOG.warnv(e, "Failed to stop the runtime of workgroup {0}", workgroupName);
+ throw new AwsException("InternalServerException",
+ "Failed to stop workgroup " + workgroupName + "; it was not deleted", 500);
+ }
+ workgroups.delete(storageKey(region, workgroupName));
+ endpoints.release(deleted.getEndpoint());
+ deleted.setStatus("DELETING");
+ return deleted;
+ }
+
+ /**
+ * Resolves a workgroup name or workgroup ARN, as the Data API accepts either, to the live
+ * connection details of its backend.
+ */
+ public WorkgroupTarget getWorkgroupTarget(String nameOrArn, String region) {
+ Workgroup workgroup = resolveWorkgroup(nameOrArn, region);
+ Namespace namespace = getNamespace(workgroup.getNamespaceName(), region);
+ if (workgroup.getRuntimeHost() == null || workgroup.getRuntimePort() <= 0
+ || namespace.getAdminUserPassword() == null) {
+ throw new AwsException("ValidationException",
+ "Workgroup runtime is not available for Data API execution.", 400);
+ }
+ return new WorkgroupTarget(workgroup.getWorkgroupArn(), workgroup.getWorkgroupName(),
+ workgroup.getRuntimeHost(), workgroup.getRuntimePort(), namespace.getDbName(),
+ workgroup.getMasterUsername(), namespace.getAdminUserPassword(), adminUserOf(namespace));
+ }
+
+ /** Connection details of a workgroup's backend; {@code adminUsername} is the name clients log in with. */
+ public record WorkgroupTarget(String arn, String workgroupName, String host, int port, String database,
+ String masterUsername, String masterPassword, String adminUsername) {
+ }
+
+ private Workgroup resolveWorkgroup(String nameOrArn, String region) {
+ if (nameOrArn == null || nameOrArn.isBlank()) {
+ throw validation("workgroupName is required.");
+ }
+ if (!nameOrArn.startsWith("arn:")) {
+ return getWorkgroup(nameOrArn, region);
+ }
+ return workgroupByArn(nameOrArn, region).orElseThrow(() -> workgroupNotFound(nameOrArn));
+ }
+
+ /**
+ * Mints a temporary credential for the caller's database user. It is master-equivalent on the
+ * backend leg, exactly like GetClusterCredentials, so the user name is nominal.
+ */
+ public TempCredential getCredentials(String workgroupName, String dbName, Integer durationSeconds,
+ String dbUser, String region) {
+ Workgroup workgroup = resolveWorkgroup(workgroupName, region);
+ Namespace namespace = getNamespace(workgroup.getNamespaceName(), region);
+ if (dbName != null && !dbName.equals(namespace.getDbName())) {
+ throw validation("dbName must be " + namespace.getDbName() + ", the only database in this workgroup.");
+ }
+ int duration = durationSeconds == null ? DEFAULT_CREDENTIAL_DURATION_SECONDS : durationSeconds;
+ if (duration < MIN_CREDENTIAL_DURATION_SECONDS || duration > MAX_CREDENTIAL_DURATION_SECONDS) {
+ throw validation("durationSeconds must be between " + MIN_CREDENTIAL_DURATION_SECONDS + " and "
+ + MAX_CREDENTIAL_DURATION_SECONDS + ".");
+ }
+ if (workgroup.getRuntimeHost() == null) {
+ throw validation("Workgroup runtime is not available.");
+ }
+ return runtime.issueCredential(workgroups.accountId(), region, workgroup.getWorkgroupName(), dbUser,
+ duration);
+ }
+
+ private Namespace ensureAdminPassword(Namespace namespace, String region) {
+ if (namespace.getAdminUserPassword() != null) {
+ return namespace;
+ }
+ Namespace withPassword = new Namespace(namespace);
+ withPassword.setAdminUserPassword(runtime.generatePassword());
+ namespaces.put(storageKey(region, namespace.getNamespaceName()), withPassword);
+ return withPassword;
+ }
+
+ private void applyNewAdminPassword(Namespace namespace, String newPassword, String region) {
+ Optional<Workgroup> attached = workgroupOf(namespace.getNamespaceName(), region);
+ if (attached.isEmpty() || attached.get().getRuntimeHost() == null) {
+ return;
+ }
+ try {
+ runtime.changeMasterPassword(workgroups.accountId(), region, attached.get().getWorkgroupName(),
+ attached.get().getMasterUsername(), namespace.getDbName(), namespace.getAdminUserPassword(),
+ newPassword);
+ } catch (AwsException e) {
+ if ("InvalidParameterValue".equals(e.getErrorCode())) {
+ throw new AwsException("ValidationException", e.getMessage(), 400);
+ }
+ throw e;
+ }
+ }
+
+ private void restoreAdminPassword(Namespace previous, String region) {
+ Optional<Workgroup> attached = workgroupOf(previous.getNamespaceName(), region);
+ if (attached.isEmpty() || attached.get().getRuntimeHost() == null
+ || previous.getAdminUserPassword() == null) {
+ return;
+ }
+ runtime.restoreMasterPassword(workgroups.accountId(), region, attached.get().getWorkgroupName(),
+ attached.get().getMasterUsername(), previous.getDbName(), previous.getAdminUserPassword());
+ }
+
+ private static String blankToNull(String value) {
+ return value == null || value.isBlank() ? null : value;
+ }
+
+ private Optional<Workgroup> workgroupOf(String namespaceName, String region) {
+ return workgroups.scan(key -> key.startsWith(region + "::")).stream()
+ .filter(workgroup -> namespaceName.equals(workgroup.getNamespaceName()))
+ .findFirst();
}
@Override
public void clear() {
+ for (AccountAwareStorageBackend.AccountEntry<Workgroup> entry : workgroups.scanAllAccountEntries(key -> true)) {
+ try {
+ runtime.stop(entry.accountId(), regionOf(entry.key()), entry.value().getWorkgroupName());
+ endpoints.release(entry.value().getEndpoint());
+ } catch (RuntimeException e) {
+ // The listener may still be bound, so the endpoint stays reserved rather than being
+ // handed to a later cluster or workgroup whose proxy could not bind it.
+ LOG.warnv(e, "Could not stop the runtime of workgroup {0} while clearing state; "
+ + "its endpoint stays reserved", entry.value().getWorkgroupName());
+ }
+ }
namespaces.clear();
+ workgroups.clear();
}
private static void validateNamespaceName(String namespaceName) {
@@ -225,10 +697,68 @@ public class RedshiftServerlessService implements Resettable {
return validated;
}
- private static List<String> copyOf(List<String> values) {
+ private static <T> List<T> copyOf(List<T> values) {
return values == null ? new ArrayList<>() : new ArrayList<>(values);
}
+ private static <T> T orDefault(T value, T fallback) {
+ return value != null ? value : fallback;
+ }
+
+ private static void validateWorkgroupName(String workgroupName) {
+ if (workgroupName == null || workgroupName.length() < 3 || workgroupName.length() > 64
+ || !WORKGROUP_NAME.matcher(workgroupName).matches()) {
+ throw validation("workgroupName must be 3-64 characters of lowercase letters, numbers, and hyphens.");
+ }
+ }
+
+ private static void validateSettings(WorkgroupSettings settings) {
+ requirePositive(settings.baseCapacity(), "baseCapacity");
+ requirePositive(settings.maxCapacity(), "maxCapacity");
+ if (settings.port() != null && !isAllowedPort(settings.port())) {
+ throw validation("port must be in the range 5431-5455 or 8191-8215.");
+ }
+ if (settings.ipAddressType() != null && !IP_ADDRESS_TYPES.contains(settings.ipAddressType())) {
+ throw validation("ipAddressType must be ipv4 or dualstack.");
+ }
+ if (settings.trackName() != null
+ && (settings.trackName().isEmpty() || settings.trackName().length() > 256
+ || !TRACK_NAME.matcher(settings.trackName()).matches())) {
+ throw validation("trackName must be 1-256 characters of letters, numbers, and underscores.");
+ }
+ PricePerformanceTarget target = settings.pricePerformanceTarget();
+ if (target != null) {
+ if (target.getStatus() != null && !PERFORMANCE_TARGET_STATUSES.contains(target.getStatus())) {
+ throw validation("pricePerformanceTarget.status must be ENABLED or DISABLED.");
+ }
+ if (target.getLevel() != null && (target.getLevel() < 1 || target.getLevel() > 100)) {
+ throw validation("pricePerformanceTarget.level must be between 1 and 100.");
+ }
+ }
+ if (settings.configParameters() != null) {
+ for (ConfigParameter parameter : settings.configParameters()) {
+ if (parameter.getParameterKey() == null || parameter.getParameterKey().isBlank()) {
+ throw validation("configParameters entries require a parameterKey.");
+ }
+ }
+ }
+ }
+
+ private static void requirePositive(Integer value, String field) {
+ if (value != null && value < 1) {
+ throw validation(field + " must be a positive integer.");
+ }
+ }
+
+ private static boolean isAllowedPort(int port) {
+ return (port >= 5431 && port <= 5455) || (port >= 8191 && port <= 8215);
+ }
+
+ private static AwsException workgroupNotFound(String workgroupName) {
+ return new AwsException("ResourceNotFoundException",
+ "The workgroup " + workgroupName + " was not found.", 404);
+ }
+
private static String storageKey(String region, String namespaceName) {
return region + "::" + namespaceName;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/WorkgroupSettings.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/WorkgroupSettings.java
new file mode 100644
index 0000000000..6a3eb5df0e
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/WorkgroupSettings.java
@@ -0,0 +1,25 @@
+package io.github.hectorvent.floci.services.redshiftserverless;
+
+import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
+import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
+
+import java.util.List;
+
+/**
+ * The optional members shared by CreateWorkgroup and UpdateWorkgroup. A {@code null} member is one
+ * the caller left out: create then applies the AWS default and update keeps the stored value.
+ */
+public record WorkgroupSettings(
+ Integer baseCapacity,
+ Integer maxCapacity,
+ Boolean enhancedVpcRouting,
+ Boolean publiclyAccessible,
+ Boolean extraComputeForAutomaticOptimization,
+ List<ConfigParameter> configParameters,
+ List<String> securityGroupIds,
+ List<String> subnetIds,
+ Integer port,
+ PricePerformanceTarget pricePerformanceTarget,
+ String ipAddressType,
+ String trackName) {
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/ConfigParameter.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/ConfigParameter.java
new file mode 100644
index 0000000000..1c6d711477
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/ConfigParameter.java
@@ -0,0 +1,35 @@
+package io.github.hectorvent.floci.services.redshiftserverless.model;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+@RegisterForReflection
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class ConfigParameter {
+ private String parameterKey;
+ private String parameterValue;
+
+ public ConfigParameter() {
+ }
+
+ public ConfigParameter(String parameterKey, String parameterValue) {
+ this.parameterKey = parameterKey;
+ this.parameterValue = parameterValue;
+ }
+
+ public String getParameterKey() {
+ return parameterKey;
+ }
+
+ public void setParameterKey(String parameterKey) {
+ this.parameterKey = parameterKey;
+ }
+
+ public String getParameterValue() {
+ return parameterValue;
+ }
+
+ public void setParameterValue(String parameterValue) {
+ this.parameterValue = parameterValue;
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/Namespace.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/Namespace.java
index 7a4ebb5dc8..e857594bef 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/Namespace.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/Namespace.java
@@ -16,6 +16,11 @@ public class Namespace {
private String namespaceId;
private String namespaceArn;
private String adminUsername;
+ /**
+ * Persisted so the workgroup backend can be created and restored with the namespace admin
+ * credentials. AWS never returns it, so no response builder may read it.
+ */
+ private String adminUserPassword;
private String dbName;
private String kmsKeyId;
private String defaultIamRoleArn;
@@ -39,6 +44,7 @@ public class Namespace {
this.namespaceId = other.namespaceId;
this.namespaceArn = other.namespaceArn;
this.adminUsername = other.adminUsername;
+ this.adminUserPassword = other.adminUserPassword;
this.dbName = other.dbName;
this.kmsKeyId = other.kmsKeyId;
this.defaultIamRoleArn = other.defaultIamRoleArn;
@@ -81,6 +87,14 @@ public class Namespace {
this.adminUsername = adminUsername;
}
+ public String getAdminUserPassword() {
+ return adminUserPassword;
+ }
+
+ public void setAdminUserPassword(String adminUserPassword) {
+ this.adminUserPassword = adminUserPassword;
+ }
+
public String getDbName() {
return dbName;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/PricePerformanceTarget.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/PricePerformanceTarget.java
new file mode 100644
index 0000000000..fad5df5407
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/PricePerformanceTarget.java
@@ -0,0 +1,37 @@
+package io.github.hectorvent.floci.services.redshiftserverless.model;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+@RegisterForReflection
+@JsonIgnoreProperties(ignoreUnknown = true)
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class PricePerformanceTarget {
+ private String status;
+ private Integer level;
+
+ public PricePerformanceTarget() {
+ }
+
+ public PricePerformanceTarget(String status, Integer level) {
+ this.status = status;
+ this.level = level;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public void setStatus(String status) {
+ this.status = status;
+ }
+
+ public Integer getLevel() {
+ return level;
+ }
+
+ public void setLevel(Integer level) {
+ this.level = level;
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/Workgroup.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/Workgroup.java
new file mode 100644
index 0000000000..72908f241b
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/Workgroup.java
@@ -0,0 +1,269 @@
+package io.github.hectorvent.floci.services.redshiftserverless.model;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+@RegisterForReflection
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class Workgroup {
+ private String workgroupName;
+ private String workgroupId;
+ private String workgroupArn;
+ private String namespaceName;
+ private Integer baseCapacity;
+ private Integer maxCapacity;
+ private boolean enhancedVpcRouting;
+ private boolean publiclyAccessible;
+ private boolean extraComputeForAutomaticOptimization;
+ private List<ConfigParameter> configParameters = new ArrayList<>();
+ private List<String> securityGroupIds = new ArrayList<>();
+ private List<String> subnetIds = new ArrayList<>();
+ private String status;
+ private Endpoint endpoint;
+ private int port;
+ private String ipAddressType;
+ private String trackName;
+ private String pendingTrackName;
+ private PricePerformanceTarget pricePerformanceTarget;
+ private Instant creationDate;
+ private Map<String, String> tags = new LinkedHashMap<>();
+ /** Address of the PostgreSQL container behind the proxy; not part of the AWS resource. */
+ private String runtimeHost;
+ private int runtimePort;
+ /** The PostgreSQL role the container was created with; it cannot be renamed afterwards. */
+ private String masterUsername;
+
+ public Workgroup() {
+ }
+
+ /**
+ * Copy used by services to build an update on a private instance and store that, so a reader
+ * holding the previous instance never sees a half-applied update. Collections are copied; the
+ * nested endpoint and target are replaced wholesale rather than mutated, so they are shared.
+ */
+ public Workgroup(Workgroup other) {
+ this.workgroupName = other.workgroupName;
+ this.workgroupId = other.workgroupId;
+ this.workgroupArn = other.workgroupArn;
+ this.namespaceName = other.namespaceName;
+ this.baseCapacity = other.baseCapacity;
+ this.maxCapacity = other.maxCapacity;
+ this.enhancedVpcRouting = other.enhancedVpcRouting;
+ this.publiclyAccessible = other.publiclyAccessible;
+ this.extraComputeForAutomaticOptimization = other.extraComputeForAutomaticOptimization;
+ this.configParameters = new ArrayList<>(other.configParameters);
+ this.securityGroupIds = new ArrayList<>(other.securityGroupIds);
+ this.subnetIds = new ArrayList<>(other.subnetIds);
+ this.status = other.status;
+ this.endpoint = other.endpoint;
+ this.port = other.port;
+ this.ipAddressType = other.ipAddressType;
+ this.trackName = other.trackName;
+ this.pendingTrackName = other.pendingTrackName;
+ this.pricePerformanceTarget = other.pricePerformanceTarget;
+ this.creationDate = other.creationDate;
+ this.tags = new LinkedHashMap<>(other.tags);
+ this.runtimeHost = other.runtimeHost;
+ this.runtimePort = other.runtimePort;
+ this.masterUsername = other.masterUsername;
+ }
+
+ public String getWorkgroupName() {
+ return workgroupName;
+ }
+
+ public void setWorkgroupName(String workgroupName) {
+ this.workgroupName = workgroupName;
+ }
+
+ public String getWorkgroupId() {
+ return workgroupId;
+ }
+
+ public void setWorkgroupId(String workgroupId) {
+ this.workgroupId = workgroupId;
+ }
+
+ public String getWorkgroupArn() {
+ return workgroupArn;
+ }
+
+ public void setWorkgroupArn(String workgroupArn) {
+ this.workgroupArn = workgroupArn;
+ }
+
+ public String getNamespaceName() {
+ return namespaceName;
+ }
+
+ public void setNamespaceName(String namespaceName) {
+ this.namespaceName = namespaceName;
+ }
+
+ public Integer getBaseCapacity() {
+ return baseCapacity;
+ }
+
+ public void setBaseCapacity(Integer baseCapacity) {
+ this.baseCapacity = baseCapacity;
+ }
+
+ public Integer getMaxCapacity() {
+ return maxCapacity;
+ }
+
+ public void setMaxCapacity(Integer maxCapacity) {
+ this.maxCapacity = maxCapacity;
+ }
+
+ public boolean isEnhancedVpcRouting() {
+ return enhancedVpcRouting;
+ }
+
+ public void setEnhancedVpcRouting(boolean enhancedVpcRouting) {
+ this.enhancedVpcRouting = enhancedVpcRouting;
+ }
+
+ public boolean isPubliclyAccessible() {
+ return publiclyAccessible;
+ }
+
+ public void setPubliclyAccessible(boolean publiclyAccessible) {
+ this.publiclyAccessible = publiclyAccessible;
+ }
+
+ public boolean isExtraComputeForAutomaticOptimization() {
+ return extraComputeForAutomaticOptimization;
+ }
+
+ public void setExtraComputeForAutomaticOptimization(boolean extraComputeForAutomaticOptimization) {
+ this.extraComputeForAutomaticOptimization = extraComputeForAutomaticOptimization;
+ }
+
+ public List<ConfigParameter> getConfigParameters() {
+ return configParameters;
+ }
+
+ public void setConfigParameters(List<ConfigParameter> configParameters) {
+ this.configParameters = configParameters;
+ }
+
+ public List<String> getSecurityGroupIds() {
+ return securityGroupIds;
+ }
+
+ public void setSecurityGroupIds(List<String> securityGroupIds) {
+ this.securityGroupIds = securityGroupIds;
+ }
+
+ public List<String> getSubnetIds() {
+ return subnetIds;
+ }
+
+ public void setSubnetIds(List<String> subnetIds) {
+ this.subnetIds = subnetIds;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public void setStatus(String status) {
+ this.status = status;
+ }
+
+ public Endpoint getEndpoint() {
+ return endpoint;
+ }
+
+ public void setEndpoint(Endpoint endpoint) {
+ this.endpoint = endpoint;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ public String getIpAddressType() {
+ return ipAddressType;
+ }
+
+ public void setIpAddressType(String ipAddressType) {
+ this.ipAddressType = ipAddressType;
+ }
+
+ public String getTrackName() {
+ return trackName;
+ }
+
+ public void setTrackName(String trackName) {
+ this.trackName = trackName;
+ }
+
+ public String getPendingTrackName() {
+ return pendingTrackName;
+ }
+
+ public void setPendingTrackName(String pendingTrackName) {
+ this.pendingTrackName = pendingTrackName;
+ }
+
+ public PricePerformanceTarget getPricePerformanceTarget() {
+ return pricePerformanceTarget;
+ }
+
+ public void setPricePerformanceTarget(PricePerformanceTarget pricePerformanceTarget) {
+ this.pricePerformanceTarget = pricePerformanceTarget;
+ }
+
+ public Instant getCreationDate() {
+ return creationDate;
+ }
+
+ public void setCreationDate(Instant creationDate) {
+ this.creationDate = creationDate;
+ }
+
+ public String getRuntimeHost() {
+ return runtimeHost;
+ }
+
+ public void setRuntimeHost(String runtimeHost) {
+ this.runtimeHost = runtimeHost;
+ }
+
+ public int getRuntimePort() {
+ return runtimePort;
+ }
+
+ public void setRuntimePort(int runtimePort) {
+ this.runtimePort = runtimePort;
+ }
+
+ public String getMasterUsername() {
+ return masterUsername;
+ }
+
+ public void setMasterUsername(String masterUsername) {
+ this.masterUsername = masterUsername;
+ }
+
+ public Map<String, String> getTags() {
+ return tags;
+ }
+
+ public void setTags(Map<String, String> tags) {
+ this.tags = tags;
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java
index 78490811f0..631fcd1ead 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java
@@ -90,6 +90,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.LogsMetri
import io.github.hectorvent.floci.services.cloudformation.provisioners.PipesCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.RdsCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.RedshiftClusterCfnProvisioner;
+import io.github.hectorvent.floci.services.cloudformation.provisioners.RedshiftServerlessCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.Route53CfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.S3CfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.SchedulerScheduleGroupCfnProvisioner;
@@ -129,6 +130,7 @@ import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.pipes.PipesService;
import io.github.hectorvent.floci.services.rds.RdsService;
import io.github.hectorvent.floci.services.redshift.RedshiftService;
+import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessService;
import io.github.hectorvent.floci.services.route53.Route53Service;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.scheduler.SchedulerService;
@@ -218,6 +220,7 @@ final class CfnProvisionerFixture {
private WafV2Service wafV2Service;
private BackupService backupService;
private RedshiftService redshiftService;
+ private RedshiftServerlessService redshiftServerlessService;
private CloudMapService cloudMapService;
private CloudFormationResourceRegistry resourceRegistry;
private boolean registryChosenByTest;
@@ -433,6 +436,9 @@ final class CfnProvisionerFixture {
if (redshiftService != null) {
discovered.add(new RedshiftClusterCfnProvisioner(redshiftService));
}
+ if (redshiftServerlessService != null) {
+ discovered.add(new RedshiftServerlessCfnProvisioner(redshiftServerlessService));
+ }
if (cloudMapService != null) {
discovered.add(new CloudMapCfnProvisioner(cloudMapService));
}
@@ -693,6 +699,11 @@ final class CfnProvisionerFixture {
return redshiftService(s);
}
+ public Builder redshiftServerlessService(RedshiftServerlessService s) {
+ this.redshiftServerlessService = s;
+ return this;
+ }
+
public Builder cloudMap(CloudMapService v) {
this.cloudMapService = v;
return this;
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftServerlessCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftServerlessCfnIntegrationTest.java
new file mode 100644
index 0000000000..124ba3220f
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftServerlessCfnIntegrationTest.java
@@ -0,0 +1,196 @@
+package io.github.hectorvent.floci.services.cloudformation;
+
+import io.github.hectorvent.floci.core.common.XmlParser;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
+import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessRuntime;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.InjectMock;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.ValidatableResponse;
+import io.restassured.specification.RequestSpecification;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItem;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.when;
+
+/**
+ * Provisions an {@code AWS::RedshiftServerless::Namespace} and its
+ * {@code AWS::RedshiftServerless::Workgroup} through a stack. An unmapped type is stubbed as
+ * CREATE_COMPLETE with a fake ARN, so the assertions that matter are the exact {@code Fn::GetAtt}
+ * keys and that what they return is what the Redshift Serverless API serves.
+ */
+@QuarkusTest
+class RedshiftServerlessCfnIntegrationTest {
+
+ private static final String CFN_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20261003/us-east-1/cloudformation/aws4_request";
+ private static final String RS_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20261003/us-east-1/redshift-serverless/aws4_request";
+ private static final String STACK = "redshift-serverless-cfn-it";
+ private static final String NAMESPACE = "rs-cfn-it-ns";
+ private static final String WORKGROUP = "rs-cfn-it-wg";
+
+ private static final String TEMPLATE = """
+ {
+ "Parameters": {"Capacity": {"Type": "Number"}},
+ "Resources": {
+ "Ns": {
+ "Type": "AWS::RedshiftServerless::Namespace",
+ "Properties": {
+ "NamespaceName": "%s",
+ "AdminUsername": "admin",
+ "AdminUserPassword": "Secret123!",
+ "DbName": "analytics",
+ "LogExports": ["userlog"],
+ "Tags": [{"Key": "env", "Value": "dev"}]
+ }
+ },
+ "Wg": {
+ "Type": "AWS::RedshiftServerless::Workgroup",
+ "Properties": {
+ "WorkgroupName": "%s",
+ "NamespaceName": {"Ref": "Ns"},
+ "BaseCapacity": {"Ref": "Capacity"},
+ "PubliclyAccessible": true
+ }
+ }
+ },
+ "Outputs": {
+ "NsRef": {"Value": {"Ref": "Ns"}},
+ "NsArn": {"Value": {"Fn::GetAtt": ["Ns", "Namespace.NamespaceArn"]}},
+ "NsId": {"Value": {"Fn::GetAtt": ["Ns", "Namespace.NamespaceId"]}},
+ "NsDb": {"Value": {"Fn::GetAtt": ["Ns", "Namespace.DbName"]}},
+ "NsStatus": {"Value": {"Fn::GetAtt": ["Ns", "Namespace.Status"]}},
+ "WgRef": {"Value": {"Ref": "Wg"}},
+ "WgArn": {"Value": {"Fn::GetAtt": ["Wg", "Workgroup.WorkgroupArn"]}},
+ "WgId": {"Value": {"Fn::GetAtt": ["Wg", "Workgroup.WorkgroupId"]}},
+ "WgNamespace": {"Value": {"Fn::GetAtt": ["Wg", "Workgroup.NamespaceName"]}},
+ "WgAddress": {"Value": {"Fn::GetAtt": ["Wg", "Workgroup.Endpoint.Address"]}},
+ "WgPort": {"Value": {"Fn::GetAtt": ["Wg", "Workgroup.Endpoint.Port"]}},
+ "WgBase": {"Value": {"Fn::GetAtt": ["Wg", "Workgroup.BaseCapacity"]}},
+ "WgStatus": {"Value": {"Fn::GetAtt": ["Wg", "Workgroup.Status"]}}
+ }
+ }
+ """.formatted(NAMESPACE, WORKGROUP);
+
+ @BeforeAll
+ static void configureRestAssured() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ /**
+ * The metadata behaviour under test needs no PostgreSQL container, so the runtime is replaced:
+ * these tests then run without Docker, and the real container and proxy are exercised by
+ * {@code RedshiftServerlessRuntimeIntegrationTest}.
+ */
+ @InjectMock
+ RedshiftServerlessRuntime runtime;
+
+ @BeforeEach
+ void stubRuntime() {
+ when(runtime.generatePassword()).thenReturn("Generated123");
+ when(runtime.start(any(String.class), any(String.class), any(String.class), any(String.class),
+ any(String.class), any(String.class), any(Endpoint.class), anyBoolean(), any()))
+ .thenReturn(new RedshiftServerlessRuntime.Backend("127.0.0.1", 55432));
+ }
+
+ @Test
+ void createUpdateAndDeleteANamespaceAndWorkgroup() {
+ cloudFormation("CreateStack", "32");
+ String created = describeStacks("CREATE_COMPLETE");
+
+ assertEquals(NAMESPACE, outputValue(created, "NsRef"));
+ assertEquals(WORKGROUP, outputValue(created, "WgRef"));
+ assertEquals("analytics", outputValue(created, "NsDb"));
+ assertEquals("AVAILABLE", outputValue(created, "NsStatus"));
+ assertEquals("AVAILABLE", outputValue(created, "WgStatus"));
+ assertEquals(NAMESPACE, outputValue(created, "WgNamespace"));
+ assertEquals("32", outputValue(created, "WgBase"));
+ assertTrue(outputValue(created, "WgArn").contains(":workgroup/" + outputValue(created, "WgId")),
+ "GetAtt must be the ARN of the workgroup, not the 'Workgroup.WorkgroupArn' literal");
+ assertNotEquals("Wg.Workgroup.Endpoint.Address", outputValue(created, "WgAddress"));
+ assertTrue(Integer.parseInt(outputValue(created, "WgPort")) > 0);
+
+ call("GetNamespace", "{\"namespaceName\":\"" + NAMESPACE + "\"}")
+ .statusCode(200)
+ .body("namespace.namespaceArn", equalTo(outputValue(created, "NsArn")))
+ .body("namespace.namespaceId", equalTo(outputValue(created, "NsId")))
+ .body("namespace.logExports", hasItem("userlog"));
+ call("GetWorkgroup", "{\"workgroupName\":\"" + WORKGROUP + "\"}")
+ .statusCode(200)
+ .body("workgroup.workgroupArn", equalTo(outputValue(created, "WgArn")))
+ .body("workgroup.baseCapacity", equalTo(32))
+ .body("workgroup.publiclyAccessible", equalTo(true))
+ .body("workgroup.endpoint.address", equalTo(outputValue(created, "WgAddress")));
+
+ cloudFormation("UpdateStack", "64");
+ String updated = describeStacks("UPDATE_COMPLETE");
+
+ // The capacity changes on the same workgroup; nothing is replaced.
+ assertEquals(outputValue(created, "WgId"), outputValue(updated, "WgId"));
+ assertEquals("64", outputValue(updated, "WgBase"));
+ call("GetWorkgroup", "{\"workgroupName\":\"" + WORKGROUP + "\"}")
+ .statusCode(200)
+ .body("workgroup.baseCapacity", equalTo(64));
+
+ cloudFormation("DeleteStack", null);
+ CfnStackWaits.awaitStackDeleted(STACK);
+
+ // The workgroup is deleted before the namespace, which would otherwise be refused.
+ call("GetWorkgroup", "{\"workgroupName\":\"" + WORKGROUP + "\"}").statusCode(404);
+ call("GetNamespace", "{\"namespaceName\":\"" + NAMESPACE + "\"}").statusCode(404);
+ }
+
+ private static void cloudFormation(String action, String capacity) {
+ RequestSpecification request = given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", action)
+ .formParam("StackName", STACK);
+ if (!"DeleteStack".equals(action)) {
+ request.formParam("TemplateBody", TEMPLATE);
+ request.formParam("Parameters.member.1.ParameterKey", "Capacity");
+ request.formParam("Parameters.member.1.ParameterValue", capacity);
+ }
+ request.when().post("/").then().statusCode(200);
+ }
+
+ private static String describeStacks(String expectedStatus) {
+ return given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", STACK)
+ .when().post("/").then().statusCode(200)
+ .body(containsString("<StackStatus>" + expectedStatus + "</StackStatus>"))
+ .extract().asString();
+ }
+
+ private static String outputValue(String xml, String key) {
+ Map<String, String> outputs = XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue");
+ return outputs.get(key);
+ }
+
+ private static ValidatableResponse call(String action, String body) {
+ return given()
+ .header("X-Amz-Target", "RedshiftServerless." + action)
+ .header("Authorization", RS_AUTH)
+ .contentType("application/x-amz-json-1.1")
+ .body(body)
+ .when()
+ .post("/")
+ .then();
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftServerlessCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftServerlessCfnProvisionerTest.java
new file mode 100644
index 0000000000..fd4c22329a
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftServerlessCfnProvisionerTest.java
@@ -0,0 +1,396 @@
+package io.github.hectorvent.floci.services.cloudformation.provisioners;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
+import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
+import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessService;
+import io.github.hectorvent.floci.services.redshiftserverless.WorkgroupSettings;
+import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
+import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
+import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
+import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/** The Redshift Serverless CFN provisioner in isolation, against a mocked service. */
+class RedshiftServerlessCfnProvisionerTest {
+
+ private static final String REGION = "us-east-1";
+ private static final String NAMESPACE_TYPE = "AWS::RedshiftServerless::Namespace";
+ private static final String WORKGROUP_TYPE = "AWS::RedshiftServerless::Workgroup";
+ private static final String NAMESPACE_ARN = "arn:aws:redshift-serverless:us-east-1:000000000000:namespace/ns-id";
+ private static final String WORKGROUP_ARN = "arn:aws:redshift-serverless:us-east-1:000000000000:workgroup/wg-id";
+
+ private final RedshiftServerlessService service = mock(RedshiftServerlessService.class);
+ private final RedshiftServerlessCfnProvisioner provisioner = new RedshiftServerlessCfnProvisioner(service);
+ private final ObjectMapper mapper = new ObjectMapper();
+
+ private ProvisionContext ctx(String priorPhysicalId) {
+ CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
+ when(engine.resolve(any())).thenAnswer(inv -> {
+ JsonNode node = inv.getArgument(0);
+ return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
+ });
+ when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
+ return new ProvisionContext(engine, REGION, "000000000000", "my-stack", priorPhysicalId);
+ }
+
+ private static StackResource resource(String type, String priorPhysicalId) {
+ StackResource r = new StackResource();
+ r.setLogicalId("MyResource");
+ r.setResourceType(type);
+ r.setPhysicalId(priorPhysicalId);
+ r.setAttributes(new HashMap<>());
+ return r;
+ }
+
+ private static Namespace namespace(String name) {
+ Namespace namespace = new Namespace();
+ namespace.setNamespaceName(name);
+ namespace.setNamespaceId("ns-id");
+ namespace.setNamespaceArn(NAMESPACE_ARN);
+ namespace.setAdminUsername("admin");
+ namespace.setDbName("dev");
+ namespace.setKmsKeyId("AWS_OWNED_KMS_KEY");
+ namespace.setIamRoles(new ArrayList<>(List.of("arn:aws:iam::000000000000:role/a")));
+ namespace.setLogExports(new ArrayList<>(List.of("userlog")));
+ namespace.setStatus("AVAILABLE");
+ namespace.setCreationDate(Instant.parse("2026-10-03T00:00:00Z"));
+ return namespace;
+ }
+
+ private static Workgroup workgroup(String name, String namespaceName) {
+ Workgroup workgroup = new Workgroup();
+ workgroup.setWorkgroupName(name);
+ workgroup.setWorkgroupId("wg-id");
+ workgroup.setWorkgroupArn(WORKGROUP_ARN);
+ workgroup.setNamespaceName(namespaceName);
+ workgroup.setBaseCapacity(32);
+ workgroup.setTrackName("current");
+ workgroup.setStatus("AVAILABLE");
+ workgroup.setCreationDate(Instant.parse("2026-10-03T00:00:00Z"));
+ workgroup.setSecurityGroupIds(new ArrayList<>(List.of("sg-1", "sg-2")));
+ workgroup.setEndpoint(new Endpoint("localhost", 7100));
+ workgroup.setConfigParameters(new ArrayList<>(List.of(new ConfigParameter("datestyle", "ISO, MDY"))));
+ return workgroup;
+ }
+
+ @Test
+ void createNamespaceSendsPropertiesAndRecordsEveryDottedAttribute() {
+ when(service.createNamespace(eq("my-ns"), eq("admin"), eq("Secret123"), eq("analytics"), isNull(),
+ isNull(), anyList(), anyList(), anyMap(), eq(REGION))).thenReturn(namespace("my-ns"));
+ ObjectNode props = mapper.createObjectNode();
+ props.put("NamespaceName", "my-ns");
+ props.put("AdminUsername", "admin");
+ props.put("AdminUserPassword", "Secret123");
+ props.put("DbName", "analytics");
+ props.putObject("Tags").put("env", "dev");
+ StackResource r = resource(NAMESPACE_TYPE, null);
+
+ provisioner.provision(r, props, ctx(null));
+
+ assertEquals("my-ns", r.getPhysicalId());
+ Map<String, String> attributes = r.getAttributes();
+ assertEquals("my-ns", attributes.get("Namespace.NamespaceName"));
+ assertEquals("ns-id", attributes.get("Namespace.NamespaceId"));
+ assertEquals(NAMESPACE_ARN, attributes.get("Namespace.NamespaceArn"));
+ assertEquals("dev", attributes.get("Namespace.DbName"));
+ assertEquals("AWS_OWNED_KMS_KEY", attributes.get("Namespace.KmsKeyId"));
+ assertEquals("AVAILABLE", attributes.get("Namespace.Status"));
+ assertEquals("2026-10-03T00:00:00Z", attributes.get("Namespace.CreationDate"));
+ assertEquals("admin", attributes.get("Namespace.AdminUsername"));
+ assertEquals("arn:aws:iam::000000000000:role/a", attributes.get("Namespace.IamRoles"));
+ assertEquals("userlog", attributes.get("Namespace.LogExports"));
+ assertFalse(attributes.containsKey("Namespace.DefaultIamRoleArn"));
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<Map<String, String>> tags = ArgumentCaptor.forClass(Map.class);
+ verify(service).createNamespace(eq("my-ns"), eq("admin"), eq("Secret123"), eq("analytics"), isNull(),
+ isNull(), anyList(), anyList(), tags.capture(), eq(REGION));
+ assertEquals(Map.of("env", "dev"), tags.getValue());
+ }
+
+ @Test
+ void anUnnamedNamespaceGetsALowercaseGeneratedNameThatIsStableAcrossUpdates() {
+ when(service.createNamespace(any(), any(), any(), any(), any(), any(), anyList(), anyList(), anyMap(),
+ eq(REGION)))
+ .thenAnswer(inv -> namespace(inv.getArgument(0)));
+ StackResource created = resource(NAMESPACE_TYPE, null);
+
+ provisioner.provision(created, mapper.createObjectNode(), ctx(null));
+
+ String generated = created.getPhysicalId();
+ assertTrue(generated.startsWith("my-stack-myresource-"), generated);
+ assertEquals(generated, generated.toLowerCase());
+
+ when(service.getNamespace(generated, REGION)).thenReturn(namespace(generated));
+ when(service.reconcileNamespace(eq(generated), any(), any(), any(), any(), anyList(), anyList(), eq(REGION)))
+ .thenReturn(namespace(generated));
+ when(service.listTagsForResource(NAMESPACE_ARN, REGION)).thenReturn(Map.of());
+ StackResource updated = resource(NAMESPACE_TYPE, generated);
+
+ provisioner.provision(updated, mapper.createObjectNode(), ctx(generated));
+
+ assertEquals(generated, updated.getPhysicalId());
+ verify(service).reconcileNamespace(eq(generated), any(), any(), any(), any(), anyList(), anyList(), eq(REGION));
+ }
+
+ @Test
+ void updateNamespaceUpdatesInPlaceAndRemovesTagsTheTemplateDropped() {
+ when(service.getNamespace("my-ns", REGION)).thenReturn(namespace("my-ns"));
+ when(service.reconcileNamespace(eq("my-ns"), isNull(), isNull(), eq("custom-key"), isNull(), anyList(),
+ anyList(), eq(REGION))).thenReturn(namespace("my-ns"));
+ when(service.listTagsForResource(NAMESPACE_ARN, REGION)).thenReturn(Map.of("old", "1", "keep", "2"));
+ ObjectNode props = mapper.createObjectNode();
+ props.put("NamespaceName", "my-ns");
+ props.put("KmsKeyId", "custom-key");
+ props.putObject("Tags").put("keep", "2");
+ StackResource r = resource(NAMESPACE_TYPE, "my-ns");
+
+ provisioner.provision(r, props, ctx("my-ns"));
+
+ verify(service, never()).createNamespace(any(), any(), any(), any(), any(), any(), anyList(), anyList(),
+ anyMap(), any());
+ verify(service).untagResource(NAMESPACE_ARN, List.of("old"), REGION);
+ verify(service).tagResource(NAMESPACE_ARN, Map.of("keep", "2"), REGION);
+ assertEquals("my-ns", r.getPhysicalId());
+ }
+
+ @Test
+ void updateNamespaceRejectsADbNameChangeBecauseItIsFixedAtCreation() {
+ when(service.getNamespace("my-ns", REGION)).thenReturn(namespace("my-ns"));
+ ObjectNode props = mapper.createObjectNode();
+ props.put("NamespaceName", "my-ns");
+ props.put("DbName", "analytics");
+ StackResource r = resource(NAMESPACE_TYPE, "my-ns");
+
+ AwsException rejected = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx("my-ns")));
+
+ assertEquals("ValidationException", rejected.getErrorCode());
+ verify(service, never()).reconcileNamespace(any(), any(), any(), any(), any(), any(), any(), any());
+ }
+
+ @Test
+ void updateNamespaceSendsOmittedKmsKeyAndDefaultRoleAsNullSoTheServiceResetsThem() {
+ when(service.getNamespace("my-ns", REGION)).thenReturn(namespace("my-ns"));
+ when(service.reconcileNamespace(eq("my-ns"), isNull(), isNull(), isNull(), isNull(), anyList(), anyList(),
+ eq(REGION))).thenReturn(namespace("my-ns"));
+ when(service.listTagsForResource(NAMESPACE_ARN, REGION)).thenReturn(Map.of());
+ ObjectNode props = mapper.createObjectNode();
+ props.put("NamespaceName", "my-ns");
+ props.put("DbName", "dev");
+ StackResource r = resource(NAMESPACE_TYPE, "my-ns");
+
+ provisioner.provision(r, props, ctx("my-ns"));
+
+ verify(service).reconcileNamespace(eq("my-ns"), isNull(), isNull(), isNull(), isNull(), anyList(),
+ anyList(), eq(REGION));
+ }
+
+ @Test
+ void updateNamespaceDropsTheDefaultRoleAttributeWhenTheTemplateNoLongerDeclaresIt() {
+ when(service.getNamespace("my-ns", REGION)).thenReturn(namespace("my-ns"));
+ when(service.reconcileNamespace(eq("my-ns"), isNull(), isNull(), isNull(), isNull(), anyList(), anyList(),
+ eq(REGION))).thenReturn(namespace("my-ns"));
+ when(service.listTagsForResource(NAMESPACE_ARN, REGION)).thenReturn(Map.of());
+ ObjectNode props = mapper.createObjectNode();
+ props.put("NamespaceName", "my-ns");
+ StackResource r = resource(NAMESPACE_TYPE, "my-ns");
+ r.getAttributes().put("Namespace.DefaultIamRoleArn", "arn:aws:iam::000000000000:role/old");
+
+ provisioner.provision(r, props, ctx("my-ns"));
+
+ assertFalse(r.getAttributes().containsKey("Namespace.DefaultIamRoleArn"));
+ }
+
+ @Test
+ void createWorkgroupParsesConfigParametersResolvedFromAnotherWorkgroupAttribute() {
+ when(service.createWorkgroup(eq("my-wg"), eq("my-ns"), any(WorkgroupSettings.class), anyMap(), eq(REGION)))
+ .thenReturn(workgroup("my-wg", "my-ns"));
+ ObjectNode props = mapper.createObjectNode();
+ props.put("WorkgroupName", "my-wg");
+ props.put("NamespaceName", "my-ns");
+ props.put("ConfigParameters", "[{\"ParameterKey\":\"datestyle\",\"ParameterValue\":\"ISO, MDY\"}]");
+
+ provisioner.provision(resource(WORKGROUP_TYPE, null), props, ctx(null));
+
+ ArgumentCaptor<WorkgroupSettings> settings = ArgumentCaptor.forClass(WorkgroupSettings.class);
+ verify(service).createWorkgroup(eq("my-wg"), eq("my-ns"), settings.capture(), anyMap(), eq(REGION));
+ assertEquals(1, settings.getValue().configParameters().size());
+ assertEquals("datestyle", settings.getValue().configParameters().get(0).getParameterKey());
+ assertEquals("ISO, MDY", settings.getValue().configParameters().get(0).getParameterValue());
+ }
+
+ @Test
+ void createWorkgroupRejectsConfigParametersThatAreNotAList() {
+ ObjectNode props = mapper.createObjectNode();
+ props.put("WorkgroupName", "my-wg");
+ props.put("NamespaceName", "my-ns");
+ props.put("ConfigParameters", "not-json");
+
+ AwsException rejected = assertThrows(AwsException.class,
+ () -> provisioner.provision(resource(WORKGROUP_TYPE, null), props, ctx(null)));
+
+ assertEquals("ValidationException", rejected.getErrorCode());
+ }
+
+ @Test
+ void createWorkgroupMapsEveryPropertyAndRecordsTheEndpointAttributes() {
+ when(service.createWorkgroup(eq("my-wg"), eq("my-ns"), any(WorkgroupSettings.class), anyMap(), eq(REGION)))
+ .thenReturn(workgroup("my-wg", "my-ns"));
+ ObjectNode props = mapper.createObjectNode();
+ props.put("WorkgroupName", "my-wg");
+ props.put("NamespaceName", "my-ns");
+ props.put("BaseCapacity", 32);
+ props.put("MaxCapacity", 64);
+ props.put("EnhancedVpcRouting", false);
+ props.put("PubliclyAccessible", true);
+ props.put("Port", 5440);
+ props.put("TrackName", "trailing");
+ props.putArray("SecurityGroupIds").add("sg-1");
+ props.putArray("SubnetIds").add("subnet-1");
+ props.putArray("ConfigParameters").addObject()
+ .put("ParameterKey", "datestyle").put("ParameterValue", "ISO, MDY");
+ props.putObject("PricePerformanceTarget").put("Status", "ENABLED").put("Level", 50);
+ StackResource r = resource(WORKGROUP_TYPE, null);
+
+ provisioner.provision(r, props, ctx(null));
+
+ ArgumentCaptor<WorkgroupSettings> settings = ArgumentCaptor.forClass(WorkgroupSettings.class);
+ verify(service).createWorkgroup(eq("my-wg"), eq("my-ns"), settings.capture(), anyMap(), eq(REGION));
+ WorkgroupSettings sent = settings.getValue();
+ assertEquals(32, sent.baseCapacity());
+ assertEquals(64, sent.maxCapacity());
+ assertEquals(Boolean.FALSE, sent.enhancedVpcRouting());
+ assertEquals(Boolean.TRUE, sent.publiclyAccessible());
+ assertEquals(5440, sent.port());
+ assertEquals("trailing", sent.trackName());
+ assertEquals("datestyle", sent.configParameters().get(0).getParameterKey());
+ PricePerformanceTarget target = sent.pricePerformanceTarget();
+ assertEquals("ENABLED", target.getStatus());
+ assertEquals(50, target.getLevel());
+
+ assertEquals("my-wg", r.getPhysicalId());
+ Map<String, String> attributes = r.getAttributes();
+ assertEquals("wg-id", attributes.get("Workgroup.WorkgroupId"));
+ assertEquals(WORKGROUP_ARN, attributes.get("Workgroup.WorkgroupArn"));
+ assertEquals("my-ns", attributes.get("Workgroup.NamespaceName"));
+ assertEquals("localhost", attributes.get("Workgroup.Endpoint.Address"));
+ assertEquals("7100", attributes.get("Workgroup.Endpoint.Port"));
+ assertEquals("32", attributes.get("Workgroup.BaseCapacity"));
+ assertEquals("sg-1,sg-2", attributes.get("Workgroup.SecurityGroupIds"));
+ assertEquals("[{\"ParameterKey\":\"datestyle\",\"ParameterValue\":\"ISO, MDY\"}]",
+ attributes.get("Workgroup.ConfigParameters"));
+ assertEquals("AVAILABLE", attributes.get("Workgroup.Status"));
+ assertFalse(attributes.containsKey("Workgroup.MaxCapacity"), "unset on the stored workgroup");
+ }
+
+ @Test
+ void updateWorkgroupUpdatesInPlaceAndRejectsANamespaceChange() {
+ when(service.getWorkgroup("my-wg", REGION)).thenReturn(workgroup("my-wg", "my-ns"));
+ when(service.updateWorkgroup(eq("my-wg"), any(WorkgroupSettings.class), eq(REGION)))
+ .thenReturn(workgroup("my-wg", "my-ns"));
+ when(service.listTagsForResource(WORKGROUP_ARN, REGION)).thenReturn(Map.of());
+ ObjectNode props = mapper.createObjectNode();
+ props.put("WorkgroupName", "my-wg");
+ props.put("NamespaceName", "my-ns");
+ props.put("BaseCapacity", 64);
+
+ provisioner.provision(resource(WORKGROUP_TYPE, "my-wg"), props, ctx("my-wg"));
+
+ verify(service).updateWorkgroup(eq("my-wg"), any(WorkgroupSettings.class), eq(REGION));
+ verify(service, never()).createWorkgroup(any(), any(), any(), any(), any());
+
+ ObjectNode moved = mapper.createObjectNode();
+ moved.put("WorkgroupName", "my-wg");
+ moved.put("NamespaceName", "other-ns");
+ AwsException rejected = assertThrows(AwsException.class,
+ () -> provisioner.provision(resource(WORKGROUP_TYPE, "my-wg"), moved, ctx("my-wg")));
+ assertEquals("ValidationException", rejected.getErrorCode());
+ }
+
+ @Test
+ void anUpdateThatOmitsAPropertyResetsItToTheDefaultInsteadOfKeepingTheStoredValue() {
+ when(service.getWorkgroup("my-wg", REGION)).thenReturn(workgroup("my-wg", "my-ns"));
+ when(service.updateWorkgroup(eq("my-wg"), any(WorkgroupSettings.class), eq(REGION)))
+ .thenReturn(workgroup("my-wg", "my-ns"));
+ when(service.listTagsForResource(WORKGROUP_ARN, REGION)).thenReturn(Map.of());
+ ObjectNode props = mapper.createObjectNode();
+ props.put("WorkgroupName", "my-wg");
+ props.put("NamespaceName", "my-ns");
+
+ provisioner.provision(resource(WORKGROUP_TYPE, "my-wg"), props, ctx("my-wg"));
+
+ ArgumentCaptor<WorkgroupSettings> settings = ArgumentCaptor.forClass(WorkgroupSettings.class);
+ verify(service).updateWorkgroup(eq("my-wg"), settings.capture(), eq(REGION));
+ WorkgroupSettings sent = settings.getValue();
+ assertEquals(RedshiftServerlessService.DEFAULT_BASE_CAPACITY, sent.baseCapacity());
+ assertEquals(Boolean.FALSE, sent.enhancedVpcRouting());
+ assertEquals(Boolean.FALSE, sent.publiclyAccessible());
+ assertEquals(RedshiftServerlessService.DEFAULT_PORT, sent.port());
+ assertEquals(RedshiftServerlessService.DEFAULT_TRACK_NAME, sent.trackName());
+ assertEquals("DISABLED", sent.pricePerformanceTarget().getStatus());
+ assertTrue(sent.configParameters().isEmpty());
+ assertTrue(sent.subnetIds().isEmpty());
+ }
+
+ @Test
+ void aNonNumericCapacityIsAValidationError() {
+ ObjectNode props = mapper.createObjectNode();
+ props.put("WorkgroupName", "my-wg");
+ props.put("NamespaceName", "my-ns");
+ props.put("BaseCapacity", "many");
+
+ AwsException rejected = assertThrows(AwsException.class,
+ () -> provisioner.provision(resource(WORKGROUP_TYPE, null), props, ctx(null)));
+
+ assertEquals("ValidationException", rejected.getErrorCode());
+ verify(service, never()).createWorkgroup(any(), any(), any(), any(), any());
+ }
+
+ @Test
+ void deleteToleratesOnlyNotFoundAndLetsOtherFailuresReachTheStack() {
+ doThrow(new AwsException("ResourceNotFoundException", "gone", 404))
+ .when(service).deleteWorkgroup("gone-wg", REGION);
+ provisioner.delete(WORKGROUP_TYPE, "gone-wg", REGION);
+
+ doThrow(new AwsException("ConflictException", "still has a workgroup", 409))
+ .when(service).deleteNamespace("busy-ns", REGION);
+ AwsException failure = assertThrows(AwsException.class,
+ () -> provisioner.delete(NAMESPACE_TYPE, "busy-ns", REGION));
+ assertEquals("ConflictException", failure.getErrorCode());
+
+ provisioner.delete(NAMESPACE_TYPE, "ok-ns", REGION);
+ verify(service).deleteNamespace("ok-ns", REGION);
+ }
+
+ @Test
+ void theProvisionerServesBothTypes() {
+ assertEquals(Set.of(NAMESPACE_TYPE, WORKGROUP_TYPE), provisioner.resourceTypes());
+ }
+}
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 0fc0079510..2e68b28fc0 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
@@ -1846,4 +1846,28 @@ class RedshiftServiceTest {
assertEquals(PasswordValidator.AuthResult.PASSTHROUGH,
validator.validate("someone-else", "whatever"));
}
+
+ @Test
+ void reservedProxyPortsAreSharedBetweenAllCallersAndReusableOnceReleased() {
+ int first = service.reserveProxyPort();
+ int second = service.reserveProxyPort();
+
+ assertEquals(7100, first);
+ assertEquals(7101, second);
+ assertFalse(service.reserveProxyPort(first), "a port already handed out cannot be reserved again");
+ assertTrue(service.reserveProxyPort(7150), "a specific free port, such as a persisted one, can be reserved");
+ assertEquals(7102, service.reserveProxyPort(), "reserving a specific port does not disturb the sequence");
+
+ service.releaseProxyPort(first);
+ assertTrue(service.reserveProxyPort(first));
+ assertFalse(service.reserveProxyPort(0), "port 0 is never a real port");
+ }
+
+ @Test
+ void advertisedEndpointUsesTheConfiguredHostAndTheGivenPort() {
+ Endpoint endpoint = service.advertisedEndpoint(7123);
+
+ assertEquals("localhost", endpoint.getAddress());
+ assertEquals(7123, endpoint.getPort());
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java
index b24b8f8a72..92204ed42c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.redshift.RedshiftCredentialBroker;
import io.github.hectorvent.floci.services.redshift.RedshiftService;
import io.github.hectorvent.floci.services.redshift.model.Cluster;
+import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessService;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
import org.junit.jupiter.api.Test;
@@ -37,10 +38,12 @@ class RedshiftDataResourceResolverTest {
return c;
}
+ private final RedshiftServerlessService serverless = mock(RedshiftServerlessService.class);
+
private RedshiftDataResourceResolver resolver(RedshiftService redshift, SecretsManagerService secrets) {
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn(ACCOUNT);
- return new RedshiftDataResourceResolver(redshift, secrets, mapper, broker, regionResolver);
+ return new RedshiftDataResourceResolver(redshift, secrets, mapper, broker, regionResolver, serverless);
}
@Test
@@ -63,14 +66,86 @@ class RedshiftDataResourceResolverTest {
assertEquals("Secret123", target.password());
}
+ private static RedshiftServerlessService.WorkgroupTarget workgroupTarget() {
+ return new RedshiftServerlessService.WorkgroupTarget(
+ "arn:aws:redshift-serverless:us-east-1:" + ACCOUNT + ":workgroup/wg-id", "wg-1",
+ "127.0.0.1", 55433, "analytics", "root", "Secret123", "root");
+ }
+
@Test
- void rejectsWorkgroupName() {
+ void resolvesAWorkgroupAsTheNamespaceAdminWhenNoSecretIsGiven() {
+ when(serverless.getWorkgroupTarget("wg-1", REGION)).thenReturn(workgroupTarget());
ObjectNode req = mapper.createObjectNode();
req.put("WorkgroupName", "wg-1");
- req.put("Database", "dev");
- AwsException e = assertThrows(AwsException.class,
- () -> resolver(mock(RedshiftService.class), mock(SecretsManagerService.class)).resolve(req, REGION));
- assertEquals("ValidationException", e.getErrorCode());
+ req.put("Database", "analytics");
+
+ RedshiftDataResourceResolver.DatabaseTarget target =
+ resolver(mock(RedshiftService.class), mock(SecretsManagerService.class)).resolve(req, REGION);
+
+ assertEquals("arn:aws:redshift-serverless:us-east-1:" + ACCOUNT + ":workgroup/wg-id", target.arn());
+ assertEquals("127.0.0.1", target.host());
+ assertEquals(55433, target.port());
+ assertEquals("analytics", target.database());
+ assertEquals("root", target.user());
+ assertEquals("Secret123", target.password());
+ }
+
+ @Test
+ void aWorkgroupWithASecretConnectsWithTheSecretsCredentials() {
+ when(serverless.getWorkgroupTarget("wg-1", REGION)).thenReturn(workgroupTarget());
+ SecretsManagerService secrets = mock(SecretsManagerService.class);
+ String secretArn = "arn:aws:secretsmanager:us-east-1:" + ACCOUNT + ":secret:wg-creds";
+ SecretVersion version = new SecretVersion();
+ version.setSecretString("{\"username\":\"root\",\"password\":\"Secret123\"}");
+ when(secrets.getSecretValue(eq(secretArn), any(), any(), eq(REGION))).thenReturn(version);
+ ObjectNode req = mapper.createObjectNode();
+ req.put("WorkgroupName", "wg-1");
+ req.put("Database", "analytics");
+ req.put("SecretArn", secretArn);
+
+ RedshiftDataResourceResolver.DatabaseTarget target =
+ resolver(mock(RedshiftService.class), secrets).resolve(req, REGION);
+
+ assertEquals("root", target.user());
+ assertEquals("Secret123", target.password());
+ assertEquals(55433, target.port());
+ }
+
+ @Test
+ void aWorkgroupRejectsAClusterIdentifierAndAWrongDatabase() {
+ when(serverless.getWorkgroupTarget("wg-1", REGION)).thenReturn(workgroupTarget());
+ RedshiftDataResourceResolver resolver = resolver(mock(RedshiftService.class), mock(SecretsManagerService.class));
+
+ ObjectNode both = mapper.createObjectNode();
+ both.put("WorkgroupName", "wg-1");
+ both.put("ClusterIdentifier", "wh");
+ both.put("Database", "analytics");
+ assertEquals("ValidationException", assertThrows(AwsException.class, () -> resolver.resolve(both, REGION))
+ .getErrorCode());
+
+ ObjectNode wrongDatabase = mapper.createObjectNode();
+ wrongDatabase.put("WorkgroupName", "wg-1");
+ wrongDatabase.put("Database", "dev");
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> resolver.resolve(wrongDatabase, REGION)).getErrorCode());
+ }
+
+ @Test
+ void aWorkgroupStillRequiresADatabaseAndPropagatesAMissingWorkgroup() {
+ when(serverless.getWorkgroupTarget("absent-wg", REGION))
+ .thenThrow(new AwsException("ResourceNotFoundException", "no such workgroup", 404));
+ RedshiftDataResourceResolver resolver = resolver(mock(RedshiftService.class), mock(SecretsManagerService.class));
+
+ ObjectNode noDatabase = mapper.createObjectNode();
+ noDatabase.put("WorkgroupName", "wg-1");
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> resolver.resolve(noDatabase, REGION)).getErrorCode());
+
+ ObjectNode absent = mapper.createObjectNode();
+ absent.put("WorkgroupName", "absent-wg");
+ absent.put("Database", "dev");
+ assertEquals("ResourceNotFoundException", assertThrows(AwsException.class,
+ () -> resolver.resolve(absent, REGION)).getErrorCode());
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java
index b23e164403..4c5354da94 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.redshiftdata;
+import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -62,6 +63,50 @@ class RedshiftDataServiceTest {
return om.createObjectNode().put("Id", id);
}
+ @Test
+ void aWorkgroupStatementNamesItsWorkgroupAndOmitsTheClusterIdentifier() {
+ ObjectNode request = om.createObjectNode();
+ request.put("Sql", "select 1");
+ request.put("WorkgroupName", "wg-1");
+ request.put("Database", "dev");
+
+ ObjectNode executed = service.executeStatement(request, REGION);
+
+ assertEquals("wg-1", executed.get("WorkgroupName").asText());
+ assertFalse(executed.has("ClusterIdentifier"),
+ "AWS does not return ClusterIdentifier when connecting to a serverless workgroup");
+ ObjectNode described = service.describeStatement(idOf(executed.get("Id").asText()));
+ assertEquals("wg-1", described.get("WorkgroupName").asText());
+ assertFalse(described.has("ClusterIdentifier"));
+ }
+
+ @Test
+ void aClusterStatementKeepsItsNullWorkgroupName() {
+ ObjectNode executed = service.executeStatement(req("select 1"), REGION);
+
+ assertEquals("wh", executed.get("ClusterIdentifier").asText());
+ assertTrue(executed.get("WorkgroupName").isNull());
+ ObjectNode described = service.describeStatement(idOf(executed.get("Id").asText()));
+ assertEquals("wh", described.get("ClusterIdentifier").asText());
+ assertTrue(described.get("WorkgroupName").isNull());
+ }
+
+ @Test
+ void aWorkgroupBatchPassesItsWorkgroupToEverySubStatement() {
+ ObjectNode request = om.createObjectNode();
+ request.putArray("Sqls").add("select 1").add("select 2");
+ request.put("WorkgroupName", "wg-1");
+ request.put("Database", "dev");
+
+ ObjectNode executed = service.batchExecuteStatement(request, REGION);
+
+ ObjectNode described = service.describeStatement(idOf(executed.get("Id").asText()));
+ assertEquals("wg-1", described.get("WorkgroupName").asText());
+ for (JsonNode sub : described.get("SubStatements")) {
+ assertFalse(sub.has("ClusterIdentifier"));
+ }
+ }
+
/**
* Messages checked against a real Redshift cluster on 2026-09-13.
*/
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpointsTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpointsTest.java
new file mode 100644
index 0000000000..de9bb4db13
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessEndpointsTest.java
@@ -0,0 +1,65 @@
+package io.github.hectorvent.floci.services.redshiftserverless;
+
+import io.github.hectorvent.floci.services.redshift.RedshiftService;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class RedshiftServerlessEndpointsTest {
+
+ private final RedshiftService redshift = mock(RedshiftService.class);
+ private final RedshiftServerlessEndpoints endpoints = new RedshiftServerlessEndpoints(redshift);
+
+ @Test
+ void allocateReservesAPortFromTheSharedPoolAndAdvertisesIt() {
+ when(redshift.reserveProxyPort()).thenReturn(7101);
+ when(redshift.advertisedEndpoint(7101)).thenReturn(new Endpoint("localhost", 7101));
+
+ Endpoint allocated = endpoints.allocate();
+
+ assertEquals("localhost", allocated.getAddress());
+ assertEquals(7101, allocated.getPort());
+ }
+
+ @Test
+ void restoreKeepsThePersistedPortAndRebuildsTheAddressFromCurrentConfiguration() {
+ when(redshift.reserveProxyPort(7105)).thenReturn(true);
+ when(redshift.advertisedEndpoint(7105)).thenReturn(new Endpoint("new-host", 7105));
+
+ Endpoint restored = endpoints.restore(new Endpoint("old-host", 7105));
+
+ assertEquals("new-host", restored.getAddress());
+ assertEquals(7105, restored.getPort());
+ verify(redshift, never()).reserveProxyPort();
+ }
+
+ @Test
+ void restoreAllocatesAFreshEndpointWhenThePersistedPortIsTaken() {
+ when(redshift.reserveProxyPort(7105)).thenReturn(false);
+ when(redshift.reserveProxyPort()).thenReturn(7110);
+ when(redshift.advertisedEndpoint(7110)).thenReturn(new Endpoint("localhost", 7110));
+
+ assertEquals(7110, endpoints.restore(new Endpoint("localhost", 7105)).getPort());
+ }
+
+ @Test
+ void restoreOfAWorkgroupWithNoEndpointAllocatesOne() {
+ when(redshift.reserveProxyPort()).thenReturn(7111);
+ when(redshift.advertisedEndpoint(7111)).thenReturn(new Endpoint("localhost", 7111));
+
+ assertEquals(7111, endpoints.restore(null).getPort());
+ }
+
+ @Test
+ void releaseReturnsThePortAndToleratesNull() {
+ endpoints.release(new Endpoint("localhost", 7120));
+ endpoints.release(null);
+
+ verify(redshift).releaseProxyPort(7120);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java
index 271e83f86b..0feda779c8 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java
@@ -1,15 +1,23 @@
package io.github.hectorvent.floci.services.redshiftserverless;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.matchesPattern;
+import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.when;
@QuarkusTest
class RedshiftServerlessIntegrationTest {
@@ -24,6 +32,22 @@ class RedshiftServerlessIntegrationTest {
RestAssuredJsonUtils.configureAwsContentTypes();
}
+ /**
+ * The metadata behaviour under test needs no PostgreSQL container, so the runtime is replaced:
+ * these tests then run without Docker, and the real container and proxy are exercised by
+ * {@code RedshiftServerlessRuntimeIntegrationTest}.
+ */
+ @InjectMock
+ RedshiftServerlessRuntime runtime;
+
+ @BeforeEach
+ void stubRuntime() {
+ when(runtime.generatePassword()).thenReturn("Generated123");
+ when(runtime.start(any(String.class), any(String.class), any(String.class), any(String.class),
+ any(String.class), any(String.class), any(Endpoint.class), anyBoolean(), any()))
+ .thenReturn(new RedshiftServerlessRuntime.Backend("127.0.0.1", 55432));
+ }
+
@Test
void createAppliesAwsDefaultsAndNeverReturnsTheAdminPassword() {
call("CreateNamespace", """
@@ -163,6 +187,100 @@ class RedshiftServerlessIntegrationTest {
.body("__type", equalTo("ResourceNotFoundException"));
}
+ @Test
+ void workgroupLifecycleIsVisibleThroughSeparateReads() {
+ call("CreateNamespace", "{\"namespaceName\":\"wg-life-ns\",\"adminUsername\":\"admin\"}").statusCode(200);
+
+ call("CreateWorkgroup", """
+ {"workgroupName":"wg-life","namespaceName":"wg-life-ns","baseCapacity":32,
+ "publiclyAccessible":true,"tags":[{"key":"env","value":"dev"}]}
+ """)
+ .statusCode(200)
+ .body("workgroup.workgroupName", equalTo("wg-life"))
+ .body("workgroup.namespaceName", equalTo("wg-life-ns"))
+ .body("workgroup.status", equalTo("AVAILABLE"))
+ .body("workgroup.baseCapacity", equalTo(32))
+ .body("workgroup.port", equalTo(5439))
+ .body("workgroup.trackName", equalTo("current"))
+ .body("workgroup.pricePerformanceTarget.status", equalTo("DISABLED"))
+ .body("workgroup.endpoint.address", notNullValue())
+ .body("workgroup.endpoint.port", greaterThan(0))
+ .body("workgroup.workgroupArn", matchesPattern(
+ "arn:aws:redshift-serverless:us-east-1:\\d{12}:workgroup/"
+ + "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"))
+ .body("workgroup.creationDate", matchesPattern(
+ "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z"));
+
+ call("GetWorkgroup", "{\"workgroupName\":\"wg-life\"}")
+ .statusCode(200)
+ .body("workgroup.publiclyAccessible", equalTo(true));
+
+ call("ListWorkgroups", "{}")
+ .statusCode(200)
+ .body("workgroups.workgroupName", hasItem("wg-life"));
+
+ call("UpdateWorkgroup", "{\"workgroupName\":\"wg-life\",\"baseCapacity\":64,\"trackName\":\"trailing\"}")
+ .statusCode(200)
+ .body("workgroup.baseCapacity", equalTo(64))
+ .body("workgroup.trackName", equalTo("current"))
+ .body("workgroup.pendingTrackName", equalTo("trailing"));
+
+ call("DeleteNamespace", "{\"namespaceName\":\"wg-life-ns\"}")
+ .statusCode(409)
+ .body("__type", equalTo("ConflictException"));
+
+ call("DeleteWorkgroup", "{\"workgroupName\":\"wg-life\"}")
+ .statusCode(200)
+ .body("workgroup.status", equalTo("DELETING"));
+ call("GetWorkgroup", "{\"workgroupName\":\"wg-life\"}")
+ .statusCode(404)
+ .body("__type", equalTo("ResourceNotFoundException"));
+ call("DeleteNamespace", "{\"namespaceName\":\"wg-life-ns\"}").statusCode(200);
+ }
+
+ @Test
+ void workgroupTagsAreEditableThroughTheWorkgroupArn() {
+ call("CreateNamespace", "{\"namespaceName\":\"wg-tag-ns\",\"adminUsername\":\"admin\"}").statusCode(200);
+ String arn = call("CreateWorkgroup", """
+ {"workgroupName":"wg-tag","namespaceName":"wg-tag-ns","tags":[{"key":"env","value":"dev"}]}
+ """)
+ .statusCode(200)
+ .extract().jsonPath().getString("workgroup.workgroupArn");
+
+ call("TagResource", "{\"resourceArn\":\"" + arn + "\",\"tags\":[{\"key\":\"team\",\"value\":\"data\"}]}")
+ .statusCode(200);
+ call("ListTagsForResource", "{\"resourceArn\":\"" + arn + "\"}")
+ .statusCode(200)
+ .body("tags.key", hasItem("env"))
+ .body("tags.key", hasItem("team"));
+ call("UntagResource", "{\"resourceArn\":\"" + arn + "\",\"tagKeys\":[\"env\"]}").statusCode(200);
+ call("ListTagsForResource", "{\"resourceArn\":\"" + arn + "\"}")
+ .statusCode(200)
+ .body("tags.size()", equalTo(1));
+
+ call("DeleteWorkgroup", "{\"workgroupName\":\"wg-tag\"}").statusCode(200);
+ call("DeleteNamespace", "{\"namespaceName\":\"wg-tag-ns\"}").statusCode(200);
+ }
+
+ @Test
+ void workgroupCreateRejectsAnUnknownNamespaceADuplicateAndABadPort() {
+ call("CreateWorkgroup", "{\"workgroupName\":\"wg-orphan\",\"namespaceName\":\"absent-ns\"}")
+ .statusCode(404)
+ .body("__type", equalTo("ResourceNotFoundException"));
+
+ call("CreateNamespace", "{\"namespaceName\":\"wg-err-ns\",\"adminUsername\":\"admin\"}").statusCode(200);
+ call("CreateWorkgroup", "{\"workgroupName\":\"wg-err\",\"namespaceName\":\"wg-err-ns\",\"port\":5000}")
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"));
+ call("CreateWorkgroup", "{\"workgroupName\":\"wg-err\",\"namespaceName\":\"wg-err-ns\"}").statusCode(200);
+ call("CreateWorkgroup", "{\"workgroupName\":\"wg-err\",\"namespaceName\":\"wg-err-ns\"}")
+ .statusCode(409)
+ .body("__type", equalTo("ConflictException"));
+
+ call("DeleteWorkgroup", "{\"workgroupName\":\"wg-err\"}").statusCode(200);
+ call("DeleteNamespace", "{\"namespaceName\":\"wg-err-ns\"}").statusCode(200);
+ }
+
private static io.restassured.response.ValidatableResponse call(String action, String body) {
return given()
.header("X-Amz-Target", TARGET_PREFIX + action)
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java
index bda254351e..694f07288c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandlerTest.java
@@ -7,18 +7,25 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.redshift.RedshiftIamDbUserResolver;
+import io.github.hectorvent.floci.services.redshift.TempCredential;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
+import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.time.Instant;
+import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -37,6 +44,7 @@ class RedshiftServerlessJsonHandlerTest {
private final ObjectMapper mapper = new ObjectMapper();
private RedshiftServerlessJsonHandler handler;
+ private RedshiftIamDbUserResolver iamDbUserResolver;
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
@@ -51,8 +59,171 @@ class RedshiftServerlessJsonHandlerTest {
.thenAnswer(invocation -> "arn:aws:redshift-serverless:"
+ invocation.getArgument(1, String.class) + ":" + ACCOUNT_ID + ":"
+ invocation.getArgument(2, String.class));
+ AccountAwareStorageBackend<Workgroup> workgroupStore = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
+ when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-workgroups.json"),
+ any(TypeReference.class))).thenReturn((AccountAwareStorageBackend) workgroupStore);
+ RedshiftServerlessEndpoints endpoints = mock(RedshiftServerlessEndpoints.class);
+ when(endpoints.allocate()).thenReturn(new Endpoint("localhost", 7100));
+ RedshiftServerlessRuntime runtime = mock(RedshiftServerlessRuntime.class);
+ when(runtime.generatePassword()).thenReturn("Generated123");
+ when(runtime.start(any(String.class), any(String.class), any(String.class), any(String.class),
+ any(String.class), any(String.class), any(Endpoint.class), anyBoolean(), any()))
+ .thenReturn(new RedshiftServerlessRuntime.Backend("127.0.0.1", 55432));
+ when(runtime.issueCredential(any(), any(), any(), any(), anyInt())).thenAnswer(invocation ->
+ new TempCredential(invocation.getArgument(3), "tempPassword1",
+ Instant.ofEpochSecond(1_800_000_000L), List.of()));
+ iamDbUserResolver = mock(RedshiftIamDbUserResolver.class);
+ when(iamDbUserResolver.resolveDbUser("AUTH")).thenReturn("IAM:alice");
handler = new RedshiftServerlessJsonHandler(
- new RedshiftServerlessService(storageFactory, regionResolver), mapper);
+ new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime), mapper,
+ iamDbUserResolver);
+ }
+
+ @Test
+ void workgroupTimestampIsAnIso8601StringAndTheShapeFollowsTheApiModel() {
+ create("shape-ns");
+
+ JsonNode workgroup = body(handler.handle("CreateWorkgroup", parse("""
+ {"workgroupName":"shape-wg","namespaceName":"shape-ns","baseCapacity":32,"maxCapacity":64,
+ "configParameters":[{"parameterKey":"datestyle","parameterValue":"ISO, MDY"}],
+ "securityGroupIds":["sg-1"],"subnetIds":["subnet-1"],
+ "pricePerformanceTarget":{"status":"ENABLED","level":50},
+ "tags":[{"key":"env","value":"dev"}]}
+ """), REGION)).get("workgroup");
+
+ assertTrue(workgroup.get("creationDate").isTextual());
+ assertTrue(workgroup.get("creationDate").textValue()
+ .matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z"));
+ assertEquals("shape-wg", workgroup.get("workgroupName").textValue());
+ assertEquals("shape-ns", workgroup.get("namespaceName").textValue());
+ assertEquals("AVAILABLE", workgroup.get("status").textValue());
+ assertEquals(32, workgroup.get("baseCapacity").intValue());
+ assertEquals(64, workgroup.get("maxCapacity").intValue());
+ assertEquals(5439, workgroup.get("port").intValue());
+ assertEquals("localhost", workgroup.get("endpoint").get("address").textValue());
+ assertEquals(7100, workgroup.get("endpoint").get("port").intValue());
+ assertEquals("datestyle", workgroup.get("configParameters").get(0).get("parameterKey").textValue());
+ assertEquals("sg-1", workgroup.get("securityGroupIds").get(0).textValue());
+ assertEquals(50, workgroup.get("pricePerformanceTarget").get("level").intValue());
+ assertEquals("ENABLED", workgroup.get("pricePerformanceTarget").get("status").textValue());
+ assertTrue(workgroup.get("workgroupArn").textValue().contains(":workgroup/"));
+ assertFalse(workgroup.has("pendingTrackName"));
+ assertFalse(workgroup.has("workgroupVersion"));
+ }
+
+ @Test
+ void getCredentialsReturnsEpochSecondNumbersAndTheCallersDatabaseUser() {
+ create("cred-shape-ns");
+ body(handler.handle("CreateWorkgroup",
+ parse("{\"workgroupName\":\"cred-shape-wg\",\"namespaceName\":\"cred-shape-ns\"}"), REGION));
+
+ JsonNode credentials = body(handler.handle("GetCredentials",
+ parse("{\"workgroupName\":\"cred-shape-wg\",\"dbName\":\"dev\",\"durationSeconds\":1800}"),
+ REGION, "AUTH"));
+
+ assertEquals("IAM:alice", credentials.get("dbUser").textValue());
+ assertEquals("tempPassword1", credentials.get("dbPassword").textValue());
+ assertTrue(credentials.get("expiration").isNumber(),
+ "expiration has no timestamp trait, so it is epoch seconds as a JSON number");
+ assertEquals(1_800_000_000L, credentials.get("expiration").longValue());
+ assertEquals(1_800_000_000L, credentials.get("nextRefreshTime").longValue());
+ }
+
+ @Test
+ void getCredentialsRejectsABadDurationAMissingWorkgroupAndACustomDomain() {
+ create("cred-err-ns");
+ body(handler.handle("CreateWorkgroup",
+ parse("{\"workgroupName\":\"cred-err-wg\",\"namespaceName\":\"cred-err-ns\"}"), REGION));
+
+ Response tooShort = handler.handle("GetCredentials",
+ parse("{\"workgroupName\":\"cred-err-wg\",\"durationSeconds\":60}"), REGION, "AUTH");
+ assertEquals(400, tooShort.getStatus());
+ assertEquals("ValidationException", errorType(tooShort));
+
+ Response wrongType = handler.handle("GetCredentials",
+ parse("{\"workgroupName\":\"cred-err-wg\",\"durationSeconds\":\"900\"}"), REGION, "AUTH");
+ assertEquals(400, wrongType.getStatus());
+
+ Response missing = handler.handle("GetCredentials", parse("{\"workgroupName\":\"absent-wg\"}"), REGION, "AUTH");
+ assertEquals(404, missing.getStatus());
+ assertEquals("ResourceNotFoundException", errorType(missing));
+
+ Response customDomain = handler.handle("GetCredentials",
+ parse("{\"customDomainName\":\"wg.example.com\"}"), REGION, "AUTH");
+ assertEquals(400, customDomain.getStatus());
+ assertEquals("ValidationException", errorType(customDomain));
+ }
+
+ @Test
+ void theAdminPasswordIsAcceptedOnCreateAndUpdateAndNeverReturned() {
+ JsonNode created = body(handler.handle("CreateNamespace",
+ parse("{\"namespaceName\":\"pw-shape-ns\",\"adminUsername\":\"root\",\"adminUserPassword\":\"Secret123\"}"),
+ REGION)).get("namespace");
+ assertFalse(created.has("adminUserPassword"));
+
+ JsonNode updated = body(handler.handle("UpdateNamespace",
+ parse("{\"namespaceName\":\"pw-shape-ns\",\"adminUserPassword\":\"Changed123\"}"), REGION))
+ .get("namespace");
+ assertFalse(updated.has("adminUserPassword"));
+ }
+
+ @Test
+ void listWorkgroupsReturnsTheWorkgroupsMember() {
+ create("list-shape-ns");
+ body(handler.handle("CreateWorkgroup",
+ parse("{\"workgroupName\":\"list-shape-wg\",\"namespaceName\":\"list-shape-ns\"}"), REGION));
+
+ JsonNode listed = body(handler.handle("ListWorkgroups", mapper.createObjectNode(), REGION));
+
+ assertEquals("list-shape-wg", listed.get("workgroups").get(0).get("workgroupName").textValue());
+ assertFalse(listed.has("nextToken"));
+ }
+
+ @Test
+ void wrongTypedWorkgroupMembersAreRejected() {
+ create("typed-ns");
+ for (String member : new String[] {
+ "\"baseCapacity\":\"32\"", "\"baseCapacity\":3.5", "\"port\":\"5439\"",
+ "\"publiclyAccessible\":\"true\"", "\"enhancedVpcRouting\":1",
+ "\"configParameters\":\"datestyle\"", "\"configParameters\":[\"datestyle\"]",
+ "\"pricePerformanceTarget\":\"ENABLED\"", "\"securityGroupIds\":\"sg-1\"",
+ "\"subnetIds\":[7]", "\"tags\":\"env=dev\""}) {
+ Response response = handler.handle("CreateWorkgroup", parse(
+ "{\"workgroupName\":\"typed-wg\",\"namespaceName\":\"typed-ns\"," + member + "}"), REGION);
+
+ assertEquals(400, response.getStatus(), member + " must be rejected");
+ assertEquals("ValidationException", errorType(response), member);
+ }
+ }
+
+ @Test
+ void updateWorkgroupKeepsOmittedMembersAndReportsAPendingTrack() {
+ create("upd-shape-ns");
+ body(handler.handle("CreateWorkgroup",
+ parse("{\"workgroupName\":\"upd-shape-wg\",\"namespaceName\":\"upd-shape-ns\",\"baseCapacity\":32}"),
+ REGION));
+
+ JsonNode updated = body(handler.handle("UpdateWorkgroup",
+ parse("{\"workgroupName\":\"upd-shape-wg\",\"trackName\":\"trailing\"}"), REGION)).get("workgroup");
+
+ assertEquals(32, updated.get("baseCapacity").intValue());
+ assertEquals("current", updated.get("trackName").textValue());
+ assertEquals("trailing", updated.get("pendingTrackName").textValue());
+ }
+
+ @Test
+ void deleteWorkgroupReturnsTheDeletedWorkgroupInTheDeletingState() {
+ create("del-shape-ns");
+ body(handler.handle("CreateWorkgroup",
+ parse("{\"workgroupName\":\"del-shape-wg\",\"namespaceName\":\"del-shape-ns\"}"), REGION));
+
+ JsonNode deleted = body(handler.handle("DeleteWorkgroup",
+ request("workgroupName", "del-shape-wg"), REGION)).get("workgroup");
+
+ assertEquals("DELETING", deleted.get("status").textValue());
+ Response missing = handler.handle("GetWorkgroup", request("workgroupName", "del-shape-wg"), REGION);
+ assertEquals(404, missing.getStatus());
+ assertEquals("ResourceNotFoundException", errorType(missing));
}
@Test
@@ -116,7 +287,7 @@ class RedshiftServerlessJsonHandlerTest {
@Test
void anUnknownActionIsReportedAsUnknownOperation() {
- Response response = handler.handle("CreateWorkgroup", mapper.createObjectNode(), REGION);
+ Response response = handler.handle("CreateSnapshot", mapper.createObjectNode(), REGION);
assertEquals(400, response.getStatus());
assertEquals("UnknownOperationException", errorType(response));
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeIntegrationTest.java
new file mode 100644
index 0000000000..fd721f076c
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeIntegrationTest.java
@@ -0,0 +1,236 @@
+package io.github.hectorvent.floci.services.redshiftserverless;
+
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.ExtractableResponse;
+import io.restassured.response.Response;
+import jakarta.inject.Inject;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.Duration;
+import java.util.Objects;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Runs a real workgroup: the PostgreSQL container, the auth proxy on the advertised endpoint, the
+ * Redshift Data API addressed by {@code WorkgroupName}, and {@code GetCredentials}. Needs Docker.
+ */
+@QuarkusTest
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+class RedshiftServerlessRuntimeIntegrationTest {
+
+ private static final String NAMESPACE = "rt-it-ns";
+ private static final String WORKGROUP = "rt-it-wg";
+ private static final String ADMIN_PASSWORD = "Secret123";
+
+ private static final String REGION = "us-east-1";
+
+ @Inject
+ RedshiftServerlessService service;
+
+ private int endpointPort;
+ private String workgroupArn;
+ private boolean created;
+
+ /**
+ * Runs before each test but creates the workgroup once: Quarkus points RestAssured at the test
+ * server in its own per-test callback, so a {@code @BeforeAll} would still see the default port.
+ */
+ @BeforeEach
+ void ensureWorkgroup() {
+ Assumptions.assumeTrue(dockerAvailable(), "Docker is required for Redshift Serverless runtime tests");
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ if (created) {
+ return;
+ }
+ RestAssuredJsonUtils.awsAction("RedshiftServerless", "CreateNamespace", """
+ {"namespaceName":"%s","adminUsername":"admin","adminUserPassword":"%s","dbName":"dev"}
+ """.formatted(NAMESPACE, ADMIN_PASSWORD)).then().statusCode(200);
+ created = true;
+ ExtractableResponse<Response> workgroup = RestAssuredJsonUtils.awsAction("RedshiftServerless", "CreateWorkgroup", """
+ {"workgroupName":"%s","namespaceName":"%s"}
+ """.formatted(WORKGROUP, NAMESPACE)).then().statusCode(200).extract();
+ endpointPort = workgroup.path("workgroup.endpoint.port");
+ workgroupArn = workgroup.path("workgroup.workgroupArn");
+ }
+
+ /** Through the service: Quarkus has already pointed RestAssured away from the test server by now. */
+ @AfterAll
+ void deleteWorkgroup() {
+ if (created) {
+ service.deleteWorkgroup(WORKGROUP, REGION);
+ service.deleteNamespace(NAMESPACE, REGION);
+ }
+ }
+
+ private static boolean dockerAvailable() {
+ try {
+ Process p = new ProcessBuilder("docker", "version", "--format", "{{.Server.Version}}")
+ .redirectErrorStream(true).start();
+ return p.waitFor() == 0;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ private String jdbcUrl() {
+ // 127.0.0.1 rather than the advertised address: CI sets floci.emulator.hostname to a name
+ // that does not resolve from the test JVM.
+ return "jdbc:postgresql://127.0.0.1:" + endpointPort + "/dev";
+ }
+
+ private Connection connect(String user, String password) throws SQLException {
+ return Awaitility.await()
+ .atMost(Duration.ofSeconds(30))
+ .pollDelay(Duration.ZERO)
+ .pollInterval(Duration.ofMillis(500))
+ .ignoreExceptions()
+ .until(() -> DriverManager.getConnection(jdbcUrl(), user, password), Objects::nonNull);
+ }
+
+ private String executeAndWait(String sql) {
+ String id = RestAssuredJsonUtils.awsAction("RedshiftData", "ExecuteStatement", """
+ {"Sql": "%s", "WorkgroupName": "%s", "Database": "dev"}
+ """.formatted(sql, WORKGROUP)).then().statusCode(200)
+ .extract().path("Id");
+ Awaitility.await().atMost(Duration.ofSeconds(30)).pollDelay(Duration.ZERO)
+ .pollInterval(Duration.ofMillis(250)).until(() -> {
+ String status = RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement",
+ "{\"Id\":\"" + id + "\"}").then().statusCode(200).extract().path("Status");
+ assertFalse("FAILED".equals(status) || "ABORTED".equals(status),
+ () -> "statement " + id + " ended " + status);
+ return "FINISHED".equals(status);
+ });
+ return id;
+ }
+
+ @Test
+ void theAdvertisedEndpointAcceptsTheNamespaceAdminAndRefusesAWrongPassword() throws SQLException {
+ try (Connection connection = connect("admin", ADMIN_PASSWORD);
+ Statement statement = connection.createStatement();
+ ResultSet result = statement.executeQuery("select 1")) {
+ assertTrue(result.next());
+ assertEquals(1, result.getInt(1));
+ }
+
+ assertThrows(SQLException.class, () -> DriverManager.getConnection(jdbcUrl(), "admin", "wrong-password"));
+ }
+
+ @Test
+ void theDataApiRunsStatementsAgainstTheWorkgroupByNameAndByArn() throws SQLException {
+ String createId = executeAndWait("CREATE TABLE sl_people (id int, name varchar(20))");
+ executeAndWait("INSERT INTO sl_people VALUES (1, 'a'), (2, 'b')");
+ String selectId = executeAndWait("SELECT id, name FROM sl_people ORDER BY id");
+
+ ExtractableResponse<Response> described = RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement",
+ "{\"Id\":\"" + selectId + "\"}").then().statusCode(200).extract();
+ assertEquals(WORKGROUP, described.path("WorkgroupName"));
+ assertNull(described.path("ClusterIdentifier"));
+ assertNotNull(createId);
+
+ ExtractableResponse<Response> result = RestAssuredJsonUtils.awsAction("RedshiftData", "GetStatementResult",
+ "{\"Id\":\"" + selectId + "\"}").then().statusCode(200).extract();
+ assertEquals(2, ((Number) result.path("TotalNumRows")).intValue());
+ assertEquals("a", result.path("Records[0][1].stringValue"));
+
+ // The same table is visible through the endpoint, and the workgroup ARN addresses the workgroup too.
+ try (Connection connection = connect("admin", ADMIN_PASSWORD);
+ Statement statement = connection.createStatement();
+ ResultSet rows = statement.executeQuery("select count(*) from sl_people")) {
+ assertTrue(rows.next());
+ assertEquals(2, rows.getInt(1));
+ }
+ RestAssuredJsonUtils.awsAction("RedshiftData", "ExecuteStatement", """
+ {"Sql": "select 1", "WorkgroupName": "%s", "Database": "dev"}
+ """.formatted(workgroupArn)).then().statusCode(200);
+ }
+
+ @Test
+ void theDataApiRejectsAWrongDatabaseAndBothIdentifiers() {
+ RestAssuredJsonUtils.awsAction("RedshiftData", "ExecuteStatement", """
+ {"Sql": "select 1", "WorkgroupName": "%s", "Database": "other"}
+ """.formatted(WORKGROUP)).then().statusCode(400);
+ RestAssuredJsonUtils.awsAction("RedshiftData", "ExecuteStatement", """
+ {"Sql": "select 1", "WorkgroupName": "%s", "ClusterIdentifier": "wh", "Database": "dev"}
+ """.formatted(WORKGROUP)).then().statusCode(400);
+ }
+
+ @Test
+ void getCredentialsMintsAPasswordTheEndpointAccepts() throws SQLException {
+ ExtractableResponse<Response> credentials = RestAssuredJsonUtils.awsAction("RedshiftServerless", "GetCredentials",
+ "{\"workgroupName\":\"" + WORKGROUP + "\",\"dbName\":\"dev\"}").then().statusCode(200).extract();
+ String dbUser = credentials.path("dbUser");
+ String dbPassword = credentials.path("dbPassword");
+ assertNotNull(dbUser);
+ assertNotNull(dbPassword);
+
+ try (Connection connection = connect(dbUser, dbPassword)) {
+ assertTrue(connection.isValid(5));
+ }
+ assertThrows(SQLException.class, () -> DriverManager.getConnection(jdbcUrl(), dbUser, "stale-password"));
+ }
+
+ @Test
+ void anAdminPasswordChangeAppliesToNewConnections() throws SQLException {
+ RestAssuredJsonUtils.awsAction("RedshiftServerless", "UpdateNamespace",
+ "{\"namespaceName\":\"" + NAMESPACE + "\",\"adminUserPassword\":\"Rotated123\"}")
+ .then().statusCode(200);
+ try {
+ assertThrows(SQLException.class, () -> DriverManager.getConnection(jdbcUrl(), "admin", ADMIN_PASSWORD));
+ try (Connection connection = connect("admin", "Rotated123")) {
+ assertTrue(connection.isValid(5));
+ }
+ } finally {
+ RestAssuredJsonUtils.awsAction("RedshiftServerless", "UpdateNamespace",
+ "{\"namespaceName\":\"" + NAMESPACE + "\",\"adminUserPassword\":\"" + ADMIN_PASSWORD + "\"}");
+ }
+ }
+
+ @Test
+ void deletingTheWorkgroupClosesItsEndpoint() {
+ String namespace = "rt-it-del-ns";
+ String workgroup = "rt-it-del-wg";
+ RestAssuredJsonUtils.awsAction("RedshiftServerless", "CreateNamespace", """
+ {"namespaceName":"%s","adminUsername":"admin","adminUserPassword":"%s"}
+ """.formatted(namespace, ADMIN_PASSWORD)).then().statusCode(200);
+ int port = RestAssuredJsonUtils.awsAction("RedshiftServerless", "CreateWorkgroup", """
+ {"workgroupName":"%s","namespaceName":"%s"}
+ """.formatted(workgroup, namespace)).then().statusCode(200).extract().path("workgroup.endpoint.port");
+ try {
+ assertTrue(isListening(port), "the endpoint must be live once the workgroup is created");
+ } finally {
+ RestAssuredJsonUtils.awsAction("RedshiftServerless", "DeleteWorkgroup",
+ "{\"workgroupName\":\"" + workgroup + "\"}").then().statusCode(200);
+ RestAssuredJsonUtils.awsAction("RedshiftServerless", "DeleteNamespace",
+ "{\"namespaceName\":\"" + namespace + "\"}").then().statusCode(200);
+ }
+ assertFalse(isListening(port), "the endpoint must be closed once the workgroup is deleted");
+ }
+
+ private static boolean isListening(int port) {
+ try (Socket socket = new Socket()) {
+ socket.connect(new InetSocketAddress("127.0.0.1", port), 1000);
+ return true;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeTest.java
new file mode 100644
index 0000000000..a7c5915bf8
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeTest.java
@@ -0,0 +1,217 @@
+package io.github.hectorvent.floci.services.redshiftserverless;
+
+import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;
+import io.github.hectorvent.floci.services.redshift.RedshiftCredentialBroker;
+import io.github.hectorvent.floci.services.redshift.TempCredential;
+import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerHandle;
+import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerManager;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
+import io.github.hectorvent.floci.services.redshift.proxy.RedshiftProxyManager;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.InOrder;
+
+import java.util.List;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class RedshiftServerlessRuntimeTest {
+
+ private static final String ACCOUNT = "123456789012";
+ private static final String REGION = "us-east-1";
+ private static final String BACKEND_ID = "serverless_us-east-1_my-wg";
+ private static final String RELAY_KEY = ACCOUNT + ":" + BACKEND_ID;
+ private static final Endpoint ENDPOINT = new Endpoint("localhost", 7100);
+
+ private final RedshiftContainerManager containers = mock(RedshiftContainerManager.class);
+ private final RedshiftProxyManager proxies = mock(RedshiftProxyManager.class);
+ private final RedshiftCredentialBroker broker = new RedshiftCredentialBroker();
+ private final RedshiftServerlessRuntime runtime = new RedshiftServerlessRuntime(containers, proxies, broker);
+
+ private static RedshiftContainerHandle handle() {
+ return new RedshiftContainerHandle("container-id", BACKEND_ID, "172.17.0.5", 5432);
+ }
+
+ private PasswordValidator startAndCaptureValidator(RedshiftServerlessRuntime.AdminCredentials admin) {
+ when(containers.start(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics")).thenReturn(handle());
+ runtime.start(ACCOUNT, REGION, "my-wg", "root", "Secret123", "analytics", ENDPOINT, false,
+ () -> Optional.ofNullable(admin));
+ ArgumentCaptor<PasswordValidator> validator = ArgumentCaptor.forClass(PasswordValidator.class);
+ verify(proxies).startProxy(eq(RELAY_KEY), eq(7100), eq("172.17.0.5"), eq(5432), eq("localhost"),
+ eq("root"), eq("Secret123"), eq("analytics"), validator.capture(), eq(List.of()));
+ return validator.getValue();
+ }
+
+ @Test
+ void startRunsTheContainerThenFrontsItWithTheProxyOnTheAllocatedEndpoint() {
+ when(containers.start(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics")).thenReturn(handle());
+
+ RedshiftServerlessRuntime.Backend backend = runtime.start(ACCOUNT, REGION, "my-wg", "root", "Secret123",
+ "analytics", ENDPOINT, false, Optional::empty);
+
+ assertEquals(new RedshiftServerlessRuntime.Backend("172.17.0.5", 5432), backend);
+ InOrder order = inOrder(containers, proxies);
+ order.verify(containers).start(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics");
+ order.verify(proxies).startProxy(eq(RELAY_KEY), eq(7100), eq("172.17.0.5"), eq(5432), eq("localhost"),
+ eq("root"), eq("Secret123"), eq("analytics"), any(PasswordValidator.class), eq(List.of()));
+ verify(containers, never()).adoptOrStart(any(), any(), any(), any(), any());
+ }
+
+ @Test
+ void adoptReattachesToASurvivingContainer() {
+ when(containers.adoptOrStart(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics")).thenReturn(handle());
+
+ runtime.start(ACCOUNT, REGION, "my-wg", "root", "Secret123", "analytics", ENDPOINT, true, Optional::empty);
+
+ verify(containers, never()).start(any(), any(), any(), any(), any());
+ verify(proxies).startProxy(eq(RELAY_KEY), eq(7100), any(), anyInt(), any(), any(), any(), any(),
+ any(PasswordValidator.class), any());
+ }
+
+ @Test
+ void aProxyThatFailsToStartRollsBackTheContainerAndCredentials() {
+ when(containers.start(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics")).thenReturn(handle());
+ doThrow(new RuntimeException("port in use")).when(proxies).startProxy(any(), anyInt(), any(), anyInt(),
+ any(), any(), any(), any(), any(PasswordValidator.class), any());
+ broker.issue(ACCOUNT, BACKEND_ID, "IAM:alice", List.of(), 900);
+
+ assertThrows(RuntimeException.class, () -> runtime.start(ACCOUNT, REGION, "my-wg", "root", "Secret123",
+ "analytics", ENDPOINT, false, Optional::empty));
+
+ verify(proxies).stopProxy(RELAY_KEY);
+ verify(containers).stop(ACCOUNT, BACKEND_ID);
+ assertTrue(broker.resolve(ACCOUNT, BACKEND_ID, "IAM:alice").isEmpty());
+ }
+
+ @Test
+ void stopClosesTheProxyBeforeRemovingTheContainerAndRevokesCredentials() {
+ broker.issue(ACCOUNT, BACKEND_ID, "IAM:alice", List.of(), 900);
+
+ runtime.stop(ACCOUNT, REGION, "my-wg");
+
+ InOrder order = inOrder(proxies, containers);
+ order.verify(proxies).stopProxy(RELAY_KEY);
+ order.verify(containers).stop(ACCOUNT, BACKEND_ID);
+ assertTrue(broker.resolve(ACCOUNT, BACKEND_ID, "IAM:alice").isEmpty());
+ }
+
+ @Test
+ void aProxyThatCannotBeClosedStopsTheStopBeforeTheContainerIsTouched() {
+ doThrow(new RuntimeException("listener still bound")).when(proxies).stopProxy(RELAY_KEY);
+
+ assertThrows(RuntimeException.class, () -> runtime.stop(ACCOUNT, REGION, "my-wg"));
+
+ verify(containers, never()).stop(any(), any());
+ }
+
+ @Test
+ void changeMasterPasswordUpdatesTheRoleAndTheProxyBackendLeg() {
+ runtime.changeMasterPassword(ACCOUNT, REGION, "my-wg", "root", "analytics", "Secret123", "Changed123");
+
+ verify(containers).alterUserPassword(ACCOUNT, BACKEND_ID, "root", "Changed123", "analytics");
+ verify(proxies).updateMasterPassword(RELAY_KEY, "Changed123");
+ }
+
+ @Test
+ void aProxyUpdateThatFailsPutsTheRolePasswordBack() {
+ doThrow(new RuntimeException("proxy gone")).when(proxies).updateMasterPassword(RELAY_KEY, "Changed123");
+
+ assertThrows(RuntimeException.class, () -> runtime.changeMasterPassword(
+ ACCOUNT, REGION, "my-wg", "root", "analytics", "Secret123", "Changed123"));
+
+ verify(containers).alterUserPassword(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics");
+ }
+
+ @Test
+ void aFailedRollbackKeepsTheOriginalProxyFailureAsTheThrownError() {
+ RuntimeException proxyFailure = new RuntimeException("proxy gone");
+ doThrow(proxyFailure).when(proxies).updateMasterPassword(RELAY_KEY, "Changed123");
+ doThrow(new RuntimeException("psql failed")).when(containers)
+ .alterUserPassword(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics");
+
+ RuntimeException thrown = assertThrows(RuntimeException.class, () -> runtime.changeMasterPassword(
+ ACCOUNT, REGION, "my-wg", "root", "analytics", "Secret123", "Changed123"));
+
+ assertEquals(proxyFailure, thrown);
+ assertEquals(1, thrown.getSuppressed().length);
+ }
+
+ @Test
+ void restoreMasterPasswordLeavesTheProxyAloneWhenTheRoleCannotBeRestored() {
+ doThrow(new RuntimeException("psql failed")).when(containers)
+ .alterUserPassword(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics");
+
+ assertThrows(RuntimeException.class,
+ () -> runtime.restoreMasterPassword(ACCOUNT, REGION, "my-wg", "root", "analytics", "Secret123"));
+
+ verify(proxies, never()).updateMasterPassword(any(), any());
+ }
+
+ @Test
+ void restoreMasterPasswordResetsTheRoleAndTheProxy() {
+ runtime.restoreMasterPassword(ACCOUNT, REGION, "my-wg", "root", "analytics", "Secret123");
+
+ verify(containers).alterUserPassword(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics");
+ verify(proxies).updateMasterPassword(RELAY_KEY, "Secret123");
+ }
+
+ @Test
+ void theAdminPairIsMasterEquivalentAndAWrongAdminPasswordIsRejected() {
+ PasswordValidator validator = startAndCaptureValidator(
+ new RedshiftServerlessRuntime.AdminCredentials("root", "Secret123"));
+
+ assertEquals(PasswordValidator.AuthResult.MASTER_EQUIVALENT, validator.validate("root", "Secret123"));
+ assertEquals(PasswordValidator.AuthResult.REJECT, validator.validate("root", "wrong"));
+ }
+
+ @Test
+ void aLiveGetCredentialsPasswordIsMasterEquivalentAndAStaleOneForThatUserIsRejected() {
+ PasswordValidator validator = startAndCaptureValidator(
+ new RedshiftServerlessRuntime.AdminCredentials("root", "Secret123"));
+
+ TempCredential credential = runtime.issueCredential(ACCOUNT, REGION, "my-wg", "IAM:alice", 900);
+
+ assertEquals(PasswordValidator.AuthResult.MASTER_EQUIVALENT,
+ validator.validate("IAM:alice", credential.password()));
+ assertEquals(PasswordValidator.AuthResult.REJECT, validator.validate("IAM:alice", "stale"));
+ assertEquals(PasswordValidator.AuthResult.PASSTHROUGH, validator.validate("someone-else", "pw"));
+ }
+
+ @Test
+ void aNamespaceThatIsGoneVouchesForNothing() {
+ PasswordValidator validator = startAndCaptureValidator(null);
+ TempCredential credential = runtime.issueCredential(ACCOUNT, REGION, "my-wg", "IAM:alice", 900);
+
+ assertEquals(PasswordValidator.AuthResult.REJECT, validator.validate("root", "Secret123"));
+ assertEquals(PasswordValidator.AuthResult.REJECT, validator.validate("IAM:alice", credential.password()));
+ }
+
+ @Test
+ void theBackendIdCannotCollideWithAClusterIdentifier() {
+ assertEquals(BACKEND_ID, RedshiftServerlessRuntime.backendId(REGION, "my-wg"));
+ assertTrue(BACKEND_ID.contains("_"), "cluster identifiers cannot contain an underscore");
+ }
+
+ @Test
+ void generatedPasswordsAreLongAlphanumericAndDifferEachTime() {
+ String first = runtime.generatePassword();
+ String second = runtime.generatePassword();
+
+ assertEquals(32, first.length());
+ assertTrue(first.matches("[A-Za-z0-9]+"), first);
+ assertNotEquals(first, second);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java
index 17f4ccd60d..d4bf203423 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessServiceTest.java
@@ -6,23 +6,37 @@ import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.redshift.TempCredential;
+import io.github.hectorvent.floci.services.redshift.model.Endpoint;
+import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
+import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
+import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import java.time.Instant;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.UUID;
+import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.clearInvocations;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -33,6 +47,10 @@ class RedshiftServerlessServiceTest {
private RedshiftServerlessService service;
private AccountAwareStorageBackend<Namespace> store;
+ private AccountAwareStorageBackend<Workgroup> workgroupStore;
+ private RedshiftServerlessEndpoints endpoints;
+ private RedshiftServerlessRuntime runtime;
+ private int nextPort;
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
@@ -45,13 +63,24 @@ class RedshiftServerlessServiceTest {
store = spy(AccountAwareStorageBackend.inMemory(ACCOUNT_ID));
when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-namespaces.json"),
any(TypeReference.class))).thenReturn((AccountAwareStorageBackend) store);
+ workgroupStore = spy(AccountAwareStorageBackend.inMemory(ACCOUNT_ID));
+ when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-workgroups.json"),
+ any(TypeReference.class))).thenReturn((AccountAwareStorageBackend) workgroupStore);
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.buildArn(eq("redshift-serverless"), any(String.class), any(String.class)))
.thenAnswer(invocation -> "arn:aws:redshift-serverless:"
+ invocation.getArgument(1, String.class) + ":" + ACCOUNT_ID + ":"
+ invocation.getArgument(2, String.class));
- service = new RedshiftServerlessService(storageFactory, regionResolver);
+ nextPort = 7100;
+ endpoints = mock(RedshiftServerlessEndpoints.class);
+ when(endpoints.allocate()).thenAnswer(invocation -> new Endpoint("localhost", nextPort++));
+ runtime = mock(RedshiftServerlessRuntime.class);
+ when(runtime.generatePassword()).thenReturn("Generated123");
+ when(runtime.start(any(String.class), any(String.class), any(String.class), any(String.class),
+ any(String.class), any(String.class), any(Endpoint.class), anyBoolean(), any()))
+ .thenReturn(new RedshiftServerlessRuntime.Backend("127.0.0.1", 55432));
+ service = new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime);
}
@Test
@@ -300,7 +329,483 @@ class RedshiftServerlessServiceTest {
assertEquals(Map.of("env", "dev"), original.getTags());
}
+ @Test
+ void createWorkgroupAppliesAwsDefaultsAndAllocatesAnEndpoint() {
+ create("wg-ns");
+
+ Workgroup workgroup = createWorkgroup("wg-one", "wg-ns");
+
+ assertEquals("AVAILABLE", workgroup.getStatus());
+ assertEquals("wg-ns", workgroup.getNamespaceName());
+ assertEquals(128, workgroup.getBaseCapacity());
+ assertEquals(5439, workgroup.getPort());
+ assertEquals("ipv4", workgroup.getIpAddressType());
+ assertEquals("current", workgroup.getTrackName());
+ assertEquals("DISABLED", workgroup.getPricePerformanceTarget().getStatus());
+ assertEquals("localhost", workgroup.getEndpoint().getAddress());
+ assertEquals(7100, workgroup.getEndpoint().getPort());
+ assertEquals("arn:aws:redshift-serverless:us-east-1:" + ACCOUNT_ID + ":workgroup/" + workgroup.getWorkgroupId(),
+ workgroup.getWorkgroupArn());
+ assertEquals(workgroup.getWorkgroupId(), UUID.fromString(workgroup.getWorkgroupId()).toString());
+ assertEquals("wg-one", service.getWorkgroup("wg-one", REGION).getWorkgroupName());
+ }
+
+ @Test
+ void createWorkgroupRequiresAnExistingNamespace() {
+ assertEquals("ResourceNotFoundException", assertThrows(AwsException.class,
+ () -> createWorkgroup("wg-orphan", "absent-ns")).getErrorCode());
+ }
+
+ @Test
+ void aNamespaceHasAtMostOneWorkgroupAndWorkgroupNamesAreUnique() {
+ create("one-ns");
+ create("two-ns");
+ createWorkgroup("wg-first", "one-ns");
+
+ assertEquals("ConflictException", assertThrows(AwsException.class,
+ () -> createWorkgroup("wg-second", "one-ns")).getErrorCode());
+ assertEquals("ConflictException", assertThrows(AwsException.class,
+ () -> createWorkgroup("wg-first", "two-ns")).getErrorCode());
+ }
+
+ @Test
+ void workgroupNamesAndSettingsAreValidated() {
+ create("valid-ns");
+ for (String name : List.of("ab", "Upper-Case", "has_underscore")) {
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> createWorkgroup(name, "valid-ns")).getErrorCode(), name);
+ }
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> service.createWorkgroup("bad-port", "valid-ns", settings(5000, null, null, null), null, REGION))
+ .getErrorCode());
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> service.createWorkgroup("bad-ip", "valid-ns", settings(null, "ipv6", null, null), null, REGION))
+ .getErrorCode());
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> service.createWorkgroup("bad-target", "valid-ns",
+ settings(null, null, new PricePerformanceTarget("MAYBE", null), null), null, REGION))
+ .getErrorCode());
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> service.createWorkgroup("bad-level", "valid-ns",
+ settings(null, null, new PricePerformanceTarget("ENABLED", 101), null), null, REGION))
+ .getErrorCode());
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> service.createWorkgroup("bad-track", "valid-ns", settings(null, null, null, "no-dashes"), null, REGION))
+ .getErrorCode());
+ assertTrue(service.listWorkgroups(REGION, null, null).items().isEmpty(),
+ "a rejected create must leave nothing behind");
+ }
+
+ @Test
+ void acceptedPortsIncludeBothDocumentedRanges() {
+ create("ports-ns");
+ create("ports-two-ns");
+ assertEquals(5431, service.createWorkgroup("low-port", "ports-ns",
+ settings(5431, null, null, null), null, REGION).getPort());
+ assertEquals(8215, service.createWorkgroup("high-port", "ports-two-ns",
+ settings(8215, null, null, null), null, REGION).getPort());
+ }
+
+ @Test
+ void getAndDeleteRejectAnUnknownWorkgroup() {
+ assertEquals("ResourceNotFoundException", assertThrows(AwsException.class,
+ () -> service.getWorkgroup("absent-wg", REGION)).getErrorCode());
+ assertEquals("ResourceNotFoundException", assertThrows(AwsException.class,
+ () -> service.deleteWorkgroup("absent-wg", REGION)).getErrorCode());
+ }
+
+ @Test
+ void updateWorkgroupAppliesOnlySuppliedFieldsAndRecordsAPendingTrack() {
+ create("upd-ns");
+ service.createWorkgroup("upd-wg", "upd-ns", new WorkgroupSettings(32, 64, null, true, null,
+ List.of(new ConfigParameter("datestyle", "ISO, MDY")), List.of("sg-1"), List.of("subnet-1"),
+ null, null, null, null), null, REGION);
+
+ Workgroup updated = service.updateWorkgroup("upd-wg", new WorkgroupSettings(null, 128, null, null, null,
+ null, null, null, null, null, null, "trailing"), REGION);
+
+ assertEquals(32, updated.getBaseCapacity());
+ assertEquals(128, updated.getMaxCapacity());
+ assertTrue(updated.isPubliclyAccessible());
+ assertEquals("datestyle", updated.getConfigParameters().get(0).getParameterKey());
+ assertEquals(List.of("sg-1"), updated.getSecurityGroupIds());
+ assertEquals("current", updated.getTrackName());
+ assertEquals("trailing", updated.getPendingTrackName());
+
+ Workgroup reverted = service.updateWorkgroup("upd-wg", new WorkgroupSettings(null, null, null, null, null,
+ null, null, null, null, null, null, "current"), REGION);
+ assertNull(reverted.getPendingTrackName());
+ }
+
+ @Test
+ void updateStoresACopySoAReaderNeverSeesAHalfAppliedWorkgroupChange() {
+ create("copy-wg-ns");
+ createWorkgroup("copy-wg", "copy-wg-ns");
+ Workgroup before = service.getWorkgroup("copy-wg", REGION);
+
+ service.updateWorkgroup("copy-wg", new WorkgroupSettings(64, null, null, null, null,
+ null, null, null, null, null, null, null), REGION);
+
+ assertNotSame(before, service.getWorkgroup("copy-wg", REGION));
+ assertEquals(128, before.getBaseCapacity());
+ assertEquals(64, service.getWorkgroup("copy-wg", REGION).getBaseCapacity());
+ }
+
+ @Test
+ void deleteWorkgroupReportsDeletingReleasesTheEndpointAndFreesTheNamespace() {
+ create("del-wg-ns");
+ Workgroup created = createWorkgroup("del-wg", "del-wg-ns");
+
+ Workgroup deleted = service.deleteWorkgroup("del-wg", REGION);
+
+ assertEquals("DELETING", deleted.getStatus());
+ verify(endpoints).release(created.getEndpoint());
+ assertThrows(AwsException.class, () -> service.getWorkgroup("del-wg", REGION));
+ createWorkgroup("del-wg-again", "del-wg-ns");
+ }
+
+ @Test
+ void deleteNamespaceIsRejectedWhileAWorkgroupIsAttached() {
+ create("attached-ns");
+ createWorkgroup("attached-wg", "attached-ns");
+
+ assertEquals("ConflictException", assertThrows(AwsException.class,
+ () -> service.deleteNamespace("attached-ns", REGION)).getErrorCode());
+ assertEquals("attached-ns", service.getNamespace("attached-ns", REGION).getNamespaceName());
+
+ service.deleteWorkgroup("attached-wg", REGION);
+ assertEquals("DELETING", service.deleteNamespace("attached-ns", REGION).getStatus());
+ }
+
+ @Test
+ void listWorkgroupsIsScopedToTheRegionAndPaginates() {
+ create("list-a-ns");
+ create("list-b-ns");
+ service.createNamespace("list-w-ns", "admin", null, null, null, null, null, Map.of(), "us-west-2");
+ createWorkgroup("alpha-wg", "list-a-ns");
+ createWorkgroup("beta-wg", "list-b-ns");
+ service.createWorkgroup("gamma-wg", "list-w-ns", emptySettings(), null, "us-west-2");
+
+ assertEquals(List.of("alpha-wg", "beta-wg"),
+ service.listWorkgroups(REGION, null, null).items().stream().map(Workgroup::getWorkgroupName).toList());
+ PaginatedResult<Workgroup> firstPage = service.listWorkgroups(REGION, 1, null);
+ assertEquals(List.of("alpha-wg"), firstPage.items().stream().map(Workgroup::getWorkgroupName).toList());
+ assertEquals(List.of("beta-wg"), service.listWorkgroups(REGION, 1, firstPage.nextToken()).items().stream()
+ .map(Workgroup::getWorkgroupName).toList());
+ }
+
+ @Test
+ void workgroupTagsAreManagedThroughTheWorkgroupArn() {
+ create("tag-wg-ns");
+ Workgroup created = service.createWorkgroup("tag-wg", "tag-wg-ns", emptySettings(),
+ Map.of("env", "dev"), REGION);
+ String arn = created.getWorkgroupArn();
+
+ assertEquals(Map.of("env", "dev"), service.listTagsForResource(arn, REGION));
+ service.tagResource(arn, Map.of("team", "data"), REGION);
+ assertEquals(Map.of("env", "dev", "team", "data"), service.listTagsForResource(arn, REGION));
+ service.untagResource(arn, List.of("env"), REGION);
+ assertEquals(Map.of("team", "data"), service.listTagsForResource(arn, REGION));
+
+ // The namespace's own tags are a separate set.
+ assertTrue(service.listTagsForResource(service.getNamespace("tag-wg-ns", REGION).getNamespaceArn(), REGION)
+ .isEmpty());
+ verify(workgroupStore, atLeastOnce()).put(eq(REGION + "::tag-wg"), any(Workgroup.class));
+ }
+
+ @Test
+ void clearRemovesWorkgroupsAndReleasesTheirEndpoints() {
+ create("clear-wg-ns");
+ Workgroup created = createWorkgroup("clear-wg", "clear-wg-ns");
+
+ service.clear();
+
+ assertTrue(service.listWorkgroups(REGION, null, null).items().isEmpty());
+ verify(endpoints).release(created.getEndpoint());
+ }
+
+ @Test
+ void createWorkgroupStartsTheRuntimeWithTheNamespaceAdminAndRecordsWhereItListens() {
+ service.createNamespace("rt-ns", "root", "Secret123", "analytics", null, null, null, null, Map.of(), REGION);
+
+ Workgroup workgroup = createWorkgroup("rt-wg", "rt-ns");
+
+ verify(runtime).start(eq(ACCOUNT_ID), eq(REGION), eq("rt-wg"), eq("root"), eq("Secret123"),
+ eq("analytics"), eq(workgroup.getEndpoint()), eq(false), any());
+ Workgroup stored = service.getWorkgroup("rt-wg", REGION);
+ assertEquals("127.0.0.1", stored.getRuntimeHost());
+ assertEquals(55432, stored.getRuntimePort());
+ assertEquals("root", stored.getMasterUsername());
+ }
+
+ @Test
+ void aNamespaceWithoutAPasswordGetsAGeneratedOneThatIsKeptForTheBackend() {
+ service.createNamespace("gen-ns", null, null, null, null, null, null, Map.of(), REGION);
+
+ createWorkgroup("gen-wg", "gen-ns");
+
+ verify(runtime).start(eq(ACCOUNT_ID), eq(REGION), eq("gen-wg"), eq("admin"), eq("Generated123"),
+ eq("dev"), any(Endpoint.class), eq(false), any());
+ assertEquals("Generated123", store.get(REGION + "::gen-ns").orElseThrow().getAdminUserPassword());
+ }
+
+ @Test
+ void theAdminSupplierHandedToTheRuntimeTracksTheNamespaceAsItChanges() {
+ service.createNamespace("sup-ns", "root", "Secret123", null, null, null, null, null, Map.of(), REGION);
+ createWorkgroup("sup-wg", "sup-ns");
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<Supplier<Optional<RedshiftServerlessRuntime.AdminCredentials>>> supplier =
+ ArgumentCaptor.forClass(Supplier.class);
+ verify(runtime).start(any(), any(), any(), any(), any(), any(), any(), anyBoolean(), supplier.capture());
+
+ assertEquals(new RedshiftServerlessRuntime.AdminCredentials("root", "Secret123"),
+ supplier.getValue().get().orElseThrow());
+ service.updateNamespace("sup-ns", "renamed", "Changed123", null, null, null, null, REGION);
+
+ assertEquals(new RedshiftServerlessRuntime.AdminCredentials("renamed", "Changed123"),
+ supplier.getValue().get().orElseThrow());
+ }
+
+ @Test
+ void aRuntimeThatFailsToStartLeavesNoWorkgroupAndReleasesTheEndpoint() {
+ create("fail-ns");
+ when(runtime.start(any(String.class), any(String.class), any(String.class), any(String.class),
+ any(String.class), any(String.class), any(Endpoint.class), anyBoolean(), any()))
+ .thenThrow(new AwsException("InternalFailure", "Failed to start container", 500));
+
+ assertThrows(AwsException.class, () -> createWorkgroup("fail-wg", "fail-ns"));
+
+ assertTrue(service.listWorkgroups(REGION, null, null).items().isEmpty());
+ verify(endpoints).release(any(Endpoint.class));
+ }
+
+ @Test
+ void aFailedPersistAfterTheRuntimeStartedStopsTheRuntimeAndReleasesTheEndpoint() {
+ create("persist-ns");
+ doThrow(new IllegalStateException("disk full")).when(workgroupStore)
+ .put(eq(REGION + "::persist-wg"), any(Workgroup.class));
+
+ assertThrows(IllegalStateException.class, () -> createWorkgroup("persist-wg", "persist-ns"));
+
+ verify(runtime).stop(ACCOUNT_ID, REGION, "persist-wg");
+ verify(endpoints).release(any(Endpoint.class));
+ }
+
+ @Test
+ void deleteWorkgroupStopsTheRuntimeFirstAndKeepsTheWorkgroupWhenThatFails() {
+ create("stop-ns");
+ createWorkgroup("stop-wg", "stop-ns");
+ doThrow(new RuntimeException("listener would not close")).when(runtime).stop(ACCOUNT_ID, REGION, "stop-wg");
+
+ AwsException failure = assertThrows(AwsException.class, () -> service.deleteWorkgroup("stop-wg", REGION));
+
+ assertEquals("InternalServerException", failure.getErrorCode());
+ assertEquals("stop-wg", service.getWorkgroup("stop-wg", REGION).getWorkgroupName());
+ verify(endpoints, never()).release(any(Endpoint.class));
+ }
+
+ @Test
+ void changingTheAdminPasswordReachesTheRunningBackendBeforeItIsStored() {
+ service.createNamespace("pw-ns", "root", "Secret123", null, null, null, null, null, Map.of(), REGION);
+ createWorkgroup("pw-wg", "pw-ns");
+
+ service.updateNamespace("pw-ns", null, "Changed123", null, null, null, null, REGION);
+
+ verify(runtime).changeMasterPassword(ACCOUNT_ID, REGION, "pw-wg", "root", "dev", "Secret123", "Changed123");
+ assertEquals("Changed123", store.get(REGION + "::pw-ns").orElseThrow().getAdminUserPassword());
+ }
+
+ @Test
+ void aPasswordTheBackendRefusesLeavesTheNamespaceUnchangedAndIsAValidationError() {
+ service.createNamespace("bad-pw-ns", "root", "Secret123", null, null, null, null, null, Map.of(), REGION);
+ createWorkgroup("bad-pw-wg", "bad-pw-ns");
+ doThrow(new AwsException("InvalidParameterValue", "too short", 400)).when(runtime)
+ .changeMasterPassword(any(), any(), any(), any(), any(), any(), eq("short"));
+
+ AwsException rejected = assertThrows(AwsException.class,
+ () -> service.updateNamespace("bad-pw-ns", null, "short", null, null, null, null, REGION));
+
+ assertEquals("ValidationException", rejected.getErrorCode());
+ assertEquals("Secret123", store.get(REGION + "::bad-pw-ns").orElseThrow().getAdminUserPassword());
+ }
+
+ @Test
+ void aNamespaceThatCannotBeStoredPutsTheBackendPasswordBack() {
+ service.createNamespace("undo-ns", "root", "Secret123", null, null, null, null, null, Map.of(), REGION);
+ createWorkgroup("undo-wg", "undo-ns");
+ doThrow(new IllegalStateException("disk full")).when(store)
+ .put(eq(REGION + "::undo-ns"), any(Namespace.class));
+
+ assertThrows(IllegalStateException.class,
+ () -> service.updateNamespace("undo-ns", null, "Changed123", null, null, null, null, REGION));
+
+ verify(runtime).restoreMasterPassword(ACCOUNT_ID, REGION, "undo-wg", "root", "dev", "Secret123");
+ }
+
+ @Test
+ void aFailedPasswordRestoreDoesNotMaskTheStorageFailure() {
+ service.createNamespace("mask-ns", "root", "Secret123", null, null, null, null, null, Map.of(), REGION);
+ createWorkgroup("mask-wg", "mask-ns");
+ IllegalStateException storageFailure = new IllegalStateException("disk full");
+ doThrow(storageFailure).when(store).put(eq(REGION + "::mask-ns"), any(Namespace.class));
+ doThrow(new RuntimeException("psql failed")).when(runtime)
+ .restoreMasterPassword(any(), any(), any(), any(), any(), any());
+
+ IllegalStateException thrown = assertThrows(IllegalStateException.class,
+ () -> service.updateNamespace("mask-ns", null, "Changed123", null, null, null, null, REGION));
+
+ assertEquals(storageFailure, thrown);
+ }
+
+ @Test
+ void aNamespaceUpdateThatFailsValidationNeverTouchesTheBackendPassword() {
+ service.createNamespace("order-ns", "root", "Secret123", null, null, null, null, null, Map.of(), REGION);
+ createWorkgroup("order-wg", "order-ns");
+
+ assertThrows(AwsException.class, () -> service.updateNamespace(
+ "order-ns", null, "Changed123", null, null, null, List.of("bogus"), REGION));
+
+ verify(runtime, never()).changeMasterPassword(any(), any(), any(), any(), any(), any(), any());
+ }
+
+ @Test
+ void reconcileNamespaceResetsAnOmittedKmsKeyAndDefaultRole() {
+ service.createNamespace("rec-ns", "root", null, null, "custom-key", "arn:aws:iam::000000000000:role/r",
+ null, null, Map.of(), REGION);
+
+ Namespace reconciled = service.reconcileNamespace("rec-ns", null, null, null, null, null, null, REGION);
+
+ assertEquals(RedshiftServerlessService.AWS_OWNED_KMS_KEY, reconciled.getKmsKeyId());
+ assertNull(reconciled.getDefaultIamRoleArn());
+ }
+
+ @Test
+ void updateNamespaceKeepsAnOmittedKmsKeyAndDefaultRole() {
+ service.createNamespace("keep-ns", "root", null, null, "custom-key", "arn:aws:iam::000000000000:role/r",
+ null, null, Map.of(), REGION);
+
+ Namespace updated = service.updateNamespace("keep-ns", null, null, null, null, null, null, REGION);
+
+ assertEquals("custom-key", updated.getKmsKeyId());
+ assertEquals("arn:aws:iam::000000000000:role/r", updated.getDefaultIamRoleArn());
+ }
+
+ @Test
+ void clearKeepsTheEndpointReservedWhenTheRuntimeCannotBeStopped() {
+ create("hold-ns");
+ Workgroup created = createWorkgroup("hold-wg", "hold-ns");
+ doThrow(new RuntimeException("listener still bound")).when(runtime).stop(ACCOUNT_ID, REGION, "hold-wg");
+
+ service.clear();
+
+ verify(endpoints, never()).release(created.getEndpoint());
+ }
+
+ @Test
+ void anAdminPasswordChangeWithNoWorkgroupJustStoresIt() {
+ service.createNamespace("solo-ns", "root", null, null, null, null, null, null, Map.of(), REGION);
+
+ service.updateNamespace("solo-ns", null, "Changed123", null, null, null, null, REGION);
+
+ verify(runtime, never()).changeMasterPassword(any(), any(), any(), any(), any(), any(), any());
+ assertEquals("Changed123", store.get(REGION + "::solo-ns").orElseThrow().getAdminUserPassword());
+ }
+
+ @Test
+ void getCredentialsMintsForTheDbUserWithTheDocumentedDurations() {
+ create("cred-ns");
+ createWorkgroup("cred-wg", "cred-ns");
+ TempCredential minted = new TempCredential("IAM:alice", "pw", Instant.parse("2026-10-03T00:15:00Z"), List.of());
+ when(runtime.issueCredential(ACCOUNT_ID, REGION, "cred-wg", "IAM:alice", 900)).thenReturn(minted);
+ when(runtime.issueCredential(ACCOUNT_ID, REGION, "cred-wg", "IAM:alice", 3600)).thenReturn(minted);
+
+ assertEquals(minted, service.getCredentials("cred-wg", null, null, "IAM:alice", REGION));
+ assertEquals(minted, service.getCredentials("cred-wg", "dev", 3600, "IAM:alice", REGION));
+ for (int bad : new int[] {899, 3601}) {
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> service.getCredentials("cred-wg", null, bad, "IAM:alice", REGION)).getErrorCode());
+ }
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> service.getCredentials("cred-wg", "other", null, "IAM:alice", REGION)).getErrorCode());
+ assertEquals("ResourceNotFoundException", assertThrows(AwsException.class,
+ () -> service.getCredentials("absent-wg", null, null, "IAM:alice", REGION)).getErrorCode());
+ }
+
+ @Test
+ void aWorkgroupTargetResolvesByNameOrArnAndNeedsALiveRuntime() {
+ service.createNamespace("tgt-ns", "root", "Secret123", "analytics", null, null, null, null, Map.of(), REGION);
+ Workgroup created = createWorkgroup("tgt-wg", "tgt-ns");
+
+ for (String reference : List.of("tgt-wg", created.getWorkgroupArn())) {
+ RedshiftServerlessService.WorkgroupTarget target = service.getWorkgroupTarget(reference, REGION);
+ assertEquals(created.getWorkgroupArn(), target.arn());
+ assertEquals("127.0.0.1", target.host());
+ assertEquals(55432, target.port());
+ assertEquals("analytics", target.database());
+ assertEquals("root", target.masterUsername());
+ assertEquals("Secret123", target.masterPassword());
+ }
+ String absentArn = "arn:aws:redshift-serverless:us-east-1:" + ACCOUNT_ID
+ + ":workgroup/00000000-0000-0000-0000-000000000000";
+ assertEquals("ResourceNotFoundException", assertThrows(AwsException.class,
+ () -> service.getWorkgroupTarget(absentArn, REGION)).getErrorCode());
+
+ Workgroup dead = new Workgroup(service.getWorkgroup("tgt-wg", REGION));
+ dead.setRuntimeHost(null);
+ dead.setRuntimePort(0);
+ workgroupStore.put(REGION + "::tgt-wg", dead);
+ assertEquals("ValidationException", assertThrows(AwsException.class,
+ () -> service.getWorkgroupTarget("tgt-wg", REGION)).getErrorCode());
+ }
+
+ @Test
+ void startupRestoresEachWorkgroupRuntimeAndClearsTheOnesItCannotRestore() {
+ service.createNamespace("rec-ns", "root", "Secret123", null, null, null, null, null, Map.of(), REGION);
+ service.createNamespace("rec-bad-ns", "root", "Secret123", null, null, null, null, null, Map.of(), REGION);
+ createWorkgroup("rec-wg", "rec-ns");
+ createWorkgroup("rec-bad-wg", "rec-bad-ns");
+ when(endpoints.restore(any(Endpoint.class))).thenAnswer(invocation -> invocation.getArgument(0));
+ when(runtime.start(any(String.class), any(String.class), eq("rec-bad-wg"), any(String.class),
+ any(String.class), any(String.class), any(Endpoint.class), eq(true), any()))
+ .thenThrow(new AwsException("InternalFailure", "no container", 500));
+ when(runtime.start(any(String.class), any(String.class), eq("rec-wg"), any(String.class),
+ any(String.class), any(String.class), any(Endpoint.class), eq(true), any()))
+ .thenReturn(new RedshiftServerlessRuntime.Backend("10.0.0.9", 6000));
+
+ service.onStart(null);
+
+ assertEquals("10.0.0.9", service.getWorkgroup("rec-wg", REGION).getRuntimeHost());
+ assertEquals(6000, service.getWorkgroup("rec-wg", REGION).getRuntimePort());
+ assertNull(service.getWorkgroup("rec-bad-wg", REGION).getRuntimeHost());
+ assertEquals(0, service.getWorkgroup("rec-bad-wg", REGION).getRuntimePort());
+ assertEquals("AVAILABLE", service.getWorkgroup("rec-bad-wg", REGION).getStatus());
+ }
+
+ @Test
+ void clearStopsEveryRuntime() {
+ create("clr-ns");
+ createWorkgroup("clr-wg", "clr-ns");
+
+ service.clear();
+
+ verify(runtime).stop(ACCOUNT_ID, REGION, "clr-wg");
+ }
+
private Namespace create(String namespaceName) {
return service.createNamespace(namespaceName, "admin", null, null, null, null, null, Map.of(), REGION);
}
+
+ private Workgroup createWorkgroup(String workgroupName, String namespaceName) {
+ return service.createWorkgroup(workgroupName, namespaceName, emptySettings(), null, REGION);
+ }
+
+ private static WorkgroupSettings emptySettings() {
+ return new WorkgroupSettings(null, null, null, null, null, null, null, null, null, null, null, null);
+ }
+
+ private static WorkgroupSettings settings(Integer port, String ipAddressType, PricePerformanceTarget target,
+ String trackName) {
+ return new WorkgroupSettings(null, null, null, null, null, null, null, null, port, target,
+ ipAddressType, trackName);
+ }
}
diff --git a/src/test/resources/cloudformation/getatt-attribute-gaps.tsv b/src/test/resources/cloudformation/getatt-attribute-gaps.tsv
index 366b4bda66..b06a00dc18 100644
--- a/src/test/resources/cloudformation/getatt-attribute-gaps.tsv
+++ b/src/test/resources/cloudformation/getatt-attribute-gaps.tsv
@@ -46,6 +46,7 @@ AWS::RDS::DBInstance StatusInfos read-replica status detail is not emulated
AWS::RDS::DBInstance StorageOperationPercentProgress storage operations (scaling, type changes) are not emulated; storage changes apply synchronously
AWS::RDS::DBInstance StorageOperationStatus storage operations (scaling, type changes) are not emulated; storage changes apply synchronously
AWS::Redshift::Cluster DeferMaintenanceIdentifier Floci does not emulate deferred maintenance windows
+AWS::RedshiftServerless::Namespace Namespace the whole Namespace object has no scalar GetAtt form; its members are set under the dotted Namespace.* attributes instead, the form AWS's GetAtt exposes
AWS::SQS::QueuePolicy Id schema gives no description; the policy has no backing entity to take an id from
AWS::Transfer::Server As2ServiceManagedEgressIpAddresses AS2 managed egress IP allocation is not emulated by the Transfer management-plane service
AWS::SecretsManager::Secret Id Fn::GetAtt Id on a secret is its ARN, which the provisioner already sets as the physical id and as Arn; closable, deferred to a follow-up PR to keep this one a pure move
diff --git a/src/test/resources/cloudformation/supported-resource-types.tsv b/src/test/resources/cloudformation/supported-resource-types.tsv
index 3d998464b7..aebebb4ad8 100644
--- a/src/test/resources/cloudformation/supported-resource-types.tsv
+++ b/src/test/resources/cloudformation/supported-resource-types.tsv
@@ -134,6 +134,8 @@ AWS::Redshift::Cluster RedshiftClusterCfnProvisioner
AWS::Redshift::ClusterParameterGroup RedshiftClusterCfnProvisioner
AWS::Redshift::ClusterSecurityGroup RedshiftClusterCfnProvisioner
AWS::Redshift::ClusterSubnetGroup RedshiftClusterCfnProvisioner
+AWS::RedshiftServerless::Namespace RedshiftServerlessCfnProvisioner
+AWS::RedshiftServerless::Workgroup RedshiftServerlessCfnProvisioner
AWS::Route53::HostedZone Route53CfnProvisioner
AWS::Route53::RecordSet Route53CfnProvisioner
AWS::S3::Bucket S3CfnProvisioner
diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yaml
index 5e6bb756bc..c6714fb310 100644
--- a/tools/docs/cfn_resource_types.yaml
+++ b/tools/docs/cfn_resource_types.yaml
@@ -25,6 +25,7 @@ order:
- AWS::EKS
- AWS::RDS
- AWS::Redshift
+ - AWS::RedshiftServerless
- AWS::EC2
- AWS::ElasticLoadBalancingV2
- AWS::AutoScaling
@@ -85,6 +86,8 @@ type_order:
[DBInstance, DBCluster, DBSubnetGroup, DBParameterGroup, DBClusterParameterGroup]
AWS::Redshift:
[Cluster, ClusterParameterGroup, ClusterSubnetGroup, ClusterSecurityGroup]
+ AWS::RedshiftServerless:
+ [Namespace, Workgroup]
AWS::EC2:
[VPC, Subnet, SecurityGroup, SecurityGroupIngress, SecurityGroupEgress, InternetGateway,
RouteTable, SubnetRouteTableAssociation, Route, NatGateway, EIP, Instance, LaunchTemplate,
@@ -138,6 +141,7 @@ service_labels:
AWS::Pipes: Pipes
AWS::RDS: RDS
AWS::Redshift: Redshift
+ AWS::RedshiftServerless: Redshift Serverless
AWS::Route53: Route 53
AWS::S3: S3
AWS::SNS: SNS
@@ -163,6 +167,8 @@ notes:
AWS::RDS::DBCluster: starts a real container
AWS::Redshift::ClusterSecurityGroup: accepted; no EC2-Classic security group model
AWS::Redshift::Cluster: single-node container; Port and non-dev DBName ignored; ManageMasterPassword unsupported
+ AWS::RedshiftServerless::Namespace: 'ManageAdminPassword is accepted and not applied; GetAtt uses the dotted Namespace.* form'
+ AWS::RedshiftServerless::Workgroup: 'starts a real container; one workgroup per namespace; capacity, VPC and price-performance settings are stored only; snapshot restore properties are ignored; GetAtt uses the dotted Workgroup.* form'
AWS::CDK::Metadata: accepted; no-op
AWS::Transfer::Server: 'management plane only; Ref returns the server ARN; Arn, ServerId and State attributes supported; AS2 managed egress IPs are not modeled; Domain replacement and IdentityProviderType changes are not supported'
AWS::IAM::User: 'template `LoginProfile` and `PermissionsBoundary` are ignored; an API-created login profile is removed on delete'