diff --git a/docs/services/codedeploy.md b/docs/services/codedeploy.md index 69fb43f6be..9ab8a2bd80 100644 --- a/docs/services/codedeploy.md +++ b/docs/services/codedeploy.md @@ -21,9 +21,18 @@ Floci implements the CodeDeploy API — stored-state management for applications | `GetApplication` | Returns application metadata | | `UpdateApplication` | Renames an application | | `DeleteApplication` | Removes application and all its deployment groups | -| `ListApplications` | Returns all application names | +| `ListApplications` | Lists application names with `nextToken` continuation | | `BatchGetApplications` | Returns info for multiple applications | +`ListApplications` returns names in stable name order with opaque `nextToken` +continuation. Floci uses 100 names per page as an emulator choice; the AWS API +does not expose a page-size input or document a fixed page size. Continue with +the returned token until it is absent. Empty and final pages omit `nextToken`. +Tokens are bound to this action, the caller account, and the request region; +malformed tokens return `InvalidNextTokenException` (HTTP 400). Omitted, null, +and empty tokens start at the first page. This change preserves the service's +existing application storage and region visibility. + ### Deployment Groups | Operation | Notes | diff --git a/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployJsonHandler.java index f2adf0d155..be18c0e0f9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployJsonHandler.java @@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.codedeploy; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; 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.services.codedeploy.model.Application; import io.github.hectorvent.floci.services.codedeploy.model.Deployment; import io.github.hectorvent.floci.services.codedeploy.model.DeploymentConfig; @@ -35,7 +37,7 @@ public class CodeDeployJsonHandler { case "GetApplication" -> getApplication(request, region); case "UpdateApplication" -> updateApplication(request, region); case "DeleteApplication" -> deleteApplication(request, region); - case "ListApplications" -> listApplications(region); + case "ListApplications" -> listApplications(request, region); case "BatchGetApplications" -> batchGetApplications(request, region); case "CreateDeploymentGroup" -> createDeploymentGroup(request, region); case "GetDeploymentGroup" -> getDeploymentGroup(request, region); @@ -97,8 +99,15 @@ public class CodeDeployJsonHandler { return Response.ok(Map.of()).build(); } - private Response listApplications(String region) { - return Response.ok(Map.of("applications", service.listApplications(region))).build(); + private Response listApplications(JsonNode request, String region) { + String token = Pagination.parseNextToken(request, "nextToken", "InvalidNextTokenException"); + PaginatedResult page = service.listApplications(region, token); + Map response = new HashMap<>(); + response.put("applications", page.items()); + if (page.nextToken() != null) { + response.put("nextToken", page.nextToken()); + } + return Response.ok(response).build(); } private Response batchGetApplications(JsonNode req, String region) { diff --git a/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployService.java b/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployService.java index 60d3e6d03b..9eacef4d08 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployService.java @@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.github.hectorvent.floci.core.common.AwsArnUtils; 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.StorageBackedMap; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -389,6 +391,12 @@ public class CodeDeployService { return new ArrayList<>(applicationsFor(region).keySet()); } + public PaginatedResult listApplications(String region, String nextToken) { + return Pagination.paginate(listApplications(region), name -> name, 100, nextToken, + "codedeploy:ListApplications:" + regionResolver.getAccountId() + ":" + region, + token -> new AwsException("InvalidNextTokenException", "Invalid nextToken.", 400)); + } + public List batchGetApplications(String region, List names) { Map store = applicationsFor(region); return names.stream() diff --git a/src/test/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployListApplicationsPaginationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployListApplicationsPaginationIntegrationTest.java new file mode 100644 index 0000000000..3f66c22cdc --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/codedeploy/CodeDeployListApplicationsPaginationIntegrationTest.java @@ -0,0 +1,123 @@ +package io.github.hectorvent.floci.services.codedeploy; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@QuarkusTest +class CodeDeployListApplicationsPaginationIntegrationTest { + + private static final String ACCOUNT = "741666200001"; + private static final String REGION = "ap-south-2"; + private final List created = new ArrayList<>(); + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @AfterEach + void cleanUp() { + for (String name : created) { + call("DeleteApplication", REGION, ACCOUNT, "{\"applicationName\":\"" + name + "\"}", 200); + } + } + + @ParameterizedTest + @ValueSource(strings = {"\"not-a-valid-token\"", "\"%%%%\"", "\"YW5vdGhlci1zY29wZTph\"", "1", "true", "{}", "[]"}) + void malformedTokensFailEvenWhenNoApplicationsExist(String value) { + Map error = list(REGION, ACCOUNT, "{\"nextToken\":" + value + "}", 400); + assertEquals("InvalidNextTokenException", error.get("__type")); + } + + @Test + void continuationWalksTheSortedListWithoutDuplicates() { + List expected = createApplications(101); + Map first = list(REGION, ACCOUNT, "{}", 200); + assertEquals(expected.subList(0, 100), first.get("applications")); + String token = (String) first.get("nextToken"); + assertNotNull(token); + Map last = list(REGION, ACCOUNT, tokenRequest(token), 200); + assertEquals(expected.subList(100, 101), last.get("applications")); + assertFalse(last.containsKey("nextToken")); + } + + @Test + void exactlyOneFullPageOmitsTheToken() { + List expected = createApplications(100); + Map response = list(REGION, ACCOUNT, "{}", 200); + assertEquals(expected, response.get("applications")); + assertFalse(response.containsKey("nextToken")); + } + + @Test + void emptyCollectionsAcceptOmittedNullAndEmptyToken() { + for (String body : List.of("{}", "{\"nextToken\":null}", "{\"nextToken\":\"\"}")) { + Map response = list(REGION, ACCOUNT, body, 200); + assertEquals(List.of(), response.get("applications")); + assertFalse(response.containsKey("nextToken")); + } + } + + @Test + void tokensCannotCrossAccountOrRegion() { + createApplications(101); + String token = (String) list(REGION, ACCOUNT, "{}", 200).get("nextToken"); + assertNotNull(token); + for (Map error : List.of( + list(REGION, "741666200002", tokenRequest(token), 400), + list("ap-southeast-3", ACCOUNT, tokenRequest(token), 400))) { + assertEquals("InvalidNextTokenException", error.get("__type")); + } + } + + @Test + void anIssuedTokenStillResumesAfterItsCursorApplicationIsDeleted() { + List expected = createApplications(101); + String token = (String) list(REGION, ACCOUNT, "{}", 200).get("nextToken"); + assertNotNull(token); + String cursor = expected.get(99); + call("DeleteApplication", REGION, ACCOUNT, "{\"applicationName\":\"" + cursor + "\"}", 200); + created.remove(cursor); + Map last = list(REGION, ACCOUNT, tokenRequest(token), 200); + assertEquals(expected.subList(100, 101), last.get("applications")); + assertFalse(last.containsKey("nextToken")); + } + + private List createApplications(int count) { + for (int i = count - 1; i >= 0; i--) { + String name = "token-test-" + String.format("%03d", i); + call("CreateApplication", REGION, ACCOUNT, "{\"applicationName\":\"" + name + "\"}", 200); + created.add(name); + } + return created.stream().sorted().toList(); + } + + private Map list(String region, String account, String body, int status) { + return call("ListApplications", region, account, body, status); + } + + private Map call(String action, String region, String account, String body, int status) { + return given().contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "CodeDeploy_20141006." + action) + .header("Authorization", "AWS4-HMAC-SHA256 Credential=" + account + "/20260101/" + region + "/codedeploy/aws4_request") + .body(body).when().post("/").then().statusCode(status).extract().jsonPath().getMap(""); + } + + private String tokenRequest(String token) { + return "{\"nextToken\":\"" + token + "\"}"; + } +}