mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): support Redshift snapshot restores (#6395)
* feat(cloudformation): support Redshift snapshot restores * fix(cloudformation): address Redshift snapshot restore review findings - Reconcile ManageMasterPassword and KMS key on in-place cluster update - Avoid overriding inherited snapshot passwords on unrelated template changes - Reconcile tags by deleting removed tags on update - Mint ARN for parameter group tags to prevent invalid parameter errors - Scope mutate locking during snapshot restore to prevent stalling writes * fix(cloudformation): narrow Redshift snapshot restore scope * fix(cloudformation): use shared rollback for Redshift state
This commit is contained in:
1 parent
307380750f
commit
4c70d6b1a2
7 files changed
+808
-50
No files matched your search
+89
@@ -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();
|
||||
|
||||
+149
@@ -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);
|
||||
}
|
||||
}
|
||||
+308
-4
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user