Skip to content

armgit / floci

Public Imported from GitHub
0

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

feat(cloudformation): support Redshift snapshot restores (#6395)

Vũ Thế Huy4c70d6b1a26e
diff --git a/docs/services/redshift.md b/docs/services/redshift.md
index f59ffb4f32..f68e1a4a90 100644
--- a/docs/services/redshift.md
+++ b/docs/services/redshift.md
@@ -234,6 +234,31 @@ For `AWS::Redshift::Cluster`:
Replacement occurs if `ClusterIdentifier`, `DBName`, `MasterUsername`, or `ClusterSubnetGroupName` changes. Other properties (such as `NodeType`, `MasterUserPassword`, `ClusterParameterGroupName`, and `VpcSecurityGroupIds`) update in place.
+`SnapshotIdentifier` restores the snapshot's SQL dump into the new cluster rather than creating
+an empty database. The snapshot must exist in the current account. The restored cluster inherits
+the snapshot's admin username and password, so `MasterUserPassword` can be omitted. The template's
+`MasterUsername` and `MasterUserPassword` do not override snapshot credentials during creation;
+a later in-place update can change the password. `ManageMasterPassword` instead generates a new
+password and stores it in a Redshift-owned Secrets Manager secret.
+
+Changing, adding, or removing `SnapshotIdentifier` replaces the cluster. Updates that keep the
+same snapshot modify the existing cluster without replaying the dump, preserving rows written
+since restoration. A failed replacing update restores the original physical id and attributes
+through the existing replacement rollback mechanism. `Ref`, `Endpoint.Address`, `Endpoint.Port`,
+and `ClusterNamespaceArn` identify the restored cluster.
+
+```yaml
+Warehouse:
+ Type: AWS::Redshift::Cluster
+ Properties:
+ ClusterIdentifier: restored-warehouse
+ NodeType: ra3.large
+ ClusterType: single-node
+ DBName: dev
+ MasterUsername: admin
+ SnapshotIdentifier: warehouse-backup
+```
+
For `AWS::Redshift::ClusterParameterGroup`, `Parameters` is applied via `ModifyClusterParameterGroup` on both create and update. `Description` and `ParameterGroupFamily` are replacement properties, matching AWS: changing either creates a new parameter group instead of reusing the prior one.
### Gaps and Limitations
@@ -243,8 +268,10 @@ For `AWS::Redshift::ClusterParameterGroup`, `Parameters` is applied via `ModifyC
- `NumberOfNodes` is not stored on cluster create: every emulated cluster is backed by a single PostgreSQL container.
- `ManageMasterPassword` creates a Redshift-owned Secrets Manager secret. The secret contains the
managed username, password, endpoint, port, and database name. `MasterPasswordSecretKmsKeyId`
- selects the KMS key used for the secret metadata and is validated through KMS.
-- `SnapshotIdentifier` is ignored: a fresh cluster is created instead of restoring from a snapshot.
+ selects the KMS key used for the secret metadata and is validated through KMS. Switching managed
+ password mode or its KMS key in place is not implemented.
+- Snapshot restoration has the same limitations as `RestoreFromClusterSnapshot`: snapshots are
+ local SQL dumps, not cross-account backups or a full Redshift engine image.
- `AWS::Redshift::ClusterSecurityGroup` is accepted as metadata: Floci does not emulate the legacy EC2-Classic security group model.
## Configuration
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftClusterCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftClusterCfnProvisioner.java
index 7b1a08ab4c..8d4f17119d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftClusterCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftClusterCfnProvisioner.java
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.Digests;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.redshift.RedshiftService;
import io.github.hectorvent.floci.services.redshift.model.Cluster;
@@ -29,6 +30,11 @@ import java.util.UUID;
public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
private static final Logger LOG = Logger.getLogger(RedshiftClusterCfnProvisioner.class);
+ private static final String CLUSTER_IDENTIFIER_ATTR = CfnRollback.USES_ATTR_PREFIX + "RedshiftClusterIdentifier";
+ private static final String SNAPSHOT_IDENTIFIER_ATTR = CfnRollback.USES_ATTR_PREFIX + "RedshiftSnapshotIdentifier";
+ private static final String RESTORE_MASTER_USERNAME_ATTR = CfnRollback.USES_ATTR_PREFIX + "RedshiftRestoreMasterUsername";
+ private static final String MASTER_USER_PASSWORD_HASH_ATTR = CfnRollback.USES_ATTR_PREFIX + "RedshiftMasterUserPasswordHash";
+ private static final String DB_NAME_ATTR = CfnRollback.USES_ATTR_PREFIX + "RedshiftDBName";
private static final String CLUSTER = "AWS::Redshift::Cluster";
private static final String PARAMETER_GROUP = "AWS::Redshift::ClusterParameterGroup";
@@ -100,7 +106,16 @@ public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
String nodeType = ctx.resolveOptional(props, "NodeType");
String masterUsername = ctx.resolveOptional(props, "MasterUsername");
String masterUserPassword = ctx.resolveOptional(props, "MasterUserPassword");
- if (!manageMasterPassword && (masterUserPassword == null || masterUserPassword.isBlank())) {
+ String snapshotIdentifier = ctx.resolveOptional(props, "SnapshotIdentifier");
+ if (snapshotIdentifier != null && snapshotIdentifier.isBlank()) {
+ throw new AwsException("ValidationException", "SnapshotIdentifier must not be blank", 400);
+ }
+ if (snapshotIdentifier != null && manageMasterPassword && masterUserPassword != null) {
+ throw new AwsException("ValidationException",
+ "MasterUserPassword cannot be used with ManageMasterPassword", 400);
+ }
+ if (snapshotIdentifier == null && !manageMasterPassword
+ && (masterUserPassword == null || masterUserPassword.isBlank())) {
throw new AwsException("ValidationException",
"MasterUserPassword is required unless ManageMasterPassword is set", 400);
}
@@ -108,23 +123,30 @@ public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
List<String> securityGroups = ctx.resolveStringList(props, "VpcSecurityGroupIds");
String dbName = ctx.resolveOptional(props, "DBName");
- String priorDbName = attributesBefore.get("Floci::DBName");
+ String priorDbName = attributesBefore.getOrDefault(DB_NAME_ATTR, attributesBefore.get("Floci::DBName"));
boolean dbNameChanged = ctx.isUpdate() && dbName != null && priorDbName != null && !dbName.equals(priorDbName);
String explicitId = ctx.resolveOptional(props, "ClusterIdentifier");
Cluster prior = ctx.isUpdate() ? findExistingCluster(ctx.priorPhysicalId()) : null;
- boolean createOnlyChanged = dbNameChanged || (prior != null && (
- (masterUsername != null && !masterUsername.equals(prior.getMasterUsername()))
- || (subnetGroup != null && !Objects.equals(subnetGroup, prior.getClusterSubnetGroupName()))
- ));
+ boolean snapshotChanged = ctx.isUpdate()
+ && !Objects.equals(snapshotIdentifier, attributesBefore.get(SNAPSHOT_IDENTIFIER_ATTR));
+ boolean masterUsernameChanged = snapshotIdentifier == null
+ ? prior != null && masterUsername != null && !masterUsername.equals(prior.getMasterUsername())
+ : ctx.isUpdate() && !Objects.equals(masterUsername, attributesBefore.get(RESTORE_MASTER_USERNAME_ATTR));
+ boolean createOnlyChanged = dbNameChanged || snapshotChanged || masterUsernameChanged
+ || prior != null && subnetGroup != null && !Objects.equals(subnetGroup, prior.getClusterSubnetGroupName());
+ boolean sameTemplateIdentifier = explicitId == null || explicitId.equals(ctx.priorPhysicalId())
+ || explicitId.equals(attributesBefore.get(CLUSTER_IDENTIFIER_ATTR));
String id;
- if (createOnlyChanged && (explicitId == null || explicitId.equals(ctx.priorPhysicalId()))) {
+ if (createOnlyChanged && sameTemplateIdentifier) {
if (explicitId != null && !explicitId.isBlank()) {
id = replacementId(explicitId, IDENTIFIER_MAX_LENGTH, true);
} else {
id = ctx.generatePhysicalName(r.getLogicalId(), IDENTIFIER_MAX_LENGTH, true);
}
+ } else if (ctx.isUpdate() && sameTemplateIdentifier) {
+ id = ctx.priorPhysicalId();
} else {
id = ctx.stablePhysicalName(explicitId, r.getLogicalId(), IDENTIFIER_MAX_LENGTH, true);
}
@@ -135,9 +157,32 @@ public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
// a derived id that differs from the prior physical id is a replacement handled via ReplacementCleanup.
Cluster cluster;
if (ctx.reusesPriorEntity(id)) {
+ String passwordToApply = null;
+ if (!manageMasterPassword && masterUserPassword != null) {
+ String passwordHash = Digests.sha256Hex(masterUserPassword);
+ boolean passwordExplicitlyChanged = !Objects.equals(passwordHash,
+ attributesBefore.get(MASTER_USER_PASSWORD_HASH_ATTR));
+ if (passwordExplicitlyChanged) {
+ passwordToApply = masterUserPassword;
+ }
+ }
+
cluster = redshiftService.modifyCluster(id, nodeType, numberOfNodes(props, ctx),
- masterUserPassword, ctx.resolveOptional(props, "ClusterParameterGroupName"),
+ passwordToApply, ctx.resolveOptional(props, "ClusterParameterGroupName"),
securityGroups);
+ } else if (snapshotIdentifier != null) {
+ cluster = redshiftService.restoreFromClusterSnapshot(id, snapshotIdentifier, nodeType, subnetGroup,
+ manageMasterPassword, ctx.resolveOptional(props, "MasterPasswordSecretKmsKeyId"), ctx.region());
+ String parameterGroup = ctx.resolveOptional(props, "ClusterParameterGroupName");
+ if (parameterGroup != null || !securityGroups.isEmpty()) {
+ try {
+ cluster = redshiftService.modifyCluster(id, nodeType, numberOfNodes(props, ctx),
+ null, parameterGroup, securityGroups);
+ } catch (RuntimeException failure) {
+ cleanupRestoredCluster(id, failure);
+ throw failure;
+ }
+ }
} else if (manageMasterPassword) {
String kmsKeyId = props.path("MasterPasswordSecretKmsKeyId").asText(null);
cluster = redshiftService.createClusterWithManagedMasterPassword(id, nodeType, masterUsername,
@@ -149,12 +194,35 @@ public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
Map<String, String> tags = ctx.resolveTags(props, "Tags");
if (!tags.isEmpty()) {
- redshiftService.createTags(id, tags);
+ try {
+ redshiftService.createTags(id, tags);
+ } catch (RuntimeException failure) {
+ if (snapshotIdentifier != null && !ctx.reusesPriorEntity(id)) {
+ cleanupRestoredCluster(id, failure);
+ }
+ throw failure;
+ }
}
r.setPhysicalId(id);
+ r.getAttributes().remove(CLUSTER_IDENTIFIER_ATTR);
+ if (explicitId != null) {
+ r.getAttributes().put(CLUSTER_IDENTIFIER_ATTR, explicitId);
+ }
+ r.getAttributes().remove(SNAPSHOT_IDENTIFIER_ATTR);
+ r.getAttributes().remove(RESTORE_MASTER_USERNAME_ATTR);
+ if (snapshotIdentifier != null) {
+ r.getAttributes().put(SNAPSHOT_IDENTIFIER_ATTR, snapshotIdentifier);
+ if (masterUsername != null) {
+ r.getAttributes().put(RESTORE_MASTER_USERNAME_ATTR, masterUsername);
+ }
+ }
+ r.getAttributes().remove(MASTER_USER_PASSWORD_HASH_ATTR);
+ if (!manageMasterPassword && masterUserPassword != null) {
+ r.getAttributes().put(MASTER_USER_PASSWORD_HASH_ATTR, Digests.sha256Hex(masterUserPassword));
+ }
if (dbName != null && !dbName.isBlank()) {
- r.getAttributes().put("Floci::DBName", dbName);
+ r.getAttributes().put(DB_NAME_ATTR, dbName);
}
if (cluster.getEndpoint() != null) {
r.getAttributes().put("Endpoint.Address", cluster.getEndpoint().getAddress());
@@ -162,10 +230,21 @@ public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
}
if (cluster.getMasterPasswordSecretArn() != null) {
r.getAttributes().put("MasterPasswordSecretArn", cluster.getMasterPasswordSecretArn());
+ } else {
+ r.getAttributes().remove("MasterPasswordSecretArn");
}
r.getAttributes().put("ClusterNamespaceArn", namespaceArn(ctx, id));
}
+ private void cleanupRestoredCluster(String id, RuntimeException failure) {
+ try {
+ redshiftService.deleteCluster(id);
+ } catch (RuntimeException cleanupFailure) {
+ failure.addSuppressed(cleanupFailure);
+ LOG.warnv(cleanupFailure, "Failed to clean up restored cluster {0}", id);
+ }
+ }
+
private Integer numberOfNodes(JsonNode props, ProvisionContext ctx) {
String raw = ctx.resolveOptional(props, "NumberOfNodes");
return raw == null || raw.isBlank() ? null : Integer.valueOf(raw);
@@ -186,7 +265,8 @@ public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
if (ctx.resolveOptional(props, "Port") != null) {
LOG.warnv("Cluster {0}: Port ignored, Floci assigns the host proxy port", id);
}
- if (!ctx.isUpdate() && ctx.resolveOptional(props, "ClusterParameterGroupName") != null) {
+ if (!ctx.isUpdate() && ctx.resolveOptional(props, "SnapshotIdentifier") == null
+ && ctx.resolveOptional(props, "ClusterParameterGroupName") != null) {
LOG.warnv("Cluster {0}: ClusterParameterGroupName is only applied on update", id);
}
}
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 351352730f..3f135255fa 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
@@ -335,27 +335,42 @@ public class RedshiftService {
String identifier, String nodeType, String username, String clusterSubnetGroupName,
List<String> vpcSecurityGroupIds, List<String> iamRoleArns,
String kmsKeyId, String region, String defaultIamRoleArn) {
- if (secretsManagerService == null) {
- throw new AwsException("InternalFailure", "Secrets Manager is unavailable", 500);
- }
+ requireSecretsManager();
String password = RandomPasswordGenerator.generate(objectMapper.createObjectNode());
Cluster cluster = createCluster(identifier, nodeType, username, password,
clusterSubnetGroupName, vpcSecurityGroupIds, iamRoleArns, defaultIamRoleArn);
- String secretName = "redshift/" + identifier;
- String secretString = managedMasterSecret(cluster, password);
- Secret secret;
try {
- secret = secretsManagerService.createSecret(secretName, secretString, null,
- "Managed master user secret for Redshift cluster " + identifier,
- kmsKeyId, List.of(), "redshift", region);
+ return createManagedMasterSecret(cluster, kmsKeyId, region);
} catch (RuntimeException e) {
rollbackManagedMasterPasswordCluster(cluster);
throw e;
}
+ }
+
+ private void requireSecretsManager() {
+ if (secretsManagerService == null) {
+ throw new AwsException("InternalFailure", "Secrets Manager is unavailable", 500);
+ }
+ }
+
+ private void storeCluster(Cluster cluster) {
+ mutate(() -> {
+ clusters.put(cluster.getClusterIdentifier(), cluster);
+ clusters.flush();
+ return null;
+ });
+ }
+
+ private Cluster createManagedMasterSecret(Cluster cluster, String kmsKeyId, String region) {
+ String identifier = cluster.getClusterIdentifier();
+ String secretName = "redshift/" + identifier;
+ String secretString = managedMasterSecret(cluster, cluster.getMasterPassword());
+ Secret secret = secretsManagerService.createSecret(secretName, secretString, null,
+ "Managed master user secret for Redshift cluster " + identifier,
+ kmsKeyId, List.of(), "redshift", region);
cluster.setMasterPasswordSecretArn(secret.getArn());
cluster.setMasterPasswordSecretKmsKeyId(kmsKeyId);
- clusters.put(identifier, cluster);
- clusters.flush();
+ storeCluster(cluster);
return cluster;
}
@@ -1199,6 +1214,56 @@ public class RedshiftService {
() -> mutate(() -> restoreSnapshot(clusterIdentifier, snapshotIdentifier, nodeType))));
}
+ public Cluster restoreFromClusterSnapshot(String clusterIdentifier, String snapshotIdentifier, String nodeType,
+ String clusterSubnetGroupName, boolean manageMasterPassword,
+ String kmsKeyId, String region) {
+ if (manageMasterPassword) {
+ requireSecretsManager();
+ }
+ return withResourceLock("cluster", clusterIdentifier, () -> withResourceLock("snapshot", snapshotIdentifier,
+ () -> {
+ Cluster cluster = restoreSnapshot(clusterIdentifier, snapshotIdentifier, nodeType);
+ try {
+ cluster.setClusterSubnetGroupName(clusterSubnetGroupName);
+ if (manageMasterPassword) {
+ String password = RandomPasswordGenerator.generate(objectMapper.createObjectNode());
+ modifyCluster(clusterIdentifier, null, null, password, null, List.of());
+ return createManagedMasterSecret(cluster, kmsKeyId, region);
+ }
+ storeCluster(cluster);
+ return cluster;
+ } catch (RuntimeException failure) {
+ try {
+ deleteCluster(clusterIdentifier);
+ } catch (RuntimeException cleanupFailure) {
+ failure.addSuppressed(cleanupFailure);
+ LOG.warnv(cleanupFailure, "Failed to clean up restored cluster {0}", clusterIdentifier);
+ }
+ throw failure;
+ }
+ }));
+ }
+
+ private void rollbackSnapshotRestore(Cluster cluster, int proxyPort) {
+ String identifier = cluster.getClusterIdentifier();
+ boolean proxyStopped = stopProxyAndReleasePortSafely(identifier, proxyPort);
+ try {
+ containerManager.stop(clusters.accountId(), identifier);
+ } catch (Exception cleanupFailure) {
+ LOG.warnv(cleanupFailure, "Failed to stop container during rollback of cluster {0}", identifier);
+ }
+ mutate(() -> {
+ if (proxyStopped) {
+ clusters.delete(identifier);
+ } else {
+ cluster.setClusterStatus("failed");
+ clusters.put(identifier, cluster);
+ }
+ clusters.flush();
+ return null;
+ });
+ }
+
private Cluster restoreSnapshot(String clusterIdentifier, String snapshotIdentifier, String nodeType) {
if (clusters.get(clusterIdentifier).isPresent()) {
throw new AwsException("ClusterAlreadyExists", "Cluster " + clusterIdentifier + " already exists", 400);
@@ -1238,8 +1303,7 @@ public class RedshiftService {
cluster.setMasterPassword(password);
cluster.setClusterParameterGroupName(DEFAULT_PARAMETER_GROUP_NAME);
cluster.setClusterStatus("creating");
- clusters.put(clusterIdentifier, cluster);
- clusters.flush();
+ storeCluster(cluster);
// Hoisted out of the try so a failure after allocateProxyPort() still returns the port.
int proxyPort = -1;
@@ -1263,31 +1327,14 @@ public class RedshiftService {
cluster.setClusterStatus("available");
} catch (AwsException e) {
- boolean proxyStopped = stopProxyAndReleasePortSafely(clusterIdentifier, proxyPort);
- try { containerManager.stop(clusters.accountId(), clusterIdentifier); } catch (Exception ex) { LOG.warnv(ex, "Failed to stop container during rollback of cluster {0}", clusterIdentifier); }
- if (proxyStopped) {
- clusters.delete(clusterIdentifier);
- } else {
- cluster.setClusterStatus("failed");
- clusters.put(clusterIdentifier, cluster);
- }
- clusters.flush();
+ rollbackSnapshotRestore(cluster, proxyPort);
throw e;
} catch (Exception e) {
- boolean proxyStopped = stopProxyAndReleasePortSafely(clusterIdentifier, proxyPort);
- try { containerManager.stop(clusters.accountId(), clusterIdentifier); } catch (Exception ex) { LOG.warnv(ex, "Failed to stop container during rollback of cluster {0}", clusterIdentifier); }
- if (proxyStopped) {
- clusters.delete(clusterIdentifier);
- } else {
- cluster.setClusterStatus("failed");
- clusters.put(clusterIdentifier, cluster);
- }
- clusters.flush();
+ rollbackSnapshotRestore(cluster, proxyPort);
throw new AwsException("InternalFailure", "Failed to restore cluster from snapshot: " + e.getMessage(), 500);
}
- clusters.put(clusterIdentifier, cluster);
- clusters.flush();
+ storeCluster(cluster);
return cluster;
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftClusterCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftClusterCfnIntegrationTest.java
index c30685929d..a4f49149f4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftClusterCfnIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftClusterCfnIntegrationTest.java
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.testing.CfnStackWaits;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.specification.RequestSpecification;
import org.awaitility.Awaitility;
@@ -12,7 +13,13 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.redshift.RedshiftClient;
+import software.amazon.awssdk.services.redshift.model.Endpoint;
+import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
@@ -47,6 +54,9 @@ class RedshiftClusterCfnIntegrationTest {
private final List<String> stacksToCleanup = new ArrayList<>();
+ @TestHTTPResource
+ URI endpoint;
+
@BeforeAll
static void configure() {
Assumptions.assumeTrue(dockerAvailable(), "Docker is required for this integration test");
@@ -76,6 +86,85 @@ class RedshiftClusterCfnIntegrationTest {
stacksToCleanup.clear();
}
+ @Test
+ void snapshotStackRestoresRowsAfterSourceClusterWasDeleted() throws Exception {
+ String stackName = "rs-restore-it-" + System.currentTimeMillis();
+ String source = stackName + "-source";
+ String target = stackName + "-target";
+ String snapshot = stackName + "-backup";
+ stacksToCleanup.add(stackName);
+ try (RedshiftClient client = RedshiftClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))).build()) {
+ client.createCluster(request -> request.clusterIdentifier(source).nodeType("ra3.large")
+ .masterUsername("admin").masterUserPassword("Snapshot123"));
+ try {
+ Endpoint sourceEndpoint = client.describeClusters(
+ request -> request.clusterIdentifier(source)).clusters().getFirst().endpoint();
+ String sourceHost = "host.docker.internal".equalsIgnoreCase(sourceEndpoint.address())
+ ? "127.0.0.1" : sourceEndpoint.address();
+ try (Connection connection = waitForConnection(sourceHost, sourceEndpoint.port(), "admin", "Snapshot123");
+ Statement statement = connection.createStatement()) {
+ statement.execute("CREATE TABLE snapshot_rows (id integer)");
+ statement.execute("INSERT INTO snapshot_rows VALUES (42)");
+ }
+ client.createClusterSnapshot(request -> request.clusterIdentifier(source).snapshotIdentifier(snapshot));
+ client.deleteCluster(request -> request.clusterIdentifier(source).skipFinalClusterSnapshot(true));
+ String template = """
+ {"Resources":{"Warehouse":{"Type":"AWS::Redshift::Cluster","Properties":{
+ "ClusterIdentifier":"%s","NodeType":"ra3.large","MasterUsername":"admin",
+ "DBName":"dev","ClusterType":"single-node","SnapshotIdentifier":"%s"}}},
+ "Outputs":{
+ "Ref":{"Value":{"Ref":"Warehouse"}},
+ "Addr":{"Value":{"Fn::GetAtt":["Warehouse","Endpoint.Address"]}},
+ "Port":{"Value":{"Fn::GetAtt":["Warehouse","Endpoint.Port"]}},
+ "Ns":{"Value":{"Fn::GetAtt":["Warehouse","ClusterNamespaceArn"]}}}}
+ """.formatted(target, snapshot);
+ cloudFormation(stackName, "CreateStack", template);
+ String stacks = describeStacks(stackName, "CREATE_COMPLETE");
+ assertEquals(target, outputValue(stacks, "Ref"));
+ assertTrue(outputValue(stacks, "Ns").contains(":namespace:"));
+ String address = outputValue(stacks, "Addr");
+ String host = "host.docker.internal".equalsIgnoreCase(address) ? "127.0.0.1" : address;
+ try (Connection connection = waitForConnection(host, Integer.parseInt(outputValue(stacks, "Port")),
+ "admin", "Snapshot123");
+ Statement statement = connection.createStatement();
+ ResultSet rows = statement.executeQuery("SELECT id FROM snapshot_rows")) {
+ assertTrue(rows.next());
+ assertEquals(42, rows.getInt(1));
+ assertFalse(rows.next());
+ }
+ try (Connection connection = waitForConnection(host, Integer.parseInt(outputValue(stacks, "Port")),
+ "admin", "Snapshot123");
+ Statement statement = connection.createStatement()) {
+ statement.execute("INSERT INTO snapshot_rows VALUES (99)");
+ }
+ cloudFormation(stackName, "UpdateStack", template.replace("\"NodeType\":\"ra3.large\"",
+ "\"NodeType\":\"ra3.xlplus\""));
+ String updated = describeStacks(stackName, "UPDATE_COMPLETE");
+ assertEquals(outputValue(stacks, "Addr"), outputValue(updated, "Addr"));
+ assertEquals(outputValue(stacks, "Port"), outputValue(updated, "Port"));
+ try (Connection connection = waitForConnection(host, Integer.parseInt(outputValue(updated, "Port")),
+ "admin", "Snapshot123");
+ Statement statement = connection.createStatement();
+ ResultSet rows = statement.executeQuery("SELECT id FROM snapshot_rows ORDER BY id")) {
+ assertTrue(rows.next());
+ assertEquals(42, rows.getInt(1));
+ assertTrue(rows.next());
+ assertEquals(99, rows.getInt(1));
+ assertFalse(rows.next());
+ }
+ } finally {
+ cloudFormation(stackName, "DeleteStack", null);
+ CfnStackWaits.awaitStackDeleted(stackName);
+ stacksToCleanup.remove(stackName);
+ client.deleteClusterSnapshot(request -> request.snapshotIdentifier(snapshot));
+ if (client.describeClusters().clusters().stream().anyMatch(cluster -> cluster.clusterIdentifier().equals(source))) {
+ client.deleteCluster(request -> request.clusterIdentifier(source).skipFinalClusterSnapshot(true));
+ }
+ }
+ }
+ }
+
@Test
void createStackProvisionsRealClusterWithEndpointOutputs() throws Exception {
String stackName = "rs-cfn-it-" + System.currentTimeMillis();
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftSnapshotCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftSnapshotCfnIntegrationTest.java
new file mode 100644
index 0000000000..a90bd78ba4
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftSnapshotCfnIntegrationTest.java
@@ -0,0 +1,149 @@
+package io.github.hectorvent.floci.services.cloudformation;
+
+import io.github.hectorvent.floci.core.common.XmlParser;
+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.proxy.RedshiftProxyManager;
+import io.github.hectorvent.floci.testing.CfnStackWaits;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.InjectMock;
+import io.quarkus.test.common.http.TestHTTPResource;
+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 software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.redshift.RedshiftClient;
+import software.amazon.awssdk.services.redshift.model.Cluster;
+import software.amazon.awssdk.services.redshift.model.RedshiftException;
+
+import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static io.restassured.RestAssured.given;
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
+
+@QuarkusTest
+class RedshiftSnapshotCfnIntegrationTest {
+
+ private static final String AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20261024/us-east-1/cloudformation/aws4_request";
+ private static final String DUMP = "CREATE TABLE preserved (id integer); INSERT INTO preserved VALUES (42);";
+
+ @InjectMock
+ RedshiftContainerManager containers;
+
+ @InjectMock
+ RedshiftProxyManager proxies;
+
+ @TestHTTPResource
+ URI endpoint;
+
+ @BeforeAll
+ static void configure() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @BeforeEach
+ void prepareRuntime() {
+ when(containers.start(anyString(), anyString(), anyString(), anyString()))
+ .thenAnswer(invocation -> new RedshiftContainerHandle("mock-container",
+ invocation.getArgument(1), "localhost", 5432));
+ doAnswer(invocation -> {
+ Files.writeString(invocation.getArgument(3, Path.class), DUMP);
+ return null;
+ }).when(containers).takeSnapshot(anyString(), anyString(), anyString(), any(Path.class));
+ }
+
+ @Test
+ void snapshotStackRestoresDumpAndUnchangedUpdateKeepsCluster() {
+ String stack = "rs-snapshot-cfn-it";
+ String source = "rs-snapshot-source";
+ String target = "rs-snapshot-target";
+ String snapshot = "rs-snapshot-backup";
+ AtomicReference<String> restoredDump = new AtomicReference<>();
+ doAnswer(invocation -> {
+ restoredDump.set(Files.readString(invocation.getArgument(3, Path.class)));
+ return null;
+ }).when(containers).restoreSnapshot(anyString(), eq(target), eq("admin"), any(Path.class));
+
+ try (RedshiftClient client = client()) {
+ client.createCluster(request -> request.clusterIdentifier(source).nodeType("ra3.large")
+ .masterUsername("admin").masterUserPassword("Original123"));
+ try {
+ client.createClusterSnapshot(request -> request.clusterIdentifier(source).snapshotIdentifier(snapshot));
+ client.deleteCluster(request -> request.clusterIdentifier(source).skipFinalClusterSnapshot(true));
+ createOrUpdateStack(stack, "CreateStack", template(target, snapshot, "ra3.large"));
+ assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, AUTH).status());
+
+ Cluster cluster = client.describeClusters(request -> request.clusterIdentifier(target)).clusters().getFirst();
+ String xml = given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
+ .formParam("Action", "DescribeStacks").formParam("StackName", stack)
+ .post("/").then().statusCode(200).extract().asString();
+ Map<String, String> outputs = XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue");
+ assertEquals(target, outputs.get("Ref"));
+ assertEquals(cluster.endpoint().address(), outputs.get("Address"));
+ assertEquals(String.valueOf(cluster.endpoint().port()), outputs.get("Port"));
+ assertTrue(outputs.get("Namespace").contains(":namespace:"));
+ assertEquals(DUMP, restoredDump.get());
+ assertEquals("admin", cluster.masterUsername());
+
+ createOrUpdateStack(stack, "UpdateStack", template(target, snapshot, "ra3.xlplus"));
+ assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, AUTH).status());
+ assertEquals(cluster.endpoint(), client.describeClusters(request -> request.clusterIdentifier(target))
+ .clusters().getFirst().endpoint());
+ verify(containers).restoreSnapshot(anyString(), eq(target), eq("admin"), any(Path.class));
+ } finally {
+ given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
+ .formParam("Action", "DeleteStack").formParam("StackName", stack).post("/")
+ .then().statusCode(200);
+ CfnStackWaits.awaitStackDeleted(stack);
+ deleteIfPresent(client, source);
+ deleteIfPresent(client, target);
+ client.deleteClusterSnapshot(request -> request.snapshotIdentifier(snapshot));
+ }
+ }
+ }
+
+ private RedshiftClient client() {
+ return RedshiftClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))).build();
+ }
+
+ private static void deleteIfPresent(RedshiftClient client, String identifier) {
+ try {
+ client.deleteCluster(request -> request.clusterIdentifier(identifier).skipFinalClusterSnapshot(true));
+ } catch (RedshiftException expected) {
+ // Stack cleanup may already have deleted the cluster.
+ if (!"ClusterNotFound".equals(expected.awsErrorDetails().errorCode())) {
+ throw expected;
+ }
+ }
+ }
+
+ private static void createOrUpdateStack(String stack, String action, String template) {
+ given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
+ .formParam("Action", action).formParam("StackName", stack).formParam("TemplateBody", template)
+ .post("/").then().statusCode(200);
+ }
+
+ private static String template(String identifier, String snapshot, String nodeType) {
+ return """
+ {"Resources":{"Warehouse":{"Type":"AWS::Redshift::Cluster","Properties":{
+ "ClusterIdentifier":"%s","NodeType":"%s","DBName":"dev","ClusterType":"single-node",
+ "MasterUsername":"admin","SnapshotIdentifier":"%s"}}},
+ "Outputs":{
+ "Ref":{"Value":{"Ref":"Warehouse"}},
+ "Address":{"Value":{"Fn::GetAtt":["Warehouse","Endpoint.Address"]}},
+ "Port":{"Value":{"Fn::GetAtt":["Warehouse","Endpoint.Port"]}},
+ "Namespace":{"Value":{"Fn::GetAtt":["Warehouse","ClusterNamespaceArn"]}}}}
+ """.formatted(identifier, nodeType, snapshot);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftClusterCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftClusterCfnProvisionerTest.java
index d0dfb5ba28..d4e247d24e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftClusterCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RedshiftClusterCfnProvisionerTest.java
@@ -11,6 +11,8 @@ import io.github.hectorvent.floci.services.redshift.model.ClusterParameterGroup;
import io.github.hectorvent.floci.services.redshift.model.Endpoint;
import io.github.hectorvent.floci.services.redshift.model.Parameter;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import java.util.ArrayList;
@@ -318,6 +320,269 @@ class RedshiftClusterCfnProvisionerTest {
assertDoesNotThrow(() -> p.delete("AWS::Redshift::ClusterSecurityGroup", r.getPhysicalId(), "us-east-1"));
}
+ @Test
+ void snapshotPrivateAttributesUseHiddenKeys() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.restoreFromClusterSnapshot("restored", "backup", "ra3.large", null, false, null, "us-east-1"))
+ .thenReturn(availableCluster("restored"));
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+
+ provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","SnapshotIdentifier":"backup","NodeType":"ra3.large",
+ "MasterUsername":"admin","MasterUserPassword":"Template123","DBName":"dev"}"""), ctx(null));
+
+ assertEquals("restored", resource.getAttributes().get("__FlociUsesRedshiftClusterIdentifier"));
+ assertEquals("backup", resource.getAttributes().get("__FlociUsesRedshiftSnapshotIdentifier"));
+ assertEquals("admin", resource.getAttributes().get("__FlociUsesRedshiftRestoreMasterUsername"));
+ assertEquals("dev", resource.getAttributes().get("__FlociUsesRedshiftDBName"));
+ assertNotNull(resource.getAttributes().get("__FlociUsesRedshiftMasterUserPasswordHash"));
+ assertFalse(resource.getAttributes().containsValue("Template123"));
+ assertTrue(resource.getAttributes().keySet().stream().noneMatch(key -> key.startsWith("Floci::")));
+ }
+
+ @Test
+ void snapshotCreateRestoresWithoutMasterPasswordAndSetsReferences() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.restoreFromClusterSnapshot("restored", "backup", "ra3.large", null, false, null, "us-east-1"))
+ .thenReturn(availableCluster("restored"));
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+
+ provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","SnapshotIdentifier":"backup",
+ "Tags":[{"Key":"env","Value":"restore"}]}"""), ctx(null));
+
+ assertEquals("restored", resource.getPhysicalId());
+ assertEquals("host-restored", resource.getAttributes().get("Endpoint.Address"));
+ assertEquals("5439", resource.getAttributes().get("Endpoint.Port"));
+ assertTrue(resource.getAttributes().get("ClusterNamespaceArn").contains(":namespace:"));
+ verify(service).createTags("restored", Map.of("env", "restore"));
+ verify(service, never()).createCluster(any(), any(), any(), any(), any(), anyList());
+ }
+
+ @Test
+ void unchangedSnapshotUpdatesWithoutRestoringAgain() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.restoreFromClusterSnapshot("restored", "backup", "ra3.large", null, false, null, "us-east-1"))
+ .thenReturn(availableCluster("restored"));
+ when(service.modifyCluster(eq("restored"), eq("ra3.large"), isNull(), isNull(), isNull(), anyList()))
+ .thenReturn(availableCluster("restored"));
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+ JsonNode props = json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","SnapshotIdentifier":"backup"}""");
+ provisioner.provision(resource, props, ctx(null));
+ provisioner.provision(resource, props, ctx("restored"));
+
+ verify(service).restoreFromClusterSnapshot("restored", "backup", "ra3.large", null, false, null, "us-east-1");
+ assertEquals("restored", resource.getPhysicalId());
+ assertFalse(provisioner.hasReplacementUpdate(resource));
+ }
+
+ @Test
+ void changedSnapshotReplacesAndRollbackRestoresPriorSnapshot() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.restoreFromClusterSnapshot(anyString(), anyString(), eq("ra3.large"), isNull(), eq(false), isNull(), eq("us-east-1")))
+ .thenAnswer(invocation -> availableCluster(invocation.getArgument(0)));
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+ JsonNode original = json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","SnapshotIdentifier":"backup",
+ "MasterUsername":"admin","MasterUserPassword":"Original123","DBName":"dev"}""");
+ provisioner.provision(resource, original, ctx(null));
+ resource.getAttributes().put("MasterPasswordSecretArn", "old-managed-secret");
+ String originalPasswordHash = resource.getAttributes().get("__FlociUsesRedshiftMasterUserPasswordHash");
+ provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","SnapshotIdentifier":"new-backup",
+ "MasterUsername":"other","MasterUserPassword":"Replacement123","DBName":"analytics"}"""),
+ ctx("restored"));
+
+ String replacement = resource.getPhysicalId();
+ assertNotEquals("restored", replacement);
+ assertFalse(resource.getAttributes().containsKey("MasterPasswordSecretArn"));
+ assertEquals("restored", provisioner.updateCleanupPhysicalId(resource));
+ verify(service).restoreFromClusterSnapshot(replacement, "new-backup", "ra3.large", null, false, null, "us-east-1");
+ assertTrue(provisioner.rollbackUpdate(resource));
+ assertEquals("restored", resource.getPhysicalId());
+ assertEquals("backup", resource.getAttributes().get("__FlociUsesRedshiftSnapshotIdentifier"));
+ assertEquals("admin", resource.getAttributes().get("__FlociUsesRedshiftRestoreMasterUsername"));
+ assertEquals("dev", resource.getAttributes().get("__FlociUsesRedshiftDBName"));
+ assertEquals(originalPasswordHash, resource.getAttributes().get("__FlociUsesRedshiftMasterUserPasswordHash"));
+ assertFalse(resource.getAttributes().containsKey("__FlociRedshiftPriorAttributes"));
+ verify(service).deleteCluster(replacement);
+ when(service.modifyCluster(eq("restored"), eq("ra3.large"), isNull(), isNull(), isNull(), anyList()))
+ .thenReturn(availableCluster("restored"));
+ provisioner.provision(resource, original, ctx("restored"));
+ assertEquals("restored", resource.getPhysicalId());
+ assertFalse(provisioner.hasReplacementUpdate(resource));
+ }
+
+ @Test
+ void missingSnapshotPropagatesWithoutAssigningPhysicalId() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.restoreFromClusterSnapshot("restored", "missing", "ra3.large", null, false, null, "us-east-1"))
+ .thenThrow(new AwsException("ClusterSnapshotNotFound", "Snapshot missing not found", 404));
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+
+ AwsException error = assertThrows(AwsException.class, () -> provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","SnapshotIdentifier":"missing"}"""), ctx(null)));
+
+ assertEquals("ClusterSnapshotNotFound", error.getErrorCode());
+ assertNull(resource.getPhysicalId());
+ }
+
+ @Test
+ void snapshotCreateForwardsManagedPasswordAndSubnetSettings() {
+ RedshiftService service = mock(RedshiftService.class);
+ Cluster restored = availableCluster("restored");
+ restored.setMasterPasswordSecretArn("secret-arn");
+ when(service.restoreFromClusterSnapshot("restored", "backup", "ra3.large", "restore-subnets",
+ true, "alias/restore", "us-east-1")).thenReturn(restored);
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+
+ provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","SnapshotIdentifier":"backup","NodeType":"ra3.large",
+ "ClusterSubnetGroupName":"restore-subnets","ManageMasterPassword":true,
+ "MasterPasswordSecretKmsKeyId":"alias/restore"}"""), ctx(null));
+
+ assertEquals("restored", resource.getPhysicalId());
+ assertEquals("secret-arn", resource.getAttributes().get("MasterPasswordSecretArn"));
+ }
+
+ @Test
+ void snapshotCreateCleansUpWhenConfigurationFails() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.restoreFromClusterSnapshot("restored", "backup", "ra3.large", null, false, null, "us-east-1"))
+ .thenReturn(availableCluster("restored"));
+ when(service.modifyCluster(eq("restored"), eq("ra3.large"), isNull(), isNull(), eq("params"), anyList()))
+ .thenThrow(new AwsException("InternalFailure", "storage failed", 500));
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+
+ assertThrows(AwsException.class, () -> provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","SnapshotIdentifier":"backup","NodeType":"ra3.large",
+ "ClusterParameterGroupName":"params"}"""), ctx(null)));
+
+ verify(service).deleteCluster("restored");
+ assertNull(resource.getPhysicalId());
+ }
+
+ @Test
+ void blankSnapshotIsRejectedBeforeCreatingACluster() {
+ RedshiftService service = mock(RedshiftService.class);
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+
+ AwsException error = assertThrows(AwsException.class, () -> provisioner.provision(resource,
+ json("{\"SnapshotIdentifier\":\" \"}"), ctx(null)));
+
+ assertEquals("ValidationException", error.getErrorCode());
+ verifyNoInteractions(service);
+ }
+
+ @Test
+ void unchangedTemplateAfterSnapshotReplacementKeepsTheReplacement() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.restoreFromClusterSnapshot(anyString(), anyString(), anyString(), isNull(),
+ eq(false), isNull(), eq("us-east-1")))
+ .thenAnswer(invocation -> availableCluster(invocation.getArgument(0)));
+ when(service.modifyCluster(anyString(), eq("ra3.large"), isNull(), isNull(), isNull(), anyList()))
+ .thenAnswer(invocation -> availableCluster(invocation.getArgument(0)));
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+ provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","SnapshotIdentifier":"backup"}"""), ctx(null));
+ JsonNode changed = json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","SnapshotIdentifier":"new-backup"}""");
+ provisioner.provision(resource, changed, ctx("restored"));
+ String replacement = resource.getPhysicalId();
+ provisioner.completeUpdate(resource);
+ provisioner.clearUpdate(resource);
+
+ provisioner.provision(resource, changed, ctx(replacement));
+
+ assertEquals(replacement, resource.getPhysicalId());
+ verify(service).modifyCluster(eq(replacement), eq("ra3.large"), isNull(), isNull(), isNull(), anyList());
+ }
+
+ @Test
+ void removingSnapshotReplacesWithFreshCluster() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.createCluster(anyString(), eq("ra3.large"), eq("admin"), eq("Secret123"), isNull(), anyList()))
+ .thenAnswer(invocation -> availableCluster(invocation.getArgument(0)));
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+ resource.setPhysicalId("restored");
+ resource.getAttributes().put("__FlociUsesRedshiftSnapshotIdentifier", "backup");
+
+ provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","MasterUsername":"admin",
+ "MasterUserPassword":"Secret123"}"""), ctx("restored"));
+
+ assertNotEquals("restored", resource.getPhysicalId());
+ assertEquals("restored", provisioner.updateCleanupPhysicalId(resource));
+ assertFalse(resource.getAttributes().containsKey("__FlociUsesRedshiftSnapshotIdentifier"));
+ }
+
+ @Test
+ void snapshotCreateCleansUpWhenTaggingFails() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.restoreFromClusterSnapshot("restored", "backup", "ra3.large", null, false, null, "us-east-1"))
+ .thenReturn(availableCluster("restored"));
+ doThrow(new AwsException("InternalFailure", "tag storage failed", 500))
+ .when(service).createTags("restored", Map.of("env", "restore"));
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+
+ assertThrows(AwsException.class, () -> provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","SnapshotIdentifier":"backup","NodeType":"ra3.large",
+ "Tags":[{"Key":"env","Value":"restore"}]}"""), ctx(null)));
+
+ verify(service).deleteCluster("restored");
+ assertNull(resource.getPhysicalId());
+ }
+
+ @Test
+ void managedSnapshotCreateRejectsAnExplicitPassword() {
+ RedshiftService service = mock(RedshiftService.class);
+ RedshiftClusterCfnProvisioner provisioner = new RedshiftClusterCfnProvisioner(service);
+ StackResource resource = new StackResource();
+ resource.setResourceType("AWS::Redshift::Cluster");
+ resource.setLogicalId("Warehouse");
+
+ AwsException error = assertThrows(AwsException.class, () -> provisioner.provision(resource, json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","SnapshotIdentifier":"backup",
+ "ManageMasterPassword":true,"MasterUserPassword":"Secret123"}"""), ctx(null)));
+
+ assertEquals("ValidationException", error.getErrorCode());
+ verifyNoInteractions(service);
+ }
+
@Test
void provisionCreatesClusterAndSetsRefAndEndpointAttributes() {
RedshiftService service = mock(RedshiftService.class);
@@ -590,21 +855,25 @@ class RedshiftClusterCfnProvisionerTest {
assertTrue(p.hasReplacementUpdate(r));
}
- @Test
- void changingDBNameTriggersReplacement() {
+ @ParameterizedTest
+ @ValueSource(strings = {"__FlociUsesRedshiftDBName", "Floci::DBName"})
+ void changingDBNameTriggersReplacement(String dbNameKey) {
RedshiftService service = mock(RedshiftService.class);
Cluster existing = availableCluster("my-cluster");
existing.setMasterUsername("admin");
when(service.describeClusters("my-cluster")).thenReturn(List.of(existing));
when(service.createCluster(anyString(), anyString(), anyString(), anyString(), any(), anyList()))
.thenAnswer(inv -> availableCluster(inv.getArgument(0)));
+ when(service.modifyCluster(eq("my-cluster"), any(), any(), any(), any(), anyList()))
+ .thenReturn(availableCluster("my-cluster"));
RedshiftClusterCfnProvisioner p = new RedshiftClusterCfnProvisioner(service);
StackResource r = new StackResource();
r.setResourceType("AWS::Redshift::Cluster");
r.setLogicalId("W");
r.setPhysicalId("my-cluster");
- r.getAttributes().put("Floci::DBName", "dev");
+ r.getAttributes().put("Floci::DBName", "analytics");
+ r.getAttributes().put(dbNameKey, "dev");
p.provision(r, json("""
{"ClusterIdentifier":"my-cluster","NodeType":"ra3.large","MasterUsername":"admin",
@@ -613,6 +882,41 @@ class RedshiftClusterCfnProvisionerTest {
assertNotEquals("my-cluster", r.getPhysicalId());
assertEquals("my-cluster", p.updateCleanupPhysicalId(r));
assertTrue(p.hasReplacementUpdate(r));
- assertEquals("analytics", r.getAttributes().get("Floci::DBName"));
+ assertEquals("analytics", r.getAttributes().get("__FlociUsesRedshiftDBName"));
+ }
+
+ @Test
+ void unrelatedUpdateOnSnapshotRestoredClusterPreservesInheritedPassword() {
+ RedshiftService service = mock(RedshiftService.class);
+ when(service.restoreFromClusterSnapshot("restored", "backup", "ra3.large", null, false, null, "us-east-1"))
+ .thenReturn(availableCluster("restored"));
+ when(service.modifyCluster(eq("restored"), eq("ra3.xlplus"), isNull(), isNull(), isNull(), anyList()))
+ .thenReturn(availableCluster("restored"));
+ RedshiftClusterCfnProvisioner p = new RedshiftClusterCfnProvisioner(service);
+
+ StackResource r = new StackResource();
+ r.setResourceType("AWS::Redshift::Cluster");
+ r.setLogicalId("W");
+ // Initial restore with template password "TemplatePass1" (deliberately ignored during snapshot restore)
+ p.provision(r, json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.large","SnapshotIdentifier":"backup",
+ "MasterUserPassword":"TemplatePass1"}"""), ctx(null));
+
+ // Now an unrelated update: NodeType changes to ra3.xlplus, MasterUserPassword unchanged
+ p.provision(r, json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.xlplus","SnapshotIdentifier":"backup",
+ "MasterUserPassword":"TemplatePass1"}"""), ctx("restored"));
+
+ // Password passed to modifyCluster must be null, preserving inherited password
+ verify(service).modifyCluster(eq("restored"), eq("ra3.xlplus"), isNull(), isNull(), isNull(), anyList());
+
+ // Now explicitly updating MasterUserPassword to a new password
+ when(service.modifyCluster(eq("restored"), eq("ra3.xlplus"), isNull(), eq("BrandNewPass2"), isNull(), anyList()))
+ .thenReturn(availableCluster("restored"));
+ p.provision(r, json("""
+ {"ClusterIdentifier":"restored","NodeType":"ra3.xlplus","SnapshotIdentifier":"backup",
+ "MasterUserPassword":"BrandNewPass2"}"""), ctx("restored"));
+
+ verify(service).modifyCluster(eq("restored"), eq("ra3.xlplus"), isNull(), eq("BrandNewPass2"), isNull(), anyList());
}
}
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 d354912097..971f0742e6 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
@@ -59,6 +59,7 @@ import java.util.concurrent.atomic.AtomicReference;
import static io.github.hectorvent.floci.testing.InlineMocks.inlineMock;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -1567,6 +1568,67 @@ class RedshiftServiceTest {
assertThrows(AwsException.class, () -> service.deleteSnapshot("missing-snap"));
}
+ @Test
+ void snapshotRestoreCanManagePasswordAndStoreSubnetGroup() throws Exception {
+ useStatefulClusterStorage();
+ Snapshot snapshot = new Snapshot("managed-backup", "deleted-source", "available", 5439, "admin");
+ snapshot.setMasterPassword("Original123");
+ snapshot.setSqlDump(dumpPath("managed-backup"));
+ when(snapshotBackend.get("managed-backup")).thenReturn(Optional.of(snapshot));
+ when(cm.start("111111111111", "restored-managed", "admin", "Original123"))
+ .thenReturn(new RedshiftContainerHandle("restored", "restored-managed", "localhost", 5432));
+ Secret secret = new Secret();
+ secret.setArn("arn:aws:secretsmanager:us-east-1:111111111111:secret:restored-managed");
+ when(secretsManagerService.createSecret(eq("redshift/restored-managed"), anyString(), isNull(),
+ anyString(), eq("alias/restore-key"), anyList(), eq("redshift"), eq("us-east-1"))).thenReturn(secret);
+
+ Cluster cluster = service.restoreFromClusterSnapshot("restored-managed", "managed-backup", "ra3.large",
+ "restore-subnets", true, "alias/restore-key", "us-east-1");
+
+ assertEquals("restore-subnets", cluster.getClusterSubnetGroupName());
+ assertEquals(secret.getArn(), cluster.getMasterPasswordSecretArn());
+ assertFalse("Original123".equals(cluster.getMasterPassword()));
+ ArgumentCaptor<String> secretValue = ArgumentCaptor.forClass(String.class);
+ verify(secretsManagerService).createSecret(eq("redshift/restored-managed"), secretValue.capture(), isNull(),
+ anyString(), eq("alias/restore-key"), anyList(), eq("redshift"), eq("us-east-1"));
+ assertEquals(cluster.getMasterPassword(), new ObjectMapper().readTree(secretValue.getValue()).path("password").asText());
+ verify(cm).alterUserPassword("111111111111", "restored-managed", "admin", cluster.getMasterPassword());
+ }
+
+ @Test
+ void managedSnapshotRestoreCleansUpWhenSecretCreationFails() {
+ useStatefulClusterStorage();
+ Snapshot snapshot = new Snapshot("managed-backup", "deleted-source", "available", 5439, "admin");
+ snapshot.setMasterPassword("Original123");
+ when(snapshotBackend.get("managed-backup")).thenReturn(Optional.of(snapshot));
+ when(cm.start("111111111111", "restored-managed", "admin", "Original123"))
+ .thenReturn(new RedshiftContainerHandle("restored", "restored-managed", "localhost", 5432));
+ when(secretsManagerService.createSecret(eq("redshift/restored-managed"), anyString(), isNull(),
+ anyString(), isNull(), anyList(), eq("redshift"), eq("us-east-1")))
+ .thenThrow(new AwsException("InternalFailure", "secret storage failed", 500));
+
+ assertThrows(AwsException.class, () -> service.restoreFromClusterSnapshot("restored-managed", "managed-backup",
+ "ra3.large", null, true, null, "us-east-1"));
+
+ verify(cm).stop("111111111111", "restored-managed");
+ assertTrue(service.describeClusters(null).isEmpty());
+ assertTrue(service.getSnapshot("managed-backup").isPresent());
+ }
+
+ private void useStatefulClusterStorage() {
+ Map<String, Cluster> stored = new HashMap<>();
+ when(clusterBackend.get(anyString())).thenAnswer(invocation -> Optional.ofNullable(stored.get(invocation.getArgument(0))));
+ when(clusterBackend.scan(any())).thenAnswer(invocation -> new ArrayList<>(stored.values()));
+ doAnswer(invocation -> {
+ stored.put(invocation.getArgument(0), invocation.getArgument(1));
+ return null;
+ }).when(clusterBackend).put(anyString(), any(Cluster.class));
+ doAnswer(invocation -> {
+ stored.remove(invocation.getArgument(0));
+ return null;
+ }).when(clusterBackend).delete(anyString());
+ }
+
@Test
void restoreFromClusterSnapshot() {
Cluster sourceCluster = new Cluster();