feat(core): relay container callbacks to a loopback Floci on a Linux host (#5774)

* feat(core): relay container callbacks to a loopback Floci on a Linux host

Containers Floci starts call back through host.docker.internal, which native
Linux Docker maps to the bridge gateway (host-gateway, normally 172.17.0.1).
Docker Desktop forwards that address to the host's loopback, but Linux does
not, so a Floci binary run on a Linux host and bound to 127.0.0.1 (the default)
refused every SDK call from a Lambda function, ECS task or CodeBuild build.
The documented workaround was to listen on 0.0.0.0 with the exposure consent.

BridgeGatewayRelay now listens on the default bridge's gateway address, which
belongs to the host but is not routed from the LAN, and pipes each connection
to Floci's port on loopback. It runs only on a Linux host outside a container,
only while the public listener is on loopback, and only when Docker answers.
The quick start says so.

Refs #5767

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(core): relay every port containers call and close cleanly on shutdown

Review follow-ups for the bridge gateway relay:

- It relays the ports ContainerReachableEndpoint advertises: without TLS the
  HTTP server's port, which can differ from floci.port; with TLS the
  proxy's public port plus its AWS-HTTPS port (a failed bind on that
  privileged port is logged at debug, as the proxy's own is warned once).
- It forwards to the loopback address Floci listens on (::1, localhost,
  127.0.0.2, ...) instead of always 127.0.0.1.
- A shutdown racing the network lookup or a bind closes whatever the
  startup opens afterwards.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(core): relay only connections that come from a Docker network

Linux accepts packets for any of its own addresses on any interface, so a
LAN neighbour that routes the bridge gateway address through this host could
reach the relay and, through it, a Floci bound to loopback.

The relay now reads every Docker network's subnets and relays a connection
only when its source lies in one of them; anything else is closed. A source
outside the known subnets refreshes the list once, at most every five
seconds, so a network created after startup is picked up.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-16 07:05:34 -04:00
1 parent 4c32fae5e3
commit a2d02ef967
4 files changed
+411 -2

No files matched your search

@@ -0,0 +1,99 @@
package io.github.hectorvent.floci.core.common.docker;
import com.github.dockerjava.api.model.Network;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class BridgeGatewayRelayTest {
@Test
void runsOnlyOnALinuxHostListeningOnLoopback() {
assertThat(BridgeGatewayRelay.shouldRun("Linux", false, "127.0.0.1"), is(true));
assertThat(BridgeGatewayRelay.shouldRun("Linux", false, "localhost"), is(true));
assertThat(BridgeGatewayRelay.shouldRun("Linux", true, "127.0.0.1"), is(false));
assertThat(BridgeGatewayRelay.shouldRun("Linux", false, "0.0.0.0"), is(false));
assertThat(BridgeGatewayRelay.shouldRun("Mac OS X", false, "127.0.0.1"), is(false));
assertThat(BridgeGatewayRelay.shouldRun("Windows 11", false, "127.0.0.1"), is(false));
}
@Test
void withoutTlsTheHttpPortIsRelayedEvenWhenItDiffersFromTheFlociPort() {
assertThat(BridgeGatewayRelay.relayPorts(false, 4566, 4570, 443), equalTo(List.of(4570)));
}
@Test
void withTlsTheProxyPortsAreRelayed() {
assertThat(BridgeGatewayRelay.relayPorts(true, 4566, 4510, 443), equalTo(List.of(4566, 443)));
assertThat(BridgeGatewayRelay.relayPorts(true, 4566, 4510, 0), equalTo(List.of(4566)));
assertThat(BridgeGatewayRelay.relayPorts(true, 443, 4510, 443), equalTo(List.of(443)));
}
@Test
void onlySourcesOnADockerNetworkAreRelayed() {
List<String> subnets = List.of("172.17.0.0/16", "172.18.0.0/16", "fd00:d0c::/64");
assertThat(BridgeGatewayRelay.inAnySubnet("172.17.0.5", subnets), is(true));
assertThat(BridgeGatewayRelay.inAnySubnet("172.18.3.9", subnets), is(true));
assertThat(BridgeGatewayRelay.inAnySubnet("::ffff:172.17.0.5", subnets), is(true));
assertThat(BridgeGatewayRelay.inAnySubnet("fd00:d0c::7", subnets), is(true));
// A LAN neighbour routing 172.17.0.1 through this host.
assertThat(BridgeGatewayRelay.inAnySubnet("192.168.1.20", subnets), is(false));
assertThat(BridgeGatewayRelay.inAnySubnet("172.19.0.5", subnets), is(false));
assertThat(BridgeGatewayRelay.inAnySubnet("192.168.1.20", List.of()), is(false));
}
@Test
void subnetMatchingRejectsWhatIsNotAnIpOrACidr() {
assertThat(BridgeGatewayRelay.inSubnet("example.com", "172.17.0.0/16"), is(false));
assertThat(BridgeGatewayRelay.inSubnet("172.17.0.5", "172.17.0.0/x"), is(false));
assertThat(BridgeGatewayRelay.inSubnet("172.17.0.5", "172.17.0.0/40"), is(false));
assertThat(BridgeGatewayRelay.inSubnet("10.1.2.3", "0.0.0.0/0"), is(true));
}
@Test
void subnetsAreReadFromEveryNetwork() {
assertThat(BridgeGatewayRelay.subnetsOf(List.of(network(subnet("172.17.0.0/16"), subnet(null)),
network(subnet("172.18.0.0/16"), subnet("fd00:d0c::/64")), mock(Network.class))),
equalTo(List.of("172.17.0.0/16", "172.18.0.0/16", "fd00:d0c::/64")));
}
@Test
void gatewayIsTheFirstIpv4Gateway() {
assertThat(BridgeGatewayRelay.gatewayOf(network(ipam("fd00::1"), ipam("172.17.0.1"))), equalTo("172.17.0.1"));
}
@Test
void noGatewayWhenTheBridgeHasNone() {
assertThat(BridgeGatewayRelay.gatewayOf(network(ipam(null))), is(nullValue()));
assertThat(BridgeGatewayRelay.gatewayOf(null), is(nullValue()));
assertThat(BridgeGatewayRelay.gatewayOf(mock(Network.class)), is(nullValue()));
}
private static Network.Ipam.Config ipam(String gateway) {
Network.Ipam.Config config = mock(Network.Ipam.Config.class);
when(config.getGateway()).thenReturn(gateway);
return config;
}
private static Network.Ipam.Config subnet(String subnet) {
Network.Ipam.Config config = mock(Network.Ipam.Config.class);
when(config.getSubnet()).thenReturn(subnet);
return config;
}
private static Network network(Network.Ipam.Config... configs) {
Network network = mock(Network.class);
Network.Ipam ipam = mock(Network.Ipam.class);
when(network.getIpam()).thenReturn(ipam);
when(ipam.getConfig()).thenReturn(List.of(configs));
return network;
}
}