fix(rds): apply DBInstanceClass, AllocatedStorage and EngineVersion on ModifyDBInstance (#4214)

* fix(rds): apply DBInstanceClass, AllocatedStorage and EngineVersion on ModifyDBInstance

ModifyDBInstance parsed the password, IAM auth, subnet group, option group,
security groups and the backup and monitoring settings, but never read
DBInstanceClass, AllocatedStorage or EngineVersion. A resize or an engine
upgrade returned 200 while DescribeDBInstances kept reporting the values the
instance was created with, so a caller that diffs its configuration against a
read-back saw the same pending change on every run.

The three members move into a DbInstanceScalingChanges record that the handler
fills from the request and the service applies, with the rules the AWS API
reference states for them:

- AllocatedStorage below the current size fails with InvalidParameterCombination
- an increase under 10% is rounded up to 10% greater on PostgreSQL, MySQL and
  MariaDB, and taken as given on SQL Server
- the same size is not a change and is left alone
- a different major EngineVersion needs AllowMajorVersionUpgrade

Floci applies the change immediately rather than deferring it to the preferred
maintenance window: it runs no maintenance window and models no
PendingModifiedValues, so a deferred change would never be applied at all.

* fix(rds): reject a ModifyDBInstance scaling change before applying any member

The scaling members were validated as they were assigned: the class was set,
then the storage was resolved, which can refuse a shrink, then the engine
version was checked, which can refuse a major upgrade. The instance a modify
works on is the stored object rather than a copy, so a request refused at the
second or third step had already written the earlier ones, and a read-back
reported a class the 400 said it never got.

The checks now run in resolveFor, ahead of every setter in modifyDbInstance,
including the password rotation that reaches the running container. applyTo is
left as pure assignment of an already-resolved record.
This commit is contained in:
Abhigya authored and GitHub committed 2026-09-23 11:41:15 -04:00
1 parent daecc6da17
commit f2d6710b24
10 files changed
+598 -16

No files matched your search

@@ -309,10 +309,11 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
// provision() is re-invoked on every UpdateStack for every resource, so a same-id instance
// already on file must be reconciled rather than re-created (createDbInstance throws
// DBInstanceAlreadyExists). Only the properties RdsService.modifyDbInstance actually supports
// (password, IAM auth, subnet group) are reconciled here; other property changes (engine,
// instance class, allocated storage, ...) are a pre-existing gap in that method, not addressed
// by this fix.
// DBInstanceAlreadyExists). Only password, IAM auth and subnet group are reconciled here.
// modifyDbInstance also takes DBInstanceClass, AllocatedStorage and EngineVersion now, but
// threading them through an UpdateStack is a separate change: DBInstanceClass and
// AllocatedStorage are update-in-place on the AWS::RDS::DBInstance schema while Engine is
// create-only, so passing them needs the replacement handling the DB cluster arm has.
DbInstance instance = sameNameExistingResource(priorPhysicalId, id, rdsService::getDbInstance);
if (instance != null) {
instance = rdsService.modifyDbInstance(
@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.services.rds.model.DbEndpoint;
import io.github.hectorvent.floci.services.docdb.DocDbQueryHandler;
import io.github.hectorvent.floci.services.neptune.NeptuneQueryHandler;
import io.github.hectorvent.floci.services.rds.model.DbInstance;
import io.github.hectorvent.floci.services.rds.model.DbInstanceScalingChanges;
import io.github.hectorvent.floci.services.rds.model.DbInstanceSettings;
import io.github.hectorvent.floci.services.rds.model.LogExportChanges;
import io.github.hectorvent.floci.services.rds.model.DbInstanceStatus;
@@ -409,11 +410,12 @@ public class RdsQueryHandler {
}
try {
DbInstanceSettings settings = instanceSettings(params, false);
DbInstanceScalingChanges scaling = scalingChanges(params);
List<String> vpcSecurityGroupIds = vpcSecurityGroupIds(params);
DbInstance instance = service.modifyDbInstance(
id, newPassword, iamEnabled, dbSubnetGroupName,
vpcSecurityGroupIds, optionGroupName, region, autoMinorVersionUpgrade,
settings, publiclyAccessible);
settings, publiclyAccessible, scaling);
String result = dbInstanceXml(instance);
return Response.ok(AwsQueryResponse.envelope("ModifyDBInstance", AwsNamespaces.RDS, result)).build();
} catch (AwsException e) {
@@ -421,6 +423,18 @@ public class RdsQueryHandler {
}
}
/**
* The members a modify scales the instance by. Reading them here is why an SDK request to
* resize a live instance, or to upgrade its engine version, used to be accepted and dropped.
*/
private static DbInstanceScalingChanges scalingChanges(MultivaluedMap<String, String> params) {
return new DbInstanceScalingChanges(
params.getFirst("DBInstanceClass"),
optionalInt(params.getFirst("AllocatedStorage")),
params.getFirst("EngineVersion"),
parseOptionalBoolean(params, "AllowMajorVersionUpgrade"));
}
private static DbInstanceSettings instanceSettings(MultivaluedMap<String, String> params) {
return instanceSettings(params, true);
}
@@ -2220,7 +2234,7 @@ public class RdsQueryHandler {
xml.elem("IAMDatabaseAuthenticationEnabled", i.isIamDatabaseAuthenticationEnabled())
.elem("MultiAZ", i.isMultiAz())
.elem("AutoMinorVersionUpgrade", i.isAutoMinorVersionUpgrade())
.elem("StorageType", "gp2")
.elem("StorageType", DbInstance.DEFAULT_STORAGE_TYPE)
.elem("PubliclyAccessible", i.isPubliclyAccessible())
.elem("AvailabilityZone", i.getAvailabilityZone() != null ? i.getAvailabilityZone() : config.defaultAvailabilityZone())
.elem("PreferredMaintenanceWindow", i.getPreferredMaintenanceWindow() != null
@@ -33,6 +33,7 @@ import io.github.hectorvent.floci.services.rds.model.DbEndpoint;
import io.github.hectorvent.floci.services.rds.model.DbInstance;
import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.kms.model.KmsKey;
import io.github.hectorvent.floci.services.rds.model.DbInstanceScalingChanges;
import io.github.hectorvent.floci.services.rds.model.DbInstanceSettings;
import io.github.hectorvent.floci.services.rds.model.DbInstanceStatus;
import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
@@ -2400,13 +2401,24 @@ public class RdsService implements Resettable, ResourceProvider {
settings, null);
}
public DbInstance modifyDbInstance(
String id, String newPassword, Boolean iamEnabled,
String dbSubnetGroupName, List<String> vpcSecurityGroupIds,
String optionGroupName, String region, Boolean autoMinorVersionUpgrade,
DbInstanceSettings settings, Boolean publiclyAccessible) {
return modifyDbInstance(id, newPassword, iamEnabled, dbSubnetGroupName,
vpcSecurityGroupIds, optionGroupName, region, autoMinorVersionUpgrade,
settings, publiclyAccessible, DbInstanceScalingChanges.unchanged());
}
// synchronized like the tag and delete paths: an unguarded read-modify-write here could
// write an instance back after deleteDbInstance removed it
public synchronized DbInstance modifyDbInstance(
String id, String newPassword, Boolean iamEnabled,
String dbSubnetGroupName, List<String> vpcSecurityGroupIds,
String optionGroupName, String region, Boolean autoMinorVersionUpgrade,
DbInstanceSettings settings, Boolean publiclyAccessible) {
DbInstanceSettings settings, Boolean publiclyAccessible,
DbInstanceScalingChanges scaling) {
validateInstanceSettings(settings);
String effectiveRegion = effectiveRegion(region);
DbInstance instance = getDbInstance(id, effectiveRegion);
@@ -2416,6 +2428,9 @@ public class RdsService implements Resettable, ResourceProvider {
"DB instance " + id + " is in state " + instance.getStatus().name().toLowerCase(Locale.ROOT)
+ " and cannot be modified.", 400);
}
// Resolved before the first setter runs, since the checks it carries read the instance's
// current size and engine version: a refusal here must leave the whole request unapplied.
DbInstanceScalingChanges resolvedScaling = scaling.resolveFor(instance);
DbInstanceSettings effective = withEffectiveWindows(settings, instance);
instance.setStatus(DbInstanceStatus.AVAILABLE);
if (optionGroupName != null && !optionGroupName.isBlank()) {
@@ -2457,6 +2472,7 @@ public class RdsService implements Resettable, ResourceProvider {
instance.setAutoMinorVersionUpgrade(autoMinorVersionUpgrade);
}
effective.applyTo(instance);
resolvedScaling.applyTo(instance);
if (publiclyAccessible != null) {
instance.setPubliclyAccessible(publiclyAccessible);
}
@@ -11,6 +11,9 @@ import java.util.Map;
@RegisterForReflection
public class DbInstance {
/** The only storage type Floci models, and the one DescribeDBInstances reports. */
public static final String DEFAULT_STORAGE_TYPE = "gp2";
private String dbInstanceIdentifier;
private DatabaseEngine engine;
// the engine name the request gave (aurora-postgresql, postgres, ...): the enum collapses the
@@ -0,0 +1,108 @@
package io.github.hectorvent.floci.services.rds.model;
import io.github.hectorvent.floci.core.common.AwsException;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.Set;
/**
* The compute, storage and engine-version members of ModifyDBInstance: the ones AWS applies with
* an outage rather than in the background. A null member is one the request left out, which leaves
* the instance's current value alone.
*
* <p>Floci applies them as soon as the request lands. AWS defers them to the preferred maintenance
* window unless ApplyImmediately is set, and reports them under PendingModifiedValues until then;
* Floci runs no maintenance window, so a deferred change would never be applied at all and every
* read-back would report drift.
*/
@RegisterForReflection
public record DbInstanceScalingChanges(String dbInstanceClass,
Integer allocatedStorage,
String engineVersion,
Boolean allowMajorVersionUpgrade) {
/**
* The engines whose AllocatedStorage member documents "the value supplied must be at least 10%
* greater than the current value". SQL Server, the fourth engine Floci models, is not one of
* them and takes the size it is given.
*/
private static final Set<DatabaseEngine> TEN_PERCENT_MINIMUM_INCREASE =
Set.of(DatabaseEngine.POSTGRES, DatabaseEngine.MYSQL, DatabaseEngine.MARIADB);
public static DbInstanceScalingChanges unchanged() {
return new DbInstanceScalingChanges(null, null, null, null);
}
/**
* The same changes with AllocatedStorage resolved against the instance and every rule checked,
* which leaves {@link #applyTo} pure assignment. AWS refuses a request as a whole, and the
* instance a modify works on is the stored object rather than a copy, so a member written
* before a later member is refused would stay written and read back as drift.
*/
public DbInstanceScalingChanges resolveFor(DbInstance instance) {
if (engineVersion != null && !engineVersion.isBlank()) {
requireMajorVersionUpgradeAllowed(instance.getEngineVersion());
}
Integer resolvedStorage =
allocatedStorage == null ? null : resolvedAllocatedStorage(instance);
return new DbInstanceScalingChanges(dbInstanceClass, resolvedStorage, engineVersion,
allowMajorVersionUpgrade);
}
/** Assigns the members of an instance resolved by {@link #resolveFor}. */
public void applyTo(DbInstance instance) {
if (dbInstanceClass != null && !dbInstanceClass.isBlank()) {
instance.setDbInstanceClass(dbInstanceClass);
}
if (allocatedStorage != null) {
instance.setAllocatedStorage(allocatedStorage);
}
if (engineVersion != null && !engineVersion.isBlank()) {
instance.setEngineVersion(engineVersion);
}
}
/**
* The size the instance ends up with. RDS storage only ever grows: a smaller size is refused,
* and an increase of less than 10% is rounded up to 10% rather than refused, both as the
* AllocatedStorage member documents. The same size is left alone, since it is not a change.
*/
private int resolvedAllocatedStorage(DbInstance instance) {
int current = instance.getAllocatedStorage();
if (allocatedStorage == current) {
return current;
}
if (allocatedStorage < current) {
throw new AwsException("InvalidParameterCombination",
"Invalid storage size for engine name " + instance.getEngineIdentifier()
+ " and storage type " + DbInstance.DEFAULT_STORAGE_TYPE + ": "
+ allocatedStorage, 400);
}
if (!TEN_PERCENT_MINIMUM_INCREASE.contains(instance.getEngine())) {
return allocatedStorage;
}
return Math.max(allocatedStorage, Math.ceilDiv(current * 11, 10));
}
/**
* AllowMajorVersionUpgrade states the rule as a constraint on EngineVersion: it "must be
* allowed when specifying a value for the EngineVersion parameter that's a different major
* version than the DB instance's current version".
*/
private void requireMajorVersionUpgradeAllowed(String currentEngineVersion) {
if (sameMajorVersion(currentEngineVersion, engineVersion)
|| Boolean.TRUE.equals(allowMajorVersionUpgrade)) {
return;
}
throw new AwsException("InvalidParameterCombination",
"The AllowMajorVersionUpgrade flag must be present when upgrading to a new major "
+ "version.", 400);
}
private static boolean sameMajorVersion(String current, String requested) {
if (current == null || requested == null) {
return true;
}
return current.split("\\.")[0].equals(requested.split("\\.")[0]);
}
}