fix(route53): answer Floci's own names with the DNS server's address (#7728)

A private hosted zone's CNAME, or alias A record, whose target is one of
Floci's own names (localhost.floci.io, localhost.localstack.cloud, the
configured hostname, the extra DNS suffixes) resolved to
InetAddress.getLocalHost(). That lookup takes the first of the
container's networks by name, so after Floci restarts still on a network
it made, an EMR cluster's or an EC2 VPC's, it could answer with Floci's
address there, one the container asking is not on, as #7636 found for
the embedded DNS server's own address.

The record source now answers with the address the embedded DNS server
answers those names with itself, which #7636 has it take from
CurrentContainerNetworkResolver, so a record pointing at one of Floci's
names resolves as the name does. getLocalHost answers only where that
server has no address.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-08 23:56:13 -06:00
1 parent fd7cc8477e
commit dd777b7f62
3 files changed
+35 -4

No files matched your search

+1 -1
View File
@@ -64,7 +64,7 @@ Route53 emulation supporting hosted zones, reusable delegation sets, resource re
Floci resolves private hosted zone DNS records directly through its embedded DNS server on port 53. Cluster containers (such as EKS k3s containers) and workloads wired to embedded DNS automatically query Floci as their primary resolver.
- **Record Types**: Resolves IPv4 `A` records (including `CNAME` and `ALIAS` target chasing across private zones, EC2 internal names, and external hostnames). When a private CNAME targets a public hostname, Floci resolves the target addresses off the event loop so container stub resolvers succeed. Multiple values are returned as separate answers. Simple private `SRV` record sets also resolve, preserving each value's priority, weight (including zero), port, target, and the record set TTL. The target `.` encodes an unavailable service. Malformed SRV values and SRV sets requiring routing policies or health evaluation return REFUSED; SRV CNAME chasing is not implemented.
- **Record Types**: Resolves IPv4 `A` records (including `CNAME` and `ALIAS` target chasing across private zones, EC2 internal names, and external hostnames). A target that is one of Floci's own names, under `localhost.floci.io`, `localhost.localstack.cloud`, `FLOCI_HOSTNAME` or a `FLOCI_DNS_EXTRA_SUFFIXES` suffix, resolves to Floci's container IP, the address the embedded DNS server answers that name with. When a private CNAME targets a public hostname, Floci resolves the target addresses off the event loop so container stub resolvers succeed. Multiple values are returned as separate answers. Simple private `SRV` record sets also resolve, preserving each value's priority, weight (including zero), port, target, and the record set TTL. The target `.` encodes an unavailable service. Malformed SRV values and SRV sets requiring routing policies or health evaluation return REFUSED; SRV CNAME chasing is not implemented.
- **TTL and Negative Answers**: Answers carry the record set's `TTL`. A chased `CNAME` chain is flattened to `A` records, so it answers with the shortest TTL in the chain; an alias answers with its target's TTL, and an alias or `CNAME` to a name outside the private zones answers with at most 60 seconds, since Floci cannot see that target's TTL. A name absent from the zone receives NXDOMAIN; a name that exists without the requested record type (only `TXT`, for example, or only records beneath it) receives NOERROR with no answers. A private `CNAME` chain preserves that distinction from its target, including after the target is created or deleted. A wildcard record such as `*.example.internal` answers only when neither the name nor an ancestor closer than the wildcard exists, so a record at `api.team.example.internal` keeps the wildcard from answering for `team.example.internal` or anything beneath it.
- **Zone Visibility and Fallthrough**: Private hosted zones (created with a VPC association) provide IPv4 and SRV resolution. Public zones support the authoritative record types described below; unowned external domains fall through to upstream forwarders. Because the standard DNS wire protocol carries no caller VPC identifier, Floci resolves across private zones without filtering by account or VPC ID.
- **Resolver Rule Precedence**: A Route 53 Resolver rule associated with the querying container's VPC takes precedence over a private hosted zone that matches the same name, as it does in AWS: the query is forwarded to the rule's targets instead of answered from the zone. See [Route 53 Resolver](route53resolver.md#resolver-rules-and-dns-resolution).
@@ -47,20 +47,23 @@ public class Route53DnsRecordSource implements DnsRecordSource {
private final Route53Service route53Service;
private final DnsLookupHelper dnsLookupHelper;
private final EmbeddedDnsServer embeddedDnsServer;
private final Set<String> flociSuffixes = new LinkedHashSet<>();
public Route53DnsRecordSource(Route53Service route53Service) {
this(route53Service, null, new DnsLookupHelper());
this(route53Service, null, new DnsLookupHelper(), null);
}
public Route53DnsRecordSource(Route53Service route53Service, EmulatorConfig config) {
this(route53Service, config, new DnsLookupHelper(config));
this(route53Service, config, new DnsLookupHelper(config), null);
}
@Inject
public Route53DnsRecordSource(Route53Service route53Service, EmulatorConfig config, DnsLookupHelper dnsLookupHelper) {
public Route53DnsRecordSource(Route53Service route53Service, EmulatorConfig config, DnsLookupHelper dnsLookupHelper,
EmbeddedDnsServer embeddedDnsServer) {
this.route53Service = route53Service;
this.dnsLookupHelper = dnsLookupHelper != null ? dnsLookupHelper : new DnsLookupHelper(config);
this.embeddedDnsServer = embeddedDnsServer;
this.flociSuffixes.addAll(BUILTIN_SUFFIXES);
if (config != null) {
config.hostname().ifPresent(this.flociSuffixes::add);
@@ -333,7 +336,16 @@ public class Route53DnsRecordSource implements DnsRecordSource {
return false;
}
/**
* The address the embedded DNS server answers Floci's own names with, so a record pointing at one resolves as the
* name itself does. The host's own lookup takes the first of this container's networks by name, which after a crash
* can be one Floci made and is still on; it answers only where that server has no address.
*/
private Optional<String> getLocalFlociAddress() {
Optional<String> serverIp = embeddedDnsServer != null ? embeddedDnsServer.getServerIp() : Optional.empty();
if (serverIp.isPresent()) {
return serverIp;
}
try {
return Optional.of(InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
@@ -3,6 +3,7 @@ package io.floci.aws.services.route53;
import io.floci.aws.core.common.dns.DnsAnswer;
import io.floci.aws.core.common.dns.DnsLookupHelper;
import io.floci.aws.core.common.dns.DnsRecord;
import io.floci.aws.core.common.dns.EmbeddedDnsServer;
import io.floci.aws.services.route53.model.AliasTarget;
import io.floci.aws.services.route53.model.ResourceRecord;
import io.floci.aws.services.route53.model.ResourceRecordSet;
@@ -38,6 +39,9 @@ class Route53DnsResolutionTest {
@InjectMock
DnsLookupHelper dnsLookupHelper;
@InjectMock
EmbeddedDnsServer embeddedDnsServer;
@Inject
Route53DnsRecordSource dnsRecordSource;
@@ -386,6 +390,21 @@ class Route53DnsResolutionTest {
assertEquals(List.of(expectedIp), result.get().addresses());
}
@Test
void aTargetOnAFlociSuffixResolvesToTheAddressTheEmbeddedDnsServerAnswersItsNamesWith() {
// After a crash Floci can still be on a network it made, whose address the host's own lookup may take.
Mockito.when(embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.19.0.2"));
String zone = uniqueZone();
String zoneId = createPrivateZone(zone);
addRecord(zoneId, "floci-cname." + zone, "CNAME", "sqs.localhost.floci.io");
addAlias(zoneId, "floci-alias." + zone, "A", "s3.localhost.localstack.cloud");
assertEquals(List.of("172.19.0.2"),
dnsRecordSource.resolveIpv4("floci-cname." + zone).orElseThrow().addresses());
assertEquals(List.of("172.19.0.2"),
dnsRecordSource.resolveIpv4("floci-alias." + zone).orElseThrow().addresses());
}
@Test
void aliasTargetToBareLocalstackCloudDoesNotResolveLocalAddress() throws Exception {
Mockito.when(dnsLookupHelper.resolveIpv4("s3.localstack.cloud"))