diff --git a/docs/services/iam.md b/docs/services/iam.md index 98bd05b55d..44223264df 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -2268,8 +2268,10 @@ As with passed roles, an operation Floci does not serve is in the table too. `Tag.N.Value` over the Query protocol. The tagging action is also checked for tags under any other spelling a handler might read, a member or a tag's key and value in any case, every element of a tag list and an object's structures, a form's list under any spelling and every number, so none a - handler applies goes unchecked. CodeDeploy's handler iterates its tags member whatever it holds, - so an object's structures are its tags too. + handler applies goes unchecked. An object's members, read as a map's tags, are a set of their own + beside its structures, so a member named as a structure's key does not hide the structure's tag + from a handler that iterates the object, as KMS's `CreateKey` does. CodeDeploy's handler iterates + its tags member whatever it holds, so an object's structures are its tags too. - CloudWatch Logs `CreateLogGroup`, which takes either of two tagging actions, is checked as described under CloudWatch Logs groups below. - Not read: an operation whose request carries no tags, which the Service Reference lists with a diff --git a/src/main/java/io/floci/aws/core/common/TagOnCreate.java b/src/main/java/io/floci/aws/core/common/TagOnCreate.java index 9957256d50..62ebccf1f1 100644 --- a/src/main/java/io/floci/aws/core/common/TagOnCreate.java +++ b/src/main/java/io/floci/aws/core/common/TagOnCreate.java @@ -181,7 +181,8 @@ final class TagOnCreate { /** * The tags a body carries at the member under any spelling a handler might read: its names, and a tag's * key and value, in any case, and a list or a map whichever the member is. One set for each top-level - * spelling, and for a tag specification each resource type. + * spelling, and for a tag specification each resource type; an object gives two, its elements read as tags + * and its members read as a map. */ private static List anySpellingInBody(JsonNode body, TagOnCreateMembers.Member member) { List found = new ArrayList<>(); @@ -189,28 +190,31 @@ final class TagOnCreate { if (!top.getKey().equalsIgnoreCase(member.steps().getFirst().name())) { return; } - Map> byType = new LinkedHashMap<>(); - anySpelling(top.getValue(), member, 0, null, byType); - byType.forEach((type, tags) -> { - if (!tags.isEmpty()) { - found.add(new TagSet(type, Collections.unmodifiableMap(tags))); - } - }); + Map> asTags = new LinkedHashMap<>(); + Map> asMap = new LinkedHashMap<>(); + anySpelling(top.getValue(), member, 0, null, asTags, asMap); + for (Map> byType : List.of(asTags, asMap)) { + byType.forEach((type, tags) -> { + if (!tags.isEmpty()) { + found.add(new TagSet(type, Collections.unmodifiableMap(tags))); + } + }); + } }); return found; } private static void anySpelling(JsonNode value, TagOnCreateMembers.Member member, int index, String type, - Map> byType) { + Map> asTags, Map> asMap) { List steps = member.steps(); if (value == null) { return; } if (index == steps.size() - 1) { - Map tags = byType.computeIfAbsent(type, ignored -> new LinkedHashMap<>()); + Map tags = asTags.computeIfAbsent(type, ignored -> new LinkedHashMap<>()); // Every element is a tag, its key as text whatever it holds and a missing one empty, as a handler that // reads it with asText takes it; an object's values that are structures are elements too, as a handler - // iterating it reads them, and its members are a map's tags. + // iterating it reads them. for (JsonNode tag : value) { if (value.isObject() && !tag.isObject()) { continue; @@ -219,8 +223,11 @@ final class TagOnCreate { tags.put(key == null || key.isNull() ? "" : key.asText(), text(field(tag, member.value() == null ? "value" : member.value()))); } + // An object's members are a map's tags, a set of their own: in one with its elements, a member named as + // a structure's key would replace the tag a handler iterating the object applies. if (value.isObject()) { - value.properties().forEach(tag -> tags.put(tag.getKey(), tag.getValue().isNull() ? "" + Map map = asMap.computeIfAbsent(type, ignored -> new LinkedHashMap<>()); + value.properties().forEach(tag -> map.put(tag.getKey(), tag.getValue().isNull() ? "" : tag.getValue().asText())); } return; @@ -230,10 +237,10 @@ final class TagOnCreate { for (JsonNode element : value) { JsonNode kind = member.resourceType() == null ? null : field(element, member.resourceType()); anySpelling(field(element, next), member, index + 1, - kind != null && kind.isTextual() ? kind.asText() : type, byType); + kind != null && kind.isTextual() ? kind.asText() : type, asTags, asMap); } } else if (value.isObject()) { - anySpelling(field(step(value, steps.get(index)), next), member, index + 1, type, byType); + anySpelling(field(step(value, steps.get(index)), next), member, index + 1, type, asTags, asMap); } } diff --git a/src/test/java/io/floci/aws/core/common/TagOnCreateTest.java b/src/test/java/io/floci/aws/core/common/TagOnCreateTest.java index dbb2c7fd95..d3a0564bdf 100644 --- a/src/test/java/io/floci/aws/core/common/TagOnCreateTest.java +++ b/src/test/java/io/floci/aws/core/common/TagOnCreateTest.java @@ -100,6 +100,23 @@ class TagOnCreateTest { .orElseThrow().otherSpellings()); } + /** + * An object read as a list of tags and as a map gives two sets, so a member named as its structure's key does not + * replace the tag a handler iterating the object applies. + */ + @Test + void anObjectReadAsTagsAndAsAMapGivesASetForEach() { + Tagging collision = TagOnCreate.of("kms:CreateKey", json( + "{\"Tags\":{\"team\":{\"TagKey\":\"team\",\"TagValue\":\"red\"}}}")).orElseThrow(); + assertEquals(List.of(), collision.sets()); + assertEquals(List.of(set(Map.of("team", "red")), set(Map.of("team", ""))), collision.otherSpellings()); + assertEquals(List.of(List.of(), List.of("red"), List.of("")), TagOnCreate.requestTagContexts(collision).stream() + .map(context -> context.getOrDefault("aws:RequestTag/team", List.of())).toList()); + // A map holding only strings is read as a map alone. + assertEquals(List.of(set(Map.of("team", "red"))), TagOnCreate.of("kms:CreateKey", json( + "{\"Tags\":{\"team\":\"red\"}}")).orElseThrow().otherSpellings()); + } + /** * CodeDeploy's handler reads a tag's key and value as text whatever they hold, a missing key as an empty one, * and so does the check; other handlers' keys are read as text when they are a number or a flag too. diff --git a/src/test/java/io/floci/aws/services/iam/IamTagOnCreateEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamTagOnCreateEnforcementIntegrationTest.java index ee7aab3e6b..c764648b08 100644 --- a/src/test/java/io/floci/aws/services/iam/IamTagOnCreateEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamTagOnCreateEnforcementIntegrationTest.java @@ -171,6 +171,28 @@ class IamTagOnCreateEnforcementIntegrationTest { user.json("kms", "TrentService.CreateKey", "1.1").body(createKey).post("/").then().statusCode(200); } + /** + * KMS's handler iterates {@code Tags} whatever it holds, so an object whose member is named as its structure's key + * still tags the key with that structure: a deny on the tag holds on the create's own action and on the tagging + * action alike. + */ + @Test + void aTagInAnObjectIsCheckedThoughAMemberOfTheSameNameIsNoTag() { + User user = user("tag-collision"); + String allowed = allow("kms:CreateKey") + "," + allow("kms:TagResource"); + for (String action : List.of("kms:CreateKey", "kms:TagResource")) { + user.policy(allowed + ",{\"Effect\":\"Deny\",\"Action\":\"" + action + "\",\"Resource\":\"*\"," + + "\"Condition\":{\"StringEquals\":{\"aws:RequestTag/team\":\"red\"}}}"); + refusedJson(user.json("kms", "TrentService.CreateKey", "1.1").body(keyTaggedByObject("red")) + .post("/").then(), action); + String keyId = user.json("kms", "TrentService.CreateKey", "1.1").body(keyTaggedByObject("blue")).post("/") + .then().statusCode(200).extract().path("KeyMetadata.KeyId"); + admin("kms").header("X-Amz-Target", "TrentService.ListResourceTags") + .contentType("application/x-amz-json-1.1").body("{\"KeyId\":\"" + keyId + "\"}").post("/") + .then().statusCode(200).body("Tags.find { it.TagKey == 'team' }.TagValue", equalTo("blue")); + } + } + /** * The tagging action is found by the operation, not by the action it is authorized as: Budgets' * {@code CreateBudget} is authorized as {@code budgets:ModifyBudget}. @@ -441,8 +463,9 @@ class IamTagOnCreateEnforcementIntegrationTest { /** * OpenSearch refuses a {@code TagList} that is not as its model has it, before it changes anything, so no reading * of one can differ from the tags a domain gets: under every grant and a deny on {@code team=red}, none of these - * creates a domain, among them an object whose structure's key is also its member's name and a tag of the denied - * key with no value after it. {@code AddTags} shares the reader, and tags at the model's limits are kept. + * creates a domain, among them a tag of the denied key with no value after it. An object whose structure's key is + * also its member's name is refused by the deny, its structure read as a tag. {@code AddTags} shares the reader, + * and tags at the model's limits are kept. */ @ParameterizedTest @CsvSource({"/2021-01-01/opensearch/domain, es:CreateDomain", @@ -452,14 +475,15 @@ class IamTagOnCreateEnforcementIntegrationTest { String name = "tag-os-" + suffix(); String arn = "arn:aws:es:us-east-1:" + ACCOUNT + ":domain/" + name; String red = "{\"Key\":\"team\",\"Value\":\"red\"}"; + String inObject = "{\"team\":" + red + "}"; user.policy(allow(create)); - refusedRest(user.rest("es").body(domainWithTagList(name, "{\"team\":" + red + "}")).post(path).then(), - "es:AddTags"); + refusedRest(user.rest("es").body(domainWithTagList(name, inObject)).post(path).then(), "es:AddTags"); user.policy(allow(create) + "," + allow("es:AddTags") + ",{\"Effect\":\"Deny\",\"Action\":\"es:AddTags\"," + "\"Resource\":\"*\",\"Condition\":{\"StringEquals\":{\"aws:RequestTag/team\":\"red\"}}}"); refusedRest(user.rest("es").body(domainWithTagList(name, "[" + red + "]")).post(path).then(), "es:AddTags"); - for (String tagList : List.of("{\"team\":" + red + "}", "[" + red + ",{\"Key\":\"team\"}]", + refusedRest(user.rest("es").body(domainWithTagList(name, inObject)).post(path).then(), "es:AddTags"); + for (String tagList : List.of("[" + red + ",{\"Key\":\"team\"}]", "[" + red + ",{\"Key\":\"team\",\"Value\":null}]", "[{\"Value\":\"blue\"}]", "[{\"Key\":\"\",\"Value\":\"blue\"}]", "[{\"Key\":123,\"Value\":\"blue\"}]", "[{\"Key\":\"team\",\"Value\":{\"a\":1}}]", "[\"team\"]", @@ -541,6 +565,11 @@ class IamTagOnCreateEnforcementIntegrationTest { "Tags":{"team":"blue"}}""".formatted(name, role); } + /** A {@code CreateKey} body whose {@code Tags} is an object, its one member named as its structure's key. */ + private static String keyTaggedByObject(String team) { + return "{\"Tags\":{\"team\":{\"TagKey\":\"team\",\"TagValue\":\"" + team + "\"}}}"; + } + /** A {@code CreateAnalyzer} body tagging the analyzer with the {@code team} given, or with no tags. */ private static String analyzer(String name, String team) { String tags = team == null ? "" : ",\"tags\":{\"team\":\"" + team + "\"}";