mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
a5ab677d6b
commit
d97574207b
10 files changed
+473
-56
No files matched your search
+9
-30
@@ -130,11 +130,7 @@ public class S3VectorsCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
private static String validateName(String name, String property, boolean index) {
|
||||
String pattern = index ? "[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]" : "[a-z0-9][a-z0-9-]{1,61}[a-z0-9]";
|
||||
if (!name.matches(pattern)) {
|
||||
throw validation(property + " must be a valid lowercase name of 3 to 63 characters.");
|
||||
}
|
||||
return name;
|
||||
return S3VectorsService.validateName(name, property, index, "ValidationError");
|
||||
}
|
||||
|
||||
private static void rejectSameNameReplacement(ProvisionContext ctx, boolean updatesEntity, String resourcePath) {
|
||||
@@ -144,36 +140,19 @@ public class S3VectorsCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
private static String bucketName(JsonNode props, ProvisionContext ctx) {
|
||||
String name = ctx.resolveOptional(props, "VectorBucketName");
|
||||
String arn = ctx.resolveOptional(props, "VectorBucketArn");
|
||||
if ((name == null) == (arn == null)) {
|
||||
throw validation("Specify exactly one of VectorBucketName or VectorBucketArn.");
|
||||
}
|
||||
if (arn == null) {
|
||||
return validateName(name, "VectorBucketName", false);
|
||||
}
|
||||
String[] parts = arnParts(arn, false);
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
if (!ctx.region().equals(parsed.region()) || !ctx.accountId().equals(parsed.accountId())
|
||||
|| !AwsRegions.partitionFor(ctx.region()).equals(parsed.partition())) {
|
||||
S3VectorsService.ResolvedBucket resolved = S3VectorsService.resolveBucket(
|
||||
ctx.resolveOptional(props, "VectorBucketName"),
|
||||
ctx.resolveOptional(props, "VectorBucketArn"),
|
||||
ctx.region(), AwsRegions::partitionFor, "ValidationError");
|
||||
if (resolved.accountId() != null
|
||||
&& (!ctx.region().equals(resolved.region()) || !ctx.accountId().equals(resolved.accountId()))) {
|
||||
throw validation("VectorBucketArn must belong to the stack's region and account.");
|
||||
}
|
||||
return validateName(parts[1], "VectorBucketArn bucket name", false);
|
||||
return resolved.bucketName();
|
||||
}
|
||||
|
||||
private static String[] arnParts(String arn, boolean index) {
|
||||
try {
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String[] parts = parsed.resource().split("/", -1);
|
||||
if (!"s3vectors".equals(parsed.service()) || parts.length != (index ? 4 : 2)
|
||||
|| !"bucket".equals(parts[0]) || parts[1].isBlank()
|
||||
|| (index && (!"index".equals(parts[2]) || parts[3].isBlank()))) {
|
||||
throw validation("Invalid S3 Vectors ARN: " + arn);
|
||||
}
|
||||
return parts;
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw validation("Invalid S3 Vectors ARN: " + arn);
|
||||
}
|
||||
return S3VectorsService.arnParts(arn, index, "ValidationError");
|
||||
}
|
||||
|
||||
private static Object encryption(JsonNode props, ProvisionContext ctx, String defaultType) {
|
||||
|
||||
@@ -102,6 +102,30 @@ public class S3VectorsController {
|
||||
String vectorBucketArn
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record PutVectorBucketPolicyRequest(
|
||||
String vectorBucketName,
|
||||
String vectorBucketArn,
|
||||
String policy
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record GetVectorBucketPolicyRequest(
|
||||
String vectorBucketName,
|
||||
String vectorBucketArn
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record GetVectorBucketPolicyResponse(
|
||||
String policy
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record DeleteVectorBucketPolicyRequest(
|
||||
String vectorBucketName,
|
||||
String vectorBucketArn
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record CreateIndexRequest(
|
||||
String vectorBucketName,
|
||||
@@ -316,6 +340,43 @@ public class S3VectorsController {
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/PutVectorBucketPolicy")
|
||||
public Response putVectorBucketPolicy(@Context HttpHeaders headers, PutVectorBucketPolicyRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
service.putVectorBucketPolicy(
|
||||
request != null ? request.vectorBucketName() : null,
|
||||
request != null ? request.vectorBucketArn() : null,
|
||||
request != null ? request.policy() : null,
|
||||
region
|
||||
);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/GetVectorBucketPolicy")
|
||||
public Response getVectorBucketPolicy(@Context HttpHeaders headers, GetVectorBucketPolicyRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
String policy = service.getVectorBucketPolicy(
|
||||
request != null ? request.vectorBucketName() : null,
|
||||
request != null ? request.vectorBucketArn() : null,
|
||||
region
|
||||
);
|
||||
return Response.ok(new GetVectorBucketPolicyResponse(policy)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/DeleteVectorBucketPolicy")
|
||||
public Response deleteVectorBucketPolicy(@Context HttpHeaders headers, DeleteVectorBucketPolicyRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
service.deleteVectorBucketPolicy(
|
||||
request != null ? request.vectorBucketName() : null,
|
||||
request != null ? request.vectorBucketArn() : null,
|
||||
region
|
||||
);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/CreateIndex")
|
||||
public Response createIndex(@Context HttpHeaders headers, CreateIndexRequest request) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package io.floci.aws.services.s3vectors;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
@@ -13,6 +17,7 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
@@ -24,11 +29,14 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class S3VectorsService {
|
||||
private static final Logger LOG = Logger.getLogger(S3VectorsService.class);
|
||||
private static final ObjectReader STRICT_JSON_READER =
|
||||
new ObjectMapper().reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
|
||||
|
||||
private final AccountAwareStorageBackend<VectorBucket> store;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -61,7 +69,7 @@ public class S3VectorsService {
|
||||
}
|
||||
|
||||
private String buildBucketArn(String region, String bucketName) {
|
||||
return AwsArnUtils.Arn.of("s3vectors", region, regionResolver.getAccountId(), "bucket/" + bucketName).toString();
|
||||
return regionResolver.buildArn("s3vectors", region, "bucket/" + bucketName);
|
||||
}
|
||||
|
||||
private String buildIndexArn(String region, String bucketName, String indexName) {
|
||||
@@ -153,6 +161,113 @@ public class S3VectorsService {
|
||||
}
|
||||
}
|
||||
|
||||
public void putVectorBucketPolicy(String bucketName, String bucketArn, String policy, String region) {
|
||||
ResolvedBucket target = resolveBucket(bucketName, bucketArn, region);
|
||||
validatePolicyDocument(policy);
|
||||
synchronized (lockFor(target.bucketName(), target.region())) {
|
||||
VectorBucket bucket = getVectorBucket(target.bucketName(), target.region());
|
||||
bucket.setPolicy(policy);
|
||||
store.put(buildStorageKey(target.region(), target.bucketName()), bucket);
|
||||
LOG.infov("Put policy on Vector Bucket: {0} in region {1}", target.bucketName(), target.region());
|
||||
}
|
||||
}
|
||||
|
||||
public String getVectorBucketPolicy(String bucketName, String bucketArn, String region) {
|
||||
ResolvedBucket target = resolveBucket(bucketName, bucketArn, region);
|
||||
synchronized (lockFor(target.bucketName(), target.region())) {
|
||||
VectorBucket bucket = getVectorBucket(target.bucketName(), target.region());
|
||||
String policy = bucket.getPolicy();
|
||||
if (policy == null) {
|
||||
throw new AwsException("NotFoundException",
|
||||
"The vector bucket policy for " + target.bucketName() + " does not exist.", 404);
|
||||
}
|
||||
return policy;
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteVectorBucketPolicy(String bucketName, String bucketArn, String region) {
|
||||
ResolvedBucket target = resolveBucket(bucketName, bucketArn, region);
|
||||
synchronized (lockFor(target.bucketName(), target.region())) {
|
||||
VectorBucket bucket = getVectorBucket(target.bucketName(), target.region());
|
||||
if (bucket.getPolicy() != null) {
|
||||
bucket.setPolicy(null);
|
||||
store.put(buildStorageKey(target.region(), target.bucketName()), bucket);
|
||||
}
|
||||
LOG.infov("Deleted policy from Vector Bucket: {0} in region {1}", target.bucketName(), target.region());
|
||||
}
|
||||
}
|
||||
|
||||
private ResolvedBucket resolveBucket(String bucketName, String bucketArn, String region) {
|
||||
ResolvedBucket resolved = resolveBucket(bucketName, bucketArn, region,
|
||||
regionResolver::partitionForRegion, "ValidationException");
|
||||
if (resolved.accountId() != null && !store.accountId().equals(resolved.accountId())) {
|
||||
throw new AwsException("NotFoundException",
|
||||
"The vector bucket " + resolved.bucketName() + " does not exist.", 404);
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
public static ResolvedBucket resolveBucket(String bucketName, String bucketArn,
|
||||
String defaultRegion, Function<String, String> partitionForRegion,
|
||||
String errorCode) {
|
||||
if ((bucketName == null) == (bucketArn == null)) {
|
||||
throw new AwsException(errorCode,
|
||||
"Specify exactly one of VectorBucketName or VectorBucketArn.", 400);
|
||||
}
|
||||
if (bucketArn == null) {
|
||||
return new ResolvedBucket(validateName(bucketName, "VectorBucketName", false, errorCode),
|
||||
defaultRegion, null);
|
||||
}
|
||||
String[] parts = arnParts(bucketArn, false, errorCode);
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(bucketArn);
|
||||
String name = validateName(parts[1], "VectorBucketArn bucket name", false, errorCode);
|
||||
if (parsed.region().isEmpty() || parsed.accountId().isEmpty()
|
||||
|| !partitionForRegion.apply(parsed.region()).equals(parsed.partition())) {
|
||||
throw new AwsException(errorCode, "Invalid S3 Vectors ARN: " + bucketArn, 400);
|
||||
}
|
||||
return new ResolvedBucket(name, parsed.region(), parsed.accountId());
|
||||
}
|
||||
|
||||
public static String validateName(String name, String property, boolean index, String errorCode) {
|
||||
String pattern = index ? "[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]" : "[a-z0-9][a-z0-9-]{1,61}[a-z0-9]";
|
||||
if (name == null || !name.matches(pattern)) {
|
||||
throw new AwsException(errorCode,
|
||||
property + " must be a valid lowercase name of 3 to 63 characters.", 400);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
public static String[] arnParts(String arn, boolean index, String errorCode) {
|
||||
try {
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String[] parts = parsed.resource().split("/", -1);
|
||||
if (!"s3vectors".equals(parsed.service()) || parts.length != (index ? 4 : 2)
|
||||
|| !"bucket".equals(parts[0]) || parts[1].isBlank()
|
||||
|| (index && (!"index".equals(parts[2]) || parts[3].isBlank()))) {
|
||||
throw new AwsException(errorCode, "Invalid S3 Vectors ARN: " + arn, 400);
|
||||
}
|
||||
return parts;
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException(errorCode, "Invalid S3 Vectors ARN: " + arn, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePolicyDocument(String policy) {
|
||||
if (policy == null || policy.isBlank()) {
|
||||
throw new AwsException("ValidationException", "Policy must be a non-empty JSON object.", 400);
|
||||
}
|
||||
try {
|
||||
JsonNode parsed = STRICT_JSON_READER.readTree(policy);
|
||||
if (parsed == null || !parsed.isObject()) {
|
||||
throw new AwsException("ValidationException", "Policy must be a valid JSON object.", 400);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new AwsException("ValidationException", "Policy must be a valid JSON object.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public record ResolvedBucket(String bucketName, String region, String accountId) {}
|
||||
|
||||
public VectorIndex createIndex(String bucketName, String indexName, int dimension, String dataType,
|
||||
String distanceMetric, List<String> nonFilterableMetadataKeys, String region) {
|
||||
return createIndex(bucketName, indexName, dimension, dataType, distanceMetric,
|
||||
@@ -328,26 +443,12 @@ public class S3VectorsService {
|
||||
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());
|
||||
String[] parts = arnParts(indexArn, true, "ValidationException");
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(indexArn);
|
||||
// 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);
|
||||
return getIndex(parts[1], parts[3], arnRegion);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,6 +13,7 @@ public class VectorBucket {
|
||||
private String vectorBucketName;
|
||||
private String vectorBucketArn;
|
||||
private Object encryptionConfiguration;
|
||||
private String policy;
|
||||
private Map<String, VectorIndex> indexes = new ConcurrentHashMap<>();
|
||||
|
||||
public VectorBucket() {}
|
||||
@@ -38,6 +39,9 @@ public class VectorBucket {
|
||||
public Object getEncryptionConfiguration() { return encryptionConfiguration; }
|
||||
public void setEncryptionConfiguration(Object encryptionConfiguration) { this.encryptionConfiguration = encryptionConfiguration; }
|
||||
|
||||
public String getPolicy() { return policy; }
|
||||
public void setPolicy(String policy) { this.policy = policy; }
|
||||
|
||||
public Map<String, VectorIndex> getIndexes() { return indexes; }
|
||||
public void setIndexes(Map<String, VectorIndex> indexes) { this.indexes = indexes; }
|
||||
}
|
||||
Reference in new issue
Block a user