fix(dns): resolve public hostnames from spawned containers (#1168)

Lambda/CodeBuild containers get Floci's IP as their only DNS, so all
public-name resolution flows through Floci's embedded UDP/53 forwarder.
The forwarder read responses into a hardcoded 512-byte buffer, silently
truncating larger EDNS0 responses (e.g. CDN-backed hosts), so public
lookups failed inside Lambda with ENOTFOUND/EBUSY.

- Enlarge the forwarder receive buffer to 4096 bytes (EDNS0).
- Forward across an ordered upstream list (resolv.conf + configurable
  public fallbacks), returning the first valid response; warn when all
  upstreams fail instead of dropping the response silently.
- Append configurable fallback resolvers (default 8.8.8.8, 8.8.4.4)
  after Floci's IP on spawned containers' HostConfig.Dns, controllable
  via floci.dns.container-fallback-* / FLOCI_DNS_CONTAINER_FALLBACK_*.

Closes #1110
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-06-04 00:42:38 -05:00
1 parent 7128758a63
commit 2d30641479
10 files changed
+401 -24

No files matched your search

@@ -3,6 +3,9 @@ package io.github.hectorvent.floci.core.common.dns;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.List;
@@ -194,6 +197,83 @@ class EmbeddedDnsServerTest {
assertEquals((byte) 42, resp.get());
}
// ── composeUpstreams — forwarder upstream ordering ────────────────────────
@Test
void composeUpstreams_resolvConfFirstThenFallbacks() {
List<String> upstreams = EmbeddedDnsServer.composeUpstreams(
List.of("192.168.65.7"), List.of("8.8.8.8", "8.8.4.4"));
assertEquals(List.of("192.168.65.7", "8.8.8.8", "8.8.4.4"), upstreams);
}
@Test
void composeUpstreams_usesDockerResolverBaselineWhenResolvConfEmpty() {
List<String> upstreams = EmbeddedDnsServer.composeUpstreams(
List.of(), List.of("8.8.8.8"));
// Docker's embedded resolver is the baseline, then the configured fallback.
assertEquals(List.of("127.0.0.11", "8.8.8.8"), upstreams);
}
@Test
void composeUpstreams_skipsLoopbackAndBlankEntries() {
List<String> upstreams = EmbeddedDnsServer.composeUpstreams(
List.of("127.0.0.1", " ", "10.0.0.2"), List.of("", "8.8.8.8"));
assertEquals(List.of("10.0.0.2", "8.8.8.8"), upstreams);
}
@Test
void composeUpstreams_dedupesAcrossResolvConfAndFallbacks() {
List<String> upstreams = EmbeddedDnsServer.composeUpstreams(
List.of("8.8.8.8"), List.of("8.8.8.8", "8.8.4.4"));
assertEquals(List.of("8.8.8.8", "8.8.4.4"), upstreams);
}
@Test
void composeUpstreams_toleratesNullFallbacks() {
List<String> upstreams = EmbeddedDnsServer.composeUpstreams(List.of("10.0.0.2"), null);
assertEquals(List.of("10.0.0.2"), upstreams);
}
// ── forwarding — response buffer size (issue #1110 regression) ────────────
@Test
void forwardToUpstreams_returnsResponseLargerThan512BytesIntact() throws Exception {
// Regression guard: a 512-byte receive buffer silently truncated EDNS0 responses from
// CDN-backed public hosts, corrupting the answer forwarded back to the Lambda container.
byte[] bigResponse = new byte[1500];
for (int i = 0; i < bigResponse.length; i++) {
bigResponse[i] = (byte) (i & 0xFF);
}
try (DatagramSocket responder = new DatagramSocket(0, InetAddress.getByName("127.0.0.1"))) {
startResponder(responder, bigResponse);
byte[] query = buildQuery("business-api.tiktok.com", (short) 0x1234);
byte[] response = dns.forwardToUpstreams(
query, List.of("127.0.0.1"), responder.getLocalPort());
assertEquals(bigResponse.length, response.length,
"response larger than 512 bytes must be forwarded without truncation");
assertArrayEquals(bigResponse, response);
}
}
/** Replies to the first datagram received with a fixed payload, on a daemon thread. */
private void startResponder(DatagramSocket responder, byte[] responsePayload) {
Thread t = new Thread(() -> {
try {
DatagramPacket req = new DatagramPacket(new byte[4096], 4096);
responder.receive(req);
responder.send(new DatagramPacket(
responsePayload, responsePayload.length, req.getAddress(), req.getPort()));
} catch (Exception ignored) {
// socket closed when the test completes
}
});
t.setDaemon(true);
t.start();
}
// ── helpers ───────────────────────────────────────────────────────────────
private byte[] encodeName(String name) {
@@ -4,6 +4,7 @@ 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.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -49,10 +50,48 @@ class ContainerBuilderTest {
assertEquals("avoxx-network", spec.networkMode());
}
@Test
void withEmbeddedDns_appendsFallbackResolversAfterFlociIp() {
TestFixture fixture = new TestFixture();
when(fixture.embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.4"));
ContainerSpec spec = fixture.builder.newContainer("alpine")
.withEmbeddedDns()
.build();
assertEquals(List.of("172.18.0.4", "8.8.8.8", "8.8.4.4"), spec.dnsServers());
}
@Test
void withEmbeddedDns_omitsFallbackResolversWhenDisabled() {
TestFixture fixture = new TestFixture();
when(fixture.embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.4"));
when(fixture.dns.containerFallbackEnabled()).thenReturn(false);
ContainerSpec spec = fixture.builder.newContainer("alpine")
.withEmbeddedDns()
.build();
assertEquals(List.of("172.18.0.4"), spec.dnsServers());
}
@Test
void withEmbeddedDns_noOpWhenEmbeddedDnsNotRunning() {
TestFixture fixture = new TestFixture();
// getServerIp() empty (default) — no Floci IP, so no fallbacks are injected either.
ContainerSpec spec = fixture.builder.newContainer("alpine")
.withEmbeddedDns()
.build();
assertEquals(List.of(), spec.dnsServers());
}
private static class TestFixture {
final EmulatorConfig config = mock(EmulatorConfig.class);
final EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
final EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class);
final EmulatorConfig.DnsConfig dns = mock(EmulatorConfig.DnsConfig.class);
final DockerHostResolver dockerHostResolver = mock(DockerHostResolver.class);
final EmbeddedDnsServer embeddedDnsServer = mock(EmbeddedDnsServer.class);
final CurrentContainerNetworkResolver currentContainerNetworkResolver =
@@ -66,6 +105,9 @@ class ContainerBuilderTest {
when(config.docker()).thenReturn(docker);
when(docker.logMaxSize()).thenReturn("10m");
when(docker.logMaxFile()).thenReturn("3");
when(config.dns()).thenReturn(dns);
when(dns.containerFallbackEnabled()).thenReturn(true);
when(dns.containerFallbackServers()).thenReturn(List.of("8.8.8.8", "8.8.4.4"));
when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty());
}
}
@@ -299,7 +299,16 @@ class EventBridgeSchedulerIntegrationTest {
@Override
public StorageConfig storage() { return null; }
@Override
public DnsConfig dns() { return Optional::empty; }
public DnsConfig dns() {
return new DnsConfig() {
@Override
public Optional<List<String>> extraSuffixes() { return Optional.empty(); }
@Override
public boolean containerFallbackEnabled() { return true; }
@Override
public List<String> containerFallbackServers() { return List.of("8.8.8.8", "8.8.4.4"); }
};
}
@Override
public AuthConfig auth() { return null; }
@Override