From e2f535bc4f89e697ac90b721ea8a0abf945d9089 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Mon, 30 Nov 2026 21:52:30 -0400 Subject: [PATCH] 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. --- .../services/sagemaker/SageMakerService.java | 92 ++++++++++++------- .../sagemaker/SageMakerServiceTest.java | 27 ++++++ 2 files changed, 87 insertions(+), 32 deletions(-) diff --git a/src/main/java/io/floci/aws/services/sagemaker/SageMakerService.java b/src/main/java/io/floci/aws/services/sagemaker/SageMakerService.java index 24eafe19eb..820c2ae374 100644 --- a/src/main/java/io/floci/aws/services/sagemaker/SageMakerService.java +++ b/src/main/java/io/floci/aws/services/sagemaker/SageMakerService.java @@ -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 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(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> tags = switch (resource.substring(0, slash)) { + String key = regionKey(named.get().region(), named.get().name()); + Optional> 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 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:::/}. */ - 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:::/}): 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(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) { diff --git a/src/test/java/io/floci/aws/services/sagemaker/SageMakerServiceTest.java b/src/test/java/io/floci/aws/services/sagemaker/SageMakerServiceTest.java index 48a9d59920..b1f8343815 100644 --- a/src/test/java/io/floci/aws/services/sagemaker/SageMakerServiceTest.java +++ b/src/test/java/io/floci/aws/services/sagemaker/SageMakerServiceTest.java @@ -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();