mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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.
This commit is contained in:
1 parent
be4803d3d8
commit
e3ca9a6064
2 files changed
+111
-9
No files matched your search
@@ -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<DataSyncAgent> listAgents(String nextToken, int maxResults) {
|
||||
List<DataSyncAgent> all = agents.scan(key -> true);
|
||||
List<DataSyncAgent> 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<DataSyncLocation> listLocations(JsonNode filters, String nextToken, int maxResults) {
|
||||
List<DataSyncLocation> 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<DataSyncTask> listTasks(JsonNode filters, String nextToken, int maxResults) {
|
||||
List<DataSyncTask> 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).<DataSyncTaggable>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));
|
||||
}
|
||||
|
||||
+98
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user