diff --git a/docs/services/iam.md b/docs/services/iam.md index 69e55251ab..b02e1c60eb 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -3071,8 +3071,8 @@ floci populates: its type (`aws:ResourceTag/`, `secretsmanager:ResourceTag/`), which `regen_iam_request_resources.py` reads from the Service Reference for every namespace it reads, valued from the tags the type's service holds on that resource (`StoredResourceArns.storedTags`); a service that holds none leaves them absent. - Secrets Manager answers for its secrets. Tag keys differing only in case are one condition key holding - every value. + Secrets Manager answers for its secrets, and S3 Tables for its table buckets and tables. 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 44f1670e94..5978e5f7fd 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -26,9 +26,10 @@ import java.util.Optional; * permission sets are provisioned to; Resource Explorer, for its default view; OAM and RAM, for the sinks, * invitations and shares of other accounts they act on; Secrets Manager, for the secret a name or an ARN names, * random suffix included, and its tags; EventBridge Scheduler, for its default schedule group; CloudFormation, for - * the stack or the stack set a name names, whose id it minted); 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 stack or the stack set a name names, whose id it minted; S3 Tables, for the table a request names by its table + * bucket, namespace and name, and the tags of a table bucket or a table); 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/s3tables/S3TablesService.java b/src/main/java/io/floci/aws/services/s3tables/S3TablesService.java index 1a825f5f99..845f72a445 100644 --- a/src/main/java/io/floci/aws/services/s3tables/S3TablesService.java +++ b/src/main/java/io/floci/aws/services/s3tables/S3TablesService.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference; 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.storage.AccountAwareStorageBackend; import io.floci.aws.core.storage.StorageBackend; import io.floci.aws.core.storage.StorageFactory; import io.floci.aws.services.s3.S3Service; @@ -187,6 +188,40 @@ public class S3TablesService { } } + /** + * The tags of the table bucket or the table stored under {@code arn}, in the account and Region the ARN names, by + * key, which IAM gives a resource-tag condition; empty when there is none. Reads that one bucket's entry, a + * pre-multi-account one of the default account in place, and matches the bucket, or a table of it, on the whole + * ARN, leaving both as they are. + */ + public synchronized Map storedTags(String arn) { + if (!AwsArnUtils.isArnFor(arn, "s3tables")) { + return Map.of(); + } + AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); + if (!parsed.resource().startsWith(BUCKET_PREFIX)) { + return Map.of(); + } + String rest = parsed.resource().substring(BUCKET_PREFIX.length()); + String bucketName = rest.contains("/") ? rest.substring(0, rest.indexOf('/')) : rest; + TableBucket bucket = stored(parsed.accountId(), storageKey(parsed.region(), bucketName)).orElse(null); + if (bucket == null) { + return Map.of(); + } + if (arn.equals(bucket.getArn())) { + return Map.copyOf(bucket.getTags()); + } + if (!arn.startsWith(bucket.getArn() + TABLE_INFIX)) { + return Map.of(); + } + return bucket.getNamespaces().values().stream() + .flatMap(namespace -> namespace.getTables().values().stream()) + .filter(table -> arn.equals(table.getArn())) + .findFirst() + .>map(table -> Map.copyOf(table.getTags())) + .orElse(Map.of()); + } + public synchronized List listTables(String tableBucketArn, String namespaceName, String prefix, String region) { TableBucket bucket = getTableBucket(tableBucketArn, region); List tables = new ArrayList<>(); @@ -442,6 +477,13 @@ public class S3TablesService { return region + "::" + name; } + /** The bucket stored under {@code key} in the account, or the default account's legacy one, read only. */ + private Optional stored(String accountId, String key) { + return store instanceof AccountAwareStorageBackend aware + ? aware.getForAccount(accountId, key).or(() -> aware.getLegacyForAccount(accountId, key)) + : store.get(key); + } + private String resourceName(String arn, String prefix) { // Guard and cut must use the same needle. Checking for ":" + prefix but cutting on prefix // alone means a name that itself contains the prefix is cut at the wrong place. diff --git a/src/main/java/io/floci/aws/services/s3tables/S3TablesStoredResourceArns.java b/src/main/java/io/floci/aws/services/s3tables/S3TablesStoredResourceArns.java index b7dd4baa65..2dd44ec6ff 100644 --- a/src/main/java/io/floci/aws/services/s3tables/S3TablesStoredResourceArns.java +++ b/src/main/java/io/floci/aws/services/s3tables/S3TablesStoredResourceArns.java @@ -5,12 +5,13 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import java.util.List; +import java.util.Map; import java.util.Optional; /** * What IAM asks S3 Tables of the resources a request names: the table a request names by its table bucket's ARN, its - * namespace and its name, whose ARN AWS ends in an id the service mints and Floci in the table's name, so only the - * stored table gives it. + * namespace and its name, whose ARN ends in an id the service mints, so only the stored table gives it; and the tags + * of a table bucket or a table, which a resource-tag condition reads. */ @ApplicationScoped public class S3TablesStoredResourceArns implements StoredResourceArns { @@ -34,4 +35,10 @@ public class S3TablesStoredResourceArns implements StoredResourceArns { } return s3TablesService.tableArn(names.get(0), names.get(1), names.get(2), region); } + + /** The tags of the table bucket or the table {@code arn} names, which a resource-tag condition reads. */ + @Override + public Map storedTags(String arn) { + return s3TablesService.storedTags(arn); + } } diff --git a/src/test/java/io/floci/aws/services/s3tables/S3TablesIamIntegrationTest.java b/src/test/java/io/floci/aws/services/s3tables/S3TablesIamIntegrationTest.java index 595adba7ef..163a3bda2a 100644 --- a/src/test/java/io/floci/aws/services/s3tables/S3TablesIamIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3tables/S3TablesIamIntegrationTest.java @@ -166,6 +166,33 @@ class S3TablesIamIntegrationTest { } } + /** A resource-tag condition reads the tags S3 Tables holds on the bucket or the table a request names. */ + @Test + void aResourceTagConditionReadsTheTagsOfTheBucketAndTheTable() throws Exception { + String tagged = send(admin, "PUT", "/buckets", "{\"name\":\"iam-" + UUID.randomUUID().toString().substring(0, 8) + + "\",\"tags\":{\"team\":\"data\"}}").then().statusCode(200).extract().path("arn"); + String untagged = createBucket(); + String path = "/tables/" + encoded(tagged) + "/analytics/"; + send(admin, "PUT", "/namespaces/" + encoded(tagged), "{\"namespace\":[\"analytics\"]}").then().statusCode(200); + send(admin, "PUT", path, "{\"name\":\"events\",\"format\":\"ICEBERG\",\"tags\":{\"team\":\"data\"}}") + .then().statusCode(200); + send(admin, "PUT", path, "{\"name\":\"orders\",\"format\":\"ICEBERG\"}").then().statusCode(200); + String onTeamData = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":" + + "[\"s3tables:GetTableBucket\",\"s3tables:GetTable\"],\"Resource\":\"*\",\"Condition\":" + + "{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}]}"; + try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "s3t-iam-", "tags", onTeamData)) { + send(user.signer("s3tables"), "GET", "/buckets/" + encoded(tagged), null).then().statusCode(200); + denied(send(user.signer("s3tables"), "GET", "/buckets/" + encoded(untagged), null), + "s3tables:GetTableBucket"); + String query = "/get-table?tableBucketARN=" + encoded(tagged) + "&namespace=analytics&name="; + send(user.signer("s3tables"), "GET", query + "events", null).then().statusCode(200); + denied(send(user.signer("s3tables"), "GET", query + "orders", null), "s3tables:GetTable"); + } finally { + deleteBucket(tagged, "analytics"); + send(admin, "DELETE", "/buckets/" + encoded(untagged), null).then().statusCode(204); + } + } + private String createTable(String bucket, String namespace, String name) throws Exception { send(admin, "PUT", "/namespaces/" + encoded(bucket), "{\"namespace\":[\"" + namespace + "\"]}"); return send(admin, "PUT", "/tables/" + encoded(bucket) + "/" + namespace + "/", diff --git a/src/test/java/io/floci/aws/services/s3tables/S3TablesServiceTest.java b/src/test/java/io/floci/aws/services/s3tables/S3TablesServiceTest.java index fe8569f5ec..8ef8c555bd 100644 --- a/src/test/java/io/floci/aws/services/s3tables/S3TablesServiceTest.java +++ b/src/test/java/io/floci/aws/services/s3tables/S3TablesServiceTest.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference; 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.s3.S3Service; import io.floci.aws.services.s3tables.model.S3Table; @@ -56,6 +57,47 @@ class S3TablesServiceTest { service = new S3TablesService(storageFactory, new RegionResolver(REGION, ACCOUNT_ID), s3Service); } + /** + * The tags IAM gives a resource-tag condition: the table bucket's or a table's, read on the whole ARN in the + * account and Region it names, and none for anything else. + */ + @Test + void storedTagsReadsTheBucketOrTheTableItsArnNames() { + TableBucket bucket = service.createTableBucket(BUCKET, null, null, Map.of("team", "data"), REGION); + service.createNamespace(bucket.getArn(), List.of("analytics"), REGION); + S3Table table = service.createTable(bucket.getArn(), "analytics", "events", "ICEBERG", null, null, null, + Map.of("owner", "alice"), REGION); + + assertEquals(Map.of("team", "data"), service.storedTags(bucket.getArn())); + assertEquals(Map.of("owner", "alice"), service.storedTags(table.getArn())); + assertEquals(Map.of(), service.storedTags(bucket.getArn() + "/table/other")); + assertEquals(Map.of(), service.storedTags(bucket.getArn().replace(":" + ACCOUNT_ID + ":", ":111122223333:"))); + assertEquals(Map.of(), service.storedTags(bucket.getArn().replace(REGION, "eu-west-1"))); + assertEquals(Map.of(), service.storedTags("arn:aws:s3:::" + BUCKET)); + } + + /** A bucket stored before storage was account-aware is read in place for the default account, never migrated. */ + @Test + void storedTagsReadsAPreMultiAccountBucketOfTheDefaultAccountInPlace() { + InMemoryStorage raw = new InMemoryStorage<>(); + StorageFactory legacyFactory = new StorageFactory(null, null) { + @Override + @SuppressWarnings("unchecked") + public AccountAwareStorageBackend create(String serviceName, String fileName, + TypeReference> typeReference) { + return (AccountAwareStorageBackend) new AccountAwareStorageBackend<>(raw, null, ACCOUNT_ID); + } + }; + S3TablesService legacy = new S3TablesService(legacyFactory, new RegionResolver(REGION, ACCOUNT_ID), s3Service); + String arn = "arn:aws:s3tables:" + REGION + ":" + ACCOUNT_ID + ":bucket/" + BUCKET; + raw.put(REGION + "::" + BUCKET, new TableBucket(BUCKET, arn, "2026-01-01T00:00:00Z", ACCOUNT_ID, null, null, + Map.of("team", "data"))); + + assertEquals(Map.of("team", "data"), legacy.storedTags(arn)); + assertEquals(Map.of(), legacy.storedTags(arn.replace(":" + ACCOUNT_ID + ":", ":111122223333:"))); + assertTrue(raw.get(REGION + "::" + BUCKET).isPresent()); + } + @Test void listingAndUpdatingATableFromBeforeWarehousesGiveItOne() { String arn = service.createTableBucket(BUCKET, null, null, Map.of(), REGION).getArn();