From 3171b8a7459075461cbe2ff1013e9052f548b0c3 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Thu, 8 Oct 2026 17:14:57 -0700 Subject: [PATCH] fix(rds): bind IAM tokens to the published port (#5305) * fix(rds): Bind IAM tokens to the published port Docker clients sign tokens for the advertised database endpoint. Checking the internal proxy port rejects those valid logins after port mapping. Use the published endpoint port for the same binding. * fix(rds): keep IAM binding aligned with advertised endpoints * fix(rds): bind proxy tokens to each listener port --- .../floci/services/rds/RdsService.java | 28 +++--- .../floci/services/rds/RdsServiceTest.java | 92 +++++++++++++++++++ 2 files changed, 105 insertions(+), 15 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 10981fa606..612e873b61 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 @@ -841,7 +841,7 @@ public class RdsService implements Resettable, ResourceProvider { masterUsername, masterPassword, dbName, (user, pw) -> validateDbPasswordForScope( accountId, instanceRegion, id, user, pw), - proxyBinding(engine, instance.getEndpoint().address(), proxyPort, + proxyBinding(engine, instance.getEndpoint().address(), instance.getEndpoint().port(), instanceRegion, accountId, instance.getDbiResourceId())); } catch (RuntimeException | Error e) { try { @@ -3195,7 +3195,7 @@ public class RdsService implements Resettable, ResourceProvider { instance.getEndpoint().address(), effectiveMasterUser, instance.getMasterPassword(), instance.getDbName(), (user, pw) -> validateDbPasswordForScope(accountId, instanceRegion, id, user, pw), - proxyBinding(instance.getEngine(), instance.getEndpoint().address(), instance.getProxyPort(), + proxyBinding(instance.getEngine(), instance.getEndpoint().address(), instance.getEndpoint().port(), instanceRegion, accountId, instance.getDbiResourceId())); } } @@ -3362,7 +3362,7 @@ public class RdsService implements Resettable, ResourceProvider { cluster.getMasterUsername() != null ? cluster.getMasterUsername() : "root", cluster.getMasterPassword(), cluster.getDatabaseName(), (user, pw) -> validateDbClusterPasswordForScope(accountId, clusterRegion, id, user, pw), - proxyBinding(cluster.getEngine(), cluster.getEndpoint().address(), cluster.getProxyPort(), + proxyBinding(cluster.getEngine(), cluster.getEndpoint().address(), cluster.getEndpoint().port(), clusterRegion, accountId, cluster.getDbClusterResourceId())); applyAutoPause(cluster); } @@ -3430,7 +3430,7 @@ public class RdsService implements Resettable, ResourceProvider { (user, pw) -> validateDbPasswordForScope( accountId, instanceRegion, id, user, pw), proxyBinding(instance.getEngine(), instance.getEndpoint().address(), - instance.getProxyPort(), instanceRegion, accountId, instance.getDbiResourceId())); + instance.getEndpoint().port(), instanceRegion, accountId, instance.getDbiResourceId())); } else { // No backing container: created or last rebooted while no daemon was reachable. instance = ensureInstanceBackend(id, effectiveRegion); @@ -3538,7 +3538,7 @@ public class RdsService implements Resettable, ResourceProvider { (user, pw) -> validateDbPasswordForScope( accountId, instanceRegion, id, user, pw), proxyBinding(instance.getEngine(), relayEndpoint.address(), - relayPort, instanceRegion, accountId, instance.getDbiResourceId())); + relayEndpoint.port(), instanceRegion, accountId, instance.getDbiResourceId())); } catch (RuntimeException e) { if (allocatedRelayPort) { releaseProxyPort(relayPort); @@ -3632,7 +3632,7 @@ public class RdsService implements Resettable, ResourceProvider { (user, pw) -> validateDbClusterPasswordForScope( accountId, clusterRegion, id, user, pw), proxyBinding(cluster.getEngine(), relayEndpoint.address(), - relayPort, clusterRegion, accountId, cluster.getDbClusterResourceId())); + relayEndpoint.port(), clusterRegion, accountId, cluster.getDbClusterResourceId())); } catch (RuntimeException e) { if (allocatedRelayPort) { releaseProxyPort(relayPort); @@ -4021,7 +4021,7 @@ public class RdsService implements Resettable, ResourceProvider { effectiveMasterUser, masterPassword, databaseName, (user, pw) -> validateDbClusterPasswordForScope( accountId, clusterRegion, id, user, pw), - proxyBinding(engine, cluster.getEndpoint().address(), proxyPort, + proxyBinding(engine, cluster.getEndpoint().address(), cluster.getEndpoint().port(), clusterRegion, accountId, cluster.getDbClusterResourceId())); } @@ -5077,9 +5077,9 @@ public class RdsService implements Resettable, ResourceProvider { // ── DB Proxies (AWS::RDS::DBProxy) ────────────────────────────────────────── /** - * Creates a DB Proxy. No relay is started here — the backend target is unknown until a target + * Creates a DB Proxy. No relay is started here: the backend target is unknown until a target * group registers a cluster/instance (see {@link #registerDbProxyTargets}). The relay listens on - * the engine family's default port so the endpoint is a bare host clients reach at 5432/3306. + * the engine family's default port when available, otherwise an allocated pool port. */ public DbProxy createDbProxy(String dbProxyName, String engineFamily, boolean requireTls, boolean iamAuth, String roleArn, List vpcSubnetIds, @@ -5135,9 +5135,7 @@ public class RdsService implements Resettable, ResourceProvider { } boolean mock = config.services().rds().mock(); - // RDS Proxy exposes a bare hostname on the engine's default port. Floci currently models - // that contract directly; a separate endpoint-routing design is required before multiple - // same-engine proxies can be made externally reachable through one Docker host. + // Proxies share a hostname, so each needs its own listener port. int proxyPort = reserveOrAllocateProxyPort(defaultPortForEngineFamily(engineFamily)); DbProxy proxy = new DbProxy(); proxy.setDbProxyName(dbProxyName); @@ -7533,7 +7531,7 @@ public class RdsService implements Resettable, ResourceProvider { return java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 17); } - /** The engine's default listener port — an RDS Proxy endpoint is a bare host reached on this port. */ + /** The preferred listener port, used when no other proxy has reserved it. */ private int defaultPortForEngineFamily(String engineFamily) { if (engineFamily == null) { return 5432; @@ -7614,7 +7612,7 @@ public class RdsService implements Resettable, ResourceProvider { (user, pw) -> validateDbClusterPasswordForScope( accountId, clusterRegion, cluster.getDbClusterIdentifier(), user, pw), - proxyBinding(cluster.getEngine(), cluster.getEndpoint().address(), proxyPort, + proxyBinding(cluster.getEngine(), cluster.getEndpoint().address(), cluster.getEndpoint().port(), clusterRegion, accountId, cluster.getDbClusterResourceId())); } cluster.setStatus(DbInstanceStatus.AVAILABLE); @@ -7757,7 +7755,7 @@ public class RdsService implements Resettable, ResourceProvider { (user, pw) -> validateDbPasswordForScope( accountId, instanceRegion, instance.getDbInstanceIdentifier(), user, pw), - proxyBinding(instance.getEngine(), instance.getEndpoint().address(), proxyPort, + proxyBinding(instance.getEngine(), instance.getEndpoint().address(), instance.getEndpoint().port(), instanceRegion, accountId, instance.getDbiResourceId())); } instance.setStatus(DbInstanceStatus.AVAILABLE); 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 1a3c4da0eb..bd1d3dbb58 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 @@ -81,6 +81,7 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.clearInvocations; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -956,6 +957,97 @@ class RdsServiceTest { assertEquals("localhost", instance.getEndpoint().address()); assertEquals(49173, instance.getEndpoint().port()); assertEquals(7000, instance.getProxyPort()); + ArgumentCaptor binding = ArgumentCaptor.forClass(RdsProxyBinding.class); + verify(proxyManager).startProxy(any(), any(), anyBoolean(), eq(7000), any(), anyInt(), + any(), any(), any(), any(), any(), binding.capture()); + assertEquals(new RdsProxyBinding("localhost", 49173, "us-east-1", "123456789012", + instance.getDbiResourceId(), true), binding.getValue()); + } + + @ParameterizedTest + @CsvSource({"false,7000", "true,49173"}) + void iamBindingUsesStoredEndpointWhenPublishedPortLookupChanges(boolean mapped, int advertisedPort) { + CurrentContainerNetworkResolver resolver = mock(CurrentContainerNetworkResolver.class); + when(config.services().rds().endpointHost()).thenReturn(Optional.of("localhost")); + when(resolver.resolvePublishedPort(7000)).thenReturn( + mapped ? OptionalInt.of(49173) : OptionalInt.empty(), OptionalInt.of(49174)); + RdsService service = new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config, + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), null, null, resolver); + + DbInstance instance = service.createDbInstance("mydb", "postgres", "13", + "admin", "password", "dbname", "db.t3.micro", 20, true, null, null, null); + + ArgumentCaptor binding = ArgumentCaptor.forClass(RdsProxyBinding.class); + verify(proxyManager).startProxy(any(), any(), anyBoolean(), eq(7000), any(), anyInt(), + any(), any(), any(), any(), any(), binding.capture()); + assertEquals(advertisedPort, instance.getEndpoint().port()); + assertEquals(new RdsProxyBinding(instance.getEndpoint().address(), instance.getEndpoint().port(), + "us-east-1", "123456789012", instance.getDbiResourceId(), true), binding.getValue()); + verify(resolver).resolvePublishedPort(7000); + } + + @ParameterizedTest + @CsvSource({"false,7000", "true,49173"}) + void clusterIamBindingUsesStoredEndpointWhenPublishedPortLookupChanges(boolean mapped, int advertisedPort) { + CurrentContainerNetworkResolver resolver = mock(CurrentContainerNetworkResolver.class); + when(config.services().rds().endpointHost()).thenReturn(Optional.of("localhost")); + when(resolver.resolvePublishedPort(7000)).thenReturn( + mapped ? OptionalInt.of(49173) : OptionalInt.empty(), OptionalInt.of(49174)); + RdsService service = new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config, + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), null, null, resolver); + + DbCluster cluster = service.createDbCluster("cluster", "aurora-postgresql", "16.3", + "admin", "password", "app", true, null); + + ArgumentCaptor binding = ArgumentCaptor.forClass(RdsProxyBinding.class); + verify(proxyManager).startProxy(any(), any(), anyBoolean(), eq(7000), any(), anyInt(), + any(), any(), any(), any(), any(), binding.capture()); + assertEquals(advertisedPort, cluster.getEndpoint().port()); + assertEquals(new RdsProxyBinding(cluster.getEndpoint().address(), cluster.getEndpoint().port(), + "us-east-1", "123456789012", cluster.getDbClusterResourceId(), true), binding.getValue()); + verify(resolver).resolvePublishedPort(7000); + } + + @ParameterizedTest + @CsvSource({"postgres,POSTGRESQL,5432", "mysql,MYSQL,3306"}) + void dbProxyIamBindingUsesEachListenerPort(String engine, String family, int defaultPort) { + CurrentContainerNetworkResolver resolver = mock(CurrentContainerNetworkResolver.class); + when(config.services().rds().endpointHost()).thenReturn(Optional.of("localhost")); + when(resolver.resolvePublishedPort(anyInt())).thenReturn(OptionalInt.of(49173)); + RdsService service = new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config, + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), null, null, resolver); + service.createDbInstance("mydb", engine, null, "admin", "password", "app", + "db.t3.micro", 20, true, null, null, null); + DbProxy first = service.createDbProxy("proxy-a", family, true, true, PROXY_ROLE_ARN, + PROXY_SUBNET_IDS, List.of(), PROXY_AUTH, Map.of()); + DbProxy second = service.createDbProxy("proxy-b", family, true, true, PROXY_ROLE_ARN, + PROXY_SUBNET_IDS, List.of(), PROXY_AUTH, Map.of()); + clearInvocations(proxyManager, resolver); + + for (DbProxy proxy : List.of(first, second)) { + service.registerDbProxyTargets(proxy.getDbProxyName(), null, + List.of(), List.of("mydb"), 90, 40); + ArgumentCaptor binding = ArgumentCaptor.forClass(RdsProxyBinding.class); + verify(proxyManager).startProxy(any(), any(), anyBoolean(), eq(proxy.getProxyPort()), any(), anyInt(), + any(), any(), any(), any(), any(), binding.capture()); + assertEquals(proxy.getEndpointHost(), proxy.getEndpoint()); + assertEquals(new RdsProxyBinding(proxy.getEndpointHost(), proxy.getProxyPort(), "us-east-1", + "123456789012", proxy.getDbProxyResourceId(), true), binding.getValue()); + clearInvocations(proxyManager); + service.modifyDbProxy(proxy.getDbProxyName(), null, PROXY_AUTH, null, null, null, + null, null, null, "us-east-1"); + verify(proxyManager).startProxy(any(), any(), eq(false), eq(proxy.getProxyPort()), any(), anyInt(), + any(), any(), any(), any(), any(), binding.capture()); + assertEquals(new RdsProxyBinding(proxy.getEndpointHost(), proxy.getProxyPort(), "us-east-1", + "123456789012", proxy.getDbProxyResourceId(), true), binding.getValue()); + clearInvocations(proxyManager); + } + assertEquals(defaultPort, first.getProxyPort()); + assertNotEquals(defaultPort, second.getProxyPort()); + verifyNoInteractions(resolver); } @Test