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:
@@ -43,6 +43,8 @@ struct World {
|
||||
state: HubState,
|
||||
a: Fixture,
|
||||
b: Fixture,
|
||||
/// A refresh run on orbit A's relation, started by an administrator.
|
||||
run: String,
|
||||
_dir: tempfile::TempDir,
|
||||
}
|
||||
|
||||
@@ -113,8 +115,14 @@ async fn world_with(exports: Option<crate::exports::ExportGate>) -> World {
|
||||
state.sessions = SessionGate::without_operator(state.db.clone()).with_oidc("matrix-fp");
|
||||
let a = fixture(&state, "orbit-a").await;
|
||||
let b = fixture(&state, "orbit-b").await;
|
||||
let confirmation = crate::db::refresh::confirmation_text("orbit-a", "orbit-a-staging", 1);
|
||||
let (run, _) = state
|
||||
.db
|
||||
.create_refresh_run(&a.relation.to_string(), &confirmation, "seed", None, "admin", "seed-run", 3600, 50, now())
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let app = crate::api::router(state.clone());
|
||||
World { app, state, a, b, _dir: dir }
|
||||
World { app, state, a, b, run: run.id, _dir: dir }
|
||||
}
|
||||
|
||||
impl World {
|
||||
@@ -173,6 +181,8 @@ fn anonymous() -> Caller {
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
enum Kind {
|
||||
SelfOnly,
|
||||
/// `/refresh-runs`: starting, listing, watching and cancelling a refresh; per-method expectations.
|
||||
RunRow,
|
||||
/// Visible only to people holding a grant on a relation (`refresh.run`).
|
||||
RelationList,
|
||||
AdminOnly,
|
||||
@@ -196,6 +206,8 @@ fn rows() -> Vec<Row> {
|
||||
}
|
||||
let kind = if f[4] == "session.self" {
|
||||
Kind::SelfOnly
|
||||
} else if f[4] == "refresh.run" && f[1].starts_with("/api/v1/refresh-runs") {
|
||||
Kind::RunRow
|
||||
} else if f[4] == "refresh.run" {
|
||||
Kind::RelationList
|
||||
} else {
|
||||
@@ -225,6 +237,7 @@ fn recipe(w: &World, row: &Row) -> (String, Option<Value>) {
|
||||
"/api/v1/capture-profiles/:id/jobs" | "/api/v1/capture-profiles/:id/restore-targets" => a.application,
|
||||
"/api/v1/capture-jobs/:id" | "/api/v1/capture-jobs/:id/events" => a.job,
|
||||
"/api/v1/wormhole/outposts/:id/rotate" | "/api/v1/wormhole/outposts/:id/revoke" => a.outpost,
|
||||
"/api/v1/refresh-runs/:id" => Uuid::parse_str(&w.run).unwrap(),
|
||||
"/api/v1/orbits/:id/environment" => a.orbit,
|
||||
"/api/v1/restore-targets/:id/environment" => a.target,
|
||||
"/api/v1/relations/:id" | "/api/v1/relations/:id/access" | "/api/v1/relations/:id/access/:grant" => a.relation,
|
||||
@@ -247,6 +260,11 @@ fn recipe(w: &World, row: &Row) -> (String, Option<Value>) {
|
||||
("POST", "/api/v1/access/grants") => Some(json!({"principal_type": "group", "principal": "matrix-group", "role": "viewer"})),
|
||||
("PUT", "/api/v1/access/users/:id") => Some(json!({"active": true})),
|
||||
("PUT", "/api/v1/orbits/:id/environment") | ("PUT", "/api/v1/restore-targets/:id/environment") => Some(json!({"environment": "staging"})),
|
||||
("POST", "/api/v1/refresh-runs") => Some(json!({
|
||||
"relation_id": a.relation,
|
||||
"confirmation": crate::db::refresh::confirmation_text("orbit-a", "orbit-a-staging", 1),
|
||||
"idempotency_key": format!("matrix-{}", Uuid::new_v4()),
|
||||
})),
|
||||
("POST", "/api/v1/relations") => Some(json!({"source_orbit_id": a.orbit, "target_id": a.target})),
|
||||
("PUT", "/api/v1/relations/:id") => Some(json!({"target_id": a.target})),
|
||||
("POST", "/api/v1/relations/:id/access") => Some(json!({"principal_type": "group", "principal": "matrix-refreshers"})),
|
||||
@@ -341,6 +359,25 @@ async fn every_human_route_enforces_the_matrix_for_every_kind_of_caller() {
|
||||
};
|
||||
assert_eq!(status, expected, "{who}");
|
||||
}
|
||||
Kind::RunRow => match (row.method.as_str(), caller.name) {
|
||||
// someone who holds a grant on the relation, and did not start this run
|
||||
("GET", "refresher-a") => {
|
||||
assert_eq!(status, StatusCode::OK, "{who}");
|
||||
if row.template.ends_with(":id") {
|
||||
assert_eq!(body_out["id"], w.run.as_str());
|
||||
} else {
|
||||
assert_eq!(body_out.as_array().unwrap().len(), 1, "{who} sees only runs on its relation");
|
||||
}
|
||||
for secret in ["requested_by", "snapshot_id", "identity", "instance", "outpost"] {
|
||||
assert!(!body_out.to_string().contains(secret), "{who}: a refresher's view leaks {secret}");
|
||||
}
|
||||
}
|
||||
("POST", "refresher-a") => assert!(!denied(status), "{who}: {status}"),
|
||||
("DELETE", "refresher-a") => assert_eq!(status, StatusCode::FORBIDDEN, "{who}: may see the run but not cancel someone else's"),
|
||||
// everyone else: not a holder of any relation grant
|
||||
("GET", _) if !row.template.ends_with(":id") => assert_eq!(status, StatusCode::FORBIDDEN, "{who}"),
|
||||
_ => assert_eq!(status, StatusCode::NOT_FOUND, "{who}: a relation or run they hold no grant on does not exist for them"),
|
||||
},
|
||||
Kind::RelationList => {
|
||||
if caller.name == "refresher-a" {
|
||||
assert_eq!(status, StatusCode::OK, "{who}");
|
||||
@@ -373,7 +410,7 @@ async fn every_human_route_enforces_the_matrix_for_every_kind_of_caller() {
|
||||
Kind::SelfOnly => unreachable!(),
|
||||
}
|
||||
// the invariant that catches a forgotten gate: no grant, no success
|
||||
if no_orbit_access && !(caller.name == "refresher-a" && row.kind == Kind::RelationList) {
|
||||
if no_orbit_access && !(caller.name == "refresher-a" && matches!(row.kind, Kind::RelationList | Kind::RunRow)) {
|
||||
assert!(!status.is_success(), "{who}: a caller without any matching grant got {status}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user