fix(cloudformation): resolve an RDS instance's previous template as it was deployed (#9058)

This commit is contained in:
Suja Shaj authored and GitHub committed 2026-11-19 20:55:19 -04:00
1 parent a6a609d62f
commit 3e1fe3c8b6
11 files changed
+190 -61

No files matched your search

@@ -2949,7 +2949,7 @@ public class CloudFormationService implements ResourceProvider {
resource.getPhysicalId(), resource.getAttributes(),
event -> addEvent(stack, logicalId, event.getPhysicalResourceId(), type,
event.getResourceStatus(), event.getResourceStatusReason()),
priorProps, changes, stack.getDelegatedRoleBoundaryArn());
priorProps, changes, stack.getDelegatedRoleBoundaryArn(), previousEngine);
}
resource.setUpdateReplacePolicy(
resDef.path("UpdateReplacePolicy").asText(null));
@@ -111,19 +111,21 @@ public class CfnResourceDispatcher {
Map<String, String> existingAttributes, Consumer<StackEvent> progress,
JsonNode priorProperties, PropertyChanges changes) {
return provision(logicalId, resourceType, properties, engine, region, accountId, stackName,
existingPhysicalId, existingAttributes, progress, priorProperties, changes, null);
existingPhysicalId, existingAttributes, progress, priorProperties, changes, null, null);
}
/**
* The same with the permissions boundary a delegation registered for whoever created the stack,
* which the role provisioner applies to a role it creates.
* which the role provisioner applies to a role it creates, and {@code priorEngine}, which
* resolves {@code priorProperties} with the previous deployment's parameters; see
* {@link ProvisionContext}.
*/
public StackResource provision(String logicalId, String resourceType, JsonNode properties,
CloudFormationTemplateEngine engine, String region, String accountId,
String stackName, String existingPhysicalId,
Map<String, String> existingAttributes, Consumer<StackEvent> progress,
JsonNode priorProperties, PropertyChanges changes,
String delegatedRoleBoundaryArn) {
String delegatedRoleBoundaryArn, CloudFormationTemplateEngine priorEngine) {
StackResource resource = new StackResource();
resource.setLogicalId(logicalId);
resource.setResourceType(resourceType);
@@ -135,7 +137,7 @@ public class CfnResourceDispatcher {
if (owner != null) {
owner.provision(resource, properties,
new ProvisionContext(engine, region, accountId, stackName, existingPhysicalId, progress,
priorProperties, changes, delegatedRoleBoundaryArn));
priorProperties, changes, delegatedRoleBoundaryArn, priorEngine));
// A provisioner that migrated a stub without writing its own Arn would leave the
// stub's, and the real resource would read as a stub on its next update.
String arn = resource.getAttributes().get("Arn");
@@ -20,11 +20,34 @@ import java.util.function.Consumer;
* intrinsic functions in properties) plus the region/account/stack it is being created in. The
* helpers were lifted verbatim from the former CloudFormation monolith's private methods
* so extracted provisioners produce byte-identical physical ids and resolved values.
*
* <p>{@code priorEngine} resolves {@code priorProperties} with the previous deployment's parameters,
* conditions and mappings, and the physical ids and attributes the update started from. A value the
* previous template took from a parameter, or from a resource this update replaces, reads as it was
* then. A dynamic reference, an export or another stack's output still reads its current value. A
* context built without one uses {@code engine} there, which reads such a value as this update
* resolves it.
*/
public record ProvisionContext(CloudFormationTemplateEngine engine, String region,
String accountId, String stackName, String priorPhysicalId,
Consumer<StackEvent> progress, JsonNode priorProperties,
PropertyChanges changes, String delegatedRoleBoundaryArn) {
PropertyChanges changes, String delegatedRoleBoundaryArn,
CloudFormationTemplateEngine priorEngine) {
public ProvisionContext {
if (priorEngine == null) {
priorEngine = engine;
}
}
/** The context without the previous deployment's engine; see the record's description. */
public ProvisionContext(CloudFormationTemplateEngine engine, String region,
String accountId, String stackName, String priorPhysicalId,
Consumer<StackEvent> progress, JsonNode priorProperties,
PropertyChanges changes, String delegatedRoleBoundaryArn) {
this(engine, region, accountId, stackName, priorPhysicalId, progress, priorProperties, changes,
delegatedRoleBoundaryArn, null);
}
/**
* The context as every provisioner but the IAM role one needs it: no delegation cap, which is
@@ -56,7 +79,7 @@ public record ProvisionContext(CloudFormationTemplateEngine engine, String regio
*/
public ProvisionContext withPriorPhysicalId(String priorPhysicalId) {
return new ProvisionContext(engine, region, accountId, stackName, priorPhysicalId, progress,
priorProperties, changes);
priorProperties, changes, delegatedRoleBoundaryArn, priorEngine);
}
/** Intermediate resource events retain the stack pipeline's event IDs and metadata. */
@@ -107,10 +107,9 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
* The change is read against this record, as CloudFormation reads the template's change, and not
* against the instance: one a stack created before Floci applied the property is unencrypted
* whatever its template said. Such a resource has no record, so its first update reads the
* previous template's value instead, resolved as well as it still can be, and replaces the
* instance only when that changed. As in {@link Ec2Tags#ownedKeys}, that template resolves on this
* update, so a value taken from a parameter, or from anything else the update changes, reads as the
* new one, and a change made only that way is not seen.
* previous template's value instead, resolved with the previous deployment's parameters (see
* {@link ProvisionContext}), and replaces the instance only when that changed, a change made only
* through a parameter included.
*/
private static final CreateOnlyInputs DB_INSTANCE_STORAGE_ENCRYPTED = new CreateOnlyInputs(
"__FlociDbInstanceStorageEncrypted", "__FlociDbInstanceStorageEncryptedPrior");
@@ -422,7 +421,8 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
DbInstance instance = sameNameExistingResource(priorPhysicalId, id, rdsService::getDbInstance);
String recordedEncryption = DB_INSTANCE_STORAGE_ENCRYPTED.read(attributesBefore);
if (recordedEncryption == null && instance != null && ctx.priorProperties() != null) {
recordedEncryption = String.valueOf(parseBoolProp(ctx.priorProperties(), "StorageEncrypted", engine));
recordedEncryption = String.valueOf(
parseBoolProp(ctx.priorProperties(), "StorageEncrypted", ctx.priorEngine()));
}
boolean replacing = instance != null && recordedEncryption != null
&& !recordedEncryption.equals(encryptionRecord);
@@ -513,8 +513,10 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
* The VPC security groups a kept instance should have, or null to leave them as they are. A
* declared list is the groups, but a cluster member's groups are its cluster's: a template that
* changes a member's list fails the update, as on AWS, where the handler sends the change to
* ModifyDBInstance and RDS refuses it for a member. The change is read from the template, as
* there, so a member a stack created before Floci applied the list still takes it. A list the
* ModifyDBInstance and RDS refuses it for a member. The change is read from the templates, each
* resolved with its own deployment's parameters and resources (see {@link ProvisionContext}), as
* there: a change made only through a parameter, or a group this update replaced, is one, and a
* member a stack created before Floci applied the list still takes it. A list the
* previous template declared and this one drops puts a standalone instance on the default security
* group of the VPC {@code subnetGroupName} leaves it in, as AWS's handler does, and leaves them
* when there is no such group, as there; a
@@ -526,7 +528,7 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
boolean member = clusterMember != null && !clusterMember.isBlank();
if (!declared.isEmpty()) {
if (member && ctx.priorProperties() != null && !Set.copyOf(declared).equals(
Set.copyOf(resolveStringList(ctx.priorProperties(), "VPCSecurityGroups", ctx.engine())))) {
Set.copyOf(resolveStringList(ctx.priorProperties(), "VPCSecurityGroups", ctx.priorEngine())))) {
throw new AwsException("InvalidParameterCombination", "The specified DB Instance is a member of a"
+ " cluster. Modify vpc security group for the DB Cluster using the ModifyDbCluster API", 400);
}