From 895b59f1eeaf1824de77578469b6520f6a72a2b2 Mon Sep 17 00:00:00 2001 From: Matan Baruch <36934912+matanbaruch@users.noreply.github.com> Date: Sun, 30 Aug 2026 06:26:26 +0300 Subject: [PATCH] fix(rds): propagate ModifyDBCluster master-password rotations to the backend and every proxy (#2727) * fix(rds): propagate ModifyDBCluster master-password rotations to the backend and every proxy ModifyDBCluster only updated the stored password: the backend container kept the original credential and the cluster's and every member endpoint's auth proxy held stale start-time snapshots, so after a rotation no endpoint accepted the new password. Reuse the ModifyDBInstance mechanism: exec the change into the backend while the old credential is still known, then swap the password snapshot in place on the cluster proxy and each member proxy, updating the members' stored passwords to match (AWS applies the cluster master password to every member endpoint). Also pass the Docker container name instead of the instance id into execUntilSuccess from rotateMasterPassword, matching the log convention of the other call sites (review follow-up from #2709). * chore: retrigger CI after main repair --- .../floci/services/rds/RdsService.java | 39 ++++++++++++- .../rds/container/RdsContainerManager.java | 16 +++--- .../floci/services/rds/RdsServiceTest.java | 56 ++++++++++++++++++- 3 files changed, 100 insertions(+), 11 deletions(-) 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 7c0f05070f..136a30a8b7 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 @@ -1089,7 +1089,8 @@ public class RdsService implements Resettable, ResourceProvider { // holds) is known. Without it every later connection fails: the proxy dials the // backend with the rotated password against a database still holding the original. if (passwordRotated) { - containerManager.rotateMasterPassword(id, instance.getContainerId(), + containerManager.rotateMasterPassword( + instance.getDockerVolumeName(), instance.getContainerId(), instance.getEngine(), instance.getMasterUsername(), oldPassword, newPassword); } instance.setMasterPassword(newPassword); @@ -1648,7 +1649,7 @@ public class RdsService implements Resettable, ResourceProvider { return modifyDbCluster(id, newPassword, iamEnabled, null, null, null, region); } - public DbCluster modifyDbCluster(String id, String newPassword, Boolean iamEnabled, + public synchronized DbCluster modifyDbCluster(String id, String newPassword, Boolean iamEnabled, Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity, Integer serverlessV2SecondsUntilAutoPause, String region) { String effectiveRegion = effectiveRegion(region); @@ -1677,7 +1678,20 @@ public class RdsService implements Resettable, ResourceProvider { effectiveAutoPauseSeconds = validateServerlessV2ScalingConfiguration( effectiveMinCapacity, effectiveMaxCapacity, requestedOrExistingAutoPause); } + boolean passwordRotated = false; if (newPassword != null && !newPassword.isBlank()) { + String oldPassword = cluster.getMasterPassword(); + boolean backendRunning = !config.services().rds().mock() + && cluster.getContainerId() != null; + passwordRotated = backendRunning && !newPassword.equals(oldPassword); + // Propagate the rotation into the running backend DB before overwriting the stored + // password — the last moment the old credential (which the backend still holds) is + // known — mirroring the ModifyDBInstance path. + if (passwordRotated) { + containerManager.rotateMasterPassword( + cluster.getDockerVolumeName(), cluster.getContainerId(), + cluster.getEngine(), cluster.getMasterUsername(), oldPassword, newPassword); + } cluster.setMasterPassword(newPassword); } if (iamEnabled != null) { @@ -1689,6 +1703,27 @@ public class RdsService implements Resettable, ResourceProvider { cluster.setServerlessV2SecondsUntilAutoPause(effectiveAutoPauseSeconds); } putClusterForScope(currentAccountId(), effectiveRegion, id, cluster); + + // A cluster rotation applies to every endpoint: the cluster's own proxy and each member + // instance's proxy hold start-time password snapshots, and member endpoints validate + // against the member's stored password, so propagate the rotated credential to all of + // them (AWS applies the cluster master password to every member endpoint). + if (passwordRotated) { + proxyManager.updateMasterPassword( + rdsResourceRelayKey(cluster.getDbClusterArn(), id), newPassword); + for (String memberId : cluster.getDbClusterMembers()) { + DbInstance member = findInstanceForScope( + currentAccountId(), effectiveRegion, memberId); + if (member == null) { + continue; + } + member.setMasterPassword(newPassword); + putInstanceForScope(currentAccountId(), effectiveRegion, memberId, member); + proxyManager.updateMasterPassword( + rdsResourceRelayKey(member.getDbInstanceArn(), memberId), newPassword); + } + } + LOG.infov("DB cluster {0} modified", id); return cluster; } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java index 94db342b20..b0d6b244d3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java @@ -408,16 +408,16 @@ public class RdsContainerManager { } /** - * Propagates a ModifyDBInstance master-password rotation into the running DB container, so - * the backend's stored credential matches what clients (and the auth proxy) use from now on. - * For MySQL/MariaDB this runs as the master user itself with the old password — changing your - * own password needs no extra privileges, and the container's root password is the - * creation-time one, not reliably known after earlier rotations. PostgreSQL's official image - * trusts local socket connections, so psql needs no password at all. + * Propagates a ModifyDBInstance/ModifyDBCluster master-password rotation into the running DB + * container, so the backend's stored credential matches what clients (and the auth proxy) use + * from now on. For MySQL/MariaDB this runs as the master user itself with the old password — + * changing your own password needs no extra privileges, and the container's root password is + * the creation-time one, not reliably known after earlier rotations. PostgreSQL's official + * image trusts local socket connections, so psql needs no password at all. */ - public void rotateMasterPassword(String instanceId, String containerId, DatabaseEngine engine, + public void rotateMasterPassword(String containerName, String containerId, DatabaseEngine engine, String masterUsername, String oldPassword, String newPassword) { - execUntilSuccess(instanceId, containerId, + execUntilSuccess(containerName, containerId, passwordRotationCommand(engine, masterUsername, oldPassword, newPassword), "master-password rotation"); } diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java index 18264fe734..1212f54bd8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java @@ -747,12 +747,13 @@ class RdsServiceTest { instance.setContainerId("container-1"); instance.setContainerHost("10.0.0.5"); instance.setContainerPort(3306); + instance.setDockerVolumeName("volume-1"); DbInstance modified = rdsService.modifyDbInstance("mydb", "rotated-password", null, null); assertEquals("rotated-password", modified.getMasterPassword()); // The backend learns the new credential while the old one is still known... - verify(containerManager).rotateMasterPassword("mydb", "container-1", + verify(containerManager).rotateMasterPassword("volume-1", "container-1", DatabaseEngine.MYSQL, "admin", "original-password", "rotated-password"); // ...and the running proxy's password snapshot is swapped in place, without a restart // (a stop/start would race the listener rebind and drop live connections). @@ -789,6 +790,59 @@ class RdsServiceTest { verify(proxyManager, never()).updateMasterPassword(anyString(), anyString()); } + @Test + void modifyDbClusterPasswordRotationPropagatesToBackendAndEveryProxy() { + DbCluster cluster = rdsService.createDbCluster("cluster1", "aurora-postgresql", "16.3", + "admin", "original-password", "dbname", false, null); + cluster.setContainerId("container-1"); + cluster.setDockerVolumeName("volume-1"); + DbInstance member = rdsService.createDbInstance("member-1", "aurora-postgresql", "16.3", + "admin", "original-password", "dbname", "db.r5.large", + 20, false, null, null, "cluster1"); + + DbCluster modified = rdsService.modifyDbCluster("cluster1", "rotated-password", null); + + assertEquals("rotated-password", modified.getMasterPassword()); + // The backend learns the new credential while the old one is still known... + verify(containerManager).rotateMasterPassword("volume-1", "container-1", + DatabaseEngine.POSTGRES, "admin", "original-password", "rotated-password"); + // ...and the cluster proxy AND every member endpoint's proxy swap their snapshots, + // with the member's stored password updated so its validator accepts the new one. + verify(proxyManager).updateMasterPassword( + eq("rds-resource:" + cluster.getDbClusterArn()), eq("rotated-password")); + verify(proxyManager).updateMasterPassword( + eq("rds-resource:" + member.getDbInstanceArn()), eq("rotated-password")); + assertEquals("rotated-password", member.getMasterPassword()); + verify(proxyManager, never()).stopProxy(anyString()); + } + + @Test + void modifyDbClusterSamePasswordDoesNotTouchBackendOrProxy() { + DbCluster cluster = rdsService.createDbCluster("cluster1", "aurora-postgresql", "16.3", + "admin", "original-password", "dbname", false, null); + cluster.setContainerId("container-1"); + + rdsService.modifyDbCluster("cluster1", "original-password", null); + + verify(containerManager, never()).rotateMasterPassword( + anyString(), anyString(), any(), anyString(), anyString(), anyString()); + verify(proxyManager, never()).updateMasterPassword(anyString(), anyString()); + } + + @Test + void modifyDbClusterPasswordRotationSkipsBackendInMockMode() { + when(config.services().rds().mock()).thenReturn(true); + rdsService.createDbCluster("cluster1", "aurora-postgresql", "16.3", + "admin", "original-password", "dbname", false, null); + + DbCluster modified = rdsService.modifyDbCluster("cluster1", "rotated-password", null); + + assertEquals("rotated-password", modified.getMasterPassword()); + verify(containerManager, never()).rotateMasterPassword( + anyString(), anyString(), any(), anyString(), anyString(), anyString()); + verify(proxyManager, never()).updateMasterPassword(anyString(), anyString()); + } + @Test void modifyDbInstanceCanToggleIamWithoutChangingPassword() { rdsService.createDbInstance("mydb", "postgres", "13",