mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(athena): give resource-tag conditions the tags of the workgroup or data catalog a request names (#8607)
The generated request resources give Athena's workgroup and data catalog types aws:ResourceTag/ keys, but AthenaStoredResourceArns did not answer storedTags, so a condition on their tags saw none. It now answers from AthenaService.storedTags, which reads the workgroup or the data catalog stored under the ARN in the account and Region it names, a pre-multi-account one of the default account in place, matched on the whole ARN; the built-in primary workgroup and AwsDataCatalog carry none. The tag lists are volatile, as tagging already replaces a list whole.
This commit is contained in:
1 parent
057684feac
commit
cf3d7e827c
8 files changed
+117
-9
No files matched your search
@@ -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
|
||||
|
||||
@@ -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<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
|
||||
* service directly.</p>
|
||||
* 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<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
|
||||
|
||||
@@ -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<String, String> 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<WorkGroupTag> 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<String, String> 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<WorkGroupTag> tags) {
|
||||
if (tags == null || tags.isEmpty()) {
|
||||
throw new AwsException("InvalidRequestException", "Tags is required.", 400);
|
||||
|
||||
@@ -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<String, String> storedTags(String arn) {
|
||||
return athena.storedTags(arn);
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public class DataCatalog {
|
||||
private Map<String, String> parameters = new LinkedHashMap<>();
|
||||
|
||||
@JsonProperty("Tags")
|
||||
private List<WorkGroupTag> tags = new ArrayList<>();
|
||||
private volatile List<WorkGroupTag> tags = new ArrayList<>();
|
||||
|
||||
public DataCatalog() {}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ public class WorkGroup {
|
||||
private Instant creationTime;
|
||||
|
||||
@JsonProperty("Tags")
|
||||
private List<WorkGroupTag> tags = new ArrayList<>();
|
||||
private volatile List<WorkGroupTag> tags = new ArrayList<>();
|
||||
|
||||
@JsonProperty("Configuration")
|
||||
private WorkGroupConfiguration configuration;
|
||||
|
||||
@@ -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);
|
||||
|
||||
+39
@@ -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
|
||||
|
||||
Reference in new issue
Block a user