mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ui): add a bind address for the web console sidecar (#4175)
The console sidecar is published with a bare host port, so Docker binds it on every interface. That matches the documented deployment, where Floci's own port is published the same way, but it does not follow an operator who narrowed theirs: a "127.0.0.1:4566:4566" mapping keeps the API off the network while the console, which is unauthenticated and can drive every emulated service, stays reachable on 4500. floci.services.ui.bind-address (FLOCI_SERVICES_UI_BIND_ADDRESS) names the host interface the console publishes on. It is unset by default, so nothing changes for anyone who has not asked; setting it to 127.0.0.1 is how the narrowed deployment keeps the console where the API is. A blank value is a hard error rather than a silent fall back to the wildcard. ContainerBuilder.withPortBinding(containerPort, hostPort, hostIp) and a portBindingHostIps component on ContainerSpec carry the address through to the Docker binding. withLoopbackPortBinding is unchanged, and a port that asks for neither still publishes on every interface, which is Docker's own default.
This commit is contained in:
1 parent
65b2b13f5c
commit
899c6f5ec1
12 files changed
+353
-14
No files matched your search
+1
-1
@@ -162,6 +162,6 @@ class ContainerLifecycleManagerNetworkTest {
|
||||
"busybox:stable", null, List.of(), null, null, null, portBindings, List.of(),
|
||||
List.of(), "test-network", List.of(), List.of(), List.of(), List.of(), Map.of(), null,
|
||||
false, null, List.of(), null, null, List.of(), List.of(), null, null, false,
|
||||
List.of(LINK_LOCAL_IP));
|
||||
List.of(LINK_LOCAL_IP), Map.of());
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.CreateContainerCmd;
|
||||
import com.github.dockerjava.api.command.CreateContainerResponse;
|
||||
import com.github.dockerjava.api.model.ExposedPort;
|
||||
import com.github.dockerjava.api.model.HostConfig;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Pins which host interface a published port lands on. A port bound to every interface is
|
||||
* reachable by anyone who can reach the host, so the difference between a null host IP and an
|
||||
* explicit one is a security boundary rather than a formatting detail.
|
||||
*/
|
||||
class ContainerLifecycleManagerPortBindingTest {
|
||||
|
||||
@Test
|
||||
void aPlainPortBindingPublishesOnEveryInterface() {
|
||||
Ports.Binding binding = bindingFor(builder -> builder.withPortBinding(8080, 18_080), 8080);
|
||||
|
||||
assertNull(binding.getHostIp(), "Docker's own default is every interface");
|
||||
assertEquals("18080", binding.getHostPortSpec());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLoopbackPortBindingPublishesOnLoopbackOnly() {
|
||||
Ports.Binding binding = bindingFor(
|
||||
builder -> builder.withLoopbackPortBinding(5050, 5050), 5050);
|
||||
|
||||
assertEquals("127.0.0.1", binding.getHostIp());
|
||||
assertEquals("5050", binding.getHostPortSpec());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitHostIpReachesTheBinding() {
|
||||
Ports.Binding binding = bindingFor(
|
||||
builder -> builder.withPortBinding(4500, 4500, "127.0.0.1"), 4500);
|
||||
|
||||
assertEquals("127.0.0.1", binding.getHostIp());
|
||||
assertEquals("4500", binding.getHostPortSpec());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitWildcardHostIpIsPassedThroughAsAsked() {
|
||||
Ports.Binding binding = bindingFor(
|
||||
builder -> builder.withPortBinding(4500, 4500, "0.0.0.0"), 4500);
|
||||
|
||||
assertEquals("0.0.0.0", binding.getHostIp());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBlankHostIpFallsBackToDockersOwnDefault() {
|
||||
Ports.Binding binding = bindingFor(
|
||||
builder -> builder.withPortBinding(4500, 4500, " "), 4500);
|
||||
|
||||
assertNull(binding.getHostIp());
|
||||
}
|
||||
|
||||
@Test
|
||||
void oneBoundInterfaceDoesNotLeakOntoTheOtherPorts() {
|
||||
ContainerSpec spec = specFor(builder -> builder
|
||||
.withPortBinding(4500, 4500, "127.0.0.1")
|
||||
.withPortBinding(9000, 9000));
|
||||
Ports ports = portsOf(spec);
|
||||
|
||||
assertEquals("127.0.0.1", only(ports, 4500).getHostIp());
|
||||
assertNull(only(ports, 9000).getHostIp());
|
||||
}
|
||||
|
||||
private static Ports.Binding bindingFor(UnaryOperator<ContainerBuilder.Builder> customizer,
|
||||
int containerPort) {
|
||||
return only(portsOf(specFor(customizer)), containerPort);
|
||||
}
|
||||
|
||||
private static Ports.Binding only(Ports ports, int containerPort) {
|
||||
Ports.Binding[] bindings = ports.getBindings().get(ExposedPort.tcp(containerPort));
|
||||
assertEquals(1, bindings.length, "expected exactly one binding for port " + containerPort);
|
||||
return bindings[0];
|
||||
}
|
||||
|
||||
private static ContainerSpec specFor(UnaryOperator<ContainerBuilder.Builder> customizer) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
|
||||
ContainerBuilder builder = new ContainerBuilder(
|
||||
config, mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class));
|
||||
return customizer.apply(builder.newContainer("app:latest")).build();
|
||||
}
|
||||
|
||||
private static Ports portsOf(ContainerSpec spec) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
CreateContainerCmd createCmd = mock(CreateContainerCmd.class, RETURNS_SELF);
|
||||
when(dockerClient.createContainerCmd("app:latest")).thenReturn(createCmd);
|
||||
CreateContainerResponse response = mock(CreateContainerResponse.class);
|
||||
when(response.getId()).thenReturn("app-id");
|
||||
when(createCmd.exec()).thenReturn(response);
|
||||
ImageCacheService imageCacheService = mock(ImageCacheService.class);
|
||||
when(imageCacheService.ensureImageExists(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
PortAllocator portAllocator = mock(PortAllocator.class);
|
||||
ContainerLifecycleManager manager = new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, mock(ContainerDetector.class), portAllocator, config);
|
||||
|
||||
manager.create(spec);
|
||||
|
||||
ArgumentCaptor<HostConfig> hostConfig = ArgumentCaptor.forClass(HostConfig.class);
|
||||
verify(createCmd).withHostConfig(hostConfig.capture());
|
||||
return hostConfig.getValue().getPortBindings();
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.E
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerReachableEndpoint;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import com.github.dockerjava.api.exception.DockerClientException;
|
||||
@@ -41,6 +42,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -74,6 +76,8 @@ class FlociUiManagerTest {
|
||||
when(ui.statusReadyField()).thenReturn(Optional.empty());
|
||||
when(ui.statusReadyValue()).thenReturn(Optional.empty());
|
||||
when(ui.statusUnavailableValue()).thenReturn(Optional.empty());
|
||||
when(ui.bindAddress()).thenReturn(Optional.empty());
|
||||
when(ui.dockerNetwork()).thenReturn(Optional.empty());
|
||||
}
|
||||
|
||||
private FlociUiManager newManager() {
|
||||
@@ -896,6 +900,104 @@ class FlociUiManagerTest {
|
||||
"a pre-contract sidecar must be recreated so it gains the canonical variable");
|
||||
}
|
||||
|
||||
// --- where the console is published ---
|
||||
|
||||
@Test
|
||||
void anUnsetBindAddressLeavesDockersOwnDefault() {
|
||||
// Unset is what the console has always had: published on every interface, the same as a
|
||||
// bare "4500:4500" mapping, which is what the documented compose files give the API too.
|
||||
withUiConfig();
|
||||
when(ui.enabled()).thenReturn(true);
|
||||
when(ui.image()).thenReturn("floci/floci-ui:latest");
|
||||
when(ui.containerName()).thenReturn("floci-ui");
|
||||
when(ui.port()).thenReturn(4500);
|
||||
when(ui.endpoint()).thenReturn(Optional.of("http://custom:4566"));
|
||||
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
|
||||
|
||||
ContainerBuilder.Builder specBuilder = newStartedSidecar();
|
||||
|
||||
verify(specBuilder).withPortBinding(4500, 4500, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aConfiguredBindAddressReachesThePortBinding() {
|
||||
withUiConfig();
|
||||
when(ui.enabled()).thenReturn(true);
|
||||
when(ui.image()).thenReturn("floci/floci-ui:latest");
|
||||
when(ui.containerName()).thenReturn("floci-ui");
|
||||
when(ui.port()).thenReturn(4500);
|
||||
when(ui.endpoint()).thenReturn(Optional.of("http://custom:4566"));
|
||||
when(ui.bindAddress()).thenReturn(Optional.of("127.0.0.1"));
|
||||
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
|
||||
|
||||
ContainerBuilder.Builder specBuilder = newStartedSidecar();
|
||||
|
||||
verify(specBuilder).withPortBinding(4500, 4500, "127.0.0.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsetBindAddressResolvesToNoAddressAtAll() {
|
||||
assertNull(FlociUiManager.resolveBindAddress(Optional.empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aConfiguredBindAddressIsTakenAsGiven() {
|
||||
assertEquals("127.0.0.1", FlociUiManager.resolveBindAddress(Optional.of("127.0.0.1")));
|
||||
assertEquals("0.0.0.0", FlociUiManager.resolveBindAddress(Optional.of("0.0.0.0")));
|
||||
assertEquals("192.168.1.10", FlociUiManager.resolveBindAddress(Optional.of(" 192.168.1.10 ")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBlankBindAddressFailsFastRatherThanSilentlyPickingOne() {
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> FlociUiManager.resolveBindAddress(Optional.of("")));
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> FlociUiManager.resolveBindAddress(Optional.of(" ")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBlankBindAddressIsReportedInsteadOfStartingTheConsole() {
|
||||
withUiConfig();
|
||||
when(ui.enabled()).thenReturn(true);
|
||||
when(ui.image()).thenReturn("floci/floci-ui:latest");
|
||||
when(ui.bindAddress()).thenReturn(Optional.of(" "));
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
|
||||
FlociUiManager manager = new FlociUiManager(
|
||||
mock(ContainerBuilder.class), lifecycleManager, logStreamer, containerDetector,
|
||||
mock(CurrentContainerNetworkResolver.class), dockerHostResolver,
|
||||
new LaunchedContainerAwsEnv(mock(ContainerReachableEndpoint.class)),
|
||||
config, regionResolver, new ObjectMapper());
|
||||
manager.ensureStarted();
|
||||
|
||||
assertFalse(manager.status().started());
|
||||
assertTrue(manager.status().error().contains("floci.services.ui.bind-address"),
|
||||
manager.status().error());
|
||||
// Rejected before the image is resolved, so a console that is not going to be published
|
||||
// is never pulled.
|
||||
verify(lifecycleManager, times(0)).findByName(anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a full {@code ensureStarted()} against a stubbed builder and returns the spec builder,
|
||||
* so a test can assert on how the container was asked to be published.
|
||||
*/
|
||||
private ContainerBuilder.Builder newStartedSidecar() {
|
||||
String image = "floci/floci-ui:latest";
|
||||
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder specBuilder = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
ContainerSpec spec = new ContainerSpec(image);
|
||||
when(containerBuilder.resolveImage(image)).thenReturn(image);
|
||||
when(containerBuilder.newContainer(image)).thenReturn(specBuilder);
|
||||
when(specBuilder.build()).thenReturn(spec);
|
||||
when(lifecycleManager.imageLabels(image)).thenReturn(Optional.empty());
|
||||
when(lifecycleManager.createAndStart(spec)).thenReturn(
|
||||
new ContainerInfo("ui-container", Map.of(4500, new EndpointInfo("127.0.0.1", 4500))));
|
||||
|
||||
newManager(containerBuilder).ensureStarted();
|
||||
return specBuilder;
|
||||
}
|
||||
|
||||
/** Adopts a console that resolves to the built-in floci-ui profile. */
|
||||
private FlociUiManager adoptSidecar(int port) {
|
||||
return adoptSidecar(port, "floci/floci-ui:latest",
|
||||
@@ -926,6 +1028,7 @@ class FlociUiManagerTest {
|
||||
when(ui.statusReadyValue()).thenReturn(readyValue);
|
||||
when(ui.statusUnavailableValue()).thenReturn(Optional.empty());
|
||||
when(ui.endpoint()).thenReturn(Optional.of("http://custom:4566"));
|
||||
when(ui.bindAddress()).thenReturn(Optional.empty());
|
||||
|
||||
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
when(containerBuilder.resolveImage(image)).thenReturn(image);
|
||||
|
||||
Reference in new issue
Block a user