fix(rds): advertise reachable endpoints (#2074)

RDS endpoints currently expose the proxy's container port even when
Docker publishes it on a random host port. That works when the host can
route to the container network, but fails with VM-backed Docker
runtimes.

Resolve the current container's published port when an external
endpoint host is configured while preserving the internal proxy port
used by the listener.
This commit is contained in:
Jan Waś authored and GitHub committed 2026-08-10 22:03:52 -04:00
1 parent 28e9f6ed63
commit f417164d42
9 files changed
+157 -13

No files matched your search

@@ -4,11 +4,14 @@ import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerCmd;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.Ports;
import org.junit.jupiter.api.Test;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -52,6 +55,35 @@ class CurrentContainerNetworkResolverTest {
assertTrue(resolver.resolveNetworkName().isEmpty());
}
@Test
void resolvePublishedPort_retriesUntilSuccessfulAndCachesResult() {
DockerClient dockerClient = mock(DockerClient.class);
ContainerDetector containerDetector = mock(ContainerDetector.class);
when(containerDetector.isRunningInContainer()).thenReturn(true);
InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class);
InspectContainerResponse inspect = mock(InspectContainerResponse.class, RETURNS_DEEP_STUBS);
Ports ports = new Ports();
ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {null});
when(dockerClient.inspectContainerCmd("floci-container")).thenReturn(inspectCmd);
when(inspectCmd.exec()).thenReturn(inspect);
when(inspect.getNetworkSettings().getPorts()).thenReturn(ports);
CurrentContainerNetworkResolver resolver =
new TestResolver(dockerClient, containerDetector, "floci-container");
assertTrue(resolver.resolvePublishedPort(7000).isEmpty());
ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {
null,
Ports.Binding.bindPort(49173)
});
assertEquals(OptionalInt.of(49173), resolver.resolvePublishedPort(7000));
ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {Ports.Binding.bindPort(49174)});
assertEquals(OptionalInt.of(49173), resolver.resolvePublishedPort(7000));
}
private static Map<String, ContainerNetwork> networks(String firstName, String firstIp,
String secondName, String secondIp) {
Map<String, ContainerNetwork> networks = new LinkedHashMap<>();
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.rds;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver;
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
@@ -29,6 +30,8 @@ import org.mockito.ArgumentCaptor;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
@@ -187,7 +190,7 @@ class RdsServiceTest {
when(dockerHostResolver.resolve()).thenReturn("floci.local");
RdsService service = new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config,
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), null, dockerHostResolver);
new InMemoryStorage<>(), new InMemoryStorage<>(), null, dockerHostResolver, null);
DbInstance instance = service.createDbInstance("mydb", "postgres", "13",
"admin", "password", "dbname", "db.t3.micro",
@@ -196,6 +199,24 @@ class RdsServiceTest {
assertEquals("floci.local", instance.getEndpoint().address());
}
@Test
void dbInstanceEndpointUsesPublishedProxyPort() {
CurrentContainerNetworkResolver currentContainerNetworkResolver = mock(CurrentContainerNetworkResolver.class);
when(config.services().rds().endpointHost()).thenReturn(Optional.of("localhost"));
when(currentContainerNetworkResolver.resolvePublishedPort(7000)).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, currentContainerNetworkResolver);
DbInstance instance = service.createDbInstance("mydb", "postgres", "13",
"admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null);
assertEquals("localhost", instance.getEndpoint().address());
assertEquals(49173, instance.getEndpoint().port());
assertEquals(7000, instance.getProxyPort());
}
@Test
void createDbInstanceWithManagedMasterPasswordCreatesSecret() {
SecretsManagerService secretsManager = mock(SecretsManagerService.class);
@@ -1114,7 +1135,7 @@ class RdsServiceTest {
SecretsManagerService secretsManager) {
return new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config,
instances, clusters, parameterGroups, clusterParameterGroups, new InMemoryStorage<>(),
secretsManager, null);
secretsManager, null, null);
}
private static List<Subnet> defaultSubnets() {