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:
Aniket Shukla authored and GitHub committed 2026-09-28 00:43:06 -04:00
1 parent c9427b5832
commit c7492ce0f4
11 files changed
+642 -61

No files matched your search

@@ -112,6 +112,135 @@ class CloudMapDnsPacketIntegrationTest {
Map.of(), REGION).getTargets().get("NAMESPACE");
}
@Test
void ipv6InstanceAnswersAaaaWithConfiguredTtl() throws Exception {
String namespace = uniqueNamespace();
Service service = typedService(namespace, "AAAA", "MULTIVALUE");
cloudMapService.registerInstance(service.getId(), "task-1", null,
Map.of("AWS_INSTANCE_IPV6", "2001:db8::1"), REGION);
byte[] request = buildQuery("typed." + namespace, (short) 28);
byte[] response = query(new EmbeddedDnsServer(List.of(), List.of(cloudMapDnsRecordSource)), request);
ByteBuffer packet = ByteBuffer.wrap(response);
assertEquals(1, packet.getShort(6));
assertEquals(28, packet.getShort(request.length + 2));
assertEquals(45, packet.getInt(request.length + 6));
assertEquals(16, packet.getShort(request.length + 10));
assertArrayEquals(new byte[]{0x20, 0x01, 0x0d, (byte) 0xb8, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1},
Arrays.copyOfRange(response, request.length + 12, response.length));
}
@Test
void cnameRecordIsReturnedForBothCnameAndAddressQueries() throws Exception {
String namespace = uniqueNamespace();
Service service = typedService(namespace, "CNAME", "WEIGHTED");
cloudMapService.registerInstance(service.getId(), "task-1", null,
Map.of("AWS_INSTANCE_CNAME", "backend.example.com"), REGION);
for (short type : new short[]{1, 5, 28}) {
byte[] request = buildQuery("typed." + namespace, type);
byte[] response = query(new EmbeddedDnsServer(List.of(), List.of(cloudMapDnsRecordSource)), request);
ByteBuffer packet = ByteBuffer.wrap(response);
assertEquals(1, packet.getShort(6));
assertEquals(5, packet.getShort(request.length + 2));
assertEquals(45, packet.getInt(request.length + 6));
packet.position(request.length + 12);
assertEquals("backend.example.com", EmbeddedDnsServer.readName(packet, response));
}
}
@Test
void srvRecordCarriesThePortAndInstanceHostname() throws Exception {
String namespace = uniqueNamespace();
Service service = typedService(namespace, "SRV", "MULTIVALUE");
cloudMapService.registerInstance(service.getId(), "task-1", null,
Map.of("AWS_INSTANCE_IPV6", "2001:db8::2", "AWS_INSTANCE_PORT", "8080"), REGION);
byte[] request = buildQuery("typed." + namespace, (short) 33);
byte[] response = query(new EmbeddedDnsServer(List.of(), List.of(cloudMapDnsRecordSource)), request);
ByteBuffer packet = ByteBuffer.wrap(response);
assertEquals(1, packet.getShort(6));
assertEquals(33, packet.getShort(request.length + 2));
packet.position(request.length + 12);
assertEquals(1, packet.getShort());
assertEquals(1, packet.getShort());
assertEquals(8080, packet.getShort());
assertEquals("task-1.typed." + namespace, EmbeddedDnsServer.readName(packet, response));
byte[] address = query(new EmbeddedDnsServer(List.of(), List.of(cloudMapDnsRecordSource)),
buildQuery("task-1.typed." + namespace, (short) 28));
assertEquals(1, ByteBuffer.wrap(address).getShort(6));
}
@Test
void weightedRoutingReturnsOneInstanceInsteadOfEight() throws Exception {
String namespace = uniqueNamespace();
Service service = typedService(namespace, "A", "WEIGHTED");
for (int i = 1; i <= 10; i++) {
cloudMapService.registerInstance(service.getId(), "task-" + i, null,
Map.of("AWS_INSTANCE_IPV4", "172.31.0." + i), REGION);
}
byte[] response = query(new EmbeddedDnsServer(List.of(), List.of(cloudMapDnsRecordSource)),
buildQuery("typed." + namespace, (short) 1));
assertEquals(1, ByteBuffer.wrap(response).getShort(6));
}
private Service typedService(String namespace, String type, String routingPolicy) {
return cloudMapService.createService("typed", privateDnsNamespace(namespace), null, null,
"{\"RoutingPolicy\":\"" + routingPolicy + "\",\"DnsRecords\":[{\"Type\":\""
+ type + "\",\"TTL\":45}]}", null, null, null, Map.of(), REGION);
}
@Test
void largeSrvResponsesRespectLegacyAndEdnsUdpSizes() throws Exception {
String namespace = uniqueNamespace();
Service service = typedService(namespace, "SRV", "MULTIVALUE");
for (int i = 0; i < 8; i++) {
cloudMapService.registerInstance(service.getId(), "instance-with-long-name-" + i, null,
Map.of("AWS_INSTANCE_IPV4", "192.0.2.1", "AWS_INSTANCE_PORT", "8080"), REGION);
}
EmbeddedDnsServer dns = new EmbeddedDnsServer(List.of(), List.of(cloudMapDnsRecordSource));
byte[] legacyQuery = buildQuery("typed." + namespace, (short) 33);
byte[] legacy = query(dns, legacyQuery);
ByteBuffer legacyPacket = ByteBuffer.wrap(legacy);
assertTrue(legacy.length <= 512);
assertTrue((legacyPacket.getShort(2) & 0x0200) != 0);
assertTrue(legacyPacket.getShort(6) < 8);
assertCompleteRecords(legacy, legacyQuery.length, legacyPacket.getShort(6));
byte[] ednsQuery = withEdns(legacyQuery, 1232);
byte[] edns = query(dns, ednsQuery);
ByteBuffer ednsPacket = ByteBuffer.wrap(edns);
assertTrue(edns.length <= 1232);
assertEquals(0, ednsPacket.getShort(2) & 0x0200);
assertEquals(8, ednsPacket.getShort(6));
assertEquals(1, ednsPacket.getShort(10));
assertEquals(41, ednsPacket.getShort(edns.length - 10));
byte[] smallEdns = query(dns, withEdns(legacyQuery, 512));
assertTrue(smallEdns.length <= 512);
assertTrue((ByteBuffer.wrap(smallEdns).getShort(2) & 0x0200) != 0);
}
private static byte[] withEdns(byte[] query, int payloadSize) {
ByteBuffer extended = ByteBuffer.allocate(query.length + 11).put(query);
extended.putShort(10, (short) 1);
extended.put((byte) 0).putShort((short) 41).putShort((short) payloadSize).putInt(0).putShort((short) 0);
return extended.array();
}
private static void assertCompleteRecords(byte[] response, int questionEnd, int count) {
ByteBuffer packet = ByteBuffer.wrap(response);
packet.position(questionEnd);
for (int i = 0; i < count; i++) {
EmbeddedDnsServer.readName(packet, response);
packet.getShort();
packet.getShort();
packet.getInt();
int length = Short.toUnsignedInt(packet.getShort());
assertTrue(packet.remaining() >= length);
packet.position(packet.position() + length);
}
assertEquals(response.length, packet.position());
}
private static String uniqueNamespace() {
return "dnspacket" + UUID.randomUUID().toString().substring(0, 8) + ".internal";
}
@@ -0,0 +1,50 @@
package io.github.hectorvent.floci.core.common.dns;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
class DnsRecordTest {
@Test
void ipv6MappedIpv4StillEncodesSixteenBytes() {
byte[] bytes = new DnsRecord.Address(28, "::ffff:192.0.2.1").data();
assertEquals(16, bytes.length);
assertArrayEquals(new byte[]{(byte) 192, 0, 2, 1}, new byte[]{bytes[12], bytes[13], bytes[14], bytes[15]});
}
@Test
void addressTypeMustMatchItsValue() {
assertThrows(IllegalArgumentException.class, () -> new DnsRecord.Address(1, "::1"));
assertThrows(IllegalArgumentException.class, () -> new DnsRecord.Address(28, "192.0.2.1"));
}
@Test
void srvPortIsAnUnsignedSixteenBitValue() {
byte[] bytes = new DnsRecord.Srv(65535, "task.api.internal").data();
assertEquals(65535, Short.toUnsignedInt(ByteBuffer.wrap(bytes).getShort(4)));
assertThrows(IllegalArgumentException.class, () -> new DnsRecord.Srv(-1, "task.api.internal"));
assertThrows(IllegalArgumentException.class, () -> new DnsRecord.Srv(65536, "task.api.internal"));
}
@Test
void namesEncodeLabelsAndIgnoreOneTrailingDot() {
assertArrayEquals(new byte[]{1, 'a', 1, 'b', 0}, DnsRecord.encodeName("a.b."));
assertThrows(IllegalArgumentException.class, () -> DnsRecord.encodeName("a..b"));
assertThrows(IllegalArgumentException.class, () -> DnsRecord.encodeName("a".repeat(64) + ".b"));
assertThrows(IllegalArgumentException.class, () -> DnsRecord.encodeName("nonascii.\u00e9"));
}
@Test
void ipv4ProjectionDoesNotIncludeNamesOrIpv6Values() {
DnsAnswer answer = DnsAnswer.typedRecords(List.of(new DnsRecord.Address(28, "::1")), 15);
assertEquals(List.of(), answer.addresses());
assertEquals(1, answer.records().size());
assertFalse(answer.isEmpty());
}
}
@@ -2,15 +2,18 @@ package io.github.hectorvent.floci.services.cloudmap;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.dns.DnsAnswer;
import io.github.hectorvent.floci.core.common.dns.DnsRecord;
import io.github.hectorvent.floci.services.cloudmap.model.Operation;
import io.github.hectorvent.floci.services.cloudmap.model.Service;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -331,4 +334,91 @@ class CloudMapDnsResolutionTest {
private static String uniqueNamespace() {
return "dnsres" + UUID.randomUUID().toString().substring(0, 8) + ".internal";
}
@Test
void dualStackRecordsHaveIndependentTtlsAndNoDataForOtherTypes() {
String namespace = uniqueNamespace();
Service service = createService(privateDnsNamespace(namespace), "dual",
dnsConfig("{\"Type\":\"A\",\"TTL\":15},{\"Type\":\"AAAA\",\"TTL\":300}"));
cloudMapService.registerInstance(service.getId(), "task-1", null,
Map.of("AWS_INSTANCE_IPV4", "172.31.0.6", "AWS_INSTANCE_IPV6", "2001:db8::1"), REGION);
DnsAnswer aaaa = cloudMapService.resolveDnsNameIfOwned("dual." + namespace, 28).orElseThrow();
assertEquals(300, aaaa.ttlSeconds());
assertEquals(List.of(new DnsRecord.Address(28, "2001:db8::1")), aaaa.records());
assertEquals(15, cloudMapService.resolveDnsNameIfOwned("dual." + namespace).orElseThrow().ttlSeconds());
DnsAnswer txt = cloudMapService.resolveDnsNameIfOwned("dual." + namespace, 16).orElseThrow();
assertTrue(txt.nameExists());
assertTrue(txt.isEmpty());
assertFalse(cloudMapService.resolveDnsNameIfOwned("missing." + namespace, 28).orElseThrow().nameExists());
}
@Test
void weightedRoutingSelectsHealthyInstancesAndFallsBackWhenAllAreUnhealthy() {
String namespace = uniqueNamespace();
Service service = createService(privateDnsNamespace(namespace), "weighted",
"{\"RoutingPolicy\":\"WEIGHTED\",\"DnsRecords\":[{\"Type\":\"AAAA\",\"TTL\":15}]}");
for (int i = 1; i <= 3; i++) {
cloudMapService.registerInstance(service.getId(), "task-" + i, null,
Map.of("AWS_INSTANCE_IPV6", "2001:db8::" + i), REGION);
}
cloudMapService.getInstance(service.getId(), "task-1").setHealthStatus("UNHEALTHY");
Set<DnsRecord> sampled = new HashSet<>();
for (int i = 0; i < 100; i++) {
DnsAnswer answer = cloudMapService.resolveDnsNameIfOwned("weighted." + namespace, 28).orElseThrow();
assertEquals(1, answer.records().size());
assertFalse(answer.records().contains(new DnsRecord.Address(28, "2001:db8::1")));
sampled.addAll(answer.records());
}
assertEquals(2, sampled.size());
cloudMapService.getInstance(service.getId(), "task-2").setHealthStatus("UNHEALTHY");
cloudMapService.getInstance(service.getId(), "task-3").setHealthStatus("UNHEALTHY");
assertEquals(1, cloudMapService.resolveDnsNameIfOwned("weighted." + namespace, 28).orElseThrow().records().size());
}
@Test
void invalidIpv6AndSrvPortsDoNotProduceMalformedPackets() {
String namespace = uniqueNamespace();
Service service = createService(privateDnsNamespace(namespace), "invalid",
dnsConfig("{\"Type\":\"SRV\",\"TTL\":15}"));
cloudMapService.registerInstance(service.getId(), "task-1", null,
Map.of("AWS_INSTANCE_IPV6", "not-an-address", "AWS_INSTANCE_PORT", "70000"), REGION);
assertTrue(cloudMapService.resolveDnsNameIfOwned("invalid." + namespace, 33).orElseThrow().isEmpty());
assertTrue(cloudMapService.resolveDnsNameIfOwned("task-1.invalid." + namespace, 28).orElseThrow().isEmpty());
}
@Test
void legacyMixedCnameConfigurationDoesNotHideConfiguredAddresses() {
String namespace = uniqueNamespace();
Service service = createService(privateDnsNamespace(namespace), "mixed",
dnsConfig("{\"Type\":\"A\",\"TTL\":15},{\"Type\":\"CNAME\",\"TTL\":45}"));
registerInstance(service.getId(), "task-1", "172.31.0.6");
DnsAnswer answer = cloudMapService.resolveDnsNameIfOwned("mixed." + namespace, 1).orElseThrow();
assertEquals(List.of("172.31.0.6"), answer.addresses());
assertEquals(15, answer.ttlSeconds());
cloudMapService.registerInstance(service.getId(), "task-1", null,
Map.of("AWS_INSTANCE_IPV4", "172.31.0.6", "AWS_INSTANCE_CNAME", "backend.example.com"), REGION);
assertEquals(List.of("172.31.0.6"), cloudMapService.resolveDnsName("mixed." + namespace));
assertEquals(List.of(new DnsRecord.Cname("backend.example.com")),
cloudMapService.resolveDnsNameIfOwned("mixed." + namespace, 5).orElseThrow().records());
}
@Test
void srvRegistrationRejectsUnencodableTargetsWithoutChangingInstanceState() {
String namespace = uniqueNamespace();
Service service = createService(privateDnsNamespace(namespace), "srv",
dnsConfig("{\"Type\":\"SRV\",\"TTL\":15}"));
Map<String, String> attributes = Map.of("AWS_INSTANCE_PORT", "8080", "AWS_INSTANCE_IPV4", "192.0.2.1");
cloudMapService.registerInstance(service.getId(), "a".repeat(63), null, attributes, REGION);
long revision = service.getRevision();
AwsException error = assertThrows(AwsException.class, () -> cloudMapService.registerInstance(
service.getId(), "a".repeat(64), null, attributes, REGION));
assertEquals("InvalidInput", error.getErrorCode());
assertEquals(1, service.getInstanceCount());
assertEquals(revision, service.getRevision());
assertEquals(1, cloudMapService.resolveDnsNameIfOwned("srv." + namespace, 33).orElseThrow().records().size());
// The DNS target restriction does not shorten the API's general identifier limit.
Service aService = createService(service.getNamespaceId(), "addresses");
registerInstance(aService.getId(), "a".repeat(64), "192.0.2.2");
assertEquals(1, aService.getInstanceCount());
}
}