fix(route53): paginate hosted zones by name (#6997)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-30 07:23:30 -04:00
1 parent 07f2d62c17
commit fb1d2d1fdb
5 files changed
+195 -17

No files matched your search

@@ -25,6 +25,7 @@ import jakarta.ws.rs.core.Response;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import java.net.URI;
import java.time.Instant;
import java.util.ArrayList;
@@ -169,24 +170,33 @@ public class Route53Controller {
@GET
@Path("/hostedzonesbyname")
public Response listHostedZonesByName(@QueryParam("dnsname") String dnsName,
@QueryParam("hostedzoneid") String hostedZoneId,
@QueryParam("maxitems") @DefaultValue("100") int maxItems) {
try {
List<HostedZone> zones = service.listHostedZonesByName(dnsName, maxItems);
long total = service.getHostedZoneCount();
boolean truncated = zones.size() == maxItems && zones.size() < total;
if (maxItems <= 0) {
throw new AwsException("InvalidInput", "MaxItems must be a positive integer.", 400);
}
Route53Pagination.Page<HostedZone> page = service.pageHostedZonesByName(dnsName, hostedZoneId, maxItems);
XmlBuilder xml = new XmlBuilder()
.start("ListHostedZonesByNameResponse", NS)
.start("HostedZones");
for (HostedZone zone : zones) {
for (HostedZone zone : page.items()) {
xml.raw(xmlHostedZone(zone));
}
xml.end("HostedZones")
.elem("IsTruncated", String.valueOf(truncated))
.elem("IsTruncated", String.valueOf(page.isTruncated()))
.elem("MaxItems", String.valueOf(maxItems));
if (dnsName != null && !dnsName.isEmpty()) {
xml.elem("DNSName", dnsName);
}
if (hostedZoneId != null && !hostedZoneId.isEmpty()) {
xml.elem("HostedZoneId", hostedZoneId);
}
if (page.isTruncated()) {
xml.elem("NextDNSName", page.nextItem().getName())
.elem("NextHostedZoneId", page.nextMarker());
}
xml.end("ListHostedZonesByNameResponse");
return Response.ok(xml.build(), XML).build();
@@ -21,13 +21,14 @@ final class Route53Pagination {
int remaining = sortedItems.size() - start;
if (pageSize <= 0 || remaining <= pageSize) {
return new Page<>(sortedItems.subList(start, sortedItems.size()), null);
return new Page<>(sortedItems.subList(start, sortedItems.size()), null, null);
}
int end = start + pageSize;
return new Page<>(sortedItems.subList(start, end), id.apply(sortedItems.get(end)));
T nextItem = sortedItems.get(end);
return new Page<>(sortedItems.subList(start, end), id.apply(nextItem), nextItem);
}
record Page<T>(List<T> items, String nextMarker) {
record Page<T>(List<T> items, String nextMarker, T nextItem) {
boolean isTruncated() {
return nextMarker != null;
}
@@ -176,22 +176,52 @@ public class Route53Service implements Resettable {
}
public List<HostedZone> listHostedZonesByName(String dnsName, int maxItems) {
return pageHostedZonesByName(dnsName, null, maxItems).items();
}
Route53Pagination.Page<HostedZone> pageHostedZonesByName(String dnsName, String hostedZoneId, int maxItems) {
List<HostedZone> all = new ArrayList<>(zoneStore.scan(k -> true));
all.sort((a, b) -> a.getName().compareTo(b.getName()));
all.sort(Comparator.comparing((HostedZone zone) -> reversedDnsName(zone.getName()))
.thenComparing(HostedZone::getId));
for (HostedZone zone : all) {
zone.setResourceRecordSetCount(recordCount(zone.getId()));
}
if (dnsName != null && !dnsName.isEmpty()) {
String normalized = normalizeName(dnsName);
all = all.stream()
.filter(z -> z.getName().compareTo(normalized) >= 0)
.toList();
all = new ArrayList<>(all);
String startName = reversedDnsName(normalizeName(dnsName));
all.removeIf(zone -> {
int comparison = reversedDnsName(zone.getName()).compareTo(startName);
return comparison < 0 || (comparison == 0 && hostedZoneId != null && !hostedZoneId.isEmpty()
&& zone.getId().compareTo(hostedZoneId) < 0);
});
}
if (maxItems > 0 && all.size() > maxItems) {
return all.subList(0, maxItems);
int pageSize = maxItems <= 0 ? maxItems : Math.min(maxItems, 100);
return Route53Pagination.page(all, null, pageSize, HostedZone::getId);
}
static String reversedDnsName(String name) {
String normalized = normalizeName(name);
List<String> labels = new ArrayList<>();
int labelStart = 0;
boolean escaped = false;
for (int i = 0; i < normalized.length(); i++) {
char character = normalized.charAt(i);
if (character == '.' && !escaped) {
if (i > labelStart) {
labels.add(normalized.substring(labelStart, i));
}
labelStart = i + 1;
}
if (character == '\\' && !escaped) {
escaped = true;
} else {
escaped = false;
}
}
return all;
StringBuilder reversed = new StringBuilder(normalized.length());
for (int i = labels.size() - 1; i >= 0; i--) {
reversed.append(labels.get(i)).append('.');
}
return reversed.toString();
}
public long getHostedZoneCount() {
@@ -0,0 +1,97 @@
package io.github.hectorvent.floci.services.route53;
import io.github.hectorvent.floci.testing.AwsTestClients;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.xml.XmlPath;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.route53.Route53Client;
import software.amazon.awssdk.services.route53.model.ListHostedZonesByNameResponse;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class Route53HostedZonesByNamePaginationIntegrationTest {
@Test
void awsSdkContinuesDuplicateNamesWithBothCursorFields() {
String name = "byname-sdk-" + UUID.randomUUID() + ".example.test.";
String firstId = Route53HostedZoneTestSupport.createZone(name, "byname-sdk-first-" + name);
String secondId = null;
try {
secondId = Route53HostedZoneTestSupport.createZone(name, "byname-sdk-second-" + name);
try (Route53Client client = AwsTestClients.create(Route53Client.builder(), "test", Region.US_EAST_1)) {
ListHostedZonesByNameResponse firstPage = client.listHostedZonesByName(request -> request
.dnsName(name).maxItems("1"));
assertEquals(1, firstPage.hostedZones().size());
String returnedId = firstPage.hostedZones().getFirst().id();
assertTrue(returnedId.equals("/hostedzone/" + firstId)
|| returnedId.equals("/hostedzone/" + secondId));
String expectedNextId = returnedId.equals("/hostedzone/" + firstId) ? secondId : firstId;
assertTrue(firstPage.isTruncated());
assertEquals(name, firstPage.nextDNSName());
assertEquals(expectedNextId, firstPage.nextHostedZoneId());
ListHostedZonesByNameResponse secondPage = client.listHostedZonesByName(request -> request
.dnsName(firstPage.nextDNSName())
.hostedZoneId(firstPage.nextHostedZoneId())
.maxItems("1"));
assertEquals(1, secondPage.hostedZones().size());
assertEquals("/hostedzone/" + expectedNextId, secondPage.hostedZones().getFirst().id());
assertEquals(name, secondPage.dnsName());
assertEquals(expectedNextId, secondPage.hostedZoneId());
}
} finally {
if (secondId != null) {
given().when().delete("/2013-04-01/hostedzone/" + secondId).then().statusCode(200);
}
given().when().delete("/2013-04-01/hostedzone/" + firstId).then().statusCode(200);
}
}
@Test
void duplicateNamesContinueFromTheNextZoneId() {
String name = "byname-" + UUID.randomUUID() + ".example.test.";
String firstId = Route53HostedZoneTestSupport.createZone(name, "byname-first-" + name);
String secondId = null;
try {
secondId = Route53HostedZoneTestSupport.createZone(name, "byname-second-" + name);
XmlPath firstPage = given()
.queryParam("dnsname", name)
.queryParam("maxitems", 1)
.when().get("/2013-04-01/hostedzonesbyname")
.then().statusCode(200).extract().xmlPath();
String returnedId = firstPage.getString("ListHostedZonesByNameResponse.HostedZones.HostedZone.Id");
String expectedNextId = returnedId.equals("/hostedzone/" + firstId) ? secondId : firstId;
assertTrue(firstPage.getBoolean("ListHostedZonesByNameResponse.IsTruncated"));
assertEquals(name, firstPage.getString("ListHostedZonesByNameResponse.NextDNSName"));
assertEquals(expectedNextId,
firstPage.getString("ListHostedZonesByNameResponse.NextHostedZoneId"));
XmlPath secondPage = given()
.queryParam("dnsname", firstPage.getString("ListHostedZonesByNameResponse.NextDNSName"))
.queryParam("hostedzoneid", firstPage.getString("ListHostedZonesByNameResponse.NextHostedZoneId"))
.queryParam("maxitems", 1)
.when().get("/2013-04-01/hostedzonesbyname")
.then().statusCode(200).extract().xmlPath();
assertEquals("/hostedzone/" + expectedNextId,
secondPage.getString("ListHostedZonesByNameResponse.HostedZones.HostedZone.Id"));
assertEquals(name, secondPage.getString("ListHostedZonesByNameResponse.DNSName"));
assertEquals(expectedNextId, secondPage.getString("ListHostedZonesByNameResponse.HostedZoneId"));
given().queryParam("maxitems", 0)
.when().get("/2013-04-01/hostedzonesbyname")
.then().statusCode(400);
} finally {
if (secondId != null) {
given().when().delete("/2013-04-01/hostedzone/" + secondId).then().statusCode(200);
}
given().when().delete("/2013-04-01/hostedzone/" + firstId).then().statusCode(200);
}
}
}
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.route53.model.AliasTarget;
import io.github.hectorvent.floci.services.route53.model.HealthCheck;
import io.github.hectorvent.floci.services.route53.model.HealthCheckConfig;
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;
@@ -74,6 +75,45 @@ class Route53ServiceTest {
return new Route53Service(storageFactory, config, regionResolver, ec2Service);
}
@Test
void hostedZonesByNameUseReversedLabelsAndFilteredPageBoundary() {
Route53Service service = newService();
String firstId = service.createHostedZone("z.example.com.", "byname-first", null, null).zone().getId();
String lastId = service.createHostedZone("a.other.com.", "byname-last", null, null).zone().getId();
assertEquals(List.of(firstId, lastId), service.listHostedZonesByName(null, 0).stream()
.map(HostedZone::getId).toList());
Route53Pagination.Page<HostedZone> lastPage = service.pageHostedZonesByName("a.other.com.", null, 1);
assertEquals(List.of(lastId), lastPage.items().stream().map(HostedZone::getId).toList());
assertFalse(lastPage.isTruncated());
}
@Test
void reversedDnsNameKeepsAnEscapedDotInsideItsLabel() {
assertEquals("com.example.a\\.b.", Route53Service.reversedDnsName("a\\.b.example.com."));
}
@Test
void hostedZonesByNameContinueAcrossNamesAndLimitPagesToOneHundred() {
Route53Service service = newService();
for (int i = 0; i < 101; i++) {
service.createHostedZone("zone-" + String.format("%03d", i) + ".example.test.",
"byname-cap-" + i, null, null);
}
assertEquals(101, service.listHostedZonesByName(null, 0).size());
Route53Pagination.Page<HostedZone> firstPage = service.pageHostedZonesByName(null, null, 200);
assertEquals(100, firstPage.items().size());
assertTrue(firstPage.isTruncated());
HostedZone nextZone = firstPage.nextItem();
Route53Pagination.Page<HostedZone> secondPage = service.pageHostedZonesByName(
nextZone.getName(), nextZone.getId(), 200);
assertEquals(List.of(nextZone.getId()), secondPage.items().stream().map(HostedZone::getId).toList());
assertFalse(secondPage.isTruncated());
}
@ParameterizedTest(name = "configured names ending in {0}")
@ValueSource(strings = {"", ".", " ", ".\t", ".\u2003"})
void defaultApexRecordsUseAbsoluteNamesAndTheDelegationSetDoesNot(String nameserverSuffix) {