fix(rds): apply Aurora IAM changes to existing members (#5608)

* fix(rds): apply Aurora IAM changes to existing members

* fix(rds): apply cluster settings to every stored member
This commit is contained in:
David Phillips authored and GitHub committed 2026-10-14 07:18:57 -04:00
1 parent 984e8187c9
commit 966da1d7fa
5 files changed
+278 -11

No files matched your search

@@ -0,0 +1,48 @@
package com.floci.test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.rds.RdsClient;
import static org.assertj.core.api.Assertions.assertThat;
class RdsClusterIamStateTest {
@Test
@DisplayName("Aurora IAM authentication changes reach existing members")
void clusterIamAuthenticationUpdatesMembers() {
String clusterId = TestFixtures.uniqueName("sdk-iam-cluster");
String memberId = TestFixtures.uniqueName("sdk-iam-member");
try (RdsClient rds = TestFixtures.rdsClient()) {
boolean clusterCreated = false;
boolean memberCreated = false;
try {
rds.createDBCluster(request -> request.dbClusterIdentifier(clusterId)
.engine("aurora-postgresql").masterUsername("admin")
.masterUserPassword("Secret12345").enableIAMDatabaseAuthentication(false));
clusterCreated = true;
rds.createDBInstance(request -> request.dbInstanceIdentifier(memberId)
.dbClusterIdentifier(clusterId).dbInstanceClass("db.r5.large")
.engine("aurora-postgresql"));
memberCreated = true;
for (boolean enabled : new boolean[] {true, false}) {
assertThat(rds.modifyDBCluster(request -> request.dbClusterIdentifier(clusterId)
.enableIAMDatabaseAuthentication(enabled).applyImmediately(true))
.dbCluster().iamDatabaseAuthenticationEnabled()).isEqualTo(enabled);
assertThat(rds.describeDBInstances(request -> request.dbInstanceIdentifier(memberId))
.dbInstances().get(0).iamDatabaseAuthenticationEnabled()).isEqualTo(enabled);
}
} finally {
try {
if (memberCreated) {
rds.deleteDBInstance(request -> request.dbInstanceIdentifier(memberId).skipFinalSnapshot(true));
}
} finally {
if (clusterCreated) {
rds.deleteDBCluster(request -> request.dbClusterIdentifier(clusterId).skipFinalSnapshot(true));
}
}
}
}
}
}
+6
View File
@@ -748,6 +748,12 @@ The RDS auth proxy validates the master username and password at the proxy layer
IAM database authentication is also supported. Set `--enable-iam-database-authentication` at instance creation time and use `aws rds generate-db-auth-token` to obtain a token.
Changing `EnableIAMDatabaseAuthentication` with `ModifyDBCluster` updates existing
Aurora members and applies to new connections through the cluster and member
endpoints. Omitting the flag preserves the current setting. Existing connections
are not disconnected.
A token can be signed with an access key or with temporary credentials: an assumed role's, or the execution role credentials Floci hands a Lambda function. A token signed with temporary credentials carries their session token in `X-Amz-Security-Token`, and it is accepted while that session is valid and only with the session token issued alongside the access key.
On RDS, a token is only good for the endpoint it was generated for: the hostname, port and region passed to `generate-db-auth-token` are the ones the instance (or cluster, or RDS Proxy) publishes. MySQL and MariaDB endpoints in Floci always refuse a token generated for another endpoint. PostgreSQL endpoints do the same by default; set `FLOCI_SERVICES_RDS_IAM_TOKEN_ENDPOINT_BINDING=false` to accept a token generated for another name, for clients that generate tokens for a container name or DNS alias the endpoint does not publish. Either way, the username must match the `DBUser` in the token exactly. Because clients on the host reach the same proxy over the loopback interface, a token generated for `localhost` or `127.0.0.1` on the published port is accepted as well. Clients on a Docker network connect by Floci's container name, so set `FLOCI_SERVICES_RDS_ENDPOINT_HOST` to that name (see [Docker Compose](#docker-compose)) so it is what the endpoint publishes and tokens are generated for. With [IAM enforcement](iam.md#iam-enforcement-mode) turned on, the token's principal must also be allowed `rds-db:connect` on the database user, scoped the way AWS scopes it:
@@ -86,6 +86,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -4665,23 +4666,34 @@ public class RdsService implements Resettable, ResourceProvider {
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);
}
updateClusterMembers(cluster, effectiveRegion,
member -> member.setMasterPassword(newPassword),
relayKey -> proxyManager.updateMasterPassword(relayKey, newPassword));
}
if (iamEnabled != null) {
proxyManager.updateIamEnabled(rdsResourceRelayKey(cluster.getDbClusterArn(), id), iamEnabled);
updateClusterMembers(cluster, effectiveRegion,
member -> member.setIamDatabaseAuthenticationEnabled(iamEnabled),
relayKey -> proxyManager.updateIamEnabled(relayKey, iamEnabled));
}
LOG.infov("DB cluster {0} modified", id);
return cluster;
}
private void updateClusterMembers(DbCluster cluster, String region,
Consumer<DbInstance> change, Consumer<String> updateProxy) {
String accountId = currentAccountId();
for (DbInstance member : listDbInstances(null, region)) {
if (Objects.equals(cluster.getDbClusterIdentifier(), member.getDbClusterIdentifier())) {
change.accept(member);
putInstanceForScope(accountId, region, member.getDbInstanceIdentifier(), member);
updateProxy.accept(rdsResourceRelayKey(member.getDbInstanceArn(), member.getDbInstanceIdentifier()));
}
}
}
public synchronized void deleteDbCluster(String id) {
deleteDbCluster(id, regionResolver.getDefaultRegion());
}
@@ -110,6 +110,79 @@ class RdsIamAuthLambdaSessionIntegrationTest {
}
}
@Test
void clusterIamToggleChangesAuthenticationAtExistingClusterAndMemberEndpoints() throws Exception {
String suffix = Long.toString(System.nanoTime(), 36);
String clusterId = "iam-cluster-" + suffix;
String memberId = "iam-member-" + suffix;
String accessKeyId = "ASIA" + suffix.toUpperCase();
try {
int clusterPort = rds("CreateDBCluster")
.formParam("DBClusterIdentifier", clusterId)
.formParam("Engine", "aurora-postgresql")
.formParam("MasterUsername", MASTER_USER)
.formParam("MasterUserPassword", MASTER_PASSWORD)
.formParam("DatabaseName", DATABASE)
.formParam("EnableIAMDatabaseAuthentication", "false")
.when().post("/").then().statusCode(200)
.extract().xmlPath()
.getInt("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.Port");
int memberPort = rds("CreateDBInstance")
.formParam("DBInstanceIdentifier", memberId)
.formParam("DBClusterIdentifier", clusterId)
.formParam("DBInstanceClass", "db.r5.large")
.formParam("Engine", "aurora-postgresql")
.formParam("MasterUsername", MASTER_USER)
.formParam("MasterUserPassword", MASTER_PASSWORD)
.formParam("DBName", DATABASE)
.formParam("AllocatedStorage", "20")
.when().post("/").then().statusCode(200)
.extract().xmlPath()
.getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port");
iamService.registerLambdaExecutionRoleSession(ACCOUNT_ID, accessKeyId, SESSION_SECRET, SESSION_TOKEN,
"arn:aws:iam::" + ACCOUNT_ID + ":role/cluster-connect-with-iam");
for (int port : new int[]{clusterPort, memberPort}) {
await().atMost(Duration.ofSeconds(60)).ignoreExceptions().until(() -> {
try (Connection connection = connect(port, MASTER_PASSWORD)) {
return connection.isValid(5);
}
});
String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900, SESSION_TOKEN);
assertThrows(SQLException.class, () -> connect(port, token).close());
}
for (boolean enabled : new boolean[]{true, false}) {
rds("ModifyDBCluster")
.formParam("DBClusterIdentifier", clusterId)
.formParam("EnableIAMDatabaseAuthentication", Boolean.toString(enabled))
.when().post("/").then().statusCode(200);
for (int port : new int[]{clusterPort, memberPort}) {
String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900, SESSION_TOKEN);
if (enabled) {
try (Connection connection = connect(port, token);
Statement statement = connection.createStatement();
ResultSet rows = statement.executeQuery("SELECT current_user")) {
assertTrue(rows.next());
assertEquals(MASTER_USER, rows.getString(1));
}
} else {
assertThrows(SQLException.class, () -> connect(port, token).close());
}
try (Connection connection = connect(port, MASTER_PASSWORD)) {
assertTrue(connection.isValid(5));
}
}
}
} finally {
iamService.unregisterSession(ACCOUNT_ID, accessKeyId);
rds("DeleteDBInstance").formParam("DBInstanceIdentifier", memberId)
.formParam("SkipFinalSnapshot", "true").when().post("/");
rds("DeleteDBCluster").formParam("DBClusterIdentifier", clusterId)
.formParam("SkipFinalSnapshot", "true").when().post("/");
}
}
private static Connection connect(int port, String password) throws SQLException {
Properties properties = new Properties();
properties.setProperty("user", MASTER_USER);
@@ -94,6 +94,7 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
class RdsServiceTest {
@@ -352,6 +353,108 @@ class RdsServiceTest {
}
}
@ParameterizedTest
@CsvSource({"aurora-postgresql,16.3,false", "aurora-mysql,8.0,false",
"aurora-postgresql,16.3,true", "aurora-mysql,8.0,true"})
void modifyClusterIamAuthenticationUpdatesExistingMembers(String engine, String version, boolean mockMode) {
when(rdsConfig.mock()).thenReturn(mockMode);
DbCluster cluster = rdsService.createDbCluster("iam-cluster", engine, version,
"admin", "password", "dbname", false, null);
DbInstance member = rdsService.createDbInstance("iam-member", engine, version,
"admin", "password", "dbname", "db.r5.large",
20, false, null, null, "iam-cluster", null, false, false, null,
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
cluster.getDbClusterMembers().add("missing-member");
clearInvocations(proxyManager);
for (boolean enabled : List.of(true, false)) {
rdsService.modifyDbCluster("iam-cluster", null, enabled);
assertEquals(enabled, rdsService.getDbCluster("iam-cluster").isIamDatabaseAuthenticationEnabled());
assertEquals(enabled, rdsService.getDbInstance("iam-member").isIamDatabaseAuthenticationEnabled());
verify(proxyManager).updateIamEnabled("rds-resource:" + cluster.getDbClusterArn(), enabled);
verify(proxyManager).updateIamEnabled("rds-resource:" + member.getDbInstanceArn(), enabled);
clearInvocations(proxyManager);
rdsService.modifyDbCluster("iam-cluster", null, null);
assertEquals(enabled, rdsService.getDbInstance("iam-member").isIamDatabaseAuthenticationEnabled());
verifyNoInteractions(proxyManager);
}
}
@ParameterizedTest
@CsvSource({"aurora-postgresql,16.3,false", "aurora-mysql,8.0,false",
"aurora-postgresql,16.3,true", "aurora-mysql,8.0,true"})
void modifyClusterIamAuthenticationFindsMembersMissingFromClusterList(
String engine, String version, boolean mockMode) {
when(rdsConfig.mock()).thenReturn(mockMode);
DbCluster cluster = rdsService.createDbCluster("iam-cluster", engine, version,
"admin", "password", "dbname", false, null);
DbInstance member = rdsService.createDbInstance("iam-member", engine, version,
"admin", "password", "dbname", "db.r5.large",
20, false, null, null, "iam-cluster", null, false, false, null,
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
rdsService.createDbInstance("standalone", engine, version,
"admin", "password", "dbname", "db.r5.large",
20, false, null, null, null);
cluster.getDbClusterMembers().clear();
clearInvocations(proxyManager);
for (boolean enabled : List.of(true, false)) {
rdsService.modifyDbCluster("iam-cluster", null, enabled);
assertEquals(enabled, rdsService.getDbInstance("iam-member").isIamDatabaseAuthenticationEnabled());
assertFalse(rdsService.getDbInstance("standalone").isIamDatabaseAuthenticationEnabled());
verify(proxyManager).updateIamEnabled("rds-resource:" + cluster.getDbClusterArn(), enabled);
verify(proxyManager).updateIamEnabled("rds-resource:" + member.getDbInstanceArn(), enabled);
verifyNoMoreInteractions(proxyManager);
clearInvocations(proxyManager);
}
}
@Test
void modifyClusterIamAuthenticationKeepsOtherAccountsAndRegionsUnchanged() {
InMemoryStorage<String, DbInstance> rawInstances = new InMemoryStorage<>();
InMemoryStorage<String, DbCluster> rawClusters = new InMemoryStorage<>();
String accountA = "111111111111";
String accountB = "222222222222";
RdsService serviceA = new RdsService(containerManager, proxyManager, ec2Service,
new RegionResolver("us-east-1", accountA), config,
new AccountAwareStorageBackend<>(rawInstances, null, accountA),
new AccountAwareStorageBackend<>(rawClusters, null, accountA),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
RdsService serviceB = new RdsService(containerManager, proxyManager, ec2Service,
new RegionResolver("us-east-1", accountB), config,
new AccountAwareStorageBackend<>(rawInstances, null, accountB),
new AccountAwareStorageBackend<>(rawClusters, null, accountB),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
for (RdsService service : List.of(serviceA, serviceB)) {
for (String region : List.of("us-east-1", "us-west-2")) {
service.createDbCluster("shared-cluster", "aurora-postgresql", "16.3",
"admin", "password", "dbname", false, null, null, null, false, region);
service.createDbInstance("shared-member", "aurora-postgresql", "16.3",
"admin", "password", "dbname", "db.r5.large", 20, false, null, null,
"shared-cluster", null, false, false, null, Map.of(), List.of(), region);
}
}
clearInvocations(proxyManager);
serviceA.modifyDbCluster("shared-cluster", null, true, "us-east-1");
for (String account : List.of(accountA, accountB)) {
for (String region : List.of("us-east-1", "us-west-2")) {
boolean enabled = account.equals(accountA) && region.equals("us-east-1");
DbCluster cluster = rawClusters.get(account + "/" + region + "::shared-cluster").orElseThrow();
DbInstance member = rawInstances.get(account + "/" + region + "::shared-member").orElseThrow();
assertEquals(enabled, cluster.isIamDatabaseAuthenticationEnabled());
assertEquals(enabled, member.isIamDatabaseAuthenticationEnabled());
verify(proxyManager, times(enabled ? 1 : 0))
.updateIamEnabled("rds-resource:" + cluster.getDbClusterArn(), true);
verify(proxyManager, times(enabled ? 1 : 0))
.updateIamEnabled("rds-resource:" + member.getDbInstanceArn(), true);
}
}
}
@Test
void createDbInstanceLeavesPostgresIamTokensUnboundWhenTheBindingIsTurnedOff() {
when(rdsConfig.iamTokenEndpointBinding()).thenReturn(false);
@@ -2062,6 +2165,31 @@ class RdsServiceTest {
verify(proxyManager, never()).stopProxy(anyString());
}
@Test
void modifyDbClusterUpdatesPasswordAndIamTogetherForExistingMembers() {
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");
cluster.getDbClusterMembers().add("missing-member");
cluster.getDbClusterMembers().clear();
clearInvocations(proxyManager);
rdsService.modifyDbCluster("cluster1", "rotated-password", true);
DbInstance stored = rdsService.getDbInstance("member-1");
assertEquals("rotated-password", stored.getMasterPassword());
assertTrue(stored.isIamDatabaseAuthenticationEnabled());
verify(proxyManager).updateMasterPassword("rds-resource:" + cluster.getDbClusterArn(), "rotated-password");
verify(proxyManager).updateMasterPassword("rds-resource:" + member.getDbInstanceArn(), "rotated-password");
verify(proxyManager).updateIamEnabled("rds-resource:" + cluster.getDbClusterArn(), true);
verify(proxyManager).updateIamEnabled("rds-resource:" + member.getDbInstanceArn(), true);
verifyNoMoreInteractions(proxyManager);
}
@Test
void modifyDbClusterSamePasswordDoesNotTouchBackendOrProxy() {
DbCluster cluster = rdsService.createDbCluster("cluster1", "aurora-postgresql", "16.3",