fix(route53): assign distinct nameservers to delegation sets (#7373)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-03 07:57:23 -04:00
1 parent 548f972d01
commit 525d48dede
11 files changed
+317 -56

No files matched your search

+8 -8
View File
@@ -83,7 +83,7 @@ Public address routing, aliases, CNAME chasing, health-based or weighted record
## Default Nameservers
New zones without a reusable delegation set use these nameservers (configurable via `floci.services.route53.*`):
By default, new public zones without a reusable delegation set and new standalone reusable sets receive four nameservers shaped like `ns-2048.awsdns-64.com`. Floci derives their numbers from each resource ID and uses the `org`, `net`, `com`, and `co.uk` TLDs. An occasional server name can overlap between resources, as with Route 53. With no explicit nameserver overrides, private zones keep these names:
```
ns-1.awsdns-01.org
@@ -92,9 +92,9 @@ 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.
Set any `floci.services.route53.default-nameserver*` value to use that exact name for its slot in new zones without a reusable set and in new standalone sets, including when the configured value equals the former default shown above. Zones using a reusable set keep that set's names. A configured name can have or omit its final dot. The delegation set lists names without the final 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.
For zones without a reusable delegation set, the apex NS and SOA records are written when a zone is created, while the delegation set that `CreateHostedZone` and `GetHostedZone` return 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.
For zones without a reusable delegation set, both the assigned servers returned by `CreateHostedZone` and `GetHostedZone` and the apex NS and SOA records remain stable after the configuration changes. Update the records with an `UPSERT` change if needed.
## Reusable Delegation Sets
@@ -106,7 +106,7 @@ Pass `delegationsetid` to `ListHostedZones` to filter by a reusable set before p
Pass `DelegationSetId` to `CreateHostedZone` to reuse a set. The create/get responses include its ID, caller reference, and name servers; the new zone's apex NS records and SOA primary server use those same servers. Passing `HostedZoneId` to `CreateReusableDelegationSet` marks an existing public zone's delegation set as reusable. Private zones cannot use reusable sets. Deleting a set associated with any hosted zone returns `DelegationSetInUse`; delete its zones first. The default quotas are 100 sets per account and 100 hosted zones per set.
A new standalone set snapshots the four configured local nameservers. Promotion reuses the zone's original assignment, even after configuration or apex NS changes. Older stored zones without a separate assignment recover it from their four apex NS entries, falling back to current settings when those entries are unavailable. Its servers and zone associations persist through the configured Route53 storage backend and remain stable after configuration changes. Floci does not allocate distinct AWS nameservers for each set, so different sets may contain the same servers. Requests selecting a reusable set reject equal, parent, or child public-zone names that share a nameserver with an existing public zone (`ConflictingDomainExists`). Sibling domains can share a set. This feature manages delegation metadata and apex records; public DNS delegation referrals remain unsupported.
A new standalone set keeps its assigned four nameservers. Promotion reuses the zone's original assignment, even after configuration or apex NS changes. Older stored zones without a separate assignment recover it from their four apex NS entries, falling back to current settings when those entries are unavailable. Its servers and zone associations persist through the configured Route53 storage backend and remain stable after configuration changes. Requests selecting a reusable set reject equal, parent, or child public-zone names only when they share a nameserver with an existing public zone (`ConflictingDomainExists`). Sibling domains can share a set. This feature manages delegation metadata and apex records; public DNS delegation referrals remain unsupported.
```bash
set_id=$(aws route53 create-reusable-delegation-set \
@@ -124,10 +124,10 @@ aws route53 create-hosted-zone \
| Variable | Default | Description |
|---|---|---|
| `FLOCI_SERVICES_ROUTE53_ENABLED` | `true` | Enable or disable the service |
| `FLOCI_SERVICES_ROUTE53_DEFAULT_NAMESERVER1` | `ns-1.awsdns-01.org` | First default nameserver returned in delegation sets |
| `FLOCI_SERVICES_ROUTE53_DEFAULT_NAMESERVER2` | `ns-2.awsdns-02.net` | Second default nameserver |
| `FLOCI_SERVICES_ROUTE53_DEFAULT_NAMESERVER3` | `ns-3.awsdns-03.com` | Third default nameserver |
| `FLOCI_SERVICES_ROUTE53_DEFAULT_NAMESERVER4` | `ns-4.awsdns-04.co.uk` | Fourth default nameserver |
| `FLOCI_SERVICES_ROUTE53_DEFAULT_NAMESERVER1` | unset | Exact override for the first assigned nameserver |
| `FLOCI_SERVICES_ROUTE53_DEFAULT_NAMESERVER2` | unset | Exact override for the second assigned nameserver |
| `FLOCI_SERVICES_ROUTE53_DEFAULT_NAMESERVER3` | unset | Exact override for the third assigned nameserver |
| `FLOCI_SERVICES_ROUTE53_DEFAULT_NAMESERVER4` | unset | Exact override for the fourth assigned nameserver |
| `FLOCI_SERVICES_ROUTE53_VPC_ASSOCIATION_CONTROL_PLANE_DELAY_MS` | `0` | Optional VPC association/auth processing window used to reproduce documented retryable overlap errors |
## CLI Examples
@@ -1250,17 +1250,13 @@ public interface EmulatorConfig {
@WithDefault("true")
boolean enabled();
@WithDefault("ns-1.awsdns-01.org")
String defaultNameserver1();
Optional<String> defaultNameserver1();
@WithDefault("ns-2.awsdns-02.net")
String defaultNameserver2();
Optional<String> defaultNameserver2();
@WithDefault("ns-3.awsdns-03.com")
String defaultNameserver3();
Optional<String> defaultNameserver3();
@WithDefault("ns-4.awsdns-04.co.uk")
String defaultNameserver4();
Optional<String> defaultNameserver4();
/**
* Optional control-plane processing window for VPC association mutations. A positive
@@ -930,7 +930,7 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
resource.setPhysicalId(id);
resource.getAttributes().put("Id", id);
resource.getAttributes().put("NameServers", String.join(",", route53Service.getNameServers()));
resource.getAttributes().put("NameServers", String.join(",", route53Service.getAssignedNameServers(id)));
List<Map<String, String>> tags = parseTags(resolved.path("HostedZoneTags"));
if (!tags.isEmpty()) {
@@ -31,6 +31,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@@ -43,6 +44,9 @@ public class Route53Service implements Resettable {
private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final SecureRandom RANDOM = new SecureRandom();
private static final List<String> DEFAULT_NAME_SERVERS = List.of("ns-1.awsdns-01.org", "ns-2.awsdns-02.net",
"ns-3.awsdns-03.com", "ns-4.awsdns-04.co.uk");
private static final List<String> NAME_SERVER_TLDS = List.of("org", "net", "com", "co.uk");
private static final int MAX_VPC_ASSOCIATIONS_PER_ZONE = 300;
private static final int MAX_VPC_ASSOCIATION_AUTHORIZATIONS = 1000;
@@ -57,6 +61,7 @@ public class Route53Service implements Resettable {
private final StorageBackend<String, Map<String, String>> tagStore;
private final StorageBackend<String, List<VpcAssociation>> vpcAuthorizationStore;
private final List<String> nameServers;
private final List<Boolean> configuredNameServerSlots;
private final String defaultAccountId;
private final RegionResolver regionResolver;
private final Ec2Service ec2Service;
@@ -85,12 +90,20 @@ public class Route53Service implements Resettable {
new TypeReference<Map<String, List<VpcAssociation>>>() {});
EmulatorConfig.Route53ServiceConfig r53 = config.services().route53();
this.nameServers = List.of(
List<Optional<String>> configuredNames = List.of(
r53.defaultNameserver1(),
r53.defaultNameserver2(),
r53.defaultNameserver3(),
r53.defaultNameserver4()
).stream().map(Route53Service::delegationName).toList();
);
List<String> effectiveNames = new ArrayList<>(4);
List<Boolean> configuredSlots = new ArrayList<>(4);
for (int i = 0; i < 4; i++) {
configuredSlots.add(configuredNames.get(i).isPresent());
effectiveNames.add(delegationName(configuredNames.get(i).orElse(DEFAULT_NAME_SERVERS.get(i))));
}
this.configuredNameServerSlots = List.copyOf(configuredSlots);
this.nameServers = List.copyOf(effectiveNames);
this.defaultAccountId = config.defaultAccountId();
this.regionResolver = regionResolver;
this.ec2Service = ec2Service;
@@ -137,7 +150,7 @@ public class Route53Service implements Resettable {
}
String id = "N" + generateZoneId().substring(1);
DelegationSet set = new DelegationSet(id, callerReference,
source == null ? nameServers : assignedNameServers(source));
source == null ? newNameServers(id) : assignedNameServers(source));
delegationSetStore.put(id, set);
if (source != null) {
source.setDelegationSetId(id);
@@ -162,10 +175,25 @@ public class Route53Service implements Resettable {
}
public DelegationSet getDelegationSet(HostedZone zone) {
return zone.getDelegationSetId() == null ? new DelegationSet(null, null, nameServers)
return zone.getDelegationSetId() == null ? new DelegationSet(null, null, assignedNameServers(zone))
: getReusableDelegationSet(zone.getDelegationSetId());
}
private List<String> newNameServers(String id) {
BigInteger value = new BigInteger(id, 36);
List<String> assigned = new ArrayList<>(4);
for (int i = 0; i < 4; i++) {
if (configuredNameServerSlots.get(i)) {
assigned.add(nameServers.get(i));
} else {
int slot = value.shiftRight(i * 18).intValue() & 0x3ffff;
assigned.add(String.format(Locale.ROOT, "ns-%d.awsdns-%02d.%s",
(slot & 0x7ff) + 1, (slot >>> 11) % 100, NAME_SERVER_TLDS.get(i)));
}
}
return List.copyOf(assigned);
}
public long getReusableDelegationSetCount() {
return delegationSetStore.keys().size();
}
@@ -245,7 +273,8 @@ public class Route53Service implements Resettable {
HostedZone zone = new HostedZone(id, normalizedName, callerReference, comment, vpcAssociation);
zone.setOwnerAccountId(callerAccountId);
zone.setDelegationSetId(set == null ? null : set.id());
zone.setDelegationNameServers(set == null ? nameServers : set.nameServers());
zone.setDelegationNameServers(set != null ? set.nameServers()
: vpcAssociation != null ? nameServers : newNameServers(id));
if (vpcAssociation != null) {
vpcAssociation.setOwnerAccountId(callerAccountId);
}
@@ -733,6 +762,10 @@ public class Route53Service implements Resettable {
return nameServers;
}
public List<String> getAssignedNameServers(String hostedZoneId) {
return getDelegationSet(getHostedZone(hostedZoneId)).nameServers();
}
public String getDefaultAccountId() {
return defaultAccountId;
}
@@ -85,7 +85,7 @@ class Route53DnsPacketIntegrationTest {
void delegationNamesAndSoaUseTheStoredZone() throws Exception {
String name = uniqueName() + ".example";
String id = createZone(name);
List<String> expected = route53.getNameServers();
List<String> expected = route53.getAssignedNameServers(id);
byte[] request = request(name, 2);
byte[] response = query(request);
assertHeader(response, 0, true, 4, 0);
@@ -57,7 +57,7 @@ class Route53CfnProvisionerTest {
when(service.createHostedZone(eq("ssm.us-east-1.amazonaws.com"), eq("dns-stack/Zone"),
eq("central endpoint zone"), argThat(sameVpc(vpc)))).thenReturn(
new Route53Service.CreateZoneResult(zone, null));
when(service.getNameServers()).thenReturn(List.of("ns-1.example", "ns-2.example"));
when(service.getAssignedNameServers("Z123456789")).thenReturn(List.of("ns-1.example", "ns-2.example"));
ObjectMapper mapper = new ObjectMapper();
JsonNode props = mapper.createObjectNode()
@@ -81,6 +81,7 @@ class Route53CfnProvisionerTest {
assertEquals("Z123456789", resource.getPhysicalId());
assertEquals(Set.of("Id", "NameServers"), resource.getAttributes().keySet());
assertEquals("Z123456789", resource.getAttributes().get("Id"));
assertEquals("ns-1.example,ns-2.example", resource.getAttributes().get("NameServers"));
verify(service).createHostedZone(eq("ssm.us-east-1.amazonaws.com"), eq("dns-stack/Zone"),
eq("central endpoint zone"), argThat(sameVpc(vpc)));
verify(service, never()).associateVpcWithHostedZone(any(), any(), any());
@@ -98,7 +99,7 @@ class Route53CfnProvisionerTest {
when(service.createHostedZone(eq("ssm.us-east-1.amazonaws.com"), eq("dns-stack/Zone"),
eq(null), argThat(sameVpc(firstVpc)))).thenReturn(
new Route53Service.CreateZoneResult(zone, null));
when(service.getNameServers()).thenReturn(List.of("ns-1.example", "ns-2.example"));
when(service.getAssignedNameServers("Z123456789")).thenReturn(List.of("ns-1.example", "ns-2.example"));
ObjectMapper mapper = new ObjectMapper();
JsonNode props = mapper.createObjectNode().put("Name", "ssm.us-east-1.amazonaws.com");
@@ -172,7 +173,7 @@ class Route53CfnProvisionerTest {
HostedZone zone = new HostedZone("Z68119D97-4AB", "ssm.us-east-1.amazonaws.com.",
"dns-stack/Zone", null, (VpcAssociation) null);
when(service.getHostedZone("Z68119D97-4AB")).thenReturn(zone);
when(service.getNameServers()).thenReturn(List.of("ns-1.example", "ns-2.example"));
when(service.getAssignedNameServers("Z68119D97-4AB")).thenReturn(List.of("ns-1.example", "ns-2.example"));
ObjectMapper mapper = new ObjectMapper();
JsonNode props = mapper.createObjectNode().put("Name", "ssm.us-east-1.amazonaws.com");
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
@@ -205,7 +206,7 @@ class Route53CfnProvisionerTest {
"dns-stack/Zone", null, vpc);
when(service.createHostedZone(eq("ssm.us-east-1.amazonaws.com"), eq("dns-stack/Zone"),
eq(null), argThat(sameVpc(vpc)))).thenReturn(new Route53Service.CreateZoneResult(zone, null));
when(service.getNameServers()).thenReturn(List.of("ns-1.example", "ns-2.example"));
when(service.getAssignedNameServers("Z987654321")).thenReturn(List.of("ns-1.example", "ns-2.example"));
ObjectMapper mapper = new ObjectMapper();
JsonNode props = mapper.createObjectNode().put("Name", "ssm.us-east-1.amazonaws.com");
@@ -278,7 +279,7 @@ class Route53CfnProvisionerTest {
"caller", null, vpc);
when(service.createHostedZone(eq("ssm.us-east-1.amazonaws.com"), eq("dns-stack/Zone"),
eq(null), argThat(sameVpc(vpc)))).thenReturn(new Route53Service.CreateZoneResult(zone, null));
when(service.getNameServers()).thenReturn(List.of("ns-1.example", "ns-2.example"));
when(service.getAssignedNameServers("Z123456789")).thenReturn(List.of("ns-1.example", "ns-2.example"));
ObjectMapper mapper = new ObjectMapper();
JsonNode props = mapper.createObjectNode().put("Name", "ssm.us-east-1.amazonaws.com");
@@ -360,7 +361,7 @@ class Route53CfnProvisionerTest {
"dns-stack/Zone", null, (VpcAssociation) null);
when(service.createHostedZone(eq("ssm.us-east-1.amazonaws.com"), eq("dns-stack/Zone"),
eq(null), eq(null))).thenReturn(new Route53Service.CreateZoneResult(zone, null));
when(service.getNameServers()).thenReturn(List.of("ns-1.example", "ns-2.example"));
when(service.getAssignedNameServers("Z987654321")).thenReturn(List.of("ns-1.example", "ns-2.example"));
RuntimeException tagFailure = new AwsException("ServiceUnavailable", "throttled", 503);
Mockito.doThrow(tagFailure).when(service)
.changeTagsForResource(eq("hostedzone"), eq("Z987654321"), any(), any());
@@ -13,6 +13,7 @@ import software.amazon.awssdk.services.route53.model.ResourceRecord;
import software.amazon.awssdk.services.route53.model.ResourceRecordSet;
import software.amazon.awssdk.services.route53.model.Route53Exception;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@@ -23,6 +24,28 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class Route53DelegationSetHostedZonesIntegrationTest {
@Test
void implicitNameserversUseRoute53NamesForZonesAndStandaloneSets() {
try (Route53Client client = client("741686000017")) {
CreateHostedZoneResponse zone = createZone(client, "generated.example.", null);
DelegationSet set = createSet(client);
try {
List<String> suffixes = List.of("org", "net", "com", "co\\.uk");
for (List<String> names : List.of(zone.delegationSet().nameServers(), set.nameServers())) {
assertEquals(4, names.size());
for (int i = 0; i < 4; i++) {
assertTrue(names.get(i).matches("ns-[1-9][0-9]{0,3}\\.awsdns-[0-9]{2}\\." + suffixes.get(i)));
}
}
assertEquals(zone.delegationSet(), client.getHostedZone(r -> r.id(zone.hostedZone().id()))
.delegationSet());
} finally {
client.deleteHostedZone(r -> r.id(zone.hostedZone().id()));
client.deleteReusableDelegationSet(r -> r.id(set.id()));
}
}
}
@Test
void zonesReuseTheSetInResponsesAndApexRecordsUntilAllZonesAreDeleted() {
try (Route53Client client = client("741686000010")) {
@@ -94,7 +117,7 @@ class Route53DelegationSetHostedZonesIntegrationTest {
void relatedPublicNamesCannotShareNameServersButSiblingDomainsCan() {
try (Route53Client client = client("741686000013")) {
DelegationSet set = createSet(client);
String zone = createZone(client, "a.example.com.", null).hostedZone().id();
String zone = createZone(client, "a.example.com.", set.id()).hostedZone().id();
try {
for (String name : List.of("A.EXAMPLE.COM", "child.a.example.com.", "example.com.")) {
assertError("ConflictingDomainExists", () -> createZone(client, name, set.id()));
@@ -108,6 +131,56 @@ class Route53DelegationSetHostedZonesIntegrationTest {
}
}
@Test
void relatedPublicNamesCanUseIndependentNameServers() {
try (Route53Client client = client("741686000015")) {
CreateHostedZoneResponse parent = createZone(client, "parent.example.com.", null);
DelegationSet firstSet = null;
DelegationSet secondSet = null;
String child = null;
String grandchild = null;
try {
firstSet = createDisjointSet(client, parent.delegationSet().nameServers());
child = createZone(client, "child.parent.example.com.", firstSet.id()).hostedZone().id();
List<String> used = new ArrayList<>(parent.delegationSet().nameServers());
used.addAll(firstSet.nameServers());
secondSet = createDisjointSet(client, used);
grandchild = createZone(client, "grandchild.child.parent.example.com.", secondSet.id())
.hostedZone().id();
} finally {
if (grandchild != null) {
String id = grandchild;
client.deleteHostedZone(r -> r.id(id));
}
if (child != null) {
String id = child;
client.deleteHostedZone(r -> r.id(id));
}
client.deleteHostedZone(r -> r.id(parent.hostedZone().id()));
if (firstSet != null) {
DelegationSet set = firstSet;
client.deleteReusableDelegationSet(r -> r.id(set.id()));
}
if (secondSet != null) {
DelegationSet set = secondSet;
client.deleteReusableDelegationSet(r -> r.id(set.id()));
}
}
}
}
private DelegationSet createDisjointSet(Route53Client client, List<String> usedNames) {
for (int attempt = 0; attempt < 100; attempt++) {
DelegationSet set = createSet(client);
if (set.nameServers().stream().noneMatch(candidate -> usedNames.stream()
.anyMatch(name -> name.equalsIgnoreCase(candidate)))) {
return set;
}
client.deleteReusableDelegationSet(r -> r.id(set.id()));
}
throw new AssertionError("Unable to allocate a disjoint reusable delegation set after 100 attempts");
}
@Test
void privateZonesCannotUseOrPromoteAReusableSet() {
try (Route53Client client = client("741686000014")) {
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.route53.model.DelegationSet;
import io.github.hectorvent.floci.services.route53.model.HostedZone;
import io.github.hectorvent.floci.services.route53.model.ResourceRecord;
import io.github.hectorvent.floci.services.route53.model.ResourceRecordSet;
import io.github.hectorvent.floci.services.route53.model.VpcAssociation;
import io.github.hectorvent.floci.testing.StorageFactories;
import jakarta.enterprise.inject.Instance;
import org.junit.jupiter.api.Test;
@@ -24,6 +25,7 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
@@ -32,6 +34,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -111,20 +114,112 @@ class Route53DelegationSetsServiceTest {
}
@Test
void legacyPublicZonesParticipateInConflictsBeforeTheirFirstRead() throws Exception {
void legacyPublicZonesUsePersistedNameServersForConflictChecks() throws Exception {
seedLegacyZone();
try (Fixture fixture = fixture("persistent", "original")) {
DelegationSet set = fixture.service().createReusableDelegationSet("set", null);
DelegationSet set = fixture.service().createReusableDelegationSet("set", "ZLEGACY");
assertError("ConflictingDomainExists", () -> fixture.service()
.createHostedZone("child.legacy.example.", "child", null, null, set.id()));
}
}
@Test
void independentZonesAndReusableSetsHaveDistinctNameServers() {
try (Fixture fixture = fixture("memory", null)) {
Route53Service service = fixture.service();
HostedZone firstZone = service.createHostedZone("first.example.", "first-zone", null, null).zone();
HostedZone secondZone = service.createHostedZone("second.example.", "second-zone", null, null).zone();
DelegationSet firstSet = service.createReusableDelegationSet("first-set", null);
DelegationSet secondSet = service.createReusableDelegationSet("second-set", null);
List<List<String>> assignments = List.of(service.getDelegationSet(firstZone).nameServers(),
service.getDelegationSet(secondZone).nameServers(), firstSet.nameServers(), secondSet.nameServers());
for (int i = 0; i < assignments.size(); i++) {
assertEquals(4, assignments.get(i).size());
for (int j = i + 1; j < assignments.size(); j++) {
assertNotEquals(assignments.get(i), assignments.get(j));
}
}
}
}
@Test
void explicitlyConfiguredNameServersRemainUnchangedForZonesAndSets() {
try (Fixture fixture = fixture("memory", "configured")) {
Route53Service service = fixture.service();
String zoneId = service.createHostedZone("configured.example.", "configured-zone", null, null)
.zone().getId();
DelegationSet set = service.createReusableDelegationSet("configured-set", null);
List<String> configured = List.of("configured-1.test", "configured-2.test",
"configured-3.test", "configured-4.test");
assertEquals(configured, service.getAssignedNameServers(zoneId));
assertEquals(configured, set.nameServers());
}
}
@Test
void explicitlyConfiguredLiteralDefaultsRemainUnchanged() {
try (Fixture fixture = fixture("memory", "literal-defaults")) {
Route53Service service = fixture.service();
String zoneId = service.createHostedZone("literal.example.", "literal-zone", null, null)
.zone().getId();
DelegationSet set = service.createReusableDelegationSet("literal-set", null);
List<String> configured = List.of("ns-1.awsdns-01.org", "ns-2.awsdns-02.net",
"ns-3.awsdns-03.com", "ns-4.awsdns-04.co.uk");
assertEquals(configured, service.getAssignedNameServers(zoneId));
assertEquals(configured, set.nameServers());
}
}
@Test
void mixedConfigurationOverridesOnlyItsExplicitSlot() {
try (Fixture fixture = fixture("memory", "mixed")) {
Route53Service service = fixture.service();
String publicId = service.createHostedZone("mixed.example.", "mixed-public", null, null)
.zone().getId();
String privateId = service.createHostedZone("private.mixed.example.", "mixed-private", null,
new VpcAssociation("vpc-mixed", "us-east-1")).zone().getId();
DelegationSet set = service.createReusableDelegationSet("mixed-set", null);
List<String> publicNames = service.getAssignedNameServers(publicId);
assertEquals("configured-1.test", publicNames.getFirst());
assertEquals("configured-1.test", set.nameServers().getFirst());
for (int i = 1; i < 4; i++) {
assertTrue(publicNames.get(i).matches("ns-[1-9][0-9]{0,3}\\.awsdns-[0-9]{2}\\..+"));
assertTrue(set.nameServers().get(i).matches("ns-[1-9][0-9]{0,3}\\.awsdns-[0-9]{2}\\..+"));
}
assertEquals(List.of("configured-1.test", "ns-2.awsdns-02.net", "ns-3.awsdns-03.com",
"ns-4.awsdns-04.co.uk"), service.getAssignedNameServers(privateId));
}
}
@Test
void ordinaryZoneAssignmentSurvivesStorageReloadAndConfigurationChange() {
String zoneId;
List<String> original;
try (Fixture fixture = fixture("persistent", "original")) {
zoneId = fixture.service().createHostedZone("stable.example.", "stable", null, null).zone().getId();
original = fixture.service().getAssignedNameServers(zoneId);
}
try (Fixture reloaded = fixture("persistent", "changed")) {
Route53Service service = reloaded.service();
assertEquals(original, service.getAssignedNameServers(zoneId));
List<ResourceRecordSet> records = service.listResourceRecordSets(zoneId, null, null, 100);
assertEquals(original.stream().map(name -> name + ".").toList(), records.stream()
.filter(record -> "NS".equals(record.getType())).findFirst().orElseThrow().getRecords().stream()
.map(ResourceRecord::getValue).toList());
assertTrue(records.stream().filter(record -> "SOA".equals(record.getType())).findFirst().orElseThrow()
.getRecords().getFirst().getValue().startsWith(original.getFirst() + ". "));
String next = service.createHostedZone("new.example.", "new", null, null).zone().getId();
assertTrue(service.getAssignedNameServers(next).stream().noneMatch(original::contains));
}
}
@Test
void promotionAfterConfigurationChangePreservesTheOriginalZoneAssignment() {
String zoneId;
List<String> original;
try (Fixture fixture = fixture("persistent", "original")) {
zoneId = fixture.service().createHostedZone("original.example.", "original", null, null).zone().getId();
original = fixture.service().getAssignedNameServers(zoneId);
ResourceRecordSet ns = fixture.service().listResourceRecordSets(zoneId, null, null, 100).stream()
.filter(record -> "NS".equals(record.getType())).findFirst().orElseThrow();
ns.setRecords(List.of(new ResourceRecord("custom.nameserver.test.")));
@@ -132,8 +227,7 @@ class Route53DelegationSetsServiceTest {
}
try (Fixture fixture = fixture("persistent", "changed")) {
DelegationSet set = fixture.service().createReusableDelegationSet("promoted", zoneId);
assertEquals(List.of("original-1.test", "original-2.test", "original-3.test", "original-4.test"),
set.nameServers());
assertEquals(original, set.nameServers());
}
}
@@ -285,10 +379,27 @@ class Route53DelegationSetsServiceTest {
private EmulatorConfig config(String prefix) {
EmulatorConfig config = StorageFactories.config(directory, ACCOUNT);
when(config.services().route53().defaultNameserver1()).thenReturn(prefix + "-1.test.");
when(config.services().route53().defaultNameserver2()).thenReturn(prefix + "-2.test.");
when(config.services().route53().defaultNameserver3()).thenReturn(prefix + "-3.test.");
when(config.services().route53().defaultNameserver4()).thenReturn(prefix + "-4.test.");
if (prefix == null) {
when(config.services().route53().defaultNameserver1()).thenReturn(Optional.empty());
when(config.services().route53().defaultNameserver2()).thenReturn(Optional.empty());
when(config.services().route53().defaultNameserver3()).thenReturn(Optional.empty());
when(config.services().route53().defaultNameserver4()).thenReturn(Optional.empty());
} else if ("mixed".equals(prefix)) {
when(config.services().route53().defaultNameserver1()).thenReturn(Optional.of("configured-1.test."));
when(config.services().route53().defaultNameserver2()).thenReturn(Optional.empty());
when(config.services().route53().defaultNameserver3()).thenReturn(Optional.empty());
when(config.services().route53().defaultNameserver4()).thenReturn(Optional.empty());
} else if ("literal-defaults".equals(prefix)) {
when(config.services().route53().defaultNameserver1()).thenReturn(Optional.of("ns-1.awsdns-01.org"));
when(config.services().route53().defaultNameserver2()).thenReturn(Optional.of("ns-2.awsdns-02.net"));
when(config.services().route53().defaultNameserver3()).thenReturn(Optional.of("ns-3.awsdns-03.com"));
when(config.services().route53().defaultNameserver4()).thenReturn(Optional.of("ns-4.awsdns-04.co.uk"));
} else {
when(config.services().route53().defaultNameserver1()).thenReturn(Optional.of(prefix + "-1.test."));
when(config.services().route53().defaultNameserver2()).thenReturn(Optional.of(prefix + "-2.test."));
when(config.services().route53().defaultNameserver3()).thenReturn(Optional.of(prefix + "-3.test."));
when(config.services().route53().defaultNameserver4()).thenReturn(Optional.of(prefix + "-4.test."));
}
return config;
}
@@ -6,6 +6,8 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
@@ -17,6 +19,7 @@ class Route53IntegrationTest {
private static final String XML = "application/xml";
private static String zoneId;
private static List<String> zoneNameServers;
private static String changeId;
private static String healthCheckId;
@@ -52,11 +55,13 @@ class Route53IntegrationTest {
.body("CreateHostedZoneResponse.ChangeInfo.Status", equalTo("INSYNC"))
.body("CreateHostedZoneResponse.ChangeInfo.Id", startsWith("/change/C"))
.body(not(containsString("<VPC>")))
.body(containsString("ns-1.awsdns-01.org"))
.body(containsString("<NameServer>ns-"))
.extract().header("Location");
// Location is an absolute URL: http://localhost:PORT/2013-04-01/hostedzone/ZXXX
zoneId = locationHeader.substring(locationHeader.lastIndexOf('/') + 1);
zoneNameServers = given().when().get("/2013-04-01/hostedzone/" + zoneId).then().extract()
.xmlPath().getList("GetHostedZoneResponse.DelegationSet.NameServers.NameServer");
}
@Test
@@ -71,7 +76,7 @@ class Route53IntegrationTest {
.body("GetHostedZoneResponse.HostedZone.Name", equalTo("example.com."))
.body("GetHostedZoneResponse.HostedZone.Config.PrivateZone", equalTo("false"))
.body(not(containsString("<VPCs>")))
.body(containsString("ns-1.awsdns-01.org"));
.body(containsString(zoneNameServers.getFirst()));
}
@Test
@@ -163,6 +168,8 @@ class Route53IntegrationTest {
@Test
@Order(7)
void listResourceRecordSets_autoCreatedSOAandNS() {
String firstNameserver = zoneNameServers.getFirst() + ".";
String fourthNameserver = zoneNameServers.get(3) + ".";
String body = given()
.when().get("/2013-04-01/hostedzone/" + zoneId + "/rrset")
.then()
@@ -174,8 +181,8 @@ class Route53IntegrationTest {
assertThat(body, containsString("<Type>SOA</Type>"));
assertThat(body, containsString("<Type>NS</Type>"));
assertThat(body, containsString(
"<Value>ns-1.awsdns-01.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400</Value>"));
assertThat(body, containsString("<Value>ns-4.awsdns-04.co.uk.</Value>"));
"<Value>" + firstNameserver + " awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400</Value>"));
assertThat(body, containsString("<Value>" + fourthNameserver + "</Value>"));
}
@Test
@@ -249,14 +256,14 @@ class Route53IntegrationTest {
<Type>SOA</Type>
<TTL>900</TTL>
<ResourceRecords>
<ResourceRecord><Value>ns-1.awsdns-01.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400</Value></ResourceRecord>
<ResourceRecord><Value>%s awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400</Value></ResourceRecord>
</ResourceRecords>
</ResourceRecordSet>
</Change>
</Changes>
</ChangeBatch>
</ChangeResourceRecordSetsRequest>
""";
""".formatted(zoneNameServers.getFirst() + ".");
given()
.contentType(XML)
@@ -0,0 +1,29 @@
package io.github.hectorvent.floci.services.route53;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.smallrye.config.EnvConfigSource;
import io.smallrye.config.SmallRyeConfigBuilder;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
class Route53NameserverConfigTest {
@Test
void explicitDefaultLiteralIsDistinctFromAnUnsetNameserver() {
assertEquals(Optional.empty(), config(Map.of()).services().route53().defaultNameserver1());
assertEquals(Optional.of("ns-1.awsdns-01.org"), config(Map.of(
"FLOCI_SERVICES_ROUTE53_DEFAULT_NAMESERVER1", "ns-1.awsdns-01.org"))
.services().route53().defaultNameserver1());
}
private static EmulatorConfig config(Map<String, String> environment) {
return new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withSources(new EnvConfigSource(environment, 300))
.withMapping(EmulatorConfig.class)
.build().getConfigMapping(EmulatorConfig.class);
}
}
@@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
@@ -63,10 +64,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" + 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.defaultNameserver1()).thenReturn(Optional.of("ns-1.awsdns-00.com" + nameserverSuffix));
Mockito.when(route53Config.defaultNameserver2()).thenReturn(Optional.of("ns-2.awsdns-00.net" + nameserverSuffix));
Mockito.when(route53Config.defaultNameserver3()).thenReturn(Optional.of("ns-3.awsdns-00.org" + nameserverSuffix));
Mockito.when(route53Config.defaultNameserver4()).thenReturn(Optional.of("ns-4.awsdns-00.co.uk" + nameserverSuffix));
Mockito.when(route53Config.vpcAssociationControlPlaneDelayMs()).thenReturn(0L);
Ec2Service ec2Service = Mockito.mock(Ec2Service.class);
@@ -116,35 +117,45 @@ class Route53ServiceTest {
@ParameterizedTest(name = "configured names ending in {0}")
@ValueSource(strings = {"", ".", " ", ".\t", ".\u2003"})
void defaultApexRecordsUseAbsoluteNamesAndTheDelegationSetDoesNot(String nameserverSuffix) {
void configuredApexRecordsUseAbsoluteNamesAndTheDelegationSetDoesNot(String nameserverSuffix) {
Route53Service service = newService(nameserverSuffix);
String zoneId = service.createHostedZone("authority.example", "authority", null, null).zone().getId();
List<ResourceRecordSet> records = service.listResourceRecordSets(zoneId, null, null, 100);
List<String> assigned = service.getAssignedNameServers(zoneId);
assertAll(
() -> assertEquals(List.of("ns-1.awsdns-00.com. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"),
() -> assertEquals(List.of(assigned.getFirst() + ". 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(assigned.stream().map(name -> name + ".").toList(), 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"));
"ns-4.awsdns-00.co.uk"), assigned, "configured names"),
() -> assertEquals(assigned, service.getNameServers(), "configured bases"));
}
@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);
String zoneId = service.createHostedZone("authority.example", "authority", null, null).zone().getId();
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.")),
assertEquals(service.getAssignedNameServers(zoneId).stream().map(name -> new DnsRecord.Ns(name + ".")).toList(),
answer.records(), "answers");
}
@Test
void privateZonesKeepTheirConfiguredNameServers() {
Route53Service service = newService();
String zoneId = service.createHostedZone("private.example", "private", null,
new VpcAssociation("vpc-123", "us-east-1")).zone().getId();
assertEquals(service.getNameServers(), service.getAssignedNameServers(zoneId));
assertEquals(service.getNameServers().stream().map(name -> name + ".").toList(),
valuesOf(service.listResourceRecordSets(zoneId, null, null, 100), "NS"));
}
@Test
void rejectedBatchDoesNotRenameSharedStoredRecord() {
Route53Service service = newService();