mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3vectors): give resource-tag conditions the tags of the vector bucket or index a request names (#8797)
The generated request resources give S3 Vectors' VectorBucket and Index types the aws:ResourceTag/ key, but no service answered the s3vectors namespace's stored tags, so a condition on a vector bucket's or an index's tags saw none. S3VectorsService now answers it: storedTags reads the bucket stored under the Region the ARN names and its name, in the request's account, where the handlers read, in place, and matches the bucket, or the index an index ARN names, on the whole ARN; an index ARN reads the index's own tags. Tags are only ever replaced by a new map, under the bucket's lock, and the tags fields are now volatile.
This commit is contained in:
1 parent
0475f96d85
commit
261fcac13a
7 files changed
+143
-13
No files matched your search
@@ -3315,9 +3315,9 @@ floci populates:
|
||||
Payment Cryptography for its keys, Resource Explorer for its indexes and views, Application Auto Scaling for its
|
||||
scalable targets, ECS for its clusters, task definitions, services, capacity providers, tasks and container
|
||||
instances, ACM for its certificates, Firehose for its delivery streams, Cognito for its user pools and identity
|
||||
pools, OpenSearch for its domains, Signer for its signing profiles, Access Analyzer for its analyzers, and Managed
|
||||
Service for Apache Flink for its applications. Tag keys differing only in case are one condition key holding every
|
||||
value.
|
||||
pools, OpenSearch for its domains, Signer for its signing profiles, Access Analyzer for its analyzers, Managed
|
||||
Service for Apache Flink for its applications, and S3 Vectors for its vector buckets and indexes. Tag keys differing
|
||||
only in case are one condition key holding every value.
|
||||
- EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and
|
||||
`ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the
|
||||
key names, an ARN for a key the Service Reference types as one. `resourceKeys` in
|
||||
|
||||
@@ -47,9 +47,9 @@ import java.util.Optional;
|
||||
* provider, a task or a container instance; ACM, for the tags of a certificate; Firehose, for the tags of a delivery
|
||||
* stream; Cognito, for the tags of a user pool or an identity pool; OpenSearch, for the tags of a domain; Signer, for
|
||||
* the tags of a signing profile; Access Analyzer, for the tags of an analyzer; Managed Service for Apache Flink, for
|
||||
* the tags of an application); looked up lazily by {@code ResourceArnBuilder} and
|
||||
* {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or an
|
||||
* ARN is of, so IAM never depends on that service directly.</p>
|
||||
* the tags of an application; S3 Vectors, for the tags of a vector bucket or an index); looked up lazily by
|
||||
* {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for
|
||||
* the namespace a request or an ARN is of, so IAM never depends on that service directly.</p>
|
||||
*
|
||||
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
|
||||
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
|
||||
|
||||
@@ -8,6 +8,7 @@ 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;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.s3vectors.model.VectorBucket;
|
||||
@@ -25,6 +26,7 @@ import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -33,10 +35,14 @@ import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class S3VectorsService {
|
||||
public class S3VectorsService implements StoredResourceArns {
|
||||
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);
|
||||
/** What a vector bucket's ARN resource starts with, its name following. */
|
||||
private static final String BUCKET_RESOURCE = "bucket/";
|
||||
/** What follows a bucket's ARN in the ARN of one of its indexes, the index's name following. */
|
||||
private static final String INDEX_SEGMENT = "/index/";
|
||||
|
||||
private final AccountAwareStorageBackend<VectorBucket> store;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -69,11 +75,11 @@ public class S3VectorsService {
|
||||
}
|
||||
|
||||
private String buildBucketArn(String region, String bucketName) {
|
||||
return regionResolver.buildArn("s3vectors", region, "bucket/" + bucketName);
|
||||
return regionResolver.buildArn("s3vectors", region, BUCKET_RESOURCE + bucketName);
|
||||
}
|
||||
|
||||
private String buildIndexArn(String region, String bucketName, String indexName) {
|
||||
return buildBucketArn(region, bucketName) + "/index/" + indexName;
|
||||
return buildBucketArn(region, bucketName) + INDEX_SEGMENT + indexName;
|
||||
}
|
||||
|
||||
public VectorBucket createVectorBucket(String bucketName, Object encryptionConfiguration, String region) {
|
||||
@@ -447,6 +453,47 @@ public class S3VectorsService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "s3vectors";
|
||||
}
|
||||
|
||||
/** A request names a vector bucket or an index by its name, which its ARN carries whole: nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the vector bucket or the index whose ARN is {@code arn}, in the request's account, where the handlers
|
||||
* read: an ARN of another account names none, and an index's ARN names the index's own tags, not its bucket's.
|
||||
* The bucket is read in place under the Region the ARN names and its name, a bucket stored before storage was
|
||||
* account-aware included, and the bucket or the index matched on the whole ARN. Tags are only ever replaced by a
|
||||
* new map, so the read is of one whole set.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "s3vectors")) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
if (!parsed.accountId().equals(store.accountId()) || !parsed.resource().startsWith(BUCKET_RESOURCE)) {
|
||||
return Map.of();
|
||||
}
|
||||
String path = parsed.resource().substring(BUCKET_RESOURCE.length());
|
||||
int segment = path.indexOf(INDEX_SEGMENT);
|
||||
String bucketName = segment < 0 ? path : path.substring(0, segment);
|
||||
Optional<VectorBucket> bucket = AccountAwareStorageBackend.readInAccount(store, parsed.accountId(),
|
||||
buildStorageKey(parsed.region(), bucketName));
|
||||
Map<String, String> tags = segment < 0
|
||||
? bucket.filter(held -> arn.equals(held.getVectorBucketArn())).map(VectorBucket::getTags)
|
||||
.orElse(Map.of())
|
||||
: bucket.map(held -> held.getIndexes().get(path.substring(segment + INDEX_SEGMENT.length())))
|
||||
.filter(index -> arn.equals(index.getIndexArn())).map(VectorIndex::getTags)
|
||||
.orElse(Map.of());
|
||||
return new LinkedHashMap<>(tags);
|
||||
}
|
||||
|
||||
public void putVectors(String bucketName, String indexName, List<VectorData> vectors, String region) {
|
||||
synchronized (lockFor(bucketName, region)) {
|
||||
VectorBucket bucket = getVectorBucket(bucketName, region);
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
@RegisterForReflection
|
||||
public class VectorBucket {
|
||||
private Instant creationTime;
|
||||
private Map<String, String> tags = new ConcurrentHashMap<>();
|
||||
private volatile Map<String, String> tags = new ConcurrentHashMap<>();
|
||||
private String vectorBucketName;
|
||||
private String vectorBucketArn;
|
||||
private Object encryptionConfiguration;
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
@RegisterForReflection
|
||||
public class VectorIndex {
|
||||
private Instant creationTime;
|
||||
private Map<String, String> tags = new ConcurrentHashMap<>();
|
||||
private volatile Map<String, String> tags = new ConcurrentHashMap<>();
|
||||
private String indexName;
|
||||
private String indexArn;
|
||||
private String vectorBucketName;
|
||||
|
||||
@@ -92,6 +92,35 @@ class S3VectorsIamIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An {@code aws:ResourceTag} condition reads the tags S3 Vectors holds on the vector bucket or the index a request
|
||||
* names, an index's own and not its bucket's, so an allow on one team's tag lets the caller read that team's
|
||||
* bucket and index and not another's.
|
||||
*/
|
||||
@Test
|
||||
void aResourceTagConditionReadsTheTagsOfTheBucketOrTheIndex() throws Exception {
|
||||
String tagged = bucketName();
|
||||
String other = bucketName();
|
||||
createBucket(tagged, "{\"team\":\"data\"}");
|
||||
createBucket(other, "{\"team\":\"web\"}");
|
||||
createIndex(other, "events", "{\"team\":\"data\"}");
|
||||
createIndex(other, "orders", "{\"team\":\"web\"}");
|
||||
try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "s3v-iam-", "access",
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Action\":[\"s3vectors:GetVectorBucket\",\"s3vectors:GetIndex\"],\"Resource\":\"*\","
|
||||
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}]}")) {
|
||||
send(user.signer("s3vectors"), "/GetVectorBucket", bucket(tagged)).then().statusCode(200);
|
||||
denied(send(user.signer("s3vectors"), "/GetVectorBucket", bucket(other)), "s3vectors:GetVectorBucket");
|
||||
send(user.signer("s3vectors"), "/GetIndex", index(other, "events")).then().statusCode(200);
|
||||
denied(send(user.signer("s3vectors"), "/GetIndex", index(other, "orders")), "s3vectors:GetIndex");
|
||||
} finally {
|
||||
send(admin, "/DeleteIndex", index(other, "events")).then().statusCode(200);
|
||||
send(admin, "/DeleteIndex", index(other, "orders")).then().statusCode(200);
|
||||
deleteBucket(tagged);
|
||||
deleteBucket(other);
|
||||
}
|
||||
}
|
||||
|
||||
private static String bucketName() {
|
||||
return "iam-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
@@ -110,13 +139,25 @@ class S3VectorsIamIntegrationTest {
|
||||
}
|
||||
|
||||
private String createBucket(String name) throws Exception {
|
||||
return send(admin, "/CreateVectorBucket", bucket(name)).then().statusCode(200)
|
||||
return createBucket(name, null);
|
||||
}
|
||||
|
||||
/** The ARN of the vector bucket {@code name} the administrator creates, with the {@code tags} object if any. */
|
||||
private String createBucket(String name, String tags) throws Exception {
|
||||
return send(admin, "/CreateVectorBucket", "{\"vectorBucketName\":\"" + name + "\""
|
||||
+ (tags == null ? "" : ",\"tags\":" + tags) + "}").then().statusCode(200)
|
||||
.extract().path("vectorBucketArn");
|
||||
}
|
||||
|
||||
private String createIndex(String bucket, String index) throws Exception {
|
||||
return createIndex(bucket, index, null);
|
||||
}
|
||||
|
||||
/** The ARN of the index the administrator creates in {@code bucket}, with the {@code tags} object if any. */
|
||||
private String createIndex(String bucket, String index, String tags) throws Exception {
|
||||
return send(admin, "/CreateIndex", "{\"vectorBucketName\":\"" + bucket + "\",\"indexName\":\"" + index
|
||||
+ "\",\"dataType\":\"float32\",\"dimension\":2,\"distanceMetric\":\"cosine\"}").then()
|
||||
+ "\",\"dataType\":\"float32\",\"dimension\":2,\"distanceMetric\":\"cosine\""
|
||||
+ (tags == null ? "" : ",\"tags\":" + tags) + "}").then()
|
||||
.statusCode(200).extract().path("indexArn");
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.s3vectors.model.VectorBucket;
|
||||
import io.floci.aws.services.s3vectors.model.VectorIndex;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -18,6 +20,7 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -44,6 +47,45 @@ class S3VectorsServiceTest {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* IAM's lookup reads the tags of the vector bucket or the index the ARN names, an index's own and not its bucket's,
|
||||
* as a replace leaves them, and nothing for another account's, another Region's or an unknown one.
|
||||
*/
|
||||
@Test
|
||||
void storedTagsAreTheTagsOfTheBucketOrTheIndexTheArnNames() {
|
||||
VectorBucket bucket = service.createVectorBucket("bucket", null, Map.of("team", "data"), REGION);
|
||||
VectorIndex index = service.createIndex("bucket", "index", 3, "float32", "cosine", List.of(), null,
|
||||
Map.of("team", "web"), REGION);
|
||||
service.replaceIndexTags("bucket", "index", Map.of("team", "ops"), REGION);
|
||||
String bucketArn = bucket.getVectorBucketArn();
|
||||
String indexArn = index.getIndexArn();
|
||||
|
||||
assertEquals("s3vectors", service.namespace());
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(bucketArn));
|
||||
assertEquals(Map.of("team", "ops"), service.storedTags(indexArn));
|
||||
assertTrue(service.storedTags(bucketArn.replace("000000000000", "111122223333")).isEmpty());
|
||||
assertTrue(service.storedTags(indexArn.replace(REGION, "us-west-2")).isEmpty());
|
||||
assertTrue(service.storedTags(bucketArn + "/index/missing").isEmpty());
|
||||
assertTrue(service.storedTags(bucketArn.replace("bucket/bucket", "bucket/missing")).isEmpty());
|
||||
assertTrue(service.storedTags("arn:aws:s3:::bucket").isEmpty());
|
||||
}
|
||||
|
||||
/** A bucket stored before storage was account-aware is read in place: the lookup migrates nothing. */
|
||||
@Test
|
||||
void storedTagsReadAPreAccountAwareBucketInPlace() {
|
||||
InMemoryStorage<String, VectorBucket> raw = new InMemoryStorage<>();
|
||||
S3VectorsService legacyState = new S3VectorsService(
|
||||
new InMemoryStorageFactory("000000000000").withStore("s3vectors.json", raw),
|
||||
new RegionResolver(REGION, "000000000000"), Clock.fixed(CREATION_TIME, ZoneOffset.UTC));
|
||||
VectorBucket legacy = new VectorBucket("legacy", "arn:aws:s3vectors:us-east-1:000000000000:bucket/legacy",
|
||||
Map.of("sseType", "AES256"));
|
||||
legacy.setTags(Map.of("team", "data"));
|
||||
raw.put(REGION + "::legacy", legacy);
|
||||
|
||||
assertEquals(Map.of("team", "data"), legacyState.storedTags(legacy.getVectorBucketArn()));
|
||||
assertEquals(Set.of(REGION + "::legacy"), raw.keys());
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistsCreationTimesConfigurationAndCreationTags() throws Exception {
|
||||
Map<String, String> encryption = Map.of("sseType", "aws:kms", "kmsKeyArn", "key");
|
||||
|
||||
Reference in new issue
Block a user