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

@@ -0,0 +1,106 @@
package com.floci.test;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.lambda.LambdaClient;
import software.amazon.awssdk.services.lambda.model.CreateFunctionRequest;
import software.amazon.awssdk.services.lambda.model.DeleteFunctionRequest;
import software.amazon.awssdk.services.lambda.model.FunctionCode;
import software.amazon.awssdk.services.lambda.model.InvocationType;
import software.amazon.awssdk.services.lambda.model.InvokeRequest;
import software.amazon.awssdk.services.lambda.model.InvokeResponse;
import software.amazon.awssdk.services.lambda.model.Runtime;
import java.net.InetAddress;
import java.time.Duration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
/**
* Verifies that a Lambda container can resolve a public hostname through Floci's embedded DNS
* server, which forwards non-Floci names to its upstream / configured fallback resolvers.
*
* <p>Regression for <a href="https://github.com/floci-io/floci/issues/1110">#1110</a>: public
* lookups failed with {@code ENOTFOUND}/{@code EBUSY} because the forwarder truncated responses
* and spawned containers had no fallback resolver.
*
* <p>Requires Docker dispatch (docker.sock mounted) and outbound internet; self-skips otherwise.
*/
@DisplayName("Lambda DNS — public hostname resolution from inside container")
class LambdaPublicDnsResolutionTest {
private static final String FUNCTION_NAME = "public-dns-probe-fn";
private static final String PUBLIC_HOST = "example.com";
private static final String ROLE = "arn:aws:iam::000000000000:role/lambda-role";
private static LambdaClient lambda;
private static final ObjectMapper MAPPER = new ObjectMapper();
@BeforeAll
static void setup() {
assumeTrue(TestFixtures.isLambdaDispatchAvailable(),
"Lambda dispatch unavailable — skipping public DNS resolution test");
assumeTrue(hasInternet(),
"No outbound internet on the test host — skipping public DNS resolution test");
lambda = TestFixtures.lambdaClient();
lambda.createFunction(CreateFunctionRequest.builder()
.functionName(FUNCTION_NAME)
.runtime(Runtime.NODEJS20_X)
.role(ROLE)
.handler("index.handler")
.timeout(30)
.code(FunctionCode.builder()
.zipFile(SdkBytes.fromByteArray(LambdaUtils.publicDnsLookupZip()))
.build())
.build());
}
@AfterAll
static void cleanup() {
if (lambda != null) {
try { lambda.deleteFunction(DeleteFunctionRequest.builder().functionName(FUNCTION_NAME).build()); } catch (Exception ignored) {}
lambda.close();
}
}
@Test
@DisplayName("Lambda resolves a public hostname via embedded DNS forwarding")
void lambdaResolvesPublicHostname() throws Exception {
String payload = MAPPER.writeValueAsString(
MAPPER.createObjectNode().put("host", PUBLIC_HOST));
InvokeResponse response = lambda.invoke(InvokeRequest.builder()
.functionName(FUNCTION_NAME)
.invocationType(InvocationType.REQUEST_RESPONSE)
.payload(SdkBytes.fromUtf8String(payload))
.overrideConfiguration(c -> c.apiCallTimeout(Duration.ofSeconds(30)))
.build());
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.functionError())
.as("public DNS lookup must not fail (ENOTFOUND/EBUSY) inside the Lambda container")
.isNullOrEmpty();
JsonNode result = MAPPER.readTree(response.payload().asUtf8String());
assertThat(result.path("resolved").asBoolean()).isTrue();
assertThat(result.path("address").asText())
.as("resolved public IP address")
.isNotBlank();
}
private static boolean hasInternet() {
try {
InetAddress.getByName(PUBLIC_HOST);
return true;
} catch (Exception e) {
return false;
}
}
}
@@ -189,6 +189,26 @@ public final class LambdaUtils {
return createZip("index.js", code);
}
/**
* ZIP containing a Node.js handler that resolves a public hostname via {@code dns.lookup}
* (the same getaddrinfo path used by {@code fetch}). Receives {@code {host}} in the event.
* Used to verify Floci's embedded DNS forwards public lookups from inside Lambda containers
* (regression for https://github.com/floci-io/floci/issues/1110).
*/
public static byte[] publicDnsLookupZip() {
String code = """
const dns = require('dns').promises;
exports.handler = async (event) => {
const host = (event && event.host) ? event.host : 'example.com';
console.log('[public-dns] resolving', host);
const res = await dns.lookup(host);
console.log('[public-dns] resolved', host, '->', res.address);
return { resolved: true, host, address: res.address };
};
""";
return createZip("index.js", code);
}
private static byte[] createZip(String filename, String content) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();