From 0f242e9ee029c1065ce647ce008dfaee919645b5 Mon Sep 17 00:00:00 2001 From: Matheus Oliveira <99829531+omatheusmesmo@users.noreply.github.com> Date: Wed, 4 Nov 2026 16:53:14 -0300 Subject: [PATCH] fix(rds): keep BackupRetentionPeriod on DB clusters (#7094) --- .../floci/services/rds/RdsQueryHandler.java | 14 ++ .../floci/services/rds/RdsService.java | 22 ++- .../floci/services/rds/model/DbCluster.java | 4 + ...rBackupRetentionPeriodIntegrationTest.java | 142 ++++++++++++++++++ 4 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterBackupRetentionPeriodIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java index 0b66439f39..1af927603c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java @@ -1310,6 +1310,9 @@ public class RdsQueryHandler { String masterUserSecretKmsKeyId = params.getFirst("MasterUserSecretKmsKeyId"); String engineMode = params.getFirst("EngineMode"); boolean storageEncrypted = "true".equalsIgnoreCase(params.getFirst("StorageEncrypted")); + Integer backupRetentionPeriodParam = parseIntegerParam(params, "BackupRetentionPeriod"); + int backupRetentionPeriod = backupRetentionPeriodParam != null ? backupRetentionPeriodParam : 1; + RdsService.validateBackupRetentionPeriod(backupRetentionPeriod); if (engineVersion == null) { engineVersion = defaultEngineVersion(engine); @@ -1379,6 +1382,9 @@ public class RdsQueryHandler { if (enableHttpEndpoint) { cluster = service.setHttpEndpoint(region, id, true); } + if (backupRetentionPeriodParam != null) { + cluster = service.setBackupRetentionPeriod(region, id, backupRetentionPeriod); + } String result = dbClusterXml(cluster); return Response.ok(AwsQueryResponse.envelope("CreateDBCluster", AwsNamespaces.RDS, result)).build(); } catch (AwsException e) { @@ -1489,6 +1495,10 @@ public class RdsQueryHandler { Integer serverlessV2SecondsUntilAutoPause = parseIntegerParam( params, "ServerlessV2ScalingConfiguration.SecondsUntilAutoPause"); Boolean enableHttpEndpoint = parseOptionalBoolean(params, "EnableHttpEndpoint"); + Integer modifyBackupRetentionPeriod = parseIntegerParam(params, "BackupRetentionPeriod"); + if (modifyBackupRetentionPeriod != null) { + RdsService.validateBackupRetentionPeriod(modifyBackupRetentionPeriod); + } if (Boolean.TRUE.equals(enableHttpEndpoint)) { RdsService.requireDataApiEngine(service.getDbCluster(id, region).getEngineIdentifier()); } @@ -1501,6 +1511,9 @@ public class RdsQueryHandler { if (enableHttpEndpoint != null) { cluster = service.setHttpEndpoint(region, id, enableHttpEndpoint); } + if (modifyBackupRetentionPeriod != null) { + cluster = service.setBackupRetentionPeriod(region, id, modifyBackupRetentionPeriod); + } String result = dbClusterXml(cluster); return Response.ok(AwsQueryResponse.envelope("ModifyDBCluster", AwsNamespaces.RDS, result)).build(); } catch (AwsException e) { @@ -3486,6 +3499,7 @@ public class RdsQueryHandler { .elem("HttpEndpointEnabled", c.isHttpEndpointEnabled()) .elem("MultiAZ", c.isMultiAz()) .elem("StorageEncrypted", c.isStorageEncrypted()) + .elem("BackupRetentionPeriod", c.getBackupRetentionPeriod()) .elem("KmsKeyId", c.getKmsKeyId()) .elem("AvailabilityZone", c.getAvailabilityZone() != null ? c.getAvailabilityZone() : config.defaultAvailabilityZone()) .elem("PreferredMaintenanceWindow", "mon:00:00-mon:03:00") diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java index ddbc89f813..c65f797bf9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java @@ -1748,9 +1748,6 @@ public class RdsService implements Resettable, ResourceProvider { /** AWS's default retention for replicated automated backups, in days. */ static final int DEFAULT_REPLICATED_BACKUP_RETENTION_DAYS = 7; - /** Floci does not model a cluster's backup retention period; one day is the AWS default. */ - static final int CLUSTER_BACKUP_RETENTION_DAYS = 1; - public record AutomatedBackupPage(List backups, String marker) {} /** @@ -2010,7 +2007,7 @@ public class RdsService implements Resettable, ResourceProvider { backup.setRegion(region); backup.setEarliestTime(cluster.getCreatedAt() != null ? cluster.getCreatedAt() : now); backup.setLatestTime(now); - backup.setBackupRetentionPeriod(CLUSTER_BACKUP_RETENTION_DAYS); + backup.setBackupRetentionPeriod(cluster.getBackupRetentionPeriod()); String dataKey = clusterBackupDataKey(region, cluster.getDbClusterResourceId()); if (cluster.getStatus() == DbInstanceStatus.DELETING && snapshotData.get(dataKey).isPresent()) { backup.setDataSaved(true); @@ -11402,6 +11399,23 @@ public class RdsService implements Resettable, ResourceProvider { return RESPONSE_COPIER.convertValue(cluster, DbCluster.class); } + public static void validateBackupRetentionPeriod(int backupRetentionPeriod) { + if (backupRetentionPeriod < 1 || backupRetentionPeriod > 35) { + throw new AwsException("InvalidParameterValue", + "BackupRetentionPeriod must be between 1 and 35.", 400); + } + } + + public synchronized DbCluster setBackupRetentionPeriod(String region, String clusterId, int backupRetentionPeriod) { + validateBackupRetentionPeriod(backupRetentionPeriod); + String effectiveRegion = effectiveRegion(region); + DbCluster cluster = getDbCluster(clusterId, effectiveRegion); + cluster.setBackupRetentionPeriod(backupRetentionPeriod); + putClusterForScope(currentAccountId(), effectiveRegion, cluster.getDbClusterIdentifier(), cluster); + LOG.infov("Backup retention period set to {0} for DB cluster {1}", backupRetentionPeriod, + cluster.getDbClusterIdentifier()); + return RESPONSE_COPIER.convertValue(cluster, DbCluster.class); + } private static final Set DATA_API_ENGINES = Set.of("aurora-postgresql", "aurora-mysql"); /** diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java index b36aea9b4c..a6944d01b1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java @@ -46,6 +46,7 @@ public class DbCluster { private List associatedRoles = new ArrayList<>(); private String engineMode; private boolean storageEncrypted; + private int backupRetentionPeriod = 1; private String kmsKeyId; private String dockerVolumeName; @@ -202,6 +203,9 @@ public class DbCluster { public boolean isStorageEncrypted() { return storageEncrypted; } public void setStorageEncrypted(boolean storageEncrypted) { this.storageEncrypted = storageEncrypted; } + public int getBackupRetentionPeriod() { return backupRetentionPeriod; } + public void setBackupRetentionPeriod(int backupRetentionPeriod) { this.backupRetentionPeriod = backupRetentionPeriod; } + public String getKmsKeyId() { return kmsKeyId; } public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; } diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterBackupRetentionPeriodIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterBackupRetentionPeriodIntegrationTest.java new file mode 100644 index 0000000000..e443b515e0 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterBackupRetentionPeriodIntegrationTest.java @@ -0,0 +1,142 @@ +package io.github.hectorvent.floci.services.rds; + +import io.github.hectorvent.floci.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.github.hectorvent.floci.testing.RdsRequests.rds; +import static org.hamcrest.Matchers.containsString; + +/** + * BackupRetentionPeriod round-trip from CreateDBCluster/ModifyDBCluster to DescribeDBClusters. + * Validates the 1-35 range enforcement on create and modify, and that retained automated + * backups inherit the cluster's period instead of the fixed constant. + */ +@QuarkusTest +@TestProfile(RdsMockProfile.class) +class RdsClusterBackupRetentionPeriodIntegrationTest { + + private static final String ID = "br-cluster"; + private static final String INVALID_ID = "br-invalid"; + + @AfterEach + void cleanUp() { + rds("DeleteDBCluster").formParam("DBClusterIdentifier", ID) + .formParam("SkipFinalSnapshot", "true").when().post("/"); + rds("DeleteDBCluster").formParam("DBClusterIdentifier", INVALID_ID) + .formParam("SkipFinalSnapshot", "true").when().post("/"); + } + + 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"); + if (period != null) { + req = req.formParam("BackupRetentionPeriod", period); + } + return req.when().post("/"); + } + + @Test + void backupRetentionPeriodDefaultsToOneOnCreate() { + createCluster(ID, null).then().statusCode(200); + + rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID) + .when().post("/").then().statusCode(200) + .body(containsString("1")); + } + + @Test + void backupRetentionPeriodEchoedOnCreateWhenProvided() { + createCluster(ID, "7").then().statusCode(200); + + rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID) + .when().post("/").then().statusCode(200) + .body(containsString("7")); + } + + @Test + void backupRetentionPeriodRejectedWhenZero() { + createCluster(INVALID_ID, "0").then().statusCode(400) + .body(containsString("BackupRetentionPeriod must be between 1 and 35")); + } + + @Test + void backupRetentionPeriodRejectedWhenAbove35() { + createCluster(INVALID_ID, "36").then().statusCode(400) + .body(containsString("BackupRetentionPeriod must be between 1 and 35")); + } + + @Test + void backupRetentionPeriodModifiedAndValidated() { + createCluster(ID, "3").then().statusCode(200); + + rds("ModifyDBCluster") + .formParam("DBClusterIdentifier", ID) + .formParam("BackupRetentionPeriod", "14") + .when().post("/").then().statusCode(200) + .body(containsString("14")); + + rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID) + .when().post("/").then().statusCode(200) + .body(containsString("14")); + } + + @Test + void backupRetentionPeriodModifyRejectedWhenInvalid() { + createCluster(ID, "5").then().statusCode(200); + + rds("ModifyDBCluster") + .formParam("DBClusterIdentifier", ID) + .formParam("BackupRetentionPeriod", "40") + .when().post("/").then().statusCode(400) + .body(containsString("BackupRetentionPeriod must be between 1 and 35")); + } + + @Test + void backupRetentionPeriodRejectedWhenNegative() { + createCluster(INVALID_ID, "-1").then().statusCode(400) + .body(containsString("BackupRetentionPeriod must be between 1 and 35")); + } + + @Test + void retainedAutomatedBackupsInheritClusterPeriod() { + createCluster(ID, "7").then().statusCode(200); + + // Delete cluster but retain automated backups + rds("DeleteDBCluster") + .formParam("DBClusterIdentifier", ID) + .formParam("SkipFinalSnapshot", "true") + .formParam("DeleteAutomatedBackups", "false") + .when().post("/").then().statusCode(200); + + // Retained backup should have the cluster's period (7), not the default (1) + rds("DescribeDBClusterAutomatedBackups") + .formParam("DBClusterIdentifier", ID) + .when().post("/").then().statusCode(200) + .body(containsString("7")); + } + + @Test + void modifyWithoutBackupRetentionPeriodKeepsExistingValue() { + createCluster(ID, "7").then().statusCode(200); + + // Modify cluster without specifying BackupRetentionPeriod + rds("ModifyDBCluster") + .formParam("DBClusterIdentifier", ID) + .formParam("ApplyImmediately", "true") + .when().post("/").then().statusCode(200) + .body(containsString("7")); + + rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID) + .when().post("/").then().statusCode(200) + .body(containsString("7")); + } +}