From a8eedb1fd99bdf7ff18c8bb572cc408f9d32f875 Mon Sep 17 00:00:00 2001 From: Simone Primarosa Date: Mon, 28 Sep 2026 15:44:28 +0200 Subject: [PATCH] feat(route53resolver): apply resolver rules to DNS resolution (#4538) * feat(route53resolver): apply resolver rules to DNS resolution Resolver rules were stored and ignored. The embedded DNS server now consults them before resolving a name itself, via two CDI SPIs so core.common.dns imports neither Route 53 Resolver nor EKS: DnsForwardingRuleSource supplies the rules associated with one VPC, DnsClientVpcSource maps a query's source address to the VPC it came from. FORWARD rules forward the raw query to their TargetIps, each on its own port, in a random order with the next target tried when one does not answer. SYSTEM and RECURSIVE rules carry no targets and hand the query back to Floci, so a SYSTEM rule carves a subdomain out of a broader FORWARD. Matching is exact-or-subdomain on label boundaries and the most specific matching rule wins. A rule takes precedence over a private hosted zone matching the same name, as AWS documents. Floci's own suffixes and ec2.internal names resolve locally whatever the rules say, as AWS autodefines system rules for its internal domains. A rule whose targets are all unreachable answers SERVFAIL for that query only. Route53ResolverDnsForwardingRules reads the rule and association stores through Route53ResolverService across account partitions, since the packet path has no caller. EksClusterManager claims its cluster node container addresses for the cluster's resourcesVpcConfig.vpcId. * fix(route53resolver): scope rule DNS lookups to the querying account AssociateResolverRule stores whatever VPCId the caller names without proving ownership of it, so joining associations across account partitions by VPC id let one account redirect another account's cluster DNS by naming its VPC. The DNS client VPC now carries the account alongside the VPC id, and the rule source reads only that account's associations. Rules are fetched by id instead of scanned for, so a query reads only the rules an association named rather than the whole rule store, and registration warns when a running cluster's container addresses cannot be determined instead of leaving its rules silently inert. Also stop setting the authoritative bit on the SERVFAIL answer for a forwarded query: Floci is not the authority for a name a rule sends elsewhere. * style(route53resolver): import Optional instead of qualifying it inline * fix(route53resolver): scope rule DNS lookups to the querying region Route 53 Resolver is regional, so a rule created in one region governs nothing in another, but the DNS path matched on account and VPC alone. A rule created in eu-west-1 therefore steered a query from a VPC in us-east-1. The DNS client origin now carries the region, and a rule is only applied when its own region matches. The stores carry no region in their keys, so a rule's region is read back from the ARN the service minted for it rather than reshaping the stores. A stored rule with no parseable ARN has no region to claim and is treated as belonging to the region asking, which no create path can produce. Associations are now read as rule ids rather than as nodes, so a query no longer deep-copies a record per association when all it needs is which rules to look at. * test(eks): pin the warning when a cluster's addresses cannot be determined Inspecting the node container can fail, and when it does the cluster gets no VPC mapping and its queries quietly ignore every associated resolver rule. Nothing fails, so the warning is the only way a reader finds out, which makes it worth a test rather than leaving it to inspection. Levels are compared by severity rather than identity because JBoss LogManager has its own Level constants, so its WARN is not java.util.logging's WARNING. --- docs/services/route53.md | 1 + docs/services/route53resolver.md | 67 +++++- .../core/common/dns/DnsClientVpcSource.java | 30 +++ .../core/common/dns/DnsForwardingRule.java | 78 ++++++ .../common/dns/DnsForwardingRuleSource.java | 23 ++ .../core/common/dns/EmbeddedDnsServer.java | 222 ++++++++++++++++-- .../floci/services/eks/EksClusterManager.java | 62 ++++- .../Route53ResolverDnsForwardingRules.java | 94 ++++++++ .../Route53ResolverService.java | 105 +++++++-- .../common/dns/EmbeddedDnsServerTest.java | 190 +++++++++++++++ .../common/dns/ResolverRuleDnsPacketTest.java | 164 +++++++++++++ .../services/eks/EksClusterManagerTest.java | 87 +++++++ ...Route53ResolverDnsForwardingRulesTest.java | 197 ++++++++++++++++ 13 files changed, 1285 insertions(+), 35 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/core/common/dns/DnsClientVpcSource.java create mode 100644 src/main/java/io/github/hectorvent/floci/core/common/dns/DnsForwardingRule.java create mode 100644 src/main/java/io/github/hectorvent/floci/core/common/dns/DnsForwardingRuleSource.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverDnsForwardingRules.java create mode 100644 src/test/java/io/github/hectorvent/floci/core/common/dns/ResolverRuleDnsPacketTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverDnsForwardingRulesTest.java diff --git a/docs/services/route53.md b/docs/services/route53.md index 52d1d67659..619bc162af 100644 --- a/docs/services/route53.md +++ b/docs/services/route53.md @@ -61,6 +61,7 @@ Floci resolves private hosted zone DNS records directly through its embedded DNS - **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. - **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 an `A` record (only `TXT`, for example, or only records beneath it) receives NOERROR with no answers. 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**: Only private hosted zones (created with a VPC association) are answered by Floci's embedded Route 53 resolver. Public zones and 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). - **Zero-Restart Propagation**: Record changes via `ChangeResourceRecordSets` take effect immediately without restarts. - **Kubernetes and external-dns Compatibility**: In-cluster controllers like `external-dns` provisioning Route 53 private records for Services or Ingresses are immediately resolvable. diff --git a/docs/services/route53resolver.md b/docs/services/route53resolver.md index ff378691e8..02e7ac275f 100644 --- a/docs/services/route53resolver.md +++ b/docs/services/route53resolver.md @@ -77,6 +77,55 @@ endpoint, the retry is reported as a conflict rather than compared on count alon nothing to compare against, sameness cannot be established, and a loud error is preferable to a success that may not match the request. +## Resolver Rules and DNS Resolution + +Resolver rules steer the DNS resolution Floci performs for containers wired to its embedded DNS +server, which is how cluster containers resolve (see +[Route 53](route53.md#private-hosted-zone-dns-resolution)). What each rule type does: + +- **`FORWARD`** sends a matching query to the rule's `TargetIps` over UDP, each target on its own + `Port` (default 53), in a random order with the next target tried when one does not answer, as in + AWS. Only IPv4 targets are used, and a rule left with no usable target is ignored. +- **`SYSTEM`** carries no targets and hands the query back to Floci's own resolution, which is how + AWS has it carve a subdomain out of a broader forwarding rule: a `FORWARD` rule for + `corp.internal` plus a `SYSTEM` rule for `acme.corp.internal` forwards everything under + `corp.internal` except that subtree. +- **`RECURSIVE`** means the same thing here. In AWS it is the autodefined `Internet Resolver` rule + that resolves anything no other rule covers, which is what Floci resolving the name itself is. +- **`DELEGATE`** is stored and returned by the API but does not affect resolution. + +**Matching** follows AWS: a rule matches a name that equals its `DomainName` or is a subdomain of it, +on label boundaries, and `.` matches everything. Where several rules match, the one with the most +labels in its domain name wins. + +**Association, account and region** all have to line up. A rule does nothing in AWS until it is +associated with a VPC, and Route 53 Resolver is regional, so a rule created in one region governs +nothing in another. Floci reads all three from the query's source address: a cluster container's +address maps to the account that owns the cluster, the cluster's region, and its +`resourcesVpcConfig.vpcId`. Only that account's associations are consulted, and only rules from the +querying region among them. `AssociateResolverRule` stores whatever `VPCId` the caller names without +proving ownership of it, so scoping the lookup to the querying account's own partition is what stops +one account redirecting another account's cluster DNS by naming its VPC. The stores carry no region +in their keys, so a rule's region is read back from the ARN Floci minted for it. An address no +service claims, or a cluster with no resolvable VPC id, belongs to no VPC, so no rule applies and +resolution is unchanged; Floci logs a warning when it cannot determine a running cluster's container +addresses, because rules then silently do not reach it. + +**Precedence over private hosted zones** matches AWS: when a private hosted zone and a resolver rule +both match a name, the rule wins and the query is forwarded instead of answered from the zone's +records. A name whose only match is the zone, or whose matching rule is not associated with the +querying VPC, still resolves from the zone. + +**Names Floci owns are never forwarded.** The emulator's own suffixes (`localhost.floci.io`, +`localhost.localstack.cloud`, `floci.hostname`, `floci.dns.extra-suffixes`) and `ip-*.ec2.internal` +addresses resolve locally whatever the rules say, the way AWS autodefines system rules for its own +internal domains so a rule for `.` cannot break the platform. + +**A rule whose targets are all unreachable fails that query with SERVFAIL** rather than falling back +to the upstream resolvers, which would answer with the wrong address for a name the rule placed +elsewhere. Every other name resolves as usual. This needs no switch of its own: it applies wherever +cluster DNS does, governed by `floci.services.eks.embedded-dns`. + ## Limitations - **All create/update operations complete synchronously.** Real AWS transitions a @@ -90,12 +139,24 @@ to a success that may not match the request. modelled. - **`CreateResolverRule`/`UpdateResolverRule` do not validate `ResolverEndpointId` against an existing endpoint.** Any non-blank string is accepted. -- **No VPC-scoping or region-scoping is enforced for custom resources.** The four +- **No region-scoping is enforced on the API surface.** The four stores go through `StorageFactory`, so every key is prefixed with the calling credential's account id and custom resources *are* isolated per account: one account cannot read, update or delete another's endpoints, rules, associations or domain lists. Neither region nor VPC is part of the key, though, so a custom resource created - in one region is visible from every other, and `VPCId` on an association is stored but - never used to filter. The AWS-managed domain lists are the deliberate exception: they + in one region is visible from every other, and `VPCId` on an association does not filter + any API response. Both do decide which rules steer DNS resolution, as above: that path reads the + region back from the rule's ARN and the account from the store partition. The AWS-managed + domain lists are the deliberate exception: they are derived per region from the name list rather than stored, so they are region-scoped and visible to every caller, as they are in AWS. +- **Inbound endpoints stay metadata.** An inbound endpoint exists so something outside the VPC can + query into it; Floci stores its direction and address count but binds no listener on its + addresses, so nothing resolves through one. +- **A `FORWARD` rule's `ResolverEndpointId` is not used when forwarding.** AWS sends the query from + the outbound endpoint's addresses inside the VPC; Floci forwards it from itself, since there is no + separate VPC network path to send it over. +- **Cross-account rule sharing is not modelled.** AWS lets an account associate a rule shared with + it through RAM; Floci reports every rule as `NOT_SHARED` and models no rule policy, so a rule and + its associations always live in one account and resolution only ever consults that account's own. +- **DNS Firewall rule groups, query logging and DNSSEC validation do not affect resolution.** diff --git a/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsClientVpcSource.java b/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsClientVpcSource.java new file mode 100644 index 0000000000..bf3c8311bc --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsClientVpcSource.java @@ -0,0 +1,30 @@ +package io.github.hectorvent.floci.core.common.dns; + +import java.util.Optional; + +/** + * Tells {@link EmbeddedDnsServer} which VPC a query came from, so rules associated with that VPC + * can be applied and rules that are not can be ignored. + * + *

The DNS wire protocol carries no VPC identifier, but the query's source address identifies the + * container that sent it, and a service that launches containers for an AWS resource knows the VPC + * that resource sits in. Implemented by those services and discovered through CDI. + */ +public interface DnsClientVpcSource { + + /** + * Where a query came from. The account is part of it because a VPC id alone is not an identity: + * any account can associate a resolver rule with any VPC id it cares to name, so a rule may only + * steer queries from a resource its own account owns. The region is part of it because Route 53 + * Resolver is regional, so a rule only governs queries from the region it was created in. + */ + record ClientVpc(String accountId, String region, String vpcId) { + } + + /** + * The account, region and VPC of the resource reachable at {@code clientAddress}, or empty when + * this source launched nothing there. Empty is not an error: a query no one claims has no VPC, so + * no rules apply to it. + */ + Optional vpcForClient(String clientAddress); +} diff --git a/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsForwardingRule.java b/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsForwardingRule.java new file mode 100644 index 0000000000..91cf98514d --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsForwardingRule.java @@ -0,0 +1,78 @@ +package io.github.hectorvent.floci.core.common.dns; + +import java.util.List; +import java.util.Locale; + +/** + * One rule that steers a DNS query away from the resolution {@link EmbeddedDnsServer} would perform + * on its own. A rule with targets sends a matching query to those resolvers, as a Route 53 Resolver + * {@code FORWARD} rule does; a rule with no targets sends it back to Floci's own resolution, as a + * {@code SYSTEM} rule does when it carves a subdomain out of a broader forwarding rule. + * + *

Matching follows Route 53 Resolver: a rule matches a query name that equals its domain name or + * is a subdomain of it, and where several rules match, the one with the most labels in its domain + * name wins. The root domain, which AWS spells {@code "."}, matches every name and is the least + * specific rule there is. + */ +public record DnsForwardingRule(String domainName, List targets) { + + /** A resolver a matching query is forwarded to. Route 53 Resolver defaults the port to 53. */ + public record Target(String address, int port) { + + public static final int DEFAULT_DNS_PORT = 53; + + public Target(String address) { + this(address, DEFAULT_DNS_PORT); + } + } + + public DnsForwardingRule { + domainName = normalize(domainName); + targets = List.copyOf(targets); + } + + /** A rule that forwards matching queries to {@code targets}. */ + public static DnsForwardingRule forwardTo(String domainName, List targets) { + return new DnsForwardingRule(domainName, targets); + } + + /** A rule that hands matching queries back to Floci's own resolution. */ + public static DnsForwardingRule system(String domainName) { + return new DnsForwardingRule(domainName, List.of()); + } + + public boolean forwards() { + return !targets.isEmpty(); + } + + /** Whether {@code queryName} is this rule's domain name or a subdomain of it. */ + public boolean matches(String queryName) { + if (queryName == null || queryName.isBlank()) { + return false; + } + if (domainName.isEmpty()) { + return true; + } + String query = normalize(queryName); + return query.equals(domainName) || query.endsWith("." + domainName); + } + + /** + * How specific this rule's domain name is, as its label count. The root domain scores zero, + * so any named rule outranks a rule for {@code "."}. + */ + public int specificity() { + return domainName.isEmpty() ? 0 : domainName.split("\\.").length; + } + + private static String normalize(String name) { + if (name == null) { + return ""; + } + String normalized = name.trim().toLowerCase(Locale.ROOT); + while (normalized.endsWith(".")) { + normalized = normalized.substring(0, normalized.length() - 1); + } + return normalized; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsForwardingRuleSource.java b/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsForwardingRuleSource.java new file mode 100644 index 0000000000..48a3f4ae39 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsForwardingRuleSource.java @@ -0,0 +1,23 @@ +package io.github.hectorvent.floci.core.common.dns; + +import java.util.List; + +/** + * A source of {@link DnsForwardingRule}s that {@link EmbeddedDnsServer} consults before resolving a + * query itself, discovered through CDI so the DNS server never imports the service that stores them. + * + *

The source is asked for the rules of one VPC, in one account and one region, so association + * and scoping are its decision; picking between several matching rules is the DNS server's, which + * compares them on domain specificity. Returning the rules most specific first makes that choice + * deterministic when two rules tie. + * + *

It runs outside any request context, on a worker thread. + */ +public interface DnsForwardingRuleSource { + + /** + * The rules {@code accountId} has associated with {@code vpcId} in {@code region}, none of which + * is blank. An empty list leaves resolution exactly as it is without this source. + */ + List rulesFor(String accountId, String region, String vpcId); +} diff --git a/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java b/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java index 3876862c5b..6f9a24a2c1 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java @@ -21,10 +21,12 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; import java.util.SequencedSet; +import java.util.concurrent.ThreadLocalRandom; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,6 +41,10 @@ import java.util.regex.Pattern; * Also answers for any name a {@link DnsRecordSource} owns, which is how a service that holds * a private DNS zone (Cloud Map) gets real records rather than only API responses. * + * A {@link DnsForwardingRuleSource} can steer a query away from all of that before it is + * resolved, which is how a Route 53 Resolver forwarding rule associated with the querying + * container's VPC sends matching names to the resolvers the rule names instead. + * * All other queries are forwarded to the upstream resolvers read from /etc/resolv.conf * (Docker's embedded DNS at 127.0.0.11), falling back to the configured public resolvers * (floci.dns.container-fallback-servers) so public hostnames still resolve when the @@ -78,21 +84,35 @@ public class EmbeddedDnsServer { // Held as the Iterable a CDI Instance already is, so iterating resolves the beans lazily on // the packet path rather than at startup, where a source's storage must not be touched yet. private final Iterable recordSources; + private final Iterable forwardingRuleSources; + private final Iterable clientVpcSources; EmbeddedDnsServer(List suffixes) { this(suffixes, List.of()); } EmbeddedDnsServer(List suffixes, Iterable recordSources) { + this(suffixes, recordSources, List.of(), List.of()); + } + + EmbeddedDnsServer(List suffixes, Iterable recordSources, + Iterable forwardingRuleSources, + Iterable clientVpcSources) { this.suffixes.addAll(BUILTIN_SUFFIXES); this.suffixes.addAll(suffixes); this.recordSources = recordSources; + this.forwardingRuleSources = forwardingRuleSources; + this.clientVpcSources = clientVpcSources; } @Inject public EmbeddedDnsServer(EmulatorConfig config, ContainerDetector containerDetector, Vertx vertx, - Instance recordSources) { + Instance recordSources, + Instance forwardingRuleSources, + Instance clientVpcSources) { this.recordSources = recordSources; + this.forwardingRuleSources = forwardingRuleSources; + this.clientVpcSources = clientVpcSources; if (!containerDetector.isRunningInContainer()) { return; } @@ -149,13 +169,18 @@ public class EmbeddedDnsServer { buf.getShort(); // qclass int questionEnd = buf.position(); - vertx.>executeBlocking(() -> resolveRecordWithOwnership(qname, myIp, qtype), false) - .onSuccess(answer -> { - if (answer.isEmpty()) { - forwardAsync(vertx, socket, data, senderHost, senderPort); + vertx.executeBlocking(() -> planQuery(qname, senderHost, myIp, qtype), false) + .onSuccess(plan -> { + if (plan.answer().isEmpty()) { + if (plan.ruleTargets().isEmpty()) { + forwardAsync(vertx, socket, data, senderHost, senderPort); + } else { + forwardToRuleTargets(vertx, socket, data, senderHost, senderPort, + plan.ruleTargets(), qname, txId, questionOffset, questionEnd); + } return; } - DnsAnswer records = answer.orElseThrow(); + DnsAnswer records = plan.answer().orElseThrow(); byte[] response = !records.isEmpty() ? buildAResponse(data, txId, questionOffset, questionEnd, records) : buildEmptyResponse(data, txId, questionOffset, questionEnd, @@ -194,15 +219,122 @@ public class EmbeddedDnsServer { } private Optional resolveRecordWithOwnership(String name, String myIp, int type) { + Optional emulatorName = resolveEmulatorName(name, myIp, type); + return emulatorName.isPresent() ? emulatorName : resolveFromRecordSources(name, type); + } + + /** + * What happens to one query, decided on a worker thread because every step behind it reads + * storage: an answer Floci owns, a set of resolvers a rule forwards the query to, or neither, + * which leaves it to the upstream resolvers. + */ + record QueryPlan(Optional answer, List ruleTargets) { + + static final QueryPlan UPSTREAM = new QueryPlan(Optional.empty(), List.of()); + + static QueryPlan answering(DnsAnswer answer) { + return new QueryPlan(Optional.of(answer), List.of()); + } + + static QueryPlan forwardingTo(List targets) { + return new QueryPlan(Optional.empty(), targets); + } + } + + /** + * Names the emulator itself owns come first, whatever the rules say. Route 53 Resolver does + * the same for the AWS-internal domains it autodefines system rules for, so that a rule for a + * broad domain (or for "." itself) cannot send the platform's own names off to a resolver that + * knows nothing about them. + * + *

A forwarding rule associated with the querying VPC is then applied before Floci resolves + * the name from a record source, because a Resolver rule takes precedence over a private + * hosted zone that matches the same name. + */ + QueryPlan planQuery(String name, String clientAddress, String myIp, int type) { + Optional emulatorName = resolveEmulatorName(name, myIp, type); + if (emulatorName.isPresent()) { + return QueryPlan.answering(emulatorName.orElseThrow()); + } + // A rule steers the name whatever the query asks about it, so this is deliberately not + // gated on the record type: the raw query is relayed to the rule's resolvers as it stands. + Optional rule = matchingRule(name, clientAddress); + if (rule.isPresent() && rule.orElseThrow().forwards()) { + return QueryPlan.forwardingTo(rule.orElseThrow().targets()); + } + return resolveFromRecordSources(name, type).map(QueryPlan::answering).orElse(QueryPlan.UPSTREAM); + } + + private Optional resolveEmulatorName(String name, String myIp, int type) { if (matchesSuffix(name)) { return Optional.of(type == 1 ? DnsAnswer.records(List.of(myIp), DnsAnswer.DEFAULT_TTL_SECONDS) : DnsAnswer.noData()); } - Optional ec2PrivateDnsName = resolveEc2PrivateDnsName(name); - return ec2PrivateDnsName + return resolveEc2PrivateDnsName(name) .map(address -> type == 1 ? DnsAnswer.records(List.of(address), DnsAnswer.DEFAULT_TTL_SECONDS) - : DnsAnswer.noData()) - .map(Optional::of).orElseGet(() -> resolveFromRecordSources(name, type)); + : DnsAnswer.noData()); + } + + /** + * The rule that governs this query: the most specific one whose domain matches the name among + * those associated with the VPC the query came from. A source that throws is skipped rather + * than allowed to fail the query, which is how {@link #resolveFromRecordSources} treats one too. + */ + Optional matchingRule(String name, String clientAddress) { + if (forwardingRuleSources == null || name == null || name.isBlank()) { + return Optional.empty(); + } + Optional clientVpc = vpcForClient(clientAddress); + if (clientVpc.isEmpty()) { + return Optional.empty(); + } + DnsClientVpcSource.ClientVpc origin = clientVpc.orElseThrow(); + DnsForwardingRule best = null; + for (DnsForwardingRuleSource source : forwardingRuleSources) { + try { + for (DnsForwardingRule rule : + source.rulesFor(origin.accountId(), origin.region(), origin.vpcId())) { + if (rule == null || !rule.matches(name)) { + continue; + } + if (best == null || rule.specificity() > best.specificity()) { + best = rule; + } + } + } catch (Exception e) { + LOG.debugv("DNS forwarding rule source {0} failed for {1}: {2}", + source.getClass().getSimpleName(), name, e.getMessage()); + } + } + return Optional.ofNullable(best); + } + + private Optional vpcForClient(String clientAddress) { + if (clientVpcSources == null || clientAddress == null || clientAddress.isBlank()) { + return Optional.empty(); + } + for (DnsClientVpcSource source : clientVpcSources) { + try { + Optional clientVpc = + source.vpcForClient(clientAddress.trim()); + if (clientVpc != null && clientVpc.isPresent() && isUsableOrigin(clientVpc.orElseThrow())) { + return clientVpc; + } + } catch (Exception e) { + LOG.debugv("DNS client VPC source {0} failed for {1}: {2}", + source.getClass().getSimpleName(), clientAddress, e.getMessage()); + } + } + return Optional.empty(); + } + + private static boolean isUsableOrigin(DnsClientVpcSource.ClientVpc clientVpc) { + return isPresent(clientVpc.accountId()) && isPresent(clientVpc.region()) + && isPresent(clientVpc.vpcId()); + } + + private static boolean isPresent(String value) { + return value != null && !value.isBlank(); } /** @@ -229,10 +361,25 @@ public class EmbeddedDnsServer { } byte[] buildEmptyResponse(byte[] query, short txId, int questionOffset, int questionEnd, int responseCode) { + return buildAnswerlessResponse(query, txId, questionOffset, questionEnd, + (short) (0x8580 | responseCode)); + } + + /** + * The failure answer for a query a forwarding rule claimed and no target answered. Not + * authoritative, unlike {@link #buildEmptyResponse}: Floci is not the authority for a name a + * rule sends elsewhere, it only failed to reach the resolver that is. + */ + byte[] buildServerFailureResponse(byte[] query, short txId, int questionOffset, int questionEnd) { + return buildAnswerlessResponse(query, txId, questionOffset, questionEnd, (short) 0x8182); + } + + private static byte[] buildAnswerlessResponse(byte[] query, short txId, int questionOffset, + int questionEnd, short flags) { UdpPayload payload = udpPayload(query, questionEnd); ByteBuffer response = ByteBuffer.allocate(12 + questionEnd - questionOffset + (payload.edns() ? 11 : 0)); response.putShort(txId); - response.putShort((short) (0x8580 | responseCode)); + response.putShort(flags); response.putShort((short) 1); response.putShort((short) 0); response.putShort((short) 0); @@ -404,28 +551,71 @@ public class EmbeddedDnsServer { upstreams, e.getMessage())); } + /** + * Sends the query to the resolvers a forwarding rule names instead of to the upstreams. The + * order is shuffled because Route 53 Resolver picks a rule's target at random and retries + * another one when it does not answer. + * + *

A rule whose targets are all unreachable fails this query with SERVFAIL rather than + * falling back to the upstream resolvers. The rule said where the name lives, so an upstream + * answer for it would be the wrong answer, and resolution of every other name is untouched. + */ + private void forwardToRuleTargets(Vertx vertx, DatagramSocket socket, byte[] query, + String senderHost, int senderPort, + List targets, String qname, + short txId, int questionOffset, int questionEnd) { + vertx.executeBlocking(() -> forwardToTargets(query, shuffled(targets))) + .onSuccess(response -> + socket.send(Buffer.buffer(response), senderPort, senderHost, v -> {})) + .onFailure(e -> { + LOG.warnv("Resolver rule forwarding for {0} failed on every target {1}: {2}", + qname, targets, e.getMessage()); + socket.send(Buffer.buffer( + buildServerFailureResponse(query, txId, questionOffset, questionEnd)), + senderPort, senderHost, v -> {}); + }); + } + + private static List shuffled(List targets) { + if (targets.size() < 2) { + return targets; + } + List order = new ArrayList<>(targets); + Collections.shuffle(order, ThreadLocalRandom.current()); + return order; + } + /** * Forwards the query to each upstream in order and returns the first valid UDP response. * Throws if every upstream times out or errors, so the caller can log a single warning. * The {@code upstreamPort} is parameterised for tests; production always uses {@link #DNS_PORT}. */ static byte[] forwardToUpstreams(byte[] query, List upstreams, int upstreamPort) throws Exception { + return forwardToTargets(query, upstreams.stream() + .map(upstream -> new DnsForwardingRule.Target(upstream, upstreamPort)).toList()); + } + + /** + * Forwards the query to each target in order and returns the first valid UDP response. Targets + * carry their own port because a resolver rule may name a resolver that does not listen on 53. + */ + static byte[] forwardToTargets(byte[] query, List targets) throws Exception { Exception last = null; - for (String upstream : upstreams) { + for (DnsForwardingRule.Target target : targets) { try (java.net.DatagramSocket fwd = new java.net.DatagramSocket()) { fwd.setSoTimeout(FORWARD_TIMEOUT_MS); - InetAddress addr = InetAddress.getByName(upstream); - fwd.send(new DatagramPacket(query, query.length, addr, upstreamPort)); + InetAddress addr = InetAddress.getByName(target.address()); + fwd.send(new DatagramPacket(query, query.length, addr, target.port())); byte[] buf = new byte[MAX_DNS_UDP_RESPONSE]; DatagramPacket resp = new DatagramPacket(buf, buf.length); fwd.receive(resp); return Arrays.copyOf(resp.getData(), resp.getLength()); } catch (Exception e) { last = e; - LOG.debugv("DNS forward to {0} failed: {1}", upstream, e.getMessage()); + LOG.debugv("DNS forward to {0} failed: {1}", target.address(), e.getMessage()); } } - throw last != null ? last : new IOException("no upstream resolvers configured"); + throw last != null ? last : new IOException("no resolvers to forward to"); } /** diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java index 814187bc6b..85c0622bbe 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java @@ -10,6 +10,8 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.config.FlociCertificateAuthority; import io.github.hectorvent.floci.core.common.AwsRegions; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.dns.DnsClientVpcSource; +import io.github.hectorvent.floci.core.common.dns.DnsClientVpcSource.ClientVpc; import io.github.hectorvent.floci.core.common.docker.ContainerBuilder; import io.github.hectorvent.floci.core.common.docker.ContainerDetector; import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager; @@ -80,7 +82,8 @@ import java.util.stream.Collectors; * Not used when {@code floci.services.eks.mock=true}. */ @ApplicationScoped -public class EksClusterManager implements ClusterNodeInstanceProvider, VpcRouteTableListener { +public class EksClusterManager + implements ClusterNodeInstanceProvider, VpcRouteTableListener, DnsClientVpcSource { private static final Logger LOG = Logger.getLogger(EksClusterManager.class); private static final int K3S_API_SERVER_PORT = 6443; @@ -174,6 +177,61 @@ public class EksClusterManager implements ClusterNodeInstanceProvider, VpcRouteT record ClusterNodeRecord(String accountId, String region, Instance instance) {} + /** Which cluster owns a Docker address its node container answers on, and where that cluster sits. */ + record ClusterNodeVpc(String clusterResourceName, ClientVpc clientVpc) {} + + private final Map clusterNodeVpcs = new ConcurrentHashMap<>(); + + /** + * A DNS query from a cluster container originates in the account, region and VPC the cluster was + * created with, which is what a Route 53 Resolver rule has to belong to for the query to follow + * it. A cluster with no resolvable VPC id claims no address: its queries resolve as they do + * without any rule rather than picking up another cluster's. + */ + @Override + public Optional vpcForClient(String clientAddress) { + if (clientAddress == null || clientAddress.isBlank()) { + return Optional.empty(); + } + return Optional.ofNullable(clusterNodeVpcs.get(clientAddress.trim())) + .map(ClusterNodeVpc::clientVpc); + } + + private void registerClusterNodeVpc(Cluster cluster, String accountId, String region, + Set addresses) { + String clusterKey = clusterResourceName(cluster); + forgetClusterNodeVpcs(clusterKey); + String vpcId = cluster.getResourcesVpcConfig() != null + ? cluster.getResourcesVpcConfig().getVpcId() : null; + if (vpcId == null || vpcId.isBlank()) { + return; + } + Set usable = new LinkedHashSet<>(); + for (String address : addresses) { + if (address != null && !address.isBlank()) { + usable.add(address); + } + } + if (usable.isEmpty()) { + // Without the container's own addresses there is nothing to recognise its queries by, so + // resolver rules cannot apply to this cluster. Said out loud rather than left as silence: + // the cluster runs fine and only rule-steered names behave as though no rule existed. + LOG.warnv("Resolver rules will not apply to EKS cluster {0} in {1}: its container" + + " addresses could not be determined, so its DNS queries cannot be attributed" + + " to the VPC. Restart the cluster to retry.", cluster.getName(), vpcId); + return; + } + ClientVpc clientVpc = new ClientVpc(accountId, region, vpcId); + for (String address : usable) { + clusterNodeVpcs.put(address, new ClusterNodeVpc(clusterKey, clientVpc)); + } + } + + private void forgetClusterNodeVpcs(String clusterResourceName) { + clusterNodeVpcs.entrySet().removeIf( + entry -> clusterResourceName.equals(entry.getValue().clusterResourceName())); + } + public EksClusterManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager, ContainerDetector containerDetector, @@ -1692,6 +1750,7 @@ public class EksClusterManager implements ClusterNodeInstanceProvider, VpcRouteT Instance nodeInstance = synthesizeClusterNodeInstance(cluster, containerIps.primaryIp(), region, accountId); nodeInstance.setDockerContainerId(containerId); clusterNodeInstances.put(clusterResourceName(cluster), new ClusterNodeRecord(accountId, region, nodeInstance)); + registerClusterNodeVpc(cluster, accountId, region, containerIps.allIps()); for (Consumer listener : nodeRegistrationListeners) { try { listener.accept(nodeInstance); @@ -1936,6 +1995,7 @@ public class EksClusterManager implements ClusterNodeInstanceProvider, VpcRouteT activeClusters.remove(clusterKey); programmedClusterRoutes.remove(clusterKey); } + forgetClusterNodeVpcs(clusterKey); ClusterNodeRecord record = clusterNodeInstances.remove(clusterKey); Instance nodeInstance = record != null ? record.instance() : null; if (metadataServer != null && nodeInstance != null) { diff --git a/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverDnsForwardingRules.java b/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverDnsForwardingRules.java new file mode 100644 index 0000000000..3ec09bbf31 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverDnsForwardingRules.java @@ -0,0 +1,94 @@ +package io.github.hectorvent.floci.services.route53resolver; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.dns.DnsForwardingRule; +import io.github.hectorvent.floci.core.common.dns.DnsForwardingRuleSource; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Optional; + +/** + * Turns the resolver rules {@link Route53ResolverService} stores into the forwarding rules the + * embedded DNS server applies. Discovered via CDI as a {@link DnsForwardingRuleSource}, so the DNS + * server never imports this service, and the rules are read from its stores rather than copied. + * + *

Only rules the querying account has associated with the querying VPC, in the querying region, + * are returned. An unassociated rule is inert in AWS, an association naming a VPC of some other + * account is not that account's business, and Route 53 Resolver is regional, so a rule governs + * nothing outside the region it was created in. {@code FORWARD} rules carry their target resolvers; + * {@code SYSTEM} and {@code RECURSIVE} rules carry none and so hand the query back to Floci's own + * resolution, which is what both mean in AWS. {@code DELEGATE} is stored but not applied. + */ +@ApplicationScoped +public class Route53ResolverDnsForwardingRules implements DnsForwardingRuleSource { + + private final Route53ResolverService resolverService; + private final EmulatorConfig config; + + @Inject + public Route53ResolverDnsForwardingRules(Route53ResolverService resolverService, EmulatorConfig config) { + this.resolverService = resolverService; + this.config = config; + } + + @Override + public List rulesFor(String accountId, String region, String vpcId) { + if (!config.services().route53resolver().enabled()) { + return List.of(); + } + List rules = new ArrayList<>(); + for (String ruleId : resolverService.resolverRuleIdsAssociatedWith(accountId, vpcId)) { + resolverService.resolverRuleIn(accountId, region, ruleId) + .flatMap(Route53ResolverDnsForwardingRules::forwardingRuleFor) + .ifPresent(rules::add); + } + // Most specific first, so the DNS server's choice between two equally specific rules is the + // same on every query rather than following whatever order the store scanned in. + rules.sort(Comparator.comparingInt(DnsForwardingRule::specificity).reversed() + .thenComparing(DnsForwardingRule::domainName)); + return List.copyOf(rules); + } + + private static Optional forwardingRuleFor(ObjectNode rule) { + String domainName = text(rule, "DomainName"); + if (domainName == null || domainName.isBlank()) { + return Optional.empty(); + } + String ruleType = text(rule, "RuleType"); + return switch (ruleType == null ? "" : ruleType) { + case "FORWARD" -> targetsOf(rule).map(targets -> DnsForwardingRule.forwardTo(domainName, targets)); + case "SYSTEM", "RECURSIVE" -> Optional.of(DnsForwardingRule.system(domainName)); + default -> Optional.empty(); + }; + } + + /** + * The rule's IPv4 targets. {@code Ipv6} targets are skipped because the embedded DNS server + * binds and forwards over IPv4 only, and a rule left with no usable target is dropped rather + * than applied, so its queries resolve as they would without it. + */ + private static Optional> targetsOf(ObjectNode rule) { + List targets = new ArrayList<>(); + for (JsonNode target : rule.path("TargetIps")) { + String address = text(target, "Ip"); + if (address == null || address.isBlank()) { + continue; + } + int port = target.path("Port").asInt(DnsForwardingRule.Target.DEFAULT_DNS_PORT); + targets.add(new DnsForwardingRule.Target(address.trim(), + port > 0 && port <= 65535 ? port : DnsForwardingRule.Target.DEFAULT_DNS_PORT)); + } + return targets.isEmpty() ? Optional.empty() : Optional.of(targets); + } + + private static String text(JsonNode node, String field) { + JsonNode value = node == null ? null : node.get(field); + return value == null || value.isNull() ? null : value.asText(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java b/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java index a2eeb05d07..ef737808ad 100644 --- a/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java +++ b/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsRegions; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import jakarta.enterprise.context.ApplicationScoped; @@ -17,8 +18,13 @@ import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.Instant; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.HexFormat; import java.util.List; +import java.util.Map; +import java.util.Optional; import java.util.UUID; /** @@ -88,16 +94,16 @@ public class Route53ResolverService { @Inject public Route53ResolverService(StorageFactory storageFactory, ObjectMapper objectMapper) { this.domainListStore = storageFactory.create("route53resolver", "route53resolver-domain-lists.json", - new TypeReference>() {}); + new TypeReference>() {}); this.endpointStore = storageFactory.create("route53resolver", "route53resolver-endpoints.json", - new TypeReference>() {}); + new TypeReference>() {}); this.ruleStore = storageFactory.create("route53resolver", "route53resolver-rules.json", - new TypeReference>() {}); + new TypeReference>() {}); this.ruleAssociationStore = storageFactory.create("route53resolver", - "route53resolver-rule-associations.json", new TypeReference>() {}); + "route53resolver-rule-associations.json", new TypeReference>() {}); this.endpointIpRequestStore = storageFactory.create("route53resolver", "route53resolver-endpoint-ip-requests.json", - new TypeReference>() {}); + new TypeReference>() {}); this.objectMapper = objectMapper; } @@ -137,7 +143,7 @@ public class Route53ResolverService { public synchronized ObjectNode createFirewallDomainList(JsonNode request, String region, String accountId) { String name = requireText(request, "Name", VALIDATION); - java.util.Optional replay = replayOf(domainListStore, request, region); + Optional replay = replayOf(domainListStore, request, region); if (replay.isPresent()) { return replay.get(); } @@ -168,7 +174,7 @@ public class Route53ResolverService { return domainListStore.scan(key -> true).stream().map(ObjectNode::deepCopy).toList(); } - public java.util.Optional getCustomFirewallDomainList(String id) { + public Optional getCustomFirewallDomainList(String id) { return domainListStore.get(id).map(ObjectNode::deepCopy); } @@ -182,7 +188,7 @@ public class Route53ResolverService { if (!ipAddresses.isArray() || ipAddresses.isEmpty()) { throw new AwsException(INVALID_PARAMETER, "IpAddresses is required", 400); } - java.util.Optional replay = replayOf(endpointStore, request, region); + Optional replay = replayOf(endpointStore, request, region); if (replay.isPresent()) { ObjectNode existing = replay.get(); requireReplayMatches(existing, request, "Name", "Direction", "SecurityGroupIds"); @@ -250,7 +256,7 @@ public class Route53ResolverService { "TargetIps must contain at least one target address.", 400); } String domainName = text(request, "DomainName"); - java.util.Optional replay = replayOf(ruleStore, request, region); + Optional replay = replayOf(ruleStore, request, region); if (replay.isPresent()) { requireReplayMatches(replay.get(), request, "Name", "RuleType", "DomainName", "TargetIps", "ResolverEndpointId"); @@ -338,6 +344,75 @@ public class Route53ResolverService { return ruleAssociationStore.scan(key -> true).stream().map(ObjectNode::deepCopy).toList(); } + /** + * The ids of the rules {@code accountId} has associated with {@code vpcId}. The DNS server reads + * these off the packet path, where there is no caller, so the account comes from the resource the + * query originated on rather than from a request context. + * + *

Scoped to that one account deliberately. A {@code VPCId} is not proof of ownership: + * {@code AssociateResolverRule} stores whatever VPC id the caller names, so an association made + * by another account naming this account's VPC must not steer its queries. Restricting the + * lookup to the querying account's partition is the boundary, and it costs nothing because + * {@code AssociateResolverRule} can only reference a rule that exists in the caller's own + * partition, so an association and its rule always share an account. + * + *

Returns ids rather than the association nodes so the DNS path does not copy a record per + * association on every query when all it needs is which rules to look at. + */ + public List resolverRuleIdsAssociatedWith(String accountId, String vpcId) { + if (accountId == null || accountId.isBlank() || vpcId == null || vpcId.isBlank()) { + return List.of(); + } + List ruleIds = new ArrayList<>(); + for (ObjectNode association : associationsForAccount(accountId)) { + String ruleId = text(association, "ResolverRuleId"); + if (ruleId != null && !ruleIds.contains(ruleId) && vpcId.equals(text(association, "VPCId"))) { + ruleIds.add(ruleId); + } + } + return List.copyOf(ruleIds); + } + + private List associationsForAccount(String accountId) { + if (ruleAssociationStore instanceof AccountAwareStorageBackend rawAccountAware) { + @SuppressWarnings("unchecked") + AccountAwareStorageBackend accountAware = + (AccountAwareStorageBackend) rawAccountAware; + return accountAware.scanForAccount(accountId, key -> true); + } + return ruleAssociationStore.scan(key -> true); + } + + /** + * One of an account's resolver rules by id, but only if it belongs to {@code region}. Fetched + * rather than scanned for, so the DNS path reads only the rules an association actually named. + * + *

Route 53 Resolver is regional: a rule created in one region governs nothing in another. The + * stores carry no region in their keys, so the rule's own region comes from the ARN this service + * minted for it, the same place {@link #replayOf} reads it from. A stored rule with no parseable + * ARN has no region to claim and is treated as belonging to the region asking, which no create + * path can produce and which cannot leak another region's rule. + */ + public Optional resolverRuleIn(String accountId, String region, String ruleId) { + if (accountId == null || accountId.isBlank() || region == null || region.isBlank() + || ruleId == null || ruleId.isBlank()) { + return Optional.empty(); + } + return storedRule(accountId, ruleId) + .filter(rule -> region.equals(AwsArnUtils.regionOrDefault(text(rule, "Arn"), region))) + .map(ObjectNode::deepCopy); + } + + private Optional storedRule(String accountId, String ruleId) { + if (ruleStore instanceof AccountAwareStorageBackend rawAccountAware) { + @SuppressWarnings("unchecked") + AccountAwareStorageBackend accountAware = + (AccountAwareStorageBackend) rawAccountAware; + return accountAware.getForAccount(accountId, ruleId); + } + return ruleStore.get(ruleId); + } + /** * AWS gives a resolver endpoint a direction-specific id prefix: {@code rslvr-in-} for * inbound endpoints, {@code rslvr-out-} for outbound. {@code INBOUND_DELEGATION} is an @@ -373,11 +448,11 @@ public class Route53ResolverService { * shapes have no field for. Same intent as {@code FisService.idempotencyKey} and * {@code BedrockAgentCoreControlService.tokenKey}, which fold the region into the key.

*/ - private java.util.Optional replayOf(StorageBackend store, JsonNode request, + private Optional replayOf(StorageBackend store, JsonNode request, String region) { String creatorRequestId = text(request, "CreatorRequestId"); if (creatorRequestId == null || creatorRequestId.isBlank()) { - return java.util.Optional.empty(); + return Optional.empty(); } String regionPrefix = "arn:" + AwsRegions.partitionFor(region) + ":route53resolver:" + region + ":"; return store.scan(key -> true).stream() @@ -478,9 +553,9 @@ public class Route53ResolverService { * key must not depend on it.

*/ private ArrayNode normalizedIpRequests(JsonNode ipAddresses) { - List entries = new java.util.ArrayList<>(); + List entries = new ArrayList<>(); ipAddresses.forEach(entries::add); - entries.sort(java.util.Comparator.comparing(Route53ResolverService::canonicalKey)); + entries.sort(Comparator.comparing(Route53ResolverService::canonicalKey)); ArrayNode normalized = objectMapper.createArrayNode(); entries.forEach(normalized::add); return normalized; @@ -489,9 +564,9 @@ public class Route53ResolverService { /** A node's contents as a string that does not depend on the order its members were written in. */ private static String canonicalKey(JsonNode node) { if (node.isObject()) { - List names = new java.util.ArrayList<>(); + List names = new ArrayList<>(); node.fieldNames().forEachRemaining(names::add); - java.util.Collections.sort(names); + Collections.sort(names); StringBuilder key = new StringBuilder("{"); for (String name : names) { key.append(name).append('=').append(canonicalKey(node.get(name))).append(';'); diff --git a/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java index 021ff77fee..e67d855d34 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java @@ -7,6 +7,8 @@ import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.Optional; @@ -191,6 +193,185 @@ class EmbeddedDnsServerTest { return DnsAnswer.records(List.of(addresses), DnsAnswer.DEFAULT_TTL_SECONDS); } + // ── planQuery: Route 53 Resolver rules ──────────────────────────────────── + + private static final String CLIENT = "172.31.0.9"; + private static final String MY_IP = "172.31.0.2"; + private static final String ACCOUNT = "000000000000"; + private static final String REGION = "us-east-1"; + + @Test + void forwardingRuleMatchesOnlyItsDomainAndTheSubdomainsBeneathIt() { + DnsForwardingRule rule = forward("acme.example.com", "10.1.0.53"); + + assertTrue(rule.matches("acme.example.com")); + assertTrue(rule.matches("zenith.acme.example.com.")); + assertFalse(rule.matches("example.com")); + assertFalse(rule.matches("nadir.example.com")); + assertFalse(rule.matches("notacme.example.com")); + assertEquals(3, rule.specificity()); + assertTrue(forward(".", "10.1.0.53").matches("anything.example.com")); + assertEquals(0, forward(".", "10.1.0.53").specificity()); + } + + @Test + void planQuery_forwardsToTheTargetsOfARuleAssociatedWithTheQueryingVpc() { + EmbeddedDnsServer dns = withRules(List.of(), + rules("vpc-a", forward("corp.internal", "10.1.0.53")), clientVpc("vpc-a")); + + EmbeddedDnsServer.QueryPlan plan = dns.planQuery("db.corp.internal", CLIENT, MY_IP, 1); + + assertTrue(plan.answer().isEmpty()); + assertEquals(List.of(new DnsForwardingRule.Target("10.1.0.53", 53)), plan.ruleTargets()); + } + + @Test + void planQuery_ignoresARuleThatIsNotAssociatedWithTheQueryingVpc() { + EmbeddedDnsServer dns = withRules(List.of(), + rules("vpc-other", forward("corp.internal", "10.1.0.53")), clientVpc("vpc-a")); + + assertEquals(EmbeddedDnsServer.QueryPlan.UPSTREAM, + dns.planQuery("db.corp.internal", CLIENT, MY_IP, 1)); + } + + @Test + void planQuery_appliesNoRuleWhenNoSourceClaimsTheQueryAddress() { + EmbeddedDnsServer dns = withRules(List.of(), + rules("vpc-a", forward("corp.internal", "10.1.0.53")), + clientAddress -> Optional.empty()); + + assertEquals(EmbeddedDnsServer.QueryPlan.UPSTREAM, + dns.planQuery("db.corp.internal", CLIENT, MY_IP, 1)); + } + + @Test + void planQuery_appliesNoRuleWhenTheQueryOriginIsIncomplete() { + DnsForwardingRuleSource anyOrigin = (accountId, region, vpcId) -> + List.of(forward("corp.internal", "10.1.0.53")); + + for (DnsClientVpcSource.ClientVpc partial : List.of( + new DnsClientVpcSource.ClientVpc(ACCOUNT, REGION, ""), + new DnsClientVpcSource.ClientVpc(ACCOUNT, "", "vpc-a"), + new DnsClientVpcSource.ClientVpc("", REGION, "vpc-a"))) { + EmbeddedDnsServer dns = withRules(List.of(), anyOrigin, + clientAddress -> Optional.of(partial)); + + assertEquals(EmbeddedDnsServer.QueryPlan.UPSTREAM, + dns.planQuery("db.corp.internal", CLIENT, MY_IP, 1), partial.toString()); + } + } + + @Test + void planQuery_appliesTheMoreSpecificOfTwoMatchingRules() { + EmbeddedDnsServer dns = withRules(List.of(), + rules("vpc-a", forward("corp.internal", "10.1.0.53"), + forward("db.corp.internal", "10.2.0.53")), + clientVpc("vpc-a")); + + assertEquals(List.of(new DnsForwardingRule.Target("10.2.0.53", 53)), + dns.planQuery("replica.db.corp.internal", CLIENT, MY_IP, 1).ruleTargets()); + assertEquals(List.of(new DnsForwardingRule.Target("10.1.0.53", 53)), + dns.planQuery("web.corp.internal", CLIENT, MY_IP, 1).ruleTargets()); + } + + @Test + void planQuery_aSystemRuleCarvesASubdomainOutOfABroaderForwardRule() { + EmbeddedDnsServer dns = withRules( + List.of(source("acme.corp.internal", List.of("10.9.9.9"))), + rules("vpc-a", forward("corp.internal", "10.1.0.53"), + DnsForwardingRule.system("acme.corp.internal")), + clientVpc("vpc-a")); + + assertEquals(List.of("10.9.9.9"), + dns.planQuery("acme.corp.internal", CLIENT, MY_IP, 1).answer().orElseThrow().addresses()); + assertEquals(List.of(new DnsForwardingRule.Target("10.1.0.53", 53)), + dns.planQuery("other.corp.internal", CLIENT, MY_IP, 1).ruleTargets()); + } + + @Test + void planQuery_aMatchingRuleTakesPrecedenceOverAPrivateHostedZoneRecord() { + EmbeddedDnsServer dns = withRules( + List.of(source("db.corp.internal", List.of("10.0.0.7"))), + rules("vpc-a", forward("corp.internal", "10.1.0.53")), clientVpc("vpc-a")); + + EmbeddedDnsServer.QueryPlan plan = dns.planQuery("db.corp.internal", CLIENT, MY_IP, 1); + + assertTrue(plan.answer().isEmpty(), "the rule decides the name, not the zone record"); + assertEquals(List.of(new DnsForwardingRule.Target("10.1.0.53", 53)), plan.ruleTargets()); + } + + @Test + void planQuery_aPrivateHostedZoneRecordAnswersWhenNoRuleIsAssociated() { + EmbeddedDnsServer dns = withRules( + List.of(source("db.corp.internal", List.of("10.0.0.7"))), + rules("vpc-other", forward("corp.internal", "10.1.0.53")), clientVpc("vpc-a")); + + assertEquals(List.of("10.0.0.7"), + dns.planQuery("db.corp.internal", CLIENT, MY_IP, 1).answer().orElseThrow().addresses()); + } + + @Test + void planQuery_aNameMatchingNoRuleStillGoesToTheUpstreamResolvers() { + EmbeddedDnsServer dns = withRules( + List.of(source("db.corp.internal", List.of("10.0.0.7"))), + rules("vpc-a", forward("corp.internal", "10.1.0.53")), clientVpc("vpc-a")); + + assertEquals(EmbeddedDnsServer.QueryPlan.UPSTREAM, dns.planQuery("example.com", CLIENT, MY_IP, 1)); + } + + @Test + void planQuery_neverForwardsTheEmulatorsOwnNames() { + EmbeddedDnsServer dns = withRules(List.of(), rules("vpc-a", forward(".", "10.1.0.53")), + clientVpc("vpc-a")); + + assertEquals(List.of(MY_IP), + dns.planQuery("bucket.localhost.floci.io", CLIENT, MY_IP, 1).answer().orElseThrow().addresses()); + assertEquals(List.of("172.16.128.9"), + dns.planQuery("ip-172-16-128-9.ec2.internal", CLIENT, MY_IP, 1).answer().orElseThrow().addresses()); + assertEquals(List.of(new DnsForwardingRule.Target("10.1.0.53", 53)), + dns.planQuery("example.com", CLIENT, MY_IP, 1).ruleTargets()); + } + + @Test + void planQuery_keepsResolvingWhenARuleSourceThrows() { + DnsForwardingRuleSource failing = (accountId, region, vpcId) -> { + throw new IllegalStateException("storage is down"); + }; + EmbeddedDnsServer dns = new EmbeddedDnsServer(List.of("localhost.floci.io"), + List.of(source("db.corp.internal", List.of("10.0.0.7"))), + List.of(failing), List.of(clientVpc("vpc-a"))); + + assertEquals(List.of("10.0.0.7"), + dns.planQuery("db.corp.internal", CLIENT, MY_IP, 1).answer().orElseThrow().addresses()); + } + + private static EmbeddedDnsServer withRules(List recordSources, + DnsForwardingRuleSource ruleSource, + DnsClientVpcSource clientVpcSource) { + return new EmbeddedDnsServer(List.of("localhost.floci.io"), recordSources, + List.of(ruleSource), List.of(clientVpcSource)); + } + + /** Returns the rules most specific first, as {@link DnsForwardingRuleSource} asks sources to. */ + private static DnsForwardingRuleSource rules(String associatedVpcId, DnsForwardingRule... rules) { + List ordered = Arrays.stream(rules) + .sorted(Comparator.comparingInt(DnsForwardingRule::specificity).reversed()) + .toList(); + return (accountId, region, vpcId) -> ACCOUNT.equals(accountId) && REGION.equals(region) + && associatedVpcId.equals(vpcId) ? ordered : List.of(); + } + + private static DnsClientVpcSource clientVpc(String vpcId) { + return clientAddress -> CLIENT.equals(clientAddress) + ? Optional.of(new DnsClientVpcSource.ClientVpc(ACCOUNT, REGION, vpcId)) + : Optional.empty(); + } + + private static DnsForwardingRule forward(String domainName, String... targets) { + return DnsForwardingRule.forwardTo(domainName, + Arrays.stream(targets).map(DnsForwardingRule.Target::new).toList()); + } + @Test void buildAResponse_usesTheDefaultTtlForFlociNames() { DnsAnswer answer = dns.resolveARecordWithOwnership( @@ -418,6 +599,15 @@ class EmbeddedDnsServerTest { } } + @Test + void forwardToTargets_throwsWhenEveryTargetIsUnreachable() { + // Nothing listens on port 1 of the loopback, so every target fails and the caller decides + // what a failed rule means rather than silently getting an upstream answer. + assertThrows(Exception.class, () -> EmbeddedDnsServer.forwardToTargets( + buildQuery("db.corp.internal", (short) 8), + List.of(new DnsForwardingRule.Target("127.0.0.1", 1)))); + } + /** Replies to the first datagram received with a fixed payload, on a daemon thread. */ private void startResponder(DatagramSocket responder, byte[] responsePayload) { Thread t = new Thread(() -> { diff --git a/src/test/java/io/github/hectorvent/floci/core/common/dns/ResolverRuleDnsPacketTest.java b/src/test/java/io/github/hectorvent/floci/core/common/dns/ResolverRuleDnsPacketTest.java new file mode 100644 index 0000000000..6aecd64afb --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/dns/ResolverRuleDnsPacketTest.java @@ -0,0 +1,164 @@ +package io.github.hectorvent.floci.core.common.dns; + +import io.quarkus.test.junit.QuarkusTest; +import io.vertx.core.Vertx; +import io.vertx.core.buffer.Buffer; +// Vert.x's DatagramSocket is the one used most here; the JDK's is qualified inline where the +// test needs a plain blocking socket to stand in for the rule's target resolver. +import io.vertx.core.datagram.DatagramSocket; +import io.vertx.core.datagram.DatagramSocketOptions; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Drives the whole packet path with a forwarding rule in place: the query really leaves the DNS + * server over UDP, and what comes back is what the rule's target answered. + */ +@QuarkusTest +class ResolverRuleDnsPacketTest { + + private static final String CLIENT = "127.0.0.1"; + private static final String ACCOUNT = "000000000000"; + private static final String REGION = "us-east-1"; + private static final String VPC_ID = "vpc-0dns0000"; + + @Inject + Vertx vertx; + + @Test + void aForwardedQueryIsAnsweredByTheRulesTarget() throws Exception { + byte[] request = buildQuery("db.corp.internal"); + byte[] targetAnswer = answerFor(request, "10.4.0.11"); + + try (java.net.DatagramSocket target = new java.net.DatagramSocket(0, InetAddress.getByName(CLIENT))) { + respondOnce(target, targetAnswer); + EmbeddedDnsServer dns = serverForwarding("corp.internal", target.getLocalPort()); + + byte[] response = query(dns, request); + + assertArrayEquals(targetAnswer, response, + "the answer the rule's target gave must reach the client unchanged"); + } + } + + @Test + void unreachableRuleTargetsFailOnlyTheQueryTheRuleMatched() throws Exception { + // Port 1 on the loopback has no resolver on it, so every target of the rule fails. + EmbeddedDnsServer dns = serverForwarding("corp.internal", 1); + + byte[] failed = query(dns, buildQuery("db.corp.internal")); + assertEquals(2, ByteBuffer.wrap(failed).getShort(2) & 0x000F, "SERVFAIL for the rule's name"); + assertEquals(0, ByteBuffer.wrap(failed).getShort(2) & 0x0400, + "Floci is not the authority for a name a rule sends elsewhere"); + + byte[] request = buildQuery("api.sapphire.internal"); + byte[] resolved = query(dns, request); + assertEquals(0, ByteBuffer.wrap(resolved).getShort(2) & 0x000F); + assertEquals(1, ByteBuffer.wrap(resolved).getShort(6)); + assertArrayEquals(new byte[]{(byte) 172, 31, 0, 6}, + Arrays.copyOfRange(resolved, request.length + 12, request.length + 16)); + } + + private EmbeddedDnsServer serverForwarding(String domainName, int targetPort) { + DnsForwardingRule rule = DnsForwardingRule.forwardTo(domainName, + List.of(new DnsForwardingRule.Target(CLIENT, targetPort))); + DnsRecordSource records = name -> "api.sapphire.internal".equals(name) + ? Optional.of(DnsAnswer.records(List.of("172.31.0.6"), 60)) : Optional.empty(); + return new EmbeddedDnsServer(List.of(), List.of(records), + List.of((accountId, region, vpcId) -> ACCOUNT.equals(accountId) + && REGION.equals(region) && VPC_ID.equals(vpcId) ? List.of(rule) : List.of()), + List.of(clientAddress -> + Optional.of(new DnsClientVpcSource.ClientVpc(ACCOUNT, REGION, VPC_ID)))); + } + + /** Answers the first datagram with {@code payload}, on a daemon thread. */ + private void respondOnce(java.net.DatagramSocket socket, byte[] payload) { + Thread responder = new Thread(() -> { + try { + DatagramPacket received = new DatagramPacket(new byte[4096], 4096); + socket.receive(received); + socket.send(new DatagramPacket(payload, payload.length, + received.getAddress(), received.getPort())); + } catch (Exception ignored) { + // the socket is closed when the test completes + } + }); + responder.setDaemon(true); + responder.start(); + } + + private byte[] query(EmbeddedDnsServer dns, byte[] request) throws Exception { + DatagramSocket server = vertx.createDatagramSocket(new DatagramSocketOptions().setIpV6(false)); + DatagramSocket client = vertx.createDatagramSocket(new DatagramSocketOptions().setIpV6(false)); + try { + server.listen(0, CLIENT).toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS); + server.handler(packet -> dns.handleQuery(vertx, server, packet.data().getBytes(), + packet.sender().host(), packet.sender().port(), CLIENT)); + client.listen(0, CLIENT).toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS); + CompletableFuture received = new CompletableFuture<>(); + client.handler(packet -> received.complete(packet.data().getBytes())); + + client.send(Buffer.buffer(request), server.localAddress().port(), CLIENT) + .toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS); + return received.get(10, TimeUnit.SECONDS); + } finally { + server.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS); + client.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS); + } + } + + private static byte[] buildQuery(String name) { + String[] labels = name.split("\\."); + int nameLength = 1; + for (String label : labels) { + nameLength += label.length() + 1; + } + ByteBuffer query = ByteBuffer.allocate(12 + nameLength + 4); + query.putShort((short) 0x2468); + query.putShort((short) 0x0100); + query.putShort((short) 1); + query.putShort((short) 0); + query.putShort((short) 0); + query.putShort((short) 0); + for (String label : labels) { + byte[] bytes = label.getBytes(StandardCharsets.US_ASCII); + query.put((byte) bytes.length); + query.put(bytes); + } + query.put((byte) 0); + query.putShort((short) 1); + query.putShort((short) 1); + return query.array(); + } + + /** The query echoed back as a one-answer response, which is what a real resolver would send. */ + private static byte[] answerFor(byte[] request, String address) { + ByteBuffer response = ByteBuffer.allocate(request.length + 16); + response.put(request); + response.putShort(2, (short) 0x8180); + response.putShort(6, (short) 1); + response.position(request.length); + response.putShort((short) 0xC00C); + response.putShort((short) 1); + response.putShort((short) 1); + response.putInt(30); + response.putShort((short) 4); + for (String octet : address.split("\\.")) { + response.put((byte) Integer.parseInt(octet)); + } + return response.array(); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java index c2171360e5..edb2fe22f2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java @@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.config.FlociCertificateAuthority; import io.github.hectorvent.floci.core.common.AwsRegions; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.dns.DnsClientVpcSource.ClientVpc; import io.github.hectorvent.floci.core.common.docker.ContainerBuilder; import io.github.hectorvent.floci.core.common.docker.ContainerDetector; import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager; @@ -63,6 +64,9 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -999,6 +1003,89 @@ class EksClusterManagerTest { assertTrue(instance.getTags().stream().anyMatch(t -> "eks:cluster-name".equals(t.getKey()) && "no-imds-cluster".equals(t.getValue()))); } + @Test + void clusterNodeAddressesResolveToTheClustersVpcForResolverRules() { + Cluster cluster = new Cluster(); + cluster.setName("dns-cluster"); + ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig(); + vpcConfig.setVpcId("vpc-12345678"); + cluster.setResourcesVpcConfig(vpcConfig); + + manager.registerClusterNodeInstance(cluster, "container-dns"); + + assertEquals(Optional.of(new ClientVpc("000000000000", "us-east-1", "vpc-12345678")), + manager.vpcForClient("172.17.0.2")); + assertTrue(manager.vpcForClient("172.17.0.99").isEmpty()); + assertTrue(manager.vpcForClient(null).isEmpty()); + + manager.unregisterMetadataEndpoint(cluster); + assertTrue(manager.vpcForClient("172.17.0.2").isEmpty()); + } + + @Test + void aClusterWithoutAVpcClaimsNoAddress() { + Cluster cluster = new Cluster(); + cluster.setName("no-vpc-cluster"); + + manager.registerClusterNodeInstance(cluster, "container-no-vpc"); + + assertTrue(manager.vpcForClient("172.17.0.2").isEmpty()); + } + + @Test + void aClusterWhoseAddressesCannotBeDeterminedClaimsNoneAndSaysWhy() { + // Inspection failing leaves resolver rules quietly not applying rather than failing + // anything, so the warning is the only way a reader finds out. See #4538 review. + when(dockerClient.inspectContainerCmd(anyString())) + .thenThrow(new RuntimeException("docker is unreachable")); + Cluster cluster = new Cluster(); + cluster.setName("unreachable-cluster"); + ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig(); + vpcConfig.setVpcId("vpc-12345678"); + cluster.setResourcesVpcConfig(vpcConfig); + + List logs = captureClusterManagerLogs( + () -> manager.registerClusterNodeInstance(cluster, "container-unreachable")); + + assertTrue(manager.vpcForClient("172.17.0.2").isEmpty()); + // Compared by severity, not identity: JBoss LogManager has its own Level constants, so + // its WARN is not the same object as java.util.logging's WARNING. + assertTrue(logs.stream().anyMatch( + record -> record.getLevel().intValue() >= Level.WARNING.intValue() + && String.valueOf(record.getMessage()).contains("Resolver rules will not apply")), + "the cluster must say why its rules stopped applying, got: " + + logs.stream().map(LogRecord::getMessage).toList()); + } + + private static List captureClusterManagerLogs(Runnable action) { + java.util.logging.Logger julLogger = + java.util.logging.Logger.getLogger(EksClusterManager.class.getName()); + julLogger.setLevel(Level.ALL); + List records = new ArrayList<>(); + Handler handler = new Handler() { + @Override + public void publish(LogRecord record) { + records.add(record); + } + + @Override + public void flush() { + } + + @Override + public void close() { + } + }; + handler.setLevel(Level.ALL); + julLogger.addHandler(handler); + try { + action.run(); + } finally { + julLogger.removeHandler(handler); + } + return records; + } + @Test void clusterNodeInstanceProviderFindsAndListsRegisteredInstances() { Cluster cluster = new Cluster(); diff --git a/src/test/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverDnsForwardingRulesTest.java b/src/test/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverDnsForwardingRulesTest.java new file mode 100644 index 0000000000..63044dd434 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverDnsForwardingRulesTest.java @@ -0,0 +1,197 @@ +package io.github.hectorvent.floci.services.route53resolver; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.dns.DnsForwardingRule; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class Route53ResolverDnsForwardingRulesTest { + + private static final String REGION = "us-east-1"; + private static final String ACCOUNT = "000000000000"; + + @Inject + Route53ResolverService resolverService; + + @Inject + Route53ResolverDnsForwardingRules forwardingRules; + + @Inject + ObjectMapper objectMapper; + + private final List createdRuleIds = new ArrayList<>(); + + @AfterEach + void deleteCreatedRules() { + for (String ruleId : createdRuleIds) { + try { + resolverService.deleteResolverRule(ruleId); + } catch (Exception ignored) { + // best-effort cleanup; the rule may already be gone + } + } + createdRuleIds.clear(); + } + + @Test + void anAssociatedForwardRuleCarriesItsTargetsAndTheirPorts() { + String vpcId = uniqueVpcId(); + associate(forwardRule("corp.internal", target("10.1.0.53", null), target("10.1.0.54", 5353)), vpcId); + + List rules = forwardingRules.rulesFor(ACCOUNT, REGION, vpcId); + + assertEquals(1, rules.size()); + assertEquals("corp.internal", rules.getFirst().domainName()); + assertEquals(List.of(new DnsForwardingRule.Target("10.1.0.53", 53), + new DnsForwardingRule.Target("10.1.0.54", 5353)), rules.getFirst().targets()); + } + + @Test + void aRuleAppliesToNoVpcUntilItIsAssociatedWithOne() { + createRule(forwardRule("unassociated.internal", target("10.1.0.53", null))); + associate(forwardRule("elsewhere.internal", target("10.1.0.53", null)), uniqueVpcId()); + + assertTrue(forwardingRules.rulesFor(ACCOUNT, REGION, uniqueVpcId()).isEmpty()); + } + + @Test + void anotherAccountSeesNoneOfThisAccountsAssociations() { + // AssociateResolverRule stores whatever VPC id the caller names, so a VPC id is not proof of + // ownership. Only the account whose partition holds the association may be steered by it, + // otherwise any account could redirect another account's cluster DNS by naming its VPC. + String vpcId = uniqueVpcId(); + associate(forwardRule("corp.internal", target("10.1.0.53", null)), vpcId); + assertEquals(1, forwardingRules.rulesFor(ACCOUNT, REGION, vpcId).size()); + + assertTrue(forwardingRules.rulesFor("999999999999", REGION, vpcId).isEmpty()); + assertTrue(resolverService.resolverRuleIdsAssociatedWith("999999999999", vpcId).isEmpty()); + } + + @Test + void noAccountRegionOrVpcMeansNoRules() { + String vpcId = uniqueVpcId(); + associate(forwardRule("corp.internal", target("10.1.0.53", null)), vpcId); + + assertTrue(forwardingRules.rulesFor(null, REGION, vpcId).isEmpty()); + assertTrue(forwardingRules.rulesFor(ACCOUNT, " ", vpcId).isEmpty()); + assertTrue(forwardingRules.rulesFor(ACCOUNT, REGION, " ").isEmpty()); + } + + @Test + void aRuleFromAnotherRegionDoesNotSteerThisRegionsQueries() { + // Route 53 Resolver is regional: "objects that you create in one AWS Region are available + // only in that Region". The stores carry no region in their keys, so the rule's region comes + // from the ARN this service minted for it. + String vpcId = uniqueVpcId(); + String ruleId = createRuleIn(forwardRule("corp.internal", target("10.1.0.53", null)), "eu-west-1"); + ObjectNode association = objectMapper.createObjectNode(); + association.put("ResolverRuleId", ruleId); + association.put("VPCId", vpcId); + resolverService.associateResolverRule(association); + + assertEquals(List.of(ruleId), resolverService.resolverRuleIdsAssociatedWith(ACCOUNT, vpcId)); + assertTrue(forwardingRules.rulesFor(ACCOUNT, REGION, vpcId).isEmpty()); + assertEquals(1, forwardingRules.rulesFor(ACCOUNT, "eu-west-1", vpcId).size()); + } + + @Test + void aSystemRuleCarriesNoTargetsAndSortsAheadOfTheBroaderForwardRule() { + String vpcId = uniqueVpcId(); + associate(forwardRule("corp.internal", target("10.1.0.53", null)), vpcId); + associate(systemRule("acme.corp.internal"), vpcId); + + List rules = forwardingRules.rulesFor(ACCOUNT, REGION, vpcId); + + assertEquals(List.of("acme.corp.internal", "corp.internal"), + rules.stream().map(DnsForwardingRule::domainName).toList()); + assertTrue(rules.getFirst().targets().isEmpty()); + assertTrue(rules.getLast().forwards()); + } + + @Test + void aRecursiveRuleResolvesLocallyAndADelegateRuleIsNotApplied() { + String vpcId = uniqueVpcId(); + associate(ruleRequest("RECURSIVE", "recursive.internal"), vpcId); + associate(ruleRequest("DELEGATE", "delegate.internal"), vpcId); + + List rules = forwardingRules.rulesFor(ACCOUNT, REGION, vpcId); + + assertEquals(List.of("recursive.internal"), + rules.stream().map(DnsForwardingRule::domainName).toList()); + assertTrue(rules.getFirst().targets().isEmpty()); + } + + @Test + void aForwardRuleWithOnlyIpv6TargetsIsNotApplied() { + String vpcId = uniqueVpcId(); + ObjectNode request = ruleRequest("FORWARD", "corp.internal"); + ObjectNode ipv6Target = objectMapper.createObjectNode(); + ipv6Target.put("Ipv6", "2001:db8::53"); + request.putArray("TargetIps").add(ipv6Target); + associate(request, vpcId); + + assertTrue(forwardingRules.rulesFor(ACCOUNT, REGION, vpcId).isEmpty()); + } + + private void associate(ObjectNode ruleRequest, String vpcId) { + ObjectNode association = objectMapper.createObjectNode(); + association.put("ResolverRuleId", createRule(ruleRequest)); + association.put("VPCId", vpcId); + resolverService.associateResolverRule(association); + } + + private String createRule(ObjectNode ruleRequest) { + return createRuleIn(ruleRequest, REGION); + } + + private String createRuleIn(ObjectNode ruleRequest, String region) { + String ruleId = resolverService.createResolverRule(ruleRequest, region, ACCOUNT).get("Id").asText(); + createdRuleIds.add(ruleId); + return ruleId; + } + + private ObjectNode forwardRule(String domainName, ObjectNode... targets) { + ObjectNode request = ruleRequest("FORWARD", domainName); + for (ObjectNode target : targets) { + request.withArray("TargetIps").add(target); + } + return request; + } + + private ObjectNode systemRule(String domainName) { + return ruleRequest("SYSTEM", domainName); + } + + private ObjectNode ruleRequest(String ruleType, String domainName) { + ObjectNode request = objectMapper.createObjectNode(); + request.put("Name", ruleType.toLowerCase() + "-rule"); + request.put("RuleType", ruleType); + request.put("DomainName", domainName); + return request; + } + + private ObjectNode target(String ip, Integer port) { + ObjectNode target = objectMapper.createObjectNode(); + target.put("Ip", ip); + if (port != null) { + target.put("Port", port); + } + return target; + } + + /** A VPC id per test, so rules from other tests in the same store cannot leak into the result. */ + private static String uniqueVpcId() { + return "vpc-" + UUID.randomUUID().toString().replace("-", "").substring(0, 12); + } +}