mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(cloudmap): answer typed DNS records and honor routing policy (#4534)
* fix(cloudmap): answer typed DNS records and honor routing policy * fix(cloudmap): validate targets and bound DNS UDP responses
This commit is contained in:
1 parent
c9427b5832
commit
c7492ce0f4
11 files changed
+642
-61
No files matched your search
@@ -3,14 +3,14 @@ package io.github.hectorvent.floci.core.common.dns;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* What a {@link DnsRecordSource} answers for a name inside a zone it owns: the A records with the
|
||||
* TTL the zone publishes for them, an existing name without A records, or an absent name. A name
|
||||
* What a {@link DnsRecordSource} answers for a name inside a zone it owns: typed records with the
|
||||
* TTL the zone publishes for them, an existing name without those records, or an absent name. A name
|
||||
* the source does not own is an empty optional, not an answer.
|
||||
*
|
||||
* <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<String> addresses, int ttlSeconds, boolean nameExists) {
|
||||
public record DnsAnswer(List<DnsRecord> records, int ttlSeconds, boolean nameExists) {
|
||||
|
||||
/** What the DNS server publishes for a name whose zone declares no TTL of its own. */
|
||||
public static final int DEFAULT_TTL_SECONDS = 60;
|
||||
@@ -24,15 +24,26 @@ public record DnsAnswer(List<String> addresses, int ttlSeconds, boolean nameExis
|
||||
if (ttlSeconds < 0) {
|
||||
throw new IllegalArgumentException("DNS TTL must not be negative: " + ttlSeconds);
|
||||
}
|
||||
addresses = List.copyOf(addresses);
|
||||
records = List.copyOf(records);
|
||||
}
|
||||
|
||||
/** A records for an existing name, all published with the same TTL. */
|
||||
public static DnsAnswer records(List<String> addresses, int ttlSeconds) {
|
||||
return new DnsAnswer(addresses, ttlSeconds, true);
|
||||
return typedRecords(addresses.stream().map(address -> (DnsRecord) new DnsRecord.Address(1, address)).toList(),
|
||||
ttlSeconds);
|
||||
}
|
||||
|
||||
/** An existing name without A records: NOERROR with no answers. */
|
||||
public static DnsAnswer typedRecords(List<DnsRecord> records, int ttlSeconds) {
|
||||
return new DnsAnswer(records, ttlSeconds, true);
|
||||
}
|
||||
|
||||
/** 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)
|
||||
.map(record -> ((DnsRecord.Address) record).value()).toList();
|
||||
}
|
||||
|
||||
/** An existing name without records of the requested type: NOERROR with no answers. */
|
||||
public static DnsAnswer noData() {
|
||||
return NO_DATA;
|
||||
}
|
||||
@@ -43,6 +54,6 @@ public record DnsAnswer(List<String> addresses, int ttlSeconds, boolean nameExis
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return addresses.isEmpty();
|
||||
return records.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package io.github.hectorvent.floci.core.common.dns;
|
||||
|
||||
import io.netty.util.NetUtil;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/** A DNS resource record's wire type and uncompressed rdata. */
|
||||
public sealed interface DnsRecord {
|
||||
int type();
|
||||
byte[] data();
|
||||
|
||||
record Address(int type, String value) implements DnsRecord {
|
||||
public Address {
|
||||
if (!(type == 1 && NetUtil.isValidIpV4Address(value))
|
||||
&& !(type == 28 && NetUtil.isValidIpV6Address(value))) {
|
||||
throw new IllegalArgumentException("Invalid DNS address: " + value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] data() {
|
||||
return NetUtil.createByteArrayFromIpAddressString(value);
|
||||
}
|
||||
}
|
||||
|
||||
record Cname(String target) implements DnsRecord {
|
||||
@Override
|
||||
public int type() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] data() {
|
||||
return encodeName(target);
|
||||
}
|
||||
}
|
||||
|
||||
record Srv(int port, String target) implements DnsRecord {
|
||||
public Srv {
|
||||
if (port < 0 || port > 65535) {
|
||||
throw new IllegalArgumentException("Invalid SRV port: " + port);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int type() {
|
||||
return 33;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] data() {
|
||||
byte[] name = encodeName(target);
|
||||
return ByteBuffer.allocate(6 + name.length).putShort((short) 1).putShort((short) 1)
|
||||
.putShort((short) port).put(name).array();
|
||||
}
|
||||
}
|
||||
|
||||
static byte[] encodeName(String name) {
|
||||
String normalized = name.endsWith(".") ? name.substring(0, name.length() - 1) : name;
|
||||
if (normalized.length() > 253 || normalized.isEmpty()) {
|
||||
throw new IllegalArgumentException("Invalid DNS name: " + name);
|
||||
}
|
||||
ByteBuffer buffer = ByteBuffer.allocate(normalized.length() + 2);
|
||||
for (String label : normalized.split("\\.", -1)) {
|
||||
if (label.isEmpty() || label.length() > 63 || !StandardCharsets.US_ASCII.newEncoder().canEncode(label)) {
|
||||
throw new IllegalArgumentException("Invalid DNS label: " + label);
|
||||
}
|
||||
buffer.put((byte) label.length()).put(label.getBytes(StandardCharsets.US_ASCII));
|
||||
}
|
||||
return buffer.put((byte) 0).array();
|
||||
}
|
||||
}
|
||||
@@ -18,4 +18,10 @@ public interface DnsRecordSource {
|
||||
* source does not own the name. The name arrives without a trailing dot and in the case the client sent.
|
||||
*/
|
||||
Optional<DnsAnswer> resolveIpv4(String name);
|
||||
|
||||
/** A source that only implements IPv4 still preserves name ownership for other query types. */
|
||||
default Optional<DnsAnswer> resolve(String name, int type) {
|
||||
return resolveIpv4(name).map(answer -> type == 1 ? answer
|
||||
: answer.nameExists() ? DnsAnswer.noData() : DnsAnswer.nxDomain());
|
||||
}
|
||||
}
|
||||
@@ -149,14 +149,14 @@ public class EmbeddedDnsServer {
|
||||
buf.getShort(); // qclass
|
||||
int questionEnd = buf.position();
|
||||
|
||||
vertx.<Optional<DnsAnswer>>executeBlocking(() -> resolveARecordWithOwnership(qname, myIp), false)
|
||||
vertx.<Optional<DnsAnswer>>executeBlocking(() -> resolveRecordWithOwnership(qname, myIp, qtype), false)
|
||||
.onSuccess(answer -> {
|
||||
if (answer.isEmpty()) {
|
||||
forwardAsync(vertx, socket, data, senderHost, senderPort);
|
||||
return;
|
||||
}
|
||||
DnsAnswer records = answer.orElseThrow();
|
||||
byte[] response = qtype == 1 && !records.isEmpty()
|
||||
byte[] response = !records.isEmpty()
|
||||
? buildAResponse(data, txId, questionOffset, questionEnd, records)
|
||||
: buildEmptyResponse(data, txId, questionOffset, questionEnd,
|
||||
records.nameExists() ? 0 : 3);
|
||||
@@ -190,13 +190,19 @@ public class EmbeddedDnsServer {
|
||||
}
|
||||
|
||||
Optional<DnsAnswer> resolveARecordWithOwnership(String name, String myIp) {
|
||||
return resolveRecordWithOwnership(name, myIp, 1);
|
||||
}
|
||||
|
||||
private Optional<DnsAnswer> resolveRecordWithOwnership(String name, String myIp, int type) {
|
||||
if (matchesSuffix(name)) {
|
||||
return Optional.of(DnsAnswer.records(List.of(myIp), DnsAnswer.DEFAULT_TTL_SECONDS));
|
||||
return Optional.of(type == 1 ? DnsAnswer.records(List.of(myIp), DnsAnswer.DEFAULT_TTL_SECONDS)
|
||||
: DnsAnswer.noData());
|
||||
}
|
||||
Optional<String> ec2PrivateDnsName = resolveEc2PrivateDnsName(name);
|
||||
return ec2PrivateDnsName
|
||||
.map(address -> DnsAnswer.records(List.of(address), DnsAnswer.DEFAULT_TTL_SECONDS))
|
||||
.map(Optional::of).orElseGet(() -> resolveFromRecordSources(name));
|
||||
.map(address -> type == 1 ? DnsAnswer.records(List.of(address), DnsAnswer.DEFAULT_TTL_SECONDS)
|
||||
: DnsAnswer.noData())
|
||||
.map(Optional::of).orElseGet(() -> resolveFromRecordSources(name, type));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,13 +210,13 @@ public class EmbeddedDnsServer {
|
||||
* today. A source that throws must not take the DNS server down with it: the query falls
|
||||
* through to the upstream resolvers, which is what happened before any source existed.
|
||||
*/
|
||||
private Optional<DnsAnswer> resolveFromRecordSources(String name) {
|
||||
private Optional<DnsAnswer> resolveFromRecordSources(String name, int type) {
|
||||
if (recordSources == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
for (DnsRecordSource source : recordSources) {
|
||||
try {
|
||||
Optional<DnsAnswer> answer = source.resolveIpv4(name);
|
||||
Optional<DnsAnswer> answer = source.resolve(name, type);
|
||||
if (answer != null && answer.isPresent()) {
|
||||
return answer;
|
||||
}
|
||||
@@ -223,14 +229,16 @@ public class EmbeddedDnsServer {
|
||||
}
|
||||
|
||||
byte[] buildEmptyResponse(byte[] query, short txId, int questionOffset, int questionEnd, int responseCode) {
|
||||
ByteBuffer response = ByteBuffer.allocate(12 + questionEnd - questionOffset);
|
||||
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((short) 1);
|
||||
response.putShort((short) 0);
|
||||
response.putShort((short) 0);
|
||||
response.putShort((short) 0);
|
||||
response.putShort((short) (payload.edns() ? 1 : 0));
|
||||
response.put(query, questionOffset, questionEnd - questionOffset);
|
||||
appendOpt(response, payload);
|
||||
return response.array();
|
||||
}
|
||||
|
||||
@@ -304,37 +312,82 @@ public class EmbeddedDnsServer {
|
||||
}
|
||||
|
||||
byte[] buildAResponse(byte[] query, short txId, int questionOffset, int questionEnd, DnsAnswer answer) {
|
||||
List<String> ips = answer.addresses();
|
||||
List<DnsRecord> records = answer.records();
|
||||
int questionLength = questionEnd - questionOffset;
|
||||
// header(12) + question + per answer(name-ptr(2) + type(2) + class(2) + ttl(4) + rdlen(2) + rdata(4))
|
||||
ByteBuffer resp = ByteBuffer.allocate(12 + questionLength + 16 * ips.size());
|
||||
List<byte[]> data = records.stream().map(DnsRecord::data).toList();
|
||||
UdpPayload payload = udpPayload(query, questionEnd);
|
||||
int responseLength = 12 + questionLength + (payload.edns() ? 11 : 0);
|
||||
int answerCount = 0;
|
||||
for (byte[] recordData : data) {
|
||||
if (responseLength + 12 + recordData.length > payload.size()) {
|
||||
break;
|
||||
}
|
||||
responseLength += 12 + recordData.length;
|
||||
answerCount++;
|
||||
}
|
||||
boolean truncated = answerCount < records.size();
|
||||
ByteBuffer resp = ByteBuffer.allocate(responseLength);
|
||||
|
||||
// header
|
||||
resp.putShort(txId);
|
||||
resp.putShort((short) 0x8180); // QR=1, AA=1, RD=1, RCODE=0
|
||||
resp.putShort((short) (0x8180 | (truncated ? 0x0200 : 0))); // QR=1, RD=1, RCODE=0
|
||||
resp.putShort((short) 1); // qdcount
|
||||
resp.putShort((short) ips.size()); // ancount
|
||||
resp.putShort((short) answerCount); // ancount
|
||||
resp.putShort((short) 0); // nscount
|
||||
resp.putShort((short) 0); // arcount
|
||||
resp.putShort((short) (payload.edns() ? 1 : 0)); // arcount
|
||||
|
||||
// question (copied verbatim from query)
|
||||
resp.put(query, questionOffset, questionLength);
|
||||
|
||||
// answers. A name with several registered addresses gets one A record each, which is
|
||||
// what a Cloud Map service backed by more than one instance resolves to on AWS.
|
||||
for (String ip : ips) {
|
||||
// Each answer carries its own type and rdata; a CNAME can answer an address query.
|
||||
for (int i = 0; i < answerCount; i++) {
|
||||
resp.putShort((short) 0xC00C); // name pointer to offset 12 (start of question name)
|
||||
resp.putShort((short) 1); // type A
|
||||
resp.putShort((short) records.get(i).type());
|
||||
resp.putShort((short) 1); // class IN
|
||||
resp.putInt(answer.ttlSeconds());
|
||||
resp.putShort((short) 4); // rdlength
|
||||
resp.putShort((short) data.get(i).length);
|
||||
resp.put(data.get(i));
|
||||
}
|
||||
appendOpt(resp, payload);
|
||||
return resp.array();
|
||||
}
|
||||
|
||||
for (String octet : ip.split("\\.")) {
|
||||
resp.put((byte) Integer.parseInt(octet));
|
||||
private record UdpPayload(int size, boolean edns) {}
|
||||
|
||||
private static UdpPayload udpPayload(byte[] query, int questionEnd) {
|
||||
UdpPayload legacy = new UdpPayload(512, false);
|
||||
ByteBuffer packet = ByteBuffer.wrap(query);
|
||||
if (query.length < 12 || packet.getShort(4) != 1
|
||||
|| packet.getShort(6) != 0 || packet.getShort(8) != 0) {
|
||||
return legacy;
|
||||
}
|
||||
int additionalCount = Short.toUnsignedInt(packet.getShort(10));
|
||||
packet.position(questionEnd);
|
||||
for (int i = 0; i < additionalCount; i++) {
|
||||
readName(packet, query);
|
||||
if (packet.remaining() < 10) {
|
||||
return legacy;
|
||||
}
|
||||
int type = Short.toUnsignedInt(packet.getShort());
|
||||
int size = Short.toUnsignedInt(packet.getShort());
|
||||
int ttl = packet.getInt();
|
||||
int dataLength = Short.toUnsignedInt(packet.getShort());
|
||||
if (packet.remaining() < dataLength) {
|
||||
return legacy;
|
||||
}
|
||||
packet.position(packet.position() + dataLength);
|
||||
if (type == 41 && ((ttl >>> 16) & 0xFF) == 0) {
|
||||
return new UdpPayload(Math.max(512, Math.min(size, MAX_DNS_UDP_RESPONSE)), true);
|
||||
}
|
||||
}
|
||||
return legacy;
|
||||
}
|
||||
|
||||
return resp.array();
|
||||
private static void appendOpt(ByteBuffer response, UdpPayload payload) {
|
||||
if (payload.edns()) {
|
||||
response.put((byte) 0).putShort((short) 41).putShort((short) payload.size())
|
||||
.putInt(0).putShort((short) 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void forwardAsync(Vertx vertx, DatagramSocket socket, byte[] query,
|
||||
|
||||
+5
@@ -30,4 +30,9 @@ public class CloudMapDnsRecordSource implements DnsRecordSource {
|
||||
public Optional<DnsAnswer> resolveIpv4(String name) {
|
||||
return cloudMapService.resolveDnsNameIfOwned(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DnsAnswer> resolve(String name, int type) {
|
||||
return cloudMapService.resolveDnsNameIfOwned(name, type);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsAnswer;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsRecord;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
@@ -16,6 +17,7 @@ import io.github.hectorvent.floci.services.cloudmap.model.Instance;
|
||||
import io.github.hectorvent.floci.services.cloudmap.model.Namespace;
|
||||
import io.github.hectorvent.floci.services.cloudmap.model.Operation;
|
||||
import io.github.hectorvent.floci.services.cloudmap.model.Service;
|
||||
import io.netty.util.NetUtil;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -34,6 +36,7 @@ import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -272,6 +275,17 @@ public class CloudMapService {
|
||||
if (attributes == null || attributes.isEmpty()) {
|
||||
throw new AwsException("InvalidInput", "Attributes are required.", 400);
|
||||
}
|
||||
if (dnsRecordTtl(service, "SRV") >= 0 && service.getNamespaceId() != null) {
|
||||
Namespace namespace = requireNamespace(service.getNamespaceId());
|
||||
if ("DNS_PRIVATE".equals(namespace.getType()) || "DNS_PUBLIC".equals(namespace.getType())) {
|
||||
try {
|
||||
DnsRecord.encodeName(instanceId + "." + service.getName() + "." + namespace.getName());
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"The SRV target must have ASCII labels of 1 to 63 bytes and at most 253 characters.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId(instanceId);
|
||||
instance.setServiceId(serviceId);
|
||||
@@ -393,6 +407,10 @@ public class CloudMapService {
|
||||
|
||||
/** Keeps zone ownership distinct from the A records and their TTL. */
|
||||
public Optional<DnsAnswer> resolveDnsNameIfOwned(String queryName) {
|
||||
return resolveDnsNameIfOwned(queryName, 1);
|
||||
}
|
||||
|
||||
public Optional<DnsAnswer> resolveDnsNameIfOwned(String queryName, int type) {
|
||||
if (queryName == null || queryName.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@@ -422,11 +440,11 @@ public class CloudMapService {
|
||||
List<Service> services = scan(serviceStore).stream()
|
||||
.filter(service -> namespace.getId().equals(service.getNamespaceId()))
|
||||
.toList();
|
||||
Optional<DnsAnswer> byService = resolveServiceName(services, label);
|
||||
Optional<DnsAnswer> byService = resolveServiceName(services, label, namespace.getName(), type);
|
||||
if (byService.filter(answer -> !answer.isEmpty()).isPresent()) {
|
||||
return byService;
|
||||
}
|
||||
Optional<DnsAnswer> byInstance = resolveInstanceHostname(services, label);
|
||||
Optional<DnsAnswer> byInstance = resolveInstanceHostname(services, label, type);
|
||||
if (byInstance.filter(answer -> !answer.isEmpty()).isPresent()) {
|
||||
return byInstance;
|
||||
}
|
||||
@@ -437,40 +455,56 @@ public class CloudMapService {
|
||||
}
|
||||
|
||||
/**
|
||||
* The A records at {@code <service>.<namespace>}. A service without an A record still owns its
|
||||
* name while it has instances, since its SRV record lives there, so it answers no-data.
|
||||
* Requested records at {@code <service>.<namespace>}, preserving ownership for other types.
|
||||
*/
|
||||
private Optional<DnsAnswer> resolveServiceName(List<Service> services, String label) {
|
||||
private Optional<DnsAnswer> resolveServiceName(List<Service> services, String label, String namespace, int type) {
|
||||
boolean exists = false;
|
||||
for (Service service : services) {
|
||||
if (!label.equalsIgnoreCase(service.getName())) {
|
||||
continue;
|
||||
}
|
||||
int ttl = dnsRecordTtl(service, "A");
|
||||
List<Instance> instances = scanInstances(service.getId());
|
||||
exists |= !instances.isEmpty();
|
||||
int recordType = type;
|
||||
String recordName = switch (type) {
|
||||
case 1 -> "A";
|
||||
case 5 -> "CNAME";
|
||||
case 28 -> "AAAA";
|
||||
case 33 -> "SRV";
|
||||
default -> "";
|
||||
};
|
||||
int ttl = dnsRecordTtl(service, recordName);
|
||||
if (ttl < 0 && dnsRecordTtl(service, "CNAME") >= 0) {
|
||||
recordType = 5;
|
||||
ttl = dnsRecordTtl(service, "CNAME");
|
||||
}
|
||||
if (ttl < 0) {
|
||||
exists |= !scanInstances(service.getId()).isEmpty();
|
||||
continue;
|
||||
}
|
||||
List<String> addresses = new ArrayList<>();
|
||||
for (Instance instance : applyHealthFilter(scanInstances(service.getId()), "HEALTHY_OR_ELSE_ALL")) {
|
||||
String ipv4 = instance.getAttributes().get("AWS_INSTANCE_IPV4");
|
||||
if (isIpv4(ipv4)) {
|
||||
addresses.add(ipv4);
|
||||
List<DnsRecord> records = new ArrayList<>();
|
||||
for (Instance instance : applyHealthFilter(instances, "HEALTHY_OR_ELSE_ALL")) {
|
||||
DnsRecord record = instanceDnsRecord(instance, service, namespace, recordType);
|
||||
if (record != null) {
|
||||
records.add(record);
|
||||
}
|
||||
}
|
||||
if (!addresses.isEmpty()) {
|
||||
return Optional.of(DnsAnswer.records(addresses.size() > MAX_DNS_ANSWERS
|
||||
? addresses.subList(0, MAX_DNS_ANSWERS) : addresses, ttl));
|
||||
if (!records.isEmpty()) {
|
||||
if (recordType == 5 || "WEIGHTED".equals(dnsConfigNode(service).path("RoutingPolicy").asText())) {
|
||||
return Optional.of(DnsAnswer.typedRecords(
|
||||
List.of(records.get(ThreadLocalRandom.current().nextInt(records.size()))), ttl));
|
||||
}
|
||||
return Optional.of(DnsAnswer.typedRecords(records.size() > MAX_DNS_ANSWERS
|
||||
? records.subList(0, MAX_DNS_ANSWERS) : records, ttl));
|
||||
}
|
||||
}
|
||||
return exists ? Optional.of(DnsAnswer.noData()) : Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The A record Cloud Map creates at an SRV record's target, {@code <InstanceId>.<service>.<namespace>},
|
||||
* The address records at an SRV record's target, {@code <InstanceId>.<service>.<namespace>},
|
||||
* published with the service's SRV TTL.
|
||||
*/
|
||||
private Optional<DnsAnswer> resolveInstanceHostname(List<Service> services, String label) {
|
||||
private Optional<DnsAnswer> resolveInstanceHostname(List<Service> services, String label, int type) {
|
||||
boolean exists = false;
|
||||
for (Service service : services) {
|
||||
String serviceSuffix = "." + service.getName().toLowerCase(Locale.ROOT);
|
||||
@@ -487,15 +521,42 @@ public class CloudMapService {
|
||||
continue;
|
||||
}
|
||||
exists = true;
|
||||
String ipv4 = instance.getAttributes().get("AWS_INSTANCE_IPV4");
|
||||
if (isIpv4(ipv4)) {
|
||||
return Optional.of(DnsAnswer.records(List.of(ipv4), ttl));
|
||||
DnsRecord record = instanceDnsRecord(instance, service, "", type);
|
||||
if ((type == 1 || type == 28) && record != null) {
|
||||
return Optional.of(DnsAnswer.typedRecords(List.of(record), ttl));
|
||||
}
|
||||
}
|
||||
}
|
||||
return exists ? Optional.of(DnsAnswer.noData()) : Optional.empty();
|
||||
}
|
||||
|
||||
private DnsRecord instanceDnsRecord(Instance instance, Service service, String namespace, int type) {
|
||||
Map<String, String> attributes = instance.getAttributes();
|
||||
String ipv4 = attributes.get("AWS_INSTANCE_IPV4");
|
||||
String ipv6 = attributes.get("AWS_INSTANCE_IPV6");
|
||||
try {
|
||||
DnsRecord record = switch (type) {
|
||||
case 1 -> isIpv4(ipv4) ? new DnsRecord.Address(1, ipv4) : null;
|
||||
case 28 -> ipv6 != null && !ipv6.contains("%") && !ipv6.startsWith("[")
|
||||
&& NetUtil.isValidIpV6Address(ipv6) ? new DnsRecord.Address(28, ipv6) : null;
|
||||
case 5 -> attributes.get("AWS_INSTANCE_CNAME") != null
|
||||
? new DnsRecord.Cname(attributes.get("AWS_INSTANCE_CNAME")) : null;
|
||||
case 33 -> attributes.get("AWS_INSTANCE_PORT") != null
|
||||
? new DnsRecord.Srv(Integer.parseInt(attributes.get("AWS_INSTANCE_PORT")),
|
||||
instance.getInstanceId() + "." + service.getName() + "." + namespace) : null;
|
||||
default -> null;
|
||||
};
|
||||
if (record != null) {
|
||||
record.data();
|
||||
}
|
||||
return record;
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.debugv("Invalid DNS attributes for Cloud Map instance {0}: {1}",
|
||||
instance.getInstanceId(), e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private JsonNode parseDnsConfig(String dnsConfig) {
|
||||
JsonNode config;
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user