From 589b1aba9cab7089e75caa371b1c8d5f73a44a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Andr=C3=A9?= <92062874+matheusandre1@users.noreply.github.com> Date: Sun, 13 Sep 2026 20:59:01 -0300 Subject: [PATCH] fix(cloudformation): replace AWS RDSDBCluster when EngineMode or StorageEncrypted changes (#3555) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matheus André --- .../provisioners/RdsCfnProvisioner.java | 88 +++++- .../cloudformation/RdsCfnProvisionerTest.java | 273 ++++++++++++++++++ 2 files changed, 355 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java index 7e4492bbc0..1d3fcea017 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java @@ -29,7 +29,8 @@ import java.util.Set; * *

Every type here deletes by physical id alone, so the id-only * {@link #delete(String, String, String)} serves all seven and none of them appears in the - * engine's {@code DELETE_NEEDS_STACK_RESOURCE} set. + * engine's {@code DELETE_NEEDS_STACK_RESOURCE} set. {@code DBCluster} is the one type that + * replaces through {@link ReplacementCleanup}; see {@code provisionDbCluster}. * *

Unlike most extractions this one does not take {@code RdsService} out of the monolith. * {@code AWS::SecretsManager::SecretTargetAttachment} is still served there and reads an @@ -84,7 +85,8 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner { public void delete(String resourceType, String physicalId, String region) { switch (resourceType) { case "AWS::RDS::DBInstance" -> rdsService.deleteDbInstance(physicalId, region); - case "AWS::RDS::DBCluster" -> rdsService.deleteDbCluster(physicalId, region); + case "AWS::RDS::DBCluster" -> CfnDeletes.safeDelete("DB cluster", physicalId, + () -> rdsService.deleteDbCluster(physicalId, region), "DBClusterNotFoundFault"); case "AWS::RDS::DBProxy" -> deleteDbProxySafe(physicalId, region); case "AWS::RDS::DBProxyTargetGroup" -> clearDbProxyTargetGroupSafe(physicalId, region); case "AWS::RDS::DBSubnetGroup" -> rdsService.deleteDbSubnetGroup(physicalId, region); @@ -96,6 +98,40 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner { } } + // ── replacement lifecycle: only AWS::RDS::DBCluster records one, so the other types answer + // "no cleanup owed" through ReplacementCleanup's own empty-record handling ── + + @Override + public boolean hasReplacementUpdate(StackResource resource) { + return ReplacementCleanup.hasReplacement(resource); + } + + @Override + public String updateCleanupPhysicalId(StackResource resource) { + return ReplacementCleanup.cleanupPhysicalId(resource); + } + + @Override + public UpdateCleanupResult completeUpdate(StackResource resource) { + return ReplacementCleanup.complete(resource, this::delete); + } + + @Override + public void clearUpdate(StackResource resource) { + ReplacementCleanup.clear(resource); + } + + /** + * A replacement is undone through the cleanup record: the resource names the displaced cluster + * again and the replacement is deleted. Without a record the update was in place, and putting + * that back needs a snapshot this provisioner does not keep, so the engine keeps reporting it + * as not rolled back, as before. + */ + @Override + public boolean rollbackUpdate(StackResource resource) { + return ReplacementCleanup.rollback(resource, this::delete); + } + private void provisionDbSubnetGroup(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, ProvisionContext ctx, String region) { String explicitName = resolveOptional(props, "DBSubnetGroupName", engine); @@ -255,12 +291,24 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner { } } + /** + * A DB cluster follows CloudFormation's replacement lifecycle. {@code DBClusterIdentifier}, + * {@code EngineMode} and {@code StorageEncrypted} are createOnlyProperties in the + * {@code AWS::RDS::DBCluster} schema, so a change to any of them creates the replacement under + * a distinct physical id first and leaves the displaced cluster standing: it is deleted once + * the stack update commits ({@link #completeUpdate}), or the resource is pointed back at it and + * the replacement is removed when a later resource fails the update ({@link #rollbackUpdate}). + * A cluster the template names explicitly has no distinct id to move to, which is the update + * CloudFormation refuses for a custom-named resource, so it is refused here the same way. + */ private void provisionDbCluster(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, ProvisionContext ctx, String region) { + Map attributesBefore = Map.copyOf(r.getAttributes()); String explicitId = resolveOptional(props, "DBClusterIdentifier", engine); String priorPhysicalId = r.getPhysicalId(); + boolean customNamed = explicitId != null && !explicitId.isBlank(); String id; - if (explicitId != null && !explicitId.isBlank()) { + if (customNamed) { id = explicitId; } else if (priorPhysicalId != null) { id = priorPhysicalId; @@ -271,6 +319,20 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner { // Same re-invocation rationale as provisionDbInstance above; modifyDbCluster only reconciles // password and IAM auth, mirroring that method's existing scope. DbCluster cluster = sameNameExistingResource(priorPhysicalId, id, rdsService::getDbCluster); + String engineMode = resolveOptional(props, "EngineMode", engine); + boolean storageEncrypted = parseBoolProp(props, "StorageEncrypted", engine); + if (cluster != null && requiresDbClusterReplacement(cluster, engineMode, storageEncrypted)) { + if (customNamed) { + throw new AwsException("ValidationError", + "CloudFormation cannot update a stack when a custom-named resource requires " + + "replacing. Rename " + id + " and update the stack again.", 400); + } + id = ctx.generatePhysicalName(r.getLogicalId(), 60, true); + LOG.infov("Replacing DB cluster {0} with {1}: EngineMode/StorageEncrypted changed from " + + "{2}/{3} to {4}/{5}", priorPhysicalId, id, cluster.getEngineMode(), + cluster.isStorageEncrypted(), engineMode, storageEncrypted); + cluster = null; + } if (cluster != null) { cluster = rdsService.modifyDbCluster( id, @@ -293,8 +355,6 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner { String databaseName = resolveOptional(props, "DatabaseName", engine); boolean iamEnabled = parseBoolProp(props, "EnableIAMDatabaseAuthentication", engine); String parameterGroup = resolveOptional(props, "DBClusterParameterGroupName", engine); - String engineMode = resolveOptional(props, "EngineMode", engine); - boolean storageEncrypted = parseBoolProp(props, "StorageEncrypted", engine); if (engineMode == null && !storageEncrypted) { if (serverlessV2MinCapacity == null && serverlessV2MaxCapacity == null && serverlessV2SecondsUntilAutoPause == null) { @@ -311,7 +371,6 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner { serverlessV2MinCapacity, serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause, false, null, engineMode, storageEncrypted); } - deleteRenamedResource(priorPhysicalId, id, rdsService::deleteDbCluster, "DB cluster"); } r.setPhysicalId(cluster.getDbClusterIdentifier()); r.getAttributes().put("DBClusterIdentifier", cluster.getDbClusterIdentifier()); @@ -328,6 +387,23 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner { if (cluster.getDbClusterResourceId() != null) { r.getAttributes().put("DBClusterResourceId", cluster.getDbClusterResourceId()); } + // A provision that left the resource with a new physical id replaced the cluster: the + // displaced one is deleted once the update commits, or restored if the update rolls back. + ReplacementCleanup.record(r, ctx, attributesBefore); + } + + /** + * Whether the template's {@code EngineMode}/{@code StorageEncrypted} differ from the cluster on + * file. Both are createOnlyProperties, so a difference means replacement. A template without + * {@code EngineMode} means the RDS default {@code provisioned}, and a cluster persisted before + * the mode was tracked reports no mode at all, which is the same default. + */ + private static boolean requiresDbClusterReplacement(DbCluster existing, String engineMode, + boolean storageEncrypted) { + String desiredMode = firstNonBlank(engineMode, "provisioned"); + String currentMode = firstNonBlank(existing.getEngineMode(), "provisioned"); + return !desiredMode.equalsIgnoreCase(currentMode) + || existing.isStorageEncrypted() != storageEncrypted; } private Double parseServerlessV2Capacity(JsonNode props, String field, diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RdsCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RdsCfnProvisionerTest.java index ee0ad8bb22..3a6f626e70 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RdsCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RdsCfnProvisionerTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; +import io.github.hectorvent.floci.services.cloudformation.provisioners.UpdateCleanupResult; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.rds.RdsService; import io.github.hectorvent.floci.services.rds.model.DbCluster; @@ -22,6 +23,7 @@ import io.github.hectorvent.floci.services.ssm.model.ParameterHistory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; +import org.mockito.InOrder; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -31,6 +33,8 @@ import java.util.function.Function; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -39,6 +43,9 @@ import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; @@ -1271,6 +1278,272 @@ class RdsCfnProvisionerTest { null, null, null, "us-east-1"); } + private static final String PRIOR_CLUSTER = "my-stack-cluster-0123456789ab"; + + /** RdsService hands back a cluster named after whatever id the provisioner asked to create. */ + private void createDbClusterEchoesRequestedId() { + org.mockito.stubbing.Answer echo = invocation -> { + DbCluster created = mock(DbCluster.class); + doReturn(invocation.getArgument(0)).when(created).getDbClusterIdentifier(); + return created; + }; + when(rdsService.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any(), + any(), any(), anyBoolean(), any())).thenAnswer(echo); + when(rdsService.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any(), + any(), any(), anyBoolean(), any(), any(), any(), any())).thenAnswer(echo); + when(rdsService.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any(), + any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean(), any(), any(), + anyBoolean())).thenAnswer(echo); + } + + /** The unnamed cluster a previous CreateStack left on file under a generated physical id. */ + private DbCluster priorUnnamedCluster(String engineMode, boolean storageEncrypted) { + DbCluster existing = mock(DbCluster.class); + when(existing.getEngineMode()).thenReturn(engineMode); + when(existing.isStorageEncrypted()).thenReturn(storageEncrypted); + when(rdsService.getDbCluster(PRIOR_CLUSTER)).thenReturn(existing); + return existing; + } + + private StackResource updateUnnamedCluster(String json) { + return provisionExisting("Cluster", "AWS::RDS::DBCluster", json, "us-east-1", PRIOR_CLUSTER, + Map.of("DBClusterIdentifier", PRIOR_CLUSTER, "Endpoint.Address", "prior.rds.local")); + } + + @Test + void updateStackReplacesDbClusterWhenEngineModeChangesAndDeletesThePriorOneOnlyAfterCommit() { + // EngineMode is a createOnlyProperty of AWS::RDS::DBCluster. CloudFormation replacement + // creates the new cluster under a distinct physical id while the prior cluster stays + // untouched, and removes the prior one only in the post-commit cleanup. + priorUnnamedCluster("provisioned", false); + createDbClusterEchoesRequestedId(); + + StackResource r = updateUnnamedCluster(""" + {"Engine":"aurora-postgresql","MasterUsername":"admin","MasterUserPassword":"secret", + "EngineMode":"serverless"} + """); + + assertEquals("CREATE_COMPLETE", r.getStatus()); + String replacement = r.getPhysicalId(); + assertNotEquals(PRIOR_CLUSTER, replacement); + assertTrue(replacement.startsWith("my-stack-cluster-"), replacement); + assertEquals(replacement, r.getAttributes().get("DBClusterIdentifier")); + verify(rdsService).createDbCluster(replacement, "aurora-postgresql", null, + "admin", "secret", null, false, null, null, null, false, "us-east-1", + null, null, null, false, null, "serverless", false); + verify(rdsService, never()).modifyDbCluster(any(), any(), any(), any(), any(), any(), any()); + verify(rdsService, never()).deleteDbCluster(any(), any()); + + // The engine announces and runs the cleanup after the stack update has committed. + assertTrue(provisioner.hasReplacementUpdate(r)); + assertEquals(PRIOR_CLUSTER, provisioner.updateCleanupPhysicalId(r)); + UpdateCleanupResult cleanup = provisioner.completeUpdate(r); + assertTrue(cleanup.applicable()); + assertTrue(cleanup.complete()); + InOrder inOrder = inOrder(rdsService); + inOrder.verify(rdsService).createDbCluster(eq(replacement), any(), any(), any(), any(), any(), + anyBoolean(), any(), any(), any(), anyBoolean(), any(), any(), any(), any(), + anyBoolean(), any(), any(), anyBoolean()); + inOrder.verify(rdsService).deleteDbCluster(PRIOR_CLUSTER, "us-east-1"); + verify(rdsService, never()).deleteDbCluster(replacement, "us-east-1"); + provisioner.clearUpdate(r); + assertFalse(provisioner.hasReplacementUpdate(r)); + } + + @Test + void updateStackReplacesDbClusterWhenStorageEncryptedChanges() { + priorUnnamedCluster("provisioned", false); + createDbClusterEchoesRequestedId(); + + StackResource r = updateUnnamedCluster(""" + {"Engine":"aurora-postgresql","StorageEncrypted":true} + """); + + assertEquals("CREATE_COMPLETE", r.getStatus()); + String replacement = r.getPhysicalId(); + assertNotEquals(PRIOR_CLUSTER, replacement); + verify(rdsService).createDbCluster(replacement, "aurora-postgresql", null, + null, null, null, false, null, null, null, false, "us-east-1", + null, null, null, false, null, null, true); + verify(rdsService, never()).modifyDbCluster(any(), any(), any(), any(), any(), any(), any()); + assertEquals(PRIOR_CLUSTER, provisioner.updateCleanupPhysicalId(r)); + verify(rdsService, never()).deleteDbCluster(any(), any()); + } + + @Test + void failedDbClusterReplacementLeavesThePriorClusterIntact() { + // The replacement is created before anything is removed, so a create that fails leaves + // the prior cluster standing and the resource still naming it; the engine then restores + // the committed resource and nothing is owed to the cleanup. + priorUnnamedCluster("provisioned", false); + when(rdsService.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any(), + any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean(), any(), any(), + anyBoolean())) + .thenThrow(new AwsException("InvalidParameterCombination", + "The engine mode serverless is not supported for aurora-postgresql 16.3.", 400)); + + StackResource r = updateUnnamedCluster(""" + {"Engine":"aurora-postgresql","EngineVersion":"16.3","EngineMode":"serverless"} + """); + + assertEquals("CREATE_FAILED", r.getStatus()); + assertTrue(r.getStatusReason().contains("engine mode serverless is not supported")); + assertEquals(PRIOR_CLUSTER, r.getPhysicalId(), "the resource still names the prior cluster"); + assertEquals(PRIOR_CLUSTER, r.getAttributes().get("DBClusterIdentifier")); + verify(rdsService, never()).deleteDbCluster(any(), any()); + verify(rdsService, never()).modifyDbCluster(any(), any(), any(), any(), any(), any(), any()); + assertFalse(provisioner.hasReplacementUpdate(r), "nothing is owed to the cleanup"); + assertFalse(provisioner.rollbackUpdate(r), "there is no replacement to undo"); + } + + @Test + void failedLaterUpdateRollsBackDbClusterReplacementToThePriorCluster() { + // A later resource failing the stack update rolls the replacement back: the resource names + // the prior cluster again, with the attributes it had, and only the replacement is deleted. + priorUnnamedCluster("provisioned", false); + createDbClusterEchoesRequestedId(); + StackResource r = updateUnnamedCluster(""" + {"Engine":"aurora-postgresql","EngineMode":"serverless"} + """); + String replacement = r.getPhysicalId(); + assertNotEquals(PRIOR_CLUSTER, replacement); + + assertTrue(provisioner.rollbackUpdate(r)); + + assertEquals(PRIOR_CLUSTER, r.getPhysicalId()); + assertEquals(PRIOR_CLUSTER, r.getAttributes().get("DBClusterIdentifier")); + assertEquals("prior.rds.local", r.getAttributes().get("Endpoint.Address")); + verify(rdsService).deleteDbCluster(replacement, "us-east-1"); + verify(rdsService, never()).deleteDbCluster(PRIOR_CLUSTER, "us-east-1"); + assertFalse(provisioner.hasReplacementUpdate(r)); + } + + @Test + void dbClusterCleanupKeepsThePriorClusterOwedWhileInstancesAreStillAttached() { + // RDS refuses to delete a cluster with members. The replacement stands regardless; the + // prior cluster stays owed a delete for the engine's retries, the next committed update + // or the stack delete, instead of the replacement being blocked on it. + priorUnnamedCluster("provisioned", false); + createDbClusterEchoesRequestedId(); + doThrow(new AwsException("InvalidDBClusterStateFault", + "DB cluster " + PRIOR_CLUSTER + " still has DB instances.", 400)) + .when(rdsService).deleteDbCluster(PRIOR_CLUSTER, "us-east-1"); + + StackResource r = updateUnnamedCluster(""" + {"Engine":"aurora-postgresql","EngineMode":"serverless"} + """); + + assertEquals("CREATE_COMPLETE", r.getStatus()); + assertNotEquals(PRIOR_CLUSTER, r.getPhysicalId()); + UpdateCleanupResult cleanup = provisioner.completeUpdate(r); + assertTrue(cleanup.applicable()); + assertFalse(cleanup.complete()); + assertEquals(PRIOR_CLUSTER, cleanup.previousPhysicalId()); + assertEquals(1, cleanup.attempts()); + assertTrue(cleanup.failureReason().contains("still has DB instances")); + assertEquals(PRIOR_CLUSTER, provisioner.updateCleanupPhysicalId(r), "still owed"); + } + + @Test + void dbClusterCleanupTreatsAnAlreadyGoneClusterAsDeleted() { + doThrow(new AwsException("DBClusterNotFoundFault", "DB cluster gone not found.", 404)) + .when(rdsService).deleteDbCluster("gone", "us-east-1"); + + assertDoesNotThrow(() -> provisioner.delete("AWS::RDS::DBCluster", "gone", "us-east-1")); + verify(rdsService).deleteDbCluster("gone", "us-east-1"); + } + + @Test + void customNamedDbClusterRefusesCreateOnlyPropertyChangeAndKeepsTheCluster() { + // A cluster the template names explicitly has no distinct physical id to move to, which is + // the update CloudFormation refuses for a custom-named resource. + DbCluster existing = mock(DbCluster.class); + when(existing.getEngineMode()).thenReturn("provisioned"); + when(rdsService.getDbCluster("mycluster")).thenReturn(existing); + + StackResource r = provisionUpdate("Cluster", "AWS::RDS::DBCluster", """ + {"DBClusterIdentifier":"mycluster","Engine":"aurora-postgresql", + "EngineMode":"serverless"} + """, "mycluster"); + + assertEquals("CREATE_FAILED", r.getStatus()); + assertTrue(r.getStatusReason().contains( + "custom-named resource requires replacing. Rename mycluster and update the stack again.")); + assertEquals("mycluster", r.getPhysicalId()); + verify(rdsService, never()).deleteDbCluster(any(), any()); + verify(rdsService, never()).modifyDbCluster(any(), any(), any(), any(), any(), any(), any()); + verify(rdsService, never()).createDbCluster(any(), any(), any(), any(), any(), any(), + anyBoolean(), any(), any(), any(), anyBoolean(), any(), any(), any(), any(), + anyBoolean(), any(), any(), anyBoolean()); + } + + @Test + void renamedDbClusterIsCreatedFirstAndThePriorOneDeletedOnlyAfterCommit() { + // DBClusterIdentifier is createOnly too: a rename is the same replacement lifecycle, not an + // eager delete of the prior cluster right after the create. + createDbClusterEchoesRequestedId(); + + StackResource r = provisionUpdate("Cluster", "AWS::RDS::DBCluster", """ + {"DBClusterIdentifier":"renamed","Engine":"aurora-postgresql"} + """, "mycluster"); + + assertEquals("CREATE_COMPLETE", r.getStatus()); + assertEquals("renamed", r.getPhysicalId()); + verify(rdsService, never()).deleteDbCluster(any(), any()); + assertTrue(provisioner.hasReplacementUpdate(r)); + assertEquals("mycluster", provisioner.updateCleanupPhysicalId(r)); + provisioner.completeUpdate(r); + verify(rdsService).deleteDbCluster("mycluster", "us-east-1"); + } + + @Test + void updateStackKeepsExistingDbClusterWhenCreateOnlyPropertiesUnchanged() { + DbCluster existing = mock(DbCluster.class); + when(existing.getEngineMode()).thenReturn("serverless"); + when(existing.isStorageEncrypted()).thenReturn(true); + when(rdsService.getDbCluster("mycluster")).thenReturn(existing); + DbCluster reconciled = mock(DbCluster.class); + when(reconciled.getDbClusterIdentifier()).thenReturn("mycluster"); + when(rdsService.modifyDbCluster(eq("mycluster"), any(), anyBoolean(), + any(), any(), any(), eq("us-east-1"))).thenReturn(reconciled); + + StackResource r = provisionUpdate("Cluster", "AWS::RDS::DBCluster", """ + {"DBClusterIdentifier":"mycluster","Engine":"aurora-postgresql", + "MasterUserPassword":"rotated", + "EngineMode":"serverless","StorageEncrypted":true} + """, "mycluster"); + + assertEquals("CREATE_COMPLETE", r.getStatus()); + verify(rdsService, never()).deleteDbCluster(any(), any()); + verify(rdsService, never()).createDbCluster(any(), any(), any(), any(), any(), any(), + anyBoolean(), any(), any(), any(), anyBoolean(), any(), any(), any(), any(), + anyBoolean(), any(), any(), anyBoolean()); + verify(rdsService).modifyDbCluster("mycluster", "rotated", false, + null, null, null, "us-east-1"); + } + + @Test + void updateStackTreatsMissingEngineModeAsProvisionedInsteadOfReplacing() { + // A cluster persisted before EngineMode was tracked reports no mode; a template that names + // the RDS default explicitly describes the same cluster and must not replace it. + DbCluster existing = mock(DbCluster.class); + when(existing.getEngineMode()).thenReturn(null); + when(rdsService.getDbCluster("mycluster")).thenReturn(existing); + DbCluster reconciled = mock(DbCluster.class); + when(reconciled.getDbClusterIdentifier()).thenReturn("mycluster"); + when(rdsService.modifyDbCluster(eq("mycluster"), any(), anyBoolean(), + any(), any(), any(), eq("us-east-1"))).thenReturn(reconciled); + + StackResource r = provisionUpdate("Cluster", "AWS::RDS::DBCluster", """ + {"DBClusterIdentifier":"mycluster","Engine":"aurora-postgresql", + "EngineMode":"provisioned","StorageEncrypted":false} + """, "mycluster"); + + assertEquals("CREATE_COMPLETE", r.getStatus()); + verify(rdsService, never()).deleteDbCluster(any(), any()); + verify(rdsService).modifyDbCluster("mycluster", null, false, null, null, null, "us-east-1"); + } + @Test void deleteDelegatesToRdsServiceForEachRdsType() { // Stack deletion tears down RDS resources via the physical id set at provision time.