fix(s3vectors): route ListVectors instead of falling through to S3 (#2285)

* fix(s3vectors): add listVectors service method with cursor pagination

ListVectors had no backing service method. Add one modeled on
listCertificates' cursor-based pagination (base64 JSON cursor over the
last returned key), sorted by vector key for stable ordering across
pages.

* fix(s3vectors): route ListVectors instead of falling through to S3

S3VectorsController declared routes for twelve S3 Vectors operations
but not ListVectors, so requests fell through to the S3 REST-XML
controller and came back as a misleading S3 InvalidArgument XML error
instead of a ListVectors response.

Add the route, request/response records (modeled on GetVectors'
returnData/returnMetadata shape), and wire it to the new
S3VectorsService.listVectors.

Fixes #2161

* test(s3vectors): add regression tests for ListVectors

Reproduces #2161: before the route existed, ListVectors fell through
to the S3 controller and returned an XML InvalidArgument error instead
of a JSON ListVectors response. Covers the basic list (with
returnData/returnMetadata) and cursor pagination via maxResults +
nextToken across two pages.

* fix(s3vectors): resolve ListVectors by indexArn and fix cursor encoding

Two issues flagged in review on #2285:

- listVectors ignored indexArn and passed it through as an unused
  request field, so an ARN-only ListVectors call (a form the input
  contract explicitly allows) resolved a null bucket name and 404'd.
  Add resolveIndex to parse "<bucketArn>/index/<indexName>" and fall
  back to it when indexName is absent.
- encodeToken/decodeToken hand-built a JSON string around the cursor
  key without escaping, so a vector key containing a double quote
  truncated the cursor and repeated or skipped pages. Drop the JSON
  wrapping entirely — the cursor only ever holds one opaque value, so
  base64-encoding the raw key round-trips any key correctly.

* fix(s3vectors): pass indexArn through to listVectors

Wire ListVectorsRequest.indexArn() into the new
S3VectorsService.listVectors(bucketName, indexName, indexArn, ...)
signature so ARN-only requests actually resolve the index instead of
being silently dropped.

* test(s3vectors): add regression tests for indexArn and quoted keys

Reproduces both issues flagged in review on #2285: confirmed
listVectorsByIndexArnOnly failed with 404 and
listVectorsPaginationRoundTripsKeysWithQuotes returned the wrong page
against the prior code, before the resolveIndex and cursor-encoding
fixes.

* fix(s3vectors): resolve indexArn using its own region, not the request's

Flagged in review on #2285: resolveIndex parsed the bucket/index names
out of indexArn with manual substring slicing but discarded the ARN's
region entirely, doing the lookup in whatever region the ListVectors
request itself was signed for. A valid cross-region indexArn (or any
request not signed for the same region as the index) silently 404'd or
could resolve a same-named index in the wrong region.

Use AwsArnUtils.parse instead of manual slicing, validate the ARN is
an s3vectors ARN, and look up the index in the region encoded in the
ARN itself.

* test(s3vectors): add regression test for cross-region indexArn lookup

Reproduces the issue flagged in review on #2285: confirmed this test
failed with 404 against the prior region-ignoring resolveIndex, using
an index created under a us-west-2 credential scope and a ListVectors
request with no Authorization header (default region).
This commit is contained in:
Feyikemi Ogunsanya authored and GitHub committed 2026-08-15 19:09:13 -04:00
1 parent 78ad357eb8
commit 6c4d63b0a9
3 files changed
+435 -2

No files matched your search

@@ -200,6 +200,24 @@ public class S3VectorsController {
Map<String, Object> metadata
) {}
@RegisterForReflection
public record ListVectorsRequest(
String vectorBucketName,
String indexName,
String indexArn,
Integer maxResults,
String nextToken,
Boolean returnData,
Boolean returnMetadata
) {}
@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ListVectorsResponse(
List<VectorGetResponseRepresentation> vectors,
String nextToken
) {}
@RegisterForReflection
public record DeleteVectorsRequest(
String vectorBucketName,
@@ -370,6 +388,28 @@ public class S3VectorsController {
return Response.ok(new GetVectorsResponse(reps)).build();
}
@POST
@Path("/ListVectors")
public Response listVectors(@Context HttpHeaders headers, ListVectorsRequest request) {
String region = regionResolver.resolveRegion(headers);
int maxResults = request.maxResults() != null ? request.maxResults() : 0;
S3VectorsService.ListVectorsResult result = service.listVectors(
request.vectorBucketName(), request.indexName(), request.indexArn(), maxResults, request.nextToken(), region);
boolean returnData = request.returnData() != null && request.returnData();
boolean returnMetadata = request.returnMetadata() != null && request.returnMetadata();
List<VectorGetResponseRepresentation> reps = result.vectors().stream()
.map(v -> {
VectorDataRepresentation data = returnData ? new VectorDataRepresentation(v.getData()) : null;
Map<String, Object> metadata = returnMetadata ? v.getMetadata() : null;
return new VectorGetResponseRepresentation(v.getKey(), data, metadata);
})
.toList();
return Response.ok(new ListVectorsResponse(reps, result.nextToken())).build();
}
@POST
@Path("/DeleteVectors")
public Response deleteVectors(@Context HttpHeaders headers, DeleteVectorsRequest request) {
@@ -13,6 +13,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@@ -154,6 +155,104 @@ public class S3VectorsService {
return result;
}
/**
* Lists the vectors stored in an index with cursor-based pagination, ordered by key.
* The index may be identified by {@code bucketName}/{@code indexName} or by {@code indexArn} alone,
* matching the ListVectors input contract.
*/
public ListVectorsResult listVectors(String bucketName, String indexName, String indexArn,
int maxResults, String nextToken, String region) {
VectorIndex index = resolveIndex(bucketName, indexName, indexArn, region);
int limit = maxResults > 0 ? Math.min(maxResults, 1000) : 500;
String lastKey = decodeToken(nextToken);
List<VectorData> allVectors = index.getVectors().values().stream()
.sorted(Comparator.comparing(VectorData::getKey))
.collect(Collectors.toList());
int startIndex = 0;
if (lastKey != null) {
for (int i = 0; i < allVectors.size(); i++) {
if (allVectors.get(i).getKey().compareTo(lastKey) > 0) {
startIndex = i;
break;
}
if (i == allVectors.size() - 1) {
startIndex = allVectors.size();
}
}
}
List<VectorData> page = allVectors.stream()
.skip(startIndex)
.limit(limit)
.collect(Collectors.toList());
String newNextToken = null;
if (startIndex + limit < allVectors.size() && !page.isEmpty()) {
newNextToken = encodeToken(page.get(page.size() - 1).getKey());
}
return new ListVectorsResult(page, newNextToken);
}
/**
* Resolves an index by name or by ARN (format: {@code <bucketArn>/index/<indexName>}), matching
* ListVectors' "vectorBucketName + indexName, or indexArn alone" input contract.
*/
private VectorIndex resolveIndex(String bucketName, String indexName, String indexArn, String region) {
if (indexName != null && !indexName.isBlank()) {
return getIndex(bucketName, indexName, region);
}
if (indexArn == null || indexArn.isBlank()) {
throw new AwsException("ValidationException", "Either indexName or indexArn is required.", 400);
}
AwsArnUtils.Arn parsed;
try {
parsed = AwsArnUtils.parse(indexArn);
} catch (IllegalArgumentException e) {
throw new AwsException("ValidationException", "Malformed indexArn: " + indexArn, 400);
}
if (!"s3vectors".equals(parsed.service())) {
throw new AwsException("ValidationException", "Not an S3 Vectors indexArn: " + indexArn, 400);
}
String resource = parsed.resource();
int marker = resource.lastIndexOf("/index/");
if (marker < 0 || !resource.startsWith("bucket/")) {
throw new AwsException("ValidationException", "Malformed indexArn: " + indexArn, 400);
}
String bucketNameFromArn = resource.substring("bucket/".length(), marker);
String indexNameFromArn = resource.substring(marker + "/index/".length());
// The ARN carries its own region — an index in one region must resolve there regardless
// of which region the ListVectors request itself was signed for.
String arnRegion = parsed.region().isEmpty() ? region : parsed.region();
return getIndex(bucketNameFromArn, indexNameFromArn, arnRegion);
}
/**
* Encodes a pagination cursor as an opaque Base64 token over the raw key — no JSON wrapping, so
* keys containing quotes or other JSON-significant characters round-trip correctly.
*/
private String encodeToken(String lastKey) {
if (lastKey == null) {
return null;
}
return Base64.getEncoder().encodeToString(lastKey.getBytes(StandardCharsets.UTF_8));
}
private String decodeToken(String token) {
if (token == null || token.isEmpty()) {
return null;
}
try {
return new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8);
} catch (Exception e) {
throw new AwsException("InvalidNextTokenException", "Invalid pagination token", 400);
}
}
public record ListVectorsResult(List<VectorData> vectors, String nextToken) {}
public void deleteVectors(String bucketName, String indexName, List<String> keys, String region) {
VectorBucket bucket = getVectorBucket(bucketName, region);
VectorIndex index = getIndex(bucketName, indexName, region);
@@ -248,6 +248,75 @@ class S3VectorsIntegrationTest {
@Test
@Order(11)
void listVectors() {
// Reproduces #2161: ListVectors previously had no route and fell through to the S3
// REST-XML controller, returning an S3 <Error>InvalidArgument</Error> instead of a
// ListVectors response.
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"returnData": true,
"returnMetadata": true
}
""".formatted(BUCKET_NAME, INDEX_NAME))
.when()
.post("/ListVectors")
.then()
.contentType(JSON_CONTENT_TYPE)
.statusCode(200)
.body("vectors", hasSize(2))
.body("vectors.find { it.key == 'v1' }.data.float32", contains(1.0f, 0.0f, 0.0f))
.body("vectors.find { it.key == 'v1' }.metadata.label", equalTo("first"))
.body("vectors.find { it.key == 'v2' }.metadata.label", equalTo("second"))
.body("nextToken", nullValue());
}
@Test
@Order(12)
void listVectorsPaginatesWithMaxResultsAndNextToken() {
String firstPageNextToken =
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"maxResults": 1
}
""".formatted(BUCKET_NAME, INDEX_NAME))
.when()
.post("/ListVectors")
.then()
.statusCode(200)
.body("vectors", hasSize(1))
.body("vectors[0].key", equalTo("v1"))
.body("nextToken", notNullValue())
.extract().path("nextToken");
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"maxResults": 1,
"nextToken": "%s"
}
""".formatted(BUCKET_NAME, INDEX_NAME, firstPageNextToken))
.when()
.post("/ListVectors")
.then()
.statusCode(200)
.body("vectors", hasSize(1))
.body("vectors[0].key", equalTo("v2"))
.body("nextToken", nullValue());
}
@Test
@Order(13)
void deleteVectors() {
given()
.contentType(JSON_CONTENT_TYPE)
@@ -282,7 +351,7 @@ class S3VectorsIntegrationTest {
}
@Test
@Order(12)
@Order(14)
void deleteIndex() {
given()
.contentType(JSON_CONTENT_TYPE)
@@ -313,7 +382,7 @@ class S3VectorsIntegrationTest {
}
@Test
@Order(13)
@Order(15)
void deleteVectorBucket() {
given()
.contentType(JSON_CONTENT_TYPE)
@@ -340,4 +409,229 @@ class S3VectorsIntegrationTest {
.then()
.statusCode(404);
}
// Self-contained: each test below creates its own bucket/index so it doesn't depend on the
// ordered setup/teardown sequence above.
@Test
@Order(16)
void listVectorsByIndexArnOnly() {
String bucketName = "arn-list-vectors-bucket";
String indexName = "arn-idx";
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{ "vectorBucketName": "%s" }
""".formatted(bucketName))
.when()
.post("/CreateVectorBucket")
.then()
.statusCode(200);
String indexArn =
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"dimension": 2,
"distanceMetric": "cosine",
"dataType": "float32"
}
""".formatted(bucketName, indexName))
.when()
.post("/CreateIndex")
.then()
.statusCode(200)
.extract().path("indexArn");
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"vectors": [{"key": "only", "data": {"float32": [1.0, 0.0]}}]
}
""".formatted(bucketName, indexName))
.when()
.post("/PutVectors")
.then()
.statusCode(200);
// ListVectors identified by indexArn alone (no vectorBucketName/indexName) must resolve
// to the same index rather than looking up a null bucket name.
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{ "indexArn": "%s" }
""".formatted(indexArn))
.when()
.post("/ListVectors")
.then()
.statusCode(200)
.body("vectors", hasSize(1))
.body("vectors[0].key", equalTo("only"));
}
@Test
@Order(17)
void listVectorsPaginationRoundTripsKeysWithQuotes() {
String bucketName = "quote-key-vector-bucket";
String indexName = "quote-idx";
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{ "vectorBucketName": "%s" }
""".formatted(bucketName))
.when()
.post("/CreateVectorBucket")
.then()
.statusCode(200);
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"dimension": 2,
"distanceMetric": "cosine",
"dataType": "float32"
}
""".formatted(bucketName, indexName))
.when()
.post("/CreateIndex")
.then()
.statusCode(200);
// One key contains a double quote — the pagination cursor must round-trip it intact
// rather than truncating at the quote.
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"vectors": [
{"key": "a\\"b", "data": {"float32": [1.0, 0.0]}},
{"key": "c", "data": {"float32": [0.0, 1.0]}}
]
}
""".formatted(bucketName, indexName))
.when()
.post("/PutVectors")
.then()
.statusCode(200);
String firstPageNextToken =
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"maxResults": 1
}
""".formatted(bucketName, indexName))
.when()
.post("/ListVectors")
.then()
.statusCode(200)
.body("vectors", hasSize(1))
.body("vectors[0].key", equalTo("a\"b"))
.body("nextToken", notNullValue())
.extract().path("nextToken");
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"maxResults": 1,
"nextToken": "%s"
}
""".formatted(bucketName, indexName, firstPageNextToken))
.when()
.post("/ListVectors")
.then()
.statusCode(200)
.body("vectors", hasSize(1))
.body("vectors[0].key", equalTo("c"))
.body("nextToken", nullValue());
}
@Test
@Order(18)
void listVectorsByIndexArnResolvesTheArnsOwnRegionNotTheRequestRegion() {
String bucketName = "cross-region-vector-bucket";
String indexName = "cross-region-idx";
String otherRegionAuth = "Credential=AKID/20260101/us-west-2/s3vectors/aws4_request";
// Create the bucket/index in us-west-2 (via the SigV4 credential scope region).
given()
.contentType(JSON_CONTENT_TYPE)
.header("Authorization", otherRegionAuth)
.body("""
{ "vectorBucketName": "%s" }
""".formatted(bucketName))
.when()
.post("/CreateVectorBucket")
.then()
.statusCode(200);
String indexArn =
given()
.contentType(JSON_CONTENT_TYPE)
.header("Authorization", otherRegionAuth)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"dimension": 2,
"distanceMetric": "cosine",
"dataType": "float32"
}
""".formatted(bucketName, indexName))
.when()
.post("/CreateIndex")
.then()
.statusCode(200)
.body("indexArn", containsString(":us-west-2:"))
.extract().path("indexArn");
given()
.contentType(JSON_CONTENT_TYPE)
.header("Authorization", otherRegionAuth)
.body("""
{
"vectorBucketName": "%s",
"indexName": "%s",
"vectors": [{"key": "only", "data": {"float32": [1.0, 0.0]}}]
}
""".formatted(bucketName, indexName))
.when()
.post("/PutVectors")
.then()
.statusCode(200);
// No Authorization header here, so this request resolves to the default region — not
// us-west-2. The lookup must still succeed because it honors the region encoded in the
// ARN itself, not the region the ListVectors request happens to be signed for.
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{ "indexArn": "%s" }
""".formatted(indexArn))
.when()
.post("/ListVectors")
.then()
.statusCode(200)
.body("vectors", hasSize(1))
.body("vectors[0].key", equalTo("only"));
}
}