fix(s3): page ListBuckets with max-buckets and continuation-token (#7846)

ListBuckets returned every bucket in one response and ignored both
pagination parameters. Buckets now come back in name order, at most
max-buckets per page (1 to 10000), with a ContinuationToken on every page
but the last. A max-buckets out of range, or a continuation token that
is unreadable or was issued to another account, is refused with
InvalidArgument. Without max-buckets the whole list is still returned.

Fixes #7618
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-11-09 20:57:28 -04:00
1 parent 69da60d07f
commit 6710ce4d46
4 files changed
+155 -5

No files matched your search

+5 -2
View File
@@ -37,8 +37,11 @@
`ListBuckets` includes each bucket's `BucketRegion` when `bucket-region`, `continuation-token`,
`max-buckets`, or `prefix` is present. It uses the stored bucket region, falling back to the
configured default region. Bare requests omit the field. ListBuckets filtering and pagination
are not implemented; supplying these parameters does not limit the returned bucket list.
configured default region. Bare requests omit the field. Buckets are listed in name order and
paged by `max-buckets` (1 to 10000), with a `ContinuationToken` on every page but the last; a
`max-buckets` out of range or a token that is unreadable or was issued to another account is
refused with `InvalidArgument`. Filtering by `prefix` and `bucket-region` is not implemented;
those parameters do not limit the returned bucket list.
## Event Notifications
@@ -12,6 +12,8 @@ import io.floci.aws.core.common.AwsRegions;
import io.floci.aws.core.common.Digests;
import io.floci.aws.core.common.IamEnforcementFilter;
import io.floci.aws.core.common.MultipartFormParser;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.Pagination;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestContext;
import io.floci.aws.core.common.XmlBuilder;
@@ -260,7 +262,9 @@ public class S3Controller {
try {
boolean includeRegion = List.of("bucket-region", "continuation-token", "max-buckets", "prefix")
.stream().anyMatch(queryParams::containsKey);
List<Bucket> buckets = s3Service.listBuckets();
PaginatedResult<Bucket> page = s3Service.listBuckets(
Pagination.parseMaxResults(queryParams.getFirst("max-buckets"), "InvalidArgument"),
queryParams.getFirst("continuation-token"));
XmlBuilder xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("ListAllMyBucketsResult", AwsNamespaces.S3)
@@ -269,7 +273,7 @@ public class S3Controller {
.elem("DisplayName", S3Service.DEFAULT_OWNER_DISPLAY_NAME)
.end("Owner")
.start("Buckets");
for (Bucket b : buckets) {
for (Bucket b : page.items()) {
xml.start("Bucket")
.elem("Name", b.getName())
.elem("CreationDate", ISO_FORMAT.format(b.getCreationDate()));
@@ -279,7 +283,11 @@ public class S3Controller {
}
xml.end("Bucket");
}
xml.end("Buckets").end("ListAllMyBucketsResult");
xml.end("Buckets");
if (page.nextToken() != null) {
xml.elem("ContinuationToken", page.nextToken());
}
xml.end("ListAllMyBucketsResult");
return Response.ok(xml.build()).type(MediaType.APPLICATION_XML).build();
} catch (AwsException e) {
return xmlErrorResponse(e);
@@ -15,6 +15,8 @@ import io.floci.aws.core.common.AwsRegions;
import io.floci.aws.core.common.Base64Url;
import io.floci.aws.core.common.Digests;
import io.floci.aws.core.common.FileTrees;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.Pagination;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestScopes;
import io.floci.aws.core.common.Resettable;
@@ -104,6 +106,8 @@ import java.util.stream.Stream;
@ApplicationScoped
public class S3Service implements Resettable, ResourceProvider {
private static final int MAX_LIST_BUCKETS = 10000;
private String ownerId() {
if (regionResolver != null) {
return regionResolver.getAccountId();
@@ -620,6 +624,17 @@ public class S3Service implements Resettable, ResourceProvider {
.toList();
}
public PaginatedResult<Bucket> listBuckets(Integer maxBuckets, String continuationToken) {
if (maxBuckets != null && (maxBuckets < 1 || maxBuckets > MAX_LIST_BUCKETS)) {
throw new AwsException("InvalidArgument",
"max-buckets must be between 1 and " + MAX_LIST_BUCKETS + ".", 400);
}
return Pagination.paginate(listBuckets(), Bucket::getName,
maxBuckets == null ? MAX_LIST_BUCKETS : maxBuckets, continuationToken,
"buckets:" + ownerId(),
token -> new AwsException("InvalidArgument", "The continuation token provided is incorrect.", 400));
}
private static boolean isScratchBucket(Bucket bucket) {
return Arrays.stream(S3ScratchSpace.values()).anyMatch(space -> space.matchesBucket(bucket));
}
@@ -0,0 +1,124 @@
package io.floci.aws.services.s3;
import io.floci.aws.testing.AwsTestClients;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.Bucket;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class S3ListBucketsPaginationIntegrationTest {
// Buckets are listed per account, so an account no other test uses holds exactly these buckets.
private static final String ACCOUNT = "444455556666";
private static final String OTHER_ACCOUNT = "444455557777";
private static final String PREFIX = "s3-page-" + UUID.randomUUID().toString().substring(0, 8);
private static final List<String> BUCKETS = List.of(PREFIX + "-a", PREFIX + "-b", PREFIX + "-c");
private static S3Client client(String account) {
return AwsTestClients.create(S3Client.builder().forcePathStyle(true), account, Region.US_EAST_1);
}
@Test
@Order(1)
void createBuckets() {
try (S3Client s3 = client(ACCOUNT)) {
for (String bucket : BUCKETS) {
s3.createBucket(r -> r.bucket(bucket));
}
}
}
@Test
@Order(2)
void listBucketsPagesOneBucketAtATimeWithATokenOnEveryPageButTheLast() {
try (S3Client s3 = client(ACCOUNT)) {
List<String> seen = new ArrayList<>();
List<Boolean> tokens = new ArrayList<>();
String token = null;
do {
String next = token;
ListBucketsResponse page = s3.listBuckets(r -> r.maxBuckets(1).continuationToken(next));
assertEquals(1, page.buckets().size());
page.buckets().stream().map(Bucket::name).forEach(seen::add);
token = page.continuationToken();
tokens.add(token != null);
} while (token != null && seen.size() < 10);
assertEquals(BUCKETS, seen);
assertEquals(List.of(true, true, false), tokens);
}
}
@Test
@Order(3)
void listBucketsWithoutMaxBucketsReturnsEveryBucketWithoutAToken() {
try (S3Client s3 = client(ACCOUNT)) {
ListBucketsResponse page = s3.listBuckets();
assertEquals(BUCKETS, page.buckets().stream().map(Bucket::name).toList());
assertNull(page.continuationToken());
}
}
@ParameterizedTest
@Order(4)
@ValueSource(ints = {0, 10001})
void listBucketsRefusesAMaxBucketsOutsideOneToTenThousand(int maxBuckets) {
try (S3Client s3 = client(ACCOUNT)) {
S3Exception e = assertThrows(S3Exception.class, () -> s3.listBuckets(r -> r.maxBuckets(maxBuckets)));
assertEquals(400, e.statusCode());
assertEquals("InvalidArgument", e.awsErrorDetails().errorCode());
}
}
@Test
@Order(5)
void listBucketsRefusesAnUnreadableToken() {
try (S3Client s3 = client(ACCOUNT)) {
S3Exception e = assertThrows(S3Exception.class,
() -> s3.listBuckets(r -> r.continuationToken("not-a-token")));
assertEquals(400, e.statusCode());
assertEquals("InvalidArgument", e.awsErrorDetails().errorCode());
}
}
@Test
@Order(6)
void listBucketsRefusesATokenIssuedToAnotherAccount() {
String token;
try (S3Client s3 = client(ACCOUNT)) {
token = s3.listBuckets(r -> r.maxBuckets(1)).continuationToken();
}
try (S3Client other = client(OTHER_ACCOUNT)) {
S3Exception e = assertThrows(S3Exception.class, () -> other.listBuckets(r -> r.continuationToken(token)));
assertEquals("InvalidArgument", e.awsErrorDetails().errorCode());
}
}
@Test
@Order(9)
void deleteBuckets() {
try (S3Client s3 = client(ACCOUNT)) {
for (String bucket : BUCKETS) {
s3.deleteBucket(r -> r.bucket(bucket));
}
}
}
}