feat: durable refresh runs from a source to a staging target (auth M6c)
A refresh captures a new dump of the relation's source, then restores exactly that snapshot onto the relation's target through the existing restore path. - refresh_runs in SQLite, one active run per target, per-requester idempotency keys, a cap on concurrent runs and a TTL that only applies until the restore starts overwriting - the run and its capture job are created in one transaction; the run follows its own capture job, never the latest snapshot, and pins that snapshot until it ends - the restore is queued and linked in one guarded transaction; every step is safe to repeat after a crash; a background driver only reads the database - authorize_apply re-checks, in the transaction that allows the overwrite, that the relation is still usable, the requester is still active, the authorising grant still exists, the deadline has not passed and the target is not the source; otherwise the restore is cancelled, the run is blocked and the agent gets 409. Administrator restores are untouched - POST/GET/DELETE /api/v1/refresh-runs: confirmation names source, target and version; non-administrators never see the snapshot id, the requester or the database identity - audit refresh.* per run Tests drive the agent through the real Outpost endpoints and cover revocation at every stage; the access matrix gains a RunRow kind. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -127,37 +127,9 @@ impl Db {
|
||||
) -> Result<Option<RestoreJob>> {
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||
let registered: Option<RestoreTarget> = tx.query_row(
|
||||
"SELECT t.profile FROM restore_targets t WHERE t.id = ?1 AND t.application_id = ?2
|
||||
AND EXISTS (SELECT 1 FROM outpost_credentials WHERE outpost_id = t.outpost_id AND state = 'active' AND expires_at > ?3)",
|
||||
params![target.to_string(), application.to_string(), now],
|
||||
|row| json(row, 0),
|
||||
).optional()?;
|
||||
let Some(registered) = registered else {
|
||||
return Ok(None);
|
||||
};
|
||||
if restore_target_mismatch(®istered, artifact).is_some() {
|
||||
return Ok(None);
|
||||
}
|
||||
let id = Uuid::new_v4();
|
||||
tx.execute(
|
||||
"INSERT INTO restore_jobs(id, snapshot_id, target_id, actor, artifact, created_at, updated_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?6)",
|
||||
params![id.to_string(), snapshot.to_string(), target.to_string(), actor,
|
||||
serde_json::to_string(artifact)?, now],
|
||||
)?;
|
||||
audit(
|
||||
&tx,
|
||||
"restore.queued",
|
||||
&format!("actor={actor} job={id} snapshot={snapshot} target={target}"),
|
||||
)?;
|
||||
let job = tx.query_row(
|
||||
&format!("{VIEW} WHERE j.id = ?1"),
|
||||
[id.to_string()],
|
||||
read_job,
|
||||
)?;
|
||||
let job = enqueue_restore_in(&tx, snapshot, application, target, actor, artifact, now)?;
|
||||
tx.commit()?;
|
||||
Ok(Some(job))
|
||||
Ok(job)
|
||||
}
|
||||
|
||||
pub fn restore_job(&self, id: Uuid, now: i64) -> Result<Option<RestoreJob>> {
|
||||
@@ -195,7 +167,10 @@ impl Db {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
Ok(conn.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM restore_jobs WHERE (snapshot_id = ?1 OR safety_id = ?1 OR attempt_safety_id = ?1)
|
||||
AND status IN ('queued','running','applying','recovery_required'))",
|
||||
AND status IN ('queued','running','applying','recovery_required'))
|
||||
-- a refresh run pins its own snapshot from the moment it starts: the snapshot id is its capture job id
|
||||
OR EXISTS(SELECT 1 FROM refresh_runs WHERE capture_job_id = ?1
|
||||
AND status IN ('capturing','restoring','recovery_required'))",
|
||||
[id.to_string()], |row| row.get(0),
|
||||
)?)
|
||||
}
|
||||
@@ -302,6 +277,18 @@ impl Db {
|
||||
if job.status != "running" || job.phase != "ready" || job.safety_snapshot_id.is_none() {
|
||||
return Ok(false);
|
||||
}
|
||||
// A refresh is only allowed to overwrite its target while what authorised it still holds. The check
|
||||
// and the flip to `applying` are one transaction, and the agent applies nothing without this answer.
|
||||
// Restores an administrator queued directly have no run and behave exactly as before.
|
||||
if let Some(run) = super::refresh::run_for_restore(&tx, &id.to_string())? {
|
||||
if let Some(reason) = super::refresh::invalid_reason(&tx, &run, now)? {
|
||||
super::refresh::block(&tx, &run, reason, now)?;
|
||||
tx.execute("UPDATE restore_jobs SET status='cancelled', phase='cancelled', updated_at=?2 WHERE id=?1 AND status='running'", params![id.to_string(), now])?;
|
||||
audit(&tx, "restore.apply_refused", &format!("job={id} run={} reason={reason}", run.id))?;
|
||||
tx.commit()?;
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
tx.execute("UPDATE restore_jobs SET status = 'applying', phase = 'applying', updated_at = ?2, lease_until = ?3 WHERE id = ?1",
|
||||
params![id.to_string(), now, now + CAPTURE_LEASE_SECONDS])?;
|
||||
audit(
|
||||
@@ -388,6 +375,48 @@ impl Db {
|
||||
}
|
||||
}
|
||||
|
||||
/// Queue a restore inside a transaction the caller owns, so a refresh run can link it atomically.
|
||||
pub(super) fn enqueue_restore_in(
|
||||
tx: &Transaction<'_>,
|
||||
snapshot: Uuid,
|
||||
application: Uuid,
|
||||
target: Uuid,
|
||||
actor: &str,
|
||||
artifact: &RestoreArtifact,
|
||||
now: i64,
|
||||
) -> Result<Option<RestoreJob>> {
|
||||
let registered: Option<RestoreTarget> = tx.query_row(
|
||||
"SELECT t.profile FROM restore_targets t WHERE t.id = ?1 AND t.application_id = ?2
|
||||
AND EXISTS (SELECT 1 FROM outpost_credentials WHERE outpost_id = t.outpost_id AND state = 'active' AND expires_at > ?3)",
|
||||
params![target.to_string(), application.to_string(), now],
|
||||
|row| json(row, 0),
|
||||
).optional()?;
|
||||
let Some(registered) = registered else {
|
||||
return Ok(None);
|
||||
};
|
||||
if restore_target_mismatch(®istered, artifact).is_some() {
|
||||
return Ok(None);
|
||||
}
|
||||
let id = Uuid::new_v4();
|
||||
tx.execute(
|
||||
"INSERT INTO restore_jobs(id, snapshot_id, target_id, actor, artifact, created_at, updated_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?6)",
|
||||
params![id.to_string(), snapshot.to_string(), target.to_string(), actor,
|
||||
serde_json::to_string(artifact)?, now],
|
||||
)?;
|
||||
audit(
|
||||
&tx,
|
||||
"restore.queued",
|
||||
&format!("actor={actor} job={id} snapshot={snapshot} target={target}"),
|
||||
)?;
|
||||
let job = tx.query_row(
|
||||
&format!("{VIEW} WHERE j.id = ?1"),
|
||||
[id.to_string()],
|
||||
read_job,
|
||||
)?;
|
||||
Ok(Some(job))
|
||||
}
|
||||
|
||||
fn restore_access(
|
||||
conn: &Connection,
|
||||
outpost: Uuid,
|
||||
|
||||
Reference in New Issue
Block a user