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.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-18 05:43:16 -04:00
1 parent 933b617961
commit 1863f7f4bb
8 files changed
+139 -17

No files matched your search

@@ -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<String, String> 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<String, DataSyncAgent> 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(
@@ -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/<id>}, 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}