fix(backup): run scheduled job transitions in the caller's account (#4965)

startBackupJob and stopBackupJob schedule transitionJob, completeJob and
abortJob on the backup-job-scheduler executor. That thread has no request
context, so the account-aware job, recovery-point and vault stores, and
regionResolver.buildArn, all resolved the default account 000000000000.

A job started from any other account was therefore never found on the
scheduler thread: it stayed CREATED (or ABORTING) forever, no recovery
point was written, and the vault kept NumberOfRecoveryPoints=0, so
DeleteBackupVault's non-empty guard let the vault be deleted.

Capture the caller's account on the request thread and run each
scheduled body under RequestScopes.runAs, the idiom EcsService,
EksService and DynamoDbService already use for background work.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-02 19:53:01 -04:00
1 parent 0bae2f0e35
commit e25f488bf3
2 files changed
+88 -6

No files matched your search

@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestScopes;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.backup.model.*;
@@ -598,11 +599,16 @@ public class BackupService {
job.setCreationDate(now);
job.setExpectedCompletionDate(now + jobCompletionDelaySeconds);
job.setStartBy(now + 3600L);
job.setAccountId(regionResolver.getAccountId());
String accountId = regionResolver.getAccountId();
job.setAccountId(accountId);
jobStore.put(jobId, job);
scheduler.schedule(() -> transitionJob(jobId, vaultName, region), 1, TimeUnit.SECONDS);
scheduler.schedule(() -> completeJob(jobId, vaultName, region), jobCompletionDelaySeconds, TimeUnit.SECONDS);
// The scheduler thread has no request context; run in the caller's account
// so the account-aware stores and buildArn resolve the right partition.
scheduler.schedule(() -> RequestScopes.runAs(accountId,
() -> transitionJob(jobId, vaultName, region)), 1, TimeUnit.SECONDS);
scheduler.schedule(() -> RequestScopes.runAs(accountId,
() -> completeJob(jobId, vaultName, region)), jobCompletionDelaySeconds, TimeUnit.SECONDS);
return job;
}
@@ -622,7 +628,8 @@ public class BackupService {
job.setState("ABORTING");
job.setStatusMessage("Job stop requested");
jobStore.put(jobId, job);
scheduler.schedule(() -> abortJob(jobId), 1, TimeUnit.SECONDS);
String accountId = regionResolver.getAccountId();
scheduler.schedule(() -> RequestScopes.runAs(accountId, () -> abortJob(jobId)), 1, TimeUnit.SECONDS);
}
public List<BackupJob> listBackupJobs(String byVaultName, String byState,
@@ -40,17 +40,21 @@ class BackupIntegrationTest {
* nothing to do with the behaviour under test.
*/
private static void awaitJobCompleted(String backupJobId) {
awaitJobState(AUTH, backupJobId, "COMPLETED");
}
private static void awaitJobState(String auth, String backupJobId, String expected) {
await().atMost(Duration.ofSeconds(10))
.pollInterval(Duration.ofMillis(25))
.until(() -> {
String state = given()
.header("Authorization", AUTH)
.header("Authorization", auth)
.when()
.get("/backup-jobs/" + backupJobId)
.then()
.statusCode(200)
.extract().path("State");
return "COMPLETED".equals(state);
return expected.equals(state);
});
}
@@ -1346,4 +1350,75 @@ class BackupIntegrationTest {
.then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));
}
// ── Non-default account ────────────────────────────────────────────────────
// Job transitions run on a scheduler thread with no request context. Without the
// caller's account bound there, the account-aware stores resolve 000000000000, so a
// job started from any other account is never found: it stays CREATED, writes no
// recovery point, and the vault's empty-guard lets a vault that should hold one be
// deleted.
private static final String OTHER_ACCOUNT_AUTH =
"AWS4-HMAC-SHA256 Credential=210987654321/20260205/us-east-1/backup/aws4_request";
@Test
@Order(160)
void aBackupJobInANonDefaultAccountCompletesInThatAccount() {
String vault = "other-account-vault";
given().header("Authorization", OTHER_ACCOUNT_AUTH).contentType("application/json").body("{}")
.when().put("/backup-vaults/" + vault).then().statusCode(200);
String jid = given().header("Authorization", OTHER_ACCOUNT_AUTH).contentType("application/json")
.body("""
{
"BackupVaultName": "%s",
"ResourceArn": "arn:aws:dynamodb:us-east-1:210987654321:table/my-table",
"IamRoleArn": "arn:aws:iam::210987654321:role/backup-role"
}
""".formatted(vault))
.when().put("/backup-jobs").then().statusCode(200).extract().path("BackupJobId");
awaitJobState(OTHER_ACCOUNT_AUTH, jid, "COMPLETED");
String rpArn = given().header("Authorization", OTHER_ACCOUNT_AUTH)
.when().get("/backup-jobs/" + jid).then().statusCode(200)
.extract().path("RecoveryPointArn");
given().header("Authorization", OTHER_ACCOUNT_AUTH)
.when().get("/backup-vaults/" + vault + "/recovery-points/")
.then().statusCode(200)
.body("RecoveryPoints", hasSize(1))
.body("RecoveryPoints[0].RecoveryPointArn", equalTo(rpArn))
.body("RecoveryPoints[0].RecoveryPointArn", containsString(":210987654321:"));
given().header("Authorization", OTHER_ACCOUNT_AUTH)
.when().get("/backup-vaults/" + vault)
.then().statusCode(200).body("NumberOfRecoveryPoints", equalTo(1));
given().header("Authorization", OTHER_ACCOUNT_AUTH)
.when().delete("/backup-vaults/" + vault)
.then().statusCode(400).body("__type", equalTo("InvalidRequestException"));
}
@Test
@Order(161)
void aStoppedBackupJobInANonDefaultAccountReachesAborted() {
String vault = "other-account-abort-vault";
given().header("Authorization", OTHER_ACCOUNT_AUTH).contentType("application/json").body("{}")
.when().put("/backup-vaults/" + vault).then().statusCode(200);
String jid = given().header("Authorization", OTHER_ACCOUNT_AUTH).contentType("application/json")
.body("""
{
"BackupVaultName": "%s",
"ResourceArn": "arn:aws:dynamodb:us-east-1:210987654321:table/my-table",
"IamRoleArn": "arn:aws:iam::210987654321:role/backup-role"
}
""".formatted(vault))
.when().put("/backup-jobs").then().statusCode(200).extract().path("BackupJobId");
given().header("Authorization", OTHER_ACCOUNT_AUTH).contentType("application/json").body("{}")
.when().post("/backup-jobs/" + jid).then().statusCode(204);
awaitJobState(OTHER_ACCOUNT_AUTH, jid, "ABORTED");
}
}