From e3ca9a6064c0e6b44e1e8d51635396f090a87d83 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Thu, 26 Nov 2026 13:05:06 -0600 Subject: [PATCH] fix(datasync): answer agents, locations and tasks only in their Region (#9488) ListAgents, ListLocations and ListTasks scanned every Region, and the by-ARN reads, updates, deletes, CreateTask's location lookups and the tag calls read the store whatever Region the request was signed for. Each resource is stored under its ARN, which carries its Region, so lists and reads now keep only the request Region's through RegionResolver.inRequestRegion, and an ARN of another Region answers InvalidRequestException. --- .../services/datasync/DataSyncService.java | 22 +++-- .../DataSyncRegionScopingIntegrationTest.java | 98 +++++++++++++++++++ 2 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/datasync/DataSyncRegionScopingIntegrationTest.java 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 b6173d4f4f..7c4f0b67ab 100644 --- a/src/main/java/io/floci/aws/services/datasync/DataSyncService.java +++ b/src/main/java/io/floci/aws/services/datasync/DataSyncService.java @@ -117,8 +117,9 @@ public class DataSyncService implements StoredResourceArns { public DataSyncAgent getAgent(String agentArn) { requireArn(agentArn, "AgentArn"); - return agents.get(agentArn).orElseThrow(() -> new AwsException("InvalidRequestException", - "Agent " + agentArn + " is not found.", 400)); + return agents.get(agentArn).filter(agent -> regionResolver.inRequestRegion(agentArn)) + .orElseThrow(() -> new AwsException("InvalidRequestException", + "Agent " + agentArn + " is not found.", 400)); } public DataSyncAgent updateAgent(String agentArn, String name) { @@ -137,7 +138,7 @@ public class DataSyncService implements StoredResourceArns { } public Page listAgents(String nextToken, int maxResults) { - List all = agents.scan(key -> true); + List all = agents.scan(regionResolver::inRequestRegion); all.sort(Comparator.comparing(DataSyncAgent::getAgentArn)); return paginate(all, DataSyncAgent::getAgentArn, nextToken, maxResults); } @@ -177,8 +178,9 @@ public class DataSyncService implements StoredResourceArns { public DataSyncLocation getLocation(String locationArn) { requireArn(locationArn, "LocationArn"); - return locations.get(locationArn).orElseThrow(() -> new AwsException("InvalidRequestException", - "Location " + locationArn + " is not found.", 400)); + return locations.get(locationArn).filter(location -> regionResolver.inRequestRegion(locationArn)) + .orElseThrow(() -> new AwsException("InvalidRequestException", + "Location " + locationArn + " is not found.", 400)); } public DataSyncLocation getLocation(String locationArn, DataSyncLocationType expected) { @@ -219,7 +221,7 @@ public class DataSyncService implements StoredResourceArns { public Page listLocations(JsonNode filters, String nextToken, int maxResults) { List all = new ArrayList<>(); - for (DataSyncLocation location : locations.scan(key -> true)) { + for (DataSyncLocation location : locations.scan(regionResolver::inRequestRegion)) { if (matchesLocationFilters(location, filters)) { all.add(location); } @@ -261,8 +263,9 @@ public class DataSyncService implements StoredResourceArns { public DataSyncTask getTask(String taskArn) { requireArn(taskArn, "TaskArn"); - return tasks.get(taskArn).orElseThrow(() -> new AwsException("InvalidRequestException", - "Task " + taskArn + " is not found.", 400)); + return tasks.get(taskArn).filter(task -> regionResolver.inRequestRegion(taskArn)) + .orElseThrow(() -> new AwsException("InvalidRequestException", + "Task " + taskArn + " is not found.", 400)); } public DataSyncTask updateTask(JsonNode request) { @@ -307,7 +310,7 @@ public class DataSyncService implements StoredResourceArns { public Page listTasks(JsonNode filters, String nextToken, int maxResults) { List all = new ArrayList<>(); - for (DataSyncTask task : tasks.scan(key -> true)) { + for (DataSyncTask task : tasks.scan(regionResolver::inRequestRegion)) { if (matchesTaskFilters(task, filters)) { all.add(task); } @@ -382,6 +385,7 @@ public class DataSyncService implements StoredResourceArns { return agents.get(resourceArn).map(agent -> agent) .or(() -> locations.get(resourceArn).map(location -> location)) .or(() -> tasks.get(resourceArn).map(task -> task)) + .filter(resource -> regionResolver.inRequestRegion(resourceArn)) .orElseThrow(() -> new AwsException("InvalidRequestException", "Resource " + resourceArn + " is not found.", 400)); } diff --git a/src/test/java/io/floci/aws/services/datasync/DataSyncRegionScopingIntegrationTest.java b/src/test/java/io/floci/aws/services/datasync/DataSyncRegionScopingIntegrationTest.java new file mode 100644 index 0000000000..8c38de86b6 --- /dev/null +++ b/src/test/java/io/floci/aws/services/datasync/DataSyncRegionScopingIntegrationTest.java @@ -0,0 +1,98 @@ +package io.floci.aws.services.datasync; + +import io.floci.aws.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; + +/** + * DataSync agents, locations and tasks are Regional: each is listed and reached by its ARN only in the Region it was + * created in. Another Region answers {@code InvalidRequestException}, as it does for an ARN that names nothing. + */ +@QuarkusTest +class DataSyncRegionScopingIntegrationTest { + + private static final String HOME = "us-east-1"; + private static final String OTHER = "eu-west-1"; + private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/datasync-bucket-access"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void anAgentALocationAndATaskAreListedAndReachedOnlyInTheirRegion() { + String agentArn = call(HOME, "CreateAgent", "{\"ActivationKey\":\"AAAAA-1AAAA-BB1CC-DDDDD-EEEEE\"}") + .statusCode(200).extract().path("AgentArn"); + String source = s3Location(HOME, "regional-source"); + String destination = s3Location(HOME, "regional-destination"); + String taskArn = call(HOME, "CreateTask", "{\"SourceLocationArn\":\"" + source + + "\",\"DestinationLocationArn\":\"" + destination + "\"}").statusCode(200).extract().path("TaskArn"); + try { + call(OTHER, "ListAgents", "{}").statusCode(200).body("Agents.AgentArn", not(hasItem(agentArn))); + call(OTHER, "ListLocations", "{}").statusCode(200) + .body("Locations.LocationArn", not(hasItem(source))); + call(OTHER, "ListTasks", "{}").statusCode(200).body("Tasks.TaskArn", not(hasItem(taskArn))); + + invalid(call(OTHER, "DescribeAgent", "{\"AgentArn\":\"" + agentArn + "\"}")); + invalid(call(OTHER, "UpdateAgent", "{\"AgentArn\":\"" + agentArn + "\",\"Name\":\"moved\"}")); + invalid(call(OTHER, "DeleteAgent", "{\"AgentArn\":\"" + agentArn + "\"}")); + invalid(call(OTHER, "DescribeLocationS3", "{\"LocationArn\":\"" + source + "\"}")); + invalid(call(OTHER, "DeleteLocation", "{\"LocationArn\":\"" + source + "\"}")); + invalid(call(OTHER, "DescribeTask", "{\"TaskArn\":\"" + taskArn + "\"}")); + invalid(call(OTHER, "UpdateTask", "{\"TaskArn\":\"" + taskArn + "\",\"Name\":\"moved\"}")); + invalid(call(OTHER, "DeleteTask", "{\"TaskArn\":\"" + taskArn + "\"}")); + invalid(call(OTHER, "ListTagsForResource", "{\"ResourceArn\":\"" + taskArn + "\"}")); + invalid(call(OTHER, "TagResource", "{\"ResourceArn\":\"" + agentArn + + "\",\"Tags\":[{\"Key\":\"k\",\"Value\":\"v\"}]}")); + String otherDestination = s3Location(OTHER, "regional-other"); + try { + invalid(call(OTHER, "CreateTask", "{\"SourceLocationArn\":\"" + source + + "\",\"DestinationLocationArn\":\"" + otherDestination + "\"}")); + } finally { + call(OTHER, "DeleteLocation", "{\"LocationArn\":\"" + otherDestination + "\"}").statusCode(200); + } + + call(HOME, "ListAgents", "{}").statusCode(200).body("Agents.AgentArn", hasItem(agentArn)); + call(HOME, "ListLocations", "{}").statusCode(200).body("Locations.LocationArn", hasItem(source)); + call(HOME, "ListTasks", "{}").statusCode(200).body("Tasks.TaskArn", hasItem(taskArn)); + call(HOME, "DescribeAgent", "{\"AgentArn\":\"" + agentArn + "\"}").statusCode(200) + .body("AgentArn", equalTo(agentArn)); + call(HOME, "DescribeLocationS3", "{\"LocationArn\":\"" + source + "\"}").statusCode(200) + .body("LocationArn", equalTo(source)); + call(HOME, "DescribeTask", "{\"TaskArn\":\"" + taskArn + "\"}").statusCode(200) + .body("TaskArn", equalTo(taskArn)); + } finally { + call(HOME, "DeleteTask", "{\"TaskArn\":\"" + taskArn + "\"}"); + call(HOME, "DeleteLocation", "{\"LocationArn\":\"" + source + "\"}"); + call(HOME, "DeleteLocation", "{\"LocationArn\":\"" + destination + "\"}"); + call(HOME, "DeleteAgent", "{\"AgentArn\":\"" + agentArn + "\"}"); + } + } + + private static String s3Location(String region, String bucket) { + return call(region, "CreateLocationS3", "{\"S3BucketArn\":\"arn:aws:s3:::" + bucket + + "\",\"S3Config\":{\"BucketAccessRoleArn\":\"" + ROLE_ARN + "\"}}") + .statusCode(200).extract().path("LocationArn"); + } + + private static ValidatableResponse call(String region, String action, String body) { + return given() + .contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "FmrsService." + action) + .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/" + region + "/datasync/aws4_request") + .body(body) + .when().post("/").then(); + } + + private static void invalid(ValidatableResponse response) { + response.statusCode(400).body("__type", equalTo("InvalidRequestException")); + } +}