fix(rds): keep BackupRetentionPeriod on DB clusters (#7094)

This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-11-04 15:53:14 -04:00
1 parent d76343992f
commit 0f242e9ee0
4 files changed
+178 -4

No files matched your search

@@ -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")
@@ -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<T>(List<T> 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<String> DATA_API_ENGINES = Set.of("aurora-postgresql", "aurora-mysql");
/**
@@ -46,6 +46,7 @@ public class DbCluster {
private List<DbRoleAssociation> 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; }
@@ -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("<BackupRetentionPeriod>1</BackupRetentionPeriod>"));
}
@Test
void backupRetentionPeriodEchoedOnCreateWhenProvided() {
createCluster(ID, "7").then().statusCode(200);
rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID)
.when().post("/").then().statusCode(200)
.body(containsString("<BackupRetentionPeriod>7</BackupRetentionPeriod>"));
}
@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("<BackupRetentionPeriod>14</BackupRetentionPeriod>"));
rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID)
.when().post("/").then().statusCode(200)
.body(containsString("<BackupRetentionPeriod>14</BackupRetentionPeriod>"));
}
@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("<BackupRetentionPeriod>7</BackupRetentionPeriod>"));
}
@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("<BackupRetentionPeriod>7</BackupRetentionPeriod>"));
rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID)
.when().post("/").then().statusCode(200)
.body(containsString("<BackupRetentionPeriod>7</BackupRetentionPeriod>"));
}
}