fix(dns): preserve the queried address in UDP replies (#7988)

Bind the embedded DNS listener to the advertised address so connected resolvers accept replies on multi-interface hosts. Preserve the current network-selection behavior.

Ensure the test-owned Vert.x instance is closed even if closing its DNS socket fails. Preserve all UDP source assertions.

Extract a package-private query helper accepting name, type and transaction ID and use it in the reply-source regression. Preserve production DNS behavior and all existing assertions.

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-11 10:37:56 -04:00
1 parent 11dafb441c
commit e5a35d6e62
5 files changed
+112 -20

No files matched your search

+3 -1
View File
@@ -969,7 +969,9 @@ invocation to time out.
**Floci solves this automatically** by running an embedded DNS server (UDP/53)
on its container IP. All Lambda containers launched by Floci are configured to
use it as their DNS resolver. The embedded DNS server:
use it as their DNS resolver. It binds that advertised IP so replies retain the
queried source address even when the container has multiple network interfaces.
The embedded DNS server:
- Resolves `*.localhost.floci.io` → Floci's Docker network IP
- Forwards all other queries to the upstream resolver(s) from `/etc/resolv.conf`,
@@ -180,18 +180,11 @@ public class EmbeddedDnsServer {
}
}
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());
}
});
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()));
} catch (Exception e) {
LOG.warnv("Failed to initialize embedded DNS server: {0}", e.getMessage());
}
@@ -201,6 +194,17 @@ public class EmbeddedDnsServer {
return Optional.ofNullable(serverIp);
}
Future<DatagramSocket> listen(Vertx vertx, String address, int port) {
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;
return bound;
});
}
// ── packet handling ───────────────────────────────────────────────────────
void handleQuery(Vertx vertx, DatagramSocket socket, byte[] data,
@@ -0,0 +1,23 @@
package io.floci.aws.core.common.dns;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
final class DnsTestQuery {
private DnsTestQuery() {
}
static byte[] query(String name, int type, int transactionId) {
ByteBuffer query = ByteBuffer.allocate(512);
query.putShort((short) transactionId).putShort((short) 0x0100).putShort((short) 1);
query.putShort((short) 0).putShort((short) 0).putShort((short) 0);
for (String label : name.split("\\.")) {
byte[] encoded = label.getBytes(StandardCharsets.US_ASCII);
query.put((byte) encoded.length).put(encoded);
}
query.put((byte) 0).putShort((short) type).putShort((short) 1);
return Arrays.copyOf(query.array(), query.position());
}
}
@@ -0,0 +1,67 @@
package io.floci.aws.core.common.dns;
import io.vertx.core.Vertx;
import io.vertx.core.datagram.DatagramSocket;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** Linux routes all of 127/8 locally, allowing distinct client and server addresses. */
@EnabledOnOs(OS.LINUX)
class EmbeddedDnsReplySourceTest {
@ParameterizedTest
@ValueSource(booleans = {false, true})
void replyComesFromTheQueriedAddress(boolean connectedClient) throws Exception {
Vertx vertx = Vertx.vertx();
EmbeddedDnsServer dns = new EmbeddedDnsServer(List.of());
InetAddress serverAddress = InetAddress.getByName("127.0.0.2");
DatagramSocket server = null;
// Vert.x owns the server socket; the JDK socket models a resolver that filters its peer.
try (java.net.DatagramSocket client =
new java.net.DatagramSocket(0, InetAddress.getByName("127.0.0.1"))) {
server = dns.listen(vertx, serverAddress.getHostAddress(), 0)
.toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
int serverPort = server.localAddress().port();
assertEquals(Optional.of(serverAddress.getHostAddress()), dns.getServerIp());
client.setSoTimeout(5000);
if (connectedClient) {
client.connect(serverAddress, serverPort);
}
byte[] request = DnsTestQuery.query(EmbeddedDnsServer.DEFAULT_SUFFIX, 1, 0x1234);
client.send(new DatagramPacket(request, request.length, serverAddress, serverPort));
DatagramPacket reply = new DatagramPacket(new byte[512], 512);
client.receive(reply);
assertEquals(serverAddress, reply.getAddress());
assertEquals(serverPort, reply.getPort());
ByteBuffer response = ByteBuffer.wrap(reply.getData(), 0, reply.getLength());
assertEquals((short) 0x1234, response.getShort(0));
assertEquals(0, response.getShort(2) & 0x000F);
assertEquals(1, response.getShort(6));
assertArrayEquals(serverAddress.getAddress(),
Arrays.copyOfRange(reply.getData(), request.length + 12, request.length + 16));
} finally {
try {
if (server != null) {
server.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
}
} finally {
vertx.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
}
}
}
}
@@ -3,9 +3,7 @@ package io.floci.aws.core.common.dns;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.docker.ContainerDetector;
import io.floci.aws.core.common.docker.CurrentContainerNetworkResolver;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.datagram.DatagramSocket;
import jakarta.enterprise.inject.Instance;
@@ -17,9 +15,9 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** The address the embedded DNS server gives the containers Floci launches. */
@@ -57,14 +55,12 @@ class EmbeddedDnsServerAddressTest {
Vertx vertx = mock(Vertx.class);
DatagramSocket socket = mock(DatagramSocket.class);
when(vertx.createDatagramSocket(any())).thenReturn(socket);
when(socket.listen(eq(53), eq("0.0.0.0"), any())).thenAnswer(invocation -> {
invocation.<Handler<AsyncResult<DatagramSocket>>>getArgument(2).handle(Future.succeededFuture(socket));
return socket;
});
when(socket.listen(53, "172.19.0.2")).thenReturn(Future.succeededFuture(socket));
EmbeddedDnsServer server = new EmbeddedDnsServer(config, containerDetector, vertx, mock(Instance.class),
mock(Instance.class), mock(Instance.class), currentContainer);
assertEquals(Optional.of("172.19.0.2"), server.getServerIp());
verify(socket).listen(53, "172.19.0.2");
}
}