From 1863f7f4bb696b162ee531947e43059a51225ed3 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Wed, 18 Nov 2026 05:43:16 -0400 Subject: [PATCH] fix(datasync): give resource-tag conditions the tags of the agent, location or task a request names (#8844) The generated request resources give DataSync's agent, location and task types the aws:ResourceTag/ key, but no service answered the datasync namespace's stored tags, so a condition on their tags saw none. DataSyncService now answers it: storedTags reads the resource stored under the ARN itself, in the request's account, in place, as the tag handlers find it. TagResource and UntagResource changed the tag map in place with no lock; they now give the resource a new map one call at a time, and the tags fields are volatile. --- docs/services/iam.md | 5 +- .../aws/core/common/StoredResourceArns.java | 7 ++- .../services/datasync/DataSyncService.java | 56 +++++++++++++++++-- .../datasync/model/DataSyncAgent.java | 2 +- .../datasync/model/DataSyncLocation.java | 2 +- .../services/datasync/model/DataSyncTask.java | 2 +- .../datasync/DataSyncServiceTest.java | 47 ++++++++++++++++ ...estResourceEnforcementIntegrationTest.java | 35 ++++++++++-- 8 files changed, 139 insertions(+), 17 deletions(-) diff --git a/docs/services/iam.md b/docs/services/iam.md index 67618ca98c..2c2dc77e9a 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -3356,8 +3356,9 @@ floci populates: pools, OpenSearch for its domains, Signer for its signing profiles, Access Analyzer for its analyzers, Managed Service for Apache Flink for its applications, S3 Vectors for its vector buckets and indexes, Timestream for InfluxDB for its DB instances, DB clusters, parameter groups and backups, CloudTrail for its trails, Budgets for its budgets and budget - actions, AWS FIS for its experiment templates, experiments and actions, and CodePipeline for its pipelines, webhooks - and custom action types. Tag keys differing only in case are one condition key holding every value. + actions, AWS FIS for its experiment templates, experiments and actions, CodePipeline for its pipelines, webhooks and + custom action types, and DataSync for its agents, locations and tasks. 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 fde2557f6b..759b765264 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -50,9 +50,10 @@ import java.util.Optional; * the tags of an application; S3 Vectors, for the tags of a vector bucket or an index; Timestream for InfluxDB, for the * tags of a DB instance, a DB cluster, a parameter group or a backup; CloudTrail, for the tags of a trail; Budgets, for * the tags of a budget or a budget action; AWS FIS, for the tags of an experiment template, an experiment or an action; - * CodePipeline, for the tags of a pipeline, a webhook or a custom action type); 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.

+ * CodePipeline, for the tags of a pipeline, a webhook or a custom action type; DataSync, for the tags of an agent, a + * location or a task); 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.

* *

Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find}, * {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone diff --git a/src/main/java/io/floci/aws/services/datasync/DataSyncService.java b/src/main/java/io/floci/aws/services/datasync/DataSyncService.java index b6c88e7ac9..b6173d4f4f 100644 --- a/src/main/java/io/floci/aws/services/datasync/DataSyncService.java +++ b/src/main/java/io/floci/aws/services/datasync/DataSyncService.java @@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.floci.aws.core.common.AwsArnUtils; import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.RegionResolver; +import io.floci.aws.core.common.StoredResourceArns; +import io.floci.aws.core.storage.AccountAwareStorageBackend; import io.floci.aws.core.storage.StorageBackend; import io.floci.aws.core.storage.StorageFactory; import io.floci.aws.services.datasync.model.DataSyncAgent; @@ -27,6 +29,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Function; @@ -42,7 +45,7 @@ import java.util.function.Function; * back. */ @ApplicationScoped -public class DataSyncService { +public class DataSyncService implements StoredResourceArns { private static final Logger LOG = Logger.getLogger(DataSyncService.class); @@ -319,18 +322,61 @@ public class DataSyncService { return new LinkedHashMap<>(findTaggable(resourceArn).getTags()); } - public void tagResource(String resourceArn, Map tags) { + /** + * Gives the resource a new map holding its tags and {@code tags}. Tag calls run one at a time, so two of them never + * lose one's tags, and a lookup reads one whole set. + */ + public synchronized void tagResource(String resourceArn, Map tags) { DataSyncTaggable resource = findTaggable(resourceArn); - resource.getTags().putAll(tags); + Map merged = new LinkedHashMap<>(resource.getTags()); + merged.putAll(tags); + resource.setTags(merged); persist(resourceArn, resource); } - public void untagResource(String resourceArn, List tagKeys) { + /** Gives the resource a new map without {@code tagKeys}, one tag call at a time, as {@link #tagResource}. */ + public synchronized void untagResource(String resourceArn, List tagKeys) { DataSyncTaggable resource = findTaggable(resourceArn); - tagKeys.forEach(resource.getTags()::remove); + Map remaining = new LinkedHashMap<>(resource.getTags()); + tagKeys.forEach(remaining::remove); + resource.setTags(remaining); persist(resourceArn, resource); } + @Override + public String namespace() { + return "datasync"; + } + + /** A request names an agent, a location or a task by its ARN: nothing to find. */ + @Override + public Optional find(String type, List names, String region) { + return Optional.empty(); + } + + /** + * The tags of the agent, the location or the task whose ARN is {@code arn}, in the request's account, where the tag + * handlers read: an ARN of another account names none. Read in place under the ARN itself, which each is stored + * under, an agent, a location or a task stored before storage was account-aware included. A task execution's ARN + * reads nothing, Floci holding no tags of its own for it. Tag calls give the resource a new map, so the read is of + * one whole set. + */ + @Override + public Map storedTags(String arn) { + if (!AwsArnUtils.isArnFor(arn, "datasync")) { + return Map.of(); + } + String account = regionResolver.getAccountId(); + if (!AwsArnUtils.parse(arn).accountId().equals(account)) { + return Map.of(); + } + Optional resource = AccountAwareStorageBackend.readInAccount(agents, account, arn) + .map(agent -> agent) + .or(() -> AccountAwareStorageBackend.readInAccount(locations, account, arn).map(location -> location)) + .or(() -> AccountAwareStorageBackend.readInAccount(tasks, account, arn).map(task -> task)); + return new LinkedHashMap<>(resource.map(DataSyncTaggable::getTags).orElse(Map.of())); + } + private DataSyncTaggable findTaggable(String resourceArn) { requireArn(resourceArn, "ResourceArn"); return agents.get(resourceArn).map(agent -> agent) diff --git a/src/main/java/io/floci/aws/services/datasync/model/DataSyncAgent.java b/src/main/java/io/floci/aws/services/datasync/model/DataSyncAgent.java index 5c46e8cae8..11d28dd313 100644 --- a/src/main/java/io/floci/aws/services/datasync/model/DataSyncAgent.java +++ b/src/main/java/io/floci/aws/services/datasync/model/DataSyncAgent.java @@ -22,7 +22,7 @@ public class DataSyncAgent implements DataSyncTaggable { private String platformVersion; private Instant creationTime; private Instant lastConnectionTime; - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); public String getAgentArn() { return agentArn; diff --git a/src/main/java/io/floci/aws/services/datasync/model/DataSyncLocation.java b/src/main/java/io/floci/aws/services/datasync/model/DataSyncLocation.java index 430a1d590d..d0e436cd7c 100644 --- a/src/main/java/io/floci/aws/services/datasync/model/DataSyncLocation.java +++ b/src/main/java/io/floci/aws/services/datasync/model/DataSyncLocation.java @@ -21,7 +21,7 @@ public class DataSyncLocation implements DataSyncTaggable { private String region; private JsonNode configuration; private Instant creationTime; - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); public String getLocationArn() { return locationArn; diff --git a/src/main/java/io/floci/aws/services/datasync/model/DataSyncTask.java b/src/main/java/io/floci/aws/services/datasync/model/DataSyncTask.java index 4427fb2d32..d85e37a3de 100644 --- a/src/main/java/io/floci/aws/services/datasync/model/DataSyncTask.java +++ b/src/main/java/io/floci/aws/services/datasync/model/DataSyncTask.java @@ -24,7 +24,7 @@ public class DataSyncTask implements DataSyncTaggable { private JsonNode manifestConfig; private JsonNode taskReportConfig; private Instant creationTime; - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); public String getTaskArn() { return taskArn; diff --git a/src/test/java/io/floci/aws/services/datasync/DataSyncServiceTest.java b/src/test/java/io/floci/aws/services/datasync/DataSyncServiceTest.java index 6093b9bc30..46d389ba28 100644 --- a/src/test/java/io/floci/aws/services/datasync/DataSyncServiceTest.java +++ b/src/test/java/io/floci/aws/services/datasync/DataSyncServiceTest.java @@ -5,17 +5,20 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.floci.aws.core.common.AwsException; 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.core.storage.StorageFactory; import io.floci.aws.services.datasync.model.DataSyncAgent; import io.floci.aws.services.datasync.model.DataSyncLocation; import io.floci.aws.services.datasync.model.DataSyncLocationType; import io.floci.aws.services.datasync.model.DataSyncTask; +import io.floci.aws.testing.InMemoryStorageFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import java.util.List; import java.util.Map; +import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -57,6 +60,50 @@ class DataSyncServiceTest { return service.createLocation(type, json(body), REGION); } + /** + * IAM's lookup reads the tags of the agent or the location the ARN names, as the tag calls leave them, a map a + * reader already holds staying as it was, and nothing for another account's, another Region's or an unknown ARN. + */ + @Test + void storedTagsAreTheTagsOfTheResourceTheArnNamesAndTaggingPutsANewMap() { + DataSyncAgent agent = service.createAgent(json(""" + {"ActivationKey": "AAAAA-1AAAA-BB1CC-DDDDD-EEEEE", "Tags": [{"Key": "team", "Value": "web"}]} + """), REGION); + String arn = agent.getAgentArn(); + service.tagResource(arn, Map.of("team", "data", "env", "dev")); + Map held = agent.getTags(); + service.untagResource(arn, List.of("env")); + DataSyncLocation location = createLocation(DataSyncLocationType.S3, """ + {"S3BucketArn": "arn:aws:s3:::floci-bucket", "Tags": [{"Key": "team", "Value": "ops"}], + "S3Config": {"BucketAccessRoleArn": "arn:aws:iam::000000000000:role/datasync"}} + """); + + assertEquals("datasync", service.namespace()); + assertEquals(Map.of("team", "data", "env", "dev"), held); + assertEquals(Map.of("team", "data"), service.storedTags(arn)); + assertEquals(Map.of("team", "ops"), service.storedTags(location.getLocationArn())); + assertTrue(service.storedTags(arn.replace("000000000000", "111122223333")).isEmpty()); + assertTrue(service.storedTags(arn.replace(REGION, "us-west-2")).isEmpty()); + assertTrue(service.storedTags(arn + "0").isEmpty()); + assertTrue(service.storedTags("arn:aws:s3:::floci-bucket").isEmpty()); + } + + /** An agent stored before storage was account-aware is read in place: the lookup migrates nothing. */ + @Test + void storedTagsReadAPreAccountAwareAgentInPlace() { + InMemoryStorage raw = new InMemoryStorage<>(); + DataSyncService legacyState = new DataSyncService( + new InMemoryStorageFactory("000000000000").withStore("datasync-agents.json", raw), + new RegionResolver(REGION, "000000000000"), mapper); + DataSyncAgent legacy = new DataSyncAgent(); + legacy.setAgentArn("arn:aws:datasync:us-east-1:000000000000:agent/agent-0123456789abcdef0"); + legacy.setTags(Map.of("team", "data")); + raw.put(legacy.getAgentArn(), legacy); + + assertEquals(Map.of("team", "data"), legacyState.storedTags(legacy.getAgentArn())); + assertEquals(Set.of(legacy.getAgentArn()), raw.keys()); + } + @Test void createAgentIsOnlineAndPublicWithoutAVpcEndpoint() { DataSyncAgent agent = service.createAgent( diff --git a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java index 661b56edaf..50f0b8b749 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java @@ -2039,10 +2039,7 @@ class IamRequestResourceEnforcementIntegrationTest { /** A DataSync agent is checked on its ARN, which ends in the id DataSync gave it, to describe it or to tag it. */ @Test void aDataSyncAgentIsCheckedOnItsArn() { - String arn = adminJson("datasync", "FmrsService.CreateAgent").body("{\"ActivationKey\":\"RES-ACTIVATION\"}") - .post("/").then().statusCode(200).extract().path("AgentArn"); - cleanup.register(() -> adminJson("datasync", "FmrsService.DeleteAgent") - .body("{\"AgentArn\":\"" + arn + "\"}").post("/")); + String arn = dataSyncAgent(null); String agents = "arn:aws:datasync:" + REGION + ":" + ACCOUNT + ":agent/"; assertTrue(arn.startsWith(agents + "agent-"), arn); String other = agents + "agent-0123456789abcdef0"; @@ -2056,6 +2053,36 @@ class IamRequestResourceEnforcementIntegrationTest { refusedJson(dataSync(user, "TagResource", "{\"ResourceArn\":\"" + other + tags), "datasync:TagResource"); } + /** + * An {@code aws:ResourceTag} condition reads the tags DataSync holds on the agent a request names, as TagResource + * leaves them, so an allow on one team's tag lets the caller describe that team's agent and not another's. + */ + @Test + void aDataSyncResourceTagConditionReadsTheTagsOfTheAgent() { + String web = "[{\"Key\":\"team\",\"Value\":\"web\"}]"; + String tagged = dataSyncAgent(web); + String other = dataSyncAgent(web); + adminJson("datasync", "FmrsService.TagResource") + .body("{\"ResourceArn\":\"" + tagged + "\",\"Tags\":[{\"Key\":\"team\",\"Value\":\"data\"}]}") + .post("/").then().statusCode(200); + + User user = user("res-agent-tags"); + user.policy("{\"Effect\":\"Allow\",\"Action\":\"datasync:DescribeAgent\",\"Resource\":\"*\"," + + "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}"); + dataSync(user, "DescribeAgent", "{\"AgentArn\":\"" + tagged + "\"}").statusCode(200); + refusedJson(dataSync(user, "DescribeAgent", "{\"AgentArn\":\"" + other + "\"}"), "datasync:DescribeAgent"); + } + + /** The ARN of an agent an administrator creates, with the {@code tags} list if any, deleted afterwards. */ + private String dataSyncAgent(String tags) { + String arn = adminJson("datasync", "FmrsService.CreateAgent") + .body("{\"ActivationKey\":\"RES-ACTIVATION\"" + (tags == null ? "" : ",\"Tags\":" + tags) + "}") + .post("/").then().statusCode(200).extract().path("AgentArn"); + cleanup.register(() -> adminJson("datasync", "FmrsService.DeleteAgent") + .body("{\"AgentArn\":\"" + arn + "\"}").post("/")); + return arn; + } + /** * An EMR cluster is checked on {@code cluster/}, the ARN EMR gives it, whether a request names it by its * {@code ClusterId} or among its {@code JobFlowIds}, and a tag request on the cluster its {@code ResourceId}