fix(backup): keep backup plans, selections, jobs and recovery points in their Region (#9490)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-11-25 22:21:38 -04:00
1 parent f046df07aa
commit 952667294d
4 files changed
+129 -14

No files matched your search

@@ -662,6 +662,7 @@ public class BackupService {
public BackupPlan getBackupPlan(String planId) {
return planStore.get(planId)
.filter(plan -> plan.getDeletionDate() == null)
.filter(plan -> regionResolver.inRequestRegion(plan.getBackupPlanArn()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup plan not found: " + planId, 400));
}
@@ -699,6 +700,7 @@ public class BackupService {
public List<BackupPlan> listBackupPlans() {
return planStore.scan(k -> true).stream()
.filter(plan -> plan.getDeletionDate() == null)
.filter(plan -> regionResolver.inRequestRegion(plan.getBackupPlanArn()))
.toList();
}
@@ -736,6 +738,7 @@ public class BackupService {
public BackupSelection getBackupSelection(String planId, String selectionId) {
BackupSelection sel = selectionStore.get(selectionId)
.filter(selection -> planInRequestRegion(selection.getBackupPlanId()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup selection not found: " + selectionId, 400));
if (!planId.equals(sel.getBackupPlanId())) {
throw new AwsException("ResourceNotFoundException", "Backup selection not found in plan: " + planId, 400);
@@ -749,11 +752,24 @@ public class BackupService {
}
public List<BackupSelection> listBackupSelections(String planId) {
if (!planInRequestRegion(planId)) {
return List.of();
}
return selectionStore.scan(k -> true).stream()
.filter(s -> planId.equals(s.getBackupPlanId()))
.toList();
}
/**
* Whether plan {@code planId}, deleted or not, belongs to the request's Region. A selection carries only its plan's
* id, so it is in the Region its plan's ARN names.
*/
private boolean planInRequestRegion(String planId) {
return planStore.get(planId)
.filter(plan -> regionResolver.inRequestRegion(plan.getBackupPlanArn()))
.isPresent();
}
// ── Job ────────────────────────────────────────────────────────────────────
public BackupJob startBackupJob(String vaultName, String resourceArn, String iamRoleArn,
@@ -787,6 +803,7 @@ public class BackupService {
public BackupJob describeBackupJob(String jobId) {
return jobStore.get(jobId)
.filter(job -> regionResolver.inRequestRegion(job.getBackupVaultArn()))
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup job not found: " + jobId, 400));
}
@@ -810,6 +827,7 @@ public class BackupService {
public List<BackupJob> listBackupJobs(String byVaultName, String byState,
String byResourceArn, String byResourceType) {
return jobStore.scan(k -> true).stream()
.filter(j -> regionResolver.inRequestRegion(j.getBackupVaultArn()))
.filter(j -> byVaultName == null || byVaultName.equals(j.getBackupVaultName()))
.filter(j -> byState == null || byState.equals(j.getState()))
.filter(j -> byResourceArn == null || byResourceArn.equals(j.getResourceArn()))
@@ -822,6 +840,7 @@ public class BackupService {
Integer maxResults, String nextToken) {
List<BackupJob> jobs = listBackupJobs(byVaultName, byState, byResourceArn, byResourceType);
String namespace = "backup:ListBackupJobs:" + regionResolver.getAccountId() + ":"
+ regionResolver.getRegion() + ":"
+ (byVaultName == null ? "all" : byVaultName) + ":" + (byState == null ? "all" : byState)
+ ":" + (byResourceArn == null ? "all" : byResourceArn) + ":" + (byResourceType == null ? "all" : byResourceType);
return paginate(jobs, BackupJob::getBackupJobId, maxResults, nextToken, namespace);
@@ -833,6 +852,7 @@ public class BackupService {
describeBackupVault(vaultName, region);
return recoveryStore.get(recoveryPointArn)
.filter(rp -> vaultName.equals(rp.getBackupVaultName()))
.filter(rp -> region.equals(regionResolver.regionOf(rp.getRecoveryPointArn())))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Recovery point not found: " + recoveryPointArn, 400));
}
@@ -841,6 +861,7 @@ public class BackupService {
describeBackupVault(vaultName, region);
return recoveryStore.scan(k -> true).stream()
.filter(rp -> vaultName.equals(rp.getBackupVaultName()))
.filter(rp -> region.equals(regionResolver.regionOf(rp.getRecoveryPointArn())))
.toList();
}
@@ -1043,9 +1064,7 @@ public class BackupService {
}
private Map<String, String> findTagsByArn(String arn) {
Optional<BackupVault> vault = vaultStore.scan(k -> true).stream()
.filter(v -> arn.equals(v.getBackupVaultArn()))
.findFirst();
Optional<BackupVault> vault = vaultByArn(arn);
if (vault.isPresent()) {
return vault.get().getTags();
}
@@ -1055,19 +1074,28 @@ public class BackupService {
}
}
/** The vault {@code arn} names, when it is of the request's Region. */
private Optional<BackupVault> vaultByArn(String arn) {
if (!regionResolver.inRequestRegion(arn)) {
return Optional.empty();
}
return vaultStore.scan(k -> true).stream()
.filter(v -> arn.equals(v.getBackupVaultArn()))
.findFirst();
}
private BackupPlan findPlanByArn(String arn) {
return AwsArnUtils.resourceIfArnFor(arn, "backup")
.filter(resource -> resource.startsWith("backup-plan:"))
.map(resource -> resource.substring("backup-plan:".length()))
.flatMap(planStore::get)
.filter(plan -> arn.equals(plan.getBackupPlanArn()) && plan.getDeletionDate() == null)
.filter(plan -> arn.equals(plan.getBackupPlanArn()) && plan.getDeletionDate() == null
&& regionResolver.inRequestRegion(arn))
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400));
}
private void applyTags(String arn, Map<String, String> newTags) {
Optional<BackupVault> vaultOpt = vaultStore.scan(k -> true).stream()
.filter(v -> arn.equals(v.getBackupVaultArn()))
.findFirst();
Optional<BackupVault> vaultOpt = vaultByArn(arn);
if (vaultOpt.isPresent()) {
BackupVault vault = vaultOpt.get();
vault.getTags().putAll(newTags);
@@ -1083,9 +1111,7 @@ public class BackupService {
}
private void removeTags(String arn, List<String> tagKeys) {
Optional<BackupVault> vaultOpt = vaultStore.scan(k -> true).stream()
.filter(v -> arn.equals(v.getBackupVaultArn()))
.findFirst();
Optional<BackupVault> vaultOpt = vaultByArn(arn);
if (vaultOpt.isPresent()) {
BackupVault vault = vaultOpt.get();
tagKeys.forEach(vault.getTags()::remove);
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.services.backup.model.BackupJob;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.JsonPath;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import jakarta.inject.Inject;
import org.junit.jupiter.api.MethodOrderer;
@@ -39,6 +40,7 @@ class BackupIntegrationTest {
@Inject ObjectMapper mapper;
private static final String AUTH = "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/backup/aws4_request";
private static final String EU_AUTH = "AWS4-HMAC-SHA256 Credential=test/20260205/eu-west-1/backup/aws4_request";
private static final String VAULT_NAME = "test-vault";
private static final String IAM_ROLE = "arn:aws:iam::000000000000:role/backup-role";
private static final String RESOURCE_ARN = "arn:aws:dynamodb:us-east-1:000000000000:table/my-table";
@@ -2040,4 +2042,91 @@ class BackupIntegrationTest {
.body("BackupPlan.Rules[1].TargetLogicallyAirGappedBackupVaultArn", equalTo(airGapped + "-2"));
given().header("Authorization", AUTH).when().delete("/backup/plans/" + id);
}
@Test
void resourcesOfOneRegionAreInvisibleFromAnother() {
String vault = "region-scope-" + System.nanoTime();
String vaultArn = given().header("Authorization", AUTH).contentType("application/json").body("{}")
.when().put("/backup-vaults/" + vault).then().statusCode(200)
.extract().path("BackupVaultArn");
JsonPath plan = given().header("Authorization", AUTH).contentType("application/json")
.body("{\"BackupPlan\": {\"BackupPlanName\": \"%s\", \"Rules\": [{\"RuleName\": \"daily\", "
.formatted(vault) + "\"TargetBackupVaultName\": \"" + vault + "\"}]}}")
.when().put("/backup/plans/").then().statusCode(200).extract().jsonPath();
String regionPlanId = plan.getString("BackupPlanId");
String planArn = plan.getString("BackupPlanArn");
String regionSelectionId = given().header("Authorization", AUTH).contentType("application/json")
.body("{\"BackupSelection\": {\"SelectionName\": \"sel\", \"IamRoleArn\": \"" + IAM_ROLE
+ "\", \"Resources\": [\"" + RESOURCE_ARN + "\"]}}")
.when().put("/backup/plans/" + regionPlanId + "/selections/").then().statusCode(200)
.extract().path("SelectionId");
String regionJobId = given().header("Authorization", AUTH).contentType("application/json")
.body("{\"BackupVaultName\": \"%s\", \"ResourceArn\": \"%s\", \"IamRoleArn\": \"%s\"}"
.formatted(vault, RESOURCE_ARN, IAM_ROLE))
.when().put("/backup-jobs").then().statusCode(200).extract().path("BackupJobId");
awaitJobCompleted(regionJobId);
String point = given().header("Authorization", AUTH).when().get("/backup-jobs/" + regionJobId)
.then().statusCode(200).extract().path("RecoveryPointArn");
given().header("Authorization", EU_AUTH).contentType("application/json").body("{}")
.when().put("/backup-vaults/" + vault).then().statusCode(200);
assertNotFound(given().header("Authorization", EU_AUTH)
.when().get("/backup/plans/" + regionPlanId + "/").then());
assertNotFound(given().header("Authorization", EU_AUTH).contentType("application/json")
.body("{\"BackupPlan\": {\"BackupPlanName\": \"stolen\", \"Rules\": []}}")
.when().post("/backup/plans/" + regionPlanId).then());
assertNotFound(given().header("Authorization", EU_AUTH)
.when().delete("/backup/plans/" + regionPlanId).then());
given().header("Authorization", EU_AUTH).when().get("/backup/plans/")
.then().statusCode(200).body("BackupPlansList.BackupPlanId", not(hasItem(regionPlanId)));
assertNotFound(given().header("Authorization", EU_AUTH)
.when().get("/backup/plans/" + regionPlanId + "/selections/" + regionSelectionId).then());
assertNotFound(given().header("Authorization", EU_AUTH)
.when().delete("/backup/plans/" + regionPlanId + "/selections/" + regionSelectionId).then());
given().header("Authorization", EU_AUTH).when().get("/backup/plans/" + regionPlanId + "/selections/")
.then().statusCode(200).body("BackupSelectionsList", hasSize(0));
assertNotFound(given().header("Authorization", EU_AUTH)
.when().get("/backup-jobs/" + regionJobId).then());
assertNotFound(given().header("Authorization", EU_AUTH).contentType("application/json").body("{}")
.when().post("/backup-jobs/" + regionJobId).then());
given().header("Authorization", EU_AUTH).when().get("/backup-jobs/")
.then().statusCode(200).body("BackupJobs.BackupJobId", not(hasItem(regionJobId)));
given().header("Authorization", EU_AUTH).queryParam("byBackupVaultName", vault)
.when().get("/backup-jobs/").then().statusCode(200).body("BackupJobs", hasSize(0));
given().header("Authorization", EU_AUTH).when().get("/backup-vaults/" + vault + "/recovery-points/")
.then().statusCode(200).body("RecoveryPoints", hasSize(0));
assertNotFound(given().header("Authorization", EU_AUTH)
.when().get("/backup-vaults/" + vault + "/recovery-points/" + point).then());
for (String arn : List.of(vaultArn, planArn)) {
assertNotFound(given().header("Authorization", EU_AUTH).when().get("/tags/" + arn).then());
assertNotFound(given().header("Authorization", EU_AUTH).contentType("application/json")
.body("{\"Tags\":{\"team\":\"eu\"}}").when().post("/tags/" + arn).then());
assertNotFound(given().header("Authorization", EU_AUTH).contentType("application/json")
.body("{\"TagKeyList\":[\"team\"]}").when().post("/untag/" + arn).then());
}
given().header("Authorization", AUTH).when().get("/backup/plans/" + regionPlanId + "/")
.then().statusCode(200).body("BackupPlan.BackupPlanName", equalTo(vault));
given().header("Authorization", AUTH).when().get("/backup/plans/" + regionPlanId + "/selections/")
.then().statusCode(200).body("BackupSelectionsList.SelectionId", hasItem(regionSelectionId));
given().header("Authorization", AUTH).queryParam("byBackupVaultName", vault)
.when().get("/backup-jobs/").then().statusCode(200).body("BackupJobs", hasSize(1));
given().header("Authorization", AUTH).when().get("/backup-vaults/" + vault + "/recovery-points/")
.then().statusCode(200).body("RecoveryPoints", hasSize(1));
given().header("Authorization", AUTH).contentType("application/json").body("{\"Tags\":{\"team\":\"us\"}}")
.when().post("/tags/" + vaultArn).then().statusCode(204);
given().header("Authorization", AUTH)
.when().delete("/backup-vaults/" + vault + "/recovery-points/" + point).then().statusCode(204);
given().header("Authorization", AUTH)
.when().delete("/backup/plans/" + regionPlanId + "/selections/" + regionSelectionId)
.then().statusCode(204);
given().header("Authorization", AUTH).when().delete("/backup/plans/" + regionPlanId).then().statusCode(200);
given().header("Authorization", AUTH).when().delete("/backup-vaults/" + vault).then().statusCode(204);
given().header("Authorization", EU_AUTH).when().delete("/backup-vaults/" + vault).then().statusCode(204);
}
private static void assertNotFound(ValidatableResponse response) {
response.statusCode(400).body("__type", equalTo("ResourceNotFoundException"));
}
}
@@ -53,7 +53,7 @@ class BackupJobRecoveryIntegrationTest {
try {
restarted.resumeJobs();
for (String account : accounts) {
await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> RequestScopes.runAs(account, () -> {
await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> RequestScopes.runAs(account, "eu-west-1", () -> {
assertEquals("COMPLETED", restarted.listBackupJobs(vaultName, null, null, null).getFirst().getState());
assertEquals(1, restarted.listRecoveryPointsByBackupVault(vaultName, "eu-west-1").size());
String arn = restarted.listRecoveryPointsByBackupVault(vaultName, "eu-west-1")
@@ -67,7 +67,7 @@ class BackupJobRecoveryIntegrationTest {
} finally {
restarted.shutdown();
for (String account : accounts) {
RequestScopes.runAs(account, () -> {
RequestScopes.runAs(account, "eu-west-1", () -> {
restarted.listRecoveryPointsByBackupVault(vaultName, "eu-west-1").forEach(point ->
restarted.deleteRecoveryPoint(vaultName, point.getRecoveryPointArn(), "eu-west-1"));
restarted.listBackupJobs(vaultName, null, null, null).forEach(job -> jobs.delete(job.getBackupJobId()));
@@ -482,7 +482,7 @@ class BackupJobRecoveryTest {
ServiceConfigAccess access = mock(ServiceConfigAccess.class);
when(access.storageMode("backup")).thenReturn("persistent");
StorageFactory firstStorage = new StorageFactory(config, access);
RegionResolver resolver = new RegionResolver("us-east-1", ACCOUNT);
RegionResolver resolver = new RegionResolver(REGION, ACCOUNT);
ObjectMapper mapper = new ObjectMapper();
SnsService sns = mock(SnsService.class);
BackupService first = new BackupService(firstStorage, config, resolver, mapper, sns);
@@ -577,7 +577,7 @@ class BackupJobRecoveryTest {
private final Map<String, AccountAwareStorageBackend<?>> stores = new HashMap<>();
private final StorageFactory storage = mock(StorageFactory.class);
private final EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
private final RegionResolver resolver = new RegionResolver("us-east-1", ACCOUNT);
private final RegionResolver resolver = new RegionResolver(REGION, ACCOUNT);
private final ObjectMapper mapper = new ObjectMapper();
private final SnsService sns = mock(SnsService.class);
private final AccountAwareStorageBackend<BackupVault> vaults;