mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -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
@@ -2313,6 +2313,21 @@ public interface EmulatorConfig {
|
||||
@WithDefault("4500")
|
||||
int port();
|
||||
|
||||
/**
|
||||
* Host interface {@link #port()} is published on.
|
||||
* Env: {@code FLOCI_SERVICES_UI_BIND_ADDRESS}
|
||||
*
|
||||
* <p>Unset by default, which is Docker's own default of publishing on every interface.
|
||||
* Set it to {@code 127.0.0.1} when Floci's own port is published on loopback only
|
||||
* (a {@code "127.0.0.1:4566:4566"} mapping, say): the console is unauthenticated and
|
||||
* drives every emulated service, so leaving it on every interface would hand out an
|
||||
* authority the API deliberately withholds.
|
||||
*
|
||||
* <p>A blank value is a hard error rather than a silent fall back to the wildcard: an
|
||||
* operator who set the key meant to choose an address.
|
||||
*/
|
||||
Optional<String> bindAddress();
|
||||
|
||||
/**
|
||||
* Port the console listens on <em>inside</em> its container, published as {@link #port()}.
|
||||
* Env: {@code FLOCI_SERVICES_UI_INTERNAL_PORT}
|
||||
|
||||
@@ -132,6 +132,7 @@ public class ContainerBuilder {
|
||||
private boolean readonlyRootfs;
|
||||
private final Map<Integer, Integer> portBindings = new HashMap<>();
|
||||
private final List<Integer> loopbackPortBindings = new ArrayList<>();
|
||||
private final Map<Integer, String> portBindingHostIps = new HashMap<>();
|
||||
private final List<Integer> exposedPorts = new ArrayList<>();
|
||||
private String networkMode;
|
||||
private final List<Mount> mounts = new ArrayList<>();
|
||||
@@ -263,6 +264,21 @@ public class ContainerBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a port binding to a specific host port on a specific host interface.
|
||||
*
|
||||
* <p>Use this for a port whose reachability is an operator decision rather than a fixed
|
||||
* property of the container: the caller passes the configured address through, and a null
|
||||
* or blank one falls back to Docker's own default of publishing on every interface.
|
||||
*/
|
||||
public Builder withPortBinding(int containerPort, int hostPort, String hostIp) {
|
||||
withPortBinding(containerPort, hostPort);
|
||||
if (hostIp != null && !hostIp.isBlank()) {
|
||||
this.portBindingHostIps.put(containerPort, hostIp.strip());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes a container port to the host loopback interface only.
|
||||
*
|
||||
@@ -657,7 +673,8 @@ public class ContainerBuilder {
|
||||
nanoCpus,
|
||||
cpuShares,
|
||||
readonlyRootfs,
|
||||
List.copyOf(linkLocalIps)
|
||||
List.copyOf(linkLocalIps),
|
||||
Map.copyOf(portBindingHostIps)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+25
-4
@@ -63,6 +63,9 @@ public class ContainerLifecycleManager {
|
||||
|
||||
private static final long NANO_CPUS_PER_CPU = 1_000_000_000L;
|
||||
|
||||
/** Host interface a port marked loopback-only publishes on. */
|
||||
private static final String LOOPBACK_HOST_IP = "127.0.0.1";
|
||||
|
||||
private final DockerClient dockerClient;
|
||||
private final ImageCacheService imageCacheService;
|
||||
private final ContainerDetector containerDetector;
|
||||
@@ -999,10 +1002,7 @@ public class ContainerLifecycleManager {
|
||||
hostPort = portAllocator.allocateAny();
|
||||
}
|
||||
|
||||
Ports.Binding binding = spec.loopbackPortBindings().contains(containerPort)
|
||||
? Ports.Binding.bindIpAndPort("127.0.0.1", hostPort)
|
||||
: Ports.Binding.bindPort(hostPort);
|
||||
ports.bind(ExposedPort.tcp(containerPort), binding);
|
||||
ports.bind(ExposedPort.tcp(containerPort), bindingFor(spec, containerPort, hostPort));
|
||||
LOG.debugv("Port binding: {0} -> {1}", String.valueOf(containerPort), String.valueOf(hostPort));
|
||||
}
|
||||
hostConfig.withPortBindings(ports);
|
||||
@@ -1058,6 +1058,27 @@ public class ContainerLifecycleManager {
|
||||
return hostConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* The host interface a published port binds to.
|
||||
*
|
||||
* <p>An explicit address from {@code portBindingHostIps} wins, so a caller that reads the
|
||||
* address from configuration keeps control of it. {@code loopbackPortBindings} is the fixed
|
||||
* form of the same decision, for a port that is an implementation detail and must never leave
|
||||
* the host. A port in neither binds every interface, which is Docker's own default and what
|
||||
* every caller that does not ask has always got.
|
||||
*/
|
||||
private static Ports.Binding bindingFor(ContainerSpec spec, int containerPort, int hostPort) {
|
||||
String hostIp = spec.portBindingHostIps() == null
|
||||
? null
|
||||
: spec.portBindingHostIps().get(containerPort);
|
||||
if (hostIp != null && !hostIp.isBlank()) {
|
||||
return Ports.Binding.bindIpAndPort(hostIp, hostPort);
|
||||
}
|
||||
return spec.loopbackPortBindings().contains(containerPort)
|
||||
? Ports.Binding.bindIpAndPort(LOOPBACK_HOST_IP, hostPort)
|
||||
: Ports.Binding.bindPort(hostPort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds a CPU quota to what the daemon will accept. Docker rejects a NanoCpus above the
|
||||
* host's CPU count ("range of CPUs is from 0.01 to 8.00, as there are only 8 CPUs
|
||||
|
||||
@@ -43,6 +43,8 @@ import java.util.regex.Pattern;
|
||||
* @param cpuShares Relative CPU weight against other containers (null = daemon default)
|
||||
* @param readonlyRootfs Whether the container's own filesystem is mounted read only
|
||||
* @param linkLocalIps Link-local IPv4 addresses assigned to the container's endpoint on the configured network
|
||||
* @param portBindingHostIps Host interface each published port binds to, keyed by container port.
|
||||
* A port with no entry here, and not in {@code loopbackPortBindings}, binds every interface
|
||||
*/
|
||||
public record ContainerSpec(
|
||||
String image,
|
||||
@@ -71,7 +73,8 @@ public record ContainerSpec(
|
||||
Long nanoCpus,
|
||||
Integer cpuShares,
|
||||
boolean readonlyRootfs,
|
||||
List<String> linkLocalIps
|
||||
List<String> linkLocalIps,
|
||||
Map<Integer, String> portBindingHostIps
|
||||
) {
|
||||
private static final Pattern LINK_LOCAL_IPV4 = Pattern.compile("^169\\.254\\.(\\d{1,3})\\.(\\d{1,3})$");
|
||||
private static final Set<String> NETWORKS_WITHOUT_ENDPOINT_IPAM = Set.of("bridge", "default", "host", "none");
|
||||
@@ -90,7 +93,7 @@ public record ContainerSpec(
|
||||
public ContainerSpec(String image) {
|
||||
this(image, null, List.of(), null, null, null, Map.of(), List.of(), List.of(), null,
|
||||
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());
|
||||
null, null, List.of(), List.of(), null, null, false, List.of(), Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,7 +124,8 @@ public record ContainerSpec(
|
||||
) {
|
||||
this(image, name, env, cmd, entrypoint, memoryBytes, portBindings, List.of(), exposedPorts,
|
||||
networkMode, mounts, binds, List.of(), extraHosts, labels, logConfig, privileged,
|
||||
cgroupnsMode, dnsServers, workingDir, user, groupAdd, List.of(), null, null, false, List.of());
|
||||
cgroupnsMode, dnsServers, workingDir, user, groupAdd, List.of(), null, null, false,
|
||||
List.of(), Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,7 +159,7 @@ public record ContainerSpec(
|
||||
this(image, name, env, cmd, entrypoint, memoryBytes, portBindings, loopbackPortBindings,
|
||||
exposedPorts, networkMode, mounts, binds, List.of(), extraHosts, labels, logConfig,
|
||||
privileged, cgroupnsMode, dnsServers, workingDir, user, groupAdd, List.of(),
|
||||
null, null, false, List.of());
|
||||
null, null, false, List.of(), Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,7 +193,7 @@ public record ContainerSpec(
|
||||
this(image, name, env, cmd, entrypoint, memoryBytes, portBindings, loopbackPortBindings,
|
||||
exposedPorts, networkMode, mounts, binds, List.of(), extraHosts, labels, logConfig,
|
||||
privileged, cgroupnsMode, dnsServers, workingDir, user, groupAdd, deviceRequests,
|
||||
null, null, false, List.of());
|
||||
null, null, false, List.of(), Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -174,6 +174,9 @@ public class FlociUiManager {
|
||||
this.lastError = null;
|
||||
String image = config.services().ui().image();
|
||||
try {
|
||||
// Before the image pull: a rejected bind address is a configuration error, and
|
||||
// downloading a console that is not going to be published is wasted work.
|
||||
String bindAddress = resolveBindAddress(config.services().ui().bindAddress());
|
||||
this.profile = resolveProfile(image);
|
||||
String name = ContainerStorageHelper.dockerName(config, config.services().ui().containerName());
|
||||
|
||||
@@ -190,7 +193,7 @@ public class FlociUiManager {
|
||||
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
|
||||
.withName(name)
|
||||
.withEnv(injectedEnv(profile))
|
||||
.withPortBinding(internalPort, chosenPort)
|
||||
.withPortBinding(internalPort, chosenPort, bindAddress)
|
||||
.withDockerNetwork(resolveDockerNetwork())
|
||||
.withLogRotation();
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
@@ -205,8 +208,13 @@ public class FlociUiManager {
|
||||
this.probeUrl = resolveProbeUrl(profile, endpoint, hostPort);
|
||||
this.started = true;
|
||||
this.lastError = null;
|
||||
LOG.infov("Started web console sidecar {0} from {1} on host port {2}",
|
||||
name, image, String.valueOf(hostPort));
|
||||
if (bindAddress == null) {
|
||||
LOG.infov("Started web console sidecar {0} from {1} on host port {2}",
|
||||
name, image, String.valueOf(hostPort));
|
||||
} else {
|
||||
LOG.infov("Started web console sidecar {0} from {1} on {2}:{3}",
|
||||
name, image, bindAddress, String.valueOf(hostPort));
|
||||
}
|
||||
attachLogStream(false);
|
||||
} catch (IllegalStateException e) {
|
||||
// replaceIfEndpointDrifted() records its own specific message for a container with an
|
||||
@@ -499,6 +507,31 @@ public class FlociUiManager {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The host interface the console is published on, or null to publish on every interface.
|
||||
*
|
||||
* <p>Unset is the default and is Docker's own behaviour, which is what the console has always
|
||||
* had. Setting it is how an operator who keeps Floci's own port on loopback keeps the console
|
||||
* there too: the console is unauthenticated and drives every emulated service, so a wildcard
|
||||
* publish would hand out an authority the API mapping deliberately withholds.
|
||||
*
|
||||
* <p>A blank value is an error rather than a silent fall back to the wildcard: an operator who
|
||||
* set the key meant to choose an address, and substituting a different one is how a binding
|
||||
* ends up somewhere nobody intended.
|
||||
*/
|
||||
static String resolveBindAddress(Optional<String> configured) {
|
||||
if (configured.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
String value = configured.get().trim();
|
||||
if (value.isEmpty()) {
|
||||
throw new IllegalStateException(
|
||||
"floci.services.ui.bind-address is set but blank: remove it to publish the "
|
||||
+ "console on every interface, or give it a host address to bind.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an adoption candidate must be destroyed and recreated rather than adopted.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user