From a6c1f9accc4c41b7309d4d301e9eafb1fdac9364 Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Sat, 14 Nov 2026 01:14:11 +0100 Subject: [PATCH] Share helper reconciliation and verify Linux and Windows native fixtures --- crates/agent-core/src/auth.rs | 16 + crates/agent-core/src/crypto.rs | 16 + crates/agent-core/src/helper_reconcile.rs | 1314 +++++++++++++++++++ crates/agent-core/src/lib.rs | 1 + crates/agent-core/src/reconcile.rs | 24 +- crates/agent-core/tests/helper_reconcile.rs | 993 ++++++++++++++ crates/agent-core/tests/reconcile.rs | 93 +- crates/privileged-helper/src/reconcile.rs | 1268 +----------------- crates/privileged-helper/tests/reconcile.rs | 2 +- deploy/shared-core-acceptance.md | 27 + deploy/shared_core_acceptance.py | 70 +- deploy/windows/client_bootstrap.py | 4 +- docs/handover.md | 5 + docs/implementation-status.md | 17 + 14 files changed, 2557 insertions(+), 1293 deletions(-) create mode 100644 crates/agent-core/src/helper_reconcile.rs create mode 100644 crates/agent-core/tests/helper_reconcile.rs diff --git a/crates/agent-core/src/auth.rs b/crates/agent-core/src/auth.rs index 6404bc1..ea13868 100644 --- a/crates/agent-core/src/auth.rs +++ b/crates/agent-core/src/auth.rs @@ -138,3 +138,19 @@ impl Outcome { } } } + +/// Explicit configured privileged identity; never infer SYSTEM from Linux root. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum HelperIdentity { + #[default] + Root, + System, +} +impl HelperIdentity { + pub fn as_str(self) -> &'static str { + match self { + Self::Root => "root", + Self::System => "system", + } + } +} diff --git a/crates/agent-core/src/crypto.rs b/crates/agent-core/src/crypto.rs index e4f2548..0f11af9 100644 --- a/crates/agent-core/src/crypto.rs +++ b/crates/agent-core/src/crypto.rs @@ -3,6 +3,7 @@ use anyhow::Result; use ed25519_dalek::{Signature, VerifyingKey}; pub trait Ed25519Verifier { + fn validate_key(&self, public_key: &[u8]) -> Result; fn verify(&self, public_key: &[u8], message: &[u8], signature: &[u8]) -> Result; } @@ -10,6 +11,13 @@ pub trait Ed25519Verifier { pub struct DalekVerifier; impl Ed25519Verifier for DalekVerifier { + fn validate_key(&self, public_key: &[u8]) -> Result { + let Ok(raw): Result<&[u8; 32], _> = public_key.try_into() else { + return Ok(false); + }; + Ok(VerifyingKey::from_bytes(raw).is_ok_and(|key| !key.is_weak())) + } + fn verify(&self, public_key: &[u8], message: &[u8], signature: &[u8]) -> Result { let Ok(raw): Result<&[u8; 32], _> = public_key.try_into() else { return Ok(false); @@ -33,6 +41,14 @@ pub struct OpenSslVerifier; #[cfg(all(unix, feature = "openssl-verifier"))] impl Ed25519Verifier for OpenSslVerifier { + fn validate_key(&self, public_key: &[u8]) -> Result { + if public_key.len() != 32 { + return Ok(false); + } + openssl::pkey::PKey::public_key_from_raw_bytes(public_key, openssl::pkey::Id::ED25519)?; + Ok(true) + } + fn verify(&self, public_key: &[u8], message: &[u8], signature: &[u8]) -> Result { use openssl::{ pkey::{Id, PKey}, diff --git a/crates/agent-core/src/helper_reconcile.rs b/crates/agent-core/src/helper_reconcile.rs new file mode 100644 index 0000000..0f57730 --- /dev/null +++ b/crates/agent-core/src/helper_reconcile.rs @@ -0,0 +1,1314 @@ +//! Independently verifying root-ledger reconciliation. This module never executes +//! an operation or grants authority from an agent-provided ready/unlock flag. +//! Both general and APT listeners must use the SAME executor.lock and SQLite DB. +use crate::auth::HelperIdentity; +use crate::auth::{Binding, Request}; +use crate::crypto::{DefaultVerifier, Ed25519Verifier}; +use anyhow::{Context, Result, ensure}; +use chrono::{DateTime, Utc}; +use rmm_domain::{ + jobs::{Action, ExecutionPermitPayload, hash}, + reconciliation::{ + AttemptDecision, ReconcileDecisionPayload, ReconciledOutcome, canonical_outcome_bytes, + }, +}; +use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params}; +use serde::{Deserialize, Serialize}; +use std::collections::{BTreeMap, BTreeSet}; +use uuid::Uuid; + +pub use crate::reconcile::SyncClock as Clock; +pub trait BootSource { + fn current_boot(&self) -> Result; +} +/// Must be backed by a held executor lock and an independently authenticated +/// ledger file identity. Windows adapters use Volume/File-ID, never Unix inodes. +/// The guard remains held through snapshot/apply/admission/consumption/effect. +pub trait ExecutorLedgerGuard { + fn check(&self, db: &Connection) -> Result<()>; +} +/// Same two fields as public SyncResponse, without adding a new dependency or +/// wire type. RPC glue copies the ORIGINAL bytes/signature, never reserializes. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields)] +pub struct SignedResponse { + pub decision_bytes: Vec, + pub signature: Vec, +} +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct LocalAttempt { + pub job_id: Uuid, + pub attempt_id: Uuid, + pub job_payload_hash: String, + pub grant_id: Uuid, + pub authorization_id: Uuid, + pub plan_hash: String, + pub action: String, + pub execution_identity: String, + pub revision: u64, + pub original_boot_id: Uuid, + pub consumption: String, + pub state: String, + pub outcome_sha256: String, + pub outcome_bytes: Vec, + /// Hash of the exact root-owned Request containing original job/permit bytes. + pub original_request_sha256: String, +} +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Snapshot { + pub journal_id: Uuid, + pub checkpoint: u64, + pub previous_decision_id: Option, + pub boot_id: Uuid, + pub captured_at: DateTime, + pub attempts: Vec, +} +pub struct Applied { + pub snapshot: Snapshot, + pub unlocked_grants: Vec, + pub ready: bool, +} +struct Capture { + snapshot: Snapshot, + fingerprint: String, + generation: u64, +} +struct Lease { + checkpoint: u64, + decision_id: Uuid, + boot_id: Uuid, + generation: u64, + until: DateTime, + fingerprint: String, +} +pub struct HelperSync { + binding: Binding, + verifier: V, + identity: HelperIdentity, + born: DateTime, + capture: Option, + lease: Option, +} +impl HelperSync { + /// No persisted ready bit can open a newly started helper. + pub fn new(binding: Binding, clock: &impl Clock) -> Result { + Self::with_verifier(binding, DefaultVerifier::default(), clock) + } +} +impl HelperSync { + pub fn with_verifier(binding: Binding, verifier: V, clock: &impl Clock) -> Result { + pinned_key(&binding, &verifier)?; + Ok(Self { + binding, + verifier, + identity: HelperIdentity::Root, + born: clock.now()?, + capture: None, + lease: None, + }) + } + /// Select exactly one protected helper execution identity, closing leases. + pub fn with_helper_identity(mut self, identity: HelperIdentity) -> Self { + self.close(); + self.identity = identity; + self + } + pub fn close(&mut self) { + self.lease = None; + self.capture = None; + } + pub fn active_generation(&self) -> Option { + self.lease.as_ref().map(|lease| lease.generation) + } + /// Every call creates a NEW durable checkpoint challenge and closes the old + /// lease. Use apply's returned snapshot for its ACK, not another snapshot(). + pub fn snapshot( + &mut self, + db: &mut Connection, + guard: &impl ExecutorLedgerGuard, + generation: u64, + clock: &impl Clock, + boot: &impl BootSource, + ) -> Result { + self.close(); + let result = self.snapshot_inner(db, guard, generation, clock, boot); + if let Err(error) = &result { + persist_fault(db, error); + } + result + } + fn snapshot_inner( + &mut self, + db: &mut Connection, + guard: &impl ExecutorLedgerGuard, + generation: u64, + clock: &impl Clock, + boot: &impl BootSource, + ) -> Result { + guard.check(db)?; + let now = clock.now()?; + ensure!( + now >= self.born && generation > 0, + "CLOCK_OR_CHANNEL_REGRESSION" + ); + let boot_id = boot.current_boot()?; + ensure!(!boot_id.is_nil(), "BOOT_ID_REQUIRED"); + initialize(db, &self.binding)?; + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + let meta = metadata(&tx)?; + ensure!(meta.fault.is_none(), "HELPER_MANUAL_RECOVERY_REQUIRED"); + let highest: u64 = tx.query_row( + "SELECT COALESCE(MAX(checkpoint),0) FROM helper_sync_captures", + [], + |r| r.get(0), + )?; + ensure!(meta.checkpoint == highest, "HELPER_CHECKPOINT_ROLLBACK"); + ensure!(generation >= meta.generation, "STALE_HELPER_CONNECTION"); + validate_history(&tx, &self.binding, &meta, generation, &self.verifier)?; + let attempts = load_rows(&tx, &self.binding, true, self.identity, &self.verifier)?; + ensure!(attempts.len() <= 256, "HELPER_SYNC_CAPACITY"); + let fingerprint = ledger_fingerprint(&tx)?; + // Two independently gated profiles share one signed helper checkpoint. + // Join only a still-unapplied root-owned capture created after THIS + // process was born, on the same fenced channel and unchanged ledger. + if meta.checkpoint > 0 && meta.generation == generation { + let applied: bool = tx.query_row( + "SELECT EXISTS(SELECT 1 FROM helper_sync_decisions WHERE checkpoint=?1)", + [meta.checkpoint], + |r| r.get(0), + )?; + let old: Option<(Vec, String)> = tx + .query_row( + "SELECT snapshot,fingerprint FROM helper_sync_captures WHERE checkpoint=?1", + [meta.checkpoint], + |r| Ok((r.get(0)?, r.get(1)?)), + ) + .optional()?; + if !applied && let Some((bytes, old_fingerprint)) = old { + let shared: Snapshot = serde_json::from_slice(&bytes)?; + if shared.captured_at >= self.born + && shared.captured_at <= now + && now - shared.captured_at < chrono::Duration::seconds(30) + && shared.boot_id == boot_id + && shared.previous_decision_id == meta.decision + && old_fingerprint == fingerprint + && serde_json::to_vec(&shared.attempts)? == serde_json::to_vec(&attempts)? + { + tx.commit()?; + self.capture = Some(Capture { + snapshot: shared.clone(), + fingerprint, + generation, + }); + return Ok(shared); + } + } + } + let checkpoint = meta + .checkpoint + .checked_add(1) + .context("CHECKPOINT_OVERFLOW")?; + i64::try_from(checkpoint)?; + let snapshot = Snapshot { + journal_id: meta.journal_id, + checkpoint, + previous_decision_id: meta.decision, + boot_id, + captured_at: now, + attempts, + }; + let bytes = serde_json::to_vec(&snapshot)?; + ensure!(bytes.len() <= 2 * 1024 * 1024, "HELPER_SNAPSHOT_SIZE"); + tx.execute("INSERT INTO helper_sync_captures(checkpoint,snapshot,fingerprint,captured_at) VALUES(?1,?2,?3,?4)",params![checkpoint,bytes,fingerprint,now.to_rfc3339()])?; + tx.execute( + "UPDATE helper_sync_meta SET checkpoint=?1,generation=?2 WHERE id=1", + params![checkpoint, generation], + )?; + tx.commit()?; + self.capture = Some(Capture { + snapshot: snapshot.clone(), + fingerprint, + generation, + }); + Ok(snapshot) + } + /// raw_request is SyncRequest::encode_to_vec(), NOT the outer Envelope or a + /// JSON view. Verify signature before decoding fields. No unsigned unlocks. + pub fn apply( + &mut self, + db: &mut Connection, + guard: &impl ExecutorLedgerGuard, + response: &SignedResponse, + raw_request: &[u8], + clock: &impl Clock, + boot: &impl BootSource, + ) -> Result { + self.lease = None; + let result = self.apply_inner(db, guard, response, raw_request, clock, boot); + if let Err(error) = &result { + self.lease = None; + persist_fault(db, error); + } + result + } + fn apply_inner( + &mut self, + db: &mut Connection, + guard: &impl ExecutorLedgerGuard, + response: &SignedResponse, + raw_request: &[u8], + clock: &impl Clock, + boot: &impl BootSource, + ) -> Result { + guard.check(db)?; + let now = clock.now()?; + let capture = self + .capture + .as_ref() + .context("FRESH_PROCESS_SNAPSHOT_REQUIRED")?; + let decision = verify(&self.verifier, &self.binding, response, now)?; + let request = RequestView::decode(raw_request)?; + let current_boot = boot.current_boot()?; + ensure!( + now >= self.born + && decision.issued_at >= self.born + && decision.issued_at >= capture.snapshot.captured_at + && current_boot == capture.snapshot.boot_id + && decision.boot_id == current_boot, + "OLD_PROCESS_DECISION_OR_BOOT" + ); + ensure!( + decision.connection_generation == capture.generation + && decision.helper_journal_id == Some(capture.snapshot.journal_id) + && decision.helper_checkpoint == capture.snapshot.checkpoint + && request.helper_journal_id == capture.snapshot.journal_id.to_string() + && request.helper_checkpoint == capture.snapshot.checkpoint + && request.helper_previous_decision_id + == capture + .snapshot + .previous_decision_id + .map(|id| id.to_string()) + .unwrap_or_default() + && decision.request_id.to_string() == request.request_id + && decision.request_hash == hash(raw_request) + && decision.journal_id.to_string() == request.journal_id + && decision.checkpoint == request.checkpoint + && request.assignment_version == self.binding.assignment_version + && request.journal_complete, + "HELPER_SYNC_REQUEST_BINDING" + ); + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + let meta = metadata(&tx)?; + ensure!( + meta.fault.is_none() + && meta.journal_id == capture.snapshot.journal_id + && meta.checkpoint >= capture.snapshot.checkpoint + && meta.generation >= capture.generation, + "HELPER_CHECKPOINT_ROLLBACK" + ); + ensure!( + meta.checkpoint == capture.snapshot.checkpoint && meta.generation == capture.generation, + "STALE_HELPER_CAPTURE" + ); + ensure!( + meta.agent_journal + .is_none_or(|id| id == decision.journal_id) + && decision.checkpoint >= meta.agent_checkpoint, + "AGENT_JOURNAL_ROLLBACK_OR_REPLACEMENT" + ); + validate_history( + &tx, + &self.binding, + &meta, + capture.generation, + &self.verifier, + )?; + if let Some(old)=tx.query_row("SELECT response,request,fingerprint FROM helper_sync_decisions WHERE decision_id=?1",[decision.decision_id.to_string()],|r|Ok((r.get::<_,Vec>(0)?,r.get::<_,Vec>(1)?,r.get::<_,String>(2)?))).optional()? { + ensure!(old.0==serde_json::to_vec(response)? && old.1==raw_request && meta.decision==Some(decision.decision_id) + && ledger_fingerprint(&tx)?==old.2,"HELPER_REPLAY_OR_LEDGER_CHANGED"); + let rows=load_rows(&tx,&self.binding,false, self.identity, &self.verifier)?; + let ready=decision.ready && decision.helper_ready && rows.iter().all(|row|definitive(row) + && decision.attempts.iter().any(|d|d.job_id==row.job_id && d.attempt_id==row.attempt_id + && d.outcome_state==row.state && d.outcome_sha256.as_deref()==Some(row.outcome_sha256.as_str()))) + && tx.query_row("SELECT count(*) FROM consumed WHERE result IS NULL",[],|r|r.get::<_,i64>(0))?==0; + let snapshot=Snapshot {journal_id:meta.journal_id,checkpoint:meta.checkpoint,previous_decision_id:meta.decision, + boot_id:current_boot,captured_at:capture.snapshot.captured_at,attempts:rows}; + tx.commit()?; + if ready {self.lease=Some(Lease {checkpoint:meta.checkpoint,decision_id:decision.decision_id,boot_id:current_boot, + generation:capture.generation,until:decision.valid_until,fingerprint:old.2});} + return Ok(Applied {snapshot,unlocked_grants:vec![],ready}); + } + ensure!( + meta.decision == capture.snapshot.previous_decision_id, + "HELPER_PREVIOUS_DECISION_CHANGED" + ); + ensure!( + ledger_fingerprint(&tx)? == capture.fingerprint, + "LOCAL_LEDGER_CHANGED_DURING_SYNC" + ); + let rows = load_rows(&tx, &self.binding, false, self.identity, &self.verifier)?; + ensure!( + serde_json::to_vec(&rows)? == serde_json::to_vec(&capture.snapshot.attempts)?, + "LOCAL_SNAPSHOT_CHANGED_DURING_SYNC" + ); + let summaries = unique_summaries(&request.attempts)?; + let mut decisions = BTreeMap::new(); + for d in &decision.attempts { + ensure!( + decisions.insert((d.job_id, d.attempt_id), d).is_none(), + "DUPLICATE_SIGNED_ATTEMPT" + ); + } + let known: BTreeSet<_> = rows.iter().map(|r| (r.job_id, r.attempt_id)).collect(); + for d in &decision.attempts { + ensure!( + d.execution_identity != self.identity.as_str() + || d.grant_id.is_none() + || known.contains(&(d.job_id, d.attempt_id)), + "SIGNED_ROOT_ATTEMPT_MISSING_LOCALLY" + ); + } + let mut unlocked = Vec::new(); + let mut safe = true; + for row in &rows { + let id = (row.job_id, row.attempt_id); + let local = summaries + .get(&id) + .context("ROOT_ATTEMPT_OMITTED_FROM_REQUEST")?; + let d = decisions + .get(&id) + .context("ROOT_ATTEMPT_OMITTED_FROM_DECISION")?; + validate_summary(row, local)?; + validate_decision(row, local, d)?; + if d.unlock { + let bytes = unlock_outcome(row, d, current_boot)?; + ensure!( + tx.execute( + "UPDATE consumed SET result=?2 WHERE grant_id=?1", + params![row.grant_id.to_string(), bytes] + )? == 1, + "ROOT_CONSUMPTION_MISSING" + ); + tx.execute( + "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", + params![row.grant_id.to_string(), bytes], + )?; + unlocked.push(row.grant_id); + } else { + safe &= definitive(row) + && d.outcome_state == row.state + && d.outcome_sha256.as_deref() == Some(row.outcome_sha256.as_str()); + // A definitive local state with an unresolved consumed lock is + // not globally ready merely because the server says ready. + let locked: bool = tx.query_row( + "SELECT result IS NULL FROM consumed WHERE grant_id=?1", + [row.grant_id.to_string()], + |r| r.get(0), + )?; + safe &= !locked; + } + } + let fault = recovery_reason(&decision.reason).then(|| decision.reason.clone()); + ensure!( + !decision.attempts.iter().any(|d| recovery_reason(&d.reason)), + "SIGNED_LEDGER_RECOVERY_REQUIRED" + ); + let after = load_rows(&tx, &self.binding, true, self.identity, &self.verifier)?; + let fingerprint = ledger_fingerprint(&tx)?; + tx.execute("INSERT INTO helper_sync_decisions(decision_id,checkpoint,response,request,fingerprint) VALUES(?1,?2,?3,?4,?5)",params![decision.decision_id.to_string(),decision.helper_checkpoint,serde_json::to_vec(response)?,raw_request,fingerprint])?; + tx.execute("UPDATE helper_sync_meta SET last_decision=?1,fault=COALESCE(fault,?2),agent_journal_id=?3,agent_checkpoint=?4 WHERE id=1",params![decision.decision_id.to_string(),fault,decision.journal_id.to_string(),decision.checkpoint])?; + tx.commit()?; + let ready = safe + && fault.is_none() + && decision.ready + && decision.helper_ready + && after.iter().all(definitive); + if ready { + self.lease = Some(Lease { + checkpoint: decision.helper_checkpoint, + decision_id: decision.decision_id, + boot_id: current_boot, + generation: capture.generation, + until: decision.valid_until, + fingerprint, + }); + } + Ok(Applied { + snapshot: Snapshot { + journal_id: meta.journal_id, + checkpoint: meta.checkpoint, + previous_decision_id: Some(decision.decision_id), + boot_id: current_boot, + captured_at: capture.snapshot.captured_at, + attempts: after, + }, + unlocked_grants: unlocked, + ready, + }) + } + /// Call under the same executor guard immediately BEFORE fresh grant + /// consumption. Once that operation changes the ledger, resync is required. + pub fn can_start( + &self, + db: &Connection, + guard: &impl ExecutorLedgerGuard, + generation: u64, + clock: &impl Clock, + boot: &impl BootSource, + ) -> bool { + let result = (|| -> Result { + guard.check(db)?; + let now = clock.now()?; + let Some(lease) = &self.lease else { + return Ok(false); + }; + let meta = metadata(db)?; + if meta.fault.is_some() + || meta.checkpoint != lease.checkpoint + || meta.decision != Some(lease.decision_id) + || generation != lease.generation + { + return Ok(false); + } + validate_history(db, &self.binding, &meta, generation, &self.verifier)?; + Ok(meta.fault.is_none() + && meta.checkpoint == lease.checkpoint + && meta.decision == Some(lease.decision_id) + && generation == lease.generation + && now < lease.until + && now >= self.born + && boot.current_boot()? == lease.boot_id + && ledger_fingerprint(db)? == lease.fingerprint + && db.query_row( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get::<_, i64>(0), + )? == 0) + })(); + if let Err(error) = &result { + persist_fault(db, error); + } + result.unwrap_or(false) + } +} + +fn pinned_key(binding: &Binding, verifier: &impl Ed25519Verifier) -> Result> { + let raw = hex::decode(&binding.public_key_hex)?; + ensure!(raw.len() == 32, "PINNED_HELPER_KEY_SIZE"); + ensure!(verifier.validate_key(&raw)?, "PINNED_HELPER_KEY_INVALID"); + Ok(raw) +} +fn verify( + verifier: &impl Ed25519Verifier, + binding: &Binding, + response: &SignedResponse, + now: DateTime, +) -> Result { + ensure!( + response.signature.len() == 64 && response.decision_bytes.len() <= 512 * 1024, + "SYNC_SIGNATURE_SIZE" + ); + let raw = pinned_key(binding, verifier)?; + ensure!( + verifier.verify(&raw, &response.decision_bytes, &response.signature)?, + "INVALID_SYNC_SIGNATURE" + ); + let d: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?; + ensure!( + d.request_hash.len() == 64 + && d.request_hash + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)), + "SIGNED_REQUEST_HASH_MISSING_OR_INVALID" + ); + i64::try_from(d.checkpoint)?; + i64::try_from(d.helper_checkpoint)?; + i64::try_from(d.connection_generation)?; + for attempt in &d.attempts { + i64::try_from(attempt.revision)?; + } + ensure!( + d.valid_profile(now) + && d.valid_until - d.issued_at <= chrono::Duration::seconds(60) + && d.key_id == hash(&raw) + && d.tenant_id == binding.tenant_id + && d.asset_id == binding.asset_id + && d.agent_id == binding.agent_id + && d.enrollment_generation == binding.enrollment_generation + && d.assignment_version == binding.assignment_version, + "HELPER_DECISION_PROFILE_OR_IDENTITY" + ); + Ok(d) +} +struct Meta { + journal_id: Uuid, + checkpoint: u64, + generation: u64, + decision: Option, + fault: Option, + agent_journal: Option, + agent_checkpoint: u64, +} +fn initialize(db: &Connection, binding: &Binding) -> Result<()> { + let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?; + ensure!(check == "ok", "ROOT_LEDGER_INTEGRITY"); + db.execute_batch("PRAGMA synchronous=FULL; + CREATE TABLE IF NOT EXISTS helper_sync_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,binding_sha256 TEXT NOT NULL,last_decision TEXT,fault TEXT,agent_journal_id TEXT,agent_checkpoint INTEGER NOT NULL DEFAULT 0); + CREATE TABLE IF NOT EXISTS helper_sync_rows(grant_id TEXT PRIMARY KEY,revision INTEGER NOT NULL,fingerprint TEXT NOT NULL); + CREATE TABLE IF NOT EXISTS helper_sync_captures(checkpoint INTEGER PRIMARY KEY,snapshot BLOB NOT NULL,fingerprint TEXT NOT NULL,captured_at TEXT NOT NULL); + CREATE TABLE IF NOT EXISTS helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL);")?; + let binding_hash = hash(&serde_json::to_vec(binding)?); + db.execute( + "INSERT OR IGNORE INTO helper_sync_meta(id,journal_id,binding_sha256) VALUES(1,?1,?2)", + params![Uuid::new_v4().to_string(), binding_hash], + )?; + let old: String = db.query_row( + "SELECT binding_sha256 FROM helper_sync_meta WHERE id=1", + [], + |r| r.get(0), + )?; + ensure!( + old == binding_hash, + "INDEPENDENT_HELPER_BINDING_OR_KEY_CHANGED" + ); + Ok(()) +} +fn metadata(db: &Connection) -> Result { + type MetaRow = ( + String, + u64, + u64, + Option, + Option, + Option, + u64, + ); + let (id,checkpoint,generation,decision,fault,agent,agent_checkpoint):MetaRow=db.query_row("SELECT journal_id,checkpoint,generation,last_decision,fault,agent_journal_id,agent_checkpoint FROM helper_sync_meta WHERE id=1",[],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?,r.get(5)?,r.get(6)?)))?; + Ok(Meta { + journal_id: Uuid::parse_str(&id)?, + checkpoint, + generation, + decision: decision.map(|s| Uuid::parse_str(&s)).transpose()?, + fault, + agent_journal: agent.map(|s| Uuid::parse_str(&s)).transpose()?, + agent_checkpoint, + }) +} +fn validate_history( + db: &Connection, + binding: &Binding, + meta: &Meta, + generation: u64, + verifier: &impl Ed25519Verifier, +) -> Result<()> { + let highest: u64 = db.query_row( + "SELECT COALESCE(MAX(checkpoint),0) FROM helper_sync_captures", + [], + |r| r.get(0), + )?; + ensure!( + highest == meta.checkpoint, + "HELPER_CAPTURE_HISTORY_ROLLBACK" + ); + let decision_count: u64 = + db.query_row("SELECT count(*) FROM helper_sync_decisions", [], |r| { + r.get(0) + })?; + if let Some(id) = meta.decision { + let stored: Option> = db + .query_row( + "SELECT response FROM helper_sync_decisions WHERE decision_id=?1", + [id.to_string()], + |r| r.get(0), + ) + .optional()?; + let response: SignedResponse = + serde_json::from_slice(&stored.context("HELPER_DECISION_HISTORY_MISSING_LOCALLY")?)?; + // Historical already-applied evidence is checked at its ORIGINAL issue + // time; it never creates a lease or bypasses the fresh-process capture. + let parsed: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?; + let previous = verify(verifier, binding, &response, parsed.issued_at)?; + let highest_decision: u64 = db.query_row( + "SELECT COALESCE(MAX(checkpoint),0) FROM helper_sync_decisions", + [], + |r| r.get(0), + )?; + ensure!( + previous.decision_id == id + && previous.helper_journal_id == Some(meta.journal_id) + && previous.helper_checkpoint == highest_decision + && previous.helper_checkpoint <= meta.checkpoint + && generation >= previous.connection_generation + && meta.agent_journal == Some(previous.journal_id) + && meta.agent_checkpoint >= previous.checkpoint, + "HELPER_DECISION_HISTORY_ROLLBACK" + ); + } else { + ensure!( + decision_count == 0 && meta.agent_journal.is_none() && meta.agent_checkpoint == 0, + "HELPER_METADATA_HISTORY_ROLLBACK" + ); + } + Ok(()) +} +/// Bootstrap-only historical verification; this never creates a lease. +pub fn verify_checkpoint_repair_history( + db: &Connection, + binding: &Binding, + links: Option<( + &rmm_domain::checkpoint_repair::Receipt, + &rmm_domain::checkpoint_repair::Evidence, + )>, +) -> Result<()> { + verify_checkpoint_repair_history_with( + db, + binding, + links, + HelperIdentity::Root, + &DefaultVerifier::default(), + ) +} +pub fn verify_checkpoint_repair_history_with( + db: &Connection, + binding: &Binding, + links: Option<( + &rmm_domain::checkpoint_repair::Receipt, + &rmm_domain::checkpoint_repair::Evidence, + )>, + identity: HelperIdentity, + verifier: &impl Ed25519Verifier, +) -> Result<()> { + let meta = metadata(db)?; + validate_history(db, binding, &meta, meta.generation, verifier)?; + let mut statement = db.prepare( + "SELECT response,request,checkpoint FROM helper_sync_decisions ORDER BY checkpoint", + )?; + let entries = statement + .query_map([], |r| { + Ok(( + r.get::<_, Vec>(0)?, + r.get::<_, Vec>(1)?, + r.get::<_, u64>(2)?, + )) + })? + .collect::>>()?; + ensure!(entries.len() <= 100000, "REPAIR_HISTORY_LIMIT"); + let mut previous = None; + let mut previous_checkpoint = 0; + let mut previous_agent_checkpoint = 0; + for (signed, raw, checkpoint) in entries { + let response: SignedResponse = serde_json::from_slice(&signed)?; + let parsed: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?; + let decision = verify(verifier, binding, &response, parsed.issued_at)?; + let request = RequestView::decode(&raw)?; + let captured: Vec = db.query_row( + "SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?1", + [checkpoint], + |r| r.get(0), + )?; + let capture: Snapshot = serde_json::from_slice(&captured)?; + ensure!( + decision.helper_journal_id == Some(meta.journal_id) + && decision.helper_checkpoint == checkpoint + && request.helper_journal_id == meta.journal_id.to_string() + && request.helper_checkpoint == checkpoint + && request.assignment_version == binding.assignment_version + && request.journal_complete + && decision.request_hash == hash(&raw) + && decision.request_id.to_string() == request.request_id + && decision.journal_id.to_string() == request.journal_id + && decision.checkpoint == request.checkpoint + && capture.journal_id == meta.journal_id + && capture.checkpoint == checkpoint + && capture.boot_id == decision.boot_id + && capture.captured_at <= decision.issued_at + && capture.previous_decision_id == previous + && checkpoint > previous_checkpoint + && decision.checkpoint >= previous_agent_checkpoint + && decision.connection_generation <= meta.generation + && request.helper_previous_decision_id + == capture + .previous_decision_id + .map(|v| v.to_string()) + .unwrap_or_default(), + "REPAIR_HELPER_HISTORY_BINDING" + ); + previous = Some(decision.decision_id); + previous_checkpoint = checkpoint; + previous_agent_checkpoint = decision.checkpoint; + } + let _ = load_rows(db, binding, false, identity, verifier)?; + if let Some((r, e)) = links { + verify_checkpoint_repair_links(db, binding, r, e, verifier)?; + } + Ok(()) +} +fn verify_checkpoint_repair_links( + db: &Connection, + binding: &Binding, + r: &rmm_domain::checkpoint_repair::Receipt, + e: &rmm_domain::checkpoint_repair::Evidence, + verifier: &impl Ed25519Verifier, +) -> Result<()> { + let x = RequestView::decode(&e.uncommitted_request)?; + let y = RequestView::decode(&e.rejected_request)?; + let a: ReconcileDecisionPayload = + serde_json::from_slice(&e.previous_valid_response.decision_bytes)?; + let a = verify( + verifier, + binding, + &SignedResponse { + decision_bytes: e.previous_valid_response.decision_bytes.clone(), + signature: e.previous_valid_response.signature.clone(), + }, + a.issued_at, + )?; + let old: Vec = db.query_row( + "SELECT response FROM helper_sync_decisions WHERE decision_id=?1", + [a.decision_id.to_string()], + |v| v.get(0), + )?; + let old: SignedResponse = serde_json::from_slice(&old)?; + ensure!( + old.decision_bytes == e.previous_valid_response.decision_bytes + && old.signature == e.previous_valid_response.signature + && a.decision_id == r.previous_valid_decision_id + && hash(&old.decision_bytes) == r.previous_valid_decision_sha256 + && a.reason == "ATTEMPT_RECONCILIATION_REQUIRED" + && !a.ready + && !a.helper_ready + && x.request_id == r.uncommitted_request_id.to_string() + && hash(&e.uncommitted_request) == r.uncommitted_request_sha256 + && y.request_id == r.rejected_request_id.to_string() + && hash(&e.rejected_request) == r.rejected_request_sha256 + && x.previous_decision_id == a.decision_id.to_string() + && x.previous_request_id.is_empty() + && x.helper_previous_decision_id == a.decision_id.to_string() + && y.helper_previous_decision_id == a.decision_id.to_string() + && y.previous_decision_id.is_empty() + && y.previous_request_id == x.request_id + && x.journal_id == r.agent_journal_id.to_string() + && y.journal_id == x.journal_id + && a.journal_id == r.agent_journal_id + && x.helper_journal_id == r.helper_journal_id.to_string() + && y.helper_journal_id == x.helper_journal_id + && a.helper_journal_id == Some(r.helper_journal_id) + && a.checkpoint < x.checkpoint + && x.checkpoint < y.checkpoint + && y.checkpoint == r.agent_checkpoint + && a.helper_checkpoint < x.helper_checkpoint + && x.helper_checkpoint < y.helper_checkpoint + && y.helper_checkpoint == r.helper_checkpoint + && x.assignment_version == binding.assignment_version + && y.assignment_version == binding.assignment_version + && x.journal_complete + && y.journal_complete, + "REPAIR_EXACT_UNSENT_CHAIN_REQUIRED" + ); + Ok(()) +} +fn persist_fault(db: &Connection, error: &anyhow::Error) { + let text = error.to_string(); + if [ + "OMITTED", + "ROLLBACK", + "LEDGER_CHANGED", + "SNAPSHOT_CHANGED", + "MISSING_LOCALLY", + "ROOT_CONSUMPTION_MISSING", + "UNKNOWN_LOCAL_STATE", + "RECOVERY_REQUIRED", + "BINDING_OR_KEY_CHANGED", + "LOCAL_START_MISSING", + "LOCAL_REQUEST_BINDING", + "LOCAL_OUTCOME_CONFLICT", + "LOCAL_SUCCESS_PROOF_MISSING", + "SIGNED_REQUEST_HASH_MISSING_OR_INVALID", + "ROOT_SIGNED_ATTEMPT_BINDING", + ] + .iter() + .any(|s| text.contains(s)) + { + let _ = db.execute( + "UPDATE helper_sync_meta SET fault=COALESCE(fault,?1) WHERE id=1", + [text], + ); + } +} +fn recovery_reason(reason: &str) -> bool { + reason.contains("ROLLBACK") + || reason.contains("RECOVERY_REQUIRED") + || matches!( + reason, + "MISSING_SERVER_JOB" + | "ATTEMPT_BINDING_MISMATCH" + | "ASSIGNMENT_MISMATCH" + | "JOURNAL_SEQUENCE_REGRESSION" + | "SERVER_CHECKPOINT_MISSING" + | "CHECKPOINT_CHAIN_MISMATCH" + | "JOURNAL_HISTORY_MISSING" + | "LOCAL_ATTEMPT_MISSING" + ) +} +type LedgerRow = ( + String, + String, + u64, + String, + String, + Option>, + Option>, + Option>, +); +fn ledger_rows(db: &Connection) -> Result> { + let mut statement=db.prepare("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 ON e.grant_id=c.grant_id ORDER BY c.grant_id")?; + Ok(statement + .query_map([], |r| { + Ok(( + r.get(0)?, + r.get(1)?, + r.get(2)?, + r.get(3)?, + r.get(4)?, + r.get(5)?, + r.get(6)?, + r.get(7)?, + )) + })? + .collect::>()?) +} +fn ledger_fingerprint(db: &Connection) -> Result { + Ok(hash(&serde_json::to_vec(&ledger_rows(db)?)?)) +} +fn canonical(bytes: &[u8]) -> Result> { + let o: ReconciledOutcome = serde_json::from_slice(bytes)?; + Ok(canonical_outcome_bytes(&o)?) +} +fn load_rows( + db: &Connection, + binding: &Binding, + refresh: bool, + identity: HelperIdentity, + verifier: &impl Ed25519Verifier, +) -> Result> { + let mut result = Vec::new(); + for (grant, agent, generation, job_id, attempt_id, finished, start, last) in ledger_rows(db)? { + let start = start.context("LOCAL_START_MISSING_POSSIBLE_EFFECT")?; + let start: serde_json::Value = serde_json::from_slice(&start)?; + let original_boot_id = Uuid::parse_str( + start["previous_boot_id"] + .as_str() + .context("LOCAL_ORIGINAL_BOOT_MISSING")?, + )?; + let request: Request = serde_json::from_value(start["request"].clone())?; + let permit: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes)?; + // Historical verification checks the original valid grant window, NOT + // authority to start now. Root consumption remains permanently recorded. + let (job, verified) = binding.verify_with(&request, permit.not_before, verifier)?; + ensure!( + verified == permit + && job.execution_identity == identity.as_str() + && grant == permit.grant_id.to_string() + && agent == binding.agent_id.to_string() + && generation == binding.enrollment_generation + && job_id == job.job_id.to_string() + && attempt_id == job.attempt_id.to_string(), + "LOCAL_REQUEST_BINDING" + ); + if let (Some(a), Some(b)) = (&finished, &last) { + ensure!(canonical(a)? == canonical(b)?, "LOCAL_OUTCOME_CONFLICT"); + } + let outcome_bytes = last + .as_deref() + .or(finished.as_deref()) + .map(canonical) + .transpose()? + .unwrap_or_default(); + ensure!(outcome_bytes.len() <= 512 * 1024, "LOCAL_OUTCOME_SIZE"); + let state = if outcome_bytes.is_empty() { + "unknown".to_string() + } else { + let o: ReconciledOutcome = serde_json::from_slice(&outcome_bytes)?; + o.state + }; + ensure!( + matches!( + state.as_str(), + "unknown" | "awaiting_reboot" | "succeeded" | "failed" | "cancelled" | "expired" + ), + "UNKNOWN_LOCAL_STATE" + ); + ensure!(!original_boot_id.is_nil(), "LOCAL_ORIGINAL_BOOT_MISSING"); + if state == "succeeded" { + let o: ReconciledOutcome = serde_json::from_slice(&outcome_bytes)?; + ensure!( + o.evidence["postconditions_passed"] == true, + "LOCAL_SUCCESS_PROOF_MISSING" + ); + if let Action::RunScript { + expected_stdout, .. + } = &job.action + { + ensure!( + o.exit_code == Some(0) + && expected_stdout + .as_ref() + .is_none_or(|s| !o.output_truncated && o.stdout == s.as_bytes()), + "LOCAL_SUCCESS_PROOF_MISSING" + ); + } + } + let mut row = LocalAttempt { + job_id: job.job_id, + attempt_id: job.attempt_id, + job_payload_hash: hash(&request.job_bytes), + grant_id: permit.grant_id, + authorization_id: permit.authorization_id, + plan_hash: permit.plan_hash, + action: job.action.name().into(), + execution_identity: job.execution_identity, + revision: 0, + original_boot_id, + consumption: "consumed".into(), + state, + outcome_sha256: if outcome_bytes.is_empty() { + String::new() + } else { + hash(&outcome_bytes) + }, + outcome_bytes, + original_request_sha256: hash(&serde_json::to_vec(&request)?), + }; + let fingerprint = hash(&serde_json::to_vec(&row)?); + let old: Option<(u64, String)> = db + .query_row( + "SELECT revision,fingerprint FROM helper_sync_rows WHERE grant_id=?1", + [&grant], + |r| Ok((r.get(0)?, r.get(1)?)), + ) + .optional()?; + row.revision = match old { + Some((revision, old)) => { + if old == fingerprint { + revision + } else { + ensure!(refresh, "ROOT_ROW_CHANGED_WITHOUT_SNAPSHOT"); + revision.checked_add(1).context("REVISION_OVERFLOW")? + } + } + None => { + ensure!(refresh, "ROOT_ROW_MISSING_WITHOUT_SNAPSHOT"); + 1 + } + }; + i64::try_from(row.revision)?; + if refresh { + db.execute("INSERT INTO helper_sync_rows(grant_id,revision,fingerprint) VALUES(?1,?2,?3) ON CONFLICT(grant_id) DO UPDATE SET revision=excluded.revision,fingerprint=excluded.fingerprint",params![grant,row.revision,fingerprint])?; + } + result.push(row); + } + let retained: u64 = db.query_row("SELECT count(*) FROM helper_sync_rows", [], |r| r.get(0))?; + ensure!( + retained == result.len() as u64, + "ROOT_CONSUMPTION_MISSING_LOCALLY" + ); + Ok(result) +} +fn definitive(row: &LocalAttempt) -> bool { + matches!( + row.state.as_str(), + "succeeded" | "failed" | "cancelled" | "expired" + ) && !row.outcome_bytes.is_empty() +} +fn validate_summary(row: &LocalAttempt, a: &AttemptView) -> Result<()> { + ensure!( + a.job_payload_hash == row.job_payload_hash + && a.grant_id == row.grant_id.to_string() + && a.consumption == "consumed" + && a.state == row.state + && a.revision >= row.revision + && a.original_boot_id == row.original_boot_id.to_string() + && a.outcome_sha256 == row.outcome_sha256 + && (a.outcome_bytes.is_empty() || a.outcome_bytes == row.outcome_bytes), + "ROOT_SNAPSHOT_REQUEST_MISMATCH" + ); + Ok(()) +} +fn validate_decision(row: &LocalAttempt, a: &AttemptView, d: &AttemptDecision) -> Result<()> { + ensure!( + d.job_payload_hash == row.job_payload_hash + && d.grant_id == Some(row.grant_id) + && d.authorization_id == Some(row.authorization_id) + && d.plan_hash.as_deref() == Some(row.plan_hash.as_str()) + && d.action == row.action + && d.execution_identity == row.execution_identity + && d.original_boot_id == Some(row.original_boot_id) + && (d.revision >= a.revision || (!d.unlock && d.outcome_state == "unknown")) + && d.previous_outcome_sha256.as_deref() + == (!row.outcome_sha256.is_empty()).then_some(row.outcome_sha256.as_str()), + "ROOT_SIGNED_ATTEMPT_BINDING" + ); + ensure!( + !d.unlock + || matches!( + d.outcome_state.as_str(), + "succeeded" | "failed" | "cancelled" | "expired" + ), + "UNKNOWN_CANNOT_UNLOCK" + ); + Ok(()) +} +fn unlock_outcome(row: &LocalAttempt, d: &AttemptDecision, current_boot: Uuid) -> Result> { + let bytes = if d.outcome_bytes.is_empty() { + row.outcome_bytes.clone() + } else { + d.outcome_bytes.clone() + }; + let o: ReconciledOutcome = serde_json::from_slice(&bytes)?; + ensure!( + canonical_outcome_bytes(&o)? == bytes + && d.outcome_sha256.as_deref() == Some(hash(&bytes).as_str()) + && o.state == d.outcome_state, + "ROOT_CANONICAL_UNLOCK_OUTCOME" + ); + if bytes != row.outcome_bytes { + ensure!( + row.action == "reboot" + && matches!(row.state.as_str(), "unknown" | "awaiting_reboot") + && o.state == "succeeded" + && row.original_boot_id != current_boot + && o.evidence["postconditions_passed"] == true + && o.evidence["reconciled_after_reboot"] == true + && o.evidence["new_boot_id"].as_str() == Some(current_boot.to_string().as_str()) + && o.evidence["inventory_message_id"] + .as_str() + .is_some_and(|s| Uuid::parse_str(s).is_ok_and(|id| !id.is_nil())), + "ROOT_REBOOT_NATIVE_BOOT_PROOF_REQUIRED" + ); + } else { + ensure!(definitive(row), "UNKNOWN_LOCAL_EFFECT_CANNOT_UNLOCK"); + if o.state == "succeeded" { + ensure!( + o.evidence["postconditions_passed"] == true, + "ROOT_POSTCONDITION_MISSING" + ); + } + if matches!(o.state.as_str(), "cancelled" | "expired") { + ensure!( + o.evidence["not_started"] == true + || (o.state == "cancelled" && o.evidence["abort_confirmed"] == true), + "ROOT_ABORT_NOT_PROVEN" + ); + } + if o.state == "failed" { + ensure!(!o.error_code.is_empty(), "ROOT_FAILURE_NOT_PROVEN"); + } + } + Ok(bytes) +} + +// The independent helper verification deliberately retains this small +// bounded parser reads the EXISTING SyncRequest tags (1..12) and SyncAttempt +// tags (1..13), not a parallel public protocol. Glue passes the actual canonical +// prost bytes. Unknown/duplicate fields, wrong wire types and overlong varints +// fail closed. Nested JobResult/Envelope are not trusted as root outcome proof. +#[derive(Default)] +struct RequestView { + request_id: String, + journal_id: String, + checkpoint: u64, + assignment_version: u64, + attempts: Vec, + journal_complete: bool, + helper_journal_id: String, + helper_checkpoint: u64, + helper_previous_decision_id: String, + previous_decision_id: String, + previous_request_id: String, +} +#[derive(Default)] +struct AttemptView { + job_id: String, + attempt_id: String, + job_payload_hash: String, + grant_id: String, + consumption: String, + state: String, + revision: u64, + original_boot_id: String, + outcome_sha256: String, + outcome_bytes: Vec, +} +enum Field<'a> { + Number(u64), + Bytes(&'a [u8]), +} +fn varint(bytes: &[u8], offset: &mut usize) -> Result { + let mut n = 0u64; + for i in 0..10 { + let b = *bytes.get(*offset).context("PROTOBUF_TRUNCATED")?; + *offset += 1; + ensure!(i != 9 || b <= 1, "PROTOBUF_VARINT_OVERFLOW"); + n |= u64::from(b & 127) << (7 * i); + if b & 128 == 0 { + ensure!(i == 0 || b != 0, "PROTOBUF_NONCANONICAL_VARINT"); + return Ok(n); + } + } + anyhow::bail!("PROTOBUF_VARINT_OVERFLOW") +} +fn fields(bytes: &[u8]) -> Result)>> { + let mut offset = 0; + let mut result = Vec::new(); + while offset < bytes.len() { + ensure!(result.len() < 2048, "PROTOBUF_FIELD_LIMIT"); + let tag = varint(bytes, &mut offset)?; + let number = u32::try_from(tag >> 3)?; + ensure!(number > 0, "PROTOBUF_ZERO_TAG"); + let value = match tag & 7 { + 0 => Field::Number(varint(bytes, &mut offset)?), + 2 => { + let len = usize::try_from(varint(bytes, &mut offset)?)?; + let end = offset + .checked_add(len) + .context("PROTOBUF_LENGTH_OVERFLOW")?; + let value = bytes.get(offset..end).context("PROTOBUF_TRUNCATED")?; + offset = end; + Field::Bytes(value) + } + _ => anyhow::bail!("PROTOBUF_WIRE_TYPE"), + }; + result.push((number, value)); + } + Ok(result) +} +fn text(field: Field<'_>) -> Result { + match field { + Field::Bytes(b) => { + ensure!(b.len() <= 1024, "PROTOBUF_STRING_SIZE"); + Ok(std::str::from_utf8(b)?.to_string()) + } + _ => anyhow::bail!("PROTOBUF_EXPECTED_STRING"), + } +} +fn number(field: Field<'_>) -> Result { + match field { + Field::Number(n) => Ok(n), + _ => anyhow::bail!("PROTOBUF_EXPECTED_NUMBER"), + } +} +fn bytes(field: Field<'_>) -> Result> { + match field { + Field::Bytes(b) => Ok(b.to_vec()), + _ => anyhow::bail!("PROTOBUF_EXPECTED_BYTES"), + } +} +impl RequestView { + fn decode(raw: &[u8]) -> Result { + ensure!(raw.len() <= 1024 * 1024, "SYNC_REQUEST_SIZE"); + let mut r = Self::default(); + let mut seen = BTreeSet::new(); + let mut receipts = 0usize; + for (tag, value) in fields(raw)? { + ensure!( + (1..=12).contains(&tag) && (matches!(tag, 6 | 7) || seen.insert(tag)), + "PROTOBUF_UNKNOWN_OR_DUPLICATE_FIELD" + ); + match tag { + 1 => r.request_id = text(value)?, + 2 => r.journal_id = text(value)?, + 3 => r.checkpoint = number(value)?, + 4 | 12 => { + let s = text(value)?; + if !s.is_empty() { + Uuid::parse_str(&s)?; + } + if tag == 4 { + r.previous_decision_id = s; + } else { + r.previous_request_id = s; + } + } + 5 => r.assignment_version = number(value)?, + 6 => { + ensure!(r.attempts.len() < 256, "SYNC_ATTEMPT_LIMIT"); + r.attempts.push(AttemptView::decode(&bytes(value)?)?); + } + 7 => { + receipts += 1; + ensure!(receipts <= 256, "SYNC_RECEIPT_LIMIT"); + let s = text(value)?; + ensure!(!Uuid::parse_str(&s)?.is_nil(), "SYNC_RECEIPT_ID"); + } + 8 => { + let n = number(value)?; + ensure!(n <= 1, "PROTOBUF_BOOL"); + r.journal_complete = n == 1; + } + 9 => r.helper_journal_id = text(value)?, + 10 => r.helper_checkpoint = number(value)?, + 11 => r.helper_previous_decision_id = text(value)?, + _ => unreachable!(), + } + } + ensure!( + !Uuid::parse_str(&r.request_id)?.is_nil() && !Uuid::parse_str(&r.journal_id)?.is_nil(), + "SYNC_REQUEST_IDENTITIES" + ); + i64::try_from(r.checkpoint)?; + i64::try_from(r.helper_checkpoint)?; + Ok(r) + } +} +impl AttemptView { + fn decode(raw: &[u8]) -> Result { + let mut r = Self::default(); + let mut seen = BTreeSet::new(); + for (tag, value) in fields(raw)? { + ensure!( + (1..=13).contains(&tag) && seen.insert(tag), + "PROTOBUF_ATTEMPT_FIELD" + ); + match tag { + 1 => r.job_id = text(value)?, + 2 => r.attempt_id = text(value)?, + 3 => r.job_payload_hash = text(value)?, + 4 => r.grant_id = text(value)?, + 5 => r.consumption = text(value)?, + 6 => r.state = text(value)?, + 7 => r.revision = number(value)?, + 8 => r.original_boot_id = text(value)?, + 9 | 13 => { + ensure!(bytes(value)?.len() <= 1024 * 1024, "SYNC_RESULT_SIZE"); + } + 10 => { + let _ = text(value)?; + } + 11 => r.outcome_sha256 = text(value)?, + 12 => { + r.outcome_bytes = bytes(value)?; + ensure!(r.outcome_bytes.len() <= 512 * 1024, "SYNC_OUTCOME_SIZE"); + } + _ => unreachable!(), + } + } + i64::try_from(r.revision)?; + Ok(r) + } +} +fn unique_summaries(rows: &[AttemptView]) -> Result> { + let mut map = BTreeMap::new(); + for row in rows { + let id = ( + Uuid::parse_str(&row.job_id)?, + Uuid::parse_str(&row.attempt_id)?, + ); + ensure!( + !id.0.is_nil() && !id.1.is_nil() && map.insert(id, row).is_none(), + "DUPLICATE_REQUEST_ATTEMPT" + ); + } + Ok(map) +} diff --git a/crates/agent-core/src/lib.rs b/crates/agent-core/src/lib.rs index f25a6b4..eaa79ca 100644 --- a/crates/agent-core/src/lib.rs +++ b/crates/agent-core/src/lib.rs @@ -4,6 +4,7 @@ //! supply a qualified clock. A successful signature is never UI approval itself. pub mod auth; pub mod crypto; +pub mod helper_reconcile; pub mod jobs; pub mod ledger; pub mod reconcile; diff --git a/crates/agent-core/src/reconcile.rs b/crates/agent-core/src/reconcile.rs index d3b490c..204c85a 100644 --- a/crates/agent-core/src/reconcile.rs +++ b/crates/agent-core/src/reconcile.rs @@ -3,6 +3,7 @@ //! Call only while holding the existing agent.lock. HelperSnapshot must come //! from the independently authenticated root-helper IPC, never from the server. use crate::auth::Binding; +pub use crate::auth::HelperIdentity; use crate::crypto::{DefaultVerifier, Ed25519Verifier}; use anyhow::{Context, Result, ensure}; use chrono::{DateTime, Utc}; @@ -69,6 +70,7 @@ struct Gate { pub struct ClientSync { binding: Binding, verifier: V, + helper_identity: HelperIdentity, pending: Option, channel: Option, gate: Option, @@ -84,11 +86,18 @@ impl ClientSync { Self { binding, verifier, + helper_identity: HelperIdentity::Root, pending: None, channel: None, gate: None, } } + /// Configure one explicit helper identity; changing it discards all leases. + pub fn with_helper_identity(mut self, identity: HelperIdentity) -> Self { + self.close(); + self.helper_identity = identity; + self + } /// Invoke on every authenticated/fenced ServerHello or transport loss. pub fn close(&mut self) { self.gate = None; @@ -204,7 +213,15 @@ impl ClientSync { ensure!(meta.helper_id.is_none(), "HELPER_JOURNAL_DISAPPEARED"); } if let Some(h) = helper { - ingest_helper(&tx, ledger, &self.binding, &channel, h, now)?; + ingest_helper( + &tx, + ledger, + &self.binding, + &channel, + h, + now, + self.helper_identity, + )?; } archive_events(&tx)?; tx.execute( @@ -884,6 +901,7 @@ fn ingest_helper( channel: &Channel, helper: &HelperSnapshot, now: DateTime, + identity: HelperIdentity, ) -> Result<()> { let mut statement = db.prepare("SELECT job,attempt,offer FROM job_attempts")?; for row in statement.query_map([], |r| { @@ -895,7 +913,7 @@ fn ingest_helper( })? { let (job, attempt, bytes) = row?; let offer = JobOffer::decode(bytes.as_slice())?; - if binding.job(&offer.payload_bytes)?.execution_identity == "root" { + if binding.job(&offer.payload_bytes)?.execution_identity == identity.as_str() { let consumed: bool = ledger.query_row( "SELECT EXISTS(SELECT 1 FROM consumed WHERE job=?1 AND attempt=?2)", params![job, attempt], @@ -928,7 +946,7 @@ fn ingest_helper( let offer = JobOffer::decode(bytes.as_slice())?; let job = binding.job(&offer.payload_bytes)?; ensure!( - job.execution_identity == "root" + job.execution_identity == identity.as_str() && h.job_payload_hash == offer.job_payload_hash && (grant.is_empty() || grant == h.grant_id), "HELPER_ATTEMPT_BINDING" diff --git a/crates/agent-core/tests/helper_reconcile.rs b/crates/agent-core/tests/helper_reconcile.rs new file mode 100644 index 0000000..b4e16fd --- /dev/null +++ b/crates/agent-core/tests/helper_reconcile.rs @@ -0,0 +1,993 @@ +//! Existing helper reconciliation semantic cases with portable test-only guard, +//! boot and signing adapters. Does not qualify OS locks, file IDs, IPC or SYSTEM. +use rmm_agent_core::helper_reconcile as reconcile; + +use chrono::{DateTime, Duration, Utc}; +use ed25519_dalek::{Signer, SigningKey}; +use reconcile::*; +use rmm_agent_core::auth::{Binding, HelperIdentity, Request}; +use rmm_domain::{jobs::*, reconciliation::*}; +use rusqlite::{Connection, params}; +use std::{fs, path::PathBuf}; +use uuid::Uuid; + +/// Test-only guard: identifies our fixture DB, not an OS lock qualification. +struct ExecutorGuard { + directory: PathBuf, +} +impl ExecutorLedgerGuard for ExecutorGuard { + fn check(&self, db: &Connection) -> anyhow::Result<()> { + let path: String = db.query_row( + "SELECT file FROM pragma_database_list WHERE name='main'", + [], + |r| r.get(0), + )?; + anyhow::ensure!( + PathBuf::from(path).canonicalize()? + == self.directory.join("consumption.sqlite").canonicalize()?, + "NOT_THE_SHARED_EXECUTOR_LEDGER" + ); + Ok(()) + } +} + +struct TestClock { + now: DateTime, + trusted: bool, +} +impl Clock for TestClock { + fn now(&self) -> anyhow::Result> { + anyhow::ensure!(self.trusted, "CLOCK_UNTRUSTED"); + Ok(self.now) + } +} +struct TestBoot(Uuid); +impl BootSource for TestBoot { + fn current_boot(&self) -> anyhow::Result { + Ok(self.0) + } +} +struct Fixture { + dir: PathBuf, + db: Connection, + guard: ExecutorGuard, + binding: Binding, + key: SigningKey, + clock: TestClock, + boot: TestBoot, + agent_journal: Uuid, + identity: HelperIdentity, +} + +#[test] +fn checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor() { + let mut f = Fixture::new(); + let mut sync = f.sync(); + let first = f.snapshot(&mut sync, 7); + let request = f.request(&first); + let d = f.decision(&first, &request, 7, false); + let signed = f.sign(&d); + sync.apply(&mut f.db, &f.guard, &signed, &request, &f.clock, &f.boot) + .unwrap(); + f.clock.now += Duration::seconds(1); + let second = f.snapshot(&mut sync, 7); + let request = f.request(&second); + let d = f.decision(&second, &request, 7, false); + let signed = f.sign(&d); + sync.apply(&mut f.db, &f.guard, &signed, &request, &f.clock, &f.boot) + .unwrap(); + verify_checkpoint_repair_history(&f.db, &f.binding, None).unwrap(); + f.db.execute( + "DELETE FROM helper_sync_decisions WHERE checkpoint=?1", + [first.checkpoint], + ) + .unwrap(); + assert!(verify_checkpoint_repair_history(&f.db, &f.binding, None).is_err()); +} +impl Drop for Fixture { + fn drop(&mut self) { + drop(std::mem::replace( + &mut self.db, + Connection::open_in_memory().unwrap(), + )); + let _ = fs::remove_dir_all(&self.dir); + } +} +impl Fixture { + fn new() -> Self { + let dir = std::env::temp_dir().join(format!("helper-sync-{}", Uuid::new_v4())); + fs::create_dir(&dir).unwrap(); + let guard = ExecutorGuard { + directory: dir.clone(), + }; + let db = Connection::open(dir.join("consumption.sqlite")).unwrap(); + rmm_agent_core::ledger::initialize_ledger(&db).unwrap(); + let key = SigningKey::from_bytes(&[42; 32]); + let binding = Binding { + tenant_id: Uuid::new_v4(), + asset_id: Uuid::new_v4(), + agent_id: Uuid::new_v4(), + enrollment_generation: 1, + assignment_version: 3, + public_key_hex: hex::encode(key.verifying_key().to_bytes()), + }; + let boot = TestBoot(Uuid::from_u128(1234)); + Self { + dir, + db, + guard, + binding, + key, + clock: TestClock { + now: Utc::now(), + trusted: true, + }, + boot, + agent_journal: Uuid::new_v4(), + identity: HelperIdentity::Root, + } + } + fn sync(&self) -> HelperSync { + HelperSync::new(self.binding.clone(), &self.clock) + .unwrap() + .with_helper_identity(self.identity) + } + fn snapshot(&mut self, sync: &mut HelperSync, generation: u64) -> Snapshot { + sync.snapshot( + &mut self.db, + &self.guard, + generation, + &self.clock, + &self.boot, + ) + .unwrap() + } + fn request(&self, snapshot: &Snapshot) -> Vec { + request( + snapshot, + self.agent_journal, + 100 + snapshot.checkpoint, + &snapshot.attempts, + ) + } + fn decision( + &self, + snapshot: &Snapshot, + raw: &[u8], + generation: u64, + ready: bool, + ) -> ReconcileDecisionPayload { + let mut d = ReconcileDecisionPayload { + domain: "rmm.reconciliation-decision".into(), + schema_version: 1, + decision_id: Uuid::new_v4(), + request_id: Uuid::nil(), + request_hash: hash(raw), + tenant_id: self.binding.tenant_id, + asset_id: self.binding.asset_id, + agent_id: self.binding.agent_id, + enrollment_generation: 1, + assignment_version: 3, + connection_generation: generation, + boot_id: self.boot.0, + journal_id: self.agent_journal, + checkpoint: 100 + snapshot.checkpoint, + helper_journal_id: Some(snapshot.journal_id), + helper_checkpoint: snapshot.checkpoint, + issued_at: self.clock.now, + valid_until: self.clock.now + Duration::seconds(60), + ready, + helper_ready: ready, + reason: if ready { + "CONSISTENT" + } else { + "ATTEMPT_RECONCILIATION_REQUIRED" + } + .into(), + attempts: snapshot + .attempts + .iter() + .map(|a| AttemptDecision { + job_id: a.job_id, + attempt_id: a.attempt_id, + job_payload_hash: a.job_payload_hash.clone(), + grant_id: Some(a.grant_id), + authorization_id: Some(a.authorization_id), + plan_hash: Some(a.plan_hash.clone()), + action: a.action.clone(), + execution_identity: self.identity.as_str().into(), + revision: a.revision + 1, + outcome_state: if matches!( + a.state.as_str(), + "succeeded" | "failed" | "cancelled" | "expired" + ) { + a.state.clone() + } else { + "unknown".into() + }, + previous_outcome_sha256: (!a.outcome_sha256.is_empty()) + .then(|| a.outcome_sha256.clone()), + outcome_sha256: matches!( + a.state.as_str(), + "succeeded" | "failed" | "cancelled" | "expired" + ) + .then(|| a.outcome_sha256.clone()), + outcome_bytes: vec![], + original_boot_id: Some(a.original_boot_id), + unlock: matches!( + a.state.as_str(), + "succeeded" | "failed" | "cancelled" | "expired" + ), + reason: "TEST_SERVER_DECISION".into(), + }) + .collect(), + acknowledged_message_ids: vec![], + key_id: hash(&self.key.verifying_key().to_bytes()), + }; + // The fixture emits a canonical tag-1 request ID first, exactly as prost. + let len = usize::from(raw[1]); + d.request_id = Uuid::parse_str(std::str::from_utf8(&raw[2..2 + len]).unwrap()).unwrap(); + d + } + fn sign(&self, d: &ReconcileDecisionPayload) -> SignedResponse { + let bytes = encode(d).unwrap(); + SignedResponse { + signature: self.key.sign(&bytes).to_bytes().to_vec(), + decision_bytes: bytes, + } + } + fn apply( + &mut self, + sync: &mut HelperSync, + d: &ReconcileDecisionPayload, + raw: &[u8], + ) -> anyhow::Result { + let r = self.sign(d); + sync.apply(&mut self.db, &self.guard, &r, raw, &self.clock, &self.boot) + } + fn add( + &mut self, + action: Action, + outcome: Option, + ) -> ExecutionPermitPayload { + let job = JobPayload { + schema_version: 1, + tenant_id: self.binding.tenant_id, + asset_id: self.binding.asset_id, + agent_id: self.binding.agent_id, + enrollment_generation: 1, + assignment_version: 3, + job_id: Uuid::new_v4(), + attempt_id: Uuid::new_v4(), + action, + execution_identity: self.identity.as_str().into(), + not_before: self.clock.now - Duration::seconds(10), + start_before: self.clock.now + Duration::minutes(5), + max_runtime_seconds: 180, + max_output_bytes: 4096, + }; + let permit = ExecutionPermitPayload { + domain: "rmm.execution-permit".into(), + schema_version: 1, + grant_id: Uuid::new_v4(), + authorization_id: Uuid::new_v4(), + tenant_id: job.tenant_id, + asset_id: job.asset_id, + agent_id: job.agent_id, + enrollment_generation: 1, + assignment_version: 3, + job_id: job.job_id, + attempt_id: job.attempt_id, + plan_hash: hash(b"approved fixed plan"), + job_payload_hash: hash(&encode(&job).unwrap()), + action: job.action.name().into(), + execution_identity: self.identity.as_str().into(), + not_before: self.clock.now - Duration::seconds(1), + start_before: self.clock.now + Duration::seconds(59), + max_runtime_seconds: 180, + key_id: hash(&self.key.verifying_key().to_bytes()), + }; + let bytes = encode(&permit).unwrap(); + let original = Request { + job_bytes: encode(&job).unwrap(), + permit_bytes: bytes.clone(), + signature: self.key.sign(&bytes).to_bytes().to_vec(), + plan_hash: permit.plan_hash.clone(), + authorization_id: permit.authorization_id, + }; + rmm_agent_core::ledger::consume(&mut self.db, &permit).unwrap(); + rmm_agent_core::ledger::save_start( + &self.db, + &permit, + &original, + self.boot.0, + self.clock.now, + ) + .unwrap(); + if let Some(o) = outcome { + self.db + .execute( + "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", + params![ + permit.grant_id.to_string(), + canonical_outcome_bytes(&o).unwrap() + ], + ) + .unwrap(); + } + permit + } +} +#[test] +fn general_and_apt_instances_share_fencing_but_never_inherit_a_process_gate() { + let mut f = Fixture::new(); + let mut general = f.sync(); + let first = f.snapshot(&mut general, 1); + let raw = f.request(&first); + let d = f.decision(&first, &raw, 1, true); + f.apply(&mut general, &d, &raw).unwrap(); + assert!(general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + let mut apt = f.sync(); + assert!(!apt.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + let second = f.snapshot(&mut apt, 1); + assert!(!general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + let raw = f.request(&second); + let d = f.decision(&second, &raw, 1, true); + f.apply(&mut apt, &d, &raw).unwrap(); + assert!(apt.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + assert!(!general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); +} +#[test] +fn targeted_final_unlock_does_not_release_an_unrelated_unknown() { + let mut f = Fixture::new(); + let known = f.add(script(), Some(success())); + let actual: rmm_agent_core::auth::Outcome = + serde_json::from_slice(&canonical_outcome_bytes(&success()).unwrap()).unwrap(); + rmm_agent_core::ledger::finish(&f.db, &known, &actual).unwrap(); + let uncertain = f.add(script(), Some(unknown())); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, false); + let applied = f.apply(&mut sync, &d, &raw).unwrap(); + assert_eq!(applied.unlocked_grants, vec![known.grant_id]); + assert!(!applied.ready); + assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + let held: bool = + f.db.query_row( + "SELECT result IS NULL FROM consumed WHERE grant_id=?1", + [uncertain.grant_id.to_string()], + |r| r.get(0), + ) + .unwrap(); + assert!(held); +} +#[test] +fn independently_remembered_agent_journal_cannot_be_replaced_by_server_ready() { + let mut f = Fixture::new(); + let mut sync = f.sync(); + let first = f.snapshot(&mut sync, 1); + let raw = f.request(&first); + let d = f.decision(&first, &raw, 1, true); + f.apply(&mut sync, &d, &raw).unwrap(); + let next = f.snapshot(&mut sync, 2); + f.agent_journal = Uuid::new_v4(); + let raw = f.request(&next); + let d = f.decision(&next, &raw, 2, true); + assert!(f.apply(&mut sync, &d, &raw).is_err()); + assert!( + sync.snapshot(&mut f.db, &f.guard, 2, &f.clock, &f.boot) + .is_err() + ); +} +#[test] +fn missing_signed_checkpoint_history_closes_the_live_gate_stickily() { + let mut f = Fixture::new(); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + f.apply(&mut sync, &d, &raw).unwrap(); + assert!(sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + f.db.execute("DELETE FROM helper_sync_decisions", []) + .unwrap(); + assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + assert!( + sync.snapshot(&mut f.db, &f.guard, 2, &f.clock, &f.boot) + .is_err() + ); +} +#[test] +fn stale_process_connection_does_not_poison_a_new_fenced_process() { + let mut f = Fixture::new(); + let mut general = f.sync(); + let snapshot = f.snapshot(&mut general, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + f.apply(&mut general, &d, &raw).unwrap(); + let mut apt = f.sync(); + let snapshot = f.snapshot(&mut apt, 2); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 2, true); + f.apply(&mut apt, &d, &raw).unwrap(); + assert!( + general + .snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) + .is_err() + ); + assert!(apt.can_start(&f.db, &f.guard, 2, &f.clock, &f.boot)); +} +#[test] +fn signed_noncanonical_outcome_bytes_do_not_unlock_root_consumption() { + let mut f = Fixture::new(); + f.add(script(), Some(success())); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let mut d = f.decision(&snapshot, &raw, 1, true); + d.attempts[0].outcome_bytes = serde_json::to_vec_pretty(&success()).unwrap(); + d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); + assert!(f.apply(&mut sync, &d, &raw).is_err()); + assert_eq!( + f.db.query_row( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get::<_, i64>(0) + ) + .unwrap(), + 1 + ); +} +#[test] +fn both_live_profiles_apply_one_signature_and_checkpoint_without_inheriting_readiness() { + let mut f = Fixture::new(); + let mut general = f.sync(); + let mut apt = f.sync(); + let first = f.snapshot(&mut general, 1); + let second = f.snapshot(&mut apt, 1); + assert_eq!(first.checkpoint, second.checkpoint); + assert!(!general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + assert!(!apt.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + let raw = f.request(&first); + let d = f.decision(&first, &raw, 1, true); + f.apply(&mut general, &d, &raw).unwrap(); + f.apply(&mut apt, &d, &raw).unwrap(); + assert!(general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + assert!(apt.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); +} +fn script() -> Action { + Action::RunScript { + interpreter: "posix_sh".into(), + script_utf8: "printf hello".into(), + arguments: vec![], + expected_stdout: Some("hello".into()), + } +} +fn success() -> ReconciledOutcome { + ReconciledOutcome { + state: "succeeded".into(), + stdout: b"hello".to_vec(), + stderr: vec![], + exit_code: Some(0), + output_truncated: false, + reboot_required: false, + evidence: serde_json::json!({"postconditions_passed":true}), + error_code: String::new(), + } +} +fn unknown() -> ReconciledOutcome { + ReconciledOutcome { + state: "unknown".into(), + stdout: vec![], + stderr: vec![], + exit_code: None, + output_truncated: false, + reboot_required: false, + evidence: serde_json::json!({"postconditions_passed":false}), + error_code: "EFFECT_UNKNOWN".into(), + } +} +// Test-only encoder for the actual existing prost field numbers. No substitute +// protocol dependency is added to the independently executable helper crate. +fn vi(mut n: u64, out: &mut Vec) { + loop { + let mut byte = (n & 127) as u8; + n >>= 7; + if n != 0 { + byte |= 128; + } + out.push(byte); + if n == 0 { + break; + } + } +} +fn num(tag: u64, n: u64, out: &mut Vec) { + if n != 0 { + vi(tag << 3, out); + vi(n, out); + } +} +fn blob(tag: u64, b: &[u8], out: &mut Vec) { + if !b.is_empty() { + vi((tag << 3) | 2, out); + vi(b.len() as u64, out); + out.extend_from_slice(b); + } +} +fn text(tag: u64, s: &str, out: &mut Vec) { + blob(tag, s.as_bytes(), out) +} +fn request( + snapshot: &Snapshot, + journal: Uuid, + checkpoint: u64, + attempts: &[LocalAttempt], +) -> Vec { + let mut out = vec![]; + text(1, &Uuid::new_v4().to_string(), &mut out); + text(2, &journal.to_string(), &mut out); + num(3, checkpoint, &mut out); + text( + 4, + &snapshot + .previous_decision_id + .map(|id| id.to_string()) + .unwrap_or_default(), + &mut out, + ); + num(5, 3, &mut out); + for a in attempts { + let mut row = vec![]; + text(1, &a.job_id.to_string(), &mut row); + text(2, &a.attempt_id.to_string(), &mut row); + text(3, &a.job_payload_hash, &mut row); + text(4, &a.grant_id.to_string(), &mut row); + text(5, &a.consumption, &mut row); + text(6, &a.state, &mut row); + num(7, a.revision + 1, &mut row); + text(8, &a.original_boot_id.to_string(), &mut row); + text(11, &a.outcome_sha256, &mut row); + blob(12, &a.outcome_bytes, &mut row); + blob(6, &row, &mut out); + } + num(8, 1, &mut out); + text(9, &snapshot.journal_id.to_string(), &mut out); + num(10, snapshot.checkpoint, &mut out); + text( + 11, + &snapshot + .previous_decision_id + .map(|id| id.to_string()) + .unwrap_or_default(), + &mut out, + ); + out +} + +#[test] +fn clean_ready_shared_lock_and_reopen_start_closed() { + let mut f = Fixture::new(); + let mut sync = f.sync(); + assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + let applied = f.apply(&mut sync, &d, &raw).unwrap(); + assert!(applied.ready); + assert_eq!(sync.active_generation(), Some(1)); + assert_eq!(applied.snapshot.previous_decision_id, Some(d.decision_id)); + assert!(applied.unlocked_grants.is_empty()); + assert!(sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + assert!(!sync.can_start(&f.db, &f.guard, 2, &f.clock, &f.boot)); + let response = f.sign(&d); + let mut new_process = f.sync(); + let mut reopened = Connection::open(f.dir.join("consumption.sqlite")).unwrap(); + assert!(!new_process.can_start(&reopened, &f.guard, 1, &f.clock, &f.boot)); + assert!( + new_process + .apply(&mut reopened, &f.guard, &response, &raw, &f.clock, &f.boot) + .is_err() + ); + sync.close(); + assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); +} +#[test] +fn exact_signatures_fake_key_wrong_domain_and_all_identity_bindings() { + let mut f = Fixture::new(); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + let base = serde_json::to_value(&d).unwrap(); + for (field, value) in [ + ("domain", serde_json::json!("rmm.execution-permit")), + ("schema_version", serde_json::json!(2)), + ("tenant_id", serde_json::json!(Uuid::new_v4())), + ("asset_id", serde_json::json!(Uuid::new_v4())), + ("agent_id", serde_json::json!(Uuid::new_v4())), + ("enrollment_generation", serde_json::json!(2)), + ("assignment_version", serde_json::json!(4)), + ("connection_generation", serde_json::json!(2)), + ("boot_id", serde_json::json!(Uuid::new_v4())), + ("journal_id", serde_json::json!(Uuid::new_v4())), + ("checkpoint", serde_json::json!(1)), + ("helper_journal_id", serde_json::json!(Uuid::new_v4())), + ( + "helper_checkpoint", + serde_json::json!(snapshot.checkpoint + 1), + ), + ("request_id", serde_json::json!(Uuid::new_v4())), + ("request_hash", serde_json::json!(hash(b"changed"))), + ("key_id", serde_json::json!(hash(b"wrongkey"))), + ] { + let mut changed = base.clone(); + changed[field] = value; + let changed: ReconcileDecisionPayload = serde_json::from_value(changed).unwrap(); + assert!(f.apply(&mut sync, &changed, &raw).is_err(), "{field}"); + } + let mut response = f.sign(&d); + response.signature[0] ^= 1; + assert!( + sync.apply(&mut f.db, &f.guard, &response, &raw, &f.clock, &f.boot) + .is_err() + ); + let mut response = f.sign(&d); + response.decision_bytes.push(b' '); + assert!( + sync.apply(&mut f.db, &f.guard, &response, &raw, &f.clock, &f.boot) + .is_err() + ); + let fake = SigningKey::from_bytes(&[43; 32]); + let mut response = f.sign(&d); + response.signature = fake.sign(&response.decision_bytes).to_bytes().to_vec(); + assert!( + sync.apply(&mut f.db, &f.guard, &response, &raw, &f.clock, &f.boot) + .is_err() + ); + assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); +} +#[test] +fn definitive_unlock_is_transactional_idempotent_and_keeps_tombstones() { + let mut f = Fixture::new(); + let grant = f.add(script(), Some(success())); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + assert_eq!(snapshot.attempts[0].original_boot_id, f.boot.0); + assert_eq!( + snapshot.attempts[0].job_payload_hash, + grant.job_payload_hash + ); + assert_eq!(snapshot.attempts[0].original_request_sha256.len(), 64); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + let applied = f.apply(&mut sync, &d, &raw).unwrap(); + assert_eq!(applied.unlocked_grants, vec![grant.grant_id]); + assert!(applied.ready); + let again = f.apply(&mut sync, &d, &raw).unwrap(); + assert!(again.ready); + assert!(again.unlocked_grants.is_empty()); + assert_eq!(again.snapshot.checkpoint, snapshot.checkpoint); + let body: Vec = + f.db.query_row( + "SELECT result FROM consumed WHERE grant_id=?1", + [grant.grant_id.to_string()], + |r| r.get(0), + ) + .unwrap(); + assert_eq!(body, canonical_outcome_bytes(&success()).unwrap()); + assert!(rmm_agent_core::ledger::consume(&mut f.db, &grant).is_err()); + assert_eq!( + f.db.query_row("SELECT count(*) FROM consumed", [], |r| r.get::<_, i64>(0)) + .unwrap(), + 1 + ); +} +#[test] +fn omitted_unknown_root_row_is_sticky_despite_signed_ready() { + let mut f = Fixture::new(); + f.add(script(), Some(unknown())); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = request(&snapshot, f.agent_journal, 100 + snapshot.checkpoint, &[]); + let d = f.decision(&snapshot, &raw, 1, true); + assert!(f.apply(&mut sync, &d, &raw).is_err()); + assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + let mut restarted = f.sync(); + assert!( + restarted + .snapshot(&mut f.db, &f.guard, 2, &f.clock, &f.boot) + .is_err() + ); +} +#[test] +fn unknown_code_or_no_result_never_unlocks_even_signed_ready() { + for outcome in [None, Some(unknown())] { + let mut f = Fixture::new(); + f.add(script(), outcome); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + assert_eq!(snapshot.attempts[0].state, "unknown"); + let raw = f.request(&snapshot); + let mut d = f.decision(&snapshot, &raw, 1, true); + let applied = f.apply(&mut sync, &d, &raw).unwrap(); + assert!(!applied.ready); + assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + d = f.decision(&snapshot, &raw, 1, true); + d.attempts[0].unlock = true; + d.attempts[0].outcome_state = "succeeded".into(); + d.attempts[0].outcome_bytes = canonical_outcome_bytes(&success()).unwrap(); + d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); + assert!(f.apply(&mut sync, &d, &raw).is_err()); + assert_eq!( + f.db.query_row( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get::<_, i64>(0) + ) + .unwrap(), + 1 + ); + } +} +#[test] +fn old_process_response_fails_after_fresh_capture_even_inside_ttl() { + let mut f = Fixture::new(); + let mut first = f.sync(); + let snapshot = f.snapshot(&mut first, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + let response = f.sign(&d); + f.clock.now += Duration::seconds(5); + let mut second = f.sync(); + let newer = f.snapshot(&mut second, 1); + assert!(newer.checkpoint > snapshot.checkpoint); + assert!( + second + .apply(&mut f.db, &f.guard, &response, &raw, &f.clock, &f.boot) + .is_err() + ); + assert!(!second.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); +} +#[test] +fn checkpoint_rollback_is_sticky_and_cannot_be_fixed_by_omission() { + let mut f = Fixture::new(); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + f.db.execute("UPDATE helper_sync_meta SET checkpoint=0", []) + .unwrap(); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + assert!(f.apply(&mut sync, &d, &raw).is_err()); + f.db.execute( + "UPDATE helper_sync_meta SET checkpoint=?1", + [snapshot.checkpoint], + ) + .unwrap(); + assert!( + sync.snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) + .is_err() + ); +} +#[test] +fn mutated_root_ledger_after_capture_is_not_the_signed_snapshot() { + let mut f = Fixture::new(); + f.add(script(), Some(success())); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + let mut changed = success(); + changed.stdout = b"different".to_vec(); + f.db.execute( + "UPDATE execution_evidence SET outcome=?1", + [canonical_outcome_bytes(&changed).unwrap()], + ) + .unwrap(); + assert!(f.apply(&mut sync, &d, &raw).is_err()); + assert!( + sync.snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) + .is_err() + ); +} +#[test] +fn unrelated_action_plan_and_previous_outcome_cannot_unlock() { + let mut f = Fixture::new(); + f.add(script(), Some(success())); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + let mut changed = d.clone(); + changed.attempts[0].action = "reboot".into(); + assert!(f.apply(&mut sync, &changed, &raw).is_err()); + let mut changed = d.clone(); + changed.attempts[0].plan_hash = Some(hash(b"changed plan")); + assert!(f.apply(&mut sync, &changed, &raw).is_err()); + let mut changed = d; + changed.attempts[0].previous_outcome_sha256 = Some(hash(b"different outcome")); + assert!(f.apply(&mut sync, &changed, &raw).is_err()); +} +#[test] +fn false_clock_expiry_and_invalid_independent_key_stay_closed() { + let mut f = Fixture::new(); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + f.clock.trusted = false; + assert!(f.apply(&mut sync, &d, &raw).is_err()); + assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); + f.clock.trusted = true; + f.clock.now += Duration::seconds(61); + assert!(f.apply(&mut sync, &d, &raw).is_err()); + let mut invalid = f.binding.clone(); + invalid.public_key_hex = "not a key".into(); + assert!(HelperSync::new(invalid, &f.clock).is_err()); + let mut changed = f.binding.clone(); + changed.public_key_hex = + hex::encode(SigningKey::from_bytes(&[43; 32]).verifying_key().to_bytes()); + let mut different = HelperSync::new(changed, &f.clock).unwrap(); + assert!( + different + .snapshot(&mut f.db, &f.guard, 2, &f.clock, &f.boot) + .is_err() + ); +} +#[test] +fn shared_database_only_and_missing_local_start_fail_closed() { + let mut f = Fixture::new(); + let mut sync = f.sync(); + let mut copy = Connection::open_in_memory().unwrap(); + assert!( + sync.snapshot(&mut copy, &f.guard, 1, &f.clock, &f.boot) + .is_err() + ); + let grant = f.add(script(), None); + f.db.execute( + "DELETE FROM execution_evidence WHERE grant_id=?1", + [grant.grant_id.to_string()], + ) + .unwrap(); + assert!( + sync.snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) + .is_err() + ); +} +#[test] +fn real_local_boot_must_match_signed_reboot_completion() { + let mut f = Fixture::new(); + let old = f.boot.0; + let mut awaiting = unknown(); + awaiting.state = "awaiting_reboot".into(); + awaiting.reboot_required = true; + f.add( + Action::Reboot { + reason: "mock reboot no native effect".into(), + reconnect_timeout_seconds: 120, + dependency_group: "acceptance".into(), + }, + Some(awaiting), + ); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let mut d = f.decision(&snapshot, &raw, 1, true); + let mut done = success(); + done.stdout.clear(); + done.exit_code = None; + done.evidence = serde_json::json!({"postconditions_passed":true,"new_boot_id":Uuid::new_v4(),"reconciled_after_reboot":true,"inventory_message_id":Uuid::new_v4()}); + d.attempts[0].unlock = true; + d.attempts[0].outcome_state = "succeeded".into(); + d.attempts[0].outcome_bytes = canonical_outcome_bytes(&done).unwrap(); + d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); + assert!(f.apply(&mut sync, &d, &raw).is_err()); // native current boot is still old + f.boot.0 = Uuid::new_v4(); + f.clock.now += Duration::seconds(1); + let snapshot = f.snapshot(&mut sync, 2); + let raw = f.request(&snapshot); + let mut d = f.decision(&snapshot, &raw, 2, true); + assert_eq!(snapshot.attempts[0].original_boot_id, old); + assert_ne!(f.boot.0, old); + done.evidence["new_boot_id"] = serde_json::json!(f.boot.0); + d.attempts[0].unlock = true; + d.attempts[0].outcome_state = "succeeded".into(); + d.attempts[0].outcome_bytes = canonical_outcome_bytes(&done).unwrap(); + d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); + let applied = f.apply(&mut sync, &d, &raw).unwrap(); + assert!(applied.ready); + assert_eq!(applied.snapshot.attempts[0].state, "succeeded"); + assert!(sync.can_start(&f.db, &f.guard, 2, &f.clock, &f.boot)); +} +#[test] +fn malformed_original_protobuf_or_digest_does_not_open_gate() { + let mut f = Fixture::new(); + let mut sync = f.sync(); + let snapshot = f.snapshot(&mut sync, 1); + let raw = f.request(&snapshot); + let d = f.decision(&snapshot, &raw, 1, true); + let mut malformed = raw.clone(); + malformed.push(0); + assert!(f.apply(&mut sync, &d, &malformed).is_err()); + let mut malformed = raw.clone(); + text(1, &Uuid::new_v4().to_string(), &mut malformed); + let mut signed = d.clone(); + signed.request_hash = hash(&malformed); + assert!(f.apply(&mut sync, &signed, &malformed).is_err()); + let mut signed = d; + signed.request_hash.clear(); + assert!(f.apply(&mut sync, &signed, &raw).is_err()); + assert!( + sync.snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) + .is_err() + ); +} + +#[test] +fn root_and_system_are_distinct_explicit_helper_domains() { + let mut root = Fixture::new(); + root.add(script(), Some(unknown())); + let before: Vec = root + .db + .query_row("SELECT start FROM execution_evidence", [], |r| r.get(0)) + .unwrap(); + let mut wrong = HelperSync::new(root.binding.clone(), &root.clock) + .unwrap() + .with_helper_identity(HelperIdentity::System); + assert!( + wrong + .snapshot(&mut root.db, &root.guard, 1, &root.clock, &root.boot) + .is_err() + ); + assert_eq!( + before, + root.db + .query_row::, _, _>("SELECT start FROM execution_evidence", [], |r| r.get(0)) + .unwrap() + ); + assert_eq!( + root.db + .query_row::( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get(0) + ) + .unwrap(), + 1 + ); + + let mut system = Fixture::new(); + system.identity = HelperIdentity::System; + system.add(script(), Some(success())); + let mut sync = system.sync(); + let snapshot = system.snapshot(&mut sync, 1); + assert_eq!(snapshot.attempts[0].execution_identity, "system"); + let raw = system.request(&snapshot); + let mut wrong = system.decision(&snapshot, &raw, 1, true); + wrong.attempts[0].execution_identity = "root".into(); + assert!(system.apply(&mut sync, &wrong, &raw).is_err()); + assert_eq!( + system + .db + .query_row::( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get(0) + ) + .unwrap(), + 1 + ); + let mut system = Fixture::new(); + system.identity = HelperIdentity::System; + system.add(script(), Some(success())); + let mut sync = system.sync(); + let snapshot = system.snapshot(&mut sync, 1); + let raw = system.request(&snapshot); + let decision = system.decision(&snapshot, &raw, 1, true); + assert!(system.apply(&mut sync, &decision, &raw).unwrap().ready); + assert!(sync.can_start(&system.db, &system.guard, 1, &system.clock, &system.boot)); + let changed = sync.with_helper_identity(HelperIdentity::Root); + assert!(!changed.can_start(&system.db, &system.guard, 1, &system.clock, &system.boot)); +} diff --git a/crates/agent-core/tests/reconcile.rs b/crates/agent-core/tests/reconcile.rs index e2334f6..afc40e5 100644 --- a/crates/agent-core/tests/reconcile.rs +++ b/crates/agent-core/tests/reconcile.rs @@ -31,6 +31,7 @@ struct Fixture { binding: Binding, channel: Channel, clock: Clock, + helper_identity: HelperIdentity, } impl Drop for Fixture { fn drop(&mut self) { @@ -77,6 +78,7 @@ impl Fixture { key, binding, channel, + helper_identity: HelperIdentity::Root, clock: Clock { now: Utc::now(), trusted: true, @@ -84,7 +86,7 @@ impl Fixture { } } fn client(&self) -> ClientSync { - ClientSync::new(self.binding.clone()) + ClientSync::new(self.binding.clone()).with_helper_identity(self.helper_identity) } fn prepare(&mut self, client: &mut ClientSync) -> PreparedSync { client @@ -216,7 +218,7 @@ impl Fixture { action: Action, ) -> (JobOffer, Option, Option) { let execution_identity = if matches!(action, Action::SetServiceState { .. }) { - "root" + self.helper_identity.as_str() } else { "agent_service" }; @@ -1143,3 +1145,90 @@ fn signed_attempt_mutation_or_terminal_regression_is_rejected() { changed.attempts[0].previous_outcome_sha256 = Some(hash(b"other outcome")); assert!(f.apply(&mut c, &changed).is_err()); } + +#[test] +fn system_helper_import_requires_explicit_system_identity_and_never_root_alias() { + let mut f = Fixture::new(); + f.helper_identity = HelperIdentity::System; + let mut unknown = success(); + unknown.state = "unknown".into(); + unknown.error_code = "HELPER_TRANSPORT_EFFECT_UNKNOWN".into(); + unknown.evidence = serde_json::json!({"postconditions_passed":false}); + let (offer, grant, old) = f.add( + "unknown", + true, + Some(unknown), + Action::SetServiceState { + unit: "rmm-acceptance.service".into(), + state: "running".into(), + }, + ); + let grant = grant.unwrap(); + let mut definitive = success(); + definitive.stdout.clear(); + definitive.exit_code = None; + let bytes = canonical_outcome_bytes(&definitive).unwrap(); + let mut helper = HelperSnapshot { + journal_id: Uuid::new_v4(), + checkpoint: 3, + previous_decision_id: None, + attempts: vec![SyncAttempt { + job_id: offer.job_id.clone(), + attempt_id: offer.attempt_id.clone(), + job_payload_hash: offer.job_payload_hash.clone(), + grant_id: grant.grant_id.to_string(), + consumption: "consumed".into(), + state: "succeeded".into(), + revision: 1, + original_boot_id: f.channel.boot_id.to_string(), + outcome_sha256: hash(&bytes), + outcome_bytes: bytes, + ..Default::default() + }], + }; + let mut wrong = ClientSync::new(f.binding.clone()); + assert!( + wrong + .prepare( + &mut f.db, + &f.ledger, + f.channel.clone(), + Some(&helper), + &f.clock + ) + .is_err() + ); + let mut c = f.client(); + let p = c + .prepare( + &mut f.db, + &f.ledger, + f.channel.clone(), + Some(&helper), + &f.clock, + ) + .unwrap(); + let imported = p.request.attempts[0].result_envelope.as_ref().unwrap(); + assert_ne!(imported.message_id, old.unwrap().message_id); + assert_eq!(p.request.attempts[0].state, "succeeded"); + assert_eq!(p.request.attempts[0].revision, 2); + let d = f.decision(&p, true); + let applied = f.apply(&mut c, &d).unwrap(); + assert!(applied.helper_instruction.is_some()); + c.apply_consumption(&mut f.ledger, &f.clock).unwrap(); + assert!(!c.can_start(&f.channel, true, &f.clock)); + helper.previous_decision_id = Some(d.decision_id); + c.confirm_helper(&helper).unwrap(); + assert!(c.can_start(&f.channel, true, &f.clock)); + let retained: i64 = + f.db.query_row("SELECT count(*) FROM reconcile_results", [], |r| r.get(0)) + .unwrap(); + assert_eq!(retained, 2); + let tombstones: i64 = f + .ledger + .query_row("SELECT count(*) FROM consumed", [], |r| r.get(0)) + .unwrap(); + assert_eq!(tombstones, 1); + let changed = c.with_helper_identity(HelperIdentity::Root); + assert!(!changed.can_start(&f.channel, true, &f.clock)); +} diff --git a/crates/privileged-helper/src/reconcile.rs b/crates/privileged-helper/src/reconcile.rs index 3ce7e2d..9db0fde 100644 --- a/crates/privileged-helper/src/reconcile.rs +++ b/crates/privileged-helper/src/reconcile.rs @@ -1,31 +1,13 @@ -//! Independently verifying root-ledger reconciliation. This module never executes -//! an operation or grants authority from an agent-provided ready/unlock flag. -//! Both general and APT listeners must use the SAME executor.lock and SQLite DB. -use anyhow::{Context, Result, ensure}; +//! Linux OS ports for the common independently verified helper reconciliation. +use anyhow::{Result, ensure}; use chrono::{DateTime, Utc}; -use openssl::{ - pkey::{Id, PKey}, - sign::Verifier, -}; -use rmm_domain::{ - jobs::{Action, ExecutionPermitPayload, hash}, - reconciliation::{ - AttemptDecision, ReconcileDecisionPayload, ReconciledOutcome, canonical_outcome_bytes, - }, -}; -use rmm_privileged_helper::{Binding, Request}; -use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params}; -use serde::{Deserialize, Serialize}; +pub use rmm_agent_core::helper_reconcile::*; +use rusqlite::Connection; use std::{ - collections::{BTreeMap, BTreeSet}, fs::File, path::{Path, PathBuf}, }; use uuid::Uuid; - -pub trait Clock { - fn now(&self) -> Result>; -} #[derive(Default)] pub struct NativeClock(rmm_privileged_helper::TrustedClock); impl Clock for NativeClock { @@ -34,9 +16,6 @@ impl Clock for NativeClock { Ok(Utc::now()) } } -pub trait BootSource { - fn current_boot(&self) -> Result; -} pub struct NativeBoot; impl BootSource for NativeBoot { fn current_boot(&self) -> Result { @@ -76,1241 +55,8 @@ impl ExecutorGuard { Ok(()) } } -/// Same two fields as public SyncResponse, without adding a new dependency or -/// wire type. RPC glue copies the ORIGINAL bytes/signature, never reserializes. -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -#[serde(deny_unknown_fields)] -pub struct SignedResponse { - pub decision_bytes: Vec, - pub signature: Vec, -} -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct LocalAttempt { - pub job_id: Uuid, - pub attempt_id: Uuid, - pub job_payload_hash: String, - pub grant_id: Uuid, - pub authorization_id: Uuid, - pub plan_hash: String, - pub action: String, - pub execution_identity: String, - pub revision: u64, - pub original_boot_id: Uuid, - pub consumption: String, - pub state: String, - pub outcome_sha256: String, - pub outcome_bytes: Vec, - /// Hash of the exact root-owned Request containing original job/permit bytes. - pub original_request_sha256: String, -} -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct Snapshot { - pub journal_id: Uuid, - pub checkpoint: u64, - pub previous_decision_id: Option, - pub boot_id: Uuid, - pub captured_at: DateTime, - pub attempts: Vec, -} -pub struct Applied { - pub snapshot: Snapshot, - pub unlocked_grants: Vec, - pub ready: bool, -} -struct Capture { - snapshot: Snapshot, - fingerprint: String, - generation: u64, -} -struct Lease { - checkpoint: u64, - decision_id: Uuid, - boot_id: Uuid, - generation: u64, - until: DateTime, - fingerprint: String, -} -pub struct HelperSync { - binding: Binding, - born: DateTime, - capture: Option, - lease: Option, -} -impl HelperSync { - /// No persisted ready bit can open a newly started general/APT helper. - pub fn new(binding: Binding, clock: &impl Clock) -> Result { - pinned_key(&binding)?; - Ok(Self { - binding, - born: clock.now()?, - capture: None, - lease: None, - }) - } - pub fn close(&mut self) { - self.lease = None; - self.capture = None; - } - pub fn active_generation(&self) -> Option { - self.lease.as_ref().map(|lease| lease.generation) - } - /// Every call creates a NEW durable checkpoint challenge and closes the old - /// lease. Use apply's returned snapshot for its ACK, not another snapshot(). - pub fn snapshot( - &mut self, - db: &mut Connection, - guard: &ExecutorGuard, - generation: u64, - clock: &impl Clock, - boot: &impl BootSource, - ) -> Result { - self.close(); - let result = self.snapshot_inner(db, guard, generation, clock, boot); - if let Err(error) = &result { - persist_fault(db, error); - } - result - } - fn snapshot_inner( - &mut self, - db: &mut Connection, - guard: &ExecutorGuard, - generation: u64, - clock: &impl Clock, - boot: &impl BootSource, - ) -> Result { - guard.check(db)?; - let now = clock.now()?; - ensure!( - now >= self.born && generation > 0, - "CLOCK_OR_CHANNEL_REGRESSION" - ); - let boot_id = boot.current_boot()?; - ensure!(!boot_id.is_nil(), "BOOT_ID_REQUIRED"); - initialize(db, &self.binding)?; - let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; - let meta = metadata(&tx)?; - ensure!(meta.fault.is_none(), "HELPER_MANUAL_RECOVERY_REQUIRED"); - let highest: u64 = tx.query_row( - "SELECT COALESCE(MAX(checkpoint),0) FROM helper_sync_captures", - [], - |r| r.get(0), - )?; - ensure!(meta.checkpoint == highest, "HELPER_CHECKPOINT_ROLLBACK"); - ensure!(generation >= meta.generation, "STALE_HELPER_CONNECTION"); - validate_history(&tx, &self.binding, &meta, generation)?; - let attempts = load_rows(&tx, &self.binding, true)?; - ensure!(attempts.len() <= 256, "HELPER_SYNC_CAPACITY"); - let fingerprint = ledger_fingerprint(&tx)?; - // Two independently gated profiles share one signed helper checkpoint. - // Join only a still-unapplied root-owned capture created after THIS - // process was born, on the same fenced channel and unchanged ledger. - if meta.checkpoint > 0 && meta.generation == generation { - let applied: bool = tx.query_row( - "SELECT EXISTS(SELECT 1 FROM helper_sync_decisions WHERE checkpoint=?1)", - [meta.checkpoint], - |r| r.get(0), - )?; - let old: Option<(Vec, String)> = tx - .query_row( - "SELECT snapshot,fingerprint FROM helper_sync_captures WHERE checkpoint=?1", - [meta.checkpoint], - |r| Ok((r.get(0)?, r.get(1)?)), - ) - .optional()?; - if !applied && let Some((bytes, old_fingerprint)) = old { - let shared: Snapshot = serde_json::from_slice(&bytes)?; - if shared.captured_at >= self.born - && shared.captured_at <= now - && now - shared.captured_at < chrono::Duration::seconds(30) - && shared.boot_id == boot_id - && shared.previous_decision_id == meta.decision - && old_fingerprint == fingerprint - && serde_json::to_vec(&shared.attempts)? == serde_json::to_vec(&attempts)? - { - tx.commit()?; - self.capture = Some(Capture { - snapshot: shared.clone(), - fingerprint, - generation, - }); - return Ok(shared); - } - } - } - let checkpoint = meta - .checkpoint - .checked_add(1) - .context("CHECKPOINT_OVERFLOW")?; - i64::try_from(checkpoint)?; - let snapshot = Snapshot { - journal_id: meta.journal_id, - checkpoint, - previous_decision_id: meta.decision, - boot_id, - captured_at: now, - attempts, - }; - let bytes = serde_json::to_vec(&snapshot)?; - ensure!(bytes.len() <= 2 * 1024 * 1024, "HELPER_SNAPSHOT_SIZE"); - tx.execute("INSERT INTO helper_sync_captures(checkpoint,snapshot,fingerprint,captured_at) VALUES(?1,?2,?3,?4)",params![checkpoint,bytes,fingerprint,now.to_rfc3339()])?; - tx.execute( - "UPDATE helper_sync_meta SET checkpoint=?1,generation=?2 WHERE id=1", - params![checkpoint, generation], - )?; - tx.commit()?; - self.capture = Some(Capture { - snapshot: snapshot.clone(), - fingerprint, - generation, - }); - Ok(snapshot) - } - /// raw_request is SyncRequest::encode_to_vec(), NOT the outer Envelope or a - /// JSON view. Verify signature before decoding fields. No unsigned unlocks. - pub fn apply( - &mut self, - db: &mut Connection, - guard: &ExecutorGuard, - response: &SignedResponse, - raw_request: &[u8], - clock: &impl Clock, - boot: &impl BootSource, - ) -> Result { - self.lease = None; - let result = self.apply_inner(db, guard, response, raw_request, clock, boot); - if let Err(error) = &result { - self.lease = None; - persist_fault(db, error); - } - result - } - fn apply_inner( - &mut self, - db: &mut Connection, - guard: &ExecutorGuard, - response: &SignedResponse, - raw_request: &[u8], - clock: &impl Clock, - boot: &impl BootSource, - ) -> Result { - guard.check(db)?; - let now = clock.now()?; - let capture = self - .capture - .as_ref() - .context("FRESH_PROCESS_SNAPSHOT_REQUIRED")?; - let decision = verify(&self.binding, response, now)?; - let request = RequestView::decode(raw_request)?; - let current_boot = boot.current_boot()?; - ensure!( - now >= self.born - && decision.issued_at >= self.born - && decision.issued_at >= capture.snapshot.captured_at - && current_boot == capture.snapshot.boot_id - && decision.boot_id == current_boot, - "OLD_PROCESS_DECISION_OR_BOOT" - ); - ensure!( - decision.connection_generation == capture.generation - && decision.helper_journal_id == Some(capture.snapshot.journal_id) - && decision.helper_checkpoint == capture.snapshot.checkpoint - && request.helper_journal_id == capture.snapshot.journal_id.to_string() - && request.helper_checkpoint == capture.snapshot.checkpoint - && request.helper_previous_decision_id - == capture - .snapshot - .previous_decision_id - .map(|id| id.to_string()) - .unwrap_or_default() - && decision.request_id.to_string() == request.request_id - && decision.request_hash == hash(raw_request) - && decision.journal_id.to_string() == request.journal_id - && decision.checkpoint == request.checkpoint - && request.assignment_version == self.binding.assignment_version - && request.journal_complete, - "HELPER_SYNC_REQUEST_BINDING" - ); - let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; - let meta = metadata(&tx)?; - ensure!( - meta.fault.is_none() - && meta.journal_id == capture.snapshot.journal_id - && meta.checkpoint >= capture.snapshot.checkpoint - && meta.generation >= capture.generation, - "HELPER_CHECKPOINT_ROLLBACK" - ); - ensure!( - meta.checkpoint == capture.snapshot.checkpoint && meta.generation == capture.generation, - "STALE_HELPER_CAPTURE" - ); - ensure!( - meta.agent_journal - .is_none_or(|id| id == decision.journal_id) - && decision.checkpoint >= meta.agent_checkpoint, - "AGENT_JOURNAL_ROLLBACK_OR_REPLACEMENT" - ); - validate_history(&tx, &self.binding, &meta, capture.generation)?; - if let Some(old)=tx.query_row("SELECT response,request,fingerprint FROM helper_sync_decisions WHERE decision_id=?1",[decision.decision_id.to_string()],|r|Ok((r.get::<_,Vec>(0)?,r.get::<_,Vec>(1)?,r.get::<_,String>(2)?))).optional()? { - ensure!(old.0==serde_json::to_vec(response)? && old.1==raw_request && meta.decision==Some(decision.decision_id) - && ledger_fingerprint(&tx)?==old.2,"HELPER_REPLAY_OR_LEDGER_CHANGED"); - let rows=load_rows(&tx,&self.binding,false)?; - let ready=decision.ready && decision.helper_ready && rows.iter().all(|row|definitive(row) - && decision.attempts.iter().any(|d|d.job_id==row.job_id && d.attempt_id==row.attempt_id - && d.outcome_state==row.state && d.outcome_sha256.as_deref()==Some(row.outcome_sha256.as_str()))) - && tx.query_row("SELECT count(*) FROM consumed WHERE result IS NULL",[],|r|r.get::<_,i64>(0))?==0; - let snapshot=Snapshot {journal_id:meta.journal_id,checkpoint:meta.checkpoint,previous_decision_id:meta.decision, - boot_id:current_boot,captured_at:capture.snapshot.captured_at,attempts:rows}; - tx.commit()?; - if ready {self.lease=Some(Lease {checkpoint:meta.checkpoint,decision_id:decision.decision_id,boot_id:current_boot, - generation:capture.generation,until:decision.valid_until,fingerprint:old.2});} - return Ok(Applied {snapshot,unlocked_grants:vec![],ready}); - } - ensure!( - meta.decision == capture.snapshot.previous_decision_id, - "HELPER_PREVIOUS_DECISION_CHANGED" - ); - ensure!( - ledger_fingerprint(&tx)? == capture.fingerprint, - "LOCAL_LEDGER_CHANGED_DURING_SYNC" - ); - let rows = load_rows(&tx, &self.binding, false)?; - ensure!( - serde_json::to_vec(&rows)? == serde_json::to_vec(&capture.snapshot.attempts)?, - "LOCAL_SNAPSHOT_CHANGED_DURING_SYNC" - ); - let summaries = unique_summaries(&request.attempts)?; - let mut decisions = BTreeMap::new(); - for d in &decision.attempts { - ensure!( - decisions.insert((d.job_id, d.attempt_id), d).is_none(), - "DUPLICATE_SIGNED_ATTEMPT" - ); - } - let known: BTreeSet<_> = rows.iter().map(|r| (r.job_id, r.attempt_id)).collect(); - for d in &decision.attempts { - ensure!( - d.execution_identity != "root" - || d.grant_id.is_none() - || known.contains(&(d.job_id, d.attempt_id)), - "SIGNED_ROOT_ATTEMPT_MISSING_LOCALLY" - ); - } - let mut unlocked = Vec::new(); - let mut safe = true; - for row in &rows { - let id = (row.job_id, row.attempt_id); - let local = summaries - .get(&id) - .context("ROOT_ATTEMPT_OMITTED_FROM_REQUEST")?; - let d = decisions - .get(&id) - .context("ROOT_ATTEMPT_OMITTED_FROM_DECISION")?; - validate_summary(row, local)?; - validate_decision(row, local, d)?; - if d.unlock { - let bytes = unlock_outcome(row, d, current_boot)?; - ensure!( - tx.execute( - "UPDATE consumed SET result=?2 WHERE grant_id=?1", - params![row.grant_id.to_string(), bytes] - )? == 1, - "ROOT_CONSUMPTION_MISSING" - ); - tx.execute( - "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", - params![row.grant_id.to_string(), bytes], - )?; - unlocked.push(row.grant_id); - } else { - safe &= definitive(row) - && d.outcome_state == row.state - && d.outcome_sha256.as_deref() == Some(row.outcome_sha256.as_str()); - // A definitive local state with an unresolved consumed lock is - // not globally ready merely because the server says ready. - let locked: bool = tx.query_row( - "SELECT result IS NULL FROM consumed WHERE grant_id=?1", - [row.grant_id.to_string()], - |r| r.get(0), - )?; - safe &= !locked; - } - } - let fault = recovery_reason(&decision.reason).then(|| decision.reason.clone()); - ensure!( - !decision.attempts.iter().any(|d| recovery_reason(&d.reason)), - "SIGNED_LEDGER_RECOVERY_REQUIRED" - ); - let after = load_rows(&tx, &self.binding, true)?; - let fingerprint = ledger_fingerprint(&tx)?; - tx.execute("INSERT INTO helper_sync_decisions(decision_id,checkpoint,response,request,fingerprint) VALUES(?1,?2,?3,?4,?5)",params![decision.decision_id.to_string(),decision.helper_checkpoint,serde_json::to_vec(response)?,raw_request,fingerprint])?; - tx.execute("UPDATE helper_sync_meta SET last_decision=?1,fault=COALESCE(fault,?2),agent_journal_id=?3,agent_checkpoint=?4 WHERE id=1",params![decision.decision_id.to_string(),fault,decision.journal_id.to_string(),decision.checkpoint])?; - tx.commit()?; - let ready = safe - && fault.is_none() - && decision.ready - && decision.helper_ready - && after.iter().all(definitive); - if ready { - self.lease = Some(Lease { - checkpoint: decision.helper_checkpoint, - decision_id: decision.decision_id, - boot_id: current_boot, - generation: capture.generation, - until: decision.valid_until, - fingerprint, - }); - } - Ok(Applied { - snapshot: Snapshot { - journal_id: meta.journal_id, - checkpoint: meta.checkpoint, - previous_decision_id: Some(decision.decision_id), - boot_id: current_boot, - captured_at: capture.snapshot.captured_at, - attempts: after, - }, - unlocked_grants: unlocked, - ready, - }) - } - /// Call under the same executor guard immediately BEFORE fresh grant - /// consumption. Once that operation changes the ledger, resync is required. - pub fn can_start( - &self, - db: &Connection, - guard: &ExecutorGuard, - generation: u64, - clock: &impl Clock, - boot: &impl BootSource, - ) -> bool { - let result = (|| -> Result { - guard.check(db)?; - let now = clock.now()?; - let Some(lease) = &self.lease else { - return Ok(false); - }; - let meta = metadata(db)?; - if meta.fault.is_some() - || meta.checkpoint != lease.checkpoint - || meta.decision != Some(lease.decision_id) - || generation != lease.generation - { - return Ok(false); - } - validate_history(db, &self.binding, &meta, generation)?; - Ok(meta.fault.is_none() - && meta.checkpoint == lease.checkpoint - && meta.decision == Some(lease.decision_id) - && generation == lease.generation - && now < lease.until - && now >= self.born - && boot.current_boot()? == lease.boot_id - && ledger_fingerprint(db)? == lease.fingerprint - && db.query_row( - "SELECT count(*) FROM consumed WHERE result IS NULL", - [], - |r| r.get::<_, i64>(0), - )? == 0) - })(); - if let Err(error) = &result { - persist_fault(db, error); - } - result.unwrap_or(false) +impl ExecutorLedgerGuard for ExecutorGuard { + fn check(&self, db: &Connection) -> Result<()> { + ExecutorGuard::check(self, db) } } - -fn pinned_key(binding: &Binding) -> Result> { - let raw = hex::decode(&binding.public_key_hex)?; - ensure!(raw.len() == 32, "PINNED_HELPER_KEY_SIZE"); - PKey::public_key_from_raw_bytes(&raw, Id::ED25519)?; - Ok(raw) -} -fn verify( - binding: &Binding, - response: &SignedResponse, - now: DateTime, -) -> Result { - ensure!( - response.signature.len() == 64 && response.decision_bytes.len() <= 512 * 1024, - "SYNC_SIGNATURE_SIZE" - ); - let raw = pinned_key(binding)?; - let key = PKey::public_key_from_raw_bytes(&raw, Id::ED25519)?; - ensure!( - Verifier::new_without_digest(&key)? - .verify_oneshot(&response.signature, &response.decision_bytes)?, - "INVALID_SYNC_SIGNATURE" - ); - let d: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?; - ensure!( - d.request_hash.len() == 64 - && d.request_hash - .bytes() - .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)), - "SIGNED_REQUEST_HASH_MISSING_OR_INVALID" - ); - i64::try_from(d.checkpoint)?; - i64::try_from(d.helper_checkpoint)?; - i64::try_from(d.connection_generation)?; - for attempt in &d.attempts { - i64::try_from(attempt.revision)?; - } - ensure!( - d.valid_profile(now) - && d.valid_until - d.issued_at <= chrono::Duration::seconds(60) - && d.key_id == hash(&raw) - && d.tenant_id == binding.tenant_id - && d.asset_id == binding.asset_id - && d.agent_id == binding.agent_id - && d.enrollment_generation == binding.enrollment_generation - && d.assignment_version == binding.assignment_version, - "HELPER_DECISION_PROFILE_OR_IDENTITY" - ); - Ok(d) -} -struct Meta { - journal_id: Uuid, - checkpoint: u64, - generation: u64, - decision: Option, - fault: Option, - agent_journal: Option, - agent_checkpoint: u64, -} -fn initialize(db: &Connection, binding: &Binding) -> Result<()> { - let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?; - ensure!(check == "ok", "ROOT_LEDGER_INTEGRITY"); - db.execute_batch("PRAGMA synchronous=FULL; - CREATE TABLE IF NOT EXISTS helper_sync_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,binding_sha256 TEXT NOT NULL,last_decision TEXT,fault TEXT,agent_journal_id TEXT,agent_checkpoint INTEGER NOT NULL DEFAULT 0); - CREATE TABLE IF NOT EXISTS helper_sync_rows(grant_id TEXT PRIMARY KEY,revision INTEGER NOT NULL,fingerprint TEXT NOT NULL); - CREATE TABLE IF NOT EXISTS helper_sync_captures(checkpoint INTEGER PRIMARY KEY,snapshot BLOB NOT NULL,fingerprint TEXT NOT NULL,captured_at TEXT NOT NULL); - CREATE TABLE IF NOT EXISTS helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL);")?; - let binding_hash = hash(&serde_json::to_vec(binding)?); - db.execute( - "INSERT OR IGNORE INTO helper_sync_meta(id,journal_id,binding_sha256) VALUES(1,?1,?2)", - params![Uuid::new_v4().to_string(), binding_hash], - )?; - let old: String = db.query_row( - "SELECT binding_sha256 FROM helper_sync_meta WHERE id=1", - [], - |r| r.get(0), - )?; - ensure!( - old == binding_hash, - "INDEPENDENT_HELPER_BINDING_OR_KEY_CHANGED" - ); - Ok(()) -} -fn metadata(db: &Connection) -> Result { - type MetaRow = ( - String, - u64, - u64, - Option, - Option, - Option, - u64, - ); - let (id,checkpoint,generation,decision,fault,agent,agent_checkpoint):MetaRow=db.query_row("SELECT journal_id,checkpoint,generation,last_decision,fault,agent_journal_id,agent_checkpoint FROM helper_sync_meta WHERE id=1",[],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?,r.get(5)?,r.get(6)?)))?; - Ok(Meta { - journal_id: Uuid::parse_str(&id)?, - checkpoint, - generation, - decision: decision.map(|s| Uuid::parse_str(&s)).transpose()?, - fault, - agent_journal: agent.map(|s| Uuid::parse_str(&s)).transpose()?, - agent_checkpoint, - }) -} -fn validate_history( - db: &Connection, - binding: &Binding, - meta: &Meta, - generation: u64, -) -> Result<()> { - let highest: u64 = db.query_row( - "SELECT COALESCE(MAX(checkpoint),0) FROM helper_sync_captures", - [], - |r| r.get(0), - )?; - ensure!( - highest == meta.checkpoint, - "HELPER_CAPTURE_HISTORY_ROLLBACK" - ); - let decision_count: u64 = - db.query_row("SELECT count(*) FROM helper_sync_decisions", [], |r| { - r.get(0) - })?; - if let Some(id) = meta.decision { - let stored: Option> = db - .query_row( - "SELECT response FROM helper_sync_decisions WHERE decision_id=?1", - [id.to_string()], - |r| r.get(0), - ) - .optional()?; - let response: SignedResponse = - serde_json::from_slice(&stored.context("HELPER_DECISION_HISTORY_MISSING_LOCALLY")?)?; - // Historical already-applied evidence is checked at its ORIGINAL issue - // time; it never creates a lease or bypasses the fresh-process capture. - let parsed: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?; - let previous = verify(binding, &response, parsed.issued_at)?; - let highest_decision: u64 = db.query_row( - "SELECT COALESCE(MAX(checkpoint),0) FROM helper_sync_decisions", - [], - |r| r.get(0), - )?; - ensure!( - previous.decision_id == id - && previous.helper_journal_id == Some(meta.journal_id) - && previous.helper_checkpoint == highest_decision - && previous.helper_checkpoint <= meta.checkpoint - && generation >= previous.connection_generation - && meta.agent_journal == Some(previous.journal_id) - && meta.agent_checkpoint >= previous.checkpoint, - "HELPER_DECISION_HISTORY_ROLLBACK" - ); - } else { - ensure!( - decision_count == 0 && meta.agent_journal.is_none() && meta.agent_checkpoint == 0, - "HELPER_METADATA_HISTORY_ROLLBACK" - ); - } - Ok(()) -} -/// Bootstrap-only historical verification; this never creates a lease. -pub fn verify_checkpoint_repair_history( - db: &Connection, - binding: &Binding, - links: Option<( - &rmm_domain::checkpoint_repair::Receipt, - &rmm_domain::checkpoint_repair::Evidence, - )>, -) -> Result<()> { - let meta = metadata(db)?; - validate_history(db, binding, &meta, meta.generation)?; - let mut statement = db.prepare( - "SELECT response,request,checkpoint FROM helper_sync_decisions ORDER BY checkpoint", - )?; - let entries = statement - .query_map([], |r| { - Ok(( - r.get::<_, Vec>(0)?, - r.get::<_, Vec>(1)?, - r.get::<_, u64>(2)?, - )) - })? - .collect::>>()?; - ensure!(entries.len() <= 100000, "REPAIR_HISTORY_LIMIT"); - let mut previous = None; - let mut previous_checkpoint = 0; - let mut previous_agent_checkpoint = 0; - for (signed, raw, checkpoint) in entries { - let response: SignedResponse = serde_json::from_slice(&signed)?; - let parsed: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?; - let decision = verify(binding, &response, parsed.issued_at)?; - let request = RequestView::decode(&raw)?; - let captured: Vec = db.query_row( - "SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?1", - [checkpoint], - |r| r.get(0), - )?; - let capture: Snapshot = serde_json::from_slice(&captured)?; - ensure!( - decision.helper_journal_id == Some(meta.journal_id) - && decision.helper_checkpoint == checkpoint - && request.helper_journal_id == meta.journal_id.to_string() - && request.helper_checkpoint == checkpoint - && request.assignment_version == binding.assignment_version - && request.journal_complete - && decision.request_hash == hash(&raw) - && decision.request_id.to_string() == request.request_id - && decision.journal_id.to_string() == request.journal_id - && decision.checkpoint == request.checkpoint - && capture.journal_id == meta.journal_id - && capture.checkpoint == checkpoint - && capture.boot_id == decision.boot_id - && capture.captured_at <= decision.issued_at - && capture.previous_decision_id == previous - && checkpoint > previous_checkpoint - && decision.checkpoint >= previous_agent_checkpoint - && decision.connection_generation <= meta.generation - && request.helper_previous_decision_id - == capture - .previous_decision_id - .map(|v| v.to_string()) - .unwrap_or_default(), - "REPAIR_HELPER_HISTORY_BINDING" - ); - previous = Some(decision.decision_id); - previous_checkpoint = checkpoint; - previous_agent_checkpoint = decision.checkpoint; - } - let _ = load_rows(db, binding, false)?; - if let Some((r, e)) = links { - verify_checkpoint_repair_links(db, binding, r, e)?; - } - Ok(()) -} -fn verify_checkpoint_repair_links( - db: &Connection, - binding: &Binding, - r: &rmm_domain::checkpoint_repair::Receipt, - e: &rmm_domain::checkpoint_repair::Evidence, -) -> Result<()> { - let x = RequestView::decode(&e.uncommitted_request)?; - let y = RequestView::decode(&e.rejected_request)?; - let a: ReconcileDecisionPayload = - serde_json::from_slice(&e.previous_valid_response.decision_bytes)?; - let a = verify( - binding, - &SignedResponse { - decision_bytes: e.previous_valid_response.decision_bytes.clone(), - signature: e.previous_valid_response.signature.clone(), - }, - a.issued_at, - )?; - let old: Vec = db.query_row( - "SELECT response FROM helper_sync_decisions WHERE decision_id=?1", - [a.decision_id.to_string()], - |v| v.get(0), - )?; - let old: SignedResponse = serde_json::from_slice(&old)?; - ensure!( - old.decision_bytes == e.previous_valid_response.decision_bytes - && old.signature == e.previous_valid_response.signature - && a.decision_id == r.previous_valid_decision_id - && hash(&old.decision_bytes) == r.previous_valid_decision_sha256 - && a.reason == "ATTEMPT_RECONCILIATION_REQUIRED" - && !a.ready - && !a.helper_ready - && x.request_id == r.uncommitted_request_id.to_string() - && hash(&e.uncommitted_request) == r.uncommitted_request_sha256 - && y.request_id == r.rejected_request_id.to_string() - && hash(&e.rejected_request) == r.rejected_request_sha256 - && x.previous_decision_id == a.decision_id.to_string() - && x.previous_request_id.is_empty() - && x.helper_previous_decision_id == a.decision_id.to_string() - && y.helper_previous_decision_id == a.decision_id.to_string() - && y.previous_decision_id.is_empty() - && y.previous_request_id == x.request_id - && x.journal_id == r.agent_journal_id.to_string() - && y.journal_id == x.journal_id - && a.journal_id == r.agent_journal_id - && x.helper_journal_id == r.helper_journal_id.to_string() - && y.helper_journal_id == x.helper_journal_id - && a.helper_journal_id == Some(r.helper_journal_id) - && a.checkpoint < x.checkpoint - && x.checkpoint < y.checkpoint - && y.checkpoint == r.agent_checkpoint - && a.helper_checkpoint < x.helper_checkpoint - && x.helper_checkpoint < y.helper_checkpoint - && y.helper_checkpoint == r.helper_checkpoint - && x.assignment_version == binding.assignment_version - && y.assignment_version == binding.assignment_version - && x.journal_complete - && y.journal_complete, - "REPAIR_EXACT_UNSENT_CHAIN_REQUIRED" - ); - Ok(()) -} -fn persist_fault(db: &Connection, error: &anyhow::Error) { - let text = error.to_string(); - if [ - "OMITTED", - "ROLLBACK", - "LEDGER_CHANGED", - "SNAPSHOT_CHANGED", - "MISSING_LOCALLY", - "ROOT_CONSUMPTION_MISSING", - "UNKNOWN_LOCAL_STATE", - "RECOVERY_REQUIRED", - "BINDING_OR_KEY_CHANGED", - "LOCAL_START_MISSING", - "LOCAL_REQUEST_BINDING", - "LOCAL_OUTCOME_CONFLICT", - "LOCAL_SUCCESS_PROOF_MISSING", - "SIGNED_REQUEST_HASH_MISSING_OR_INVALID", - "ROOT_SIGNED_ATTEMPT_BINDING", - ] - .iter() - .any(|s| text.contains(s)) - { - let _ = db.execute( - "UPDATE helper_sync_meta SET fault=COALESCE(fault,?1) WHERE id=1", - [text], - ); - } -} -fn recovery_reason(reason: &str) -> bool { - reason.contains("ROLLBACK") - || reason.contains("RECOVERY_REQUIRED") - || matches!( - reason, - "MISSING_SERVER_JOB" - | "ATTEMPT_BINDING_MISMATCH" - | "ASSIGNMENT_MISMATCH" - | "JOURNAL_SEQUENCE_REGRESSION" - | "SERVER_CHECKPOINT_MISSING" - | "CHECKPOINT_CHAIN_MISMATCH" - | "JOURNAL_HISTORY_MISSING" - | "LOCAL_ATTEMPT_MISSING" - ) -} -type LedgerRow = ( - String, - String, - u64, - String, - String, - Option>, - Option>, - Option>, -); -fn ledger_rows(db: &Connection) -> Result> { - let mut statement=db.prepare("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 ON e.grant_id=c.grant_id ORDER BY c.grant_id")?; - Ok(statement - .query_map([], |r| { - Ok(( - r.get(0)?, - r.get(1)?, - r.get(2)?, - r.get(3)?, - r.get(4)?, - r.get(5)?, - r.get(6)?, - r.get(7)?, - )) - })? - .collect::>()?) -} -fn ledger_fingerprint(db: &Connection) -> Result { - Ok(hash(&serde_json::to_vec(&ledger_rows(db)?)?)) -} -fn canonical(bytes: &[u8]) -> Result> { - let o: ReconciledOutcome = serde_json::from_slice(bytes)?; - Ok(canonical_outcome_bytes(&o)?) -} -fn load_rows(db: &Connection, binding: &Binding, refresh: bool) -> Result> { - let mut result = Vec::new(); - for (grant, agent, generation, job_id, attempt_id, finished, start, last) in ledger_rows(db)? { - let start = start.context("LOCAL_START_MISSING_POSSIBLE_EFFECT")?; - let start: serde_json::Value = serde_json::from_slice(&start)?; - let original_boot_id = Uuid::parse_str( - start["previous_boot_id"] - .as_str() - .context("LOCAL_ORIGINAL_BOOT_MISSING")?, - )?; - let request: Request = serde_json::from_value(start["request"].clone())?; - let permit: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes)?; - // Historical verification checks the original valid grant window, NOT - // authority to start now. Root consumption remains permanently recorded. - let (job, verified) = binding.verify(&request, permit.not_before)?; - ensure!( - verified == permit - && job.execution_identity == "root" - && grant == permit.grant_id.to_string() - && agent == binding.agent_id.to_string() - && generation == binding.enrollment_generation - && job_id == job.job_id.to_string() - && attempt_id == job.attempt_id.to_string(), - "LOCAL_REQUEST_BINDING" - ); - if let (Some(a), Some(b)) = (&finished, &last) { - ensure!(canonical(a)? == canonical(b)?, "LOCAL_OUTCOME_CONFLICT"); - } - let outcome_bytes = last - .as_deref() - .or(finished.as_deref()) - .map(canonical) - .transpose()? - .unwrap_or_default(); - ensure!(outcome_bytes.len() <= 512 * 1024, "LOCAL_OUTCOME_SIZE"); - let state = if outcome_bytes.is_empty() { - "unknown".to_string() - } else { - let o: ReconciledOutcome = serde_json::from_slice(&outcome_bytes)?; - o.state - }; - ensure!( - matches!( - state.as_str(), - "unknown" | "awaiting_reboot" | "succeeded" | "failed" | "cancelled" | "expired" - ), - "UNKNOWN_LOCAL_STATE" - ); - ensure!(!original_boot_id.is_nil(), "LOCAL_ORIGINAL_BOOT_MISSING"); - if state == "succeeded" { - let o: ReconciledOutcome = serde_json::from_slice(&outcome_bytes)?; - ensure!( - o.evidence["postconditions_passed"] == true, - "LOCAL_SUCCESS_PROOF_MISSING" - ); - if let Action::RunScript { - expected_stdout, .. - } = &job.action - { - ensure!( - o.exit_code == Some(0) - && expected_stdout - .as_ref() - .is_none_or(|s| !o.output_truncated && o.stdout == s.as_bytes()), - "LOCAL_SUCCESS_PROOF_MISSING" - ); - } - } - let mut row = LocalAttempt { - job_id: job.job_id, - attempt_id: job.attempt_id, - job_payload_hash: hash(&request.job_bytes), - grant_id: permit.grant_id, - authorization_id: permit.authorization_id, - plan_hash: permit.plan_hash, - action: job.action.name().into(), - execution_identity: job.execution_identity, - revision: 0, - original_boot_id, - consumption: "consumed".into(), - state, - outcome_sha256: if outcome_bytes.is_empty() { - String::new() - } else { - hash(&outcome_bytes) - }, - outcome_bytes, - original_request_sha256: hash(&serde_json::to_vec(&request)?), - }; - let fingerprint = hash(&serde_json::to_vec(&row)?); - let old: Option<(u64, String)> = db - .query_row( - "SELECT revision,fingerprint FROM helper_sync_rows WHERE grant_id=?1", - [&grant], - |r| Ok((r.get(0)?, r.get(1)?)), - ) - .optional()?; - row.revision = match old { - Some((revision, old)) => { - if old == fingerprint { - revision - } else { - ensure!(refresh, "ROOT_ROW_CHANGED_WITHOUT_SNAPSHOT"); - revision.checked_add(1).context("REVISION_OVERFLOW")? - } - } - None => { - ensure!(refresh, "ROOT_ROW_MISSING_WITHOUT_SNAPSHOT"); - 1 - } - }; - i64::try_from(row.revision)?; - if refresh { - db.execute("INSERT INTO helper_sync_rows(grant_id,revision,fingerprint) VALUES(?1,?2,?3) ON CONFLICT(grant_id) DO UPDATE SET revision=excluded.revision,fingerprint=excluded.fingerprint",params![grant,row.revision,fingerprint])?; - } - result.push(row); - } - let retained: u64 = db.query_row("SELECT count(*) FROM helper_sync_rows", [], |r| r.get(0))?; - ensure!( - retained == result.len() as u64, - "ROOT_CONSUMPTION_MISSING_LOCALLY" - ); - Ok(result) -} -fn definitive(row: &LocalAttempt) -> bool { - matches!( - row.state.as_str(), - "succeeded" | "failed" | "cancelled" | "expired" - ) && !row.outcome_bytes.is_empty() -} -fn validate_summary(row: &LocalAttempt, a: &AttemptView) -> Result<()> { - ensure!( - a.job_payload_hash == row.job_payload_hash - && a.grant_id == row.grant_id.to_string() - && a.consumption == "consumed" - && a.state == row.state - && a.revision >= row.revision - && a.original_boot_id == row.original_boot_id.to_string() - && a.outcome_sha256 == row.outcome_sha256 - && (a.outcome_bytes.is_empty() || a.outcome_bytes == row.outcome_bytes), - "ROOT_SNAPSHOT_REQUEST_MISMATCH" - ); - Ok(()) -} -fn validate_decision(row: &LocalAttempt, a: &AttemptView, d: &AttemptDecision) -> Result<()> { - ensure!( - d.job_payload_hash == row.job_payload_hash - && d.grant_id == Some(row.grant_id) - && d.authorization_id == Some(row.authorization_id) - && d.plan_hash.as_deref() == Some(row.plan_hash.as_str()) - && d.action == row.action - && d.execution_identity == "root" - && d.original_boot_id == Some(row.original_boot_id) - && (d.revision >= a.revision || (!d.unlock && d.outcome_state == "unknown")) - && d.previous_outcome_sha256.as_deref() - == (!row.outcome_sha256.is_empty()).then_some(row.outcome_sha256.as_str()), - "ROOT_SIGNED_ATTEMPT_BINDING" - ); - ensure!( - !d.unlock - || matches!( - d.outcome_state.as_str(), - "succeeded" | "failed" | "cancelled" | "expired" - ), - "UNKNOWN_CANNOT_UNLOCK" - ); - Ok(()) -} -fn unlock_outcome(row: &LocalAttempt, d: &AttemptDecision, current_boot: Uuid) -> Result> { - let bytes = if d.outcome_bytes.is_empty() { - row.outcome_bytes.clone() - } else { - d.outcome_bytes.clone() - }; - let o: ReconciledOutcome = serde_json::from_slice(&bytes)?; - ensure!( - canonical_outcome_bytes(&o)? == bytes - && d.outcome_sha256.as_deref() == Some(hash(&bytes).as_str()) - && o.state == d.outcome_state, - "ROOT_CANONICAL_UNLOCK_OUTCOME" - ); - if bytes != row.outcome_bytes { - ensure!( - row.action == "reboot" - && matches!(row.state.as_str(), "unknown" | "awaiting_reboot") - && o.state == "succeeded" - && row.original_boot_id != current_boot - && o.evidence["postconditions_passed"] == true - && o.evidence["reconciled_after_reboot"] == true - && o.evidence["new_boot_id"].as_str() == Some(current_boot.to_string().as_str()) - && o.evidence["inventory_message_id"] - .as_str() - .is_some_and(|s| Uuid::parse_str(s).is_ok_and(|id| !id.is_nil())), - "ROOT_REBOOT_NATIVE_BOOT_PROOF_REQUIRED" - ); - } else { - ensure!(definitive(row), "UNKNOWN_LOCAL_EFFECT_CANNOT_UNLOCK"); - if o.state == "succeeded" { - ensure!( - o.evidence["postconditions_passed"] == true, - "ROOT_POSTCONDITION_MISSING" - ); - } - if matches!(o.state.as_str(), "cancelled" | "expired") { - ensure!( - o.evidence["not_started"] == true - || (o.state == "cancelled" && o.evidence["abort_confirmed"] == true), - "ROOT_ABORT_NOT_PROVEN" - ); - } - if o.state == "failed" { - ensure!(!o.error_code.is_empty(), "ROOT_FAILURE_NOT_PROVEN"); - } - } - Ok(bytes) -} - -// The helper manifest deliberately has no prost/protocol dependency. This small -// bounded parser reads the EXISTING SyncRequest tags (1..12) and SyncAttempt -// tags (1..13), not a parallel public protocol. Glue passes the actual canonical -// prost bytes. Unknown/duplicate fields, wrong wire types and overlong varints -// fail closed. Nested JobResult/Envelope are not trusted as root outcome proof. -#[derive(Default)] -struct RequestView { - request_id: String, - journal_id: String, - checkpoint: u64, - assignment_version: u64, - attempts: Vec, - journal_complete: bool, - helper_journal_id: String, - helper_checkpoint: u64, - helper_previous_decision_id: String, - previous_decision_id: String, - previous_request_id: String, -} -#[derive(Default)] -struct AttemptView { - job_id: String, - attempt_id: String, - job_payload_hash: String, - grant_id: String, - consumption: String, - state: String, - revision: u64, - original_boot_id: String, - outcome_sha256: String, - outcome_bytes: Vec, -} -enum Field<'a> { - Number(u64), - Bytes(&'a [u8]), -} -fn varint(bytes: &[u8], offset: &mut usize) -> Result { - let mut n = 0u64; - for i in 0..10 { - let b = *bytes.get(*offset).context("PROTOBUF_TRUNCATED")?; - *offset += 1; - ensure!(i != 9 || b <= 1, "PROTOBUF_VARINT_OVERFLOW"); - n |= u64::from(b & 127) << (7 * i); - if b & 128 == 0 { - ensure!(i == 0 || b != 0, "PROTOBUF_NONCANONICAL_VARINT"); - return Ok(n); - } - } - anyhow::bail!("PROTOBUF_VARINT_OVERFLOW") -} -fn fields(bytes: &[u8]) -> Result)>> { - let mut offset = 0; - let mut result = Vec::new(); - while offset < bytes.len() { - ensure!(result.len() < 2048, "PROTOBUF_FIELD_LIMIT"); - let tag = varint(bytes, &mut offset)?; - let number = u32::try_from(tag >> 3)?; - ensure!(number > 0, "PROTOBUF_ZERO_TAG"); - let value = match tag & 7 { - 0 => Field::Number(varint(bytes, &mut offset)?), - 2 => { - let len = usize::try_from(varint(bytes, &mut offset)?)?; - let end = offset - .checked_add(len) - .context("PROTOBUF_LENGTH_OVERFLOW")?; - let value = bytes.get(offset..end).context("PROTOBUF_TRUNCATED")?; - offset = end; - Field::Bytes(value) - } - _ => anyhow::bail!("PROTOBUF_WIRE_TYPE"), - }; - result.push((number, value)); - } - Ok(result) -} -fn text(field: Field<'_>) -> Result { - match field { - Field::Bytes(b) => { - ensure!(b.len() <= 1024, "PROTOBUF_STRING_SIZE"); - Ok(std::str::from_utf8(b)?.to_string()) - } - _ => anyhow::bail!("PROTOBUF_EXPECTED_STRING"), - } -} -fn number(field: Field<'_>) -> Result { - match field { - Field::Number(n) => Ok(n), - _ => anyhow::bail!("PROTOBUF_EXPECTED_NUMBER"), - } -} -fn bytes(field: Field<'_>) -> Result> { - match field { - Field::Bytes(b) => Ok(b.to_vec()), - _ => anyhow::bail!("PROTOBUF_EXPECTED_BYTES"), - } -} -impl RequestView { - fn decode(raw: &[u8]) -> Result { - ensure!(raw.len() <= 1024 * 1024, "SYNC_REQUEST_SIZE"); - let mut r = Self::default(); - let mut seen = BTreeSet::new(); - let mut receipts = 0usize; - for (tag, value) in fields(raw)? { - ensure!( - (1..=12).contains(&tag) && (matches!(tag, 6 | 7) || seen.insert(tag)), - "PROTOBUF_UNKNOWN_OR_DUPLICATE_FIELD" - ); - match tag { - 1 => r.request_id = text(value)?, - 2 => r.journal_id = text(value)?, - 3 => r.checkpoint = number(value)?, - 4 | 12 => { - let s = text(value)?; - if !s.is_empty() { - Uuid::parse_str(&s)?; - } - if tag == 4 { - r.previous_decision_id = s; - } else { - r.previous_request_id = s; - } - } - 5 => r.assignment_version = number(value)?, - 6 => { - ensure!(r.attempts.len() < 256, "SYNC_ATTEMPT_LIMIT"); - r.attempts.push(AttemptView::decode(&bytes(value)?)?); - } - 7 => { - receipts += 1; - ensure!(receipts <= 256, "SYNC_RECEIPT_LIMIT"); - let s = text(value)?; - ensure!(!Uuid::parse_str(&s)?.is_nil(), "SYNC_RECEIPT_ID"); - } - 8 => { - let n = number(value)?; - ensure!(n <= 1, "PROTOBUF_BOOL"); - r.journal_complete = n == 1; - } - 9 => r.helper_journal_id = text(value)?, - 10 => r.helper_checkpoint = number(value)?, - 11 => r.helper_previous_decision_id = text(value)?, - _ => unreachable!(), - } - } - ensure!( - !Uuid::parse_str(&r.request_id)?.is_nil() && !Uuid::parse_str(&r.journal_id)?.is_nil(), - "SYNC_REQUEST_IDENTITIES" - ); - i64::try_from(r.checkpoint)?; - i64::try_from(r.helper_checkpoint)?; - Ok(r) - } -} -impl AttemptView { - fn decode(raw: &[u8]) -> Result { - let mut r = Self::default(); - let mut seen = BTreeSet::new(); - for (tag, value) in fields(raw)? { - ensure!( - (1..=13).contains(&tag) && seen.insert(tag), - "PROTOBUF_ATTEMPT_FIELD" - ); - match tag { - 1 => r.job_id = text(value)?, - 2 => r.attempt_id = text(value)?, - 3 => r.job_payload_hash = text(value)?, - 4 => r.grant_id = text(value)?, - 5 => r.consumption = text(value)?, - 6 => r.state = text(value)?, - 7 => r.revision = number(value)?, - 8 => r.original_boot_id = text(value)?, - 9 | 13 => { - ensure!(bytes(value)?.len() <= 1024 * 1024, "SYNC_RESULT_SIZE"); - } - 10 => { - let _ = text(value)?; - } - 11 => r.outcome_sha256 = text(value)?, - 12 => { - r.outcome_bytes = bytes(value)?; - ensure!(r.outcome_bytes.len() <= 512 * 1024, "SYNC_OUTCOME_SIZE"); - } - _ => unreachable!(), - } - } - i64::try_from(r.revision)?; - Ok(r) - } -} -fn unique_summaries(rows: &[AttemptView]) -> Result> { - let mut map = BTreeMap::new(); - for row in rows { - let id = ( - Uuid::parse_str(&row.job_id)?, - Uuid::parse_str(&row.attempt_id)?, - ); - ensure!( - !id.0.is_nil() && !id.1.is_nil() && map.insert(id, row).is_none(), - "DUPLICATE_REQUEST_ATTEMPT" - ); - } - Ok(map) -} diff --git a/crates/privileged-helper/tests/reconcile.rs b/crates/privileged-helper/tests/reconcile.rs index 360e947..1b3bc0b 100644 --- a/crates/privileged-helper/tests/reconcile.rs +++ b/crates/privileged-helper/tests/reconcile.rs @@ -72,7 +72,7 @@ impl Drop for Fixture { } impl Fixture { fn new() -> Self { - let dir = PathBuf::from("/tmp/opencode").join(format!("helper-sync-{}", Uuid::new_v4())); + let dir = std::env::temp_dir().join(format!("helper-sync-{}", Uuid::new_v4())); fs::DirBuilder::new().mode(0o700).create(&dir).unwrap(); let uid = unsafe { libc::geteuid() }; let guard = ExecutorGuard::acquire(&dir, uid).unwrap(); diff --git a/deploy/shared-core-acceptance.md b/deploy/shared-core-acceptance.md index f3f5cd1..be12c45 100644 --- a/deploy/shared-core-acceptance.md +++ b/deploy/shared-core-acceptance.md @@ -1,5 +1,9 @@ # Native gemeinsame Core-Unitabnahme +**Helper-Sync-Profil tatsächlich ausgeführt, 2026-11-14:** +Fall `5704073c-a135-431c-aa84-fedc68a3de0b`, Linux 69/69 vor Windows 47/47, +keine Skips oder RMM-Wirkung. Auch dies ersetzt keine Windows-SYSTEM-Dienstprobe. + `shared_core_acceptance.py` führt ausschließlich die zwei source-/hashgebundenen Testprogramme `contracts` und `reconcile` aus: eigene temporäre SQLite-Fixturen, Testsignaturen, Fake-Clock und Fake-Channel. Keine RMM-Jobs, Hello-Wirkung, @@ -45,3 +49,26 @@ Fehlt eine Quittung, bleibt die Abnahme unvollständig; kein automatisches Resum keine Bereinigung oder Wiederholung. Native Unittests ersetzen weder den späteren Linux-Dienst-Syncnachweis des extrahierten Kerns noch Windows-WebUI-/Clock-/ Prozess-/Jobabnahmen. Der aktuelle Windowsclient erhält dadurch keine Jobfähigkeit. + +## Nachfolgendes festes Profil `helper-sync-v1` + +Eigenständige neue Qualifikationsmanifeste/Artefakte unter +`devstate/shared-core/helper-sync-v1/` und `devstate/windows-core/helper-sync-v1/`; +Originalartefakte, Manifeste und deren native Fälle bleiben unverändert. +Das feste Profil prüft Linux **5+21+22+21=69** Fälle, zuletzt den bestehenden +Linuxhelper mit echtem Unix-ExecutorGuard und Bootquelle an eigenen Fixturen. +Erst dessen vollständiger Nativbeleg erlaubt Windows **4+21+22=47** Fälle. +Portable Helperfälle verwenden einen ausdrücklich simulierten Ledgerguard und +qualifizieren keine Windows-Pipe, Volume-/File-ID oder SYSTEM-Wirkung. + +```sh +# GEPLANT, NICHT AUSGEFÜHRT: Parent übernimmt tatsächliche VM-Ausführung. +python3 deploy/shared_core_acceptance.py linux --profile helper-sync-v1 +python3 deploy/shared_core_acceptance.py windows --profile helper-sync-v1 --case-id +# Bei verlorener Antwort weiterhin nur lesen, mit demselben festen Profil: +python3 deploy/shared_core_acceptance.py finish-linux --profile helper-sync-v1 --case-id +python3 deploy/shared_core_acceptance.py finish-windows --profile helper-sync-v1 --case-id +``` + +Profil, Quellen und Artefakte sind an den privaten create-only Intent gebunden. +Es gibt weiterhin keinen frei wählbaren Programmpfad oder Argumentvektor. diff --git a/deploy/shared_core_acceptance.py b/deploy/shared_core_acceptance.py index 44fd35e..c635d78 100644 --- a/deploy/shared_core_acceptance.py +++ b/deploy/shared_core_acceptance.py @@ -23,7 +23,11 @@ from windows import client_bootstrap as windows ROOT = Path(__file__).resolve().parents[1] STATE = ROOT / 'devstate/shared-core/acceptance' -KINDS = ('contracts', 'reconcile') +PROFILES = { + 'original': {'linux': {'contracts':5, 'reconcile':20}, 'windows': {'contracts':4, 'reconcile':20}}, + 'helper-sync-v1': {'linux': {'contracts':5, 'reconcile':21, 'helper-reconcile':22, 'linux-helper':21}, 'windows': {'contracts':4, 'reconcile':21, 'helper-reconcile':22}}, +} +KINDS = ('contracts', 'reconcile') # Historical original qualification only. REMOTE_LINUX = '/var/lib/rmm-shared-core-acceptance' @@ -36,25 +40,36 @@ def write_new(path, value): contract.write_private(path, value) -def qualified(platform): - folder = ROOT/'devstate'/('shared-core' if platform == 'linux' else 'windows-core') - manifest = contract.protected_json(folder/'manifest.json') +def source_paths(profile): + contract.require(profile in PROFILES, 'fixed qualification profile') sources = [ROOT/'Cargo.toml', ROOT/'Cargo.lock', ROOT/'rust-toolchain.toml'] - for name in ('agent-core', 'domain', 'protocol'): + crates = ['agent-core', 'domain', 'protocol'] + if profile == 'helper-sync-v1': crates.extend(('agent', 'agent-linux', 'privileged-helper')) + for name in crates: sources.extend(p for p in (ROOT/'crates'/name).rglob('*') if p.is_file() and (p.suffix == '.rs' or p.name == 'Cargo.toml')) sources.extend((ROOT/'proto').glob('*.proto')) - actual = {str(p.relative_to(ROOT)): sha(p.read_bytes()) for p in sources} + if profile == 'helper-sync-v1': + sources.extend(ROOT/p for p in ('tools/apt-transaction/apt_transaction.py', 'tools/apt-transaction/hook', 'deploy/linux/rmm-apt-helper.service', 'tests/apt_transaction/fixtures/planning-canonical.json')) + return sources + + +def qualified(platform, profile='original'): + folder = ROOT/'devstate'/('shared-core' if platform == 'linux' else 'windows-core') + if profile != 'original': folder = folder/profile + manifest = contract.protected_json(folder/'manifest.json') + contract.require(manifest.get('qualification_profile', 'original') == profile, 'bound qualification profile') + actual = {str(p.relative_to(ROOT)): sha(p.read_bytes()) for p in source_paths(profile)} contract.require(manifest['source_files'] == actual and manifest['source_before_after_equal'] is True, 'qualified source bindings changed') target = 'x86_64-unknown-linux-gnu' if platform == 'linux' else 'x86_64-pc-windows-gnu' contract.require(manifest['schema_version'] == 1 and manifest['target'] == target and manifest['os_effects'] is False and manifest['native_vm_executed'] is False, 'fixed test-only qualification') contract.require(all(manifest['checks'][n] is True for n in ('rustfmt_check', 'linux_all_target_clippy_warnings_denied', 'windows_all_target_clippy_warnings_denied')), 'qualified checks') artifacts = {} - for kind, item in zip(KINDS, manifest['artifacts'], strict=True): + for kind, item in zip(PROFILES[profile][platform], manifest['artifacts'], strict=True): path = folder/(kind + ('.exe' if platform == 'windows' else '')) contract.require(item['path'] == str(path.relative_to(ROOT)) and not path.is_symlink() and path.is_file(), 'fixed artifact path') contract.require(stat.S_IMODE(path.stat().st_mode) == 0o600 and path.stat().st_uid == os.getuid(), 'private artifact owner/mode') contract.require(item['binary_sha256'] == sha(path.read_bytes()) and item['size_bytes'] == path.stat().st_size, 'artifact hash/size') - contract.require(item['test_count'] == (20 if kind == 'reconcile' else (5 if platform == 'linux' else 4)), 'fixed test count') + contract.require(item['test_count'] == PROFILES[profile][platform][kind], 'fixed test count') artifacts[kind] = item return dict(manifest_sha256=sha((folder/'manifest.json').read_bytes()), artifacts=artifacts) @@ -67,10 +82,12 @@ def linux_case(payload): ident = str(uuid.UUID(payload['id'])) require(ident == payload['id'] and payload['action'] in ('prepare', 'run', 'read'), 'fixed case') entries = payload['artifacts'] - require(set(entries) == {'contracts', 'reconcile'}, 'two fixed test artifacts') + profile=payload.get('profile','original') + profiles={'original':{'contracts':5,'reconcile':20},'helper-sync-v1':{'contracts':5,'reconcile':21,'helper-reconcile':22,'linux-helper':21}} + require(profile in profiles and set(entries)==set(profiles[profile]), 'fixed qualification artifacts') for kind, item in entries.items(): require(len(item['binary_sha256']) == 64 and all(c in '0123456789abcdef' for c in item['binary_sha256']), 'fixed hash') - require(item['test_count'] == (5 if kind == 'contracts' else 20), 'fixed test count') + require(item['test_count'] == profiles[profile][kind], 'fixed test count') require(os.geteuid() == 0, 'bootstrap root only') def safe(path, directory): m = path.lstat() @@ -84,13 +101,13 @@ def linux_case(payload): case = root/ident if payload['action'] == 'prepare': case.mkdir(mode=0o700) - save(case/'intent.json', dict(id=ident, artifacts=entries, linux_manifest_sha256=payload['linux_manifest_sha256'], windows_manifest_sha256=payload['windows_manifest_sha256'], test_only=True)) + save(case/'intent.json', dict(id=ident, profile=profile, artifacts=entries, linux_manifest_sha256=payload['linux_manifest_sha256'], windows_manifest_sha256=payload['windows_manifest_sha256'], test_only=True)) for item in entries.values(): (case/item['binary_sha256']).mkdir(mode=0o700) return dict(prepared=True) safe(case, True); safe(case/'intent.json', False) intent = json.loads((case/'intent.json').read_bytes()) - require(intent['artifacts'] == entries and intent['linux_manifest_sha256'] == payload['linux_manifest_sha256'] and intent['windows_manifest_sha256'] == payload['windows_manifest_sha256'], 'same native intent') - kind = payload['kind']; require(kind in ('contracts', 'reconcile'), 'fixed test kind') + require(intent.get('profile','original') == profile and intent['artifacts'] == entries and intent['linux_manifest_sha256'] == payload['linux_manifest_sha256'] and intent['windows_manifest_sha256'] == payload['windows_manifest_sha256'], 'same native intent') + kind = payload['kind']; require(kind in profiles[profile], 'fixed test kind') result_path = case/(kind+'-result.json') if payload['action'] == 'read': safe(result_path, False) @@ -123,7 +140,10 @@ def linux_case(payload): WINDOWS = r''' $ErrorActionPreference='Stop' $p=([Console]::In.ReadLine() | ConvertFrom-Json) -if ([string]([guid]$p.id) -cne $p.id -or $p.kind -notin @('contracts','reconcile') -or $p.action -notin @('run','read') -or $p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.linux_evidence_sha256 -notmatch '^[0-9a-f]{64}$') {throw 'Fixed native core case'} +if (!$p.profile) {$p | Add-Member -NotePropertyName profile -NotePropertyValue 'original'} +$kinds=@('contracts','reconcile');if ($p.profile -eq 'helper-sync-v1') {$kinds+=@('helper-reconcile')} +if ($p.profile -notin @('original','helper-sync-v1')) {throw 'Fixed qualification profile'} +if ([string]([guid]$p.id) -cne $p.id -or $p.kind -notin $kinds -or $p.action -notin @('run','read') -or $p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.linux_evidence_sha256 -notmatch '^[0-9a-f]{64}$') {throw 'Fixed native core case'} $root='C:\ProgramData\RustDiscoveryBootstrap' $case=Join-Path $root ('shared-core-'+$p.id) function Safe([string]$path,[bool]$directory) { @@ -144,11 +164,11 @@ Safe $root $true if (!(Test-Path -LiteralPath $case)) { if ($p.action -ne 'run' -or $p.kind -ne 'contracts') {throw 'Native intent missing'} New-Item -ItemType Directory -Path $case | Out-Null - Write-New (Join-Path $case 'intent.json') @{id=$p.id;linux_evidence_sha256=$p.linux_evidence_sha256;test_only=$true} + Write-New (Join-Path $case 'intent.json') @{id=$p.id;profile=$p.profile;linux_evidence_sha256=$p.linux_evidence_sha256;test_only=$true} } Safe $case $true $intent=Get-Content -LiteralPath (Join-Path $case 'intent.json') -Raw | ConvertFrom-Json -if ($intent.id -cne $p.id -or $intent.linux_evidence_sha256 -cne $p.linux_evidence_sha256) {throw 'Bound Linux-first evidence'} +if ((!$intent.profile -and $p.profile -ne 'original') -or ($intent.profile -and $intent.profile -cne $p.profile) -or $intent.id -cne $p.id -or $intent.linux_evidence_sha256 -cne $p.linux_evidence_sha256) {throw 'Bound Linux-first evidence'} $result=Join-Path $case ($p.kind+'-result.json') if ($p.action -eq 'read') {Safe $result $false;Get-Content -LiteralPath $result -Raw;exit 0} Write-New (Join-Path $case ($p.kind+'-started.json')) @{kind=$p.kind;sha256=$p.sha256} @@ -180,7 +200,7 @@ def upload_linux(case, kind, item): def payload(intent, action, kind=None): - return dict(id=intent['id'], action=action, kind=kind, artifacts=intent['linux']['artifacts'], linux_manifest_sha256=intent['linux']['manifest_sha256'], windows_manifest_sha256=intent['windows']['manifest_sha256']) + return dict(id=intent['id'], profile=intent.get('profile','original'), action=action, kind=kind, artifacts=intent['linux']['artifacts'], linux_manifest_sha256=intent['linux']['manifest_sha256'], windows_manifest_sha256=intent['windows']['manifest_sha256']) def verify_result(platform, kind, result, expected): @@ -197,32 +217,34 @@ def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('action', choices=('linux', 'windows', 'finish-linux', 'finish-windows')) parser.add_argument('--case-id') + parser.add_argument('--profile', choices=tuple(PROFILES), default='original') args = parser.parse_args() if args.action == 'linux': contract.require(args.case_id is None, 'new Linux case chooses fresh UUID') ident = str(uuid.uuid4()); case = STATE/ident - intent = dict(id=ident, linux=qualified('linux'), windows=qualified('windows')) + intent = dict(id=ident, linux=qualified('linux',args.profile), windows=qualified('windows',args.profile)) + if args.profile != 'original': intent['profile']=args.profile write_new(case/'intent.json', intent) else: contract.require(args.case_id is not None and str(uuid.UUID(args.case_id)) == args.case_id, 'fixed existing case UUID') case = STATE/args.case_id intent = contract.protected_json(case/'intent.json') - contract.require(intent['id'] == args.case_id and intent['linux'] == qualified('linux') and intent['windows'] == qualified('windows'), 'same qualified case source/artifacts') + contract.require(intent['id'] == args.case_id and intent.get('profile','original')==args.profile and intent['linux'] == qualified('linux',args.profile) and intent['windows'] == qualified('windows',args.profile), 'same qualified case source/artifacts') platform = 'linux' if args.action.endswith('linux') else 'windows' if platform == 'windows': proof = contract.protected_json(case/'linux-passed.json') contract.require(proof['id'] == intent['id'] and proof['passed'] is True and proof['qualification'] == intent['linux'], 'Linux-first native qualification required') - for kind in KINDS: verify_result('linux', kind, proof['results'][kind], intent['linux']['artifacts'][kind]) + for kind in PROFILES[args.profile]['linux']: verify_result('linux', kind, proof['results'][kind], intent['linux']['artifacts'][kind]) linux_sha = sha((case/'linux-passed.json').read_bytes()) else: linux_sha = None finish = args.action.startswith('finish-') if not finish: - write_new(case/(platform+'-started.json'), dict(id=intent['id'], linux_evidence_sha256=linux_sha)) + write_new(case/(platform+'-started.json'), dict(id=intent['id'], profile=args.profile, linux_evidence_sha256=linux_sha)) if args.action == 'linux': linux.rpc('linux-vm.conf', linux_case, payload(intent, 'prepare')) - for kind in KINDS: upload_linux(case, kind, intent['linux']['artifacts'][kind]) + for kind in PROFILES[args.profile]['linux']: upload_linux(case, kind, intent['linux']['artifacts'][kind]) results = {} - for kind in KINDS: + for kind in PROFILES[args.profile][platform]: if platform == 'linux': result = linux.rpc('linux-vm.conf', linux_case, payload(intent, 'read' if finish else 'run', kind)) else: @@ -233,7 +255,7 @@ def main(): try: with contextlib.redirect_stdout(captured): if not finish: windows.upload(ROOT/item['path'], 'windows-core-'+kind+'.exe') - result = windows.powershell(WINDOWS, dict(id=intent['id'], kind=kind, action='read' if finish else 'run', sha256=item['binary_sha256'], linux_evidence_sha256=linux_sha), timeout=40) + result = windows.powershell(WINDOWS, dict(id=intent['id'], profile=args.profile, kind=kind, action='read' if finish else 'run', sha256=item['binary_sha256'], linux_evidence_sha256=linux_sha), timeout=40) finally: write_new(case/('windows-'+kind+'-transport-'+uuid.uuid4().hex+'.json'), dict(sanitized_bootstrap_output=captured.getvalue())) write_new(case/(platform+'-'+kind+'-readback-'+uuid.uuid4().hex+'.json'), result) diff --git a/deploy/windows/client_bootstrap.py b/deploy/windows/client_bootstrap.py index dcbd9be..5ab0fac 100644 --- a/deploy/windows/client_bootstrap.py +++ b/deploy/windows/client_bootstrap.py @@ -45,7 +45,7 @@ def powershell(script, payload=None, timeout=60, remote_signed=False): UPLOAD = r''' $ErrorActionPreference='Stop' $p=([Console]::In.ReadLine() | ConvertFrom-Json) -if ($p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile))\.exe|Install-PassiveClient\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe)$') {throw 'Artifact allowlist'} +if ($p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|Install-PassiveClient\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe)$') {throw 'Artifact allowlist'} $root='C:\ProgramData\RustDiscoveryBootstrap' if (!(Test-Path -LiteralPath $root)) { New-Item -ItemType Directory -Path $root | Out-Null @@ -68,7 +68,7 @@ if (Test-Path -LiteralPath $path) {throw 'Artifact already exists'} VERIFY_UPLOAD = r''' $ErrorActionPreference='Stop' $p=([Console]::In.ReadLine() | ConvertFrom-Json) -if ($p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile))\.exe|Install-PassiveClient\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe)$') {throw 'Artifact allowlist'} +if ($p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|Install-PassiveClient\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe)$') {throw 'Artifact allowlist'} $path='C:\ProgramData\RustDiscoveryBootstrap\'+$p.sha256+'\'+$p.name $hash=(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant() if ($hash -cne $p.sha256) {throw 'Artifact hash'} diff --git a/docs/handover.md b/docs/handover.md index 7dc90d1..3ab3ecf 100644 --- a/docs/handover.md +++ b/docs/handover.md @@ -63,6 +63,11 @@ Server/Domain/WebUI unterstützen lokal geprüft feste Plattformvarianten; Clouddeployment steht noch aus. Vollständiger Restumfang: [Windows-Gates](windows-gates.md), [Umsetzungsplan](windows-remaining-implementation.md). +Danach gemeinsamer Helper-Sync mit explizit getrennten Root-/Systembindungen +portiert: im eigenen Fall `5704073c-a135-431c-aa84-fedc68a3de0b` zuerst +69 Linux- und danach 47 Windows-Fixturen nativ bestanden, ohne RMM-Wirkung oder +Dienstersetzung. Windows-SYSTEM-Helper/Named-Pipe-Integration bleibt separat offen. + Der bestehende Windows-11-LocalService bleibt passiv: gleiche Geräteidentität, Telemetriehistorie und Schlüssel; keine Jobs, Outbox oder Startgrants. Native WSS-/Inventar-/Tenantnachweise und sieben Rusttests liegen vor. Explizites HTTPS diff --git a/docs/implementation-status.md b/docs/implementation-status.md index 1f4ca13..93dae7e 100644 --- a/docs/implementation-status.md +++ b/docs/implementation-status.md @@ -1,5 +1,22 @@ # Tatsächlicher Implementierungsstand +## Gemeinsamer Helper-Sync nativ geprüft — 2026-11-14 + +Der bisherige Linux-Helper-Sync ist jetzt ebenfalls Teil von `rmm-agent-core`. +Linux behält seine tatsächlichen Datei-/Lock-/Boot-/Clockadapter und den +OpenSSL-Verifier. Der gemeinsame Kern erhält explizite `Root`-/`System`- +Konfiguration; Windows wird niemals durch ein falsches `root`-Label dargestellt. +Historische Snapshot-/LocalAttempt-/SignedResponse-Codecs bleiben bytegleich. + +Lokal bestanden: 174 betroffene Tests, Linux-/Windows-Clippy und Formatprüfung. +Im getrennten nativen Fall `5704073c-a135-431c-aa84-fedc68a3de0b` zuerst +69 Linuxfälle einschließlich 21 tatsächlicher Linux-Datei-/Lockadapter-Fixturen, +danach 47 portable Windowsfälle ohne Skip bestanden. Quell-/Artefaktbindungen +liegen separat unter `devstate/{shared-core,windows-core}/helper-sync-v1/`; +Rohbelege unter `devstate/shared-core/acceptance//`. Frühere Belege bleiben +unverändert. Keine Dienste ersetzt, keine RMM-Jobs, Systemaktionen oder neue +Grants. Dies ist kein nativer Windows-SYSTEM-/Named-Pipe-Nachweis. + ## Gemeinsamer Jobkern nativ auf Linux und Windows geprüft — 2026-11-14 Der neue `rmm-agent-core` enthält die gemeinsame signierte Sync-/Checkpoint-