mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(backup): paginate ListBackupVaults (#6541)
This commit is contained in:
1 parent
5fbc51f537
commit
13d0452a20
5 files changed
+288
-4
No files matched your search
+59
@@ -0,0 +1,59 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import software.amazon.awssdk.services.backup.BackupClient;
|
||||
import software.amazon.awssdk.services.backup.model.BackupVaultListMember;
|
||||
import software.amazon.awssdk.services.backup.model.InvalidParameterValueException;
|
||||
import software.amazon.awssdk.services.backup.model.ListBackupVaultsResponse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("AWS Backup vault pagination")
|
||||
class BackupVaultPaginationTest {
|
||||
|
||||
@Test
|
||||
@Timeout(30)
|
||||
@DisplayName("ListBackupVaults paginator visits every vault and stops on the last page")
|
||||
void paginatorVisitsEveryVault() {
|
||||
String prefix = "compat-pages-" + UUID.randomUUID();
|
||||
List<String> created = new ArrayList<>();
|
||||
try (BackupClient backup = TestFixtures.backupClient()) {
|
||||
try {
|
||||
for (String suffix : List.of("a", "b", "c")) {
|
||||
String name = prefix + suffix;
|
||||
backup.createBackupVault(r -> r.backupVaultName(name));
|
||||
created.add(name);
|
||||
}
|
||||
|
||||
List<String> names = new ArrayList<>();
|
||||
ListBackupVaultsResponse last = null;
|
||||
for (ListBackupVaultsResponse page : backup.listBackupVaultsPaginator(r -> r.maxResults(1))) {
|
||||
assertThat(page.backupVaultList()).hasSize(1);
|
||||
names.addAll(page.backupVaultList().stream().map(BackupVaultListMember::backupVaultName).toList());
|
||||
last = page;
|
||||
}
|
||||
assertThat(names).containsAll(created).doesNotHaveDuplicates();
|
||||
assertThat(last).isNotNull();
|
||||
assertThat(last.nextToken()).isNull();
|
||||
|
||||
assertThatThrownBy(() -> backup.listBackupVaults(r -> r.maxResults(0)))
|
||||
.isInstanceOfSatisfying(InvalidParameterValueException.class,
|
||||
error -> assertThat(error.statusCode()).isEqualTo(400));
|
||||
assertThatThrownBy(() -> backup.listBackupVaults(r -> r.nextToken("invalid-token")))
|
||||
.isInstanceOfSatisfying(InvalidParameterValueException.class,
|
||||
error -> assertThat(error.statusCode()).isEqualTo(400));
|
||||
} finally {
|
||||
for (String name : created) {
|
||||
backup.deleteBackupVault(r -> r.backupVaultName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -247,7 +247,12 @@ Actual backup is simulated — no data is read from or written to the referenced
|
||||
- Report plans (`CreateReportPlan`, `DescribeReportPlan`, etc.)
|
||||
- Framework operations (`CreateFramework`, etc.)
|
||||
- Legal holds
|
||||
- Pagination tokens on list operations
|
||||
- Pagination tokens on list operations other than `ListBackupVaults`
|
||||
|
||||
`ListBackupVaults` accepts `maxResults` from 1 to 1000 and returns `NextToken` when
|
||||
more vaults remain. Pass it as `nextToken` with the same region and `vaultType`
|
||||
filter to continue. Floci uses vault-name order and a page size of 1000 when the
|
||||
size is omitted; AWS does not document a default order or page size.
|
||||
|
||||
## Examples
|
||||
|
||||
|
||||
@@ -5,6 +5,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.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.backup.model.*;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -103,12 +105,18 @@ public class BackupController {
|
||||
@GET
|
||||
@Path("/backup-vaults/")
|
||||
public Response listBackupVaults(@Context HttpHeaders headers,
|
||||
@QueryParam("vaultType") String byVaultType) {
|
||||
@QueryParam("vaultType") String byVaultType,
|
||||
@QueryParam("maxResults") String maxResults,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<BackupVault> vaults = service.listBackupVaults(region, byVaultType);
|
||||
PaginatedResult<BackupVault> page = service.listBackupVaults(region, byVaultType,
|
||||
Pagination.parseMaxResults(maxResults, "InvalidParameterValueException"), nextToken);
|
||||
ObjectNode out = objectMapper.createObjectNode();
|
||||
ArrayNode list = out.putArray("BackupVaultList");
|
||||
vaults.forEach(v -> list.add(vaultJson(v)));
|
||||
page.items().forEach(v -> list.add(vaultJson(v)));
|
||||
if (page.nextToken() != null) {
|
||||
out.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(out).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
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.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
@@ -279,6 +281,18 @@ public class BackupService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public PaginatedResult<BackupVault> listBackupVaults(String region, String byVaultType,
|
||||
Integer maxResults, String nextToken) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > 1000)) {
|
||||
throw new AwsException("InvalidParameterValueException", "maxResults must be between 1 and 1000", 400);
|
||||
}
|
||||
String namespace = "backup:ListBackupVaults:" + regionResolver.getAccountId() + ":" + region + ":"
|
||||
+ (byVaultType == null ? "all" : "type:" + byVaultType);
|
||||
return Pagination.paginate(listBackupVaults(region, byVaultType), BackupVault::getBackupVaultName,
|
||||
maxResults == null ? 1000 : maxResults, nextToken, namespace,
|
||||
token -> new AwsException("InvalidParameterValueException", "Invalid nextToken.", 400));
|
||||
}
|
||||
|
||||
private static void requireNotAirGapped(BackupVault vault, String action) {
|
||||
if (vault.getVaultType() != null) {
|
||||
throw new AwsException("InvalidRequestException",
|
||||
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
package io.github.hectorvent.floci.services.backup;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
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 java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
class BackupPaginationIntegrationTest {
|
||||
|
||||
private static final String REGION = "ap-south-2";
|
||||
private static final String AUTH = "AWS4-HMAC-SHA256 Credential=test/20261025/ap-south-2/backup/aws4_request";
|
||||
private static final String OTHER_REGION_AUTH = "AWS4-HMAC-SHA256 Credential=test/20261025/ap-southeast-4/backup/aws4_request";
|
||||
|
||||
@Inject BackupService service;
|
||||
|
||||
private final List<String> vaultNames = new ArrayList<>();
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
for (String name : vaultNames) {
|
||||
service.deleteBackupVault(name, REGION);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listBackupVaultsHonorsMaxResultsAndResumes() {
|
||||
createVault("page-c");
|
||||
createVault("page-a");
|
||||
createVault("page-b");
|
||||
|
||||
JsonPath first = given().header("Authorization", AUTH).queryParam("maxResults", 1)
|
||||
.get("/backup-vaults/").then().statusCode(200)
|
||||
.body("BackupVaultList.BackupVaultName", contains("page-a"))
|
||||
.body("BackupVaultList[0]", not(hasKey("FlociVaultId")))
|
||||
.body("BackupVaultList[0].VaultType", equalTo("BACKUP_VAULT"))
|
||||
.extract().jsonPath();
|
||||
String token = first.getString("NextToken");
|
||||
assertNotNull(token);
|
||||
|
||||
given().header("Authorization", AUTH).queryParam("maxResults", 2).queryParam("nextToken", token)
|
||||
.get("/backup-vaults/").then().statusCode(200)
|
||||
.body("BackupVaultList.BackupVaultName", contains("page-b", "page-c"))
|
||||
.body("$", not(hasKey("NextToken")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maximumAndOmittedPageSizesReturnAtMostOneThousandVaults() {
|
||||
for (int i = 0; i < 1001; i++) {
|
||||
String name = "boundary-%04d".formatted(i);
|
||||
service.createBackupVault(name, null, null, Map.of(), REGION);
|
||||
vaultNames.add(name);
|
||||
}
|
||||
|
||||
for (Map<String, ?> params : List.of(Map.<String, Object>of(), Map.of("maxResults", 1000))) {
|
||||
String token = given().header("Authorization", AUTH).queryParams(params)
|
||||
.get("/backup-vaults/").then().statusCode(200)
|
||||
.body("BackupVaultList", hasSize(1000))
|
||||
.extract().path("NextToken");
|
||||
assertNotNull(token);
|
||||
given().header("Authorization", AUTH).queryParam("nextToken", token)
|
||||
.get("/backup-vaults/").then().statusCode(200)
|
||||
.body("BackupVaultList.BackupVaultName", contains("boundary-1000"))
|
||||
.body("$", not(hasKey("NextToken")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyRegionOmitsNextToken() {
|
||||
given().header("Authorization", OTHER_REGION_AUTH).queryParam("maxResults", 1)
|
||||
.get("/backup-vaults/").then().statusCode(200)
|
||||
.body("BackupVaultList", empty())
|
||||
.body("$", not(hasKey("NextToken")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"0", "-1", "1001", "1.5", "abc", "2147483648"})
|
||||
void invalidPageSizesReturnAwsError(String size) {
|
||||
given().header("Authorization", AUTH).queryParam("maxResults", size)
|
||||
.get("/backup-vaults/").then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValueException"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"not-a-token!", "MQ=="})
|
||||
void invalidTokensReturnAwsError(String token) {
|
||||
given().header("Authorization", AUTH).queryParam("nextToken", token)
|
||||
.get("/backup-vaults/").then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValueException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void vaultTypeFilterIsAppliedBeforePagingAndBoundToToken() {
|
||||
createAirGappedVault("filter-a");
|
||||
createVault("filter-b");
|
||||
createAirGappedVault("filter-c");
|
||||
createVault("filter-d");
|
||||
|
||||
String token = given().header("Authorization", AUTH).queryParam("maxResults", 1)
|
||||
.queryParam("vaultType", "BACKUP_VAULT")
|
||||
.get("/backup-vaults/").then().statusCode(200)
|
||||
.body("BackupVaultList.BackupVaultName", contains("filter-b"))
|
||||
.extract().path("NextToken");
|
||||
assertNotNull(token);
|
||||
given().header("Authorization", AUTH).queryParam("maxResults", 1)
|
||||
.queryParam("vaultType", "BACKUP_VAULT").queryParam("nextToken", token)
|
||||
.get("/backup-vaults/").then().statusCode(200)
|
||||
.body("BackupVaultList.BackupVaultName", contains("filter-d"))
|
||||
.body("$", not(hasKey("NextToken")));
|
||||
|
||||
given().header("Authorization", AUTH).queryParam("vaultType", "LOGICALLY_AIR_GAPPED_BACKUP_VAULT")
|
||||
.queryParam("nextToken", token).get("/backup-vaults/").then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValueException"));
|
||||
given().header("Authorization", AUTH).queryParam("nextToken", token)
|
||||
.get("/backup-vaults/").then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValueException"));
|
||||
given().header("Authorization", OTHER_REGION_AUTH).queryParam("vaultType", "BACKUP_VAULT")
|
||||
.queryParam("nextToken", token).get("/backup-vaults/").then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValueException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void continuationTokenCannotCrossAccounts() {
|
||||
String firstAccount = "111111111111";
|
||||
String secondAccount = "222222222222";
|
||||
String prefix = "account-pages-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
List<String> firstNames = List.of(prefix + "-a", prefix + "-b");
|
||||
List<String> secondNames = List.of(prefix + "-c", prefix + "-d");
|
||||
|
||||
RequestScopes.runAs(firstAccount, REGION, () -> firstNames.forEach(name ->
|
||||
service.createBackupVault(name, null, null, Map.of(), REGION)));
|
||||
RequestScopes.runAs(secondAccount, REGION, () -> secondNames.forEach(name ->
|
||||
service.createBackupVault(name, null, null, Map.of(), REGION)));
|
||||
try {
|
||||
String token = RequestScopes.callAs(firstAccount, REGION, () ->
|
||||
service.listBackupVaults(REGION, null, 1, null).nextToken());
|
||||
assertNotNull(token);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () ->
|
||||
RequestScopes.callAs(secondAccount, REGION, () ->
|
||||
service.listBackupVaults(REGION, null, 1, token)));
|
||||
assertEquals("InvalidParameterValueException", error.getErrorCode());
|
||||
} finally {
|
||||
RequestScopes.runAs(firstAccount, REGION, () -> firstNames.forEach(name ->
|
||||
service.deleteBackupVault(name, REGION)));
|
||||
RequestScopes.runAs(secondAccount, REGION, () -> secondNames.forEach(name ->
|
||||
service.deleteBackupVault(name, REGION)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void removingPreviousPageDoesNotSkipTheNextVault() {
|
||||
createVault("delete-a");
|
||||
createVault("delete-b");
|
||||
createVault("delete-c");
|
||||
String token = given().header("Authorization", AUTH).queryParam("maxResults", 1)
|
||||
.get("/backup-vaults/").then().statusCode(200)
|
||||
.body("BackupVaultList.BackupVaultName", contains("delete-a"))
|
||||
.extract().path("NextToken");
|
||||
assertNotNull(token);
|
||||
given().header("Authorization", AUTH).delete("/backup-vaults/delete-a").then().statusCode(204);
|
||||
vaultNames.remove("delete-a");
|
||||
|
||||
given().header("Authorization", AUTH).queryParam("nextToken", token)
|
||||
.get("/backup-vaults/").then().statusCode(200)
|
||||
.body("BackupVaultList.BackupVaultName", contains("delete-b", "delete-c"))
|
||||
.body("$", not(hasKey("NextToken")));
|
||||
}
|
||||
|
||||
private void createAirGappedVault(String name) {
|
||||
given().header("Authorization", AUTH).contentType("application/json")
|
||||
.body("{\"MinRetentionDays\":7,\"MaxRetentionDays\":30}")
|
||||
.put("/logically-air-gapped-backup-vaults/" + name).then().statusCode(200);
|
||||
vaultNames.add(name);
|
||||
}
|
||||
|
||||
private void createVault(String name) {
|
||||
given().header("Authorization", AUTH).contentType("application/json").body("{}")
|
||||
.put("/backup-vaults/" + name).then().statusCode(200);
|
||||
vaultNames.add(name);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user