fix(cloudmap): create the Route 53 hosted zone behind a DNS namespace (#7042)

* fix(cloudmap): create the Route 53 hosted zone behind a DNS namespace

Private and public DNS namespaces reported a random HostedZoneId that no
Route 53 zone backed. They now create a real zone (private with the
namespace VPC and region, or public) via Route53Service, and DeleteNamespace
deletes it; a zone holding user records refuses with HostedZoneNotEmpty and
the namespace is kept.

A private zone now covers the namespace's names, and the Route 53 DNS source
answers NXDOMAIN for names it has no record for. EmbeddedDnsServer took the
first present answer across sources, so bean order could hide Cloud Map
instances. An NXDOMAIN from one source no longer wins over another source's
real answer.

Reset: zones and namespaces live in separate stores; Route53Service.clear()
only drops in-memory counters, so no Cloud Map reset hook was added.

* test(cloudmap): select the user's zone now that namespaces own a real one

A private DNS namespace now creates a real Route 53 hosted zone, as AWS
does, so the compat projects hold two zones named floci-compat.internal:
the namespace's private zone and the public aws_route53_zone. The bats
lookups took the first zone by name, which picked the namespace zone
whenever its id sorted first, so the record and tag assertions read the
wrong zone. They now select the public zone.

The CloudFormation test pinned the namespace HostedZoneId to 14
characters, the length of Cloud Map's old placeholder. The id now comes
from Route 53, which, like AWS, does not use a fixed length; the test
checks for a bare Z-prefixed id instead.

* fix(dns): let a record source with records win over another's NODATA

Two sources can own one zone (a Cloud Map namespace and its Route 53
zone). An empty NOERROR from the first consulted source was returned
before the other was asked, so a TXT record in the zone hid Cloud Map's
A record at the same name. Empty answers are now fallbacks, an existing
name preferred over NXDOMAIN.

* fix(cloudmap): log a namespace zone that was already deleted

* docs(cloudmap): describe the real hosted zone in the operations table

* test(dns): pin how record sources combine REFUSED and negative answers

Another source's records now beat a REFUSED, and REFUSED beats NXDOMAIN
in either source order; nothing pinned either, so returning REFUSED
immediately went unnoticed. Also pin, as current behaviour, that a plain
NODATA beats an NXDOMAIN carrying an SOA authority.

The packet test now deletes the TXT records it adds to the namespace's
hosted zone, so the zone is left deletable.

---------

Co-authored-by: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
This commit is contained in:
Allen SanbornandPriamo Germosen authored and GitHub committed 2026-10-30 23:14:37 -04:00
1 parent b3d9f2f576
commit f41f794337
9 files changed
+256 -28

No files matched your search

@@ -3,6 +3,10 @@ package io.github.hectorvent.floci.core.common.dns;
import io.github.hectorvent.floci.services.cloudmap.CloudMapDnsRecordSource;
import io.github.hectorvent.floci.services.cloudmap.CloudMapService;
import io.github.hectorvent.floci.services.cloudmap.model.Service;
import io.github.hectorvent.floci.services.route53.Route53DnsRecordSource;
import io.github.hectorvent.floci.services.route53.Route53Service;
import io.github.hectorvent.floci.services.route53.model.ResourceRecord;
import io.github.hectorvent.floci.services.route53.model.ResourceRecordSet;
import io.quarkus.test.junit.QuarkusTest;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
@@ -13,6 +17,7 @@ import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -35,6 +40,12 @@ class CloudMapDnsPacketIntegrationTest {
@Inject
CloudMapDnsRecordSource cloudMapDnsRecordSource;
@Inject
Route53DnsRecordSource route53DnsRecordSource;
@Inject
Route53Service route53Service;
@Inject
Vertx vertx;
@@ -58,6 +69,64 @@ class CloudMapDnsPacketIntegrationTest {
Arrays.copyOfRange(response, request.length + 12, request.length + 16));
}
/**
* The namespace's Route 53 zone covers the same names, and answers NXDOMAIN for a name it
* has no record for, or NOERROR/NODATA for a name holding only another type (the TXT below).
* With that source consulted first, Cloud Map's A record must still win.
*/
@Test
void cloudMapInstanceResolvesWhenTheRoute53ZoneSourceIsConsultedFirst() throws Exception {
String namespace = uniqueNamespace();
String namespaceId = privateDnsNamespace(namespace);
Service service = cloudMapService.createService("both", namespaceId, null, null,
"{\"DnsRecords\":[{\"Type\":\"A\",\"TTL\":15}]}", null, null, null, Map.of(), REGION);
cloudMapService.registerInstance(service.getId(), "task-1", null,
Map.of("AWS_INSTANCE_IPV4", "172.31.0.7"), REGION);
String zoneId = cloudMapService.getNamespace(namespaceId).getHostedZoneId();
List<Map<String, Object>> txts = new ArrayList<>();
for (String txtName : List.of("both." + namespace, "txtonly." + namespace)) {
ResourceRecordSet txt = new ResourceRecordSet();
txt.setName(txtName);
txt.setType("TXT");
txt.setTtl(60L);
txt.setRecords(List.of(new ResourceRecord("\"owner=test\"")));
route53Service.changeResourceRecordSets(zoneId, List.of(Map.of("action", "CREATE", "rrs", txt)), null);
txts.add(Map.of("action", "DELETE", "rrs", txt));
}
try {
assertBothSourceOrdersResolve(namespace);
} finally {
// The namespace's zone must hold only its SOA and NS again, or DeleteNamespace fails.
route53Service.changeResourceRecordSets(zoneId, txts, null);
}
}
private void assertBothSourceOrdersResolve(String namespace) throws Exception {
for (List<DnsRecordSource> sources : List.of(
List.of(route53DnsRecordSource, cloudMapDnsRecordSource),
List.of(cloudMapDnsRecordSource, route53DnsRecordSource))) {
byte[] request = buildQuery("both." + namespace, (short) 1);
byte[] response = query(new EmbeddedDnsServer(List.of(), sources), request);
ByteBuffer packet = ByteBuffer.wrap(response);
assertEquals(0, packet.getShort(2) & 0x000F);
assertEquals(1, packet.getShort(6));
assertArrayEquals(new byte[]{(byte) 172, 31, 0, 7},
Arrays.copyOfRange(response, request.length + 12, request.length + 16));
// A name only Route 53 holds (as TXT) is NODATA for A, not Cloud Map's NXDOMAIN.
ByteBuffer txtOnly = ByteBuffer.wrap(query(new EmbeddedDnsServer(List.of(), sources),
buildQuery("txtonly." + namespace, (short) 1)));
assertEquals(0, txtOnly.getShort(2) & 0x000F);
assertEquals(0, txtOnly.getShort(6));
}
// A name neither source has is still NXDOMAIN.
byte[] missing = query(new EmbeddedDnsServer(List.of(),
List.of(route53DnsRecordSource, cloudMapDnsRecordSource)),
buildQuery("nothere." + namespace, (short) 1));
assertEquals(3, ByteBuffer.wrap(missing).getShort(2) & 0x000F);
}
@Test
void srvOnlyNameReturnsNoDataForAQuery() throws Exception {
String namespace = uniqueNamespace();
@@ -207,6 +207,46 @@ class EmbeddedDnsServerTest {
withSource.resolveARecord("bucket.localhost.floci.io", "172.31.0.2"));
}
@Test
void anotherSourcesRecordsWinOverRefused() {
DnsRecordSource refusing = name -> Optional.of(DnsAnswer.refused());
EmbeddedDnsServer withSources = new EmbeddedDnsServer(List.of(),
List.of(refusing, source("api.sapphire.internal", List.of("172.31.0.6"))));
DnsAnswer answer = withSources.resolveARecordWithOwnership(
"api.sapphire.internal", "172.31.0.2").orElseThrow();
assertEquals(List.of("172.31.0.6"), answer.addresses());
assertEquals(0, answer.responseCode());
}
@Test
void refusedWinsOverNxDomainInEitherOrder() {
DnsRecordSource refusing = name -> Optional.of(DnsAnswer.refused());
DnsRecordSource absent = name -> Optional.of(DnsAnswer.nxDomain());
for (List<DnsRecordSource> sources : List.of(List.of(refusing, absent), List.of(absent, refusing))) {
assertEquals(5, new EmbeddedDnsServer(List.of(), sources).resolveARecordWithOwnership(
"api.sapphire.internal", "172.31.0.2").orElseThrow().responseCode());
}
}
// Current behaviour, not a decision: a plain NODATA from one source beats another source's
// NXDOMAIN even when that NXDOMAIN carries a Route 53 SOA, so the SOA authority is dropped.
// Unreachable today, since only one source answers authoritatively.
@Test
void noDataCurrentlyWinsOverNxDomainCarryingAuthority() {
DnsAnswer nxWithSoa = DnsAnswer.nxDomain().withAuthority("sapphire.internal",
new DnsRecord.Soa("ns.example. host.example. 1 7200 900 1209600 30"), 900);
DnsRecordSource authoritative = name -> Optional.of(nxWithSoa);
DnsRecordSource noData = name -> Optional.of(DnsAnswer.noData());
for (List<DnsRecordSource> sources : List.of(List.of(authoritative, noData),
List.of(noData, authoritative))) {
DnsAnswer answer = new EmbeddedDnsServer(List.of(), sources).resolveARecordWithOwnership(
"api.sapphire.internal", "172.31.0.2").orElseThrow();
assertEquals(0, answer.responseCode());
assertNull(answer.authority());
}
}
private static DnsRecordSource source(String owned, List<String> addresses) {
return source(owned, addresses, DnsAnswer.DEFAULT_TTL_SECONDS);
}
@@ -143,7 +143,8 @@ class CloudMapCfnIntegrationTest {
.then().statusCode(200)
.extract().asString())
.path("Namespace").path("Properties").path("DnsProperties").path("HostedZoneId").asText(null);
assertThat(hostedZoneId, matchesPattern("Z[A-Z0-9]{13}"));
// A bare Route 53 zone id (no /hostedzone/ prefix); AWS ids vary in length, so none is pinned.
assertThat(hostedZoneId, matchesPattern("Z[A-Z0-9]+"));
assertEquals(hostedZoneId, outputValue(outputs, "NsHz"));
assertEquals("main", outputValue(outputs, "SvcName"));
assertThat(outputValue(outputs, "NoNameName"), matchesPattern("SvcNoName-[0-9a-f]{12}"));
@@ -12,6 +12,7 @@ import org.junit.jupiter.api.TestMethodOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -215,6 +216,92 @@ class CloudMapIntegrationTest {
call("DeleteNamespace", "{\"Id\":\"" + customNamespaceId + "\"}").then().statusCode(200);
}
@Test
void privateDnsNamespaceBacksARealPrivateHostedZone() {
String nsId = createDnsNamespace("CreatePrivateDnsNamespace",
"{\"Name\":\"cm-private.example.com\",\"Vpc\":\"vpc-cm123456\"}");
String zoneId = hostedZoneId(nsId);
given().get("/2013-04-01/hostedzone/" + zoneId).then().statusCode(200)
.body("GetHostedZoneResponse.HostedZone.Name", equalTo("cm-private.example.com."))
.body("GetHostedZoneResponse.HostedZone.Config.PrivateZone", equalTo("true"))
.body("GetHostedZoneResponse.VPCs.VPC.VPCId", equalTo("vpc-cm123456"));
call("DeleteNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(200);
given().get("/2013-04-01/hostedzone/" + zoneId).then().statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchHostedZone"));
}
@Test
void publicDnsNamespaceBacksARealPublicHostedZone() {
String nsId = createDnsNamespace("CreatePublicDnsNamespace", "{\"Name\":\"cm-public.example.com\"}");
String zoneId = hostedZoneId(nsId);
given().get("/2013-04-01/hostedzone/" + zoneId).then().statusCode(200)
.body("GetHostedZoneResponse.HostedZone.Name", equalTo("cm-public.example.com."))
.body("GetHostedZoneResponse.HostedZone.Config.PrivateZone", equalTo("false"));
call("DeleteNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(200);
given().get("/2013-04-01/hostedzone/" + zoneId).then().statusCode(404);
}
@Test
void deleteNamespaceWithServicesKeepsNamespaceAndZone() {
String nsId = createDnsNamespace("CreatePrivateDnsNamespace",
"{\"Name\":\"cm-inuse.example.com\",\"Vpc\":\"vpc-cm123456\"}");
String zoneId = hostedZoneId(nsId);
call("CreateService", "{\"Name\":\"s\",\"NamespaceId\":\"" + nsId + "\"}").then().statusCode(200);
call("DeleteNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(400)
.body("__type", equalTo("ResourceInUse"));
call("GetNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(200);
given().get("/2013-04-01/hostedzone/" + zoneId).then().statusCode(200);
}
@Test
void deleteNamespaceToleratesAZoneThatIsAlreadyGone() {
String nsId = createDnsNamespace("CreatePublicDnsNamespace", "{\"Name\":\"cm-nozone.example.com\"}");
given().delete("/2013-04-01/hostedzone/" + hostedZoneId(nsId)).then().statusCode(200);
call("DeleteNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(200);
call("GetNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(404)
.body("__type", equalTo("NamespaceNotFound"));
}
@Test
void deleteNamespaceWithUserRecordsInTheZoneKeepsTheNamespace() {
String nsId = createDnsNamespace("CreatePublicDnsNamespace", "{\"Name\":\"cm-records.example.com\"}");
String zoneId = hostedZoneId(nsId);
given().contentType("application/xml").body("""
<ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
<ChangeBatch><Changes><Change><Action>CREATE</Action><ResourceRecordSet>
<Name>www.cm-records.example.com</Name><Type>A</Type><TTL>60</TTL>
<ResourceRecords><ResourceRecord><Value>10.0.0.1</Value></ResourceRecord></ResourceRecords>
</ResourceRecordSet></Change></Changes></ChangeBatch>
</ChangeResourceRecordSetsRequest>""")
.post("/2013-04-01/hostedzone/" + zoneId + "/rrset").then().statusCode(200);
call("DeleteNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(400)
.body("__type", equalTo("HostedZoneNotEmpty"));
call("GetNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(200);
}
private static String createDnsNamespace(String action, String body) {
String operationId = call(action, body).then().statusCode(200)
.extract().jsonPath().getString("OperationId");
return call("GetOperation", "{\"OperationId\":\"" + operationId + "\"}")
.then().statusCode(200).extract().jsonPath().getString("Operation.Targets.NAMESPACE");
}
@SuppressWarnings("unchecked")
private static String hostedZoneId(String nsId) {
Map<String, Object> ns = call("GetNamespace", "{\"Id\":\"" + nsId + "\"}").then().statusCode(200)
.extract().jsonPath().getMap("Namespace");
Map<String, Object> props = (Map<String, Object>) ns.get("Properties");
return (String) ((Map<String, Object>) props.get("DnsProperties")).get("HostedZoneId");
}
private static String createCustomHealthService(String name, String nsId, String healthCheckCustomConfig) {
return call("CreateService", "{\"Name\":\"" + name + "\",\"NamespaceId\":\"" + nsId
+ "\",\"HealthCheckCustomConfig\":" + healthCheckCustomConfig + "}")