mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): read the stored tags of SageMaker and BCM Data Exports resources (#9872)
The request-resources table gives resource-tag keys to SageMaker's model, endpoint configuration, endpoint and training job types and to BCM Data Exports' export type, but neither namespace gave IAM the tags it stores, so an aws:ResourceTag condition on one of their actions never matched. SageMakerStoredResourceArns reads SageMakerService.storedTags: the tags of the caller's model, endpoint configuration, endpoint or training job the ARN names, in its Region, by the resource part the ARN starts with, read in place and copied under the monitor AddTags and DeleteTags change them under. BcmDataExportsStoredResourceArns reads BcmDataExportsService.storedTags: the ResourceTags an export's create gave it, read in place under its ARN. StoredResourceTagIamIntegrationTest checks a caller allowed sagemaker:DescribeModel and bcm-data-exports:GetExport only on resources tagged team=blue reaches the blue one and is refused the red one; both fail without the readers. SageMakerServiceTest checks the read, its copy, the ARNs that name nothing, and that a model, an endpoint configuration, an endpoint and a training job of one name answer their own tags by their own ARNs.
This commit is contained in:
1 parent
1fd2f4fa8f
commit
368488cd5b
7 files changed
+244
-4
No files matched your search
@@ -4049,8 +4049,9 @@ floci populates:
|
||||
for its servers and users, SES for its identities, configuration sets, templates, contact lists, custom
|
||||
verification email templates, dedicated IP pools and tenants, ElastiCache for its users, user groups, replication
|
||||
groups, cache clusters, snapshots, subnet groups, parameter groups and serverless caches, DMS for its endpoints,
|
||||
replication instances, replication tasks and replication subnet groups, and Amazon Managed Service for Prometheus
|
||||
for its workspaces and rule groups namespaces. Tag keys
|
||||
replication instances, replication tasks and replication subnet groups, SageMaker for its models, endpoint
|
||||
configurations, endpoints and training jobs, BCM Data Exports for its exports, and Amazon Managed Service for
|
||||
Prometheus for its workspaces and rule groups namespaces. 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
|
||||
|
||||
@@ -109,6 +109,23 @@ public class BcmDataExportsService {
|
||||
return incoming;
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the caller's export the ARN names, the {@code ResourceTags} its create gave it, read in place; empty
|
||||
* for an ARN of another account or naming no export this service holds. Every change stores a new export record,
|
||||
* so the copy read is of one whole set.
|
||||
*/
|
||||
public Map<String, String> storedTags(String exportArn) {
|
||||
String account = regionResolver.getAccountId();
|
||||
if (!AwsArnUtils.isArnFor(exportArn, "bcm-data-exports")
|
||||
|| !account.equals(AwsArnUtils.parse(exportArn).accountId())) {
|
||||
return Map.of();
|
||||
}
|
||||
return AccountAwareStorageBackend.readInAccount(exportStore, account, exportKey(exportArn))
|
||||
.map(Export::getResourceTags)
|
||||
.<Map<String, String>>map(LinkedHashMap::new)
|
||||
.orElse(Map.of());
|
||||
}
|
||||
|
||||
/** {@code GetExport} — returns the {@link Export} matching {@code exportArn}. */
|
||||
public Export getExport(String exportArn) {
|
||||
requireNonEmpty(exportArn, "ExportArn");
|
||||
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package io.floci.aws.services.bcmdataexports;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* What IAM asks BCM Data Exports of the resources a request names: the tags it keeps on an export, which the
|
||||
* request-resources table's resource-tag keys for that type take as their values ({@code aws:ResourceTag/<key>}).
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class BcmDataExportsStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private final BcmDataExportsService exports;
|
||||
|
||||
@Inject
|
||||
public BcmDataExportsStoredResourceArns(BcmDataExportsService exports) {
|
||||
this.exports = exports;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "bcm-data-exports";
|
||||
}
|
||||
|
||||
/** A request names an export by its ARN: nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "bcm-data-exports")) {
|
||||
return Map.of();
|
||||
}
|
||||
return StoredResourceArns.withEmptyValues(exports.storedTags(arn));
|
||||
}
|
||||
}
|
||||
@@ -462,6 +462,41 @@ public class SageMakerService {
|
||||
return mapper.createObjectNode();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the caller's model, endpoint configuration, endpoint or training job the ARN names, in the ARN's
|
||||
* Region, the ones {@code ListTags} reads, read in place and copied under the monitor {@code AddTags} and
|
||||
* {@code DeleteTags} change them under; empty for an ARN of another kind, account or Region, or naming nothing
|
||||
* this service holds. The kind is the resource part the ARN starts with, as its format names it.
|
||||
*/
|
||||
public synchronized Map<String, String> storedTags(String arn) {
|
||||
AwsArnUtils.Arn parsed;
|
||||
try {
|
||||
parsed = AwsArnUtils.parse(arn);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
// Not an ARN: it names no SageMaker resource.
|
||||
return Map.of();
|
||||
}
|
||||
String account = regionResolver.getAccountId();
|
||||
String resource = parsed.resource();
|
||||
int slash = resource.indexOf('/');
|
||||
if (!"sagemaker".equals(parsed.service()) || !account.equals(parsed.accountId()) || parsed.region().isBlank()
|
||||
|| slash < 0) {
|
||||
return Map.of();
|
||||
}
|
||||
String key = regionKey(parsed.region(), resource.substring(slash + 1));
|
||||
Optional<Map<String, String>> tags = switch (resource.substring(0, slash)) {
|
||||
case "model" -> AccountAwareStorageBackend.readInAccount(modelStore, account, key).map(model -> model.tags);
|
||||
case "endpoint-config" -> AccountAwareStorageBackend.readInAccount(endpointConfigStore, account, key)
|
||||
.map(config -> config.tags);
|
||||
case "endpoint" -> AccountAwareStorageBackend.readInAccount(endpointStore, account, key)
|
||||
.map(endpoint -> endpoint.tags);
|
||||
case "training-job" -> AccountAwareStorageBackend.readInAccount(trainingJobStore, account, key)
|
||||
.map(job -> job.tags);
|
||||
default -> Optional.empty();
|
||||
};
|
||||
return tags.<Map<String, String>>map(LinkedHashMap::new).orElse(Map.of());
|
||||
}
|
||||
|
||||
public synchronized ObjectNode addTags(JsonNode request) {
|
||||
String arn = required(request, "ResourceArn");
|
||||
Map<String, String> tags = tagsFromList(request.path("Tags"));
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package io.floci.aws.services.sagemaker;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* What IAM asks SageMaker of the resources a request names: the tags it keeps on a model, an endpoint
|
||||
* configuration, an endpoint or a training job, which the request-resources table's resource-tag keys for those types
|
||||
* take as their values ({@code aws:ResourceTag/<key>}).
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class SageMakerStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private final SageMakerService sageMaker;
|
||||
|
||||
@Inject
|
||||
public SageMakerStoredResourceArns(SageMakerService sageMaker) {
|
||||
this.sageMaker = sageMaker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "sagemaker";
|
||||
}
|
||||
|
||||
/** A request names a SageMaker resource by the name its ARN ends in: nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "sagemaker")) {
|
||||
return Map.of();
|
||||
}
|
||||
return StoredResourceArns.withEmptyValues(sageMaker.storedTags(arn));
|
||||
}
|
||||
}
|
||||
@@ -36,8 +36,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
/**
|
||||
* A condition on {@code aws:ResourceTag} reads the tags a service keeps on the resource a request names, for the types
|
||||
* the request-resources table gives resource-tag keys: a Config rule, an EFS file system, a Route 53 Resolver rule, a
|
||||
* Transfer Family server, an SES configuration set, an SES tenant, a DMS endpoint and a DMS subnet group. A caller
|
||||
* allowed an action only on resources tagged {@code team=blue} may act on the blue one and is refused the red one.
|
||||
* Transfer Family server, an SES configuration set, an SES tenant, a DMS endpoint, a DMS subnet group, a SageMaker
|
||||
* model and a BCM Data Exports export. A caller allowed an action only on resources tagged {@code team=blue} may act
|
||||
* on the blue one and is refused the red one.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
@@ -257,6 +258,51 @@ class StoredResourceTagIamIntegrationTest {
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSageMakerModelIsCheckedWithItsTags() {
|
||||
String admin = auth(ACCOUNT, REGION.id(), "sagemaker");
|
||||
String[] names = new String[2];
|
||||
for (int i = 0; i < 2; i++) {
|
||||
String team = i == 0 ? "blue" : "red";
|
||||
String name = team + "-" + suffix();
|
||||
json("SageMaker.CreateModel", """
|
||||
{"ModelName":"%s","PrimaryContainer":{"Image":"busybox:stable"},
|
||||
"Tags":[{"Key":"team","Value":"%s"}]}""".formatted(name, team), admin).statusCode(200);
|
||||
cleanup.register(() -> json("SageMaker.DeleteModel", "{\"ModelName\":\"" + name + "\"}", admin));
|
||||
names[i] = name;
|
||||
}
|
||||
String caller = auth(user("sagemaker:DescribeModel").accessKeyId(), REGION.id(), "sagemaker");
|
||||
json("SageMaker.DescribeModel", "{\"ModelName\":\"" + names[1] + "\"}", caller)
|
||||
.statusCode(400).body(containsString("not authorized to perform: sagemaker:DescribeModel"));
|
||||
json("SageMaker.DescribeModel", "{\"ModelName\":\"" + names[0] + "\"}", caller)
|
||||
.statusCode(200).body("ModelName", equalTo(names[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBcmDataExportIsCheckedWithItsTags() {
|
||||
String admin = auth(ACCOUNT, REGION.id(), "bcm-data-exports");
|
||||
String[] arns = new String[2];
|
||||
for (int i = 0; i < 2; i++) {
|
||||
String team = i == 0 ? "blue" : "red";
|
||||
String arn = json("AWSBillingAndCostManagementDataExports.CreateExport", """
|
||||
{"Export":{"Name":"%s","DataQuery":{"QueryStatement":"SELECT * FROM COST_AND_USAGE_REPORT"},
|
||||
"DestinationConfigurations":{"S3Destination":{"S3Bucket":"tags-bucket","S3Prefix":"out/",
|
||||
"S3Region":"us-east-1","S3OutputConfigurations":{"Format":"PARQUET","Compression":"PARQUET",
|
||||
"OutputType":"CUSTOM","Overwrite":"OVERWRITE_REPORT"}}},
|
||||
"RefreshCadence":{"Frequency":"SYNCHRONOUS"}},
|
||||
"ResourceTags":[{"Key":"team","Value":"%s"}]}""".formatted(team + "-" + suffix(), team), admin)
|
||||
.statusCode(200).extract().path("ExportArn");
|
||||
cleanup.register(() -> json("AWSBillingAndCostManagementDataExports.DeleteExport",
|
||||
"{\"ExportArn\":\"" + arn + "\"}", admin));
|
||||
arns[i] = arn;
|
||||
}
|
||||
String caller = auth(user("bcm-data-exports:GetExport").accessKeyId(), REGION.id(), "bcm-data-exports");
|
||||
json("AWSBillingAndCostManagementDataExports.GetExport", "{\"ExportArn\":\"" + arns[1] + "\"}", caller)
|
||||
.statusCode(400).body(containsString("not authorized to perform: bcm-data-exports:GetExport"));
|
||||
json("AWSBillingAndCostManagementDataExports.GetExport", "{\"ExportArn\":\"" + arns[0] + "\"}", caller)
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* A JSON 1.1 call on the test's port: a cleanup runs after the test method, once Quarkus has reset RestAssured's
|
||||
* port, so every call names it.
|
||||
|
||||
@@ -42,6 +42,58 @@ class SageMakerServiceTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* IAM reads the tags SageMaker keeps on a resource by its ARN, in the ARN's Region, as a copy; an ARN of another
|
||||
* kind, Region or account, or naming nothing it holds, has none, and a name holding another kind's word is read as
|
||||
* its own kind.
|
||||
*/
|
||||
@Test
|
||||
void storedTagsReadTheResourceTheArnNames() throws Exception {
|
||||
SageMakerService service = service();
|
||||
String arn = service.createModel(mapper.readTree("""
|
||||
{"ModelName":"endpoint-like","PrimaryContainer":{"Image":"busybox:stable"},
|
||||
"Tags":[{"Key":"team","Value":"blue"}]}
|
||||
"""), "us-east-1").path("ModelArn").asText();
|
||||
|
||||
assertEquals(Map.of("team", "blue"), service.storedTags(arn));
|
||||
service.storedTags(arn).put("forged", "yes");
|
||||
assertEquals(Map.of("team", "blue"), service.storedTags(arn));
|
||||
assertEquals(Map.of(), service.storedTags(arn.replace(":us-east-1:", ":eu-west-1:")));
|
||||
assertEquals(Map.of(), service.storedTags(arn.replace(":" + DEFAULT_ACCOUNT + ":", ":" + OTHER_ACCOUNT + ":")));
|
||||
assertEquals(Map.of(), service.storedTags(arn.replace(":model/", ":endpoint/")));
|
||||
assertEquals(Map.of(), service.storedTags(arn + "-missing"));
|
||||
assertEquals(Map.of(), service.storedTags("not-an-arn"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Each kind of SageMaker resource is read from its own store: a model, an endpoint configuration, an endpoint and a
|
||||
* training job of one name, each tagged apart, answer their own tags by their own ARNs.
|
||||
*/
|
||||
@Test
|
||||
void storedTagsReadEachKindFromItsOwnStore() throws Exception {
|
||||
SageMakerService service = service();
|
||||
String model = service.createModel(mapper.readTree("""
|
||||
{"ModelName":"same","PrimaryContainer":{"Image":"busybox:stable"},
|
||||
"Tags":[{"Key":"kind","Value":"model"}]}
|
||||
"""), "us-east-1").path("ModelArn").asText();
|
||||
String config = service.createEndpointConfig(mapper.readTree("""
|
||||
{"EndpointConfigName":"same","ProductionVariants":[{"VariantName":"AllTraffic","ModelName":"same",
|
||||
"InitialInstanceCount":1,"InstanceType":"ml.t2.medium"}],"Tags":[{"Key":"kind","Value":"config"}]}
|
||||
"""), "us-east-1").path("EndpointConfigArn").asText();
|
||||
String endpoint = service.createEndpoint(mapper.readTree("""
|
||||
{"EndpointName":"same","EndpointConfigName":"same","Tags":[{"Key":"kind","Value":"endpoint"}]}
|
||||
"""), "us-east-1").path("EndpointArn").asText();
|
||||
String job = service.createTrainingJob(mapper.readTree("""
|
||||
{"TrainingJobName":"same","AlgorithmSpecification":{"TrainingImage":"busybox:stable"},
|
||||
"OutputDataConfig":{"S3OutputPath":"s3://bucket/out"},"Tags":[{"Key":"kind","Value":"job"}]}
|
||||
"""), "us-east-1").path("TrainingJobArn").asText();
|
||||
|
||||
assertEquals(Map.of("kind", "model"), service.storedTags(model));
|
||||
assertEquals(Map.of("kind", "config"), service.storedTags(config));
|
||||
assertEquals(Map.of("kind", "endpoint"), service.storedTags(endpoint));
|
||||
assertEquals(Map.of("kind", "job"), service.storedTags(job));
|
||||
}
|
||||
|
||||
@Test
|
||||
void modelCrudAndDuplicateValidation() throws Exception {
|
||||
SageMakerService service = service();
|
||||
|
||||
Reference in new issue
Block a user