feat(dns): answer Cloud Map and Route 53 names when Floci runs natively (#8522)

The embedded DNS server only started when Floci ran in Docker, so a
natively running Floci left <service>.<namespace>, Route 53 private
zone records and its own names unresolvable, both for the containers it
launches and for the host.

Natively it now starts up to two listeners. On a host with a docker0
bridge it listens on that bridge's address, port 53, the address
host-gateway points at, and hands it to the containers it launches as
their resolver, as it does from Docker. Port 53 needs root or
CAP_NET_BIND_SERVICE; without it Floci logs how to grant it and the
containers keep resolving through Docker. floci.dns.bridge-listener-enabled
turns it off. With floci.dns.host-port set it also listens on 127.0.0.1
at that port for the host's own resolver; that listener is never handed
to containers, and it refuses names Floci does not hold rather than
forwarding them, so a host resolver that routes a domain to Floci cannot
get a lookup back. Each listener answers Floci's own names with its own
address.

Startup now waits, up to five seconds, for the listener containers are
given to bind or fail, in Docker as well as natively, so a container
launched right after startup is not created without Floci's resolver.

Closes #2556
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 23:39:48 -04:00
1 parent 9d65db843b
commit b0625347d8
12 files changed
+461 -61

No files matched your search

@@ -244,6 +244,26 @@ public interface EmulatorConfig {
*/
@WithDefault("false")
boolean spoofAwsEndpoints();
/**
* When Floci runs natively, the UDP port of a resolver on {@code 127.0.0.1} that answers the names the
* embedded DNS server answers for containers, Cloud Map {@code <service>.<namespace>} and Route 53 private
* zones among them, so the host's own resolver can be pointed at Floci for them. It refuses every other
* name rather than forwarding it. Unset by default, which starts no listener. When Floci runs in Docker,
* publish the container's port 53/udp instead.
* Env: {@code FLOCI_DNS_HOST_PORT}
*/
OptionalInt hostPort();
/**
* When Floci runs natively on a host with a {@code docker0} bridge, the embedded DNS server also listens on
* that bridge's address, port 53, and the containers Floci launches resolve through it, as they do when
* Floci runs in Docker. Port 53 needs root or {@code CAP_NET_BIND_SERVICE}; without either, Floci logs how
* to grant it and the containers keep resolving through Docker.
* Env: {@code FLOCI_DNS_BRIDGE_LISTENER_ENABLED}
*/
@WithDefault("true")
boolean bridgeListenerEnabled();
}
interface SecurityConfig {
@@ -7,6 +7,7 @@ import io.floci.aws.core.common.DomainNames;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.docker.ContainerDetector;
import io.floci.aws.core.common.docker.CurrentContainerNetworkResolver;
import io.floci.aws.core.common.docker.DockerBridgeAddress;
import io.quarkus.runtime.Startup;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
@@ -34,15 +35,19 @@ import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.SequencedSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 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.
* Embedded UDP/53 DNS server that 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
@@ -64,7 +69,13 @@ import java.util.regex.Pattern;
* (floci.dns.container-fallback-servers) so public hostnames still resolve when the
* resolv.conf resolver does not answer.
*
* Only starts when Floci detects it is running inside Docker. No-op on the host.
* Inside Docker it listens on Floci's own container address. When Floci runs natively it listens on
* the {@code docker0} bridge address, port 53, for the containers it launches (when the process may
* bind that port), and on {@code 127.0.0.1} at {@code floci.dns.host-port} for the host's own
* resolver. Each listener answers Floci's own names with its own address; only the one for
* containers is handed to them. The host listener answers only the names Floci holds and refuses
* the rest instead of forwarding them: the host's resolver handles those itself, and a forwarded
* name it routes to Floci would come straight back.
*/
@ApplicationScoped
@Startup
@@ -72,6 +83,9 @@ public class EmbeddedDnsServer {
private static final Logger LOG = Logger.getLogger(EmbeddedDnsServer.class);
static final int DNS_PORT = 53;
static final String LOOPBACK = "127.0.0.1";
/** How long startup waits for the listener containers are given, before any of them can be launched. */
static final long CONTAINER_LISTENER_WAIT_SECONDS = 5;
private static final String FALLBACK_UPSTREAM = "127.0.0.11";
// EDNS0-capable resolvers (Node/c-ares, glibc) advertise UDP payloads well above the
// legacy 512-byte limit; CDN-backed public names return larger responses. Receiving into
@@ -157,50 +171,121 @@ public class EmbeddedDnsServer {
Instance<DnsRecordSource> recordSources,
Instance<DnsForwardingRuleSource> forwardingRuleSources,
Instance<DnsClientVpcSource> clientVpcSources,
CurrentContainerNetworkResolver currentContainer) {
CurrentContainerNetworkResolver currentContainer,
DockerBridgeAddress dockerBridge) {
this.recordSources = recordSources;
this.forwardingRuleSources = forwardingRuleSources;
this.clientVpcSources = clientVpcSources;
if (!containerDetector.isRunningInContainer()) {
return;
}
try {
String myIp = ownAddress(currentContainer);
upstreamDnsServers = composeUpstreams(readResolvConfNameservers(),
config.dns().containerFallbackServers());
suffixes.addAll(BUILTIN_SUFFIXES);
config.hostname().ifPresent(suffixes::add);
LocalStackParity.withDnsSuffixes(config.dns().extraSuffixes()).ifPresent(suffixes::addAll);
if (config.dns().spoofAwsEndpoints()) {
suffixes.addAll(TlsConfigSource.awsEndpointSuffixes());
if (shouldWarnSpoofInvisibleToTls(true, TlsConfigSource.tlsEnabledVisibleToTls(),
TlsConfigSource.spoofAwsEndpointsVisibleToTls())) {
LOG.warnv("floci.dns.spoof-aws-endpoints is set in application config but the TLS certificate only reads it from FLOCI_DNS_SPOOF_AWS_ENDPOINTS or -Dfloci.dns.spoof-aws-endpoints; https:// calls to {0} will fail the handshake until it is set there.", TlsConfigSource.awsEndpointSuffixes());
if (containerDetector.isRunningInContainer()) {
prepare(config);
if (config.dns().hostPort().isPresent()) {
LOG.info("floci.dns.host-port only applies when Floci runs natively; "
+ "publish port 53/udp of the Floci container instead");
}
listenForContainers(vertx, ownAddress(currentContainer), false);
return;
}
listen(vertx, myIp, DNS_PORT)
.onSuccess(socket -> LOG.infov(
"Embedded DNS server started on {0}:53, resolving {1} → {0}", myIp, suffixes))
.onFailure(error -> LOG.warnv(
"Embedded DNS server failed to bind on {0}:53: {1}", myIp, error.getMessage()));
OptionalInt hostPort = config.dns().hostPort();
Optional<String> bridge = config.dns().bridgeListenerEnabled() ? dockerBridge.resolve() : Optional.empty();
if (hostPort.isEmpty() && bridge.isEmpty()) {
return;
}
prepare(config);
if (hostPort.isPresent()) {
listenForHost(vertx, hostPort.getAsInt());
}
bridge.ifPresent(address -> listenForContainers(vertx, address, true));
} catch (Exception e) {
LOG.warnv("Failed to initialize embedded DNS server: {0}", e.getMessage());
}
}
private void prepare(EmulatorConfig config) {
upstreamDnsServers = composeUpstreams(readResolvConfNameservers(), config.dns().containerFallbackServers());
suffixes.addAll(BUILTIN_SUFFIXES);
config.hostname().ifPresent(suffixes::add);
LocalStackParity.withDnsSuffixes(config.dns().extraSuffixes()).ifPresent(suffixes::addAll);
if (config.dns().spoofAwsEndpoints()) {
suffixes.addAll(TlsConfigSource.awsEndpointSuffixes());
if (shouldWarnSpoofInvisibleToTls(true, TlsConfigSource.tlsEnabledVisibleToTls(),
TlsConfigSource.spoofAwsEndpointsVisibleToTls())) {
LOG.warnv("floci.dns.spoof-aws-endpoints is set in application config but the TLS certificate only reads it from FLOCI_DNS_SPOOF_AWS_ENDPOINTS or -Dfloci.dns.spoof-aws-endpoints; https:// calls to {0} will fail the handshake until it is set there.", TlsConfigSource.awsEndpointSuffixes());
}
}
}
private void listenForContainers(Vertx vertx, String address, boolean onDockerBridge) {
Future<DatagramSocket> bound = listen(vertx, address, DNS_PORT)
.onSuccess(socket -> LOG.infov(
"Embedded DNS server started on {0}:53, resolving {1} → {0}", address, suffixes))
.onFailure(error -> {
if (onDockerBridge) {
LOG.infov("Embedded DNS server could not listen on the Docker bridge at {0}:53 ({1}), so "
+ "the containers Floci launches keep resolving through Docker. Port 53 needs root "
+ "or CAP_NET_BIND_SERVICE, or net.ipv4.ip_unprivileged_port_start=53; "
+ "floci.dns.bridge-listener-enabled=false turns this listener off.",
address, error.getMessage());
} else {
LOG.warnv("Embedded DNS server failed to bind on {0}:53: {1}", address, error.getMessage());
}
});
awaitBind(bound, address);
}
/**
* Holds startup until the bind succeeds or fails: until then {@link #getServerIp()} is empty, and a container
* launched in that window would be created without Floci's resolver and never be given it.
*/
private static void awaitBind(Future<DatagramSocket> bound, String address) {
try {
bound.toCompletionStage().toCompletableFuture().get(CONTAINER_LISTENER_WAIT_SECONDS, TimeUnit.SECONDS);
} catch (ExecutionException ignored) {
// The failure handler has already logged why the bind failed; containers keep resolving without it.
} catch (TimeoutException e) {
LOG.warnv("Embedded DNS server on {0}:53 did not finish binding within {1} seconds; containers "
+ "launched before it does are not given Floci's resolver",
address, String.valueOf(CONTAINER_LISTENER_WAIT_SECONDS));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private void listenForHost(Vertx vertx, int port) {
if (port < 1 || port > 65535) {
LOG.warnv("Ignoring floci.dns.host-port {0}: not a port between 1 and 65535", String.valueOf(port));
return;
}
bind(vertx, LOOPBACK, port, false)
.onSuccess(socket -> LOG.infov("Embedded DNS server listening for the host on {0}:{1}",
LOOPBACK, String.valueOf(port)))
.onFailure(error -> LOG.warnv("Embedded DNS server failed to bind on {0}:{1}: {2}",
LOOPBACK, String.valueOf(port), error.getMessage()));
}
public Optional<String> getServerIp() {
return Optional.ofNullable(serverIp);
}
/** Starts the listener the containers Floci launches are given as their resolver. */
Future<DatagramSocket> listen(Vertx vertx, String address, int port) {
return bind(vertx, address, port, true).map(bound -> {
serverIp = address;
return bound;
});
}
/**
* Starts a listener that answers Floci's own names with {@code address}, without handing it to containers. One
* that does not forward refuses every name Floci does not hold.
*/
Future<DatagramSocket> bind(Vertx vertx, String address, int port, boolean forwardsUnknownNames) {
DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions().setIpV6(false));
// A wildcard bind can reply from a different interface, which DNS clients reject.
return socket.listen(port, address).map(bound -> {
bound.handler(packet -> handleQuery(vertx, bound, packet.data().getBytes(),
packet.sender().host(), packet.sender().port(), address));
serverIp = address;
packet.sender().host(), packet.sender().port(), address, forwardsUnknownNames));
return bound;
});
}
@@ -209,6 +294,11 @@ public class EmbeddedDnsServer {
void handleQuery(Vertx vertx, DatagramSocket socket, byte[] data,
String senderHost, int senderPort, String myIp) {
handleQuery(vertx, socket, data, senderHost, senderPort, myIp, true);
}
void handleQuery(Vertx vertx, DatagramSocket socket, byte[] data,
String senderHost, int senderPort, String myIp, boolean forwardsUnknownNames) {
try {
ByteBuffer buf = ByteBuffer.wrap(data);
short txId = buf.getShort();
@@ -231,11 +321,15 @@ public class EmbeddedDnsServer {
vertx.<QueryPlan>executeBlocking(() -> planQuery(qname, senderHost, myIp, qtype), false)
.onSuccess(plan -> {
if (plan.answer().isEmpty()) {
if (plan.ruleTargets().isEmpty()) {
forwardAsync(vertx, socket, data, senderHost, senderPort);
} else {
if (!plan.ruleTargets().isEmpty()) {
forwardToRuleTargets(vertx, socket, data, senderHost, senderPort,
plan.ruleTargets(), qname, txId, questionOffset, questionEnd);
} else if (forwardsUnknownNames) {
forwardAsync(vertx, socket, data, senderHost, senderPort);
} else {
byte[] refused = buildPublicResponse(data, txId, questionOffset, questionEnd,
DnsAnswer.refused());
socket.send(Buffer.buffer(refused), senderPort, senderHost, v -> {});
}
return;
}
@@ -0,0 +1,41 @@
package io.floci.aws.core.common.docker;
import jakarta.enterprise.context.ApplicationScoped;
import org.jboss.logging.Logger;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Optional;
import java.util.stream.Stream;
/**
* The host's IPv4 address on Docker's default bridge, {@code docker0}: where {@code host-gateway} points unless the
* daemon sets {@code host-gateway-ip}, so it is the address a container reaches a natively running Floci at. Empty
* where the host has no such interface, as with Docker Desktop or rootless Docker, whose bridge lives inside a VM or
* a namespace.
*/
@ApplicationScoped
public class DockerBridgeAddress {
private static final Logger LOG = Logger.getLogger(DockerBridgeAddress.class);
static final String INTERFACE_NAME = "docker0";
public Optional<String> resolve() {
try {
NetworkInterface bridge = NetworkInterface.getByName(INTERFACE_NAME);
return bridge == null ? Optional.empty() : firstIpv4(bridge.inetAddresses());
} catch (SocketException e) {
LOG.debugv("Could not read the {0} interface: {1}", INTERFACE_NAME, e.getMessage());
return Optional.empty();
}
}
static Optional<String> firstIpv4(Stream<InetAddress> addresses) {
return addresses
.filter(Inet4Address.class::isInstance)
.map(InetAddress::getHostAddress)
.findFirst();
}
}