fix(rds): propagate ModifyDBInstance master-password rotations to the backend and proxy (#2709)

* fix(rds): propagate ModifyDBInstance master-password rotations to the backend and proxy

Rotating the master password only updated Floci's stored state. The
backend DB container kept the original credential and the running auth
proxy kept validating the master scramble against the password it was
started with, so after a rotation no client could connect at all: the
old password failed the proxy's (rotated) validator and the new one
failed against the backend.

On rotation, while the old credential is still known:
- exec into the DB container and change the password there. MySQL and
  MariaDB run SET PASSWORD as the master user itself with the old
  password (changing your own password needs no privileges, and root's
  password is the creation-time one, unknown after earlier rotations).
  PostgreSQL runs ALTER ROLE over the trusted local socket.
- restart the auth proxy on the rotated credential, the same stop/start
  the reboot path uses.

Mock mode, cluster members and instances without a running container
are unaffected.

* fix(rds): swap the proxy password snapshot in place instead of restarting it

An immediate stop/start of the auth proxy races the listener rebind: closing
the server socket defers the port release while relay connections are still
draining, so the restart failed with EADDRINUSE and ModifyDBInstance threw
(caught by the sdk-test-java RdsJdbcCompatTest). Updating the snapshot on the
running proxy avoids the race entirely and keeps live connections open, which
is also what rotation should do.
This commit is contained in:
Matan Baruch authored and GitHub committed 2026-08-29 14:43:07 -04:00
1 parent 88bf0a2931
commit 09a05c2d77
7 files changed
+195 -1

No files matched your search

@@ -58,6 +58,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
@@ -738,6 +739,56 @@ class RdsServiceTest {
assertFalse(modified.isIamDatabaseAuthenticationEnabled());
}
@Test
void modifyDbInstancePasswordRotationPropagatesToBackendAndProxy() {
DbInstance instance = rdsService.createDbInstance("mydb", "mysql", "8.0",
"admin", "original-password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
instance.setContainerId("container-1");
instance.setContainerHost("10.0.0.5");
instance.setContainerPort(3306);
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",
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).
verify(proxyManager).updateMasterPassword(anyString(), eq("rotated-password"));
verify(proxyManager, never()).stopProxy(anyString());
}
@Test
void modifyDbInstanceSamePasswordDoesNotTouchBackendOrProxy() {
DbInstance instance = rdsService.createDbInstance("mydb", "mysql", "8.0",
"admin", "original-password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
instance.setContainerId("container-1");
rdsService.modifyDbInstance("mydb", "original-password", null, null);
verify(containerManager, never()).rotateMasterPassword(
anyString(), anyString(), any(), anyString(), anyString(), anyString());
verify(proxyManager, never()).updateMasterPassword(anyString(), anyString());
}
@Test
void modifyDbInstancePasswordRotationSkipsBackendInMockMode() {
when(config.services().rds().mock()).thenReturn(true);
rdsService.createDbInstance("mydb", "mysql", "8.0",
"admin", "original-password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
DbInstance modified = rdsService.modifyDbInstance("mydb", "rotated-password", null, 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",
@@ -73,6 +73,36 @@ class RdsContainerManagerTest {
RdsContainerManager.mysqlMasterGrantSql("we'ird\\"));
}
@Test
void passwordRotationCommandRunsAsTheMasterUserWithTheOldPassword() {
String[] mysql = RdsContainerManager.passwordRotationCommand(
DatabaseEngine.MYSQL, "admin", "old-pass", "new-pass");
assertEquals("mysql", mysql[0]);
assertEquals("-uadmin", mysql[1]);
assertEquals("-pold-pass", mysql[2]);
assertEquals("SET PASSWORD = 'new-pass';", mysql[4]);
assertEquals("mariadb", RdsContainerManager.passwordRotationCommand(
DatabaseEngine.MARIADB, "admin", "old-pass", "new-pass")[0]);
// PostgreSQL: local socket connections are trusted, so no old password appears at all.
String[] postgres = RdsContainerManager.passwordRotationCommand(
DatabaseEngine.POSTGRES, "admin", "old-pass", "new-pass");
assertEquals("psql", postgres[0]);
assertEquals("ALTER ROLE \"admin\" WITH PASSWORD 'new-pass';", postgres[postgres.length - 1]);
}
@Test
void passwordRotationSqlUsesEngineSyntaxAndEscapes() {
assertEquals("SET PASSWORD = 'a\\'b';",
RdsContainerManager.mysqlPasswordRotationSql(DatabaseEngine.MYSQL, "a'b"));
// MariaDB only accepts the PASSWORD() form.
assertEquals("SET PASSWORD = PASSWORD('a\\'b');",
RdsContainerManager.mysqlPasswordRotationSql(DatabaseEngine.MARIADB, "a'b"));
assertEquals("ALTER ROLE \"we\"\"ird\" WITH PASSWORD 'a''b';",
RdsContainerManager.postgresPasswordRotationSql("we\"ird", "a'b"));
}
@Test
void needsMasterGrantSkipsRootAndPostgres() {
assertTrue(RdsContainerManager.needsMasterGrant(DatabaseEngine.MYSQL, "admin"));
@@ -10,6 +10,7 @@ import java.net.ServerSocket;
import java.util.concurrent.ConcurrentHashMap;
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.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -74,6 +75,31 @@ class RdsProxyManagerTest {
}
}
@Test
void updateMasterPasswordSwapsTheRunningProxySnapshotWithoutARestart() throws Exception {
RdsProxyManager manager = new RdsProxyManager(
mock(RdsSigV4Validator.class), mock(RdsProxyTlsCertificates.class));
int proxyPort = availablePort();
try {
start(manager, "proxy", proxyPort);
manager.updateMasterPassword("proxy", "rotated");
assertEquals("rotated", masterPassword(registry(manager).get("proxy")));
assertPortUnavailable(proxyPort);
} finally {
manager.stopAll();
}
}
@Test
void updateMasterPasswordForUnknownInstanceIsANoOp() {
RdsProxyManager manager = new RdsProxyManager(
mock(RdsSigV4Validator.class), mock(RdsProxyTlsCertificates.class));
assertDoesNotThrow(() -> manager.updateMasterPassword("missing", "rotated"));
}
@Test
void stopAllReleasesEveryListenerAndIsIdempotent() throws IOException {
RdsProxyManager manager = new RdsProxyManager(
@@ -159,6 +185,12 @@ class RdsProxyManagerTest {
mock(RdsProxyTlsCertificates.class), (user, password) -> true);
}
private static String masterPassword(RdsAuthProxy proxy) throws Exception {
Field field = RdsAuthProxy.class.getDeclaredField("masterPassword");
field.setAccessible(true);
return (String) field.get(proxy);
}
private static void setServerSocket(RdsAuthProxy proxy, ServerSocket socket) throws Exception {
Field field = RdsAuthProxy.class.getDeclaredField("serverSocket");
field.setAccessible(true);