mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
9d65db843b
commit
b0625347d8
12 files changed
+461
-61
No files matched your search
@@ -2,11 +2,13 @@ package io.floci.aws.core.common.dns;
|
||||
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.datagram.DatagramSocket;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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 org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -22,9 +24,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@EnabledOnOs(OS.LINUX)
|
||||
class EmbeddedDnsReplySourceTest {
|
||||
|
||||
// The host listener (forContainers false) answers Floci's own names with its own address too, and
|
||||
// is never the address handed to the containers Floci launches.
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void replyComesFromTheQueriedAddress(boolean connectedClient) throws Exception {
|
||||
@CsvSource({"false,true", "true,true", "false,false", "true,false"})
|
||||
void replyComesFromTheQueriedAddress(boolean connectedClient, boolean forContainers) throws Exception {
|
||||
Vertx vertx = Vertx.vertx();
|
||||
EmbeddedDnsServer dns = new EmbeddedDnsServer(List.of());
|
||||
InetAddress serverAddress = InetAddress.getByName("127.0.0.2");
|
||||
@@ -32,19 +36,16 @@ class EmbeddedDnsReplySourceTest {
|
||||
// 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)
|
||||
String address = serverAddress.getHostAddress();
|
||||
server = (forContainers ? dns.listen(vertx, address, 0) : dns.bind(vertx, address, 0, false))
|
||||
.toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
||||
int serverPort = server.localAddress().port();
|
||||
assertEquals(Optional.of(serverAddress.getHostAddress()), dns.getServerIp());
|
||||
client.setSoTimeout(5000);
|
||||
assertEquals(forContainers ? Optional.of(address) : Optional.empty(), dns.getServerIp());
|
||||
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);
|
||||
DatagramPacket reply = exchange(client, serverAddress, serverPort, request);
|
||||
|
||||
assertEquals(serverAddress, reply.getAddress());
|
||||
assertEquals(serverPort, reply.getPort());
|
||||
@@ -55,13 +56,49 @@ class EmbeddedDnsReplySourceTest {
|
||||
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);
|
||||
close(server, vertx);
|
||||
}
|
||||
}
|
||||
|
||||
// Catches: the host listener forwarding a name Floci does not hold to the host's resolver, which a
|
||||
// resolver routing that domain to Floci sends straight back, so the lookup loops until it times out.
|
||||
@Test
|
||||
void theHostListenerRefusesNamesFlociDoesNotHold() throws Exception {
|
||||
Vertx vertx = Vertx.vertx();
|
||||
EmbeddedDnsServer dns = new EmbeddedDnsServer(List.of());
|
||||
InetAddress loopback = InetAddress.getByName(EmbeddedDnsServer.LOOPBACK);
|
||||
DatagramSocket server = null;
|
||||
try (java.net.DatagramSocket client = new java.net.DatagramSocket(0, loopback)) {
|
||||
server = dns.bind(vertx, EmbeddedDnsServer.LOOPBACK, 0, false)
|
||||
.toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
||||
byte[] request = DnsTestQuery.query("not-held.example.invalid", 1, 0x4321);
|
||||
|
||||
DatagramPacket reply = exchange(client, loopback, server.localAddress().port(), request);
|
||||
|
||||
ByteBuffer response = ByteBuffer.wrap(reply.getData(), 0, reply.getLength());
|
||||
assertEquals((short) 0x4321, response.getShort(0));
|
||||
assertEquals(5, response.getShort(2) & 0x000F, "REFUSED");
|
||||
} finally {
|
||||
close(server, vertx);
|
||||
}
|
||||
}
|
||||
|
||||
private static DatagramPacket exchange(java.net.DatagramSocket client, InetAddress serverAddress, int serverPort,
|
||||
byte[] request) throws IOException {
|
||||
client.setSoTimeout(5000);
|
||||
client.send(new DatagramPacket(request, request.length, serverAddress, serverPort));
|
||||
DatagramPacket reply = new DatagramPacket(new byte[512], 512);
|
||||
client.receive(reply);
|
||||
return reply;
|
||||
}
|
||||
|
||||
private static void close(DatagramSocket server, Vertx vertx) throws Exception {
|
||||
try {
|
||||
if (server != null) {
|
||||
server.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
} finally {
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,20 +3,33 @@ 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.floci.aws.core.common.docker.DockerBridgeAddress;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.datagram.DatagramSocket;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -40,27 +53,125 @@ class EmbeddedDnsServerAddressTest {
|
||||
assertEquals(InetAddress.getLocalHost().getHostAddress(), EmbeddedDnsServer.ownAddress(currentContainer));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void inAContainerTheServerAnswersAtThatAddress() {
|
||||
ContainerDetector containerDetector = mock(ContainerDetector.class);
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
CurrentContainerNetworkResolver currentContainer = mock(CurrentContainerNetworkResolver.class);
|
||||
when(currentContainer.resolveContainerIp()).thenReturn(Optional.of("172.19.0.2"));
|
||||
private final Vertx vertx = mock(Vertx.class);
|
||||
private final DatagramSocket socket = mock(DatagramSocket.class);
|
||||
private final DockerBridgeAddress dockerBridge = mock(DockerBridgeAddress.class);
|
||||
|
||||
private static EmulatorConfig config(OptionalInt hostPort, boolean bridgeListenerEnabled) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.hostname()).thenReturn(Optional.empty());
|
||||
when(config.dns().extraSuffixes()).thenReturn(Optional.empty());
|
||||
when(config.dns().containerFallbackServers()).thenReturn(List.of());
|
||||
when(config.dns().spoofAwsEndpoints()).thenReturn(false);
|
||||
Vertx vertx = mock(Vertx.class);
|
||||
DatagramSocket socket = mock(DatagramSocket.class);
|
||||
when(config.dns().hostPort()).thenReturn(hostPort);
|
||||
when(config.dns().bridgeListenerEnabled()).thenReturn(bridgeListenerEnabled);
|
||||
return config;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private EmbeddedDnsServer server(EmulatorConfig config, boolean inContainer) {
|
||||
ContainerDetector containerDetector = mock(ContainerDetector.class);
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(inContainer);
|
||||
CurrentContainerNetworkResolver currentContainer = mock(CurrentContainerNetworkResolver.class);
|
||||
when(currentContainer.resolveContainerIp()).thenReturn(Optional.of("172.19.0.2"));
|
||||
when(vertx.createDatagramSocket(any())).thenReturn(socket);
|
||||
return new EmbeddedDnsServer(config, containerDetector, vertx, mock(Instance.class),
|
||||
mock(Instance.class), mock(Instance.class), currentContainer, dockerBridge);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inAContainerTheServerAnswersAtThatAddress() {
|
||||
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);
|
||||
// The host port belongs to a native Floci; in Docker the container's port 53 is published instead.
|
||||
EmbeddedDnsServer server = server(config(OptionalInt.of(5354), true), true);
|
||||
|
||||
assertEquals(Optional.of("172.19.0.2"), server.getServerIp());
|
||||
verify(socket).listen(53, "172.19.0.2");
|
||||
verify(socket, never()).listen(5354, "127.0.0.1");
|
||||
verify(dockerBridge, never()).resolve();
|
||||
}
|
||||
|
||||
// Catches: a native Floci leaving the containers it launches without its resolver, so Cloud Map
|
||||
// and Route 53 private names fail in them.
|
||||
@Test
|
||||
void nativelyTheContainersResolveThroughTheDockerBridge() {
|
||||
when(dockerBridge.resolve()).thenReturn(Optional.of("172.17.0.1"));
|
||||
when(socket.listen(53, "172.17.0.1")).thenReturn(Future.succeededFuture(socket));
|
||||
|
||||
EmbeddedDnsServer server = server(config(OptionalInt.empty(), true), false);
|
||||
|
||||
assertEquals(Optional.of("172.17.0.1"), server.getServerIp());
|
||||
}
|
||||
|
||||
// Catches: the host listener handed to containers, which cannot reach the host's loopback.
|
||||
@Test
|
||||
void nativelyTheHostListenerAnswersOnLoopbackAndIsNotGivenToContainers() {
|
||||
when(socket.listen(5354, "127.0.0.1")).thenReturn(Future.succeededFuture(socket));
|
||||
|
||||
EmbeddedDnsServer server = server(config(OptionalInt.of(5354), false), false);
|
||||
|
||||
verify(socket).listen(5354, "127.0.0.1");
|
||||
assertEquals(Optional.empty(), server.getServerIp());
|
||||
}
|
||||
|
||||
// Catches: containers pointed at a bridge listener that never started, so every name they look up fails.
|
||||
@Test
|
||||
void nativelyABridgeTheProcessMayNotBindLeavesContainersResolvingThroughDocker() {
|
||||
when(dockerBridge.resolve()).thenReturn(Optional.of("172.17.0.1"));
|
||||
when(socket.listen(53, "172.17.0.1"))
|
||||
.thenReturn(Future.failedFuture(new SocketException("Permission denied")));
|
||||
|
||||
EmbeddedDnsServer server = server(config(OptionalInt.empty(), true), false);
|
||||
|
||||
assertEquals(Optional.empty(), server.getServerIp());
|
||||
}
|
||||
|
||||
// Catches: startup going on while the containers' listener is still binding, so a container launched in
|
||||
// that window is created without Floci's resolver and is never given it.
|
||||
@Test
|
||||
void startupWaitsForTheContainersListenerToBind() throws Exception {
|
||||
when(dockerBridge.resolve()).thenReturn(Optional.of("172.17.0.1"));
|
||||
Promise<DatagramSocket> bind = Promise.promise();
|
||||
CountDownLatch bindRequested = new CountDownLatch(1);
|
||||
when(socket.listen(53, "172.17.0.1")).thenAnswer(invocation -> {
|
||||
bindRequested.countDown();
|
||||
return bind.future();
|
||||
});
|
||||
|
||||
CompletableFuture<EmbeddedDnsServer> started =
|
||||
CompletableFuture.supplyAsync(() -> server(config(OptionalInt.empty(), true), false));
|
||||
assertTrue(bindRequested.await(5, TimeUnit.SECONDS));
|
||||
assertThrows(TimeoutException.class, () -> started.get(200, TimeUnit.MILLISECONDS),
|
||||
"startup must still be waiting on the bind");
|
||||
bind.complete(socket);
|
||||
|
||||
assertEquals(Optional.of("172.17.0.1"), started.get(5, TimeUnit.SECONDS).getServerIp());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nativelyWithNoBridgeAndNoHostPortNothingListens() {
|
||||
when(dockerBridge.resolve()).thenReturn(Optional.empty());
|
||||
|
||||
EmbeddedDnsServer server = server(config(OptionalInt.empty(), true), false);
|
||||
|
||||
verify(vertx, never()).createDatagramSocket(any());
|
||||
assertEquals(Optional.empty(), server.getServerIp());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theBridgeListenerCanBeTurnedOff() {
|
||||
when(dockerBridge.resolve()).thenReturn(Optional.of("172.17.0.1"));
|
||||
|
||||
server(config(OptionalInt.empty(), false), false);
|
||||
|
||||
verify(socket, never()).listen(anyInt(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aHostPortOutsideThePortRangeStartsNoListener() {
|
||||
server(config(OptionalInt.of(70000), false), false);
|
||||
|
||||
verify(socket, never()).listen(anyInt(), anyString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package io.floci.aws.core.common.docker;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/** The address a container reaches a natively running Floci at, read from the {@code docker0} interface. */
|
||||
class DockerBridgeAddressTest {
|
||||
|
||||
// Catches: the bridge's IPv6 link-local address handed to containers as their resolver, which
|
||||
// Docker's --dns cannot use.
|
||||
@Test
|
||||
void theBridgesIpv4AddressIsTheOne() throws UnknownHostException {
|
||||
Stream<InetAddress> addresses = Stream.of(InetAddress.getByName("fe80::1"), InetAddress.getByName("172.17.0.1"));
|
||||
|
||||
assertEquals(Optional.of("172.17.0.1"), DockerBridgeAddress.firstIpv4(addresses));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBridgeWithoutAnIpv4AddressHasNoAddress() throws UnknownHostException {
|
||||
assertEquals(Optional.empty(), DockerBridgeAddress.firstIpv4(Stream.of(InetAddress.getByName("fe80::1"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBridgeWithNoAddressHasNoAddress() {
|
||||
assertEquals(Optional.empty(), DockerBridgeAddress.firstIpv4(Stream.empty()));
|
||||
}
|
||||
}
|
||||
+5
@@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test;
|
||||
import java.time.Clock;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -342,6 +343,10 @@ class EventBridgeSchedulerIntegrationTest {
|
||||
public List<String> containerFallbackServers() { return List.of("8.8.8.8", "8.8.4.4"); }
|
||||
@Override
|
||||
public boolean spoofAwsEndpoints() { return false; }
|
||||
@Override
|
||||
public OptionalInt hostPort() { return OptionalInt.empty(); }
|
||||
@Override
|
||||
public boolean bridgeListenerEnabled() { return true; }
|
||||
};
|
||||
}
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user