feat(s3vectors): support vector bucket policy operations (#7825)

* feat(s3vectors): implement vector bucket policy management (create, get, delete)

* fix(s3vectors): validate bucket name length and ARN account in policy operations

* refactor(s3vectors): share bucket selector validation and use partitionForRegion
This commit is contained in:
Koushik Dutta authored and GitHub committed 2026-11-10 13:58:19 -04:00
1 parent a5ab677d6b
commit d97574207b
10 files changed
+473 -56

No files matched your search

@@ -9,6 +9,8 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@@ -718,4 +720,72 @@ class S3VectorsIntegrationTest {
.body("vectors", hasSize(1))
.body("vectors[0].key", equalTo("only"));
}
@Test
@Order(19)
void vectorBucketPolicyLifecycleByNameAndArn() {
String bucketName = "policy-vector-bucket";
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3vectors:*\",\"Resource\":\"*\"}]}";
String bucketArn =
given()
.contentType(JSON_CONTENT_TYPE)
.body("""
{ "vectorBucketName": "%s" }
""".formatted(bucketName))
.when()
.post("/CreateVectorBucket")
.then()
.statusCode(200)
.extract().path("vectorBucketArn");
given()
.contentType(JSON_CONTENT_TYPE)
.body(Map.of("vectorBucketName", bucketName))
.when()
.post("/GetVectorBucketPolicy")
.then()
.statusCode(404);
given()
.contentType(JSON_CONTENT_TYPE)
.body(Map.of("vectorBucketName", bucketName, "policy", policy))
.when()
.post("/PutVectorBucketPolicy")
.then()
.statusCode(200);
given()
.contentType(JSON_CONTENT_TYPE)
.body(Map.of("vectorBucketArn", bucketArn))
.when()
.post("/GetVectorBucketPolicy")
.then()
.statusCode(200)
.body("policy", equalTo(policy));
given()
.contentType(JSON_CONTENT_TYPE)
.body(Map.of("vectorBucketArn", bucketArn))
.when()
.post("/DeleteVectorBucketPolicy")
.then()
.statusCode(200);
given()
.contentType(JSON_CONTENT_TYPE)
.body(Map.of("vectorBucketName", bucketName))
.when()
.post("/GetVectorBucketPolicy")
.then()
.statusCode(404);
given()
.contentType(JSON_CONTENT_TYPE)
.body(Map.of("vectorBucketName", bucketName))
.when()
.post("/DeleteVectorBucket")
.then()
.statusCode(200);
}
}
@@ -16,6 +16,7 @@ import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3vectors.S3VectorsClient;
import software.amazon.awssdk.services.s3vectors.model.Index;
import software.amazon.awssdk.services.s3vectors.model.NotFoundException;
import software.amazon.awssdk.services.s3vectors.model.PutInputVector;
import software.amazon.awssdk.services.s3vectors.model.QueryVectorsResponse;
import software.amazon.awssdk.services.s3vectors.model.ValidationException;
@@ -240,6 +241,47 @@ class S3VectorsSdkIntegrationTest {
}
}
@Test
void vectorBucketPolicyLifecycleAndValidationThroughTheSdk() {
String name = "policy-vectors-" + UUID.randomUUID().toString().substring(0, 8);
String policyV1 = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Read\",\"Effect\":\"Allow\"}]}";
String policyV2 = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Write\",\"Effect\":\"Deny\"}]}";
try (S3VectorsClient vectors = vectors("us-east-1")) {
String bucketArn = vectors.createVectorBucket(request -> request.vectorBucketName(name)).vectorBucketArn();
try {
NotFoundException initialMissing = assertThrows(NotFoundException.class,
() -> vectors.getVectorBucketPolicy(request -> request.vectorBucketName(name)));
assertEquals(404, initialMissing.statusCode());
vectors.putVectorBucketPolicy(request -> request.vectorBucketName(name).policy(policyV1));
assertEquals(policyV1,
vectors.getVectorBucketPolicy(request -> request.vectorBucketName(name)).policy());
assertEquals(policyV1,
vectors.getVectorBucketPolicy(request -> request.vectorBucketArn(bucketArn)).policy());
vectors.putVectorBucketPolicy(request -> request.vectorBucketArn(bucketArn).policy(policyV2));
assertEquals(policyV2,
vectors.getVectorBucketPolicy(request -> request.vectorBucketName(name)).policy());
ValidationException invalidJson = assertThrows(ValidationException.class,
() -> vectors.putVectorBucketPolicy(request -> request.vectorBucketName(name).policy("not-json")));
assertEquals(400, invalidJson.statusCode());
ValidationException bothIdentifiers = assertThrows(ValidationException.class,
() -> vectors.getVectorBucketPolicy(request -> request
.vectorBucketName(name).vectorBucketArn(bucketArn)));
assertEquals(400, bothIdentifiers.statusCode());
vectors.deleteVectorBucketPolicy(request -> request.vectorBucketArn(bucketArn));
NotFoundException deletedMissing = assertThrows(NotFoundException.class,
() -> vectors.getVectorBucketPolicy(request -> request.vectorBucketName(name)));
assertEquals(404, deletedMissing.statusCode());
} finally {
vectors.deleteVectorBucket(request -> request.vectorBucketName(name));
}
}
}
private S3VectorsClient vectors(String region) {
return S3VectorsClient.builder().endpointOverride(endpoint).region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
@@ -14,6 +14,7 @@ import org.junit.jupiter.api.Test;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -28,15 +29,19 @@ class S3VectorsServiceTest {
@BeforeEach
void setUp() {
StorageFactory factory = new StorageFactory(null, null) {
StorageFactory factory = inMemoryFactory();
service = new S3VectorsService(factory, new RegionResolver(REGION, "000000000000"),
Clock.fixed(CREATION_TIME, ZoneOffset.UTC));
}
private static StorageFactory inMemoryFactory() {
return new StorageFactory(null, null) {
@Override
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
TypeReference<Map<String, V>> typeReference) {
return AccountAwareStorageBackend.inMemory("000000000000");
}
};
service = new S3VectorsService(factory, new RegionResolver(REGION, "000000000000"),
Clock.fixed(CREATION_TIME, ZoneOffset.UTC));
}
@Test
@@ -131,4 +136,125 @@ class S3VectorsServiceTest {
VectorIndex index = service.createIndex("bucket", "index", 3, "float32", "euclidean", keys, REGION);
assertEquals(keys, index.getNonFilterableMetadataKeys());
}
@Test
void vectorBucketPolicyLifecycleAndPersistenceByNameAndArn() throws Exception {
VectorBucket bucket = service.createVectorBucket("bucket", null, REGION);
String policyV1 = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
String policyV2 = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"}]}";
AwsException missingInitial = assertThrows(AwsException.class,
() -> service.getVectorBucketPolicy("bucket", null, REGION));
assertEquals("NotFoundException", missingInitial.getErrorCode());
service.putVectorBucketPolicy("bucket", null, policyV1, REGION);
assertEquals(policyV1, service.getVectorBucketPolicy("bucket", null, REGION));
assertEquals(policyV1, service.getVectorBucketPolicy(null, bucket.getVectorBucketArn(), REGION));
ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
VectorBucket restored = mapper.readValue(mapper.writeValueAsString(bucket), VectorBucket.class);
assertEquals(policyV1, restored.getPolicy());
service.putVectorBucketPolicy(null, bucket.getVectorBucketArn(), policyV2, REGION);
assertEquals(policyV2, service.getVectorBucketPolicy("bucket", null, REGION));
service.deleteVectorBucketPolicy(null, bucket.getVectorBucketArn(), REGION);
AwsException afterDelete = assertThrows(AwsException.class,
() -> service.getVectorBucketPolicy("bucket", null, REGION));
assertEquals("NotFoundException", afterDelete.getErrorCode());
// Deleting again when no policy is attached remains idempotent on an existing bucket.
service.deleteVectorBucketPolicy("bucket", null, REGION);
}
@Test
void vectorBucketPolicyByArnResolvesTheArnsOwnRegion() {
VectorBucket westBucket = service.createVectorBucket("west-bucket", null, "us-west-2");
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
service.putVectorBucketPolicy(null, westBucket.getVectorBucketArn(), policy, REGION);
assertEquals(policy, service.getVectorBucketPolicy(null, westBucket.getVectorBucketArn(), REGION));
assertEquals(policy, service.getVectorBucketPolicy("west-bucket", null, "us-west-2"));
service.deleteVectorBucketPolicy(null, westBucket.getVectorBucketArn(), REGION);
AwsException notFound = assertThrows(AwsException.class,
() -> service.getVectorBucketPolicy("west-bucket", null, "us-west-2"));
assertEquals("NotFoundException", notFound.getErrorCode());
// Custom region in a non-commercial deployment stays in the deployment's partition.
S3VectorsService chinaService = new S3VectorsService(inMemoryFactory(),
new RegionResolver("cn-north-1", "000000000000"),
Clock.fixed(CREATION_TIME, ZoneOffset.UTC));
VectorBucket customRegionBucket = chinaService.createVectorBucket("custom-bucket", null, "custom-region-1");
assertTrue(customRegionBucket.getVectorBucketArn().startsWith("arn:aws-cn:s3vectors:custom-region-1:"));
chinaService.putVectorBucketPolicy(null, customRegionBucket.getVectorBucketArn(), policy, "cn-north-1");
assertEquals(policy, chinaService.getVectorBucketPolicy("custom-bucket", null, "custom-region-1"));
}
@Test
void vectorBucketPolicyRejectsInvalidInputsAndMissingBuckets() {
VectorBucket bucket = service.createVectorBucket("bucket", null, REGION);
VectorIndex index = service.createIndex("bucket", "index", 3, "float32", "cosine", List.of(), REGION);
String validPolicy = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
service.putVectorBucketPolicy("bucket", null, validPolicy, REGION);
// Missing bucket or foreign-account ARN returns NotFoundException (404) and leaves local bucket untouched.
String foreignAccountArn = "arn:aws:s3vectors:us-east-1:111122223333:bucket/bucket";
for (Runnable action : List.<Runnable>of(
() -> service.putVectorBucketPolicy("missing", null, validPolicy, REGION),
() -> service.getVectorBucketPolicy("missing", null, REGION),
() -> service.deleteVectorBucketPolicy("missing", null, REGION),
() -> service.putVectorBucketPolicy(null, foreignAccountArn, "{\"Statement\":[]}", REGION),
() -> service.getVectorBucketPolicy(null, foreignAccountArn, REGION),
() -> service.deleteVectorBucketPolicy(null, foreignAccountArn, REGION))) {
assertRejected("NotFoundException", 404, validPolicy, action);
}
// Neither, both, or invalid bucket names return ValidationException (400).
String overlongName = "a".repeat(64);
for (Runnable action : List.<Runnable>of(
() -> service.putVectorBucketPolicy(null, null, validPolicy, REGION),
() -> service.putVectorBucketPolicy(" ", "", validPolicy, REGION),
() -> service.putVectorBucketPolicy("ab", null, validPolicy, REGION),
() -> service.putVectorBucketPolicy("Upper", null, validPolicy, REGION),
() -> service.putVectorBucketPolicy(overlongName, null, validPolicy, REGION),
() -> service.putVectorBucketPolicy("bucket", bucket.getVectorBucketArn(), validPolicy, REGION),
() -> service.getVectorBucketPolicy(null, null, REGION),
() -> service.getVectorBucketPolicy("ab", null, REGION),
() -> service.getVectorBucketPolicy(overlongName, null, REGION),
() -> service.getVectorBucketPolicy("bucket", bucket.getVectorBucketArn(), REGION),
() -> service.deleteVectorBucketPolicy(null, null, REGION),
() -> service.deleteVectorBucketPolicy("ab", null, REGION),
() -> service.deleteVectorBucketPolicy(overlongName, null, REGION),
() -> service.deleteVectorBucketPolicy("bucket", bucket.getVectorBucketArn(), REGION))) {
assertRejected("ValidationException", 400, validPolicy, action);
}
// Malformed, regionless, wrong-resource, or partition-mismatched ARNs return ValidationException (400).
for (String invalidArn : List.of(
"not-an-arn",
"arn:aws:s3:::bucket",
"arn:aws:s3vectors:us-east-1:000000000000:bucket/",
"arn:aws:s3vectors:us-east-1:000000000000:bucket/ab",
"arn:aws:s3vectors::000000000000:bucket/bucket",
"arn:aws:s3vectors:us-east-1::bucket/bucket",
"arn:aws-cn:s3vectors:us-east-1:000000000000:bucket/bucket",
index.getIndexArn())) {
assertRejected("ValidationException", 400, validPolicy,
() -> service.putVectorBucketPolicy(null, invalidArn, validPolicy, REGION));
}
// Non-JSON or non-object policy documents return ValidationException (400) without mutating stored policy.
for (String invalidPolicy : Arrays.asList(null, "", " ", "not-json", "[]", "\"string\"", "{} trailing")) {
assertRejected("ValidationException", 400, validPolicy,
() -> service.putVectorBucketPolicy("bucket", null, invalidPolicy, REGION));
}
}
private void assertRejected(String errorCode, int status, String expectedPolicy, Runnable action) {
AwsException error = assertThrows(AwsException.class, action::run);
assertEquals(errorCode, error.getErrorCode());
assertEquals(status, error.getHttpStatus());
assertEquals(expectedPolicy, service.getVectorBucketPolicy("bucket", null, REGION));
}
}
@@ -150,6 +150,34 @@ class S3VectorsWriteLockTest {
assertEquals(Map.of("new", "tag"), service.getVectorBucket("bucket", REGION).getTags());
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void bucketPolicyDoesNotOverwriteConcurrentIndexCreation(boolean copyOnAccess) throws Exception {
setUp(copyOnAccess);
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
overlap(() -> service.putVectorBucketPolicy("bucket", null, policy, REGION), this::createIndex);
assertEquals(policy, service.getVectorBucketPolicy("bucket", null, REGION));
assertEquals("index", service.getIndex("bucket", "index", REGION).getIndexName());
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void bucketPolicyUpdateCannotResurrectADeletedOrRecreatedBucket(boolean copyOnAccess) throws Exception {
setUp(copyOnAccess);
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
overlap(() -> service.putVectorBucketPolicy("bucket", null, policy, REGION),
() -> service.deleteVectorBucket("bucket", REGION));
assertThrows(AwsException.class, () -> service.getVectorBucket("bucket", REGION));
service.createVectorBucket("bucket", null, REGION);
overlap(() -> service.putVectorBucketPolicy("bucket", null, policy, REGION), () -> {
service.deleteVectorBucket("bucket", REGION);
service.createVectorBucket("bucket", null, Map.of("new", "tag"), REGION);
});
assertEquals(Map.of("new", "tag"), service.getVectorBucket("bucket", REGION).getTags());
assertThrows(AwsException.class, () -> service.getVectorBucketPolicy("bucket", null, REGION));
}
private void putVector(String key) {
service.putVectors("bucket", "index", List.of(new VectorData(key, List.of(1.0f), Map.of())), REGION);
}