mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
88f40a839b
commit
e023936a81
13 files changed
+533
-8
No files matched your search
@@ -0,0 +1,164 @@
|
||||
package io.github.hectorvent.floci.core.common.dns;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EmbeddedDnsServerTest {
|
||||
|
||||
private EmbeddedDnsServer dns;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
dns = new EmbeddedDnsServer(List.of("localhost.floci.io"));
|
||||
}
|
||||
|
||||
// ── matchesSuffix ─────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void matchesSuffix_exactMatch() {
|
||||
assertTrue(dns.matchesSuffix("localhost.floci.io"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesSuffix_singleSubdomain() {
|
||||
assertTrue(dns.matchesSuffix("my-bucket.localhost.floci.io"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesSuffix_deeplyNested() {
|
||||
assertTrue(dns.matchesSuffix("deeply.nested.bucket.localhost.floci.io"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesSuffix_caseInsensitive() {
|
||||
assertTrue(dns.matchesSuffix("My-Bucket.Localhost.Floci.IO"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesSuffix_noMatch() {
|
||||
assertFalse(dns.matchesSuffix("my-bucket.s3.amazonaws.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesSuffix_partialSuffixNoMatch() {
|
||||
assertFalse(dns.matchesSuffix("floci.io"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesSuffix_nullAndEmpty() {
|
||||
assertFalse(dns.matchesSuffix(null));
|
||||
assertFalse(dns.matchesSuffix(""));
|
||||
}
|
||||
|
||||
// ── readName ──────────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void readName_simple() {
|
||||
// my-bucket.localhost.floci.io encoded as DNS labels
|
||||
byte[] encoded = encodeName("my-bucket.localhost.floci.io");
|
||||
ByteBuffer buf = ByteBuffer.wrap(encoded);
|
||||
assertEquals("my-bucket.localhost.floci.io", dns.readName(buf, encoded));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readName_singleLabel() {
|
||||
byte[] encoded = encodeName("floci");
|
||||
ByteBuffer buf = ByteBuffer.wrap(encoded);
|
||||
assertEquals("floci", dns.readName(buf, encoded));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readName_withCompressionPointer() {
|
||||
// Build a buffer where the name at offset 12 is "floci.io" and
|
||||
// a pointer at offset 0 points to it.
|
||||
byte[] data = new byte[20];
|
||||
// pointer at offset 0 → offset 4
|
||||
data[0] = (byte) 0xC0;
|
||||
data[1] = 0x04;
|
||||
// "floci.io" at offset 4
|
||||
byte[] name = encodeName("floci.io");
|
||||
System.arraycopy(name, 0, data, 4, name.length);
|
||||
|
||||
ByteBuffer buf = ByteBuffer.wrap(data);
|
||||
assertEquals("floci.io", dns.readName(buf, data));
|
||||
}
|
||||
|
||||
// ── buildAResponse ────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void buildAResponse_hasCorrectTransactionId() {
|
||||
byte[] query = buildQuery("my-bucket.localhost.floci.io", (short) 0x1234);
|
||||
byte[] response = dns.buildAResponse(query, (short) 0x1234, 12, query.length, "172.19.0.2");
|
||||
short txId = ByteBuffer.wrap(response).getShort(0);
|
||||
assertEquals((short) 0x1234, txId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildAResponse_flagsIndicateResponse() {
|
||||
byte[] query = buildQuery("bucket.localhost.floci.io", (short) 1);
|
||||
byte[] response = dns.buildAResponse(query, (short) 1, 12, query.length, "10.0.0.1");
|
||||
short flags = ByteBuffer.wrap(response).getShort(2);
|
||||
assertTrue((flags & 0x8000) != 0, "QR bit must be set");
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildAResponse_answerCountIsOne() {
|
||||
byte[] query = buildQuery("bucket.localhost.floci.io", (short) 2);
|
||||
byte[] response = dns.buildAResponse(query, (short) 2, 12, query.length, "10.0.0.1");
|
||||
short ancount = ByteBuffer.wrap(response).getShort(6);
|
||||
assertEquals(1, ancount);
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildAResponse_ipAddressIsCorrect() {
|
||||
byte[] query = buildQuery("bucket.localhost.floci.io", (short) 3);
|
||||
byte[] response = dns.buildAResponse(query, (short) 3, 12, query.length, "172.19.0.42");
|
||||
// IP starts at offset: 12 (header) + questionLength + 2+2+2+4+2 = questionLength + 24
|
||||
int questionLength = query.length - 12;
|
||||
ByteBuffer resp = ByteBuffer.wrap(response);
|
||||
resp.position(12 + questionLength + 10); // skip header + question + name-ptr(2) + type(2) + class(2) + ttl(4)
|
||||
short rdlen = resp.getShort();
|
||||
assertEquals(4, rdlen);
|
||||
assertEquals((byte) 172, resp.get());
|
||||
assertEquals((byte) 19, resp.get());
|
||||
assertEquals((byte) 0, resp.get());
|
||||
assertEquals((byte) 42, resp.get());
|
||||
}
|
||||
|
||||
// ── helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private byte[] encodeName(String name) {
|
||||
String[] labels = name.split("\\.");
|
||||
int len = 1; // trailing zero
|
||||
for (String l : labels) len += 1 + l.length();
|
||||
byte[] buf = new byte[len];
|
||||
int pos = 0;
|
||||
for (String label : labels) {
|
||||
buf[pos++] = (byte) label.length();
|
||||
for (char c : label.toCharArray()) buf[pos++] = (byte) c;
|
||||
}
|
||||
buf[pos] = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
private byte[] buildQuery(String name, short txId) {
|
||||
byte[] encodedName = encodeName(name);
|
||||
// header(12) + name + type(2) + class(2)
|
||||
ByteBuffer buf = ByteBuffer.allocate(12 + encodedName.length + 4);
|
||||
buf.putShort(txId);
|
||||
buf.putShort((short) 0x0100); // standard query, RD=1
|
||||
buf.putShort((short) 1); // qdcount
|
||||
buf.putShort((short) 0);
|
||||
buf.putShort((short) 0);
|
||||
buf.putShort((short) 0);
|
||||
buf.put(encodedName);
|
||||
buf.putShort((short) 1); // type A
|
||||
buf.putShort((short) 1); // class IN
|
||||
return buf.array();
|
||||
}
|
||||
}
|
||||
+2
@@ -294,6 +294,8 @@ class EventBridgeSchedulerIntegrationTest {
|
||||
@Override
|
||||
public StorageConfig storage() { return null; }
|
||||
@Override
|
||||
public DnsConfig dns() { return Optional::empty; }
|
||||
@Override
|
||||
public AuthConfig auth() { return null; }
|
||||
@Override
|
||||
public ServicesConfig services() { return null; }
|
||||
|
||||
+5
-1
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.lambda.launcher;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
@@ -41,6 +42,7 @@ class ContainerLauncherTest {
|
||||
@Mock DockerHostResolver dockerHostResolver;
|
||||
@Mock EmulatorConfig config;
|
||||
@Mock EcrRegistryManager ecrRegistryManager;
|
||||
@Mock EmbeddedDnsServer embeddedDnsServer;
|
||||
@Mock RuntimeApiServer runtimeApiServer;
|
||||
@Mock DockerClient dockerClient;
|
||||
|
||||
@@ -64,7 +66,9 @@ class ContainerLauncherTest {
|
||||
when(docker.logMaxSize()).thenReturn("10m");
|
||||
when(docker.logMaxFile()).thenReturn("3");
|
||||
|
||||
ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver);
|
||||
when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty());
|
||||
|
||||
ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer);
|
||||
launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver,
|
||||
runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager);
|
||||
|
||||
|
||||
Reference in new issue
Block a user