fix(sagemaker): read and change the tags of the resource a tag ARN names (#9878)

The tag operations took a resource's name as everything after the last / of its ARN and its kind from a substring
anywhere in it, while the store keys a resource by all of its resource part after the kind's /, which IAM's read of
the stored tags follows. A name SageMaker kept with a / in it then sent the two to different resources: AddTags on
model/blue/x changed the model named x while an aws:ResourceTag condition read the tags of blue/x.

AddTags, DeleteTags, ListTags and IAM's read now share one parse of the ARN: its kind, the resource part before its
first /, and its name, all after it, as each store keys it. An ARN with no Region or no kind is refused as before.

SageMakerServiceTest checks that tagging a model named blue/x by its ARN changes that model and not the one named x,
and that its stored tags are the ones the tag operation changed; it fails with the old parse.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-30 21:52:30 -04:00
1 parent 022873e1ef
commit e2f535bc4f
2 files changed
+87 -32

No files matched your search

@@ -469,22 +469,14 @@ public class SageMakerService {
* 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) {
Optional<Named> named = named(arn).filter(resource -> account.equals(resource.account())
&& !resource.region().isBlank());
if (named.isEmpty()) {
return Map.of();
}
String key = regionKey(parsed.region(), resource.substring(slash + 1));
Optional<Map<String, String>> tags = switch (resource.substring(0, slash)) {
String key = regionKey(named.get().region(), named.get().name());
Optional<Map<String, String>> tags = switch (named.get().kind()) {
case "model" -> AccountAwareStorageBackend.readInAccount(modelStore, account, key).map(model -> model.tags);
case "endpoint-config" -> AccountAwareStorageBackend.readInAccount(endpointConfigStore, account, key)
.map(config -> config.tags);
@@ -677,31 +669,67 @@ public class SageMakerService {
}
private Map<String, String> resourceTags(String arn) {
String region = arnRegion(arn);
String name = arn.substring(arn.lastIndexOf('/') + 1);
if (arn.contains(":model/")) return model(region, name).orElseThrow(() -> validation("Could not find model \"" + name + "\".")).tags;
if (arn.contains(":endpoint-config/")) return endpointConfig(region, name).orElseThrow(() -> validation("Could not find endpoint configuration \"" + name + "\".")).tags;
if (arn.contains(":endpoint/")) return endpoint(region, name).orElseThrow(() -> validation("Could not find endpoint \"" + name + "\".")).tags;
if (arn.contains(":training-job/")) return trainingJob(region, name).orElseThrow(() -> validation("Could not find training job \"" + name + "\".")).tags;
throw validation("ResourceArn is invalid");
Named resource = taggable(arn);
String region = resource.region();
String name = resource.name();
return switch (resource.kind()) {
case "model" -> model(region, name)
.orElseThrow(() -> validation("Could not find model \"" + name + "\".")).tags;
case "endpoint-config" -> endpointConfig(region, name)
.orElseThrow(() -> validation("Could not find endpoint configuration \"" + name + "\".")).tags;
case "endpoint" -> endpoint(region, name)
.orElseThrow(() -> validation("Could not find endpoint \"" + name + "\".")).tags;
case "training-job" -> trainingJob(region, name)
.orElseThrow(() -> validation("Could not find training job \"" + name + "\".")).tags;
default -> throw validation("ResourceArn is invalid");
};
}
private void persistByArn(String arn) {
String region = arnRegion(arn);
String name = arn.substring(arn.lastIndexOf('/') + 1);
if (arn.contains(":model/")) model(region, name).ifPresent(v -> modelStore.put(regionKey(region, name), v));
else if (arn.contains(":endpoint-config/")) endpointConfig(region, name).ifPresent(v -> endpointConfigStore.put(regionKey(region, name), v));
else if (arn.contains(":endpoint/")) endpoint(region, name).ifPresent(v -> endpointStore.put(regionKey(region, name), v));
else if (arn.contains(":training-job/")) trainingJob(region, name).ifPresent(v -> trainingJobStore.put(regionKey(region, name), v));
Named resource = taggable(arn);
String region = resource.region();
String name = resource.name();
String key = regionKey(region, name);
switch (resource.kind()) {
case "model" -> model(region, name).ifPresent(v -> modelStore.put(key, v));
case "endpoint-config" -> endpointConfig(region, name).ifPresent(v -> endpointConfigStore.put(key, v));
case "endpoint" -> endpoint(region, name).ifPresent(v -> endpointStore.put(key, v));
case "training-job" -> trainingJob(region, name).ifPresent(v -> trainingJobStore.put(key, v));
default -> {
// resourceTags refused any other kind before the tags changed.
}
}
}
/** ARN shape: {@code arn:aws:sagemaker:<region>:<account>:<type>/<name>}. */
private static String arnRegion(String arn) {
String region = AwsArnUtils.regionOrDefault(arn, "");
if (region.isBlank()) {
throw validation("ResourceArn is invalid");
/** The resource a tag operation's ARN names, refused when it names none in a Region. */
private static Named taggable(String arn) {
return named(arn).filter(resource -> !resource.region().isBlank())
.orElseThrow(() -> validation("ResourceArn is invalid"));
}
/**
* The SageMaker resource an ARN names ({@code arn:aws:sagemaker:<region>:<account>:<kind>/<name>}): its kind, the
* resource part before its first {@code /}, and its name, all after it, as each store keys it, so IAM's read of its
* tags and the tag operations' change of them reach the same resource; empty for no SageMaker ARN or no kind.
*/
private static Optional<Named> named(String arn) {
AwsArnUtils.Arn parsed;
try {
parsed = AwsArnUtils.parse(arn);
} catch (IllegalArgumentException expected) {
// Not an ARN: it names no SageMaker resource.
return Optional.empty();
}
return region;
String resource = parsed.resource();
int slash = resource.indexOf('/');
if (!"sagemaker".equals(parsed.service()) || slash < 0) {
return Optional.empty();
}
return Optional.of(new Named(parsed.accountId(), parsed.region(), resource.substring(0, slash),
resource.substring(slash + 1)));
}
private record Named(String account, String region, String kind, String name) {
}
private static String regionKey(String region, String name) {
@@ -94,6 +94,33 @@ class SageMakerServiceTest {
assertEquals(Map.of("kind", "job"), service.storedTags(job));
}
/**
* A tag operation and IAM's read of the tags reach the same resource the ARN names, its name all of the resource
* part after the kind's {@code /}: tagging a model named {@code blue/x} by its ARN changes that model, not the one
* named {@code x}, and its stored tags are the ones the tag operation changed.
*/
@Test
void tagOperationsAndStoredTagsReachTheResourceTheArnNames() throws Exception {
SageMakerService service = service();
String nested = null;
for (String[] model : new String[][] {{"x", "red"}, {"blue/x", "blue"}}) {
String arn = service.createModel(mapper.readTree("""
{"ModelName":"%s","PrimaryContainer":{"Image":"busybox:stable"},
"Tags":[{"Key":"team","Value":"%s"}]}""".formatted(model[0], model[1])), "us-east-1")
.path("ModelArn").asText();
nested = arn;
}
String plain = nested.replace("model/blue/x", "model/x");
service.addTags(mapper.readTree("{\"ResourceArn\":\"" + nested
+ "\",\"Tags\":[{\"Key\":\"owner\",\"Value\":\"blue\"}]}"));
assertEquals(Map.of("team", "blue", "owner", "blue"), service.storedTags(nested));
assertEquals(Map.of("team", "red"), service.storedTags(plain));
assertEquals(2, service.listTags(mapper.readTree("{\"ResourceArn\":\"" + nested + "\"}"))
.path("Tags").size());
}
@Test
void modelCrudAndDuplicateValidation() throws Exception {
SageMakerService service = service();