From b3a22d464b5f2d1cc2374f9d52444a66ef569333 Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Fri, 13 Nov 2026 19:49:12 +0100 Subject: [PATCH] fix: bind reboot success to independently received inventory within reserved deadline --- Rust_RMM_Implementierungsspezifikation.md | 22 +- crates/server/src/job_gateway.rs | 11 +- crates/server/src/synchronization.rs | 517 ++++++++++++++++++++- deploy/browser/cloud_reboot_acceptance.cjs | 94 ++++ deploy/cloud_reboot_acceptance.py | 273 +++++++++++ docs/handover.md | 13 + docs/mvp-final-audit.md | 9 +- docs/project-decisions.md | 18 +- docs/reboot-acceptance.md | 118 +++++ 9 files changed, 1048 insertions(+), 27 deletions(-) create mode 100644 deploy/browser/cloud_reboot_acceptance.cjs create mode 100644 deploy/cloud_reboot_acceptance.py create mode 100644 docs/reboot-acceptance.md diff --git a/Rust_RMM_Implementierungsspezifikation.md b/Rust_RMM_Implementierungsspezifikation.md index ce8f183..0911cca 100644 --- a/Rust_RMM_Implementierungsspezifikation.md +++ b/Rust_RMM_Implementierungsspezifikation.md @@ -1,6 +1,6 @@ # Rust RMM: Architektur und Implementierungsspezifikation -Version 1.12 · 2026-11-13 · Sprache: Deutsch +Version 1.13 · 2026-11-13 · Sprache: Deutsch **Zweck:** Vollständige, eigenständig nutzbare Grundlage für die Implementierung einer mandantenfähigen Plattform zur Inventarisierung, Überwachung und Fernadministration von Kundengeräten durch eine Coding-KI und ein Entwicklungsteam. @@ -43,6 +43,26 @@ Version 1.12 · 2026-11-13 · Sprache: Deutsch ## 0 Auftrag und verbindliche Produktanforderungen +**Präzisierung Version1.13, begrenzte MVP-Nachbedingungen:** Ein bereits vor +einer Paketwirkung vorhandener Host-Neustartbedarf verhindert deren Erfolg +nicht, wenn sämtliche Paketnachbedingungen unabhängig vollständig nachgewiesen +sind. `no_reboot=true` verbietet den eigenmächtigen Neustart; das tatsächliche +Hostflag wird unverfälscht als `reboot_required` berichtet. Für die harmlosen +APT-Fixturerezepte muss derselbe gebundene Hostflagwert vor/nach der Wirkung +nachgewiesen sein; der private Sandboxwert ersetzt diese Hostbeobachtung nicht. +Fehlende historische Vorhermessungen werden nicht rückwirkend erfunden. + +Beim festen Rebootjob beginnt die zentrale `reconnect_timeout_seconds`-Frist +konservativ an der dauerhaft gespeicherten serverseitigen Startreservierung +des gleichen Jobs/Versuchs/Grants. Ein späterer lokaler Start verlängert sie +nicht. Erstmaliger Erfolg braucht authentisierten Wiederkontakt mit neuer +Boot-ID sowie unabhängig gespeichertes Inventar, dessen Serverempfang nach +Reservierung und spätestens zur Frist liegt. Fehlender/verspäteter Nachweis +bleibt `unknown`. Späterer signierter Abgleich darf den ursprünglichen +rechtzeitigen Inventarbeleg erneut prüfen, aber keinen anderen verspäteten +Beleg unterschieben. Dies begrenzt die vorhandene Rebootabnahme und erlaubt +keinen zusätzlichen Reboot oder automatischen Wiederholungsversuch. + **Topologieoverride des Users vom 2026-11-13 (Version 1.6):** Der Entwickler-PC wird nicht mehr als RMM-Server genutzt. Der dedizierte Internetserver übernimmt Backend, Datenbank und TLS-Endpunkt; Agenten verbinden sich direkt ausgehend diff --git a/crates/server/src/job_gateway.rs b/crates/server/src/job_gateway.rs index 17a4cf8..4ed5f10 100644 --- a/crates/server/src/job_gateway.rs +++ b/crates/server/src/job_gateway.rs @@ -189,13 +189,10 @@ pub async fn event( } } if matches!(payload.action, Action::Reboot { .. }) { - ensure!( - evidence.get("new_boot_id").and_then(Value::as_str) - == Some(e.boot_id.as_str()), - "new boot evidence required" - ); - let old_boot=sqlx::query_scalar::<_,String>("SELECT data->>'boot_id' FROM audit_events WHERE event_type='start_reserved' AND data->>'job_id'=$1 ORDER BY created_at DESC LIMIT 1").bind(&r.job_id).fetch_one(&mut **tx).await?; - ensure!(old_boot != e.boot_id, "reboot needs new boot"); + crate::synchronization::verify_reboot_success( + tx, tenant, agent, e, &payload, r, &evidence, + ) + .await?; } } sqlx::query("UPDATE jobs SET state=$2,revision=$3,result=$4 WHERE job_id=$1") diff --git a/crates/server/src/synchronization.rs b/crates/server/src/synchronization.rs index 3cecdb3..bfed78f 100644 --- a/crates/server/src/synchronization.rs +++ b/crates/server/src/synchronization.rs @@ -67,6 +67,115 @@ fn previous_anchor( last_applied } } +/// The server's durable grant reservation precedes the helper's saved start. +/// Using that trusted timestamp is conservative: no device clock or late +/// reconnect may extend the fixed, approved reconnect window. +fn reboot_deadline( + action: &Action, + reserved_at: Option>, +) -> Option> { + let Action::Reboot { + reconnect_timeout_seconds, + .. + } = action + else { + return None; + }; + if !action.validate() { + return None; + } + reserved_at?.checked_add_signed(Duration::seconds(i64::from(*reconnect_timeout_seconds))) +} +struct RebootWindow { + reserved_at: chrono::DateTime, + deadline: chrono::DateTime, + now: chrono::DateTime, + require_fresh: bool, +} +async fn reboot_inventory( + tx: &mut Transaction<'_, Postgres>, + tenant: Uuid, + agent: Uuid, + boot: Uuid, + window: &RebootWindow, + existing_receipt: Option, +) -> Result)>> { + // Already-persisted definitive success keeps its original receipt. It need + // not be younger than five minutes again, but its original server receipt + // time must still prove the approved reconnect window and current boot. + Ok(sqlx::query_as("SELECT message_id,received_at FROM observations WHERE tenant_id=$1 AND agent_id=$2 AND boot_id=$3 AND received_at>$4 AND received_at<=$5 AND (NOT $9::boolean OR received_at>$6) AND received_at<=$7 AND ($8::uuid IS NULL OR message_id=$8) ORDER BY received_at DESC LIMIT 1") + .bind(tenant).bind(agent).bind(boot).bind(window.reserved_at).bind(window.deadline).bind(window.now-Duration::minutes(5)).bind(window.now).bind(existing_receipt).bind(window.require_fresh) + .fetch_optional(&mut **tx).await?) +} +/// First ingress of success needs the same independent, timely receipt as an +/// authoritative Sync decision. Claimed deadlines/boot IDs alone grant nothing. +pub(crate) async fn verify_reboot_success( + tx: &mut Transaction<'_, Postgres>, + tenant: Uuid, + agent: Uuid, + envelope: &Envelope, + job: &JobPayload, + result: &JobResult, + evidence: &Value, +) -> Result<()> { + let boot = Uuid::parse_str(&envelope.boot_id)?; + let grant = Uuid::parse_str(&result.grant_id)?; + ensure!( + job.tenant_id == tenant + && job.agent_id == agent + && job.job_id.to_string() == result.job_id + && job.attempt_id.to_string() == result.attempt_id + && evidence["postconditions_passed"] == true + && evidence["reconciled_after_reboot"] == true + && evidence["new_boot_id"] == envelope.boot_id, + "REBOOT_RESULT_BINDING" + ); + let reserved:chrono::DateTime=sqlx::query_scalar("SELECT created_at FROM execution_permits WHERE tenant_id=$1 AND grant_id=$2 AND job_id=$3 AND attempt_id=$4") + .bind(tenant).bind(grant).bind(job.job_id).bind(job.attempt_id).fetch_optional(&mut **tx).await?.context("REBOOT_RESERVED_GRANT_REQUIRED")?; + let deadline = + reboot_deadline(&job.action, Some(reserved)).context("REBOOT_FIXED_DEADLINE_REQUIRED")?; + let original:Option=sqlx::query_scalar("SELECT data->>'boot_id' FROM audit_events WHERE tenant_id=$1 AND event_type='start_reserved' AND data->>'job_id'=$2 AND data->>'grant_id'=$3 ORDER BY created_at DESC LIMIT 1") + .bind(tenant).bind(job.job_id.to_string()).bind(grant.to_string()).fetch_optional(&mut **tx).await?; + ensure!( + original + .as_deref() + .and_then(|v| Uuid::parse_str(v).ok()) + .is_some_and(|old| !old.is_nil() && old != boot), + "REBOOT_ORIGINAL_BOOT_REQUIRED" + ); + let receipt = evidence["inventory_message_id"] + .as_str() + .and_then(|v| Uuid::parse_str(v).ok()) + .filter(|id| !id.is_nil()) + .context("REBOOT_PERSISTED_INVENTORY_REQUIRED")?; + let (_, received) = reboot_inventory( + tx, + tenant, + agent, + boot, + &RebootWindow { + reserved_at: reserved, + deadline, + now: Utc::now(), + require_fresh: true, + }, + Some(receipt), + ) + .await? + .context("REBOOT_TIMELY_INVENTORY_REQUIRED")?; + for (key, actual) in [ + ("reboot_reserved_at", reserved), + ("reconnect_deadline", deadline), + ("inventory_received_at", received), + ] { + ensure!( + serde_json::from_value::>(evidence[key].clone()).ok() + == Some(actual), + "REBOOT_SERVER_TIME_BINDING" + ); + } + Ok(()) +} pub async fn reconcile( state: &crate::State, tx: &mut Transaction<'_, Postgres>, @@ -335,7 +444,7 @@ pub async fn reconcile( continue; } let grant = sqlx::query( - "SELECT grant_id,payload FROM execution_permits WHERE job_id=$1 AND attempt_id=$2", + "SELECT grant_id,payload,created_at FROM execution_permits WHERE job_id=$1 AND attempt_id=$2", ) .bind(job_id) .bind(attempt) @@ -433,18 +542,53 @@ pub async fn reconcile( // A reboot's postcondition is independently tied to persisted current- // boot inventory and the original boot in the durable reservation audit. let mut reboot_reconciled = false; + let mut reboot_witness = None; if matches!(payload.action, Action::Reboot { .. }) && grant_matches && matches!(local.state.as_str(), "awaiting_reboot" | "unknown") { - let original:Option=sqlx::query_scalar("SELECT data->>'boot_id' FROM audit_events WHERE event_type='start_reserved' AND data->>'job_id'=$1 ORDER BY created_at DESC LIMIT 1").bind(job_id.to_string()).fetch_optional(&mut **tx).await?; - let inventory:Option=sqlx::query_scalar("SELECT message_id FROM observations WHERE agent_id=$1 AND boot_id=$2 AND received_at>now()-interval '5 minutes' AND received_at>(SELECT created_at FROM execution_permits WHERE job_id=$3) ORDER BY received_at DESC LIMIT 1").bind(agent).bind(boot).bind(job_id).fetch_optional(&mut **tx).await?; - if let (Some(original), Some(inventory)) = (original, inventory) + let original:Option=sqlx::query_scalar("SELECT data->>'boot_id' FROM audit_events WHERE tenant_id=$1 AND event_type='start_reserved' AND data->>'job_id'=$2 AND data->>'grant_id'=$3 ORDER BY created_at DESC LIMIT 1").bind(tenant).bind(job_id.to_string()).bind(&local.grant_id).fetch_optional(&mut **tx).await?; + let reserved_at = grant + .as_ref() + .map(|g| g.get::, _>("created_at")); + let deadline = reboot_deadline(&payload.action, reserved_at); + let inventory = if let (Some(reserved), Some(until)) = (reserved_at, deadline) { + let existing_receipt = if final_row.get::("state") == "succeeded" { + final_row + .get::, _>("result") + .and_then(|value| { + value["evidence"]["inventory_message_id"] + .as_str() + .and_then(|v| Uuid::parse_str(v).ok()) + }) + } else { + None + }; + reboot_inventory( + tx, + tenant, + agent, + boot, + &RebootWindow { + reserved_at: reserved, + deadline: until, + now: Utc::now(), + require_fresh: existing_receipt.is_none(), + }, + existing_receipt, + ) + .await? + } else { + None + }; + if let (Some(original), Some((inventory, received_at)), Some(reserved), Some(until)) = + (original, inventory, reserved_at, deadline) && original != e.boot_id && decision.original_boot_id == Some(Uuid::parse_str(&original)?) { + reboot_witness = Some((original, inventory)); let revision = (final_row.get::("revision") as u64).max(local.revision) + 1; - let result=JobResult{job_id:local.job_id.clone(),attempt_id:local.attempt_id.clone(),revision,state:"succeeded".into(),grant_id:local.grant_id.clone(),stdout:vec![],stderr:vec![],output_truncated:false,reboot_required:false,exit_code:None,evidence_json:json!({"postconditions_passed":true,"new_boot_id":boot,"inventory_message_id":inventory,"reconciled_after_reboot":true}).to_string(),error_code:String::new()}; + let result=JobResult{job_id:local.job_id.clone(),attempt_id:local.attempt_id.clone(),revision,state:"succeeded".into(),grant_id:local.grant_id.clone(),stdout:vec![],stderr:vec![],output_truncated:false,reboot_required:false,exit_code:None,evidence_json:json!({"postconditions_passed":true,"new_boot_id":boot,"inventory_message_id":inventory,"reconciled_after_reboot":true,"reboot_reserved_at":reserved,"reconnect_deadline":until,"inventory_received_at":received_at}).to_string(),error_code:String::new()}; let mut evidence = e.clone(); evidence.message_id = Uuid::new_v4().to_string(); evidence.payload = Some(Payload::JobResult(result)); @@ -465,18 +609,17 @@ pub async fn reconcile( && terminal == "succeeded" && matches!(local.state.as_str(), "awaiting_reboot" | "unknown") && let Some(value) = final_row.get::, _>("result") + && let Some((original, inventory)) = reboot_witness + && Uuid::parse_str(&original).ok() == decision.original_boot_id + && decision.original_boot_id.is_some() + && value["evidence"]["postconditions_passed"] == json!(true) + && value["evidence"]["reconciled_after_reboot"] == json!(true) + && value["evidence"]["new_boot_id"] + .as_str() + .is_some_and(|id| id == e.boot_id) + && value["evidence"]["inventory_message_id"] == json!(inventory) { - let original:Option=sqlx::query_scalar("SELECT data->>'boot_id' FROM audit_events WHERE event_type='start_reserved' AND data->>'job_id'=$1 ORDER BY created_at DESC LIMIT 1").bind(job_id.to_string()).fetch_optional(&mut **tx).await?; - if original.as_ref().and_then(|v| Uuid::parse_str(v).ok()) == decision.original_boot_id - && decision.original_boot_id.is_some() - && value["evidence"]["postconditions_passed"] == json!(true) - && value["evidence"]["reconciled_after_reboot"] == json!(true) - && value["evidence"]["new_boot_id"] - .as_str() - .is_some_and(|id| Some(id) != original.as_deref()) - { - reboot_reconciled = true; - } + reboot_reconciled = true; } decision.revision = final_row.get::("revision") as u64; decision.outcome_state = terminal.clone(); @@ -603,6 +746,348 @@ async fn mark_unknown(tx: &mut Transaction<'_, Postgres>, job: Uuid, reason: &st mod tests { use super::*; #[test] + fn reboot_deadline_uses_only_the_fixed_plan_and_trusted_reservation() { + let reserved = Utc::now(); + let action = Action::Reboot { + reason: "Explicit final Cloud VM acceptance".into(), + reconnect_timeout_seconds: 180, + dependency_group: "single-test-vm".into(), + }; + assert_eq!( + reboot_deadline(&action, Some(reserved)), + Some(reserved + Duration::seconds(180)) + ); + assert_eq!(reboot_deadline(&action, None), None); + assert_eq!( + reboot_deadline( + &Action::Reboot { + reason: "test".into(), + reconnect_timeout_seconds: 0, + dependency_group: "single-test-vm".into() + }, + Some(reserved) + ), + None + ); + assert_eq!( + reboot_deadline(&action, Some(chrono::DateTime::::MAX_UTC)), + None + ); + } + #[tokio::test] + #[ignore = "requires the existing dedicated isolated PostgreSQL harness"] + async fn reboot_inventory_requires_trusted_timely_same_identity_and_boot() { + let admin = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_ADMIN_URL").unwrap()) + .await + .unwrap(); + let app = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_APP_URL").unwrap()) + .await + .unwrap(); + rmm_persistence::verify_application_role(&app) + .await + .unwrap(); + let tenant = Uuid::new_v4(); + let asset = Uuid::new_v4(); + let agent = Uuid::new_v4(); + let boot = Uuid::new_v4(); + sqlx::query("INSERT INTO tenants VALUES($1,'isolated reboot deadline test')") + .bind(tenant) + .execute(&admin) + .await + .unwrap(); + sqlx::query("INSERT INTO assets VALUES($1,$2)") + .bind(tenant) + .bind(asset) + .execute(&admin) + .await + .unwrap(); + sqlx::query("INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256) VALUES($1,$2,$3,$4)") + .bind(tenant).bind(agent).bind(asset).bind(hash(agent.as_bytes())).execute(&admin).await.unwrap(); + let now = Utc::now(); + let reserved = now - Duration::seconds(200); + let deadline = reserved + Duration::seconds(180); + let window = RebootWindow { + reserved_at: reserved, + deadline, + now, + require_fresh: true, + }; + let timely = Uuid::new_v4(); + let late = Uuid::new_v4(); + for (sequence, message, received) in [ + (1, timely, deadline), + (2, late, deadline + Duration::microseconds(1)), + (3, Uuid::new_v4(), reserved), + ] { + sqlx::query("INSERT INTO observations(tenant_id,agent_id,message_id,boot_id,sequence,observed_at,received_at,source_id,quality,completeness,snapshot_id,data,payload_sha256) VALUES($1,$2,$3,$4,$5,$6,$6,'linux.base','valid','partial',$7,'{}',$8)") + .bind(tenant).bind(agent).bind(message).bind(boot).bind(sequence).bind(received).bind(Uuid::new_v4()).bind("ab".repeat(32)).execute(&admin).await.unwrap(); + } + let mut tx = rmm_persistence::tenant_tx(&app, tenant).await.unwrap(); + assert_eq!( + reboot_inventory(&mut tx, tenant, agent, boot, &window, None) + .await + .unwrap() + .map(|v| v.0), + Some(timely) + ); + assert!( + reboot_inventory(&mut tx, tenant, agent, boot, &window, Some(late)) + .await + .unwrap() + .is_none() + ); + assert!( + reboot_inventory(&mut tx, tenant, Uuid::new_v4(), boot, &window, None) + .await + .unwrap() + .is_none() + ); + assert!( + reboot_inventory(&mut tx, Uuid::new_v4(), agent, boot, &window, None) + .await + .unwrap() + .is_none() + ); + assert!( + reboot_inventory(&mut tx, tenant, agent, Uuid::new_v4(), &window, None) + .await + .unwrap() + .is_none() + ); + let later = RebootWindow { + now: now + Duration::minutes(6), + ..window + }; + assert!( + reboot_inventory(&mut tx, tenant, agent, boot, &later, None) + .await + .unwrap() + .is_none() + ); + // A terminal result reuses only its exact original timely receipt; + // later metadata cannot replace or extend that immutable proof. + let historical = RebootWindow { + require_fresh: false, + ..later + }; + assert_eq!( + reboot_inventory(&mut tx, tenant, agent, boot, &historical, Some(timely)) + .await + .unwrap() + .map(|v| v.0), + Some(timely) + ); + let before_return = RebootWindow { + now: deadline - Duration::microseconds(1), + ..window + }; + assert!( + reboot_inventory(&mut tx, tenant, agent, boot, &before_return, None) + .await + .unwrap() + .is_none() + ); + tx.rollback().await.unwrap(); + let effects:i64=sqlx::query_scalar("SELECT (SELECT count(*) FROM jobs WHERE tenant_id=$1)+(SELECT count(*) FROM outbox WHERE tenant_id=$1)+(SELECT count(*) FROM execution_permits WHERE tenant_id=$1)").bind(tenant).fetch_one(&admin).await.unwrap(); + assert_eq!(effects, 0); + } + #[tokio::test] + #[ignore = "requires the existing dedicated isolated PostgreSQL harness"] + async fn reboot_success_ingress_rejects_forged_late_proof_and_preserves_exact_replay() { + let admin = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_ADMIN_URL").unwrap()) + .await + .unwrap(); + let app = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_APP_URL").unwrap()) + .await + .unwrap(); + rmm_persistence::verify_application_role(&app) + .await + .unwrap(); + let tenant = Uuid::new_v4(); + let asset = Uuid::new_v4(); + let agent = Uuid::new_v4(); + let boot = Uuid::new_v4(); + let old = Uuid::new_v4(); + let job = Uuid::new_v4(); + let attempt = Uuid::new_v4(); + let operation = Uuid::new_v4(); + let plan = Uuid::new_v4(); + let authorization = Uuid::new_v4(); + let grant = Uuid::new_v4(); + let now = chrono::DateTime::from_timestamp_micros(Utc::now().timestamp_micros()).unwrap(); + let reserved = now - Duration::seconds(200); + let deadline = reserved + Duration::seconds(180); + let payload = JobPayload { + schema_version: 1, + tenant_id: tenant, + asset_id: asset, + agent_id: agent, + enrollment_generation: 1, + assignment_version: 1, + job_id: job, + attempt_id: attempt, + action: Action::Reboot { + reason: "Isolated postcondition test; no actual reboot".into(), + reconnect_timeout_seconds: 180, + dependency_group: "single-test-vm".into(), + }, + execution_identity: "root".into(), + not_before: reserved, + start_before: now + Duration::minutes(5), + max_runtime_seconds: 240, + max_output_bytes: 65536, + }; + let bytes = serde_json::to_vec(&payload).unwrap(); + sqlx::query("INSERT INTO tenants VALUES($1,'isolated reboot ingress test')") + .bind(tenant) + .execute(&admin) + .await + .unwrap(); + sqlx::query("INSERT INTO assets VALUES($1,$2)") + .bind(tenant) + .bind(asset) + .execute(&admin) + .await + .unwrap(); + sqlx::query("INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256) VALUES($1,$2,$3,$4)").bind(tenant).bind(agent).bind(asset).bind(hash(agent.as_bytes())).execute(&admin).await.unwrap(); + sqlx::query("INSERT INTO operation_plans(tenant_id,plan_id,principal,payload,plan_hash,expires_at) VALUES($1,$2,'isolated-test',$3,$4,$5)").bind(tenant).bind(plan).bind(b"{}".as_slice()).bind("ab".repeat(32)).bind(now+Duration::minutes(10)).execute(&admin).await.unwrap(); + sqlx::query("INSERT INTO approval_requests(tenant_id,operation_id,plan_id,principal,idempotency_key,request_hash,approval_state) VALUES($1,$2,$3,'isolated-test',$4,$5,'approved')").bind(tenant).bind(operation).bind(plan).bind(operation.to_string()).bind("cd".repeat(32)).execute(&admin).await.unwrap(); + sqlx::query("INSERT INTO authorizations(tenant_id,authorization_id,operation_id,plan_id,plan_hash,principal,session_hash,approval_origin,testsimulation,policy_version) VALUES($1,$2,$3,$4,$5,'isolated-test',$6,'dev_test_browser_simulation',true,1)").bind(tenant).bind(authorization).bind(operation).bind(plan).bind("ab".repeat(32)).bind("ef".repeat(32)).execute(&admin).await.unwrap(); + sqlx::query("INSERT INTO operations VALUES($1,$2,$3)") + .bind(tenant) + .bind(operation) + .bind(authorization) + .execute(&admin) + .await + .unwrap(); + sqlx::query("INSERT INTO jobs(tenant_id,job_id,attempt_id,operation_id,agent_id,asset_id,payload,payload_hash,state,start_reserved,resource) VALUES($1,$2,$3,$4,$5,$6,$7,$8,'unknown',true,'device_execution')").bind(tenant).bind(job).bind(attempt).bind(operation).bind(agent).bind(asset).bind(&bytes).bind(hash(&bytes)).execute(&admin).await.unwrap(); + sqlx::query("INSERT INTO execution_permits VALUES($1,$2,$3,$4,$5,$6,$7)") + .bind(tenant) + .bind(grant) + .bind(job) + .bind(attempt) + .bind(b"{}".as_slice()) + .bind(vec![0u8; 64]) + .bind(reserved) + .execute(&admin) + .await + .unwrap(); + sqlx::query( + "INSERT INTO audit_events VALUES($1,$2,'isolated-test','start_reserved',$3,$4)", + ) + .bind(tenant) + .bind(Uuid::new_v4()) + .bind(json!({"job_id":job,"grant_id":grant,"boot_id":old})) + .bind(reserved) + .execute(&admin) + .await + .unwrap(); + let timely = Uuid::new_v4(); + let late = Uuid::new_v4(); + for (sequence, message, received) in [ + (1, timely, deadline), + (2, late, deadline + Duration::microseconds(1)), + ] { + sqlx::query("INSERT INTO observations(tenant_id,agent_id,message_id,boot_id,sequence,observed_at,received_at,source_id,quality,completeness,snapshot_id,data,payload_sha256) VALUES($1,$2,$3,$4,$5,$6,$6,'linux.base','valid','partial',$7,'{}',$8)").bind(tenant).bind(agent).bind(message).bind(boot).bind(sequence).bind(received).bind(Uuid::new_v4()).bind("aa".repeat(32)).execute(&admin).await.unwrap(); + } + let evidence = json!({"postconditions_passed":true,"reconciled_after_reboot":true,"new_boot_id":boot,"inventory_message_id":late, + "reboot_reserved_at":reserved,"reconnect_deadline":deadline,"inventory_received_at":deadline}); + let mut result = JobResult { + job_id: job.to_string(), + attempt_id: attempt.to_string(), + grant_id: grant.to_string(), + revision: 1, + state: "succeeded".into(), + evidence_json: evidence.to_string(), + ..Default::default() + }; + let mut envelope = Envelope { + message_id: Uuid::new_v4().to_string(), + agent_id: agent.to_string(), + boot_id: boot.to_string(), + payload: Some(Payload::JobResult(result.clone())), + ..Default::default() + }; + let mut tx = rmm_persistence::tenant_tx(&app, tenant).await.unwrap(); + assert!( + crate::job_gateway::event(&mut tx, tenant, agent, &envelope) + .await + .is_err() + ); + assert_eq!( + sqlx::query_scalar::<_, String>("SELECT state FROM jobs WHERE job_id=$1") + .bind(job) + .fetch_one(&mut *tx) + .await + .unwrap(), + "unknown" + ); + assert_eq!( + sqlx::query_scalar::<_, i64>("SELECT count(*) FROM job_receipts") + .fetch_one(&mut *tx) + .await + .unwrap(), + 0 + ); + let mut good = evidence.clone(); + good["inventory_message_id"] = json!(timely); + let mut false_clock = good.clone(); + false_clock["reconnect_deadline"] = json!(now + Duration::days(1)); + result.evidence_json = false_clock.to_string(); + envelope.payload = Some(Payload::JobResult(result.clone())); + assert!( + crate::job_gateway::event(&mut tx, tenant, agent, &envelope) + .await + .is_err() + ); + result.evidence_json = good.to_string(); + envelope.payload = Some(Payload::JobResult(result.clone())); + crate::job_gateway::event(&mut tx, tenant, agent, &envelope) + .await + .unwrap(); + assert_eq!( + sqlx::query_scalar::<_, String>("SELECT state FROM jobs WHERE job_id=$1") + .bind(job) + .fetch_one(&mut *tx) + .await + .unwrap(), + "succeeded" + ); + // The original timely receipt survives later inventories and wall time. + let later = RebootWindow { + reserved_at: reserved, + deadline, + now: now + Duration::minutes(10), + require_fresh: false, + }; + assert_eq!( + reboot_inventory(&mut tx, tenant, agent, boot, &later, Some(timely)) + .await + .unwrap() + .map(|v| v.0), + Some(timely) + ); + assert!( + reboot_inventory(&mut tx, tenant, agent, boot, &later, Some(late)) + .await + .unwrap() + .is_none() + ); + // Exact durable-message replay ACKs the existing receipt, without + // accepting a new result or extending its original evidence window. + crate::job_gateway::event(&mut tx, tenant, agent, &envelope) + .await + .unwrap(); + assert_eq!( + sqlx::query_scalar::<_, i64>("SELECT count(*) FROM job_receipts") + .fetch_one(&mut *tx) + .await + .unwrap(), + 1 + ); + tx.rollback().await.unwrap(); + } + #[test] fn uncertain_request_resolves_only_to_a_known_checkpoint_anchor() { let applied = Uuid::new_v4(); let pending = Uuid::new_v4(); diff --git a/deploy/browser/cloud_reboot_acceptance.cjs b/deploy/browser/cloud_reboot_acceptance.cjs new file mode 100644 index 0000000..2ee33a5 --- /dev/null +++ b/deploy/browser/cloud_reboot_acceptance.cjs @@ -0,0 +1,94 @@ +// Final single Cloud VM reboot: actual dev/test UI only, never API approval. +const fs = require('node:fs'); +const path = require('node:path'); +const crypto = require('node:crypto'); +const { isDeepStrictEqual: equal } = require('node:util'); +const { chromium } = require('../../web/node_modules/playwright'); +const root = path.resolve(__dirname, '../..'); +const reason = 'Explicit final Linux VM dev/test acceptance; reconnect within 180 seconds of the central start reservation'; +const assert = value => { if (!value) throw new Error('Final reboot UI contract mismatch'); }; +function readPrivate(file) { + const s = fs.lstatSync(file); + assert(s.isFile() && !s.isSymbolicLink() && s.uid === process.getuid() && (s.mode & 0o777) === 0o600 && s.size < 1048576); + return JSON.parse(fs.readFileSync(file, 'utf8')); +} +function checkPlan(plan, expected) { + assert(plan.jobs.length === 1 && plan.resolved_targets.length === 1 + && crypto.createHash('sha256').update(Buffer.from(plan.plan_payload_bytes)).digest('hex') === plan.plan_hash); + const p = plan.jobs[0].payload; + assert(p.tenant_id === expected.tenant_id && p.agent_id === expected.agent_id && p.asset_id === expected.asset_id + && p.execution_identity === 'root' && p.max_runtime_seconds === 240 && p.max_output_bytes === 65536); + assert(equal(p.action, { action: 'reboot', reason, reconnect_timeout_seconds: 180, dependency_group: 'single-test-vm' })); + assert(Date.parse(p.start_before) > Date.now()); +} +(async () => { + const file = path.resolve(process.argv[2]); const expected = readPrivate(file); + assert(expected.profile === 'dev/test' && expected.case === 'reboot_last' + && ['prepare', 'approve'].includes(expected.phase) && expected.expected_principal === 'simulated-testprincipal-a'); + const login = readPrivate(path.join(root, 'credentials/server-browser-login.json')); + const browsers = path.join(root, 'web/node_modules/.cache/playwright-browsers'); + const version = fs.readdirSync(browsers).find(x => /^chromium-/.test(x)); + const executablePath = ['chrome-linux/chrome', 'chrome-linux64/chrome'].map(x => path.join(browsers, version, x)).find(x => fs.existsSync(x)); + const browser = await chromium.launch({ headless: true, executablePath, + env: { ...process.env, HOME: path.join(root, 'web/node_modules/.cache/cloud-acceptance/home') } }); + try { + const page = await browser.newPage({ ignoreHTTPSErrors: false, timezoneId: 'UTC' }); page.setDefaultTimeout(15000); + let approvals = 0; + page.on('request', r => { if (new URL(r.url()).pathname.endsWith('/authorizations')) approvals++; }); + await page.goto('https://localhost:18443/'); + await page.getByLabel('Benutzername').fill(login.username); await page.getByLabel('Passwort', { exact: true }).fill(login.password); + const response = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/session' && r.status() === 200); + await page.getByRole('button', { name: 'Anmelden', exact: true }).click(); + const session = await (await response).json(); + assert(session.testsimulation === true && session.approval_origin === 'dev_test_browser_simulation' + && session.principal === expected.expected_principal); + await page.getByRole('status').filter({ hasText: 'SIMULIERT' }).waitFor(); + assert(await page.getByLabel('Mandant', { exact: true }).inputValue() === expected.tenant_id); + const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) }); + if (expected.phase === 'prepare') { + const request = expected.request; + assert(request.action === 'reboot' && request.reason === reason && equal(request.targets.asset_ids, [expected.asset_id]) + && equal(request.rollout, { max_parallel: 1, halt_after_failures: 1 }) + && equal(request.parameters, { reason, dependency_group: 'single-test-vm', reconnect_timeout_seconds: 180, + execution_identity: 'root', timeout_seconds: 240, max_output_bytes: 65536 })); + await page.getByRole('checkbox', { name: 'Ziel ' + expected.asset_id, exact: true }).check(); + await page.getByLabel('Aktion', { exact: true }).selectOption('reboot'); + await page.getByLabel('Aktionsparameter', { exact: true }).fill(JSON.stringify(request.parameters)); + await page.getByLabel('Ausführungsidentität', { exact: true }).selectOption('root'); + await page.getByLabel('Laufzeitlimit (s)', { exact: true }).fill('240'); + await page.getByLabel('Ausgabelimit (Bytes)', { exact: true }).fill('65536'); + await page.getByLabel('Frühester Start (lokale Zeit)', { exact: true }).fill(new Date(request.schedule.not_before).toISOString().slice(0, 16)); + await page.getByLabel('Startfrist / Ablauf (lokale Zeit)', { exact: true }).fill(new Date(request.schedule.start_before).toISOString().slice(0, 16)); + await page.getByLabel('Maximal parallel', { exact: true }).fill('1'); await page.getByLabel('Stopp nach Fehlern', { exact: true }).fill('1'); + await page.getByLabel('Begründung', { exact: true }).fill(reason); + await page.getByRole('button', { name: 'Plan erstellen · noch keine Ausführung' }).click(); + await detail.locator('pre').waitFor(); const plan = JSON.parse(await detail.locator('pre').innerText()); checkPlan(plan, expected); + await detail.getByRole('button', { name: 'Antrag absenden · pending_approval' }).click(); + const operation = page.locator('section').filter({ has: page.getByRole('heading', { name: /^Antrag \/ Ausführung / }) }); + await operation.locator('pre').first().filter({ hasText: '"job_count": 0' }).waitFor(); + const pending = JSON.parse(await operation.locator('pre').first().innerText()); + assert(pending.approval_state === 'pending_approval' && pending.execution_state === null && approvals === 0); + assert(path.dirname(path.resolve(expected.output_file)) === path.dirname(file)); + fs.writeFileSync(expected.output_file, JSON.stringify({ plan, operation: pending }), { flag: 'wx', mode: 0o600 }); + } else { + await page.getByRole('button', { name: expected.operation_id, exact: true }).click(); + const operation = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Antrag / Ausführung ' + expected.operation_id, exact: true }) }); + const pending = JSON.parse(await operation.locator('pre').first().innerText()); + assert(pending.approval_state === 'pending_approval' && pending.job_count === 0 && pending.execution_state === null); + await operation.getByRole('button', { name: 'Unveränderlichen Plan zur Prüfung öffnen', exact: true }).click(); + const plan = JSON.parse(await detail.locator('pre').innerText()); checkPlan(plan, expected); + const { approval_state: currentState, ...actual } = plan; + const { approval_state: previousState, ...prepared } = expected.plan; + assert(currentState === 'pending_approval' && previousState === null && equal(actual, prepared) + && equal(plan.jobs[0], expected.job)); + const approve = detail.getByRole('button', { name: 'SIMULIERT: Diesen festen Plan ausdrücklich genehmigen', exact: true }); + assert(await approve.isDisabled()); + await detail.getByRole('checkbox', { name: 'Ich habe sämtliche festen Ziele', exact: false }).check(); await approve.click(); + await operation.locator('p.warning').filter({ hasText: 'Antrag: approved' }).waitFor(); assert(approvals === 1); + } + await page.getByRole('button', { name: 'Abmelden', exact: true }).click(); + } finally { await browser.close(); } +})().catch(error => { + const line = error.stack?.match(/cloud_reboot_acceptance\.cjs:(\d+):\d+/)?.[1] || 'unknown'; + console.log('CLOUD_REBOOT_UI_INCOMPLETE:' + error.constructor.name + ':source_line=' + line); process.exitCode = 1; +}); diff --git a/deploy/cloud_reboot_acceptance.py b/deploy/cloud_reboot_acceptance.py new file mode 100644 index 0000000..2a881af --- /dev/null +++ b/deploy/cloud_reboot_acceptance.py @@ -0,0 +1,273 @@ +#!/usr/bin/env python3 +"""LAST Linux-MVP gate: one fixed WebUI-approved Cloud-VM reboot. + +Bootstrap changes only the existing General policy/pin and an expiring central +reboot-only scope. Bootstrap never requests a reboot. Unknown means stop, never +retry; finish reads the same job and close restores the original policy/pin. +""" +import argparse +import datetime as dt +import hashlib +import json +import os +from pathlib import Path +import subprocess +import time +import uuid + +import cloud_apt_recovery as apt + +cloud=apt.cloud +require=cloud.contract.require +STATE=cloud.ROOT/'devstate/cloud-reboot' +REASON='Explicit final Linux VM dev/test acceptance; reconnect within 180 seconds of the central start reservation' +GATES=('apt_native_update','ensure_package','apply_updates','offline_wait','expired_offline','revoked_before_start','agent_crash','transport_fallback','mcp') + + +def vm_state(payload): + import sqlite3 + root=Path('/var/lib/rmm-agent-remote');helper=Path('/var/lib/rmm-helper') + require(json.loads((root/'identity.json').read_text())['agent_id']==payload['agent_id'],'permanent Cloud identity') + for path,key in ((Path('/usr/local/libexec/rmm-agent-remote'),'agent_sha256'),(Path('/usr/local/libexec/rmm-privileged-helper'),'helper_sha256')): + require(not path.is_symlink() and hashlib.sha256(path.read_bytes()).hexdigest()==payload[key],'qualified current binaries') + require(hashlib.sha256(Path('/etc/systemd/system/rmm-helper-cloud.service').read_bytes()).hexdigest()==payload['general_unit_sha256'],'reviewed General D-Bus service profile with direct reboot syscalls denied') + services={} + for unit in ('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'): + raw=subprocess.run(['systemctl','show',unit,'-p','ActiveState','-p','MainPID','-p','UnitFileState'],capture_output=True,check=True,timeout=10).stdout.decode() + services[unit]=dict(line.split('=',1) for line in raw.splitlines()) + dropin=Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf') + require(not dropin.is_symlink() and 'RMM_APT_HELPER_' not in dropin.read_text() and services['rmm-apt-helper.service']['ActiveState']=='inactive','close APT endpoint before final reboot gate') + result=dict(boot_id=Path('/proc/sys/kernel/random/boot_id').read_text().strip(),services=services,root_rows={},database_inodes={}) + for path in (root/'journal.sqlite',root/'consumption.sqlite',helper/'consumption.sqlite'): + s=path.stat();result['database_inodes'][str(path)]=[s.st_dev,s.st_ino] + with sqlite3.connect(path.as_uri()+'?mode=ro',uri=True) as db: + require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','preserved database integrity') + if path==helper/'consumption.sqlite': + rows=db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY grant_id').fetchall() + for row in rows: + serial=[dict(sha256=hashlib.sha256(v).hexdigest(),size=len(v)) if isinstance(v,bytes) else v for v in row] + result['root_rows'][row[0]]=hashlib.sha256(json.dumps(serial,separators=(',',':')).encode()).hexdigest() + result['root_unresolved']=sum(row[5] is None for row in rows) + require(db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0]==0,'no helper fault') + if payload.get('job_id'): + selected=[r for r in rows if r[3]==payload['job_id'] and r[4]==payload['attempt_id']] + result['selected_root_grants']=[r[0] for r in selected] + row=db.execute('SELECT response FROM helper_sync_decisions WHERE decision_id=(SELECT last_decision FROM helper_sync_meta WHERE id=1)').fetchone() + signed=json.loads(row[0]);decision=json.loads(bytes(signed['decision_bytes'])) + result['signed_sync']=dict(decision_id=decision['decision_id'],ready=decision['ready'],helper_ready=decision['helper_ready'],boot_id=decision['boot_id'], + selected=[a for a in decision['attempts'] if a['job_id']==payload['job_id'] and a['attempt_id']==payload['attempt_id']]) + elif path==root/'journal.sqlite': + require(db.execute('SELECT count(*) FROM reconcile_meta WHERE fault IS NOT NULL').fetchone()[0]==0,'no agent fault') + result['agent_last_decision']=db.execute('SELECT last_decision FROM reconcile_meta WHERE id=1').fetchone()[0] + if payload.get('job_id'): + result['other_unresolved']=db.execute("SELECT count(*) FROM job_attempts WHERE NOT(job=? AND attempt=?) AND state NOT IN ('succeeded','failed','cancelled','expired')",(payload['job_id'],payload['attempt_id'])).fetchone()[0] + else:result['other_unresolved']=db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0] + config=Path('/etc/rmm-remote/helper-general.json');pin=Path('/etc/rmm-remote/general-helper-proof.json') + result.update(config_sha256=hashlib.sha256(config.read_bytes()).hexdigest(),pin_sha256=hashlib.sha256(pin.read_bytes()).hexdigest(),general_pin=json.loads(pin.read_text())) + return result + + +def vm_policy(payload): + import fcntl + import sqlite3 + os.umask(0o077) + state=Path('/var/lib/rmm-agent-remote');helper=Path('/var/lib/rmm-helper');config=Path('/etc/rmm-remote/helper-general.json');pin=Path('/etc/rmm-remote/general-helper-proof.json') + binary=Path('/usr/local/libexec/rmm-privileged-helper');agent=Path('/usr/local/libexec/rmm-agent-remote') + require(hashlib.sha256(binary.read_bytes()).hexdigest()==payload['helper_sha256'] and hashlib.sha256(agent.read_bytes()).hexdigest()==payload['agent_sha256'],'same qualified runtime') + require(json.loads((state/'identity.json').read_text())['agent_id']==payload['agent_id'],'same permanent identity') + require(not config.is_symlink() and not pin.is_symlink() and hashlib.sha256(config.read_bytes()).hexdigest()==payload['config_sha256'] and hashlib.sha256(pin.read_bytes()).hexdigest()==payload['pin_sha256'],'exact General policy/pin required') + enabled=payload['enabled'];require(type(enabled) is bool,'fixed policy direction') + current=json.loads(config.read_text());require(current['allow_reboot'] is (not enabled),'only one explicit policy transition') + backup=Path('/var/lib/rmm-bootstrap-history')/('cloud-reboot-'+str(uuid.UUID(payload['id']))+('-enable' if enabled else '-restore'));backup.mkdir(mode=0o700) + def create(path,raw,mode=0o600,gid=0): + fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,mode) + with os.fdopen(fd,'wb') as f:f.write(raw);f.flush();os.fchown(f.fileno(),0,gid);os.fchmod(f.fileno(),mode);os.fsync(f.fileno()) + def manifest(path): + result={} + for p in [path,*sorted(path.rglob('*'))]: + s=p.lstat();require(not p.is_symlink() and (p.is_file() or p.is_dir()),'preserved state must be regular') + result[str(p.relative_to(path))]=[s.st_dev,s.st_ino,hashlib.sha256(p.read_bytes()).hexdigest() if p.is_file() else None] + return result + subprocess.run(['systemctl','stop','rmm-agent-remote.service','rmm-helper-cloud.service'],capture_output=True,check=True,timeout=30) + lock=os.open(helper/'executor.lock',os.O_RDWR|os.O_NOFOLLOW);fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB) + try: + with sqlite3.connect((helper/'consumption.sqlite').as_uri()+'?mode=ro',uri=True) as db: + require(db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0 and db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0]==0,'all Root effects closed') + preserved={str(p):manifest(p) for p in (state,helper,Path('/var/lib/rmm-apt-transaction'))} + old_config=config.read_bytes();old_pin=pin.read_bytes() + if enabled: + next_config=dict(current,allow_reboot=True) + else: + original=Path(payload['original_backup']) + require(original.parent==backup.parent and original.name=='cloud-reboot-'+payload['id']+'-enable','same versioned policy origin') + original_raw=(original/'general-before.json').read_bytes();next_config=json.loads(original_raw) + require(next_config==dict(current,allow_reboot=False),'restore only the exact original policy') + for p,name in ((config,'general-before.json'),(pin,'pin-before.json')): + require(p.stat().st_dev==backup.stat().st_dev,'same-filesystem versioned original') + os.rename(p,backup/name) + create(config,json.dumps(next_config).encode() if enabled else original_raw) + output=subprocess.run([str(binary),'--inspect-general-profile'],capture_output=True,check=True,timeout=15,env=dict(os.environ,RMM_HELPER_CONFIG=str(config))).stdout + new_pin=json.loads(output) + require(new_pin['identity']==json.loads(old_pin)['identity'],'helper identity unchanged') + if not enabled:require(new_pin==json.loads((Path(payload['original_backup'])/'pin-before.json').read_text()),'original pin restored') + create(pin,json.dumps(new_pin).encode() if enabled else (Path(payload['original_backup'])/'pin-before.json').read_bytes(),0o640,984) + require(all(manifest(Path(p))==v for p,v in preserved.items()),'journal/ledger/fixture/history bytes and inodes preserved before restart') + report=dict(enabled=enabled,backup=str(backup),preserved_state=True,config_sha256=hashlib.sha256(config.read_bytes()).hexdigest(),pin_sha256=hashlib.sha256(pin.read_bytes()).hexdigest(),general_pin=new_pin,reboot_requested=False) + create(backup/'report.json',json.dumps(report).encode()) + finally:fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock) + subprocess.run(['systemctl','start','rmm-helper-cloud.service','rmm-agent-remote.service'],capture_output=True,check=True,timeout=30) + return report + + +def server_policy(payload): + import datetime as dt + root=Path('/opt/rust-discovery');path=root/'devstate/root-policy/policy.json' + compose=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml')] + require(subprocess.run(compose+['exec','-T','gateway','printenv','RMM_PROFILE','RMM_DEV_AUTH'],capture_output=True,check=True,timeout=15).stdout.splitlines()==[b'development',b'true'],'actual development gateway only') + raw=path.read_bytes();policy=json.loads(raw);digest=hashlib.sha256(raw).hexdigest() + require(not path.is_symlink() and path.stat().st_uid==10001 and path.stat().st_mode&0o777==0o600,'private root policy') + if payload['action']=='state':return dict(policy_sha256=digest,closed=dt.datetime.fromisoformat(policy['expires_at'])<=dt.datetime.now(dt.timezone.utc)) + require(digest==payload['previous_sha256'],'Root policy drift') + if payload['action']=='enable': + require(dt.datetime.fromisoformat(policy['expires_at'])<=dt.datetime.now(dt.timezone.utc),'previous policy must be closed') + updated=payload['policy'];require(len(updated['scopes'])==1,'one final Cloud VM scope') + scope=updated['scopes'][0] + now=dt.datetime.now(dt.timezone.utc) + require(updated['schema_version']==1 and updated['profile']=='development' and now>'boot_id' FROM audit_events WHERE tenant_id=a.tenant_id AND event_type='start_reserved' AND data->>'job_id'='"+job+"' AND data->>'grant_id'=p.grant_id::text)," + "'inventory_message_id',o.message_id,'inventory_boot_id',o.boot_id,'inventory_received_at',o.received_at) FROM agent_installations a " + "JOIN execution_permits p ON p.tenant_id=a.tenant_id AND p.job_id='"+job+"' JOIN observations o ON o.tenant_id=a.tenant_id AND o.agent_id=a.agent_id AND o.message_id='"+receipt+"' " + "WHERE a.tenant_id='"+tenant+"' AND a.agent_id='"+agent+"';") + cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm'] + raw=subprocess.run(cmd,input=query.encode(),capture_output=True,check=True,timeout=15).stdout.strip();require(raw,'bound authenticated reboot/inventory evidence required') + return json.loads(raw) + + +def prerequisites(path,target): + m=cloud.contract;manifest=m.protected_json(Path(path));require(manifest['schema_version']==1 and manifest['target']==target and set(manifest['checks'])==set(GATES),'all earlier MVP gates for this permanent target') + selected={} + for name,item in manifest['checks'].items(): + p=Path(item['path']).resolve();require(p.is_relative_to(cloud.ROOT/'devstate') and hashlib.sha256(p.read_bytes()).hexdigest()==item['sha256'],'exact protected native gate witness') + data=m.protected_json(p);require(data.get('verified') is True,'native gate not verified');selected[name]=data + for name in ('apt_native_update','ensure_package','apply_updates'): + result=selected[name]['result'];proof=result['result']['evidence']['apt'] + require(result['state']=='succeeded' and proof['packages']==[dict(name='rmm-mvp-test',architecture='all',version='1.1')] and proof['fixture_payload_sha256']==hashlib.sha256(b'1.1\n').hexdigest(),'earlier approved stored fixture1.1 postcondition; no SSH package scan') + return dict(path=str(Path(path).resolve()),sha256=hashlib.sha256(Path(path).read_bytes()).hexdigest(),checks=manifest['checks']) + + +def fixed(): + installed=cloud.contract.protected_json(cloud.ROOT/'devstate/remote-vm.json');runtime,_=apt.runtime_session() + return dict(tenant_id=cloud.TENANT,**installed['identity']),dict(agent_id=installed['identity']['agent_id'],agent_sha256=installed['binary_sha256'],helper_sha256=runtime['helper_sha256'],general_unit_sha256=hashlib.sha256((cloud.ROOT/'deploy/linux/rmm-helper-cloud.service').read_bytes()).hexdigest()) + + +def evidence(target,kind,**extra):return cloud.bootstrap.rpc('server.conf',cloud.server,dict(kind=kind,tenant_id=target['tenant_id'],agent_id=target['agent_id'],**extra)) +def browser(path): + cloud.prepare_browser();result=subprocess.run(['node',str(cloud.ROOT/'deploy/browser/cloud_reboot_acceptance.cjs'),str(path)],capture_output=True,timeout=120) + require(result.returncode==0,'actual reboot UI incomplete; retain fixed checkpoint, never repeat approval') + + +def enable(path): + m=cloud.contract;target,pins=fixed();gates=prerequisites(path,target) + require(evidence(target,'gate')['unresolved']==0,'no earlier unresolved work') + old=cloud.bootstrap.rpc('server.conf',server_policy,dict(action='state'));require(old['closed'],'all earlier Root scopes must be closed') + before=cloud.bootstrap.rpc('linux-vm.conf',vm_state,pins) + require(before['root_unresolved']==before['other_unresolved']==0,'healthy fully reconciled runtime') + for name in ('rmm-agent-remote.service','rmm-helper-cloud.service'): + require(before['services'][name]['ActiveState']=='active' and before['services'][name]['UnitFileState']=='enabled','agent and General helper must already autostart after boot') + ident=str(uuid.uuid4());folder=STATE/ident;folder.mkdir(mode=0o700,parents=True) + session=dict(id=ident,target=target,pins=pins,prerequisites=gates,before=before,old_boot_id=before['boot_id']) + m.write_private(folder/'before.json',session) + changed=cloud.bootstrap.rpc('linux-vm.conf',vm_policy,dict(pins,id=ident,enabled=True,config_sha256=before['config_sha256'],pin_sha256=before['pin_sha256'])) + m.write_private(folder/'helper-enabled.json',changed) + scope={k:changed['general_pin']['identity'][k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')} + scope.update(principals=['simulated-testprincipal-a'],helper_policy_sha256=changed['general_pin']['policy_sha256'],acceptance_evidence_id='final-cloud-reboot:'+ident,allowed_services=[],allow_root_scripts=False,allow_reboot=True) + policy=dict(schema_version=1,profile='development',expires_at=(m.now()+dt.timedelta(minutes=30)).isoformat(),scopes=[scope]) + opened=cloud.bootstrap.rpc('server.conf',server_policy,dict(id=ident,action='enable',previous_sha256=old['policy_sha256'],policy=policy)) + m.write_private(folder/'policy-open.json',dict(opened,policy=policy));print(dict(id=ident,reboot_requested=False,reboot_only_limits_enabled=True)) + + +def accept(ident): + m=cloud.contract;folder=STATE/str(uuid.UUID(ident));session=m.protected_json(folder/'before.json');target=session['target'];pins=session['pins'] + prerequisites(session['prerequisites']['path'],target) + require(not (folder/'prepare.json').exists() and evidence(target,'gate')['unresolved']==0,'one new fixed attempt only') + deadline=time.monotonic()+100 + while True: + device=cloud.Backend().call('inventory.get_device',{k:target[k] for k in ('tenant_id','asset_id')}) + if device['presence']=='online' and {'reboot','root:reboot'}<=set(device['capabilities']):break + require(time.monotonic() +python3 deploy/cloud_reboot_acceptance.py close --id +``` + +`accept` erstellt und beantragt genau einen festen Root-Job über die tatsächliche +WebUI, beweist `pending_approval` mit **0/0/0** und öffnet erst danach eine eigene +WebUI-Genehmigungssitzung. Der Job nennt den ausdrücklichen Grund, Gruppe +`single-test-vm`, Parallelität 1, Laufzeit 240 s und Wiederverbindungsfrist 180 s +ab zentraler Startreservierung. Alte Boot-ID und unveränderlicher Plan werden +vor Genehmigung gespeichert. Kein SSH-Reboot oder direkter Freigabeaufruf existiert. + +Erst tatsächliche ausgehende Wiederanmeldung, neuer Boot-ID, rechtzeitig +gespeichertes Inventar, angewandter signierter RootSync und terminaler Erfolg +bestätigen den gleichen Grant. Alte Ledgerzeilen und DB-Inodes müssen erhalten +sein; genau ein neuer Rootverbrauch sowie **1 Job / 1 Outbox / 1 Grant** sind +erforderlich. Unterbrochenes Warten darf ausschließlich `finish --id` für diesen +gleichen Versuch fortsetzen. Unknown oder Timeout erlauben keinen zweiten Reboot. + +Nach normal bestätigter Rückkehr schließt `close` zuerst die zentrale Scope und +stellt dann die ursprünglichen Config-/Pinbytes mit `allow_reboot:false` wieder +her. Diese operative Rücknahme startet Agent und General-Helper neu, führt aber +keinen zweiten Reboot aus. Tatsächliche Durchführung bleibt bis zur Freigabe des +abschließenden Schritts ausdrücklich offen.