fix(rds): inherit Aurora member master credentials (#5667)

* fix(rds): inherit Aurora member master credentials

* fix(rds): retain defaults for incomplete cluster credentials

* test(rds): keep credential checks compatible with Java 17

* fix(rds): keep Aurora managed secrets on the cluster

* fix(rds): reject member credentials and publish offline changes

* fix(rds): match the member username error message

* fix(rds): release proxy listeners before reboot

* test(rds): shorten listener retirement failure checks
This commit is contained in:
David Phillips authored and GitHub committed 2026-10-14 19:31:42 -04:00
1 parent 1883855c94
commit 92b2cf74f7
10 files changed
+863 -75

No files matched your search

@@ -702,6 +702,19 @@ public class RdsService implements Resettable, ResourceProvider {
"DB instance " + id + " already exists.", 400);
}
if (dbClusterIdentifier != null && !dbClusterIdentifier.isBlank()) {
if (masterPassword != null) {
throw new AwsException("InvalidParameterCombination",
"The requested DB Instance will be a member of a DB Cluster. "
+ "Set master user password for the DB Cluster.", 400);
}
if (masterUsername != null) {
throw new AwsException("InvalidParameterCombination",
"The requested DB Instance will be a member of a DB Cluster. "
+ "Set master user name for the DB Cluster.", 400);
}
}
DatabaseEngine engine = resolveEngine(engineParam);
if (engine == DatabaseEngine.SQLSERVER && dbName != null && !dbName.isBlank()) {
throw new AwsException("InvalidParameterCombination",
@@ -726,10 +739,12 @@ public class RdsService implements Resettable, ResourceProvider {
throw new AwsException("InvalidParameterValue",
"MasterUsername must begin with a letter and contain only alphanumeric characters or underscores.", 400);
}
if (manageMasterUserPassword && (masterPassword == null || masterPassword.isBlank())) {
boolean manageInstancePassword = manageMasterUserPassword
&& (dbClusterIdentifier == null || dbClusterIdentifier.isBlank());
if (manageInstancePassword && (masterPassword == null || masterPassword.isBlank())) {
masterPassword = generatedMasterPassword();
}
String effectiveSecretKmsKeyId = manageMasterUserPassword
String effectiveSecretKmsKeyId = manageInstancePassword
? resolveManagedSecretKmsKeyId(masterUserSecretKmsKeyId, effectiveRegion) : null;
// Always reserve a unique port (even in mock) so endpoints stay distinct and usedPorts
// is consistent; mock mode only skips starting the container and auth proxy.
@@ -860,9 +875,16 @@ public class RdsService implements Resettable, ResourceProvider {
instance.setContainerPort(backendPort);
iamEnabled = currentCluster.isIamDatabaseAuthenticationEnabled();
instance.setIamDatabaseAuthenticationEnabled(iamEnabled);
// Incomplete cluster records retain the member's existing defaults for missing credentials.
if (currentCluster.getMasterUsername() != null) {
instance.setMasterUsername(currentCluster.getMasterUsername());
}
if (currentCluster.getMasterPassword() != null) {
instance.setMasterPassword(currentCluster.getMasterPassword());
}
}
}
if (manageMasterUserPassword) {
if (manageInstancePassword) {
attachManagedMasterUserSecret(instance, effectiveRegion, masterUserSecretKmsKeyId, effectiveSecretKmsKeyId);
}
putInstanceForScope(accountId, effectiveRegion, id, instance);
@@ -873,7 +895,7 @@ public class RdsService implements Resettable, ResourceProvider {
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
engine, iamEnabled, proxyPort, backendHost, backendPort,
instance.getEndpoint().address(),
masterUsername, masterPassword, dbName,
instance.getMasterUsername(), instance.getMasterPassword(), dbName,
(user, pw) -> validateDbPasswordForScope(
accountId, instanceRegion, id, user, pw),
proxyBinding(engine, instance.getEndpoint().address(), instance.getEndpoint().port(),
@@ -4753,8 +4775,9 @@ public class RdsService implements Resettable, ResourceProvider {
} else {
changeCredentials.run();
}
boolean passwordRotated = credentialsChanged && rotateBackend;
if (credentialsChanged && hasNewPassword) {
boolean passwordAccepted = credentialsChanged && hasNewPassword;
boolean passwordChanged = passwordAccepted && !newPassword.equals(oldPassword);
if (passwordAccepted) {
cluster.setMasterPassword(newPassword);
}
if (iamEnabled != null) {
@@ -4786,11 +4809,9 @@ public class RdsService implements Resettable, ResourceProvider {
applyAutoPause(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) {
// Accepted credentials belong to every endpoint even before a backend is provisioned.
// Running backends have already accepted the rotation before metadata is published.
if (passwordChanged) {
proxyManager.updateMasterPassword(
rdsResourceRelayKey(cluster.getDbClusterArn(), id), newPassword);
updateClusterMembers(cluster, effectiveRegion,
@@ -8,6 +8,7 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.Duration;
import java.util.concurrent.Semaphore;
/**
@@ -39,6 +40,8 @@ public class RdsAuthProxy {
private volatile boolean running;
private ServerSocket serverSocket;
private Thread acceptThread;
Duration acceptRetirementTimeout = Duration.ofSeconds(5);
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
DatabaseEngine engine, boolean iamEnabled,
@@ -99,7 +102,8 @@ public class RdsAuthProxy {
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(proxyPort));
running = true;
Thread.ofVirtual().name("rds-proxy-accept-" + instanceId).start(this::acceptLoop);
acceptThread = Thread.ofVirtual().name("rds-proxy-accept-" + instanceId).unstarted(this::acceptLoop);
acceptThread.start();
LOG.infov("RDS proxy started for instance {0} on port {1} → {2}:{3}",
instanceId, String.valueOf(proxyPort), backendHost, String.valueOf(backendPort));
}
@@ -125,12 +129,29 @@ public class RdsAuthProxy {
throw new RuntimeException(
"Failed to stop RDS proxy for instance " + instanceId, e);
}
// A socket closed during accept can retain its native descriptor until accept exits.
// Retire that loop before a reboot binds its replacement to the same port.
Thread accepting = acceptThread;
if (accepting != null && accepting != Thread.currentThread() && accepting.isAlive()) {
try {
if (!accepting.join(acceptRetirementTimeout)) {
throw new IllegalStateException("RDS proxy accept loop did not stop for instance " + instanceId);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted while stopping RDS proxy for instance " + instanceId, e);
}
}
}
private void acceptLoop() {
while (running) {
try {
Socket client = serverSocket.accept();
if (!running) {
closeQuietly(client);
break;
}
if (!connectionPermits.tryAcquire()) {
LOG.warnv("Refusing RDS connection for instance {0}: connection limit reached",
instanceId);