From 3aa8149d089a632ecdde80fa4536ff4a7f7eab59 Mon Sep 17 00:00:00 2001 From: Aniket Shukla Date: Sun, 25 Oct 2026 23:08:39 +0530 Subject: [PATCH] fix(globalaccelerator): paginate accelerator listings (#6522) * fix(globalaccelerator): paginate accelerator listings * fix(globalaccelerator): validate pagination field types --- docs/services/globalaccelerator.md | 8 +- pom.xml | 6 + .../GlobalAcceleratorJsonHandler.java | 22 +- .../GlobalAcceleratorService.java | 15 ++ ...lAcceleratorPaginationIntegrationTest.java | 212 ++++++++++++++++++ 5 files changed, 259 insertions(+), 4 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorPaginationIntegrationTest.java diff --git a/docs/services/globalaccelerator.md b/docs/services/globalaccelerator.md index 64642fd5df..27310074a9 100644 --- a/docs/services/globalaccelerator.md +++ b/docs/services/globalaccelerator.md @@ -125,12 +125,14 @@ These operations return `UnknownOperationException` rather than a stub success: list, so a BYOIP address can be pinned without provisioning a pool. - Cross-account attachments (`CreateCrossAccountAttachment` and friends) -Two more gaps are worth calling out: +`ListAccelerators` supports `MaxResults` (1–100, default 10) and `NextToken`. + +Other gaps are worth calling out: - `IdempotencyToken` is accepted but not deduplicated, so repeating a create produces a second resource. -- The list operations ignore `MaxResults` and return every resource in one page with no - `NextToken`. A paginator terminates after the first call. +- The other list operations ignore `MaxResults` and return every resource in one page + with no `NextToken`. - `AddEndpoints` returns the endpoint group's whole endpoint list rather than only the endpoints the call added. diff --git a/pom.xml b/pom.xml index a59b4b9818..649f06ea9a 100644 --- a/pom.xml +++ b/pom.xml @@ -348,6 +348,12 @@ ${aws.sdk.version} test + + software.amazon.awssdk + globalaccelerator + ${aws.sdk.version} + test + software.amazon.awssdk batch diff --git a/src/main/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorJsonHandler.java index 1d69c7967e..3a96ccc340 100644 --- a/src/main/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorJsonHandler.java @@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.PaginatedResult; +import io.github.hectorvent.floci.services.globalaccelerator.model.Accelerator; import io.github.hectorvent.floci.services.globalaccelerator.model.EndpointGroup; import io.github.hectorvent.floci.services.globalaccelerator.model.PortOverride; import io.github.hectorvent.floci.services.globalaccelerator.model.PortRange; @@ -57,7 +59,25 @@ public class GlobalAcceleratorJsonHandler { service.deleteAccelerator(text(request, "AcceleratorArn")); yield Response.ok(Map.of()).build(); } - case "ListAccelerators" -> Response.ok(Map.of("Accelerators", service.listAccelerators())).build(); + case "ListAccelerators" -> { + JsonNode maxResults = request.get("MaxResults"); + if (maxResults != null && !maxResults.isNull() + && (!maxResults.isIntegralNumber() || !maxResults.canConvertToInt())) { + throw new AwsException("InvalidArgumentException", "MaxResults must be between 1 and 100", 400); + } + JsonNode nextToken = request.get("NextToken"); + if (nextToken != null && !nextToken.isNull() && !nextToken.isTextual()) { + throw new AwsException("InvalidNextTokenException", "Invalid NextToken", 400); + } + PaginatedResult page = service.listAccelerators( + integer(request, "MaxResults"), text(request, "NextToken")); + Map result = new LinkedHashMap<>(); + result.put("Accelerators", page.items()); + if (page.nextToken() != null) { + result.put("NextToken", page.nextToken()); + } + yield Response.ok(result).build(); + } case "DescribeAcceleratorAttributes" -> Response.ok(Map.of("AcceleratorAttributes", service.describeAcceleratorAttributes(text(request, "AcceleratorArn")))).build(); case "UpdateAcceleratorAttributes" -> Response.ok(Map.of("AcceleratorAttributes", diff --git a/src/main/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorService.java b/src/main/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorService.java index 7fd0a2c81a..c168858d25 100644 --- a/src/main/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorService.java +++ b/src/main/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorService.java @@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.globalaccelerator; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.PaginatedResult; +import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -188,6 +190,19 @@ public class GlobalAcceleratorService { return accelerators.scan(key -> true); } + public PaginatedResult listAccelerators(Integer maxResults, String nextToken) { + int limit = maxResults != null ? maxResults : 10; + if (limit < 1 || limit > 100) { + throw new AwsException("InvalidArgumentException", "MaxResults must be between 1 and 100", 400); + } + if (nextToken != null && nextToken.length() > 255) { + throw new AwsException("InvalidNextTokenException", "Invalid NextToken", 400); + } + String namespace = "ListAccelerators:" + regionResolver.getPartition() + ":" + regionResolver.getAccountId(); + return Pagination.paginate(listAccelerators(), Accelerator::getAcceleratorArn, limit, nextToken, namespace, + token -> new AwsException("InvalidNextTokenException", "Invalid NextToken", 400)); + } + public AcceleratorAttributes describeAcceleratorAttributes(String acceleratorArn) { describeAccelerator(acceleratorArn); return acceleratorAttributes.get(acceleratorArn).orElseGet(GlobalAcceleratorService::defaultAttributes); diff --git a/src/test/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorPaginationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorPaginationIntegrationTest.java new file mode 100644 index 0000000000..231e4b1f55 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorPaginationIntegrationTest.java @@ -0,0 +1,212 @@ +package io.github.hectorvent.floci.services.globalaccelerator; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.common.AwsArnUtils; +import io.github.hectorvent.floci.core.common.AwsRegions; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.globalaccelerator.model.Accelerator; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.RestAssured; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.globalaccelerator.GlobalAcceleratorClient; +import software.amazon.awssdk.services.globalaccelerator.model.InvalidArgumentException; +import software.amazon.awssdk.services.globalaccelerator.model.InvalidNextTokenException; +import software.amazon.awssdk.services.globalaccelerator.model.ListAcceleratorsResponse; + +import java.net.URI; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class GlobalAcceleratorPaginationIntegrationTest { + + private static final String ACCOUNT = "741649500001"; + private static final String OTHER_ACCOUNT = "741649500002"; + private static final Region REGION = Region.US_WEST_2; + + @Inject + StorageFactory storageFactory; + + private AccountAwareStorageBackend accelerators; + private final List seeded = new ArrayList<>(); + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @BeforeEach + void setUp() { + accelerators = storageFactory.create("globalaccelerator", "globalaccelerator-accelerators.json", + new TypeReference>() {}); + } + + @AfterEach + void cleanUp() { + seeded.forEach(item -> accelerators.deleteForAccount(item.account(), item.arn())); + } + + @Test + void sdkPagesTheCallingAccountsGlobalAcceleratorsAcrossSigningRegions() { + List arns = seed(ACCOUNT, 3).stream().sorted().toList(); + List foreignArns = seed(OTHER_ACCOUNT, 1); + try (GlobalAcceleratorClient client = client(ACCOUNT, REGION); + GlobalAcceleratorClient otherRegion = client(ACCOUNT, Region.US_EAST_1); + GlobalAcceleratorClient otherAccount = client(OTHER_ACCOUNT, REGION)) { + ListAcceleratorsResponse first = client.listAccelerators(request -> request.maxResults(1)); + assertEquals(List.of(arns.get(0)), first.accelerators().stream() + .map(accelerator -> accelerator.acceleratorArn()).toList()); + assertNotNull(first.nextToken()); + ListAcceleratorsResponse second = otherRegion.listAccelerators(request -> request + .maxResults(1).nextToken(first.nextToken())); + assertEquals(List.of(arns.get(1)), second.accelerators().stream() + .map(accelerator -> accelerator.acceleratorArn()).toList()); + assertNotNull(second.nextToken()); + ListAcceleratorsResponse last = client.listAccelerators(request -> request + .maxResults(1).nextToken(second.nextToken())); + assertEquals(List.of(arns.get(2)), last.accelerators().stream() + .map(accelerator -> accelerator.acceleratorArn()).toList()); + assertNull(last.nextToken()); + assertEquals(arns, client.listAcceleratorsPaginator(request -> request.maxResults(2)) + .accelerators().stream().map(accelerator -> accelerator.acceleratorArn()).toList()); + assertEquals(foreignArns, otherAccount.listAccelerators(request -> {}).accelerators().stream() + .map(accelerator -> accelerator.acceleratorArn()).toList()); + } + } + + @Test + void sdkUsesTheDocumentedTenItemDefaultAndAcceptsTheHundredItemMaximum() { + seed(ACCOUNT, 11); + try (GlobalAcceleratorClient client = client(ACCOUNT, REGION)) { + ListAcceleratorsResponse first = client.listAccelerators(request -> {}); + assertEquals(10, first.accelerators().size()); + assertNotNull(first.nextToken()); + ListAcceleratorsResponse last = client.listAccelerators(request -> request.nextToken(first.nextToken())); + assertEquals(1, last.accelerators().size()); + assertNull(last.nextToken()); + assertEquals(11, client.listAccelerators(request -> request.maxResults(100)).accelerators().size()); + } + } + + @ParameterizedTest + @ValueSource(ints = {-1, 0, 101}) + void sdkRejectsPageSizesOutsideTheDocumentedRange(int maxResults) { + try (GlobalAcceleratorClient client = client(ACCOUNT, REGION)) { + InvalidArgumentException failure = assertThrows(InvalidArgumentException.class, + () -> client.listAccelerators(request -> request.maxResults(maxResults))); + assertEquals(400, failure.statusCode()); + } + } + + @ParameterizedTest + @ValueSource(strings = {"not-a-token", "%%%%"}) + void sdkRejectsMalformedContinuationTokens(String token) { + try (GlobalAcceleratorClient client = client(ACCOUNT, REGION)) { + InvalidNextTokenException failure = assertThrows(InvalidNextTokenException.class, + () -> client.listAccelerators(request -> request.nextToken(token))); + assertEquals(400, failure.statusCode()); + } + } + + @Test + void sdkRejectsTokensLongerThanTheDocumentedMaximum() { + try (GlobalAcceleratorClient client = client(ACCOUNT, REGION)) { + InvalidNextTokenException failure = assertThrows(InvalidNextTokenException.class, + () -> client.listAccelerators(request -> request.nextToken("x".repeat(256)))); + assertEquals(400, failure.statusCode()); + } + } + + @Test + void sdkDoesNotAcceptAnotherAccountsContinuationToken() { + seed(ACCOUNT, 3); + try (GlobalAcceleratorClient client = client(ACCOUNT, REGION); + GlobalAcceleratorClient otherAccount = client(OTHER_ACCOUNT, REGION)) { + String token = client.listAccelerators(request -> request.maxResults(1)).nextToken(); + assertNotNull(token); + assertEquals(400, assertThrows(InvalidNextTokenException.class, + () -> otherAccount.listAccelerators(request -> request.nextToken(token))).statusCode()); + } + } + + @Test + void sdkReturnsAnEmptyPageWithoutAToken() { + try (GlobalAcceleratorClient client = client(ACCOUNT, REGION)) { + ListAcceleratorsResponse response = client.listAccelerators(request -> request.maxResults(100)); + assertTrue(response.accelerators().isEmpty()); + assertNull(response.nextToken()); + } + } + + @ParameterizedTest + @ValueSource(strings = {"{\"MaxResults\":4294967297}", "{\"MaxResults\":1.5}", + "{\"MaxResults\":\"1\"}"}) + void httpRejectsMalformedPageSizes(String body) { + given().header("X-Amz-Target", "GlobalAccelerator_V20180706.ListAccelerators") + .contentType("application/x-amz-json-1.1").body(body) + .post("/").then().statusCode(400) + .body("__type", equalTo("InvalidArgumentException")); + } + + @ParameterizedTest + @ValueSource(strings = {"{\"NextToken\":{}}", "{\"NextToken\":[]}"}) + void httpRejectsMalformedTokenFields(String body) { + given().header("X-Amz-Target", "GlobalAccelerator_V20180706.ListAccelerators") + .contentType("application/x-amz-json-1.1").body(body) + .post("/").then().statusCode(400) + .body("__type", equalTo("InvalidNextTokenException")); + } + + private List seed(String account, int count) { + List arns = new ArrayList<>(); + for (int index = 0; index < count; index++) { + String id = UUID.randomUUID().toString(); + String arn = AwsArnUtils.Arn.global(AwsRegions.partitionFor(REGION.id()), "globalaccelerator", + account, "accelerator/" + id).toString(); + Accelerator accelerator = new Accelerator(); + accelerator.setAcceleratorArn(arn); + accelerator.setName("page-" + id); + accelerator.setIpAddressType("IPV4"); + accelerator.setEnabled(true); + accelerator.setStatus("DEPLOYED"); + accelerator.setCreatedTime(Instant.now().getEpochSecond()); + accelerator.setLastModifiedTime(accelerator.getCreatedTime()); + accelerators.putForAccount(account, arn, accelerator); + seeded.add(new SeededAccelerator(account, arn)); + arns.add(arn); + } + return arns; + } + + private static GlobalAcceleratorClient client(String account, Region region) { + return GlobalAcceleratorClient.builder().endpointOverride(URI.create("http://localhost:" + RestAssured.port)) + .httpClientBuilder(UrlConnectionHttpClient.builder()).region(region) + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test"))) + .build(); + } + + private record SeededAccelerator(String account, String arn) {} +}