diff --git a/docs/services/rds.md b/docs/services/rds.md index 2d824b98c9..9b96f3cd6f 100644 --- a/docs/services/rds.md +++ b/docs/services/rds.md @@ -756,7 +756,7 @@ On RDS, a token is only good for the endpoint it was generated for: the hostname arn:aws:rds-db:::dbuser:/ ``` -Aurora clusters use the `DbClusterResourceId`, and connections through an RDS Proxy use the proxy's `prx-…` resource id. A token that fails any of these checks is refused with the engine's ordinary authentication failure; the reason is written to Floci's log. +Aurora cluster endpoints and their member instance endpoints use the cluster's `DbClusterResourceId`, including after member reboot or runtime recovery, and connections through an RDS Proxy use the proxy's `prx-…` resource id. A token that fails any of these checks is refused with the engine's ordinary authentication failure; the reason is written to Floci's log. On PostgreSQL, the token names a database role (`DBUser`) and the session runs as that role: `current_user` and `session_user` both report it, objects it creates are owned by it, and a token naming a role the database does not have is refused with `FATAL: role "..." does not exist`. Create the role first with `CREATE ROLE WITH LOGIN` as the master user, and grant it whatever the application needs. 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 33d399156c..a2fe670bf5 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 @@ -847,7 +847,7 @@ public class RdsService implements Resettable, ResourceProvider { (user, pw) -> validateDbPasswordForScope( accountId, instanceRegion, id, user, pw), proxyBinding(engine, instance.getEndpoint().address(), instance.getEndpoint().port(), - instanceRegion, accountId, instance.getDbiResourceId())); + instanceRegion, accountId, dbUserResourceId(instance))); } catch (RuntimeException | Error e) { try { deleteInstanceForScope(accountId, effectiveRegion, id); @@ -3610,7 +3610,7 @@ public class RdsService implements Resettable, ResourceProvider { (user, pw) -> validateDbPasswordForScope( accountId, instanceRegion, id, user, pw), proxyBinding(instance.getEngine(), instance.getEndpoint().address(), - instance.getEndpoint().port(), instanceRegion, accountId, instance.getDbiResourceId())); + instance.getEndpoint().port(), instanceRegion, accountId, dbUserResourceId(instance))); } else { // No backing container: created or last rebooted while no daemon was reachable. instance = ensureInstanceBackend(id, effectiveRegion); @@ -3621,6 +3621,18 @@ public class RdsService implements Resettable, ResourceProvider { return instance; } + private String dbUserResourceId(DbInstance instance) { + String clusterId = instance.getDbClusterIdentifier(); + if (clusterId != null && !clusterId.isBlank()) { + DbCluster cluster = findClusterForScope(accountIdFromArn(instance.getDbInstanceArn()), + regionFromArn(instance.getDbInstanceArn()), clusterId); + if (cluster != null) { + return cluster.getDbClusterResourceId(); + } + } + return instance.getDbiResourceId(); + } + /** * The binding the proxy validates IAM auth tokens against. As on RDS a token is good for the * endpoint it was generated for (hostname, port and region); MySQL and MariaDB proxies always @@ -3718,7 +3730,7 @@ public class RdsService implements Resettable, ResourceProvider { (user, pw) -> validateDbPasswordForScope( accountId, instanceRegion, id, user, pw), proxyBinding(instance.getEngine(), relayEndpoint.address(), - relayEndpoint.port(), instanceRegion, accountId, instance.getDbiResourceId())); + relayEndpoint.port(), instanceRegion, accountId, dbUserResourceId(instance))); } catch (RuntimeException e) { if (allocatedRelayPort) { releaseProxyPort(relayPort); @@ -7954,7 +7966,7 @@ public class RdsService implements Resettable, ResourceProvider { accountId, instanceRegion, instance.getDbInstanceIdentifier(), user, pw), proxyBinding(instance.getEngine(), instance.getEndpoint().address(), instance.getEndpoint().port(), - instanceRegion, accountId, instance.getDbiResourceId())); + instanceRegion, accountId, dbUserResourceId(instance))); } instance.setStatus(DbInstanceStatus.AVAILABLE); putInstanceForScope(accountId, instanceRegion, diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsMemberIamResourceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsMemberIamResourceIntegrationTest.java new file mode 100644 index 0000000000..e14139ac39 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsMemberIamResourceIntegrationTest.java @@ -0,0 +1,160 @@ +package io.github.hectorvent.floci.services.rds; + +import com.github.dockerjava.api.DockerClient; +import io.github.hectorvent.floci.services.iam.IamService; +import io.github.hectorvent.floci.testing.IamEnforcementProfile; +import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.path.xml.XmlPath; +import jakarta.inject.Inject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.time.Duration; +import java.time.Instant; +import java.util.Map; +import java.util.Properties; + +import static io.github.hectorvent.floci.testing.RdsRequests.rds; +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +/** Cluster-scoped IAM policies must work on an Aurora member endpoint. */ +@QuarkusTest +@Tag("docker") +@TestProfile(IamEnforcementProfile.class) +class RdsMemberIamResourceIntegrationTest { + + private static final String ACCOUNT_ID = "000000000000"; + private static final String MASTER_USER = "master"; + private static final String MASTER_PASSWORD = "Master123!"; + private static final String DATABASE = "appdb"; + private static final String SESSION_SECRET = "lambda+session/secret=key"; + private static final String SESSION_TOKEN = "IQoJb3JpZ2luX2VjEJr+abc/def+ghi/jkl%2Bmno=="; + + @Inject + DockerClient dockerClient; + + @Inject + IamService iamService; + + @BeforeEach + void checkDocker() { + try { + dockerClient.pingCmd().exec(); + } catch (Exception e) { + assumeTrue(false, "Docker is not available: " + e.getMessage()); + } + } + + @Test + void memberEndpointUsesClusterPolicyBeforeAndAfterReboot() throws Exception { + String suffix = Long.toString(System.nanoTime(), 36); + String clusterId = "iam-cluster-" + suffix; + String memberId = "iam-member-" + suffix; + String roleName = "iam-connect-" + suffix; + String accessKeyId = "ASIA" + suffix.toUpperCase(); + boolean roleCreated = false; + boolean policyCreated = false; + try { + String resourceId = rds("CreateDBCluster") + .formParam("DBClusterIdentifier", clusterId) + .formParam("Engine", "aurora-postgresql") + .formParam("MasterUsername", MASTER_USER) + .formParam("MasterUserPassword", MASTER_PASSWORD) + .formParam("DatabaseName", DATABASE) + .formParam("EnableIAMDatabaseAuthentication", "true") + .when().post("/").then().statusCode(200).extract().xmlPath() + .getString("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.DbClusterResourceId"); + // Explicit matching credentials and IAM flag isolate the resource binding from + // the separate member credential/flag inheritance gaps. + XmlPath member = rds("CreateDBInstance") + .formParam("DBInstanceIdentifier", memberId) + .formParam("DBClusterIdentifier", clusterId) + .formParam("DBInstanceClass", "db.serverless") + .formParam("Engine", "aurora-postgresql") + .formParam("MasterUsername", MASTER_USER) + .formParam("MasterUserPassword", MASTER_PASSWORD) + .formParam("DBName", DATABASE) + .formParam("EnableIAMDatabaseAuthentication", "true") + .when().post("/").then().statusCode(200).extract().xmlPath(); + int port = member.getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port"); + String instanceResourceId = member.getString( + "CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.DbiResourceId"); + String roleArn = iamService.createRole(roleName, "/", """ + {"Version":"2012-10-17","Statement":[{"Effect":"Allow", + "Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]} + """, null, 3600, Map.of()).getArn(); + roleCreated = true; + iamService.registerLambdaExecutionRoleSession(ACCOUNT_ID, accessKeyId, + SESSION_SECRET, SESSION_TOKEN, roleArn); + 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); + iamService.putRolePolicy(roleName, "connect", connectPolicy(resourceId)); + policyCreated = true; + assertIamLogin(port, token); + rds("RebootDBInstance").formParam("DBInstanceIdentifier", memberId) + .when().post("/").then().statusCode(200); + assertIamLogin(port, token); + // A member's DbiResourceId must not grant cluster access. + iamService.putRolePolicy(roleName, "connect", connectPolicy(instanceResourceId)); + SQLException refused = assertThrows(SQLException.class, () -> connect(port, token).close()); + assertTrue(refused.getMessage().contains("password authentication failed"), refused.getMessage()); + iamService.putRolePolicy(roleName, "connect", connectPolicy(resourceId)); + assertIamLogin(port, token); + } finally { + iamService.unregisterSession(ACCOUNT_ID, accessKeyId); + if (policyCreated) { + iamService.deleteRolePolicy(roleName, "connect"); + } + if (roleCreated) { + iamService.deleteRole(roleName); + } + rds("DeleteDBInstance").formParam("DBInstanceIdentifier", memberId) + .formParam("SkipFinalSnapshot", "true").when().post("/"); + rds("DeleteDBCluster").formParam("DBClusterIdentifier", clusterId) + .formParam("SkipFinalSnapshot", "true").when().post("/"); + } + } + + private static String connectPolicy(String resourceId) { + return """ + {"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"rds-db:connect", + "Resource":"arn:aws:rds-db:us-east-1:%s:dbuser:%s/%s"}]} + """.formatted(ACCOUNT_ID, resourceId, MASTER_USER); + } + + private static void assertIamLogin(int port, String token) throws SQLException { + 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)); + } + } + + private static Connection connect(int port, String password) throws SQLException { + Properties properties = new Properties(); + properties.setProperty("user", MASTER_USER); + properties.setProperty("password", password); + properties.setProperty("sslmode", "disable"); + properties.setProperty("connectTimeout", "5"); + return DriverManager.getConnection("jdbc:postgresql://127.0.0.1:" + port + "/" + DATABASE, properties); + } + +} 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 f789e08ea8..eb3200a143 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 @@ -278,6 +278,80 @@ class RdsServiceTest { binding.getValue()); } + @ParameterizedTest + @CsvSource({ + "aurora-postgresql, create", "aurora-postgresql, reboot", "aurora-postgresql, retry", + "aurora-mysql, create", "aurora-mysql, reboot", "aurora-mysql, retry" + }) + void memberIamBindingUsesClusterResourceIdAcrossRuntimePaths(String engine, String path) { + if ("retry".equals(path)) { + when(containerManager.tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any())) + .thenReturn(null); + } + DbCluster cluster = rdsService.createDbCluster("iam-cluster", engine, null, + "admin", "password", "dbname", true, null); + clearInvocations(proxyManager); + DbInstance member = rdsService.createDbInstance("iam-member", engine, null, + "admin", "password", "dbname", "db.serverless", + 0, true, null, null, "iam-cluster"); + if (!"create".equals(path)) { + clearInvocations(proxyManager); + if ("reboot".equals(path)) { + rdsService.rebootDbInstance("iam-member"); + } else { + when(containerManager.tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any())) + .thenReturn(new RdsContainerHandle("late-container", cluster.getDbClusterArn(), + "iam-cluster", "127.0.0.1", 15432)); + rdsService.ensureInstanceBackend("iam-member", "us-east-1"); + } + } + ArgumentCaptor binding = ArgumentCaptor.forClass(RdsProxyBinding.class); + verify(proxyManager).startProxy(eq("rds-resource:" + member.getDbInstanceArn()), + any(), eq(true), anyInt(), any(), anyInt(), any(), any(), any(), any(), any(), binding.capture()); + assertEquals(cluster.getDbClusterResourceId(), binding.getValue().resourceId()); + assertNotEquals(member.getDbiResourceId(), binding.getValue().resourceId()); + assertEquals("123456789012", binding.getValue().accountId()); + assertEquals("us-east-1", binding.getValue().region()); + } + + @Test + void restoredMemberIamBindingsResolveSameNamedClustersByStoredAccountAndRegion() { + String defaultAccount = "123456789012"; + InMemoryStorage rawClusters = new InMemoryStorage<>(); + InMemoryStorage rawInstances = new InMemoryStorage<>(); + int port = 7000; + for (String account : List.of(defaultAccount, "999999999999")) { + for (String region : List.of("us-east-1", "us-west-2")) { + DbCluster cluster = persistedCluster(account, "password", port++); + cluster.setDbClusterArn("arn:aws:rds:" + region + ":" + account + ":cluster:cluster1"); + cluster.setDbClusterResourceId("cluster-" + account + "-" + region); + DbInstance member = persistedInstance("member", account, "password", port++); + member.setDbInstanceArn("arn:aws:rds:" + region + ":" + account + ":db:member"); + member.setDbiResourceId("db-" + account + "-" + region); + member.setDbClusterIdentifier("cluster1"); + member.setIamDatabaseAuthenticationEnabled(true); + cluster.getDbClusterMembers().add("member"); + rawClusters.put(account + "/" + region + "::cluster1", cluster); + rawInstances.put(account + "/" + region + "::member", member); + } + } + RdsService service = newService(containerManager, proxyManager, + new AccountAwareStorageBackend<>(rawInstances, null, defaultAccount), + new AccountAwareStorageBackend<>(rawClusters, null, defaultAccount), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>()); + service.restorePersistedRuntime(); + for (String account : List.of(defaultAccount, "999999999999")) { + for (String region : List.of("us-east-1", "us-west-2")) { + ArgumentCaptor binding = ArgumentCaptor.forClass(RdsProxyBinding.class); + verify(proxyManager).startProxy(eq("rds-resource:arn:aws:rds:" + region + ":" + account + ":db:member"), + any(), eq(true), anyInt(), any(), anyInt(), any(), any(), any(), any(), any(), binding.capture()); + assertEquals("cluster-" + account + "-" + region, binding.getValue().resourceId()); + assertEquals(account, binding.getValue().accountId()); + assertEquals(region, binding.getValue().region()); + } + } + } + @Test void createDbInstanceLeavesPostgresIamTokensUnboundWhenTheBindingIsTurnedOff() { when(rdsConfig.iamTokenEndpointBinding()).thenReturn(false);