diff --git a/docs/services/iam.md b/docs/services/iam.md index 80746ca9ad..40d699da30 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -3149,8 +3149,9 @@ floci populates: (the function's tags), event source mappings, web functions, their revisions and endpoints (the web function's tags), MicroVM images and network connectors, Glue for its schema registries and schemas and for the jobs, crawlers, triggers, workflows, connections, databases, tables and other resources it tags, Step Functions for its - state machines, their versions and executions (the state machine's tags) and activities, and CodeBuild for its - projects and report groups. Tag keys differing only in case are one condition key holding every value. + state machines, their versions and executions (the state machine's tags) and activities, CodeBuild for its + projects and report groups, and Athena for its workgroups and data catalogs. 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 ed2d2eb274..379c723c6d 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -32,9 +32,10 @@ import java.util.Optional; * execution of a callback, and the tags of a function, an event source mapping, a web function, a MicroVM image or a * network connector; Glue, for the tags of a resource it tags; Step Functions, for the tags of a state machine, its * versions and executions, and an activity; CodeBuild, for the project a build belongs to and the tags of a project or - * a report group); 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.

+ * a report group; Athena, for the workgroup and the data catalog a request naming none uses and the tags of a + * workgroup or a data catalog); 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/athena/AthenaService.java b/src/main/java/io/floci/aws/services/athena/AthenaService.java index 7832d956d9..0be3059424 100644 --- a/src/main/java/io/floci/aws/services/athena/AthenaService.java +++ b/src/main/java/io/floci/aws/services/athena/AthenaService.java @@ -9,6 +9,7 @@ import io.floci.aws.core.common.PaginatedResult; import io.floci.aws.core.common.Pagination; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.common.RequestScopes; +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.athena.model.CreateWorkGroupConfigurationRequest; @@ -432,6 +433,43 @@ public class AthenaService { return requireTaggedDataCatalog(arn.region(), name, resourceArn).getTags(); } + /** + * The tags of the workgroup or the data catalog stored under {@code arn} in the account and Region it names, read + * in place and matched on the whole ARN; empty for an ARN naming nothing stored, the built-in {@code primary} + * workgroup and {@code AwsDataCatalog} included, which carry none. + */ + public Map storedTags(String arn) { + if (!AwsArnUtils.isArnFor(arn, "athena")) { + return Map.of(); + } + AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); + String resource = parsed.resource(); + if (!arn.equals(AwsArnUtils.Arn.of("athena", parsed.region(), parsed.accountId(), resource).toString())) { + return Map.of(); + } + List tags = null; + if (resource.startsWith(WORKGROUP_RESOURCE) && resource.indexOf('/', WORKGROUP_RESOURCE.length()) < 0) { + tags = AccountAwareStorageBackend.readInAccount(workGroupStore, parsed.accountId(), + workGroupKey(parsed.region(), resource.substring(WORKGROUP_RESOURCE.length()))) + .map(WorkGroup::getTags) + .orElse(null); + } else if (resource.startsWith(DATA_CATALOG_RESOURCE)) { + tags = AccountAwareStorageBackend.readInAccount(dataCatalogStore, parsed.accountId(), + catalogKey(parsed.region(), resource.substring(DATA_CATALOG_RESOURCE.length()))) + .map(DataCatalog::getTags) + .orElse(null); + } + Map byKey = new LinkedHashMap<>(); + if (tags != null) { + for (WorkGroupTag tag : tags) { + if (tag.getKey() != null && tag.getValue() != null) { + byKey.put(tag.getKey(), tag.getValue()); + } + } + } + return byKey; + } + public synchronized void tagResource(String resourceArn, List tags) { if (tags == null || tags.isEmpty()) { throw new AwsException("InvalidRequestException", "Tags is required.", 400); diff --git a/src/main/java/io/floci/aws/services/athena/AthenaStoredResourceArns.java b/src/main/java/io/floci/aws/services/athena/AthenaStoredResourceArns.java index 6fd1efd073..685106d5e6 100644 --- a/src/main/java/io/floci/aws/services/athena/AthenaStoredResourceArns.java +++ b/src/main/java/io/floci/aws/services/athena/AthenaStoredResourceArns.java @@ -6,20 +6,23 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import java.util.List; +import java.util.Map; import java.util.Optional; /** * What IAM asks Athena of the resources a request names: the workgroup and the data catalog its handlers use for a - * request naming none, {@code primary} and {@code AwsDataCatalog}. + * request naming none, {@code primary} and {@code AwsDataCatalog}, and the tags of a workgroup or a data catalog. */ @ApplicationScoped public class AthenaStoredResourceArns implements StoredResourceArns { private final RegionResolver regionResolver; + private final AthenaService athena; @Inject - public AthenaStoredResourceArns(RegionResolver regionResolver) { + public AthenaStoredResourceArns(RegionResolver regionResolver, AthenaService athena) { this.regionResolver = regionResolver; + this.athena = athena; } @Override @@ -41,4 +44,9 @@ public class AthenaStoredResourceArns implements StoredResourceArns { default -> Optional.empty(); }; } + + @Override + public Map storedTags(String arn) { + return athena.storedTags(arn); + } } diff --git a/src/main/java/io/floci/aws/services/athena/model/DataCatalog.java b/src/main/java/io/floci/aws/services/athena/model/DataCatalog.java index e12707d0af..d4b8113061 100644 --- a/src/main/java/io/floci/aws/services/athena/model/DataCatalog.java +++ b/src/main/java/io/floci/aws/services/athena/model/DataCatalog.java @@ -30,7 +30,7 @@ public class DataCatalog { private Map parameters = new LinkedHashMap<>(); @JsonProperty("Tags") - private List tags = new ArrayList<>(); + private volatile List tags = new ArrayList<>(); public DataCatalog() {} diff --git a/src/main/java/io/floci/aws/services/athena/model/WorkGroup.java b/src/main/java/io/floci/aws/services/athena/model/WorkGroup.java index 7effe34430..ea580d3aee 100644 --- a/src/main/java/io/floci/aws/services/athena/model/WorkGroup.java +++ b/src/main/java/io/floci/aws/services/athena/model/WorkGroup.java @@ -23,7 +23,7 @@ public class WorkGroup { private Instant creationTime; @JsonProperty("Tags") - private List tags = new ArrayList<>(); + private volatile List tags = new ArrayList<>(); @JsonProperty("Configuration") private WorkGroupConfiguration configuration; diff --git a/src/test/java/io/floci/aws/services/athena/AthenaServiceTest.java b/src/test/java/io/floci/aws/services/athena/AthenaServiceTest.java index 3aa1fa7b81..df0ba85907 100644 --- a/src/test/java/io/floci/aws/services/athena/AthenaServiceTest.java +++ b/src/test/java/io/floci/aws/services/athena/AthenaServiceTest.java @@ -4,9 +4,11 @@ import io.floci.aws.config.EmulatorConfig; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.storage.AccountAwareStorageBackend; import io.floci.aws.core.storage.StorageFactory; +import io.floci.aws.services.athena.model.CreateWorkGroupRequest; import io.floci.aws.services.athena.model.QueryExecutionState; import io.floci.aws.services.athena.model.ResultConfiguration; import io.floci.aws.services.athena.model.ResultSet; +import io.floci.aws.services.athena.model.WorkGroupTag; import io.floci.aws.services.floci.duck.FlociDuckClient; import io.floci.aws.services.glue.GlueService; import io.floci.aws.services.s3.S3Service; @@ -74,6 +76,25 @@ class AthenaServiceTest { vertx.close(); } + @Test + void storedTagsReadTheWorkgroupOrTheDataCatalogTheArnNamesInItsAccountAndRegion() { + CreateWorkGroupRequest request = new CreateWorkGroupRequest(); + request.setName("analytics"); + request.setTags(List.of(new WorkGroupTag("team", "data"))); + service.createWorkGroup(request, "us-east-1"); + service.createDataCatalog("us-east-1", "lake", "LAMBDA", null, null, List.of(new WorkGroupTag("team", "lake"))); + String arn = "arn:aws:athena:us-east-1:000000000000:"; + + assertEquals(Map.of("team", "data"), service.storedTags(arn + "workgroup/analytics")); + assertEquals(Map.of("team", "lake"), service.storedTags(arn + "datacatalog/lake")); + assertEquals(Map.of(), service.storedTags(arn + "workgroup/primary")); + assertEquals(Map.of(), service.storedTags(arn + "datacatalog/AwsDataCatalog")); + assertEquals(Map.of(), service.storedTags(arn + "workgroup/analytics/session/s-1")); + assertEquals(Map.of(), service.storedTags("arn:aws:athena:us-east-1:111111111111:workgroup/analytics")); + assertEquals(Map.of(), service.storedTags("arn:aws:athena:us-west-2:000000000000:workgroup/analytics")); + assertEquals(Map.of(), service.storedTags("arn:aws-cn:athena:us-east-1:000000000000:workgroup/analytics")); + } + @Test void stopDuringExecutionKeepsCancellationTerminal() throws Exception { CountDownLatch started = new CountDownLatch(1); diff --git a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java index 6054717e10..c611ef4558 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java @@ -992,6 +992,45 @@ class IamRequestResourceEnforcementIntegrationTest { "athena:StartQueryExecution"); } + /** + * An {@code aws:ResourceTag} condition reads the tags Athena holds on a workgroup and a data catalog, so an allow + * on one team's tag lets the caller read that team's and not another's. + */ + @Test + void anAthenaResourceTagConditionReadsTheTagsOfTheWorkgroupAndTheDataCatalog() { + String workGroup = athenaTagged("CreateWorkGroup", "res-tag-wg-" + suffix(), "data"); + String otherWorkGroup = athenaTagged("CreateWorkGroup", "res-tag-wg-" + suffix(), "web"); + String catalog = athenaTagged("CreateDataCatalog", "res_tag_cat_" + suffix(), "data"); + String otherCatalog = athenaTagged("CreateDataCatalog", "res_tag_cat_" + suffix(), "web"); + + User user = user("res-athena-tags"); + user.policy("{\"Effect\":\"Allow\",\"Action\":[\"athena:GetWorkGroup\",\"athena:GetDataCatalog\"]," + + "\"Resource\":\"*\",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}"); + send(user, "athena", "AmazonAthena.GetWorkGroup", "1.1", "{\"WorkGroup\":\"" + workGroup + "\"}") + .statusCode(200); + refusedJson(send(user, "athena", "AmazonAthena.GetWorkGroup", "1.1", "{\"WorkGroup\":\"" + otherWorkGroup + + "\"}"), "athena:GetWorkGroup"); + send(user, "athena", "AmazonAthena.GetDataCatalog", "1.1", "{\"Name\":\"" + catalog + "\"}").statusCode(200); + refusedJson(send(user, "athena", "AmazonAthena.GetDataCatalog", "1.1", "{\"Name\":\"" + otherCatalog + + "\"}"), "athena:GetDataCatalog"); + } + + /** An Athena workgroup or {@code LAMBDA} data catalog of the name, tagged with the team, deleted afterwards. */ + private String athenaTagged(String operation, String name, String team) { + String body = "CreateWorkGroup".equals(operation) ? "{\"Name\":\"" + name + "\"" + : "{\"Name\":\"" + name + "\",\"Type\":\"LAMBDA\""; + adminJson("athena", "AmazonAthena." + operation).body(body + ",\"Tags\":[{\"Key\":\"team\",\"Value\":\"" + + team + "\"}]}").post("/").then().statusCode(200); + if ("CreateWorkGroup".equals(operation)) { + cleanup.register(() -> adminJson("athena", "AmazonAthena.DeleteWorkGroup") + .body("{\"WorkGroup\":\"" + name + "\"}").post("/")); + } else { + cleanup.register(() -> adminJson("athena", "AmazonAthena.DeleteDataCatalog") + .body("{\"Name\":\"" + name + "\"}").post("/")); + } + return name; + } + /** * An Athena catalog read is checked on the catalog its {@code CatalogName} names, and Floci reads the metadata of * none but the account's Glue Data Catalog: a catalog of another type is refused, so a grant on it does not reach