fix(rds): persist DB cluster DeletionProtection and refuse to delete a protected cluster (#8985)

This commit is contained in:
rajp33 authored and GitHub committed 2026-11-19 19:53:59 -04:00
1 parent fca7dfdd22
commit 30503c4c2e
14 files changed
+346 -77

No files matched your search

@@ -8,6 +8,7 @@ import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static io.floci.aws.testing.RdsRequests.createAuroraPostgresCluster;
import static io.floci.aws.testing.RdsRequests.rds;
import static org.hamcrest.Matchers.containsString;
@@ -32,12 +33,7 @@ class RdsClusterBackupRetentionPeriodIntegrationTest {
}
private static Response createCluster(String id, String period) {
RequestSpecification req = rds("CreateDBCluster")
.formParam("DBClusterIdentifier", id)
.formParam("Engine", "aurora-postgresql")
.formParam("EngineVersion", "16.3")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "secret99password");
RequestSpecification req = createAuroraPostgresCluster(id);
if (period != null) {
req = req.formParam("BackupRetentionPeriod", period);
}
@@ -3,10 +3,10 @@ package io.floci.aws.services.rds;
import io.floci.aws.testing.RdsMockProfile;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static io.floci.aws.testing.RdsRequests.createAuroraPostgresCluster;
import static io.floci.aws.testing.RdsRequests.rds;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
@@ -31,7 +31,7 @@ class RdsClusterCreateTagsAndKmsKeyIntegrationTest {
@Test
void tagsGivenAtCreateAreReadableThroughListTagsForResource() {
String arn = createCluster()
String arn = createAuroraPostgresCluster(ID)
.formParam("Tags.Tag.1.Key", "Accounting").formParam("Tags.Tag.1.Value", "gna")
.formParam("Tags.Tag.2.Key", "BusinessSolution").formParam("Tags.Tag.2.Value", "gue")
.when().post("/").then().statusCode(200)
@@ -48,7 +48,7 @@ class RdsClusterCreateTagsAndKmsKeyIntegrationTest {
@Test
void tagsAreAlsoReturnedAsTheTagListOfDescribeDbClusters() {
// The Terraform AWS provider reads a cluster's tags from here, not from ListTagsForResource.
createCluster()
createAuroraPostgresCluster(ID)
.formParam("Tags.Tag.1.Key", "Accounting").formParam("Tags.Tag.1.Value", "gna")
.when().post("/").then().statusCode(200)
.body(containsString("<TagList><Tag><Key>Accounting</Key><Value>gna</Value></Tag></TagList>"));
@@ -60,7 +60,7 @@ class RdsClusterCreateTagsAndKmsKeyIntegrationTest {
@Test
void tagsAreAlsoReadInTheMemberForm() {
String arn = createCluster()
String arn = createAuroraPostgresCluster(ID)
.formParam("Tags.member.1.Key", "team").formParam("Tags.member.1.Value", "platform")
.when().post("/").then().statusCode(200)
.extract().path("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.DBClusterArn");
@@ -73,7 +73,7 @@ class RdsClusterCreateTagsAndKmsKeyIntegrationTest {
@Test
void kmsKeyIdRoundTripsFromCreateToDescribe() {
createCluster()
createAuroraPostgresCluster(ID)
.formParam("StorageEncrypted", "true").formParam("KmsKeyId", KEY_ARN)
.when().post("/").then().statusCode(200)
.body(containsString("<KmsKeyId>" + KEY_ARN + "</KmsKeyId>"));
@@ -86,7 +86,7 @@ class RdsClusterCreateTagsAndKmsKeyIntegrationTest {
@Test
void aClusterCreatedWithoutThemHasNoTagsAndNoKmsKeyId() {
String arn = createCluster().when().post("/").then().statusCode(200)
String arn = createAuroraPostgresCluster(ID).when().post("/").then().statusCode(200)
.body(not(containsString("<KmsKeyId>")))
.extract().path("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.DBClusterArn");
@@ -97,13 +97,4 @@ class RdsClusterCreateTagsAndKmsKeyIntegrationTest {
.when().post("/").then().statusCode(200)
.body(not(containsString("<KmsKeyId>")));
}
private static RequestSpecification createCluster() {
return rds("CreateDBCluster")
.formParam("DBClusterIdentifier", ID)
.formParam("Engine", "aurora-postgresql")
.formParam("EngineVersion", "16.3")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "secret99password");
}
}
@@ -0,0 +1,106 @@
package io.floci.aws.services.rds;
import io.floci.aws.testing.RdsMockProfile;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static io.floci.aws.testing.RdsRequests.createAuroraPostgresCluster;
import static io.floci.aws.testing.RdsRequests.rds;
import static org.hamcrest.Matchers.containsString;
@QuarkusTest
@TestProfile(RdsMockProfile.class)
class RdsClusterDeletionProtectionIntegrationTest {
private static final String ID = "dp-cluster";
@AfterEach
void cleanUp() {
rds("ModifyDBCluster").formParam("DBClusterIdentifier", ID)
.formParam("DeletionProtection", "false").when().post("/");
rds("DeleteDBCluster").formParam("DBClusterIdentifier", ID)
.formParam("SkipFinalSnapshot", "true").when().post("/");
}
private static Response createCluster(String deletionProtection) {
RequestSpecification req = createAuroraPostgresCluster(ID);
if (deletionProtection != null) {
req = req.formParam("DeletionProtection", deletionProtection);
}
return req.when().post("/");
}
private static void describeReports(String expected) {
rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID)
.when().post("/").then().statusCode(200)
.body(containsString("<DeletionProtection>" + expected + "</DeletionProtection>"));
}
@Test
void deletionProtectionDefaultsToFalse() {
createCluster(null).then().statusCode(200)
.body(containsString("<DeletionProtection>false</DeletionProtection>"));
describeReports("false");
}
@Test
void deletionProtectionSetOnCreateIsReadBack() {
createCluster("true").then().statusCode(200)
.body(containsString("<DeletionProtection>true</DeletionProtection>"));
describeReports("true");
}
@Test
void modifyChangesDeletionProtection() {
createCluster(null).then().statusCode(200);
rds("ModifyDBCluster")
.formParam("DBClusterIdentifier", ID)
.formParam("DeletionProtection", "true")
.when().post("/").then().statusCode(200)
.body(containsString("<DeletionProtection>true</DeletionProtection>"));
describeReports("true");
rds("ModifyDBCluster")
.formParam("DBClusterIdentifier", ID)
.formParam("DeletionProtection", "false")
.when().post("/").then().statusCode(200);
describeReports("false");
}
@Test
void protectedClusterIsNotDeletedUntilProtectionIsDisabled() {
createCluster("true").then().statusCode(200);
rds("DeleteDBCluster").formParam("DBClusterIdentifier", ID)
.formParam("SkipFinalSnapshot", "true")
.when().post("/").then().statusCode(400)
.body(containsString("<Code>InvalidParameterCombination</Code>"));
describeReports("true");
rds("ModifyDBCluster").formParam("DBClusterIdentifier", ID)
.formParam("DeletionProtection", "false")
.when().post("/").then().statusCode(200);
rds("DeleteDBCluster").formParam("DBClusterIdentifier", ID)
.formParam("SkipFinalSnapshot", "true")
.when().post("/").then().statusCode(200);
}
@Test
void modifyWithoutDeletionProtectionKeepsIt() {
createCluster("true").then().statusCode(200);
rds("ModifyDBCluster")
.formParam("DBClusterIdentifier", ID)
.formParam("ApplyImmediately", "true")
.when().post("/").then().statusCode(200)
.body(containsString("<DeletionProtection>true</DeletionProtection>"));
describeReports("true");
}
}
@@ -6,6 +6,7 @@ import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static io.floci.aws.testing.RdsRequests.createAuroraPostgresCluster;
import static io.floci.aws.testing.RdsRequests.rds;
import static org.hamcrest.Matchers.containsString;
@@ -33,12 +34,7 @@ class RdsClusterEngineModeStorageEncryptedIntegrationTest {
@Test
void engineModeAndStorageEncryptedRoundTripThroughDescribe() {
rds("CreateDBCluster")
.formParam("DBClusterIdentifier", ID)
.formParam("Engine", "aurora-postgresql")
.formParam("EngineVersion", "16.3")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "secret99password")
createAuroraPostgresCluster(ID)
.formParam("EngineMode", "serverless")
.formParam("StorageEncrypted", "true")
.when().post("/").then().statusCode(200)
@@ -53,12 +49,7 @@ class RdsClusterEngineModeStorageEncryptedIntegrationTest {
@Test
void engineModeDefaultsToProvisionedAndStorageEncryptedDefaultsToFalseWhenOmitted() {
rds("CreateDBCluster")
.formParam("DBClusterIdentifier", ID)
.formParam("Engine", "aurora-postgresql")
.formParam("EngineVersion", "16.3")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "secret99password")
createAuroraPostgresCluster(ID)
.when().post("/").then().statusCode(200)
.body(containsString("<EngineMode>provisioned</EngineMode>"))
.body(containsString("<StorageEncrypted>false</StorageEncrypted>"));
@@ -927,7 +927,7 @@ class RdsQueryHandlerTest {
when(service.createDbCluster(eq("mycluster"), eq("aurora-postgresql"), any(),
eq("omni_admin"), isNull(), eq("omni"), eq(false), isNull(),
isNull(), isNull(), eq(false), any(),
isNull(), isNull(), isNull(), eq(true), eq("kms-key-1"), isNull(), eq(false)))
isNull(), isNull(), isNull(), eq(true), eq("kms-key-1"), isNull(), eq(false), isNull(), eq(false)))
.thenReturn(cluster);
MultivaluedMap<String, String> p = params();
@@ -947,7 +947,7 @@ class RdsQueryHandlerTest {
verify(service).createDbCluster(eq("mycluster"), eq("aurora-postgresql"), any(),
eq("omni_admin"), isNull(), eq("omni"), eq(false), isNull(),
isNull(), isNull(), eq(false), any(),
isNull(), isNull(), isNull(), eq(true), eq("kms-key-1"), isNull(), eq(false));
isNull(), isNull(), isNull(), eq(true), eq("kms-key-1"), isNull(), eq(false), isNull(), eq(false));
}
@Test
@@ -955,7 +955,7 @@ class RdsQueryHandlerTest {
DbCluster cluster = makeCluster("mycluster");
when(service.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any(),
any(), any(), anyBoolean(), any(), any(), any(), any(), eq(false), isNull(),
any(), anyBoolean()))
any(), anyBoolean(), any(), anyBoolean()))
.thenReturn(cluster);
MultivaluedMap<String, String> p = params();
@@ -972,7 +972,7 @@ class RdsQueryHandlerTest {
void createDbClusterAnswersADockerRejectionWithANonRetryableCapacityError() {
when(service.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any(),
any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean(), any(),
any(), anyBoolean()))
any(), anyBoolean(), any(), anyBoolean()))
.thenThrow(new ContainerRejectedException(
new DockerException("authorization denied by plugin: --mounts is not allowed", 403)));
@@ -993,7 +993,7 @@ class RdsQueryHandlerTest {
DbCluster cluster = makeCluster("mycluster");
when(service.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any(),
any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean(), any(),
any(), anyBoolean()))
any(), anyBoolean(), any(), anyBoolean()))
.thenReturn(cluster);
// Another request associated the role between the create and the roles being added.
when(service.addRolesToNewDbCluster(any(), eq("mycluster"), anyList()))
@@ -59,6 +59,7 @@ class DbClusterPersistenceTest {
DbCluster restored = clusterReader.get("legacy").orElseThrow();
assertFalse(restored.isCopyTagsToSnapshot());
assertTrue(restored.getEnabledCloudwatchLogsExports().isEmpty());
assertFalse(restored.isDeletionProtection());
PersistentStorage<String, DbInstance> instanceReader = new PersistentStorage<>(
instances, new TypeReference<Map<String, DbInstance>>() {});
instanceReader.load();
@@ -156,4 +157,22 @@ class DbClusterPersistenceTest {
assertTrue(reader.get("data-api-cluster").orElseThrow().isHttpEndpointEnabled());
}
@Test
void deletionProtectionPersistsAcrossInstances(@TempDir Path directory) {
Path file = directory.resolve("rds-clusters.json");
DbCluster cluster = new DbCluster();
cluster.setDbClusterIdentifier("protected-cluster");
cluster.setDeletionProtection(true);
PersistentStorage<String, DbCluster> writer = new PersistentStorage<>(
file, new TypeReference<Map<String, DbCluster>>() {});
writer.put(cluster.getDbClusterIdentifier(), cluster);
PersistentStorage<String, DbCluster> reader = new PersistentStorage<>(
file, new TypeReference<Map<String, DbCluster>>() {});
reader.load();
assertTrue(reader.get("protected-cluster").orElseThrow().isDeletionProtection());
}
}
@@ -128,6 +128,15 @@ public final class RdsRequests {
.getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port");
}
public static RequestSpecification createAuroraPostgresCluster(String id) {
return rds("CreateDBCluster")
.formParam("DBClusterIdentifier", id)
.formParam("Engine", "aurora-postgresql")
.formParam("EngineVersion", "16.3")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "secret99password");
}
public static void deleteInstance(String id) {
rds("DeleteDBInstance")
.formParam("DBInstanceIdentifier", id)