diff --git a/docs/services/iam.md b/docs/services/iam.md index b02e1c60eb..dc16af3c15 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -3071,8 +3071,8 @@ floci populates: its type (`aws:ResourceTag/`, `secretsmanager:ResourceTag/`), which `regen_iam_request_resources.py` reads from the Service Reference for every namespace it reads, valued from the tags the type's service holds on that resource (`StoredResourceArns.storedTags`); a service that holds none leaves them absent. - Secrets Manager answers for its secrets, and S3 Tables for its table buckets and tables. Tag keys differing - only in case are one condition key holding every value. + Secrets Manager answers for its secrets, S3 Tables for its table buckets and tables, and EventBridge + Scheduler for its schedule groups. 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 diff --git a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java index 5978e5f7fd..4aec68e485 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -25,9 +25,9 @@ import java.util.Optional; * the key an alias names; EMR, for the clusters of its instance groups; IAM Identity Center, for the accounts its * permission sets are provisioned to; Resource Explorer, for its default view; OAM and RAM, for the sinks, * invitations and shares of other accounts they act on; Secrets Manager, for the secret a name or an ARN names, - * random suffix included, and its tags; EventBridge Scheduler, for its default schedule group; CloudFormation, for - * the stack or the stack set a name names, whose id it minted; S3 Tables, for the table a request names by its table - * bucket, namespace and name, and the tags of a table bucket or a table); looked up lazily by + * random suffix included, and its tags; EventBridge Scheduler, for its default schedule group and its groups' tags; + * CloudFormation, for the stack or the stack set a name names, whose id it minted; S3 Tables, for the table a request + * names by its table bucket, namespace and name, and the tags of a table bucket or a table); looked up lazily by * {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance} for * the namespace a request or an ARN is of, so IAM never depends on that service directly.

* diff --git a/src/main/java/io/floci/aws/services/scheduler/SchedulerService.java b/src/main/java/io/floci/aws/services/scheduler/SchedulerService.java index da80242836..29113830a8 100644 --- a/src/main/java/io/floci/aws/services/scheduler/SchedulerService.java +++ b/src/main/java/io/floci/aws/services/scheduler/SchedulerService.java @@ -25,6 +25,7 @@ import org.jboss.logging.Logger; import java.time.DateTimeException; import java.time.Instant; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -41,6 +42,7 @@ public class SchedulerService implements StoredResourceArns { "arn:(?:" + AwsArnUtils.PARTITION_REGEX + "):iam::\\d{12}:role/[\\w+=,.@/-]+"); private static final String ROLE_ARN_MODEL_PATTERN = "^arn:aws(-[a-z]+)?:iam::\\d{12}:role\\/[\\w+=,.@\\/-]+$"; // partition-literal: the Scheduler model's RoleArn pattern, quoted verbatim in the error message private static final String DEFAULT_GROUP = "default"; + private static final String GROUP_RESOURCE = "schedule-group/"; // ListScheduleGroups and ListSchedules model MaxResults as 1-100 and NextToken as 1-2048 chars. private static final int MAX_PAGE_SIZE = 100; private static final int MAX_TOKEN_LENGTH = 2048; @@ -95,6 +97,31 @@ public class SchedulerService implements StoredResourceArns { return "schedule-group".equals(type) ? Optional.of(buildGroupArn(region, DEFAULT_GROUP)) : Optional.empty(); } + /** + * The tags of the schedule group stored under {@code arn}, in the account and Region the ARN names, by key, which + * IAM gives a resource-tag condition; empty when there is none, the default group before a request stores it among + * them. Reads that one group's entry, a pre-multi-account one of the default account in place, and matches it on + * the whole ARN, leaving it as it is. + */ + @Override + public Map storedTags(String arn) { + if (!AwsArnUtils.isArnFor(arn, "scheduler")) { + return Map.of(); + } + AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); + if (!parsed.resource().startsWith(GROUP_RESOURCE)) { + return Map.of(); + } + String key = groupKey(parsed.region(), parsed.resource().substring(GROUP_RESOURCE.length())); + String account = parsed.accountId(); + Optional group = groupStore instanceof AccountAwareStorageBackend aware + ? aware.getForAccount(account, key).or(() -> aware.getLegacyForAccount(account, key)) + : groupStore.get(key); + return group.filter(stored -> arn.equals(stored.getArn())) + .>map(stored -> Map.copyOf(stored.getTags())) + .orElse(Map.of()); + } + // ──────────────────────────── Schedule Groups ──────────────────────────── public ScheduleGroup getOrCreateDefaultGroup(String region) { @@ -184,8 +211,14 @@ public class SchedulerService implements StoredResourceArns { return; } ScheduleGroup group = getScheduleGroup(name, region); - group.getTags().putAll(tags); - group.setLastModificationDate(Instant.now()); + // A new map in place of the old, never a change to the one a tag lookup may be copying (storedTags, the tag + // listing), so each reads one whole set; writers of the group take turns. + synchronized (group) { + Map updated = new HashMap<>(group.getTags()); + updated.putAll(tags); + group.setTags(updated); + group.setLastModificationDate(Instant.now()); + } groupStore.put(groupKey(region, group.getName()), group); } @@ -194,8 +227,12 @@ public class SchedulerService implements StoredResourceArns { return; } ScheduleGroup group = getScheduleGroup(name, region); - tagKeys.forEach(group.getTags()::remove); - group.setLastModificationDate(Instant.now()); + synchronized (group) { + Map updated = new HashMap<>(group.getTags()); + tagKeys.forEach(updated::remove); + group.setTags(updated); + group.setLastModificationDate(Instant.now()); + } groupStore.put(groupKey(region, group.getName()), group); } @@ -500,7 +537,7 @@ public class SchedulerService implements StoredResourceArns { } private String buildGroupArn(String region, String name) { - return regionResolver.buildArn("scheduler", region, "schedule-group/" + name); + return regionResolver.buildArn("scheduler", region, GROUP_RESOURCE + name); } private static String groupKey(String region, String name) { diff --git a/src/main/java/io/floci/aws/services/scheduler/model/ScheduleGroup.java b/src/main/java/io/floci/aws/services/scheduler/model/ScheduleGroup.java index c6a127cded..3ec088b7d0 100644 --- a/src/main/java/io/floci/aws/services/scheduler/model/ScheduleGroup.java +++ b/src/main/java/io/floci/aws/services/scheduler/model/ScheduleGroup.java @@ -14,7 +14,7 @@ public class ScheduleGroup { private String state; private Instant creationDate; private Instant lastModificationDate; - private Map tags = new HashMap<>(); + private volatile Map tags = new HashMap<>(); public ScheduleGroup() {} diff --git a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java index c574df1f59..a45dcd9bbc 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java @@ -167,6 +167,26 @@ class IamRestRouteEnforcementIntegrationTest { .body(containsString("Invalid resource ARN")); } + /** A resource-tag condition reads the tags Scheduler holds on the schedule group a request names. */ + @Test + void aResourceTagConditionReadsTheTagsOfTheScheduleGroup() { + String tagged = "route-tagged-" + suffix(); + String untagged = "route-untagged-" + suffix(); + for (String[] group : new String[][] {{tagged, "{\"Tags\":[{\"Key\":\"team\",\"Value\":\"data\"}]}"}, + {untagged, "{}"}}) { + given().header("Authorization", auth(ADMIN, "scheduler")).contentType("application/json").body(group[1]) + .post("/schedule-groups/" + group[0]).then().statusCode(200); + cleanup.register(() -> given().header("Authorization", auth(ADMIN, "scheduler")) + .delete("/schedule-groups/" + group[0]).then().statusCode(200)); + } + String user = user("route-scheduler-tag-condition"); + + policy(user, "{\"Effect\":\"Allow\",\"Action\":\"scheduler:GetScheduleGroup\",\"Resource\":\"*\"," + + "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}"); + as(user, "scheduler").get("/schedule-groups/" + tagged).then().statusCode(200); + refused(as(user, "scheduler").get("/schedule-groups/" + untagged).then(), "scheduler:GetScheduleGroup"); + } + @Test void postTagsIsCheckedAgainstTheArnItsBodyNames() { // POST /tags tags the ARN its body names: one named in the query does not move the check. diff --git a/src/test/java/io/floci/aws/services/scheduler/SchedulerServiceTest.java b/src/test/java/io/floci/aws/services/scheduler/SchedulerServiceTest.java index d8619dbe63..6b68ebdd61 100644 --- a/src/test/java/io/floci/aws/services/scheduler/SchedulerServiceTest.java +++ b/src/test/java/io/floci/aws/services/scheduler/SchedulerServiceTest.java @@ -3,6 +3,7 @@ package io.floci.aws.services.scheduler; import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.PaginatedResult; import io.floci.aws.core.common.RegionResolver; +import io.floci.aws.core.storage.AccountAwareStorageBackend; import io.floci.aws.core.storage.InMemoryStorage; import io.floci.aws.services.scheduler.model.DeadLetterConfig; import io.floci.aws.services.scheduler.model.FlexibleTimeWindow; @@ -46,6 +47,51 @@ class SchedulerServiceTest { return req; } + + /** + * Tagging and untagging put a new map in place of the group's, never changing the one a tag lookup may be + * copying, so a lookup during either reads one whole set. + */ + @Test + void taggingReplacesTheGroupsTagsAndLeavesTheOldSetWhole() { + ScheduleGroup group = service.createScheduleGroup("tags", Map.of("team", "data"), REGION); + Map before = group.getTags(); + + service.tagScheduleGroup("tags", REGION, Map.of("owner", "alice")); + Map tagged = service.getScheduleGroup("tags", REGION).getTags(); + service.untagScheduleGroup("tags", REGION, List.of("team")); + + assertEquals(Map.of("team", "data"), before); + assertEquals(Map.of("team", "data", "owner", "alice"), tagged); + assertEquals(Map.of("owner", "alice"), service.getScheduleGroup("tags", REGION).getTags()); + } + + /** + * The tags IAM gives a resource-tag condition: the schedule group's, read on its whole ARN in the account and + * Region it names, a pre-multi-account entry of the default account in place; none for anything else, the default + * group before a request stores it among them. + */ + @Test + void storedTagsReadsTheScheduleGroupItsArnNames() { + InMemoryStorage raw = new InMemoryStorage<>(); + SchedulerService accounts = new SchedulerService(new AccountAwareStorageBackend<>(raw, null, "000000000000"), + new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000")); + ScheduleGroup group = accounts.createScheduleGroup("tagged", Map.of("team", "data"), REGION); + + assertEquals(Map.of("team", "data"), accounts.storedTags(group.getArn())); + assertEquals(Map.of(), accounts.storedTags(group.getArn().replace(":000000000000:", ":111122223333:"))); + assertEquals(Map.of(), accounts.storedTags(group.getArn().replace(REGION, "eu-west-1"))); + String schedule = group.getArn().replace("schedule-group/tagged", "schedule/tagged/s"); + assertEquals(Map.of(), accounts.storedTags(schedule)); + assertEquals(Map.of(), accounts.storedTags(accounts.findDefault("schedule-group", REGION).orElseThrow())); + + String legacyArn = group.getArn().replace("tagged", "legacy"); + ScheduleGroup legacy = new ScheduleGroup("legacy", legacyArn, "ACTIVE", Instant.now(), Instant.now()); + legacy.getTags().put("team", "old"); + raw.put("group:" + REGION + ":legacy", legacy); + assertEquals(Map.of("team", "old"), accounts.storedTags(legacyArn)); + assertTrue(raw.get("group:" + REGION + ":legacy").isPresent()); + } @Test void getOrCreateDefaultGroup() { ScheduleGroup group = service.getOrCreateDefaultGroup(REGION);