Light, fluffy, and always free - The AWS Local Emulator alternative
feat: support managed credentials in Redshift restore (#6515)
Vũ Thế Huy2a5f7347774d
diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.mdindex 70152830af..de73103f51 100644--- a/docs/services/cloudformation.md+++ b/docs/services/cloudformation.md@@ -188,7 +188,7 @@ cross-resource references.| Aurora DSQL | `Cluster` || ElastiCache | `CacheCluster` (starts a real container; modifying an existing cluster in place is not supported), `SubnetGroup`, `User` (Tags are ignored), `UserGroup` (Tags are ignored), `ServerlessCache` (GetAtt uses the dotted Endpoint.* and ReaderEndpoint.* form) || 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) |+| Redshift Serverless | `Namespace` (Managed admin credentials use Secrets Manager; 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` (deleted on stack teardown; a VPC with dependents outside the stack, including anything still holding a network interface in its subnets (an instance that is not terminated, a NAT gateway that is not deleted or failed, a standalone interface, an interface endpoint), fails the delete with `DependencyViolation`, as on AWS), `Subnet`, `SecurityGroup` (inline `SecurityGroupIngress`/`SecurityGroupEgress` supported), `SecurityGroupIngress`, `SecurityGroupEgress`, `InternetGateway`, `RouteTable`, `SubnetRouteTableAssociation`, `Route`, `NatGateway`, `EIP`, `Instance`, `LaunchTemplate`, `VPCGatewayAttachment`, `VPCEndpoint`, `NetworkAcl`, `NetworkAclEntry`, `SubnetNetworkAclAssociation`, `FlowLog`, `PrefixList` (updated in place; an update that changes `AddressFamily`, or `MaxEntries` together with `Entries`, fails) || Elastic Load Balancing v2 | `LoadBalancer`, `TargetGroup`, `Listener`, `ListenerRule` || Auto Scaling | `LaunchConfiguration`, `AutoScalingGroup`, `LifecycleHook`, `ScalingPolicy` |diff --git a/docs/services/redshift-serverless.md b/docs/services/redshift-serverless.mdindex fffe3188a3..59fb4b1cbd 100644--- a/docs/services/redshift-serverless.md+++ b/docs/services/redshift-serverless.md@@ -77,7 +77,7 @@ Namespace and workgroup state is account and Region scoped and persisted through- **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 unless managed credentials are enabled. 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` can be used in place of `workgroupName` to resolve the associated workgroup. 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.-- **Managed admin credentials use Secrets Manager.** `CreateNamespace` and `UpdateNamespace` accept `manageAdminPassword=true` to generate a password and create a service-owned secret. The namespace returns `adminPasswordSecretArn` and, when supplied, `adminPasswordSecretKmsKeyId`; the latter requires `manageAdminPassword=true`. The secret contains `engine`, `username`, `password`, `dbname`, and `namespaceName`. `GetNamespace` and `ListNamespaces` return the same references, and the Data API accepts the secret ARN with `WorkgroupName`. Passwords are never returned in namespace responses. Enabling management updates a running backend's password. While credentials are managed, an explicit `adminUserPassword` is rejected; switching to `manageAdminPassword=false` requires a replacement password, updates the backend, and removes the secret. Deleting the namespace also removes its managed secret. Credential changes compensate backend, secret, and namespace writes on failure. The secret reference survives persistence and snapshot restore of the existing namespace. Changing managed-credential settings through `RestoreFromSnapshot` or CloudFormation is not implemented. As for other service-owned secrets in Floci, automatic rotation and `RotateSecret` password replacement are not executed; no successful rotation is reported.+- **Managed admin credentials use Secrets Manager.** `CreateNamespace` and `UpdateNamespace` accept `manageAdminPassword=true` to generate a password and create a service-owned secret. The namespace returns `adminPasswordSecretArn` and, when supplied, `adminPasswordSecretKmsKeyId`; the latter requires `manageAdminPassword=true`. The secret contains `engine`, `username`, `password`, `dbname`, and `namespaceName`. `GetNamespace` and `ListNamespaces` return the same references, and the Data API accepts the secret ARN with `WorkgroupName`. Passwords are never returned in namespace responses. Enabling management updates a running backend's password. While credentials are managed, an explicit `adminUserPassword` is rejected; switching to `manageAdminPassword=false` requires a replacement password, updates the backend, and removes the secret. Deleting the namespace also removes its managed secret. Credential changes compensate backend, secret, and namespace writes on failure. Snapshots retain the admin credentials captured at snapshot time. `RestoreFromSnapshot` with `manageAdminPassword=true` generates managed credentials and creates or refreshes the namespace secret; false or omitted restores the snapshot credentials and removes a currently managed secret. `AWS::RedshiftServerless::Namespace` supports `ManageAdminPassword` and `AdminPasswordSecretKmsKeyId` on create and update. Failed CloudFormation updates restore the previous namespace, backend password, secret ARN, and secret versions. If another secret has reused a deleted managed secret's name, rollback reports a conflict and preserves that secret. Concurrent creation and rollback cannot overwrite each other's secrets. As for other service-owned secrets in Floci, automatic rotation and `RotateSecret` password replacement are not executed; no successful rotation is reported.- **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 theepoch-seconds number that awsJson1.1 uses by default. `Namespace.creationDate` carries@@ -139,7 +139,7 @@ aws redshift-serverless update-workgroup --workgroup-name analytics-wg --base-caaws redshift-serverless delete-workgroup --workgroup-name analytics-wgaws redshift-serverless delete-namespace --namespace-name analytics```-- **Snapshots dump the database.** While the namespace's workgroup is running, `CreateSnapshot` runs `pg_dump` on its container and stores the dump under `<persistent-path>/redshift-serverless-dumps/<account>/<region>/`. `RestoreFromSnapshot` drops every user schema (the catalog bootstrap schema excepted) and replays the dump with `psql`, stopping at the first failing statement, so rows written after the snapshot are gone. The current contents are dumped first and put back if the replay fails, and a restore whose recorded dump file has gone missing fails instead of reporting success. A snapshot taken with no running workgroup is metadata only, and restoring it leaves the database untouched. `DeleteSnapshot` removes the dump. Snapshot ARNs use the snapshot name as their id, snapshot names are 3-255 lowercase letters, digits and hyphens, and `retentionPeriod` is `-1` (indefinite, the default) or 1 to 3653 days. Snapshots are taggable by ARN. `snapshotName` and `snapshotArn` cannot be sent together. Floci's own `floci_internal` bootstrap schema, which feeds the `svv_external_*` views from the Glue catalog, is left out of both the dump and the reset, matching AWS where the Glue catalog is outside a namespace snapshot, so a client schema with that exact name is not captured. A real snapshot expires after its retention period; Floci reports `snapshotRemainingDays` but does not expire snapshots.+- **Snapshots dump the database.** While the namespace's workgroup is running, `CreateSnapshot` runs `pg_dump` on its container and stores the dump under `<persistent-path>/redshift-serverless-dumps/<account>/<region>/`. `RestoreFromSnapshot` drops every user schema (the catalog bootstrap schema excepted) and replays the dump with `psql`, stopping at the first failing statement, so rows written after the snapshot are gone. The current contents are dumped first and put back if replay or credential changes fail, and a restore whose recorded dump file has gone missing fails instead of reporting success. A snapshot taken with no running workgroup is metadata only, and restoring it leaves the database untouched. `DeleteSnapshot` removes the dump. Snapshot ARNs use the snapshot name as their id, snapshot names are 3-255 lowercase letters, digits and hyphens, and `retentionPeriod` is `-1` (indefinite, the default) or 1 to 3653 days. Snapshots are taggable by ARN. `snapshotName` and `snapshotArn` cannot be sent together. Floci's own `floci_internal` bootstrap schema, which feeds the `svv_external_*` views from the Glue catalog, is left out of both the dump and the reset, matching AWS where the Glue catalog is outside a namespace snapshot, so a client schema with that exact name is not captured. A real snapshot expires after its retention period; Floci reports `snapshotRemainingDays` but does not expire snapshots.- **`UpdateSnapshot` changes only `retentionPeriod`.** It is validated like `CreateSnapshot` (`-1` or 1 to 3653 days), `-1` makes the snapshot indefinite, and an omitted period leaves the snapshot unchanged. `snapshotRemainingDays` keeps counting from the creation time, because Floci stores no separate retention start.- **Resource policies are recorded, not acted on.** A policy is stored per account and Region, echoed in the shape of the API model, and dropped on read once the snapshot it belongs to is gone. A snapshot recreated under the same name is a different snapshot: the record keeps the snapshot's creation time, so the policy is not inherited. `PutResourcePolicy` accepts a JSON object for a snapshot ARN of this account and Region; no principal in it gains access. A foreign or non-snapshot ARN is a `ValidationException`.- **Endpoint access is recorded, not acted on.** An endpoint is stored per account and Region, echoed in the shape of the API model, and dropped on read once its workgroup is gone; a workgroup recreated under the same name is a different workgroup and does not inherit it. `CreateEndpointAccess` creates no VPC endpoint. `address` and `port` are the workgroup's own, so a client can connect; `vpcEndpoint` carries a generated id and one network interface per subnet, without a `vpcId`, and `ListEndpointAccess` with a `vpcId` returns nothing. `vpcSecurityGroupIds` is optional. Endpoint names are 1 to 30 letters, digits or hyphens, start with a letter, and contain no consecutive or trailing hyphens. Security groups are the only member `UpdateEndpointAccess` changes.diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.javaindex e18fbea845..3ab5cc142b 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java@@ -34,6 +34,10 @@ public final class CfnRollback {*/public static final String UPDATE_ROLLBACK_FAILURE_ATTR = "__FlociUpdateRollbackFailure";+ /** The namespace, credentials, secret and attributes overwritten by a Serverless namespace update. */+ public static final String REDSHIFT_SERVERLESS_NAMESPACE_UPDATE_SNAPSHOT_ATTR =+ "__FlociRedshiftServerlessNamespaceUpdateSnapshot";+/*** Holds the configuration a pipe carried before the update in flight mutated it, so a failed* stack update can put it back. Written by {@code PipesCfnProvisioner} before its firstdiff --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.javaindex 7594e4843a..15faf59f74 100644--- 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@@ -4,9 +4,11 @@ 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 com.fasterxml.jackson.databind.node.ObjectNode;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.RedshiftServerlessService.NamespaceUpdateSnapshot;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;@@ -21,6 +23,8 @@ import java.util.List;import java.util.Map;import java.util.Set;+import static io.github.hectorvent.floci.services.cloudformation.provisioners.CfnRollback.REDSHIFT_SERVERLESS_NAMESPACE_UPDATE_SNAPSHOT_ATTR;+/*** 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.@@ -35,7 +39,7 @@ public class RedshiftServerlessCfnProvisioner implements CfnResourceProvisionerprivate static final Logger LOG = Logger.getLogger(RedshiftServerlessCfnProvisioner.class);- private static final ObjectMapper MAPPER = new ObjectMapper();+ private static final ObjectMapper MAPPER = new ObjectMapper().findAndRegisterModules();private static final String NAMESPACE = "AWS::RedshiftServerless::Namespace";private static final String WORKGROUP = "AWS::RedshiftServerless::Workgroup";private static final int NAME_MAX_LENGTH = 64;@@ -90,16 +94,40 @@ public class RedshiftServerlessCfnProvisioner implements CfnResourceProvisioner@Overridepublic UpdateCleanupResult completeUpdate(StackResource resource) {+ if (resource.getAttributes().containsKey(REDSHIFT_SERVERLESS_NAMESPACE_UPDATE_SNAPSHOT_ATTR)+ && !ReplacementCleanup.hasReplacement(resource)) {+ return new UpdateCleanupResult(true, true, null, 0, null);+ }return ReplacementCleanup.complete(resource, this::delete);}@Overridepublic void clearUpdate(StackResource resource) {+ resource.getAttributes().remove(REDSHIFT_SERVERLESS_NAMESPACE_UPDATE_SNAPSHOT_ATTR);ReplacementCleanup.clear(resource);}+ @Override+ public boolean retainsFailedUpdateState(StackResource resource) {+ return resource.getAttributes().containsKey(REDSHIFT_SERVERLESS_NAMESPACE_UPDATE_SNAPSHOT_ATTR);+ }+@Overridepublic boolean rollbackUpdate(StackResource resource) {+ String raw = resource.getAttributes().get(REDSHIFT_SERVERLESS_NAMESPACE_UPDATE_SNAPSHOT_ATTR);+ if (raw != null) {+ try {+ JsonNode snapshot = MAPPER.readTree(raw);+ service.restoreNamespaceUpdate(MAPPER.treeToValue(snapshot.path("state"), NamespaceUpdateSnapshot.class),+ snapshot.path("region").asText());+ resource.getAttributes().clear();+ snapshot.path("attributes").fields().forEachRemaining(entry ->+ resource.getAttributes().put(entry.getKey(), entry.getValue().asText()));+ return true;+ } catch (JsonProcessingException e) {+ throw new IllegalStateException("Could not read the Redshift Serverless namespace update snapshot", e);+ }+ }return ReplacementCleanup.rollback(resource, this::delete);}@@ -108,6 +136,10 @@ public class RedshiftServerlessCfnProvisioner implements CfnResourceProvisionerr.getLogicalId(), NAME_MAX_LENGTH, true);String adminUsername = ctx.resolveOptional(props, "AdminUsername");String adminUserPassword = ctx.resolveOptional(props, "AdminUserPassword");+ String manageAdminPasswordProperty = ctx.resolveOptional(props, "ManageAdminPassword");+ Boolean manageAdminPassword = manageAdminPasswordProperty == null+ ? null : Boolean.parseBoolean(manageAdminPasswordProperty);+ String adminPasswordSecretKmsKeyId = ctx.resolveOptional(props, "AdminPasswordSecretKmsKeyId");String kmsKeyId = ctx.resolveOptional(props, "KmsKeyId");String defaultIamRoleArn = ctx.resolveOptional(props, "DefaultIamRoleArn");List<String> iamRoles = ctx.resolveStringList(props, "IamRoles");@@ -124,13 +156,21 @@ public class RedshiftServerlessCfnProvisioner implements CfnResourceProvisioner+ "remove the namespace from the template and add it back",400);}+ if (r.getAttributes().containsKey(REDSHIFT_SERVERLESS_NAMESPACE_UPDATE_SNAPSHOT_ATTR)) {+ throw new AwsException("ConflictException", "Namespace update rollback is still pending.", 400);+ }+ ObjectNode snapshot = MAPPER.createObjectNode().put("region", ctx.region());+ snapshot.set("attributes", MAPPER.valueToTree(r.getAttributes()));+ snapshot.set("state", MAPPER.valueToTree(service.snapshotNamespaceUpdate(name, ctx.region())));+ r.getAttributes().put(REDSHIFT_SERVERLESS_NAMESPACE_UPDATE_SNAPSHOT_ATTR, snapshot.toString());namespace = service.reconcileNamespace(name, adminUsername, adminUserPassword, kmsKeyId,- defaultIamRoleArn, iamRoles, logExports, ctx.region());+ defaultIamRoleArn, iamRoles, logExports, Boolean.TRUE.equals(manageAdminPassword),+ adminPasswordSecretKmsKeyId, 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());+ ctx.resolveOptional(props, "DbName"), kmsKeyId, defaultIamRoleArn, iamRoles, logExports,+ tags, manageAdminPassword, adminPasswordSecretKmsKeyId, ctx.region());}r.setPhysicalId(name);diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminSecrets.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminSecrets.javaindex 4a0190dd5f..8c2e0ae5fc 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminSecrets.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminSecrets.java@@ -50,6 +50,17 @@ public class RedshiftServerlessAdminSecrets {}}+ Secret snapshot(Namespace namespace, String region) {+ if (namespace.getAdminPasswordSecretArn() == null) {+ return null;+ }+ return mapper.convertValue(secrets.describeSecret(namespace.getAdminPasswordSecretArn(), region), Secret.class);+ }++ void restore(Secret snapshot, String region) {+ secrets.restoreManagedSecret(mapper.convertValue(snapshot, Secret.class), region);+ }+private String value(Namespace namespace) {return mapper.createObjectNode().put("engine", "redshift").put("username", namespace.getAdminUsername())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.javaindex 1df4f96d20..537280ac79 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@@ -283,7 +283,9 @@ public class RedshiftServerlessJsonHandler {private Response handleRestoreFromSnapshot(JsonNode request, String region) {RedshiftServerlessService.RestoreResult result = service.restoreFromSnapshot(ServerlessSupport.text(request, "namespaceName"), ServerlessSupport.text(request, "workgroupName"),- snapshotNameOf(request, region), ServerlessSupport.text(request, "ownerAccount"), region);+ snapshotNameOf(request, region), ServerlessSupport.text(request, "ownerAccount"),+ ServerlessSupport.bool(request, "manageAdminPassword"),+ ServerlessSupport.text(request, "adminPasswordSecretKmsKeyId"), region);ObjectNode response = objectMapper.createObjectNode();response.set("namespace", namespaceNode(result.namespace()));response.put("ownerAccount", result.snapshot().getOwnerAccount());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.javaindex 4334572af7..48ce77a725 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntime.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntime.java@@ -99,6 +99,12 @@ public class RedshiftServerlessRuntime {*/public void restoreSnapshot(String accountId, String region, String workgroupName, String masterUsername,String dbName, Path dumpFile) {+ restoreSnapshot(accountId, region, workgroupName, masterUsername, dbName, dumpFile, () -> { });+ }++ /** Keeps the safety dump until the namespace's credentials and metadata have also committed. */+ public void restoreSnapshot(String accountId, String region, String workgroupName, String masterUsername,+ String dbName, Path dumpFile, Runnable afterRestore) {String backendId = backendId(region, workgroupName);Path safety;try {@@ -112,6 +118,7 @@ public class RedshiftServerlessRuntime {try {containerManager.resetUserSchemas(accountId, backendId, masterUsername, dbName);containerManager.restoreSnapshot(accountId, backendId, masterUsername, dbName, dumpFile, true);+ afterRestore.run();} catch (RuntimeException e) {rollBackRestore(accountId, backendId, masterUsername, dbName, safety, e);throw e;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.javaindex e79724137f..eae435af76 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@@ -15,7 +15,9 @@ 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.RedshiftServerlessSnapshot;import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;+import io.github.hectorvent.floci.services.secretsmanager.model.Secret;import io.quarkus.runtime.StartupEvent;+import io.quarkus.runtime.annotations.RegisterForReflection;import jakarta.annotation.Priority;import jakarta.enterprise.context.ApplicationScoped;import jakarta.enterprise.event.Observes;@@ -300,7 +302,7 @@ public class RedshiftServerlessService implements Resettable {String defaultIamRoleArn, List<String> iamRoles,List<String> logExports, String region) {return applyNamespaceUpdate(namespaceName, adminUsername, adminUserPassword, kmsKeyId,- defaultIamRoleArn, iamRoles, logExports, null, null, region, false);+ defaultIamRoleArn, iamRoles, logExports, null, null, region, false, false);}public synchronized Namespace updateNamespace(String namespaceName, String adminUsername,@@ -309,7 +311,8 @@ public class RedshiftServerlessService implements Resettable {List<String> logExports, Boolean manageAdminPassword,String adminPasswordSecretKmsKeyId, String region) {return applyNamespaceUpdate(namespaceName, adminUsername, adminUserPassword, kmsKeyId,- defaultIamRoleArn, iamRoles, logExports, manageAdminPassword, adminPasswordSecretKmsKeyId, region, false);+ defaultIamRoleArn, iamRoles, logExports, manageAdminPassword, adminPasswordSecretKmsKeyId, region, false,+ false);}/**@@ -322,7 +325,17 @@ public class RedshiftServerlessService implements Resettable {String defaultIamRoleArn, List<String> iamRoles,List<String> logExports, String region) {return applyNamespaceUpdate(namespaceName, adminUsername, adminUserPassword, kmsKeyId,- defaultIamRoleArn, iamRoles, logExports, null, null, region, true);+ defaultIamRoleArn, iamRoles, logExports, null, null, region, true, false);+ }++ public synchronized Namespace reconcileNamespace(String namespaceName, String adminUsername,+ String adminUserPassword, String kmsKeyId,+ String defaultIamRoleArn, List<String> iamRoles,+ List<String> logExports, Boolean manageAdminPassword,+ String adminPasswordSecretKmsKeyId, String region) {+ return applyNamespaceUpdate(namespaceName, adminUsername, adminUserPassword, kmsKeyId,+ defaultIamRoleArn, iamRoles, logExports, manageAdminPassword, adminPasswordSecretKmsKeyId, region, true,+ false);}/**@@ -334,7 +347,8 @@ public class RedshiftServerlessService implements Resettable {String adminUserPassword, String kmsKeyId,String defaultIamRoleArn, List<String> iamRoles,List<String> logExports, Boolean manageAdminPassword,- String adminPasswordSecretKmsKeyId, String region, boolean resetOmitted) {+ String adminPasswordSecretKmsKeyId, String region, boolean resetOmitted,+ boolean forceManagedPasswordRotation) {validateManagedPassword(manageAdminPassword, adminUserPassword, adminPasswordSecretKmsKeyId);String key = storageKey(region, namespaceName);Namespace current = getNamespace(namespaceName, region);@@ -366,7 +380,7 @@ public class RedshiftServerlessService implements Resettable {}if (managed) {updated.setAdminUsername(adminUserOf(updated));- if (!wasManaged) {+ if (!wasManaged || forceManagedPasswordRotation) {updated.setAdminUserPassword(runtime.generatePassword());}if (adminPasswordSecretKmsKeyId != null) {@@ -382,7 +396,8 @@ public class RedshiftServerlessService implements Resettable {boolean rotated = !Objects.equals(current.getAdminUserPassword(), updated.getAdminUserPassword());boolean secretUpdated = managed && wasManaged&& (!Objects.equals(current.getAdminUsername(), updated.getAdminUsername())- || !Objects.equals(current.getAdminPasswordSecretKmsKeyId(), updated.getAdminPasswordSecretKmsKeyId()));+ || !Objects.equals(current.getAdminPasswordSecretKmsKeyId(), updated.getAdminPasswordSecretKmsKeyId())+ || !Objects.equals(current.getAdminUserPassword(), updated.getAdminUserPassword()));boolean backendChanged = false;boolean storeAttempted = false;try {@@ -417,6 +432,42 @@ public class RedshiftServerlessService implements Resettable {return updated;}+ @RegisterForReflection+ public record NamespaceUpdateSnapshot(Namespace namespace, Secret secret) {}++ public synchronized NamespaceUpdateSnapshot snapshotNamespaceUpdate(String namespaceName, String region) {+ Namespace current = new Namespace(getNamespace(namespaceName, region));+ Secret secret = current.getAdminPasswordSecretArn() == null ? null+ : requireAdminSecrets().snapshot(current, region);+ return new NamespaceUpdateSnapshot(current, secret);+ }++ /** Restores a CloudFormation update's credentials without generating a new password or secret ARN. */+ public synchronized void restoreNamespaceUpdate(NamespaceUpdateSnapshot snapshot, String region) {+ Namespace previous = new Namespace(snapshot.namespace());+ Namespace current = getNamespace(previous.getNamespaceName(), region);+ if (snapshot.secret() != null) {+ requireAdminSecrets().restore(snapshot.secret(), region);+ }+ boolean passwordChanged = !Objects.equals(current.getAdminUserPassword(), previous.getAdminUserPassword());+ if (passwordChanged) {+ applyNewAdminPassword(current, previous.getAdminUserPassword(), region);+ }+ if (current.getAdminPasswordSecretArn() != null+ && !Objects.equals(current.getAdminPasswordSecretArn(), previous.getAdminPasswordSecretArn())) {+ try {+ requireAdminSecrets().delete(current, region);+ } catch (RuntimeException failure) {+ if (passwordChanged) {+ compensate(() -> restoreAdminPassword(current, region), failure,+ "restore current backend admin password after blocked rollback");+ }+ throw failure;+ }+ }+ namespaces.put(storageKey(region, previous.getNamespaceName()), previous);+ }+public synchronized Namespace deleteNamespace(String namespaceName, String region) {Namespace current = getNamespace(namespaceName, region);Namespace deleted = new Namespace(current);@@ -467,6 +518,7 @@ public class RedshiftServerlessService implements Resettable {snapshot.setStatus("AVAILABLE");snapshot.setSnapshotCreateTime(Instant.now());snapshot.setAdminUsername(adminUserOf(namespace));+ snapshot.setAdminUserPassword(namespace.getAdminUserPassword());snapshot.setKmsKeyId(namespace.getKmsKeyId());snapshot.setRetentionPeriod(retentionDays);snapshot.setTags(tags);@@ -641,6 +693,14 @@ public class RedshiftServerlessService implements Resettable {public synchronized RestoreResult restoreFromSnapshot(String namespaceName, String workgroupName,String snapshotName, String ownerAccount,String region) {+ return restoreFromSnapshot(namespaceName, workgroupName, snapshotName, ownerAccount, null, null, region);+ }++ public synchronized RestoreResult restoreFromSnapshot(String namespaceName, String workgroupName,+ String snapshotName, String ownerAccount,+ Boolean manageAdminPassword,+ String adminPasswordSecretKmsKeyId, String region) {+ validateManagedPassword(manageAdminPassword, null, adminPasswordSecretKmsKeyId);validateNamespaceName(namespaceName);RedshiftServerlessSnapshot snapshot = getSnapshot(snapshotName, region);if (ownerAccount != null && !ownerAccount.isBlank() && !ownerAccount.equals(snapshot.getOwnerAccount())) {@@ -652,17 +712,34 @@ public class RedshiftServerlessService implements Resettable {+ namespaceName + ".");}Namespace namespace = getNamespace(namespaceName, region);+ Runnable restoreCredentials = () -> restoreSnapshotCredentials(namespace, snapshot, manageAdminPassword,+ adminPasswordSecretKmsKeyId, region);if (snapshot.getSqlDump() != null && hasLiveRuntime(workgroup)) {Path dump = trustedDump(snapshot, region).filter(Files::exists).orElseThrow(() -> new AwsException("InternalServerException", "The data of snapshot " + snapshotName + " is no longer available.",500));runtime.restoreSnapshot(workgroups.accountId(), region, workgroupName, workgroup.getMasterUsername(),- namespace.getDbName(), dump);+ namespace.getDbName(), dump, restoreCredentials);+ } else {+ restoreCredentials.run();+ }+ return new RestoreResult(getNamespace(namespaceName, region), snapshot);+ }++ private void restoreSnapshotCredentials(Namespace namespace, RedshiftServerlessSnapshot snapshot,+ Boolean manageAdminPassword, String adminPasswordSecretKmsKeyId,+ String region) {+ if (Boolean.TRUE.equals(manageAdminPassword)) {+ applyNamespaceUpdate(namespace.getNamespaceName(), null, null, null, null, null, null,+ true, adminPasswordSecretKmsKeyId, region, false, true);+ } else if (snapshot.getAdminUserPassword() != null) {+ applyNamespaceUpdate(namespace.getNamespaceName(), snapshot.getAdminUsername(), snapshot.getAdminUserPassword(),+ null, null, null, null, false, null, region, false, false);+ } else {+ Namespace restored = new Namespace(namespace);+ restored.setStatus("AVAILABLE");+ namespaces.put(storageKey(region, namespace.getNamespaceName()), restored);}- Namespace restored = new Namespace(namespace);- restored.setStatus("AVAILABLE");- namespaces.put(storageKey(region, namespaceName), restored);- return new RestoreResult(restored, snapshot);}public record RestoreResult(Namespace namespace, RedshiftServerlessSnapshot snapshot) {}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/RedshiftServerlessSnapshot.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/RedshiftServerlessSnapshot.javaindex 41a0ccae19..de3658379b 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/RedshiftServerlessSnapshot.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/model/RedshiftServerlessSnapshot.java@@ -27,6 +27,7 @@ public class RedshiftServerlessSnapshot {private String status = "AVAILABLE";private Instant snapshotCreateTime;private String adminUsername;+ private String adminUserPassword;private String kmsKeyId;private Integer retentionPeriod;private String sqlDump;@@ -46,6 +47,7 @@ public class RedshiftServerlessSnapshot {this.status = other.status;this.snapshotCreateTime = other.snapshotCreateTime;this.adminUsername = other.adminUsername;+ this.adminUserPassword = other.adminUserPassword;this.kmsKeyId = other.kmsKeyId;this.retentionPeriod = other.retentionPeriod;this.sqlDump = other.sqlDump;@@ -132,6 +134,14 @@ public class RedshiftServerlessSnapshot {this.adminUsername = adminUsername;}+ public String getAdminUserPassword() {+ return adminUserPassword;+ }++ public void setAdminUserPassword(String adminUserPassword) {+ this.adminUserPassword = adminUserPassword;+ }+public String getKmsKeyId() {return kmsKeyId;}diff --git a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.javaindex 44702eff50..ab6e110ac9 100644--- a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java+++ b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java@@ -82,6 +82,9 @@ public class SecretsManagerService implements ResourceProvider {// stripe only contend; no lock is held while a rotation Lambda runs.private static final int ROTATION_LOCK_STRIPE_COUNT = 32;private final Object[] rotationLockStripes = newLockStripes(ROTATION_LOCK_STRIPE_COUNT);+ // Names outlive ARNs across deletion and recreation. Hold these after any ARN lock, never before it.+ private static final int NAME_LOCK_STRIPE_COUNT = 32;+ private final Object[] nameLockStripes = newLockStripes(NAME_LOCK_STRIPE_COUNT);// Entries exist only while a service owner changes backend credentials outside the stripe.private final Set<String> managedReleases = ConcurrentHashMap.newKeySet();@@ -98,6 +101,11 @@ public class SecretsManagerService implements ResourceProvider {return rotationLockStripes[index];}+ private Object nameLockFor(String accountId, String region, String name) {+ int index = Math.floorMod(Objects.hash(accountId, region, name), nameLockStripes.length);+ return nameLockStripes[index];+ }+/** Names rotation threads for diagnostics. */private static final class RotationThreadFactory implements ThreadFactory {private final AtomicInteger nextId = new AtomicInteger(1);@@ -184,6 +192,17 @@ public class SecretsManagerService implements ResourceProvider {throwIfBothSecretValues(secretString, secretBinary);validateKmsKey(kmsKeyId, region);+ String accountId = store instanceof AccountAwareStorageBackend<Secret> aware+ ? aware.accountId() : regionResolver.getAccountId();+ synchronized (nameLockFor(accountId, region, name)) {+ return createSecretLocked(name, secretString, secretBinary, description, kmsKeyId, tags,+ owningService, clientRequestToken, region);+ }+ }++ private Secret createSecretLocked(String name, String secretString, String secretBinary,+ String description, String kmsKeyId, List<Secret.Tag> tags,+ String owningService, String clientRequestToken, String region) {String storageKey = regionKey(region, name);Secret existing = store.get(storageKey).orElse(null);@@ -1188,6 +1207,26 @@ public class SecretsManagerService implements ResourceProvider {}}+ /** Restores the exact service-owned secret, including its ARN and version labels, during rollback. */+ public void restoreManagedSecret(Secret snapshot, String region) {+ if (snapshot.getOwningService() == null) {+ throw new IllegalArgumentException("A managed secret snapshot must have an owning service");+ }+ AwsArnUtils.Arn arn = AwsArnUtils.parse(snapshot.getArn());+ synchronized (lockFor(snapshot.getArn())) {+ synchronized (nameLockFor(arn.accountId(), region, snapshot.getName())) {+ throwIfManagedRelease(snapshot.getArn());+ String currentArn = findArnByName(arn.accountId(), snapshot.getName(), region).orElse(null);+ if (currentArn != null && !snapshot.getArn().equals(currentArn)) {+ throw new AwsException("ResourceExistsException",+ "Cannot restore managed secret " + snapshot.getArn()+ + " because another secret already uses the name " + snapshot.getName() + ".", 400);+ }+ persist(snapshot, region);+ }+ }+ }+public Secret restoreSecret(String secretId, String region) {Secret secret = resolveSecret(secretId, region);String storageKey = regionKey(region, secret.getName());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.javaindex f99cc0e84d..2a4aca41c1 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftServerlessCfnIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftServerlessCfnIntegrationTest.java@@ -3,12 +3,17 @@ 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.services.redshiftserverless.RedshiftServerlessService;+import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;+import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;+import io.github.hectorvent.floci.services.secretsmanager.model.Secret;import io.github.hectorvent.floci.testing.CfnStackWaits;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 jakarta.inject.Inject;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;@@ -52,7 +57,7 @@ class RedshiftServerlessCfnIntegrationTest {"Properties": {"NamespaceName": "%s","AdminUsername": "admin",- "AdminUserPassword": "Secret123!",+ "ManageAdminPassword": true,"DbName": "analytics","LogExports": ["userlog"],"Tags": [{"Key": "env", "Value": "dev"}]@@ -99,6 +104,12 @@ class RedshiftServerlessCfnIntegrationTest {@InjectMockRedshiftServerlessRuntime runtime;+ @Inject+ RedshiftServerlessService serverless;++ @Inject+ SecretsManagerService secrets;+@BeforeEachvoid stubRuntime() {when(runtime.generatePassword()).thenReturn("Generated123");@@ -107,6 +118,37 @@ class RedshiftServerlessCfnIntegrationTest {.thenReturn(new RedshiftServerlessRuntime.Backend("127.0.0.1", 55432));}+ @Test+ void failedStackUpdateRestoresManagedCredentialsAndOriginalSecret() {+ cloudFormation("CreateStack", "32");+ describeStacks("CREATE_COMPLETE");+ Namespace original = serverless.getNamespace(NAMESPACE, "us-east-1");+ String arn = original.getAdminPasswordSecretArn();+ Secret originalSecret = secrets.describeSecret(arn, "us-east-1");+ String originalVersion = originalSecret.getCurrentVersionId();+ String originalValue = secrets.getSecretValue(arn, null, null, "us-east-1").getSecretString();+ try {+ String update = TEMPLATE.replace("\"ManageAdminPassword\": true",+ "\"ManageAdminPassword\": false, \"AdminUserPassword\": \"Changed123!\"");+ given().contentType("application/x-www-form-urlencoded").header("Authorization", CFN_AUTH)+ .formParam("Action", "UpdateStack").formParam("StackName", STACK)+ .formParam("TemplateBody", update)+ .formParam("Parameters.member.1.ParameterKey", "Capacity")+ .formParam("Parameters.member.1.ParameterValue", "0")+ .when().post("/").then().statusCode(200);+ CfnStackWaits.awaitStatus(STACK, "UPDATE_ROLLBACK_COMPLETE");++ Namespace restored = serverless.getNamespace(NAMESPACE, "us-east-1");+ assertEquals(arn, restored.getAdminPasswordSecretArn());+ assertEquals(original.getAdminUserPassword(), restored.getAdminUserPassword());+ assertEquals(originalVersion, secrets.describeSecret(arn, "us-east-1").getCurrentVersionId());+ assertEquals(originalValue, secrets.getSecretValue(arn, null, null, "us-east-1").getSecretString());+ } finally {+ cloudFormation("DeleteStack", null);+ CfnStackWaits.awaitStackDeleted(STACK);+ }+ }+@Testvoid createUpdateAndDeleteANamespaceAndWorkgroup() {cloudFormation("CreateStack", "32");@@ -116,6 +158,8 @@ class RedshiftServerlessCfnIntegrationTest {assertEquals(WORKGROUP, outputValue(created, "WgRef"));assertEquals("analytics", outputValue(created, "NsDb"));assertEquals("AVAILABLE", outputValue(created, "NsStatus"));+ String secretArn = serverless.getNamespace(NAMESPACE, "us-east-1").getAdminPasswordSecretArn();+ assertTrue(secretArn.startsWith("arn:aws:secretsmanager:"));assertEquals("AVAILABLE", outputValue(created, "WgStatus"));assertEquals(NAMESPACE, outputValue(created, "WgNamespace"));assertEquals("32", outputValue(created, "WgBase"));@@ -128,6 +172,7 @@ class RedshiftServerlessCfnIntegrationTest {.statusCode(200).body("namespace.namespaceArn", equalTo(outputValue(created, "NsArn"))).body("namespace.namespaceId", equalTo(outputValue(created, "NsId")))+ .body("namespace.adminPasswordSecretArn", equalTo(secretArn)).body("namespace.logExports", hasItem("userlog"));call("GetWorkgroup", "{\"workgroupName\":\"" + WORKGROUP + "\"}").statusCode(200)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.javaindex fd4c22329a..c43eb69110 100644--- 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@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateimport 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.RedshiftServerlessService.NamespaceUpdateSnapshot;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;@@ -32,9 +33,11 @@ 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.doNothing;import static org.mockito.Mockito.doThrow;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.never;+import static org.mockito.Mockito.times;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;@@ -46,6 +49,9 @@ class RedshiftServerlessCfnProvisionerTest {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 static final String ADMIN_SECRET_ARN =+ "arn:aws:secretsmanager:us-east-1:000000000000:secret:admin-abcdef";+ private static final String ADMIN_SECRET_KEY = "arn:aws:kms:us-east-1:000000000000:key/admin-key";private final RedshiftServerlessService service = mock(RedshiftServerlessService.class);private final RedshiftServerlessCfnProvisioner provisioner = new RedshiftServerlessCfnProvisioner(service);@@ -104,7 +110,8 @@ class RedshiftServerlessCfnProvisionerTest {@Testvoid createNamespaceSendsPropertiesAndRecordsEveryDottedAttribute() {when(service.createNamespace(eq("my-ns"), eq("admin"), eq("Secret123"), eq("analytics"), isNull(),- isNull(), anyList(), anyList(), anyMap(), eq(REGION))).thenReturn(namespace("my-ns"));+ isNull(), anyList(), anyList(), anyMap(), isNull(), isNull(), eq(REGION)))+ .thenReturn(namespace("my-ns"));ObjectNode props = mapper.createObjectNode();props.put("NamespaceName", "my-ns");props.put("AdminUsername", "admin");@@ -131,14 +138,123 @@ class RedshiftServerlessCfnProvisionerTest {@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));+ isNull(), anyList(), anyList(), tags.capture(), isNull(), isNull(), eq(REGION));assertEquals(Map.of("env", "dev"), tags.getValue());}+ @Test+ void namespaceRollbackRetainsSnapshotOnFailureAndRestoresAttributesOnRetry() throws Exception {+ Namespace previous = namespace("existing-ns");+ previous.setAdminUserPassword("Original123!");+ previous.setAdminPasswordSecretArn(ADMIN_SECRET_ARN);+ NamespaceUpdateSnapshot snapshot = new NamespaceUpdateSnapshot(previous, null);+ when(service.getNamespace("existing-ns", REGION)).thenReturn(previous);+ when(service.snapshotNamespaceUpdate("existing-ns", REGION)).thenReturn(snapshot);+ when(service.reconcileNamespace(eq("existing-ns"), any(), any(), any(), any(), anyList(), anyList(),+ eq(false), any(), eq(REGION))).thenReturn(namespace("existing-ns"));+ StackResource resource = resource(NAMESPACE_TYPE, "existing-ns");+ resource.getAttributes().put("Namespace.NamespaceArn", NAMESPACE_ARN);+ Map<String, String> previousAttributes = Map.copyOf(resource.getAttributes());+ provisioner.provision(resource, mapper.readTree("""+ {"NamespaceName":"existing-ns", "AdminUserPassword":"Changed123!", "ManageAdminPassword":false}+ """), ctx("existing-ns"));++ assertTrue(provisioner.retainsFailedUpdateState(resource));+ doThrow(new IllegalStateException("Backend unavailable")).when(service).restoreNamespaceUpdate(any(), eq(REGION));+ assertThrows(IllegalStateException.class, () -> provisioner.rollbackUpdate(resource));+ assertTrue(provisioner.retainsFailedUpdateState(resource));+ doNothing().when(service).restoreNamespaceUpdate(any(), eq(REGION));++ assertTrue(provisioner.rollbackUpdate(resource));+ assertEquals(previousAttributes, resource.getAttributes());+ assertFalse(provisioner.retainsFailedUpdateState(resource));+ ArgumentCaptor<NamespaceUpdateSnapshot> restored = ArgumentCaptor.forClass(NamespaceUpdateSnapshot.class);+ verify(service, times(2)).restoreNamespaceUpdate(restored.capture(), eq(REGION));+ assertEquals("Original123!", restored.getValue().namespace().getAdminUserPassword());+ assertEquals(ADMIN_SECRET_ARN, restored.getValue().namespace().getAdminPasswordSecretArn());+ }++ @Test+ void committedNamespaceUpdateClearsRollbackSnapshot() throws Exception {+ Namespace previous = namespace("existing-ns");+ when(service.getNamespace("existing-ns", REGION)).thenReturn(previous);+ when(service.snapshotNamespaceUpdate("existing-ns", REGION))+ .thenReturn(new NamespaceUpdateSnapshot(previous, null));+ when(service.reconcileNamespace(eq("existing-ns"), any(), any(), any(), any(), anyList(), anyList(),+ eq(false), any(), eq(REGION))).thenReturn(previous);+ StackResource resource = resource(NAMESPACE_TYPE, "existing-ns");+ provisioner.provision(resource, mapper.readTree("{\"NamespaceName\":\"existing-ns\"}"), ctx("existing-ns"));++ assertTrue(provisioner.completeUpdate(resource).complete());+ provisioner.clearUpdate(resource);++ assertFalse(provisioner.retainsFailedUpdateState(resource));+ assertFalse(provisioner.rollbackUpdate(resource));+ }++ @Test+ void createNamespacePassesManagedCredentialPropertiesWithoutExposingSecretAttributes() {+ Namespace namespace = namespace("managed-ns");+ namespace.setAdminPasswordSecretArn(ADMIN_SECRET_ARN);+ namespace.setAdminPasswordSecretKmsKeyId(ADMIN_SECRET_KEY);+ when(service.createNamespace(eq("managed-ns"), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),+ anyList(), anyMap(), eq(true), eq(ADMIN_SECRET_KEY), eq(REGION))).thenReturn(namespace);+ ObjectNode props = mapper.createObjectNode();+ props.put("NamespaceName", "managed-ns");+ props.put("ManageAdminPassword", true);+ props.put("AdminPasswordSecretKmsKeyId", ADMIN_SECRET_KEY);+ StackResource resource = resource(NAMESPACE_TYPE, null);++ provisioner.provision(resource, props, ctx(null));++ verify(service).createNamespace(eq("managed-ns"), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),+ anyList(), anyMap(), eq(true), eq(ADMIN_SECRET_KEY), eq(REGION));+ assertFalse(resource.getAttributes().containsKey("Namespace.AdminPasswordSecretArn"));+ assertFalse(resource.getAttributes().containsKey("Namespace.AdminPasswordSecretKmsKeyId"));+ }++ @Test+ void updateNamespacePassesManagedCredentialPropertiesWithoutExposingSecretAttributes() {+ Namespace namespace = namespace("managed-ns");+ namespace.setAdminPasswordSecretArn(ADMIN_SECRET_ARN);+ namespace.setAdminPasswordSecretKmsKeyId(ADMIN_SECRET_KEY);+ when(service.getNamespace("managed-ns", REGION)).thenReturn(namespace);+ when(service.reconcileNamespace(eq("managed-ns"), isNull(), isNull(), isNull(), isNull(), anyList(),+ anyList(), eq(true), eq(ADMIN_SECRET_KEY), eq(REGION))).thenReturn(namespace);+ when(service.listTagsForResource(NAMESPACE_ARN, REGION)).thenReturn(Map.of());+ ObjectNode props = mapper.createObjectNode();+ props.put("NamespaceName", "managed-ns");+ props.put("ManageAdminPassword", true);+ props.put("AdminPasswordSecretKmsKeyId", ADMIN_SECRET_KEY);+ StackResource resource = resource(NAMESPACE_TYPE, "managed-ns");++ provisioner.provision(resource, props, ctx("managed-ns"));++ verify(service).reconcileNamespace(eq("managed-ns"), isNull(), isNull(), isNull(), isNull(), anyList(),+ anyList(), eq(true), eq(ADMIN_SECRET_KEY), eq(REGION));+ assertFalse(resource.getAttributes().containsKey("Namespace.AdminPasswordSecretArn"));+ assertFalse(resource.getAttributes().containsKey("Namespace.AdminPasswordSecretKmsKeyId"));+ }++ @Test+ void updateNamespaceDisablesManagementWithoutExposingSecretAttributes() {+ Namespace namespace = namespace("managed-ns");+ when(service.getNamespace("managed-ns", REGION)).thenReturn(namespace);+ when(service.reconcileNamespace(eq("managed-ns"), isNull(), isNull(), isNull(), isNull(), anyList(),+ anyList(), eq(false), isNull(), eq(REGION))).thenReturn(namespace);+ when(service.listTagsForResource(NAMESPACE_ARN, REGION)).thenReturn(Map.of());+ StackResource resource = resource(NAMESPACE_TYPE, "managed-ns");++ provisioner.provision(resource, mapper.createObjectNode(), ctx("managed-ns"));++ assertFalse(resource.getAttributes().containsKey("Namespace.AdminPasswordSecretArn"));+ assertFalse(resource.getAttributes().containsKey("Namespace.AdminPasswordSecretKmsKeyId"));+ }+@Testvoid anUnnamedNamespaceGetsALowercaseGeneratedNameThatIsStableAcrossUpdates() {when(service.createNamespace(any(), any(), any(), any(), any(), any(), anyList(), anyList(), anyMap(),- eq(REGION)))+ isNull(), isNull(), eq(REGION))).thenAnswer(inv -> namespace(inv.getArgument(0)));StackResource created = resource(NAMESPACE_TYPE, null);@@ -149,7 +265,8 @@ class RedshiftServerlessCfnProvisionerTest {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)))+ when(service.reconcileNamespace(eq(generated), any(), any(), any(), any(), anyList(), anyList(), eq(false),+ isNull(), eq(REGION))).thenReturn(namespace(generated));when(service.listTagsForResource(NAMESPACE_ARN, REGION)).thenReturn(Map.of());StackResource updated = resource(NAMESPACE_TYPE, generated);@@ -157,14 +274,15 @@ class RedshiftServerlessCfnProvisionerTest {provisioner.provision(updated, mapper.createObjectNode(), ctx(generated));assertEquals(generated, updated.getPhysicalId());- verify(service).reconcileNamespace(eq(generated), any(), any(), any(), any(), anyList(), anyList(), eq(REGION));+ verify(service).reconcileNamespace(eq(generated), any(), any(), any(), any(), anyList(), anyList(), eq(false),+ isNull(), eq(REGION));}@Testvoid 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"));+ anyList(), eq(false), isNull(), 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");@@ -175,7 +293,7 @@ class RedshiftServerlessCfnProvisionerTest {provisioner.provision(r, props, ctx("my-ns"));verify(service, never()).createNamespace(any(), any(), any(), any(), any(), any(), anyList(), anyList(),- anyMap(), any());+ anyMap(), any(), any(), 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());@@ -192,14 +310,14 @@ class RedshiftServerlessCfnProvisionerTest {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());+ verify(service, never()).reconcileNamespace(any(), any(), any(), any(), any(), any(), any(), any(), any(), any());}@Testvoid 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"));+ eq(false), isNull(), eq(REGION))).thenReturn(namespace("my-ns"));when(service.listTagsForResource(NAMESPACE_ARN, REGION)).thenReturn(Map.of());ObjectNode props = mapper.createObjectNode();props.put("NamespaceName", "my-ns");@@ -209,14 +327,14 @@ class RedshiftServerlessCfnProvisionerTest {provisioner.provision(r, props, ctx("my-ns"));verify(service).reconcileNamespace(eq("my-ns"), isNull(), isNull(), isNull(), isNull(), anyList(),- anyList(), eq(REGION));+ anyList(), eq(false), isNull(), eq(REGION));}@Testvoid 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"));+ eq(false), isNull(), eq(REGION))).thenReturn(namespace("my-ns"));when(service.listTagsForResource(NAMESPACE_ARN, REGION)).thenReturn(Map.of());ObjectNode props = mapper.createObjectNode();props.put("NamespaceName", "my-ns");diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminSecretsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminSecretsServiceTest.javaindex 9e1c4ce931..93712a3cd8 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminSecretsServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminSecretsServiceTest.java@@ -8,16 +8,24 @@ import io.github.hectorvent.floci.core.common.AwsException;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.RedshiftCredentialBroker;+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 io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;+import io.github.hectorvent.floci.services.redshiftserverless.model.RedshiftServerlessSnapshot;import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;import io.github.hectorvent.floci.services.secretsmanager.model.Secret;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.io.TempDir;import org.mockito.ArgumentCaptor;+import java.nio.file.Files;+import java.nio.file.Path;import java.util.List;import java.util.Map;+import java.util.concurrent.atomic.AtomicReference;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNull;@@ -29,6 +37,8 @@ import static org.mockito.ArgumentMatchers.eq;import static org.mockito.ArgumentMatchers.isNull;import static org.mockito.Mockito.RETURNS_DEEP_STUBS;import static org.mockito.Mockito.clearInvocations;+import static org.mockito.Mockito.doAnswer;+import static org.mockito.Mockito.doReturn;import static org.mockito.Mockito.doThrow;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.never;@@ -54,11 +64,18 @@ class RedshiftServerlessAdminSecretsServiceTest {private RedshiftServerlessRuntime runtime;private SecretsManagerService secrets;private ObjectMapper mapper;+ private StorageFactory storageFactory;+ private RegionResolver regions;+ private RedshiftServerlessEndpoints endpoints;+ private EmulatorConfig config;++ @TempDir+ Path tempDir;@BeforeEach@SuppressWarnings({"unchecked", "rawtypes"})void setup() {- StorageFactory storageFactory = mock(StorageFactory.class);+ storageFactory = mock(StorageFactory.class);namespaces = spy(AccountAwareStorageBackend.inMemory(ACCOUNT));when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-namespaces.json"),any(TypeReference.class))).thenReturn((AccountAwareStorageBackend) namespaces);@@ -66,12 +83,12 @@ class RedshiftServerlessAdminSecretsServiceTest {any(TypeReference.class))).thenReturn(AccountAwareStorageBackend.inMemory(ACCOUNT));when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-snapshots.json"),any(TypeReference.class))).thenReturn(AccountAwareStorageBackend.inMemory(ACCOUNT));- RegionResolver regions = mock(RegionResolver.class);+ regions = mock(RegionResolver.class);when(regions.getAccountId()).thenReturn(ACCOUNT);when(regions.buildArn(eq("redshift-serverless"), eq(REGION), any(String.class))).thenAnswer(invocation -> "arn:aws:redshift-serverless:" + REGION + ":" + ACCOUNT + ":"+ invocation.getArgument(2, String.class));- RedshiftServerlessEndpoints endpoints = mock(RedshiftServerlessEndpoints.class);+ endpoints = mock(RedshiftServerlessEndpoints.class);when(endpoints.allocate()).thenReturn(new Endpoint("localhost", 7100));runtime = mock(RedshiftServerlessRuntime.class);when(runtime.generatePassword()).thenReturn(GENERATED_PASSWORD);@@ -82,10 +99,13 @@ class RedshiftServerlessAdminSecretsServiceTest {Secret secret = new Secret();secret.setArn(SECRET_ARN);secret.setKmsKeyId(SECRET_KEY);+ secret.setOwningService("redshift");+ when(secrets.describeSecret(SECRET_ARN, REGION)).thenReturn(secret);when(secrets.createSecret(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(secret);when(secrets.createSecret(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(secret);mapper = new ObjectMapper().findAndRegisterModules();- EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);+ config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);+ when(config.storage().persistentPath()).thenReturn(tempDir.toString());service = new RedshiftServerlessService(storageFactory, regions, endpoints, runtime, config,new RedshiftServerlessAdminSecrets(secrets, mapper));}@@ -231,6 +251,186 @@ class RedshiftServerlessAdminSecretsServiceTest {assertEquals(SECRET_KEY, copy.getAdminPasswordSecretKmsKeyId());}+ @Test+ void namespaceRollbackRestoresDeletedManagedSecretAndBackendPassword() {+ Namespace previous = createManaged();+ attachWorkgroup();+ RedshiftServerlessService.NamespaceUpdateSnapshot snapshot = service.snapshotNamespaceUpdate(NAME, REGION);+ update(null, ORIGINAL_PASSWORD, false);+ clearInvocations(runtime, secrets);++ service.restoreNamespaceUpdate(snapshot, REGION);++ assertEquals(previous.getAdminPasswordSecretArn(), service.getNamespace(NAME, REGION).getAdminPasswordSecretArn());+ assertEquals(GENERATED_PASSWORD, service.getNamespace(NAME, REGION).getAdminUserPassword());+ verify(runtime).changeMasterPassword(ACCOUNT, REGION, WORKGROUP, "admin", "analytics",+ ORIGINAL_PASSWORD, GENERATED_PASSWORD);+ ArgumentCaptor<Secret> restored = ArgumentCaptor.forClass(Secret.class);+ verify(secrets).restoreManagedSecret(restored.capture(), eq(REGION));+ assertEquals(SECRET_ARN, restored.getValue().getArn());+ assertEquals(SECRET_KEY, restored.getValue().getKmsKeyId());+ }++ @Test+ void namespaceRollbackLeavesCredentialsUnchangedWhenSecretRestoreConflicts() {+ createManaged();+ attachWorkgroup();+ RedshiftServerlessService.NamespaceUpdateSnapshot snapshot = service.snapshotNamespaceUpdate(NAME, REGION);+ Namespace current = update(null, ORIGINAL_PASSWORD, false);+ clearInvocations(runtime, secrets);+ AwsException conflict = new AwsException("ResourceExistsException", "Secret name has been reused", 400);+ doThrow(conflict).when(secrets).restoreManagedSecret(any(Secret.class), eq(REGION));++ assertSame(conflict, assertThrows(AwsException.class, () -> service.restoreNamespaceUpdate(snapshot, REGION)));++ assertSame(current, service.getNamespace(NAME, REGION));+ assertEquals(ORIGINAL_PASSWORD, current.getAdminUserPassword());+ assertNull(current.getAdminPasswordSecretArn());+ verify(runtime, never()).changeMasterPassword(any(), any(), any(), any(), any(), any(), any());+ verify(runtime, never()).restoreMasterPassword(any(), any(), any(), any(), any(), any());+ verify(secrets, never()).deleteSecret(any(), any(), anyBoolean(), any());+ }++ @Test+ void namespaceRollbackRemovesNewManagedSecretAndRestoresUnmanagedPassword() {+ createUnmanaged();+ attachWorkgroup();+ RedshiftServerlessService.NamespaceUpdateSnapshot snapshot = service.snapshotNamespaceUpdate(NAME, REGION);+ update(null, null, true);+ clearInvocations(runtime, secrets);++ service.restoreNamespaceUpdate(snapshot, REGION);++ assertNull(service.getNamespace(NAME, REGION).getAdminPasswordSecretArn());+ assertEquals(ORIGINAL_PASSWORD, service.getNamespace(NAME, REGION).getAdminUserPassword());+ verify(runtime).changeMasterPassword(ACCOUNT, REGION, WORKGROUP, "admin", "analytics",+ GENERATED_PASSWORD, ORIGINAL_PASSWORD);+ verify(secrets).deleteSecret(SECRET_ARN, null, true, REGION);+ }++ @Test+ void blockedRollbackSecretDeletionRestoresCurrentBackendPasswordAndCanRetry() {+ createUnmanaged();+ attachWorkgroup();+ RedshiftServerlessService.NamespaceUpdateSnapshot snapshot = service.snapshotNamespaceUpdate(NAME, REGION);+ Namespace managed = update(null, null, true);+ AtomicReference<String> backendPassword = new AtomicReference<>(GENERATED_PASSWORD);+ doAnswer(invocation -> {+ backendPassword.set(invocation.getArgument(6, String.class));+ return null;+ }).when(runtime).changeMasterPassword(any(), any(), any(), any(), any(), any(), any());+ doAnswer(invocation -> {+ backendPassword.set(invocation.getArgument(5, String.class));+ return null;+ }).when(runtime).restoreMasterPassword(any(), any(), any(), any(), any(), any());+ AwsException failure = new AwsException("InvalidRequestException", "Secret has replicas", 400);+ doThrow(failure).when(secrets).deleteSecret(SECRET_ARN, null, true, REGION);++ assertSame(failure, assertThrows(AwsException.class, () -> service.restoreNamespaceUpdate(snapshot, REGION)));++ assertEquals(GENERATED_PASSWORD, backendPassword.get());+ assertSame(managed, service.getNamespace(NAME, REGION));+ assertEquals(SECRET_ARN, managed.getAdminPasswordSecretArn());+ assertEquals(managed.getAdminUserPassword(), backendPassword.get());+ doReturn(null).when(secrets).deleteSecret(SECRET_ARN, null, true, REGION);++ service.restoreNamespaceUpdate(snapshot, REGION);++ assertEquals(ORIGINAL_PASSWORD, backendPassword.get());+ assertEquals(ORIGINAL_PASSWORD, service.getNamespace(NAME, REGION).getAdminUserPassword());+ assertNull(service.getNamespace(NAME, REGION).getAdminPasswordSecretArn());+ }++ @Test+ void failedCredentialRestorePutsBackLiveDatabaseContents() throws Exception {+ createUnmanaged();+ RedshiftServerlessSnapshot snapshot = service.createSnapshot("failed-restore", NAME, null, null, REGION);+ Path dump = tempDir.resolve("redshift-serverless-dumps").resolve(ACCOUNT).resolve(REGION)+ .resolve("failed-restore.sql");+ Files.createDirectories(dump.getParent());+ Files.writeString(dump, "snapshot-data");+ snapshot.setSqlDump(dump.toString());+ attachWorkgroup();+ Namespace previous = update(null, null, true);+ AtomicReference<String> contents = new AtomicReference<>("live-data");+ RedshiftContainerManager containers = mock(RedshiftContainerManager.class);+ doAnswer(invocation -> {+ Files.writeString(invocation.getArgument(4, Path.class), contents.get());+ return null;+ }).when(containers).takeSnapshot(any(), any(), any(), any(), any(), eq(true));+ doAnswer(invocation -> {+ contents.set("");+ return null;+ }).when(containers).resetUserSchemas(any(), any(), any(), any());+ doAnswer(invocation -> {+ contents.set(Files.readString(invocation.getArgument(4, Path.class)));+ return null;+ }).when(containers).restoreSnapshot(any(), any(), any(), any(), any(), eq(true));+ RedshiftServerlessRuntime realRuntime = new RedshiftServerlessRuntime(containers,+ mock(RedshiftProxyManager.class), mock(RedshiftCredentialBroker.class));+ service = new RedshiftServerlessService(storageFactory, regions, endpoints, realRuntime, config,+ new RedshiftServerlessAdminSecrets(secrets, mapper));+ AwsException failure = new AwsException("InvalidRequestException", "Secret has replicas", 400);+ doThrow(failure).when(secrets).deleteSecret(SECRET_ARN, null, true, REGION);++ assertSame(failure, assertThrows(AwsException.class,+ () -> service.restoreFromSnapshot(NAME, WORKGROUP, "failed-restore", null, REGION)));++ assertEquals("live-data", contents.get());+ assertSame(previous, service.getNamespace(NAME, REGION));+ assertEquals(GENERATED_PASSWORD, service.getNamespace(NAME, REGION).getAdminUserPassword());+ }++ @Test+ void restoreWithoutManagedCredentialsRestoresSnapshotPasswordAndRemovesManagedSecret() {+ createUnmanaged();+ service.createSnapshot("managed-restore-snap", NAME, null, null, REGION);+ attachWorkgroup();+ update(null, null, true);+ clearInvocations(secrets, runtime);++ Namespace restored = service.restoreFromSnapshot(NAME, WORKGROUP, "managed-restore-snap", null, REGION)+ .namespace();++ assertEquals(ORIGINAL_PASSWORD, restored.getAdminUserPassword());+ assertNull(restored.getAdminPasswordSecretArn());+ verify(runtime).changeMasterPassword(ACCOUNT, REGION, WORKGROUP, "admin", "analytics",+ GENERATED_PASSWORD, ORIGINAL_PASSWORD);+ verify(secrets).deleteSecret(SECRET_ARN, null, true, REGION);+ }++ @Test+ void restoreWithManagedCredentialsRefreshesPasswordAndExistingSecret() throws Exception {+ when(runtime.generatePassword()).thenReturn(GENERATED_PASSWORD, "Restored456!");+ createManaged();+ service.createSnapshot("refresh-managed-snap", NAME, null, null, REGION);+ attachWorkgroup();+ clearInvocations(secrets, runtime);++ Namespace restored = service.restoreFromSnapshot(NAME, WORKGROUP, "refresh-managed-snap", null,+ true, SECRET_KEY, REGION).namespace();++ assertEquals("Restored456!", restored.getAdminUserPassword());+ assertEquals(SECRET_ARN, restored.getAdminPasswordSecretArn());+ verify(runtime).changeMasterPassword(ACCOUNT, REGION, WORKGROUP, "admin", "analytics",+ GENERATED_PASSWORD, "Restored456!");+ ArgumentCaptor<String> secretValue = ArgumentCaptor.forClass(String.class);+ verify(secrets).putSecretValue(eq(SECRET_ARN), secretValue.capture(), isNull(), isNull(), eq(REGION), any());+ assertEquals("Restored456!", mapper.readTree(secretValue.getValue()).path("password").asText());+ }++ @Test+ void restoreFromSnapshotWithoutStoredPasswordKeepsCurrentCredentials() {+ createUnmanaged();+ service.createSnapshot("legacy-snap", NAME, null, null, REGION).setAdminUserPassword(null);+ service.updateNamespace(NAME, null, "Changed123!", null, null, null, null, REGION);+ attachWorkgroup();++ Namespace restored = service.restoreFromSnapshot(NAME, WORKGROUP, "legacy-snap", null, REGION).namespace();++ assertEquals("Changed123!", restored.getAdminUserPassword());+ }+private Namespace createManaged() {return service.createNamespace(NAME, "admin", null, "analytics", null, null, List.of(), List.of(), Map.of(),true, SECRET_KEY, REGION);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.javaindex d5704c0383..96e4d3f3c5 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@@ -256,6 +256,10 @@ class RedshiftServerlessIntegrationTest {@Testvoid snapshotLifecycleIsVisibleThroughSeparateReads() {+ doAnswer(invocation -> {+ invocation.getArgument(6, Runnable.class).run();+ return null;+ }).when(runtime).restoreSnapshot(any(), any(), any(), any(), any(), any(), any());doAnswer(invocation -> {Files.writeString(invocation.getArgument(5, Path.class), "-- dump");return null;@@ -330,10 +334,11 @@ class RedshiftServerlessIntegrationTest {.body("snapshots", hasSize(0));call("RestoreFromSnapshot", "{\"namespaceName\":\"snap-life-ns\",\"workgroupName\":\"snap-life-wg\","- + "\"snapshotArn\":\"" + snapshotArn + "\"}")+ + "\"snapshotArn\":\"" + snapshotArn + "\",\"manageAdminPassword\":true}").statusCode(200).body("snapshotName", equalTo("snap-life")).body("namespace.namespaceName", equalTo("snap-life-ns"))+ .body("namespace.adminPasswordSecretArn", notNullValue()).body("namespace.status", equalTo("AVAILABLE")).body("ownerAccount", matchesPattern("\\d{12}"));call("RestoreFromSnapshot", "{\"workgroupName\":\"snap-life-wg\",\"snapshotName\":\"snap-life\"}")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.javaindex ef1e9c7a1a..d998e10b86 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@@ -50,6 +50,27 @@ class RedshiftServerlessJsonHandlerTest {assertTrue(response.path("namespace").path("adminPasswordSecretArn").isTextual(), response.toString());}+ @Test+ void restoreFromSnapshotEnablesManagedCredentialsWhenRequested() {+ body(handler.handle("CreateNamespace", parse(+ "{\"namespaceName\":\"restore-managed-ns\",\"adminUsername\":\"admin\","+ + "\"adminUserPassword\":\"Initial123\"}"), REGION));+ body(handler.handle("CreateSnapshot", parse(+ "{\"snapshotName\":\"restore-managed-snap\",\"namespaceName\":\"restore-managed-ns\"}"), REGION));+ body(handler.handle("CreateWorkgroup", parse(+ "{\"workgroupName\":\"restore-managed-wg\",\"namespaceName\":\"restore-managed-ns\"}"), REGION));++ JsonNode restored = body(handler.handle("RestoreFromSnapshot", parse(+ "{\"namespaceName\":\"restore-managed-ns\",\"workgroupName\":\"restore-managed-wg\","+ + "\"snapshotName\":\"restore-managed-snap\",\"manageAdminPassword\":true,"+ + "\"adminPasswordSecretKmsKeyId\":\"arn:aws:kms:us-east-1:123456789012:key/managed\"}"),+ REGION));++ assertTrue(restored.path("namespace").path("adminPasswordSecretArn").isTextual(), restored.toString());+ assertEquals("arn:aws:kms:us-east-1:123456789012:key/managed",+ restored.path("namespace").path("adminPasswordSecretKmsKeyId").textValue());+ }+private static final String REGION = "us-east-1";private static final String ACCOUNT_ID = "123456789012";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.javaindex b6ededf487..c036ab68e3 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessRuntimeTest.java@@ -12,17 +12,22 @@ import org.junit.jupiter.api.Test;import org.mockito.ArgumentCaptor;import org.mockito.InOrder;+import java.nio.file.Files;import java.nio.file.Path;import java.util.List;import java.util.Optional;+import java.util.concurrent.atomic.AtomicReference;import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertNotEquals;+import static org.junit.jupiter.api.Assertions.assertSame;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.doAnswer;import static org.mockito.Mockito.doThrow;import static org.mockito.Mockito.inOrder;import static org.mockito.Mockito.mock;@@ -108,6 +113,30 @@ class RedshiftServerlessRuntimeTest {any(Path.class), eq(true));}+ @Test+ void credentialFailureKeepsSafetyDumpUntilDataIsRestored() {+ Path dump = Path.of("/tmp/dump.sql");+ AtomicReference<Path> safety = new AtomicReference<>();+ doAnswer(invocation -> {+ safety.set(invocation.getArgument(4, Path.class));+ return null;+ }).when(containers).takeSnapshot(any(), any(), any(), any(), any(), eq(true));+ AwsException failure = new AwsException("InvalidRequestException", "Secret has replicas", 400);++ assertSame(failure, assertThrows(AwsException.class, () -> runtime.restoreSnapshot(+ ACCOUNT, REGION, "my-wg", "root", "analytics", dump, () -> {+ assertTrue(Files.exists(safety.get()));+ throw failure;+ })));++ InOrder order = inOrder(containers);+ order.verify(containers).resetUserSchemas(ACCOUNT, BACKEND_ID, "root", "analytics");+ order.verify(containers).restoreSnapshot(ACCOUNT, BACKEND_ID, "root", "analytics", dump, true);+ order.verify(containers).resetUserSchemas(ACCOUNT, BACKEND_ID, "root", "analytics");+ order.verify(containers).restoreSnapshot(ACCOUNT, BACKEND_ID, "root", "analytics", safety.get(), true);+ assertFalse(Files.exists(safety.get()));+ }+@Testvoid failedAdoptedProxyUsesRecoveryRollback() {when(containers.adoptOrStart(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics", "serverless")).thenReturn(handle());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.javaindex 5e8816780e..86b1ae341a 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@@ -90,6 +90,10 @@ class RedshiftServerlessServiceTest {endpoints = mock(RedshiftServerlessEndpoints.class);when(endpoints.allocate()).thenAnswer(invocation -> new Endpoint("localhost", nextPort++));runtime = mock(RedshiftServerlessRuntime.class);+ doAnswer(invocation -> {+ invocation.getArgument(6, Runnable.class).run();+ return null;+ }).when(runtime).restoreSnapshot(any(), any(), any(), any(), any(), any(), any());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()))@@ -925,7 +929,7 @@ class RedshiftServerlessServiceTest {assertTrue(Files.exists(Path.of(snapshot.getSqlDump())));service.restoreFromSnapshot("dump-ns", "dump-wg", "dump-snap", null, REGION);verify(runtime).restoreSnapshot(eq(ACCOUNT_ID), eq(REGION), eq("dump-wg"), any(), eq("dev"),- eq(Path.of(snapshot.getSqlDump())));+ eq(Path.of(snapshot.getSqlDump())), any(Runnable.class));service.deleteSnapshot("dump-snap", REGION);assertFalse(Files.exists(Path.of(snapshot.getSqlDump())));@@ -945,7 +949,7 @@ class RedshiftServerlessServiceTest {AwsException missing = assertThrows(AwsException.class,() -> service.restoreFromSnapshot("gone-ns", "gone-wg", "gone-snap", null, REGION));assertEquals("InternalServerException", missing.getErrorCode());- verify(runtime, never()).restoreSnapshot(any(), any(), any(), any(), any(), any());+ verify(runtime, never()).restoreSnapshot(any(), any(), any(), any(), any(), any(), any());}@Test@@ -962,7 +966,7 @@ class RedshiftServerlessServiceTest {assertNull(snapshot.getSqlDump());verify(runtime, never()).takeSnapshot(any(), any(), any(), any(), any(), any());- verify(runtime, never()).restoreSnapshot(any(), any(), any(), any(), any(), any());+ verify(runtime, never()).restoreSnapshot(any(), any(), any(), any(), any(), any(), any());}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerManagedSecretRestoreTest.java b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerManagedSecretRestoreTest.javanew file mode 100644index 0000000000..ab22406be1--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerManagedSecretRestoreTest.java@@ -0,0 +1,197 @@+package io.github.hectorvent.floci.services.secretsmanager;++import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.core.storage.InMemoryStorage;+import io.github.hectorvent.floci.services.secretsmanager.model.Secret;+import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;++import java.util.List;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicBoolean;+import java.util.concurrent.atomic.AtomicReference;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNotEquals;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.junit.jupiter.api.Assertions.assertTrue;++class SecretsManagerManagedSecretRestoreTest {++ private static final String REGION = "us-east-1";+ private static final String NAME = "redshift-serverless/namespace-id";++ private final SecretsManagerService service = new SecretsManagerService(new InMemoryStorage<>(), 30);+ private final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();++ @ParameterizedTest+ @CsvSource({"true,000000000000", "false,000000000000", "true,111122223333", "false,111122223333"})+ void createAndRollbackCannotOverwriteEachOther(boolean restoreFirst, String accountId) throws Exception {+ ParkingStorage storage = new ParkingStorage();+ AccountAwareStorageBackend<Secret> accountStore = new AccountAwareStorageBackend<>(storage, null, accountId);+ SecretsManagerService concurrentService = new SecretsManagerService(accountStore, 30,+ new RegionResolver(REGION, accountId), null, mapper);+ Secret original = concurrentService.createSecret(NAME, "original", null, null, null, null, "redshift", REGION);+ Secret snapshot = mapper.convertValue(original, Secret.class);+ concurrentService.deleteSecret(original.getArn(), null, true, REGION);+ AtomicReference<Secret> replacement = new AtomicReference<>();+ Runnable create = () -> {+ Secret created = concurrentService.createSecret(NAME, "replacement-v1", null, null, null, null, REGION);+ concurrentService.putSecretValue(created.getArn(), "replacement-v2", null, null, REGION,+ List.of("AWSCURRENT", "custom-stage"));+ replacement.set(created);+ };+ Runnable restore = () -> concurrentService.restoreManagedSecret(snapshot, REGION);+ storage.parkNextWrite.set(true);+ Watched winner = new Watched(restoreFirst ? restore : create);+ Watched loser = new Watched(() -> {+ AwsException conflict = assertThrows(AwsException.class, restoreFirst ? create::run : restore::run);+ assertEquals("ResourceExistsException", conflict.getErrorCode());+ });++ winner.thread.start();+ try {+ assertTrue(storage.entered.await(5, TimeUnit.SECONDS));+ loser.thread.start();+ assertTrue(loser.awaitBlocked(), "The competing operation must wait for the name's pending write");+ } finally {+ storage.released.countDown();+ winner.awaitSuccess();+ loser.awaitSuccess();+ }++ Secret expected = restoreFirst ? snapshot : replacement.get();+ assertEquals(mapper.valueToTree(expected), mapper.valueToTree(concurrentService.describeSecret(NAME, REGION)));+ }++ @Test+ void rollbackRefusesAReusedNameAndPreservesTheReplacementSecret() {+ Secret original = service.createSecret(NAME, "original", null, null, null, null,+ "redshift-serverless", REGION);+ Secret snapshot = mapper.convertValue(original, Secret.class);+ service.deleteSecret(original.getArn(), null, true, REGION);+ Secret replacement = service.createSecret(NAME, "replacement-v1", null, null, null, null,+ "redshift-serverless", REGION);+ service.putSecretValue(replacement.getArn(), "replacement-v2", null, null, REGION,+ List.of("AWSCURRENT", "custom-stage"));+ Secret replacementBefore = mapper.convertValue(replacement, Secret.class);+ assertNotEquals(snapshot.getArn(), replacement.getArn());++ AwsException conflict = assertThrows(AwsException.class,+ () -> service.restoreManagedSecret(snapshot, REGION));++ assertEquals("ResourceExistsException", conflict.getErrorCode());+ assertEquals(mapper.valueToTree(replacementBefore),+ mapper.valueToTree(service.describeSecret(NAME, REGION)));+ }++ @Test+ void rollbackChecksTheAccountNamedInTheSnapshotArn() {+ AccountAwareStorageBackend<Secret> store = AccountAwareStorageBackend.inMemory("000000000000");+ SecretsManagerService accountService = new SecretsManagerService(store, 30);+ Secret snapshot = accountService.createSecret(NAME, "original", null, null, null, null,+ "redshift", REGION);+ Secret replacement = mapper.convertValue(snapshot, Secret.class);+ replacement.setArn("arn:aws:secretsmanager:us-east-1:111122223333:secret:" + NAME + "-AbCdEf");+ store.putForAccount("111122223333", REGION + "::" + NAME, replacement);+ Secret foreignSnapshot = mapper.convertValue(snapshot, Secret.class);+ foreignSnapshot.setArn("arn:aws:secretsmanager:us-east-1:111122223333:secret:" + NAME + "-GhIjKl");++ AwsException conflict = assertThrows(AwsException.class,+ () -> accountService.restoreManagedSecret(foreignSnapshot, REGION));++ assertEquals("ResourceExistsException", conflict.getErrorCode());+ assertEquals(replacement.getArn(), store.getForAccount("111122223333", REGION + "::" + NAME)+ .orElseThrow().getArn());+ assertEquals(snapshot.getArn(), accountService.describeSecret(NAME, REGION).getArn());+ }++ @Test+ void rollbackRestoresADeletedSecretWithItsOriginalArnAndVersions() {+ Secret original = service.createSecret(NAME, "original-v1", null, null, null, null,+ "redshift-serverless", REGION);+ service.putSecretValue(original.getArn(), "original-v2", null, null, REGION, null);+ Secret snapshot = mapper.convertValue(original, Secret.class);+ service.deleteSecret(original.getArn(), null, true, REGION);++ service.restoreManagedSecret(snapshot, REGION);++ assertEquals(mapper.valueToTree(snapshot), mapper.valueToTree(service.describeSecret(NAME, REGION)));+ }++ @Test+ void rollbackRestoresTheSnapshotWhenTheCurrentArnMatches() {+ Secret original = service.createSecret(NAME, "original", null, null, null, null,+ "redshift-serverless", REGION);+ Secret snapshot = mapper.convertValue(original, Secret.class);+ SecretVersion changed = service.putSecretValue(original.getArn(), "changed", null, null, REGION, null);+ assertNotEquals(snapshot.getCurrentVersionId(), changed.getVersionId());++ service.restoreManagedSecret(snapshot, REGION);++ assertEquals(mapper.valueToTree(snapshot), mapper.valueToTree(service.describeSecret(NAME, REGION)));+ }++ private static final class ParkingStorage extends InMemoryStorage<String, Secret> {+ private final AtomicBoolean parkNextWrite = new AtomicBoolean();+ private final CountDownLatch entered = new CountDownLatch(1);+ private final CountDownLatch released = new CountDownLatch(1);++ @Override+ public void put(String key, Secret value) {+ if (parkNextWrite.compareAndSet(true, false)) {+ entered.countDown();+ try {+ assertTrue(released.await(10, TimeUnit.SECONDS), "Pending write was not released");+ } catch (InterruptedException e) {+ Thread.currentThread().interrupt();+ throw new AssertionError(e);+ }+ }+ super.put(key, value);+ }+ }++ private static final class Watched {+ private final Thread thread;+ private final AtomicReference<Throwable> failure = new AtomicReference<>();++ Watched(Runnable action) {+ thread = new Thread(() -> {+ try {+ action.run();+ } catch (Throwable t) {+ failure.set(t);+ }+ });+ thread.setDaemon(true);+ }++ boolean awaitBlocked() throws InterruptedException {+ long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);+ while (System.nanoTime() < deadline) {+ if (thread.getState() == Thread.State.BLOCKED) {+ return true;+ }+ if (!thread.isAlive()) {+ return false;+ }+ Thread.sleep(5);+ }+ return false;+ }++ void awaitSuccess() throws InterruptedException {+ thread.join(TimeUnit.SECONDS.toMillis(5));+ assertTrue(!thread.isAlive(), "Secret operation did not finish");+ assertNull(failure.get(), () -> "Secret operation failed: " + failure.get());+ }+ }+}diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yamlindex f49131acba..d62d3fb50f 100644--- a/tools/docs/cfn_resource_types.yaml+++ b/tools/docs/cfn_resource_types.yaml@@ -197,7 +197,7 @@ notes:AWS::ElastiCache::ServerlessCache: 'GetAtt uses the dotted Endpoint.* and ReaderEndpoint.* form'AWS::Redshift::ClusterSecurityGroup: accepted; no EC2-Classic security group modelAWS::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::Namespace: 'Managed admin credentials use Secrets Manager; 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-opAWS::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'