mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(route53): serve stored public TXT and delegation records (#6034)
Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
be2cb677d0
commit
0e6db1f2b3
11 files changed
+709
-30
No files matched your search
@@ -10,7 +10,15 @@ import java.util.List;
|
||||
* <p>The TTL is per answer rather than per record: every address behind one Cloud Map service
|
||||
* shares that service's TTL, which is how Route 53 publishes a record set.
|
||||
*/
|
||||
public record DnsAnswer(List<DnsRecord> records, int ttlSeconds, boolean nameExists) {
|
||||
public record DnsAnswer(List<DnsRecord> records, int ttlSeconds, boolean nameExists,
|
||||
Authority authority, int responseCode) {
|
||||
|
||||
/** The zone's SOA supplies the authority section and cache lifetime of negative answers. */
|
||||
public record Authority(String name, DnsRecord.Soa soa, int ttlSeconds) {}
|
||||
|
||||
public DnsAnswer(List<DnsRecord> records, int ttlSeconds, boolean nameExists) {
|
||||
this(records, ttlSeconds, nameExists, null, nameExists ? 0 : 3);
|
||||
}
|
||||
|
||||
/** What the DNS server publishes for a name whose zone declares no TTL of its own. */
|
||||
public static final int DEFAULT_TTL_SECONDS = 60;
|
||||
@@ -37,6 +45,21 @@ public record DnsAnswer(List<DnsRecord> records, int ttlSeconds, boolean nameExi
|
||||
return new DnsAnswer(records, ttlSeconds, true);
|
||||
}
|
||||
|
||||
public DnsAnswer withAuthority(String zoneName, DnsRecord.Soa soa, int soaTtl) {
|
||||
int negativeTtl = (int) Math.min(soaTtl, soa.minimum());
|
||||
return new DnsAnswer(records, ttlSeconds, nameExists,
|
||||
new Authority(zoneName, soa, negativeTtl), responseCode);
|
||||
}
|
||||
|
||||
/** An owned public query whose type, routing policy, or zone identity cannot be served. */
|
||||
public static DnsAnswer refused() {
|
||||
return new DnsAnswer(List.of(), DEFAULT_TTL_SECONDS, true, null, 5);
|
||||
}
|
||||
|
||||
public boolean isPublicAuthorityAnswer() {
|
||||
return authority != null || responseCode == 5;
|
||||
}
|
||||
|
||||
/** IPv4 projection retained for callers that explicitly resolve A records. */
|
||||
public List<String> addresses() {
|
||||
return records.stream().filter(record -> record instanceof DnsRecord.Address && record.type() == 1)
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common.dns;
|
||||
|
||||
import io.netty.util.NetUtil;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@@ -36,6 +37,126 @@ public sealed interface DnsRecord {
|
||||
}
|
||||
}
|
||||
|
||||
record Ns(String target) implements DnsRecord {
|
||||
@Override
|
||||
public int type() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] data() {
|
||||
return encodeName(target);
|
||||
}
|
||||
}
|
||||
|
||||
/** Route 53 TXT values use DNS presentation syntax, including multiple quoted strings. */
|
||||
record Txt(String value) implements DnsRecord {
|
||||
@Override
|
||||
public int type() {
|
||||
return 16;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] data() {
|
||||
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||
int index = 0;
|
||||
while (index < value.length()) {
|
||||
if (Character.isWhitespace(value.charAt(index))) {
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
if (value.charAt(index++) != '"') {
|
||||
throw new IllegalArgumentException("TXT strings must be quoted");
|
||||
}
|
||||
ByteArrayOutputStream text = new ByteArrayOutputStream();
|
||||
boolean closed = false;
|
||||
while (index < value.length()) {
|
||||
char character = value.charAt(index++);
|
||||
if (character == '"') {
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
if (character == '\\') {
|
||||
if (index == value.length()) {
|
||||
throw new IllegalArgumentException("Incomplete TXT escape");
|
||||
}
|
||||
character = value.charAt(index++);
|
||||
if (character >= '0' && character <= '9') {
|
||||
if (character > '7' || index + 1 >= value.length()
|
||||
|| value.charAt(index) < '0' || value.charAt(index) > '7'
|
||||
|| value.charAt(index + 1) < '0' || value.charAt(index + 1) > '7') {
|
||||
throw new IllegalArgumentException("TXT octal escapes require three octal digits");
|
||||
}
|
||||
int octet = (character - '0') * 64 + (value.charAt(index) - '0') * 8
|
||||
+ value.charAt(index + 1) - '0';
|
||||
if (octet > 255) {
|
||||
throw new IllegalArgumentException("TXT escape exceeds one byte");
|
||||
}
|
||||
text.write(octet);
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
int codePoint = character;
|
||||
if (Character.isHighSurrogate(character) && index < value.length()
|
||||
&& Character.isLowSurrogate(value.charAt(index))) {
|
||||
codePoint = Character.toCodePoint(character, value.charAt(index++));
|
||||
}
|
||||
text.writeBytes(new String(Character.toChars(codePoint)).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
if (!closed || text.size() > 255) {
|
||||
throw new IllegalArgumentException("TXT character strings must close and contain at most 255 bytes");
|
||||
}
|
||||
result.write(text.size());
|
||||
result.writeBytes(text.toByteArray());
|
||||
}
|
||||
if (result.size() == 0) {
|
||||
throw new IllegalArgumentException("TXT requires at least one character string");
|
||||
}
|
||||
return result.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
record Soa(String value) implements DnsRecord {
|
||||
@Override
|
||||
public int type() {
|
||||
return 6;
|
||||
}
|
||||
|
||||
private String[] fields() {
|
||||
String[] fields = value.trim().split("\\s+");
|
||||
if (fields.length != 7) {
|
||||
throw new IllegalArgumentException("SOA requires two names and five unsigned integers");
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
public long minimum() {
|
||||
return unsignedInteger(fields()[6]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] data() {
|
||||
String[] fields = fields();
|
||||
byte[] primary = encodeName(fields[0]);
|
||||
byte[] mailbox = encodeName(fields[1]);
|
||||
ByteBuffer result = ByteBuffer.allocate(primary.length + mailbox.length + 20);
|
||||
result.put(primary).put(mailbox);
|
||||
for (int i = 2; i < fields.length; i++) {
|
||||
result.putInt((int) unsignedInteger(fields[i]));
|
||||
}
|
||||
return result.array();
|
||||
}
|
||||
|
||||
private static long unsignedInteger(String value) {
|
||||
long number = Long.parseLong(value);
|
||||
if (number < 0 || number > 0xFFFFFFFFL) {
|
||||
throw new IllegalArgumentException("SOA integer must fit an unsigned 32-bit field");
|
||||
}
|
||||
return number;
|
||||
}
|
||||
}
|
||||
|
||||
record Srv(int port, String target) implements DnsRecord {
|
||||
public Srv {
|
||||
if (port < 0 || port > 65535) {
|
||||
|
||||
@@ -13,6 +13,11 @@ import java.util.Optional;
|
||||
*/
|
||||
public interface DnsRecordSource {
|
||||
|
||||
/** Stored public authority data only; this lookup must not perform recursive resolution. */
|
||||
default Optional<DnsAnswer> resolveAuthoritative(String name, int type) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a query name to IPv4 addresses and their TTL. An empty optional means this
|
||||
* source does not own the name. The name arrives without a trailing dot and in the case the client sent.
|
||||
|
||||
@@ -208,7 +208,7 @@ public class EmbeddedDnsServer {
|
||||
int questionOffset = buf.position(); // always 12 for a standard query
|
||||
String qname = readName(buf, data);
|
||||
short qtype = buf.getShort();
|
||||
buf.getShort(); // qclass
|
||||
short qclass = buf.getShort();
|
||||
int questionEnd = buf.position();
|
||||
|
||||
vertx.<QueryPlan>executeBlocking(() -> planQuery(qname, senderHost, myIp, qtype), false)
|
||||
@@ -223,7 +223,13 @@ public class EmbeddedDnsServer {
|
||||
return;
|
||||
}
|
||||
DnsAnswer records = plan.answer().orElseThrow();
|
||||
byte[] response = !records.isEmpty()
|
||||
if (records.isPublicAuthorityAnswer()
|
||||
&& (qdCount != 1 || qclass != 1 || (flags & 0x7840) != 0)) {
|
||||
records = DnsAnswer.refused();
|
||||
}
|
||||
byte[] response = records.isPublicAuthorityAnswer()
|
||||
? buildPublicResponse(data, txId, questionOffset, questionEnd, records)
|
||||
: !records.isEmpty()
|
||||
? buildAResponse(data, txId, questionOffset, questionEnd, records)
|
||||
: buildEmptyResponse(data, txId, questionOffset, questionEnd,
|
||||
records.nameExists() ? 0 : 3);
|
||||
@@ -261,6 +267,10 @@ public class EmbeddedDnsServer {
|
||||
}
|
||||
|
||||
private Optional<DnsAnswer> resolveRecordWithOwnership(String name, String myIp, int type) {
|
||||
Optional<DnsAnswer> stored = resolveFromRecordSources(name, type, true);
|
||||
if (stored.isPresent()) {
|
||||
return stored;
|
||||
}
|
||||
Optional<DnsAnswer> emulatorName = resolveEmulatorName(name, myIp, type);
|
||||
return emulatorName.isPresent() ? emulatorName : resolveFromRecordSources(name, type);
|
||||
}
|
||||
@@ -284,7 +294,8 @@ public class EmbeddedDnsServer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Names the emulator itself owns come first, whatever the rules say. Route 53 Resolver does
|
||||
* Stored public authority answers take precedence over broad emulator suffix spoofing.
|
||||
* Other 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.
|
||||
@@ -294,8 +305,9 @@ public class EmbeddedDnsServer {
|
||||
* hosted zone that matches the same name.
|
||||
*/
|
||||
QueryPlan planQuery(String name, String clientAddress, String myIp, int type) {
|
||||
Optional<DnsAnswer> stored = resolveFromRecordSources(name, type, true);
|
||||
Optional<DnsAnswer> emulatorName = resolveEmulatorName(name, myIp, type);
|
||||
if (emulatorName.isPresent()) {
|
||||
if (emulatorName.isPresent() && stored.isEmpty()) {
|
||||
return QueryPlan.answering(emulatorName.orElseThrow());
|
||||
}
|
||||
// A rule steers the name whatever the query asks about it, so this is deliberately not
|
||||
@@ -304,7 +316,8 @@ public class EmbeddedDnsServer {
|
||||
if (rule.isPresent() && rule.orElseThrow().forwards()) {
|
||||
return QueryPlan.forwardingTo(rule.orElseThrow().targets());
|
||||
}
|
||||
return resolveFromRecordSources(name, type).map(QueryPlan::answering).orElse(QueryPlan.UPSTREAM);
|
||||
return stored.or(() -> resolveFromRecordSources(name, type))
|
||||
.map(QueryPlan::answering).orElse(QueryPlan.UPSTREAM);
|
||||
}
|
||||
|
||||
private Optional<DnsAnswer> resolveEmulatorName(String name, String myIp, int type) {
|
||||
@@ -385,12 +398,17 @@ public class EmbeddedDnsServer {
|
||||
* through to the upstream resolvers, which is what happened before any source existed.
|
||||
*/
|
||||
private Optional<DnsAnswer> resolveFromRecordSources(String name, int type) {
|
||||
return resolveFromRecordSources(name, type, false);
|
||||
}
|
||||
|
||||
private Optional<DnsAnswer> resolveFromRecordSources(String name, int type, boolean authoritative) {
|
||||
if (recordSources == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
for (DnsRecordSource source : recordSources) {
|
||||
try {
|
||||
Optional<DnsAnswer> answer = source.resolve(name, type);
|
||||
Optional<DnsAnswer> answer = authoritative ? source.resolveAuthoritative(name, type)
|
||||
: source.resolve(name, type);
|
||||
if (answer != null && answer.isPresent()) {
|
||||
return answer;
|
||||
}
|
||||
@@ -522,7 +540,8 @@ public class EmbeddedDnsServer {
|
||||
|
||||
// header
|
||||
resp.putShort(txId);
|
||||
resp.putShort((short) (0x8180 | (truncated ? 0x0200 : 0))); // QR=1, RD=1, RCODE=0
|
||||
int flags = answer.authority() == null ? 0x8180 : 0x8400 | (query[2] & 1) << 8;
|
||||
resp.putShort((short) (flags | (truncated ? 0x0200 : 0)));
|
||||
resp.putShort((short) 1); // qdcount
|
||||
resp.putShort((short) answerCount); // ancount
|
||||
resp.putShort((short) 0); // nscount
|
||||
@@ -544,6 +563,34 @@ public class EmbeddedDnsServer {
|
||||
return resp.array();
|
||||
}
|
||||
|
||||
byte[] buildPublicResponse(byte[] query, short txId, int questionOffset, int questionEnd, DnsAnswer answer) {
|
||||
if (!answer.isEmpty()) {
|
||||
return buildAResponse(query, txId, questionOffset, questionEnd, answer);
|
||||
}
|
||||
int flags = 0x8000 | (query[2] & 0x79) << 8 | answer.responseCode();
|
||||
DnsAnswer.Authority authority = answer.authority();
|
||||
if (authority == null) {
|
||||
return buildAnswerlessResponse(query, txId, questionOffset, questionEnd, (short) flags);
|
||||
}
|
||||
byte[] owner = DnsRecord.encodeName(authority.name());
|
||||
byte[] soa = authority.soa().data();
|
||||
UdpPayload payload = udpPayload(query, questionEnd);
|
||||
int questionLength = questionEnd - questionOffset;
|
||||
int length = 12 + questionLength + (payload.edns() ? 11 : 0);
|
||||
boolean fits = length + owner.length + 10 + soa.length <= payload.size();
|
||||
ByteBuffer response = ByteBuffer.allocate(length + (fits ? owner.length + 10 + soa.length : 0));
|
||||
response.putShort(txId).putShort((short) (flags | 0x0400 | (fits ? 0 : 0x0200)));
|
||||
response.putShort((short) 1).putShort((short) 0).putShort((short) (fits ? 1 : 0))
|
||||
.putShort((short) (payload.edns() ? 1 : 0));
|
||||
response.put(query, questionOffset, questionLength);
|
||||
if (fits) {
|
||||
response.put(owner).putShort((short) 6).putShort((short) 1).putInt(authority.ttlSeconds())
|
||||
.putShort((short) soa.length).put(soa);
|
||||
}
|
||||
appendOpt(response, payload);
|
||||
return response.array();
|
||||
}
|
||||
|
||||
private record UdpPayload(int size, boolean edns) {}
|
||||
|
||||
private static UdpPayload udpPayload(byte[] query, int questionEnd) {
|
||||
|
||||
+93
-1
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.config.LocalStackParity;
|
||||
import io.github.hectorvent.floci.core.common.DomainNames;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsAnswer;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsLookupHelper;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsRecord;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsRecordSource;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import io.github.hectorvent.floci.services.route53.model.AliasTarget;
|
||||
@@ -15,6 +16,7 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -25,7 +27,7 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Resolves Route 53 private hosted zone records for the embedded DNS server.
|
||||
* Resolves private IPv4 records and public TXT/NS/SOA records for the embedded DNS server.
|
||||
*
|
||||
* <p>Discovered via CDI as a {@link DnsRecordSource} implementation, keeping
|
||||
* the embedded DNS server decoupled from Route 53 service classes.
|
||||
@@ -66,6 +68,88 @@ public class Route53DnsRecordSource implements DnsRecordSource {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DnsAnswer> resolveAuthoritative(String name, int type) {
|
||||
if (name == null || name.isBlank() || (type != 2 && type != 6 && type != 16)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (route53Service.isCoveredByPrivateZone(name)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
List<Route53Service.PublicDnsZone> zones = route53Service.publicDnsZones(name);
|
||||
if (zones.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (zones.size() != 1) {
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
Route53Service.PublicDnsZone zone = zones.getFirst();
|
||||
String normalized = Route53Service.normalizeName(name).toLowerCase(Locale.ROOT);
|
||||
String apex = Route53Service.normalizeName(zone.name()).toLowerCase(Locale.ROOT);
|
||||
// A delegation ends this zone's authority. Serving the parent's negative answer below
|
||||
// that cut, or flattening an unsupported CNAME/routing policy, would hide real records.
|
||||
boolean delegated = zone.records().stream().anyMatch(record -> "NS".equals(record.getType())
|
||||
&& !Route53Service.normalizeName(record.getName()).equalsIgnoreCase(apex)
|
||||
&& DomainNames.isWithin(normalized, Route53Service.normalizeName(record.getName())));
|
||||
if (delegated || zone.matchingRecords().stream().anyMatch(record -> "CNAME".equals(record.getType()))) {
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
ResourceRecordSet soaSet = zone.records().stream()
|
||||
.filter(record -> "SOA".equals(record.getType())
|
||||
&& Route53Service.normalizeName(record.getName()).equalsIgnoreCase(apex))
|
||||
.findFirst().orElse(null);
|
||||
if (soaSet == null || soaSet.getRecords() == null || soaSet.getRecords().size() != 1) {
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
List<DnsRecord> answers = new ArrayList<>();
|
||||
int ttl = Integer.MAX_VALUE;
|
||||
try {
|
||||
for (ResourceRecordSet record : zone.matchingRecords()) {
|
||||
if (!record.getType().equals(switch (type) {
|
||||
case 2 -> "NS";
|
||||
case 6 -> "SOA";
|
||||
default -> "TXT";
|
||||
})) {
|
||||
continue;
|
||||
}
|
||||
if (record.getSetIdentifier() != null || record.getAliasTarget() != null
|
||||
|| record.getWeight() != null || record.getRegion() != null
|
||||
|| record.getFailover() != null || record.getHealthCheckId() != null
|
||||
|| record.getRecords() == null) {
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
for (ResourceRecord value : record.getRecords()) {
|
||||
if (value == null || value.getValue() == null) {
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
DnsRecord answer = switch (type) {
|
||||
case 2 -> new DnsRecord.Ns(value.getValue());
|
||||
case 6 -> new DnsRecord.Soa(value.getValue());
|
||||
default -> new DnsRecord.Txt(value.getValue());
|
||||
};
|
||||
answer.data();
|
||||
answers.add(answer);
|
||||
}
|
||||
ttl = Math.min(ttl, publicTtlOf(record));
|
||||
}
|
||||
boolean exists = !zone.matchingRecords().isEmpty() || zone.records().stream()
|
||||
.anyMatch(record -> Route53Service.normalizeName(record.getName()).toLowerCase(Locale.ROOT)
|
||||
.endsWith("." + normalized));
|
||||
DnsAnswer answer = answers.isEmpty() ? (exists ? DnsAnswer.noData() : DnsAnswer.nxDomain())
|
||||
: DnsAnswer.typedRecords(answers, ttl);
|
||||
ResourceRecord soaValue = soaSet.getRecords().getFirst();
|
||||
if (soaValue == null || soaValue.getValue() == null) {
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
DnsRecord.Soa soa = new DnsRecord.Soa(soaValue.getValue());
|
||||
soa.data();
|
||||
return Optional.of(answer.withAuthority(apex, soa, publicTtlOf(soaSet)));
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.debugv("Cannot encode public DNS records for {0}: {1}", name, e.getMessage());
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DnsAnswer> resolveIpv4(String name) {
|
||||
if (name == null || name.isBlank()) {
|
||||
@@ -175,6 +259,14 @@ public class Route53DnsRecordSource implements DnsRecordSource {
|
||||
return dnsLookupHelper.resolveIpv4(name);
|
||||
}
|
||||
|
||||
private static int publicTtlOf(ResourceRecordSet record) {
|
||||
Long ttl = record.getTtl();
|
||||
if (ttl == null || ttl < 0 || ttl > Integer.MAX_VALUE) {
|
||||
throw new IllegalArgumentException("Invalid public record TTL");
|
||||
}
|
||||
return ttl.intValue();
|
||||
}
|
||||
|
||||
private static int ttlOf(ResourceRecordSet rrs) {
|
||||
Long ttl = rrs.getTtl();
|
||||
if (ttl == null || ttl < 0) {
|
||||
|
||||
@@ -59,6 +59,7 @@ public class Route53Service implements Resettable {
|
||||
private final Map<String, Long> hostedZoneMutationBusyUntilNanos = new ConcurrentHashMap<>();
|
||||
private final Map<String, Long> authorizationMutationBusyUntilNanos = new ConcurrentHashMap<>();
|
||||
private final Map<String, Integer> privateZoneNameCounts = new ConcurrentHashMap<>();
|
||||
private final Map<String, Integer> publicZoneNameCounts = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
public Route53Service(StorageFactory factory, EmulatorConfig config, RegionResolver regionResolver,
|
||||
@@ -88,9 +89,8 @@ public class Route53Service implements Resettable {
|
||||
this.ec2Service = ec2Service;
|
||||
this.vpcAssociationControlPlaneDelayMs = Math.max(0, r53.vpcAssociationControlPlaneDelayMs());
|
||||
for (OwnedZone owned : allHostedZonesAcrossAccounts()) {
|
||||
if (owned.zone().isPrivateZone()) {
|
||||
privateZoneNameCounts.merge(normalizeName(owned.zone().getName()).toLowerCase(), 1, Integer::sum);
|
||||
}
|
||||
zoneNameCounts(owned.zone().isPrivateZone()).merge(
|
||||
normalizeName(owned.zone().getName()).toLowerCase(), 1, Integer::sum);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,8 +114,8 @@ public class Route53Service implements Resettable {
|
||||
zone.setOwnerAccountId(callerAccountId);
|
||||
if (vpcAssociation != null) {
|
||||
vpcAssociation.setOwnerAccountId(callerAccountId);
|
||||
privateZoneNameCounts.merge(normalizedName.toLowerCase(), 1, Integer::sum);
|
||||
}
|
||||
zoneNameCounts(zone.isPrivateZone()).merge(normalizedName.toLowerCase(), 1, Integer::sum);
|
||||
zoneStore.put(id, zone);
|
||||
recordStore.put(id, buildDefaultRecords(normalizedName));
|
||||
ChangeInfo change = newChange(null);
|
||||
@@ -144,10 +144,8 @@ public class Route53Service implements Resettable {
|
||||
recordStore.delete(id);
|
||||
tagStore.delete("hostedzone/" + id);
|
||||
vpcAuthorizationStore.delete(id);
|
||||
if (zone.isPrivateZone()) {
|
||||
privateZoneNameCounts.computeIfPresent(normalizeName(zone.getName()).toLowerCase(),
|
||||
(k, v) -> v > 1 ? v - 1 : null);
|
||||
}
|
||||
zoneNameCounts(zone.isPrivateZone()).computeIfPresent(normalizeName(zone.getName()).toLowerCase(),
|
||||
(k, v) -> v > 1 ? v - 1 : null);
|
||||
return newChange(null);
|
||||
}
|
||||
|
||||
@@ -623,12 +621,18 @@ public class Route53Service implements Resettable {
|
||||
if (qname == null || qname.isBlank()) {
|
||||
return List.of();
|
||||
}
|
||||
String normalizedQname = normalizeName(qname).toLowerCase();
|
||||
return findRecordsForName(normalizeName(qname).toLowerCase(),
|
||||
mostSpecificZones(normalizeName(qname).toLowerCase(), true).stream()
|
||||
.map(owned -> getRecordsForZoneAcrossAccounts(owned.accountId(), owned.zone().getId()))
|
||||
.toList());
|
||||
}
|
||||
|
||||
private static List<ResourceRecordSet> findRecordsForName(String normalizedQname,
|
||||
List<List<ResourceRecordSet>> zoneRecords) {
|
||||
List<ResourceRecordSet> nameMatches = new ArrayList<>();
|
||||
List<ResourceRecordSet> wildcardMatches = new ArrayList<>();
|
||||
|
||||
for (OwnedZone owned : mostSpecificPrivateZones(normalizedQname)) {
|
||||
List<ResourceRecordSet> records = getRecordsForZoneAcrossAccounts(owned.accountId(), owned.zone().getId());
|
||||
for (List<ResourceRecordSet> records : zoneRecords) {
|
||||
List<ResourceRecordSet> zoneWildcards = new ArrayList<>();
|
||||
for (ResourceRecordSet rrs : records) {
|
||||
String rName = normalizeName(rrs.getName()).toLowerCase();
|
||||
@@ -684,7 +688,7 @@ public class Route53Service implements Resettable {
|
||||
}
|
||||
String normalizedQname = normalizeName(qname).toLowerCase();
|
||||
String suffix = "." + normalizedQname;
|
||||
for (OwnedZone owned : mostSpecificPrivateZones(normalizedQname)) {
|
||||
for (OwnedZone owned : mostSpecificZones(normalizedQname, true)) {
|
||||
for (ResourceRecordSet rrs : getRecordsForZoneAcrossAccounts(owned.accountId(), owned.zone().getId())) {
|
||||
if (normalizeName(rrs.getName()).toLowerCase().endsWith(suffix)) {
|
||||
return true;
|
||||
@@ -694,17 +698,35 @@ public class Route53Service implements Resettable {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** The private zones with the longest name containing {@code normalizedQname}, so a parent zone never answers for a child. */
|
||||
private List<OwnedZone> mostSpecificPrivateZones(String normalizedQname) {
|
||||
if (privateZoneNameCounts.isEmpty()
|
||||
|| privateZoneNameCounts.keySet().stream().noneMatch(z -> DomainNames.isWithin(normalizedQname, z))) {
|
||||
record PublicDnsZone(String name, List<ResourceRecordSet> matchingRecords, List<ResourceRecordSet> records) {}
|
||||
|
||||
List<PublicDnsZone> publicDnsZones(String qname) {
|
||||
String normalized = normalizeName(qname).toLowerCase();
|
||||
return mostSpecificZones(normalized, false).stream()
|
||||
.map(owned -> {
|
||||
List<ResourceRecordSet> records = getRecordsForZoneAcrossAccounts(
|
||||
owned.accountId(), owned.zone().getId());
|
||||
return new PublicDnsZone(owned.zone().getName(),
|
||||
findRecordsForName(normalized, List.of(records)), records);
|
||||
})
|
||||
.toList();
|
||||
}
|
||||
|
||||
private Map<String, Integer> zoneNameCounts(boolean privateZone) {
|
||||
return privateZone ? privateZoneNameCounts : publicZoneNameCounts;
|
||||
}
|
||||
|
||||
/** The longest containing zones, so a parent never answers for a more specific child. */
|
||||
private List<OwnedZone> mostSpecificZones(String normalizedQname, boolean privateZone) {
|
||||
Map<String, Integer> names = zoneNameCounts(privateZone);
|
||||
if (names.isEmpty() || names.keySet().stream().noneMatch(z -> DomainNames.isWithin(normalizedQname, z))) {
|
||||
return List.of();
|
||||
}
|
||||
List<OwnedZone> matchingZones = new ArrayList<>();
|
||||
int maxZoneLength = -1;
|
||||
for (OwnedZone owned : allHostedZonesAcrossAccounts()) {
|
||||
HostedZone zone = owned.zone();
|
||||
if (!zone.isPrivateZone()) {
|
||||
if (zone.isPrivateZone() != privateZone) {
|
||||
continue;
|
||||
}
|
||||
String zoneName = normalizeName(zone.getName()).toLowerCase();
|
||||
@@ -1026,5 +1048,6 @@ public class Route53Service implements Resettable {
|
||||
hostedZoneMutationBusyUntilNanos.clear();
|
||||
authorizationMutationBusyUntilNanos.clear();
|
||||
privateZoneNameCounts.clear();
|
||||
publicZoneNameCounts.clear();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user