diff --git a/docs/services/iam.md b/docs/services/iam.md index 3776d0e4c8..4819e7473b 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -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 diff --git a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java index cde3e5c9f1..abd94907e2 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -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} for the namespace a request or an - * ARN is of, so IAM never depends on that service directly.

+ * 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} for + * the namespace a request or an ARN is of, so IAM never depends on that service directly.

* *

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 diff --git a/src/main/java/io/floci/aws/services/s3vectors/S3VectorsService.java b/src/main/java/io/floci/aws/services/s3vectors/S3VectorsService.java index 52ec71022b..2190376971 100644 --- a/src/main/java/io/floci/aws/services/s3vectors/S3VectorsService.java +++ b/src/main/java/io/floci/aws/services/s3vectors/S3VectorsService.java @@ -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 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 find(String type, List 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 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 bucket = AccountAwareStorageBackend.readInAccount(store, parsed.accountId(), + buildStorageKey(parsed.region(), bucketName)); + Map 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 vectors, String region) { synchronized (lockFor(bucketName, region)) { VectorBucket bucket = getVectorBucket(bucketName, region); diff --git a/src/main/java/io/floci/aws/services/s3vectors/model/VectorBucket.java b/src/main/java/io/floci/aws/services/s3vectors/model/VectorBucket.java index 3666cb5da8..003c4efcc7 100644 --- a/src/main/java/io/floci/aws/services/s3vectors/model/VectorBucket.java +++ b/src/main/java/io/floci/aws/services/s3vectors/model/VectorBucket.java @@ -9,7 +9,7 @@ import java.util.concurrent.ConcurrentHashMap; @RegisterForReflection public class VectorBucket { private Instant creationTime; - private Map tags = new ConcurrentHashMap<>(); + private volatile Map tags = new ConcurrentHashMap<>(); private String vectorBucketName; private String vectorBucketArn; private Object encryptionConfiguration; diff --git a/src/main/java/io/floci/aws/services/s3vectors/model/VectorIndex.java b/src/main/java/io/floci/aws/services/s3vectors/model/VectorIndex.java index ebe6d75e9f..f5da5b620d 100644 --- a/src/main/java/io/floci/aws/services/s3vectors/model/VectorIndex.java +++ b/src/main/java/io/floci/aws/services/s3vectors/model/VectorIndex.java @@ -10,7 +10,7 @@ import java.util.concurrent.ConcurrentHashMap; @RegisterForReflection public class VectorIndex { private Instant creationTime; - private Map tags = new ConcurrentHashMap<>(); + private volatile Map tags = new ConcurrentHashMap<>(); private String indexName; private String indexArn; private String vectorBucketName; diff --git a/src/test/java/io/floci/aws/services/s3vectors/S3VectorsIamIntegrationTest.java b/src/test/java/io/floci/aws/services/s3vectors/S3VectorsIamIntegrationTest.java index 63ac160391..fb8d22b8f7 100644 --- a/src/test/java/io/floci/aws/services/s3vectors/S3VectorsIamIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3vectors/S3VectorsIamIntegrationTest.java @@ -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"); } diff --git a/src/test/java/io/floci/aws/services/s3vectors/S3VectorsServiceTest.java b/src/test/java/io/floci/aws/services/s3vectors/S3VectorsServiceTest.java index 936f4d94e6..864d5dcab9 100644 --- a/src/test/java/io/floci/aws/services/s3vectors/S3VectorsServiceTest.java +++ b/src/test/java/io/floci/aws/services/s3vectors/S3VectorsServiceTest.java @@ -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 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 encryption = Map.of("sseType", "aws:kms", "kmsKeyArn", "key");