diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaPublicDnsResolutionTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaPublicDnsResolutionTest.java new file mode 100644 index 0000000000..bb7aea25cf --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaPublicDnsResolutionTest.java @@ -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. + * + *
Regression for #1110: public + * lookups failed with {@code ENOTFOUND}/{@code EBUSY} because the forwarder truncated responses + * and spawned containers had no fallback resolver. + * + *
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;
+ }
+ }
+}
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java
index c35b6d0298..336e297f34 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java
@@ -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();
diff --git a/docs/services/lambda.md b/docs/services/lambda.md
index 420009db29..de024b33a8 100644
--- a/docs/services/lambda.md
+++ b/docs/services/lambda.md
@@ -223,12 +223,29 @@ on its container IP. All Lambda containers launched by Floci are configured to
use it as their DNS resolver. The embedded DNS server:
- Resolves `*.localhost.floci.io` → Floci's Docker network IP
-- Forwards all other queries to the upstream resolver from `/etc/resolv.conf`
+- Forwards all other queries to the upstream resolver(s) from `/etc/resolv.conf`,
+ falling back to public resolvers so **public hostnames** (e.g.
+ `business-api.tiktok.com`) resolve from inside Lambda containers
No extra configuration or `cap_add` is needed — Docker containers have
`CAP_NET_BIND_SERVICE` in their default capability set, so Floci (running as a
non-root user) can bind UDP/53 without any changes to your Compose file.
+!!! note "Resolving public hostnames from Lambda"
+ A Lambda whose handler reaches a public host (`fetch()`/HTTPS to e.g.
+ `business-api.tiktok.com`) resolves it through Floci's embedded DNS. As a
+ safety net, Floci also appends configurable public resolvers (default
+ `8.8.8.8`, `8.8.4.4`) after its own IP on each spawned container's DNS list,
+ so name resolution still works if the embedded forwarder cannot answer.
+
+ Tune or disable this for offline / locked-down networks where those resolvers
+ are blocked:
+
+ ```bash
+ FLOCI_DNS_CONTAINER_FALLBACK_SERVERS=1.1.1.1,1.0.0.1 # use different resolvers
+ FLOCI_DNS_CONTAINER_FALLBACK_ENABLED=false # inject only Floci's DNS
+ ```
+
!!! tip "Docker Compose service names"
If Floci runs as a Docker Compose service, set `FLOCI_HOSTNAME` to the
service name, for example `FLOCI_HOSTNAME=floci`. When no explicit Lambda
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
index c720e97614..fe6f02fcb0 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -91,6 +91,32 @@ public interface EmulatorConfig {
*
*/
Optional Disable (via {@code FLOCI_DNS_CONTAINER_FALLBACK_ENABLED=false}) in offline or
+ * locked-down networks where the public resolvers are unreachable/blocked.
+ */
+ @WithDefault("true")
+ boolean containerFallbackEnabled();
+
+ /**
+ * Ordered list of public DNS resolvers used both as the fallback upstream for Floci's
+ * embedded DNS forwarder and (when {@link #containerFallbackEnabled()}) as the secondary
+ * resolvers injected into spawned containers.
+ *
+ * Via environment variable (comma-separated):
+ * When {@code floci.dns.container-fallback-enabled} is set, the configured public
+ * fallback resolvers are appended after Floci's IP so the container's own resolver can
+ * fall through for public hostnames if Floci's embedded forwarder cannot answer.
*/
public Builder withEmbeddedDns() {
- embeddedDnsServer.getServerIp().ifPresent(dnsServers::add);
+ embeddedDnsServer.getServerIp().ifPresent(flociIp -> {
+ dnsServers.add(flociIp);
+ if (config.dns().containerFallbackEnabled()) {
+ for (String fallback : config.dns().containerFallbackServers()) {
+ if (fallback != null && !fallback.isBlank() && !dnsServers.contains(fallback.trim())) {
+ dnsServers.add(fallback.trim());
+ }
+ }
+ }
+ });
return this;
}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index bb62b3dffd..d1f8506256 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -93,6 +93,15 @@ floci:
# extra-suffixes:
# - localhost.localstack.cloud
+ # Public resolvers appended after Floci's embedded DNS on spawned containers' HostConfig.Dns,
+ # and used as the fallback upstream for Floci's own DNS forwarder. Lets Lambda/CodeBuild
+ # resolve public hostnames even if the embedded forwarder cannot answer.
+ # Disable in offline/locked-down networks where these resolvers are blocked.
+ container-fallback-enabled: true # FLOCI_DNS_CONTAINER_FALLBACK_ENABLED
+ container-fallback-servers: # FLOCI_DNS_CONTAINER_FALLBACK_SERVERS=1.1.1.1,1.0.0.1
+ - 8.8.8.8
+ - 8.8.4.4
+
auth:
validate-signatures: false
presign-secret: local-emulator-secret
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java
index 4c9563eeda..fdbd346946 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java
@@ -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> extraSuffixes();
+
+ /**
+ * When {@code true} (default), the configured {@link #containerFallbackServers()} are
+ * appended after Floci's embedded DNS to every spawned container's {@code HostConfig.Dns}.
+ * This gives Lambda/CodeBuild/etc. a real secondary resolver so public hostnames still
+ * resolve if Floci's embedded forwarder cannot answer — mirroring the
+ * {@code docker run --dns
+ * FLOCI_DNS_CONTAINER_FALLBACK_SERVERS=1.1.1.1,1.0.0.1
+ *
+ */
+ @WithDefault("8.8.8.8,8.8.4.4")
+ List> extraSuffixes() { return Optional.empty(); }
+ @Override
+ public boolean containerFallbackEnabled() { return true; }
+ @Override
+ public List