diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpoint.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpoint.java index fb91c4b761..76172271a8 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpoint.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpoint.java @@ -4,8 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; - -import java.net.URI; +import org.eclipse.microprofile.config.inject.ConfigProperty; /** * Computes the Floci base URL reachable from inside a Docker container that Floci launches. @@ -33,19 +32,31 @@ public class ContainerReachableEndpoint { private final EmulatorConfig config; private final DockerHostResolver dockerHostResolver; private final EmbeddedDnsServer embeddedDnsServer; + private final int httpPort; @Inject public ContainerReachableEndpoint(EmulatorConfig config, DockerHostResolver dockerHostResolver, - EmbeddedDnsServer embeddedDnsServer) { + EmbeddedDnsServer embeddedDnsServer, + @ConfigProperty(name = "quarkus.http.port") int httpPort) { this.config = config; this.dockerHostResolver = dockerHostResolver; this.embeddedDnsServer = embeddedDnsServer; + this.httpPort = httpPort; } - /** The Floci {@code http://host:port} base URL reachable from inside a launched Docker container. */ + /** + * The Floci {@code http://host:port} base URL reachable from inside a launched Docker container. + * + *

The port is the one Floci listens on, not the port in {@code floci.base-url}: base-url + * advertises the host-facing address, which differs under a port mapping such as + * {@code -p 4811:4566} and is refused inside the Docker network. That is + * {@code quarkus.http.port}, which defaults to {@code floci.port} but can be overridden on its + * own; with TLS enabled the HTTP server moves to a loopback-only internal port and the public + * listener is {@code TlsProxyServer} on {@code floci.port}. + */ public String baseUrl() { - int flociPort = URI.create(config.baseUrl()).getPort(); + int flociPort = config.tls().enabled() ? config.port() : httpPort; String flociHostname = embeddedDnsServer.getServerIp().isPresent() ? config.hostname().orElse(EmbeddedDnsServer.DEFAULT_SUFFIX) : dockerHostResolver.resolve(); diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpointTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpointTest.java new file mode 100644 index 0000000000..ef8bff5d79 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpointTest.java @@ -0,0 +1,92 @@ +package io.github.hectorvent.floci.core.common.docker; + +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer; +import org.junit.jupiter.api.Test; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Launched containers talk to the Floci process directly, so the port must be the one Floci + * listens on ({@code quarkus.http.port}, or {@code floci.port} under TLS where the proxy owns the + * public port), not the host-facing port advertised in {@code floci.base-url}. + * With {@code docker run -p 4811:4566 -e FLOCI_BASE_URL=http://localhost:4811}, port 4811 exists + * only on the host; inside the Docker network Floci answers on 4566. + */ +class ContainerReachableEndpointTest { + + private final EmulatorConfig config = mock(EmulatorConfig.class); + private final DockerHostResolver dockerHostResolver = mock(DockerHostResolver.class); + private final EmbeddedDnsServer embeddedDnsServer = mock(EmbeddedDnsServer.class); + private final EmulatorConfig.TlsConfig tls = mock(EmulatorConfig.TlsConfig.class); + private final ContainerReachableEndpoint endpoint = endpoint(4566); + + private ContainerReachableEndpoint endpoint(int httpPort) { + when(config.tls()).thenReturn(tls); + return new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer, httpPort); + } + + private void hostMappedTo4811() { + lenient().when(config.port()).thenReturn(4566); + lenient().when(config.baseUrl()).thenReturn("http://localhost:4811"); + lenient().when(config.hostname()).thenReturn(Optional.empty()); + } + + @Test + void embeddedDnsUsesListenPortNotAdvertisedPort() { + hostMappedTo4811(); + when(embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.2")); + + assertEquals("http://" + EmbeddedDnsServer.DEFAULT_SUFFIX + ":4566", endpoint.baseUrl()); + } + + @Test + void dockerHostFallbackUsesListenPortNotAdvertisedPort() { + hostMappedTo4811(); + when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty()); + when(dockerHostResolver.resolve()).thenReturn("172.18.0.2"); + + assertEquals("http://172.18.0.2:4566", endpoint.baseUrl()); + } + + @Test + void customListenPortAndHostnameAreHonoured() { + // Listen port, advertised port and floci.port all differ, so only quarkus.http.port can + // yield 4900. + lenient().when(config.port()).thenReturn(4566); + lenient().when(config.baseUrl()).thenReturn("http://localhost:4811"); + when(config.hostname()).thenReturn(Optional.of("floci")); + when(embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.2")); + + assertEquals("http://floci:4900", endpoint(4900).baseUrl()); + } + + @Test + void httpPortOverriddenAloneIsTheListenPort() { + // QUARKUS_HTTP_PORT=4811 FLOCI_BASE_URL=http://localhost:4811 with floci.port left at 4566: + // the server listens on 4811, so that is where containers must call. + lenient().when(config.port()).thenReturn(4566); + lenient().when(config.baseUrl()).thenReturn("http://localhost:4811"); + when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty()); + when(dockerHostResolver.resolve()).thenReturn("172.18.0.2"); + + assertEquals("http://172.18.0.2:4811", endpoint(4811).baseUrl()); + } + + @Test + void tlsUsesPublicProxyPortNotInternalHttpPort() { + // TLS pins quarkus.http.port to a loopback-only backend (4510); the TlsProxyServer serves + // floci.port, the only one a container can reach. + when(tls.enabled()).thenReturn(true); + when(config.port()).thenReturn(4566); + when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty()); + when(dockerHostResolver.resolve()).thenReturn("172.18.0.2"); + + assertEquals("http://172.18.0.2:4566", endpoint(4510).baseUrl()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java index f4f630dd57..b329a2d678 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java @@ -239,13 +239,15 @@ class LambdaImageConfigTest { when(config.docker()).thenReturn(docker); when(docker.logMaxSize()).thenReturn("10m"); when(docker.logMaxFile()).thenReturn("3"); - when(config.baseUrl()).thenReturn("http://localhost:4566"); + lenient().when(config.baseUrl()).thenReturn("http://localhost:4566"); + lenient().when(config.port()).thenReturn(4566); + lenient().when(config.tls()).thenReturn(mock(EmulatorConfig.TlsConfig.class)); lenient().when(config.hostname()).thenReturn(Optional.empty()); when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty()); ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer); ContainerReachableEndpoint reachableEndpoint = - new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer); + new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer, 4566); LaunchedContainerAwsEnv awsEnv = new LaunchedContainerAwsEnv(reachableEndpoint); launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver, runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager, diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java index 4ad459d005..8508de431b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java @@ -108,7 +108,9 @@ class ContainerLauncherTest { when(config.docker()).thenReturn(docker); when(docker.logMaxSize()).thenReturn("10m"); when(docker.logMaxFile()).thenReturn("3"); - when(config.baseUrl()).thenReturn("http://localhost:4566"); + lenient().when(config.baseUrl()).thenReturn("http://localhost:4566"); + lenient().when(config.port()).thenReturn(4566); + lenient().when(config.tls()).thenReturn(mock(EmulatorConfig.TlsConfig.class)); lenient().when(config.defaultRegion()).thenReturn("us-east-1"); lenient().when(config.defaultAccountId()).thenReturn("000000000000"); lenient().when(config.hostname()).thenReturn(Optional.empty()); @@ -131,7 +133,7 @@ class ContainerLauncherTest { ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer); ContainerReachableEndpoint reachableEndpoint = - new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer); + new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer, 4566); LaunchedContainerAwsEnv awsEnv = new LaunchedContainerAwsEnv(reachableEndpoint); launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver, runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager, @@ -649,6 +651,27 @@ class ContainerLauncherTest { "a partial user-supplied access key must never override the owner-account baseline"); } + @Test + void launchFunction_endpointUsesListenPortNotAdvertisedBaseUrlPort() throws Exception { + // docker run -p 4811:4566 -e FLOCI_BASE_URL=http://localhost:4811: 4811 exists only on the + // host. The function container reaches Floci over the Docker network, where it listens on + // quarkus.http.port (4566, set in setUp), so that is the port AWS_ENDPOINT_URL must carry. + Path codePath = Files.createDirectory(tempDir.resolve("endpoint-port")); + // lenient: the fixed code never reads base-url here; the stub sets up the mismatch. + lenient().when(config.baseUrl()).thenReturn("http://localhost:4811"); + + LambdaFunction fn = new LambdaFunction(); + fn.setFunctionName("endpoint-port-fn"); + fn.setRuntime("nodejs20.x"); + fn.setHandler("index.handler"); + fn.setCodeLocalPath(codePath.toString()); + + launcher.launch(fn); + + List env = captureRealContainerSpec().env(); + assertTrue(env.contains("AWS_ENDPOINT_URL=http://127.0.0.1:4566"), env.toString()); + } + @Test void launchFunction_injectsConfiguredDefaultRegionWhenArnMissing() throws Exception { Path codePath = Files.createDirectory(tempDir.resolve("region-default")); @@ -1701,7 +1724,7 @@ class ContainerLauncherTest { void launchFunction_forwardsExtensionOutputToCloudWatchLogs() throws Exception { CloudWatchLogsService cloudWatchLogs = mock(CloudWatchLogsService.class); ContainerReachableEndpoint reachableEndpoint = - new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer); + new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer, 4566); ContainerLauncher launcherWithRealStreamer = new ContainerLauncher( new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer), lifecycleManager, diff --git a/src/test/java/io/github/hectorvent/floci/testing/SidecarContainersProfile.java b/src/test/java/io/github/hectorvent/floci/testing/SidecarContainersProfile.java index 964e8f34ce..6eb4efe278 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/SidecarContainersProfile.java +++ b/src/test/java/io/github/hectorvent/floci/testing/SidecarContainersProfile.java @@ -32,9 +32,10 @@ import java.util.Map; * process ids mean nothing here. A normal shutdown stops the sidecars; those of a run that was * killed are removed by the next run on the same host key, in {@link #removeAbandonedSidecars()}. * - *

Also sets the HTTP port and {@code floci.base-url} together. The AppSync resolver callback URL - * is built from the base URL's port, which in production is the port Floci listens on. Under - * {@code @QuarkusTest} the application listens on the test port instead, so without this the + *

Also sets the HTTP port, {@code floci.port} and {@code floci.base-url} together. The AppSync + * resolver callback URL a launched container uses is built from {@code quarkus.http.port}, which in + * production is the port Floci listens on. Under {@code @QuarkusTest} the application listens on + * the test port instead, so without this the * sidecar would call back to 4566 and every resolver-backed field would fail with a connection * error, and floci-duck would reach for Floci's S3 on the wrong port. The port is a free one * chosen once when the class loads, rather than a fixed number, so two runs on the same machine do @@ -64,6 +65,8 @@ public class SidecarContainersProfile implements QuarkusTestProfile { return Map.of( "floci.docker.resource-namespace", NAMESPACE, "quarkus.http.test-port", String.valueOf(TEST_PORT), + "quarkus.http.port", String.valueOf(TEST_PORT), + "floci.port", String.valueOf(TEST_PORT), "floci.base-url", "http://localhost:" + TEST_PORT, "floci.services.cur.emit-mode", "off", "floci.services.bcm-data-exports.emit-mode", "off");