diff --git a/docs/services/route53.md b/docs/services/route53.md index c64d2c7c81..6d25d79e1f 100644 --- a/docs/services/route53.md +++ b/docs/services/route53.md @@ -88,6 +88,10 @@ ns-3.awsdns-03.com ns-4.awsdns-04.co.uk ``` +A configured name can be written with or without the final dot. The delegation set lists it without the dot, as Route 53 does. A new zone's apex NS records, and the primary server named in its SOA record (the first nameserver), use the fully qualified form with the dot. + +The apex NS and SOA records are written when a zone is created, while the delegation set that `CreateHostedZone` and `GetHostedZone` return always reflects the current settings. A zone kept in persistent storage therefore keeps its original NS and SOA records after these settings change; update them with an `UPSERT` change. + ## Configuration | Variable | Default | Description | diff --git a/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsLookupHelper.java b/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsLookupHelper.java index 75edd6d3c2..b6bfd93143 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsLookupHelper.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/dns/DnsLookupHelper.java @@ -205,7 +205,11 @@ public class DnsLookupHelper { return List.of(); } - private static String normalizeTarget(String s) { + /** + * A DNS name without surrounding whitespace and without its final dot, keeping its case. A + * null name reads as empty. + */ + public static String normalizeTarget(String s) { if (s == null) { return ""; } diff --git a/src/main/java/io/github/hectorvent/floci/services/route53/Route53DnsRecordSource.java b/src/main/java/io/github/hectorvent/floci/services/route53/Route53DnsRecordSource.java index 501003c7ce..2902deed2e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/route53/Route53DnsRecordSource.java +++ b/src/main/java/io/github/hectorvent/floci/services/route53/Route53DnsRecordSource.java @@ -195,10 +195,7 @@ public class Route53DnsRecordSource implements DnsRecordSource { } AliasTarget alias = rrs.getAliasTarget(); if (alias != null && alias.getDnsName() != null && !alias.getDnsName().isBlank()) { - String cleanAlias = alias.getDnsName().trim(); - if (cleanAlias.endsWith(".")) { - cleanAlias = cleanAlias.substring(0, cleanAlias.length() - 1); - } + String cleanAlias = DnsLookupHelper.normalizeTarget(alias.getDnsName()); // An alias record has no TTL of its own; Route 53 answers with the target's. if (route53Service.isCoveredByPrivateZone(cleanAlias)) { DnsAnswer target = resolveAddresses(cleanAlias, visited, depth + 1); @@ -226,10 +223,7 @@ public class Route53DnsRecordSource implements DnsRecordSource { if (rrs.getRecords() != null && !rrs.getRecords().isEmpty()) { String target = rrs.getRecords().get(0).getValue(); if (target != null && !target.isBlank()) { - String cleanTarget = target.trim(); - if (cleanTarget.endsWith(".")) { - cleanTarget = cleanTarget.substring(0, cleanTarget.length() - 1); - } + String cleanTarget = DnsLookupHelper.normalizeTarget(target); // The answer is flattened to A records, so it can be cached only as long // as the shortest-lived record in the chain. int cnameTtl = ttlOf(rrs); diff --git a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java index 89c29b7dd4..0e24a1f39d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java @@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.DomainNames; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.Resettable; +import io.github.hectorvent.floci.core.common.dns.DnsLookupHelper; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -83,7 +84,7 @@ public class Route53Service implements Resettable { r53.defaultNameserver2(), r53.defaultNameserver3(), r53.defaultNameserver4() - ); + ).stream().map(Route53Service::delegationName).toList(); this.defaultAccountId = config.defaultAccountId(); this.regionResolver = regionResolver; this.ec2Service = ec2Service; @@ -889,6 +890,15 @@ public class Route53Service implements Resettable { return name.endsWith(".") ? name : name + "."; } + /** + * A configured nameserver as the delegation set lists it: without surrounding whitespace, which + * the config does not trim, and without the final dot, as Route 53 lists it. {@code strip()} + * also removes the Unicode spaces that {@code trim()} keeps. + */ + private static String delegationName(String configured) { + return DnsLookupHelper.normalizeTarget(configured.strip()); + } + public static String normalizeRecordName(String name) { String normalized = normalizeName(name); if (normalized == null) { @@ -932,7 +942,7 @@ public class Route53Service implements Resettable { soa.setType("SOA"); soa.setTtl(900L); soa.setRecords(List.of(new ResourceRecord( - nameServers.get(0) + " awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"))); + normalizeName(nameServers.get(0)) + " awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"))); records.add(soa); ResourceRecordSet ns = new ResourceRecordSet(); @@ -940,7 +950,7 @@ public class Route53Service implements Resettable { ns.setType("NS"); ns.setTtl(172800L); ns.setRecords(nameServers.stream() - .map(n -> new ResourceRecord(n + ".")) + .map(n -> new ResourceRecord(normalizeName(n))) .toList()); records.add(ns); diff --git a/src/test/java/io/github/hectorvent/floci/core/common/dns/DnsLookupHelperTest.java b/src/test/java/io/github/hectorvent/floci/core/common/dns/DnsLookupHelperTest.java index 0b76b64a9b..a061b1993d 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/dns/DnsLookupHelperTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/dns/DnsLookupHelperTest.java @@ -11,6 +11,20 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class DnsLookupHelperTest { + @Test + void normalizeTargetTrimsAndDropsOneFinalDotKeepingCase() { + assertEquals("", DnsLookupHelper.normalizeTarget(null)); + assertEquals("", DnsLookupHelper.normalizeTarget("")); + assertEquals("", DnsLookupHelper.normalizeTarget(" \t ")); + assertEquals("", DnsLookupHelper.normalizeTarget(".")); + assertEquals("a.example", DnsLookupHelper.normalizeTarget("a.example")); + assertEquals("a.example", DnsLookupHelper.normalizeTarget("a.example.")); + assertEquals("a.example", DnsLookupHelper.normalizeTarget("\t a.example.\r\n")); + assertEquals("a.example.", DnsLookupHelper.normalizeTarget("a.example..")); + assertEquals("A.Example", DnsLookupHelper.normalizeTarget("A.Example.")); + assertEquals("a.example.\u2003", DnsLookupHelper.normalizeTarget("a.example.\u2003")); + } + @Test void parseARecordsRejectsMismatchedTxId() { byte[] resp = buildSyntheticDnsResponse((short) 100, "example.com", "93.184.216.34"); diff --git a/src/test/java/io/github/hectorvent/floci/services/route53/Route53IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/route53/Route53IntegrationTest.java index 126e6082db..c1b58430df 100644 --- a/src/test/java/io/github/hectorvent/floci/services/route53/Route53IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/route53/Route53IntegrationTest.java @@ -173,6 +173,9 @@ class Route53IntegrationTest { assertThat(body, containsString("SOA")); assertThat(body, containsString("NS")); + assertThat(body, containsString( + "ns-1.awsdns-01.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400")); + assertThat(body, containsString("ns-4.awsdns-04.co.uk.")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/route53/Route53ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/route53/Route53ServiceTest.java index b7877b7cb8..d9e9ac6776 100644 --- a/src/test/java/io/github/hectorvent/floci/services/route53/Route53ServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/route53/Route53ServiceTest.java @@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.route53; 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.StorageFactory; import io.github.hectorvent.floci.services.ec2.Ec2Service; @@ -15,6 +17,7 @@ import io.github.hectorvent.floci.services.route53.model.VpcAssociation; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mockito; import java.util.ArrayList; @@ -26,6 +29,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -38,6 +42,10 @@ class Route53ServiceTest { private final Map> stores = new HashMap<>(); private Route53Service newService() { + return newService("."); + } + + private Route53Service newService(String nameserverSuffix) { StorageFactory storageFactory = Mockito.mock(StorageFactory.class); Mockito.when(storageFactory.create(Mockito.anyString(), Mockito.anyString(), Mockito.any())) .thenAnswer(invocation -> { @@ -54,10 +62,10 @@ class Route53ServiceTest { Mockito.when(config.defaultAccountId()).thenReturn(ACCOUNT); Mockito.when(config.services()).thenReturn(servicesConfig); Mockito.when(servicesConfig.route53()).thenReturn(route53Config); - Mockito.when(route53Config.defaultNameserver1()).thenReturn("ns-1.awsdns-00.com."); - Mockito.when(route53Config.defaultNameserver2()).thenReturn("ns-2.awsdns-00.net."); - Mockito.when(route53Config.defaultNameserver3()).thenReturn("ns-3.awsdns-00.org."); - Mockito.when(route53Config.defaultNameserver4()).thenReturn("ns-4.awsdns-00.co.uk."); + Mockito.when(route53Config.defaultNameserver1()).thenReturn("ns-1.awsdns-00.com" + nameserverSuffix); + Mockito.when(route53Config.defaultNameserver2()).thenReturn("ns-2.awsdns-00.net" + nameserverSuffix); + Mockito.when(route53Config.defaultNameserver3()).thenReturn("ns-3.awsdns-00.org" + nameserverSuffix); + Mockito.when(route53Config.defaultNameserver4()).thenReturn("ns-4.awsdns-00.co.uk" + nameserverSuffix); Mockito.when(route53Config.vpcAssociationControlPlaneDelayMs()).thenReturn(0L); Ec2Service ec2Service = Mockito.mock(Ec2Service.class); @@ -66,6 +74,37 @@ class Route53ServiceTest { return new Route53Service(storageFactory, config, regionResolver, ec2Service); } + @ParameterizedTest(name = "configured names ending in {0}") + @ValueSource(strings = {"", ".", " ", ".\t", ".\u2003"}) + void defaultApexRecordsUseAbsoluteNamesAndTheDelegationSetDoesNot(String nameserverSuffix) { + Route53Service service = newService(nameserverSuffix); + String zoneId = service.createHostedZone("authority.example", "authority", null, null).zone().getId(); + List records = service.listResourceRecordSets(zoneId, null, null, 100); + + assertAll( + () -> assertEquals(List.of("ns-1.awsdns-00.com. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"), + valuesOf(records, "SOA"), "SOA"), + () -> assertEquals(List.of("ns-1.awsdns-00.com.", "ns-2.awsdns-00.net.", "ns-3.awsdns-00.org.", + "ns-4.awsdns-00.co.uk."), valuesOf(records, "NS"), "NS"), + () -> assertEquals(List.of("ns-1.awsdns-00.com", "ns-2.awsdns-00.net", "ns-3.awsdns-00.org", + "ns-4.awsdns-00.co.uk"), service.getNameServers(), "delegation set")); + } + + @ParameterizedTest(name = "configured names ending in {0}") + @ValueSource(strings = {"", ".", " ", ".\t", ".\u2003"}) + void apexNsQueryIsAnswered(String nameserverSuffix) { + Route53Service service = newService(nameserverSuffix); + service.createHostedZone("authority.example", "authority", null, null); + + DnsAnswer answer = new Route53DnsRecordSource(service).resolveAuthoritative("authority.example", 2) + .orElseThrow(); + + assertEquals(0, answer.responseCode(), "rcode"); + assertEquals(List.of(new DnsRecord.Ns("ns-1.awsdns-00.com."), new DnsRecord.Ns("ns-2.awsdns-00.net."), + new DnsRecord.Ns("ns-3.awsdns-00.org."), new DnsRecord.Ns("ns-4.awsdns-00.co.uk.")), + answer.records(), "answers"); + } + @Test void rejectedBatchDoesNotRenameSharedStoredRecord() { Route53Service service = newService(); @@ -106,6 +145,14 @@ class Route53ServiceTest { Mockito.verifyNoInteractions(zones); } + private static List valuesOf(List records, String type) { + return records.stream() + .filter(rrs -> type.equals(rrs.getType())) + .flatMap(rrs -> rrs.getRecords().stream()) + .map(ResourceRecord::getValue) + .toList(); + } + @Test void recordIdentityFoldsOnlyAsciiLettersAndPreservesPolicyIdentity() { ResourceRecordSet upper = weightedRecord("WWW.example.com", 60, "Blue", 1, "192.0.2.1");