fix(efs): paginate access points and resource tags (#7538)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-04 17:32:57 -04:00
1 parent 68271ef6b8
commit 6e16b80281
8 files changed
+302 -10

No files matched your search

+6
View File
@@ -546,6 +546,12 @@
<version>${aws.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>efs</artifactId>
<version>${aws.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sfn</artifactId>
@@ -123,6 +123,20 @@ public final class Pagination {
return slice(all, cursorOf, limit, after, scope, markers::issue);
}
/** Paginates markers whose scope is signed without being carried in the token. */
public static <T> PaginatedResult<T> slice(List<T> all, Function<T, String> cursorOf,
int limit, String marker, SignedMarkers markers,
String scope, Function<String, AwsException> invalidMarker) {
if (limit < 1) {
throw new IllegalArgumentException("limit must be at least 1, was " + limit);
}
String after = marker == null ? null : markers.cursorOf(scope, marker);
if (marker != null && after == null) {
throw invalidMarker.apply(marker);
}
return slice(all, cursorOf, limit, after, "", cursor -> markers.issue(scope, cursor));
}
/** Validates service-specific bounds before applying the existing namespaced cursor format. */
public static <T> PaginatedResult<T> paginate(List<T> all, Function<T, String> cursorOf,
Integer maxResults, String nextToken,
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.efs;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.efs.model.AccessPointDescription;
import io.github.hectorvent.floci.services.efs.model.BackupPolicy;
@@ -8,7 +9,6 @@ import io.github.hectorvent.floci.services.efs.model.CreateFileSystemRequest;
import io.github.hectorvent.floci.services.efs.model.CreateMountTargetRequest;
import io.github.hectorvent.floci.services.efs.model.CreateTagsRequest;
import io.github.hectorvent.floci.services.efs.model.DeleteTagsRequest;
import io.github.hectorvent.floci.services.efs.model.DescribeAccessPointsResponse;
import io.github.hectorvent.floci.services.efs.model.DescribeFileSystemPolicyResponse;
import io.github.hectorvent.floci.services.efs.model.DescribeFileSystemsRequest;
import io.github.hectorvent.floci.services.efs.model.DescribeFileSystemsResponse;
@@ -178,9 +178,11 @@ public class EfsController {
@GET
@Path("/resource-tags/{ResourceId}")
public Response listTagsForResource(@Context HttpHeaders headers, @PathParam("ResourceId") String resourceId) {
public Response listTagsForResource(@Context HttpHeaders headers, @PathParam("ResourceId") String resourceId,
@QueryParam("MaxResults") String maxResults,
@QueryParam("NextToken") String nextToken) {
String region = regionResolver.resolveRegion(headers);
return Response.ok(efsService.listTagsForResource(region, resourceId)).build();
return Response.ok(efsService.listTagsForResource(region, resourceId, parseMaxResults(maxResults), nextToken)).build();
}
// --- Mount Targets ---
@@ -236,11 +238,14 @@ public class EfsController {
@GET
@Path("/access-points")
public Response describeAccessPoints(@Context HttpHeaders headers, @QueryParam("FileSystemId") String fileSystemId, @QueryParam("AccessPointId") String accessPointId) {
public Response describeAccessPoints(@Context HttpHeaders headers,
@QueryParam("FileSystemId") String fileSystemId,
@QueryParam("AccessPointId") String accessPointId,
@QueryParam("MaxResults") String maxResults,
@QueryParam("NextToken") String nextToken) {
String region = regionResolver.resolveRegion(headers);
DescribeAccessPointsResponse res = new DescribeAccessPointsResponse();
res.setAccessPoints(efsService.describeAccessPoints(region, fileSystemId, accessPointId));
return Response.ok(res).build();
return Response.ok(efsService.describeAccessPointsPage(
region, fileSystemId, accessPointId, parseMaxResults(maxResults), nextToken)).build();
}
@DELETE
@@ -321,4 +326,11 @@ public class EfsController {
res.setLifecyclePolicies(efsService.getLifecycleConfiguration(region, fileSystemId));
return Response.ok(res).build();
}
private static Integer parseMaxResults(String value) {
if (value != null && value.isBlank()) {
throw EfsException.badRequest("MaxResults must be an integer of at least 1.");
}
return Pagination.parseMaxResults(value, "BadRequest");
}
}
@@ -2,8 +2,11 @@ package io.github.hectorvent.floci.services.efs;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AvailabilityZoneIds;
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.Resettable;
import io.github.hectorvent.floci.core.common.SignedMarkers;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.efs.model.AccessPointDescription;
@@ -13,6 +16,7 @@ import io.github.hectorvent.floci.services.efs.model.CreateFileSystemRequest;
import io.github.hectorvent.floci.services.efs.model.CreateMountTargetRequest;
import io.github.hectorvent.floci.services.efs.model.CreateTagsRequest;
import io.github.hectorvent.floci.services.efs.model.DeleteTagsRequest;
import io.github.hectorvent.floci.services.efs.model.DescribeAccessPointsResponse;
import io.github.hectorvent.floci.services.efs.model.DescribeFileSystemsRequest;
import io.github.hectorvent.floci.services.efs.model.DescribeFileSystemsResponse;
import io.github.hectorvent.floci.services.efs.model.DescribeMountTargetSecurityGroupsResponse;
@@ -42,6 +46,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
@ApplicationScoped
@@ -57,6 +62,7 @@ public class EfsService implements Resettable {
private final StorageBackend<String, List<LifecyclePolicy>> lifecycleConfigurationStore;
private final ConcurrentHashMap<String, Object> syncLocks = new ConcurrentHashMap<>();
private final RegionResolver regionResolver;
private final SignedMarkers pageMarkers = new SignedMarkers(128);
private Object lockFor(String key) {
return syncLocks.computeIfAbsent(key, k -> new Object());
@@ -343,10 +349,14 @@ public class EfsService implements Resettable {
}
}
public ListTagsForResourceResponse listTagsForResource(String region, String resourceId) {
List<Tag> tags = getResourceTags(region, resourceId);
public ListTagsForResourceResponse listTagsForResource(String region, String resourceId,
Integer maxResults, String nextToken) {
String scope = "efs:tags:" + regionResolver.getAccountId() + ":" + region + ":" + resourceId;
PaginatedResult<Tag> page = page(getResourceTags(region, resourceId), Tag::getKey,
maxResults, nextToken, scope);
ListTagsForResourceResponse res = new ListTagsForResourceResponse();
res.setTags(tags);
res.setTags(page.items());
res.setNextToken(page.nextToken());
return res;
}
@@ -570,6 +580,20 @@ public class EfsService implements Resettable {
.collect(Collectors.toList());
}
public DescribeAccessPointsResponse describeAccessPointsPage(String region, String fileSystemId,
String accessPointId, Integer maxResults,
String nextToken) {
String scope = "efs:access-points:" + regionResolver.getAccountId() + ":" + region
+ ":" + fileSystemId + ":" + accessPointId;
PaginatedResult<AccessPointDescription> page = page(
describeAccessPoints(region, fileSystemId, accessPointId), AccessPointDescription::getAccessPointId,
maxResults, nextToken, scope);
DescribeAccessPointsResponse res = new DescribeAccessPointsResponse();
res.setAccessPoints(page.items());
res.setNextToken(page.nextToken());
return res;
}
public void deleteAccessPoint(String region, String accessPointId) {
String key = regionKey(region, accessPointId);
AccessPointDescription ap = accessPointStore.get(key).orElse(null);
@@ -634,6 +658,19 @@ public class EfsService implements Resettable {
return policies == null ? new ArrayList<>() : policies;
}
private <T> PaginatedResult<T> page(List<T> items, Function<T, String> cursorOf,
Integer maxResults, String nextToken, String scope) {
if (maxResults != null && maxResults < 1) {
throw EfsException.badRequest("MaxResults must be at least 1.");
}
if (nextToken != null && (nextToken.isEmpty() || nextToken.length() > 128)) {
throw EfsException.badRequest("NextToken must be between 1 and 128 characters.");
}
int limit = maxResults == null ? 100 : maxResults;
return Pagination.slice(items, cursorOf, limit, nextToken, pageMarkers, scope,
token -> EfsException.badRequest("Invalid NextToken."));
}
private String regionKey(String region, String id) {
return region + "::" + id;
}
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.efs.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.quarkus.runtime.annotations.RegisterForReflection;
@@ -10,6 +11,7 @@ import java.util.List;
@RegisterForReflection
public class DescribeAccessPointsResponse {
private List<AccessPointDescription> accessPoints;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String nextToken;
public List<AccessPointDescription> getAccessPoints() { return accessPoints; }
public void setAccessPoints(List<AccessPointDescription> accessPoints) { this.accessPoints = accessPoints; }
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.efs.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.quarkus.runtime.annotations.RegisterForReflection;
@@ -10,6 +11,7 @@ import java.util.List;
@RegisterForReflection
public class ListTagsForResourceResponse {
private List<Tag> tags;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String nextToken;
public List<Tag> getTags() { return tags; }
@@ -97,6 +97,28 @@ class PaginationTest {
() -> signedPage(markers, 1, "!!!", "")));
}
@Test
void scopedSignedSliceBoundsLongCursorsAndRejectsOtherScopes() {
SignedMarkers markers = new SignedMarkers(128);
String longCursor = "a".repeat(128);
List<String> items = List.of("z", longCursor);
String scope = "account:region:resource";
PaginatedResult<String> first = Pagination.slice(items, Function.identity(), 1,
null, markers, scope, INVALID_TOKEN);
assertEquals(List.of(longCursor), first.items());
assertTrue(first.nextToken().length() <= 128);
assertEquals(longCursor, markers.cursorOf(scope, first.nextToken()));
assertNull(markers.cursorOf(first.nextToken()));
PaginatedResult<String> second = Pagination.slice(List.of("z"), Function.identity(), 1,
first.nextToken(), markers, scope, INVALID_TOKEN);
assertEquals(List.of("z"), second.items());
assertNull(second.nextToken());
assertSame(INVALID, assertThrows(AwsException.class, () -> Pagination.slice(items,
Function.identity(), 1, first.nextToken(), markers, "other:region:resource", INVALID_TOKEN)));
}
@Test
void expiringPaging_validatesTheTokenBeforeLoadingItems() {
SignedMarkers markers = new SignedMarkers();
@@ -0,0 +1,197 @@
package io.github.hectorvent.floci.services.efs;
import io.github.hectorvent.floci.testing.AwsTestClients;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.JsonPath;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.efs.EfsClient;
import software.amazon.awssdk.services.efs.model.BadRequestException;
import software.amazon.awssdk.services.efs.model.DescribeAccessPointsResponse;
import software.amazon.awssdk.services.efs.model.ListTagsForResourceResponse;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
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 EfsPaginationIssueIntegrationTest {
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void listTagsAndAccessPointsHonorMaxResults() {
String fileSystemId = createFileSystem("first", "second");
String[] accessPointIds = createAccessPoints(fileSystemId);
JsonPath tags = given()
.queryParam("MaxResults", 1)
.get("/2015-02-01/resource-tags/" + fileSystemId)
.then().statusCode(200).extract().jsonPath();
JsonPath accessPoints = given()
.queryParam("FileSystemId", fileSystemId)
.queryParam("MaxResults", 1)
.get("/2015-02-01/access-points")
.then().statusCode(200).extract().jsonPath();
assertAll(
() -> assertEquals(1, tags.getList("Tags").size(), "ListTagsForResource page size"),
() -> assertNotNull(tags.getString("NextToken"), "ListTagsForResource continuation"),
() -> assertEquals(1, accessPoints.getList("AccessPoints").size(), "DescribeAccessPoints page size"),
() -> assertNotNull(accessPoints.getString("NextToken"), "DescribeAccessPoints continuation"));
JsonPath nextTags = given()
.queryParam("MaxResults", 1)
.queryParam("NextToken", tags.getString("NextToken"))
.get("/2015-02-01/resource-tags/" + fileSystemId)
.then().statusCode(200).extract().jsonPath();
JsonPath nextAccessPoints = given()
.queryParam("FileSystemId", fileSystemId)
.queryParam("MaxResults", 1)
.queryParam("NextToken", accessPoints.getString("NextToken"))
.get("/2015-02-01/access-points")
.then().statusCode(200).extract().jsonPath();
assertAll(
() -> assertEquals(1, nextTags.getList("Tags").size()),
() -> assertNotEquals(tags.getString("Tags[0].Key"), nextTags.getString("Tags[0].Key")),
() -> assertFalse(nextTags.getMap("$").containsKey("NextToken")),
() -> assertEquals(1, nextAccessPoints.getList("AccessPoints").size()),
() -> assertNotEquals(accessPoints.getString("AccessPoints[0].AccessPointId"),
nextAccessPoints.getString("AccessPoints[0].AccessPointId")),
() -> assertFalse(nextAccessPoints.getMap("$").containsKey("NextToken")));
given().queryParam("NextToken", tags.getString("NextToken"))
.get("/2015-02-01/resource-tags/" + accessPointIds[0])
.then().statusCode(400);
given().queryParam("AccessPointId", accessPointIds[0])
.queryParam("NextToken", accessPoints.getString("NextToken"))
.get("/2015-02-01/access-points")
.then().statusCode(400);
given().queryParam("NextToken", "not-a-valid-token")
.get("/2015-02-01/resource-tags/" + fileSystemId)
.then().statusCode(400);
given().queryParam("NextToken", "not-a-valid-token")
.get("/2015-02-01/access-points")
.then().statusCode(400);
given().queryParam("NextToken", "a".repeat(129))
.get("/2015-02-01/resource-tags/" + fileSystemId)
.then().statusCode(400);
given().queryParam("MaxResults", 0)
.get("/2015-02-01/resource-tags/" + fileSystemId)
.then().statusCode(400);
given().queryParam("MaxResults", "invalid")
.get("/2015-02-01/access-points")
.then().statusCode(400);
assertEquals(2, given().queryParam("MaxResults", 1000)
.get("/2015-02-01/resource-tags/" + fileSystemId)
.then().statusCode(200).extract().jsonPath().getList("Tags").size());
}
@Test
void longTagKeyStillUsesABoundedToken() {
String longKey = "a".repeat(128);
String fileSystemId = createFileSystem(longKey, "z");
JsonPath first = given().queryParam("MaxResults", 1)
.get("/2015-02-01/resource-tags/" + fileSystemId)
.then().statusCode(200).extract().jsonPath();
String nextToken = first.getString("NextToken");
assertAll(
() -> assertEquals(longKey, first.getString("Tags[0].Key")),
() -> assertNotNull(nextToken),
() -> assertTrue(nextToken.length() <= 128));
JsonPath second = given().queryParam("MaxResults", 1)
.queryParam("NextToken", nextToken)
.get("/2015-02-01/resource-tags/" + fileSystemId)
.then().statusCode(200).extract().jsonPath();
assertEquals("z", second.getString("Tags[0].Key"));
assertFalse(second.getMap("$").containsKey("NextToken"));
}
@Test
void awsSdkReadsBothPages() {
String fileSystemId = createFileSystem("first", "second");
createAccessPoints(fileSystemId);
try (EfsClient client = AwsTestClients.create(EfsClient.builder(), "000000000000", Region.US_EAST_1)) {
ListTagsForResourceResponse firstTags = client.listTagsForResource(
request -> request.resourceId(fileSystemId).maxResults(1));
ListTagsForResourceResponse secondTags = client.listTagsForResource(
request -> request.resourceId(fileSystemId).maxResults(1).nextToken(firstTags.nextToken()));
assertAll(
() -> assertEquals(1, firstTags.tags().size()),
() -> assertNotNull(firstTags.nextToken()),
() -> assertEquals(1, secondTags.tags().size()),
() -> assertNotEquals(firstTags.tags().getFirst().key(), secondTags.tags().getFirst().key()),
() -> assertNull(secondTags.nextToken()));
DescribeAccessPointsResponse firstAccessPoints = client.describeAccessPoints(
request -> request.fileSystemId(fileSystemId).maxResults(1));
DescribeAccessPointsResponse secondAccessPoints = client.describeAccessPoints(
request -> request.fileSystemId(fileSystemId).maxResults(1)
.nextToken(firstAccessPoints.nextToken()));
assertAll(
() -> assertEquals(1, firstAccessPoints.accessPoints().size()),
() -> assertNotNull(firstAccessPoints.nextToken()),
() -> assertEquals(1, secondAccessPoints.accessPoints().size()),
() -> assertNotEquals(firstAccessPoints.accessPoints().getFirst().accessPointId(),
secondAccessPoints.accessPoints().getFirst().accessPointId()),
() -> assertNull(secondAccessPoints.nextToken()));
DescribeAccessPointsResponse unfiltered = client.describeAccessPoints(
request -> request.maxResults(1));
assertNotNull(unfiltered.nextToken());
try (EfsClient otherAccount = AwsTestClients.create(
EfsClient.builder(), "111111111111", Region.US_EAST_1);
EfsClient otherRegion = AwsTestClients.create(
EfsClient.builder(), "000000000000", Region.US_WEST_2)) {
assertThrows(BadRequestException.class, () -> otherAccount.describeAccessPoints(
request -> request.maxResults(1).nextToken(unfiltered.nextToken())));
assertThrows(BadRequestException.class, () -> otherRegion.describeAccessPoints(
request -> request.maxResults(1).nextToken(unfiltered.nextToken())));
}
}
}
private static String createFileSystem(String firstTagKey, String secondTagKey) {
return given()
.contentType("application/json")
.body("""
{"CreationToken":"%s","Tags":[
{"Key":"%s","Value":"1"},
{"Key":"%s","Value":"2"}
]}
""".formatted(UUID.randomUUID(), firstTagKey, secondTagKey))
.post("/2015-02-01/file-systems")
.then().statusCode(201).extract().path("FileSystemId");
}
private static String[] createAccessPoints(String fileSystemId) {
String token = UUID.randomUUID().toString();
String[] accessPointIds = new String[2];
for (int i = 0; i < accessPointIds.length; i++) {
accessPointIds[i] = given().contentType("application/json")
.body("""
{"ClientToken":"%s","FileSystemId":"%s"}
""".formatted(token + "-" + i, fileSystemId))
.post("/2015-02-01/access-points")
.then().statusCode(200).extract().path("AccessPointId");
}
return accessPointIds;
}
}