feat(lambda): embedded DNS server for virtual-hosted S3 resolution (#585)

* feat(lambda): embedded DNS server for virtual-hosted S3 resolution

  Lambda containers cannot resolve virtual-hosted S3 URLs
  (my-bucket.floci:4566) because Docker DNS has no wildcard support —
  queries for *.floci fall through to public DNS and hit real AWS.

  Adds a Vert.x UDP/53 DNS server that binds automatically when Floci
  runs inside Docker. It resolves any *.{floci.hostname} query to
  Floci's container IP and forwards everything else upstream. Lambda
  containers receive Floci's IP as their DNS server via HostConfig.withDns().

  An optional floci.dns.extra-suffixes config allows LocalStack migrants
  to route *.localhost.localstack.cloud without changing Lambda code.

  Netty NioDatagramChannel was tried first but silently dropped IPv4
  UDP packets due to AF_INET6 socket defaults — Vert.x DatagramSocket
  with setIpV6(false) resolves this.

* fix: adding missing dns class
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-25 09:33:00 -05:00
1 parent 88f40a839b
commit e023936a81
13 files changed
+533 -8

No files matched your search

@@ -53,6 +53,8 @@ public interface EmulatorConfig {
StorageConfig storage();
DnsConfig dns();
AuthConfig auth();
ServicesConfig services();
@@ -61,6 +63,27 @@ public interface EmulatorConfig {
InitHooksConfig initHooks();
interface DnsConfig {
/**
* Additional hostname suffixes the embedded DNS server will resolve to Floci's
* container IP, alongside the primary {@code floci.hostname}.
*
* Useful for migrating from LocalStack without changing Lambda endpoint configuration:
* <pre>
* floci:
* dns:
* extra-suffixes:
* - localhost.localstack.cloud
* </pre>
*
* Via environment variable (comma-separated for multiple values):
* <pre>
* FLOCI_DNS_EXTRA_SUFFIXES=localhost.localstack.cloud,localhost.example.internal
* </pre>
*/
Optional<List<String>> extraSuffixes();
}
interface StorageConfig {
@WithDefault("hybrid")
String mode();
@@ -0,0 +1,233 @@
package io.github.hectorvent.floci.core.common.dns;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.datagram.DatagramSocket;
import io.vertx.core.datagram.DatagramSocketOptions;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/**
* Embedded UDP/53 DNS server that runs inside the Floci container and is injected
* into every spawned container (Lambda, RDS, ElastiCache) as their DNS resolver.
*
* Resolves *.{floci.hostname} (and any configured extra-suffixes) to Floci's own
* Docker network IP so virtual-hosted S3 URLs (my-bucket.floci:4566) work from
* inside Lambda containers without requiring wildcard Docker aliases.
*
* All other queries are forwarded transparently to the upstream resolver read from
* /etc/resolv.conf (Docker's embedded DNS at 127.0.0.11).
*
* Only starts when Floci detects it is running inside Docker. No-op on the host.
*/
@ApplicationScoped
public class EmbeddedDnsServer {
private static final Logger LOG = Logger.getLogger(EmbeddedDnsServer.class);
private static final int DNS_PORT = 53;
private static final int TTL = 60;
private static final String FALLBACK_UPSTREAM = "127.0.0.11";
private volatile String serverIp;
private final List<String> suffixes = new ArrayList<>();
private String upstreamDns;
EmbeddedDnsServer(List<String> suffixes) {
this.suffixes.addAll(suffixes);
}
@Inject
public EmbeddedDnsServer(EmulatorConfig config, ContainerDetector containerDetector, Vertx vertx) {
if (!containerDetector.isRunningInContainer()) {
return;
}
try {
String myIp = InetAddress.getLocalHost().getHostAddress();
upstreamDns = readUpstreamDns();
config.hostname().ifPresent(suffixes::add);
config.dns().extraSuffixes().ifPresent(suffixes::addAll);
DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions().setIpV6(false));
socket.listen(DNS_PORT, "0.0.0.0", ar -> {
if (ar.succeeded()) {
serverIp = myIp;
LOG.infov("Embedded DNS server started on {0}:53, resolving {1} → {0}", myIp, suffixes);
socket.handler(packet -> handleQuery(
vertx, socket, packet.data().getBytes(),
packet.sender().host(), packet.sender().port(), myIp));
} else {
LOG.warnv("Embedded DNS server failed to bind on port 53: {0}", ar.cause().getMessage());
}
});
} catch (Exception e) {
LOG.warnv("Failed to initialize embedded DNS server: {0}", e.getMessage());
}
}
public Optional<String> getServerIp() {
return Optional.ofNullable(serverIp);
}
// ── packet handling ───────────────────────────────────────────────────────
private void handleQuery(Vertx vertx, DatagramSocket socket, byte[] data,
String senderHost, int senderPort, String myIp) {
try {
ByteBuffer buf = ByteBuffer.wrap(data);
short txId = buf.getShort();
short flags = buf.getShort();
short qdCount = buf.getShort();
buf.getShort(); // ancount
buf.getShort(); // nscount
buf.getShort(); // arcount
if ((flags & 0x8000) != 0 || qdCount < 1) {
return; // not a standard query
}
int questionOffset = buf.position(); // always 12 for a standard query
String qname = readName(buf, data);
short qtype = buf.getShort();
buf.getShort(); // qclass
int questionEnd = buf.position();
if (qtype == 1 && matchesSuffix(qname)) {
byte[] response = buildAResponse(data, txId, questionOffset, questionEnd, myIp);
socket.send(Buffer.buffer(response), senderPort, senderHost, v -> {});
} else {
forwardAsync(vertx, socket, data, senderHost, senderPort);
}
} catch (Exception e) {
LOG.debugv("DNS packet error: {0}", e.getMessage());
}
}
// ── helpers ───────────────────────────────────────────────────────────────
boolean matchesSuffix(String name) {
if (name == null || name.isEmpty()) {
return false;
}
String lower = name.toLowerCase();
for (String suffix : suffixes) {
String s = suffix.toLowerCase();
if (lower.equals(s) || lower.endsWith("." + s)) {
return true;
}
}
return false;
}
String readName(ByteBuffer buf, byte[] data) {
StringBuilder sb = new StringBuilder();
int safety = 0;
while (buf.hasRemaining() && safety++ < 128) {
int len = buf.get() & 0xFF;
if (len == 0) {
break;
}
if ((len & 0xC0) == 0xC0) {
// compression pointer
int offset = ((len & 0x3F) << 8) | (buf.get() & 0xFF);
ByteBuffer ptr = ByteBuffer.wrap(data);
ptr.position(offset);
if (sb.length() > 0) {
sb.append('.');
}
sb.append(readName(ptr, data));
return sb.toString();
}
if (sb.length() > 0) {
sb.append('.');
}
byte[] label = new byte[len];
buf.get(label);
sb.append(new String(label));
}
return sb.toString();
}
byte[] buildAResponse(byte[] query, short txId, int questionOffset, int questionEnd, String ip) {
int questionLength = questionEnd - questionOffset;
// header(12) + question + answer(name-ptr(2) + type(2) + class(2) + ttl(4) + rdlen(2) + rdata(4))
ByteBuffer resp = ByteBuffer.allocate(12 + questionLength + 16);
// header
resp.putShort(txId);
resp.putShort((short) 0x8180); // QR=1, AA=1, RD=1, RCODE=0
resp.putShort((short) 1); // qdcount
resp.putShort((short) 1); // ancount
resp.putShort((short) 0); // nscount
resp.putShort((short) 0); // arcount
// question (copied verbatim from query)
resp.put(query, questionOffset, questionLength);
// answer
resp.putShort((short) 0xC00C); // name pointer to offset 12 (start of question name)
resp.putShort((short) 1); // type A
resp.putShort((short) 1); // class IN
resp.putInt(TTL);
resp.putShort((short) 4); // rdlength
for (String octet : ip.split("\\.")) {
resp.put((byte) Integer.parseInt(octet));
}
return resp.array();
}
private void forwardAsync(Vertx vertx, DatagramSocket socket, byte[] query,
String senderHost, int senderPort) {
String upstream = upstreamDns;
if (upstream == null) {
return;
}
vertx.executeBlocking(() -> {
try (java.net.DatagramSocket fwd = new java.net.DatagramSocket()) {
fwd.setSoTimeout(2000);
InetAddress addr = InetAddress.getByName(upstream);
fwd.send(new DatagramPacket(query, query.length, addr, DNS_PORT));
byte[] buf = new byte[512];
DatagramPacket resp = new DatagramPacket(buf, buf.length);
fwd.receive(resp);
return Arrays.copyOf(resp.getData(), resp.getLength());
}
}).onSuccess(response ->
socket.send(Buffer.buffer(response), senderPort, senderHost, v -> {})
).onFailure(e ->
LOG.debugv("DNS forwarding to {0} failed: {1}", upstream, e.getMessage())
);
}
private String readUpstreamDns() {
try {
for (String line : Files.readAllLines(Path.of("/etc/resolv.conf"))) {
line = line.trim();
if (line.startsWith("nameserver ")) {
String server = line.substring("nameserver ".length()).trim();
if (!server.equals("127.0.0.1")) {
return server;
}
}
}
} catch (Exception e) {
LOG.debugv("Could not read /etc/resolv.conf: {0}", e.getMessage());
}
return FALLBACK_UPSTREAM;
}
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.core.common.docker;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.LogConfig;
import com.github.dockerjava.api.model.Mount;
@@ -35,11 +36,14 @@ public class ContainerBuilder {
private final EmulatorConfig config;
private final DockerHostResolver dockerHostResolver;
private final EmbeddedDnsServer embeddedDnsServer;
@Inject
public ContainerBuilder(EmulatorConfig config, DockerHostResolver dockerHostResolver) {
public ContainerBuilder(EmulatorConfig config, DockerHostResolver dockerHostResolver,
EmbeddedDnsServer embeddedDnsServer) {
this.config = config;
this.dockerHostResolver = dockerHostResolver;
this.embeddedDnsServer = embeddedDnsServer;
}
/**
@@ -49,7 +53,7 @@ public class ContainerBuilder {
* @return a new Builder instance
*/
public Builder newContainer(String image) {
return new Builder(image, config, dockerHostResolver);
return new Builder(image, config, dockerHostResolver, embeddedDnsServer);
}
/**
@@ -59,6 +63,7 @@ public class ContainerBuilder {
private final String image;
private final EmulatorConfig config;
private final DockerHostResolver dockerHostResolver;
private final EmbeddedDnsServer embeddedDnsServer;
private String name;
private final List<String> env = new ArrayList<>();
@@ -73,11 +78,14 @@ public class ContainerBuilder {
private final List<String> extraHosts = new ArrayList<>();
private LogConfig logConfig;
private boolean privileged;
private final List<String> dnsServers = new ArrayList<>();
Builder(String image, EmulatorConfig config, DockerHostResolver dockerHostResolver) {
Builder(String image, EmulatorConfig config, DockerHostResolver dockerHostResolver,
EmbeddedDnsServer embeddedDnsServer) {
this.image = image;
this.config = config;
this.dockerHostResolver = dockerHostResolver;
this.embeddedDnsServer = embeddedDnsServer;
}
/**
@@ -278,6 +286,16 @@ public class ContainerBuilder {
return this;
}
/**
* Injects Floci's embedded DNS server into the container so virtual-hosted
* S3 hostnames (my-bucket.localhost.floci.io) resolve to Floci's Docker
* network IP. No-op when the embedded DNS server is not running.
*/
public Builder withEmbeddedDns() {
embeddedDnsServer.getServerIp().ifPresent(dnsServers::add);
return this;
}
/**
* Builds the immutable ContainerSpec.
*/
@@ -296,7 +314,8 @@ public class ContainerBuilder {
List.copyOf(binds),
List.copyOf(extraHosts),
logConfig,
privileged
privileged,
List.copyOf(dnsServers)
);
}
}
@@ -329,6 +329,12 @@ public class ContainerLifecycleManager {
hostConfig.withLogConfig(spec.logConfig());
}
// DNS servers — used to inject Floci's embedded DNS so spawned containers
// can resolve *.localhost.floci.io to Floci's Docker network IP.
if (spec.dnsServers() != null && !spec.dnsServers().isEmpty()) {
hostConfig.withDns(spec.dnsServers().toArray(new String[0]));
}
return hostConfig;
}
@@ -25,6 +25,7 @@ import java.util.Map;
* @param extraHosts Extra /etc/hosts entries as "hostname:ip" strings
* @param logConfig Docker log driver configuration (null = daemon default)
* @param privileged Whether to run the container in privileged mode (required for k3s)
* @param dnsServers DNS server IPs to inject into the container (e.g. Floci's embedded DNS)
*/
public record ContainerSpec(
String image,
@@ -40,14 +41,15 @@ public record ContainerSpec(
List<Bind> binds,
List<String> extraHosts,
LogConfig logConfig,
boolean privileged
boolean privileged,
List<String> dnsServers
) {
/**
* Creates a minimal spec with just the image name.
* All other fields will be null or empty lists.
*/
public ContainerSpec(String image) {
this(image, null, List.of(), null, null, null, Map.of(), List.of(), null, List.of(), List.of(), List.of(), null, false);
this(image, null, List.of(), null, null, null, Map.of(), List.of(), null, List.of(), List.of(), List.of(), null, false, List.of());
}
/**
@@ -157,6 +157,8 @@ public class ContainerLauncher {
.withHostDockerInternalOnLinux()
.withLogRotation();
specBuilder.withEmbeddedDns();
// For Image package type without an explicit handler, omit CMD so the image's own CMD is used
if (fn.getHandler() != null && !fn.getHandler().isBlank()) {
specBuilder.withCmd(fn.getHandler());