diff --git a/Cargo.lock b/Cargo.lock index a2ec760..08bd7ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2184,6 +2184,7 @@ dependencies = [ "prost", "rand 0.9.5", "reqwest", + "rmm-agent-core", "rmm-agent-linux", "rmm-domain", "rmm-privileged-helper", @@ -2199,6 +2200,24 @@ dependencies = [ "uuid", ] +[[package]] +name = "rmm-agent-core" +version = "0.1.0" +dependencies = [ + "anyhow", + "chrono", + "ed25519-dalek", + "hex", + "openssl", + "prost", + "rmm-domain", + "rmm-protocol", + "rusqlite", + "serde", + "serde_json", + "uuid", +] + [[package]] name = "rmm-agent-linux" version = "0.1.0" @@ -2217,6 +2236,7 @@ name = "rmm-agent-windows" version = "0.1.0" dependencies = [ "anyhow", + "cc", "chrono", "fs2", "futures-util", @@ -2224,6 +2244,7 @@ dependencies = [ "rand 0.9.5", "rcgen", "reqwest", + "rmm-agent-core", "rmm-domain", "rmm-protocol", "rusqlite", @@ -2292,6 +2313,7 @@ dependencies = [ "hex", "libc", "openssl", + "rmm-agent-core", "rmm-agent-linux", "rmm-domain", "rusqlite", diff --git a/Cargo.toml b/Cargo.toml index dec2728..ffa935a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] resolver = "2" exclude = ["crates/oidc-auth"] -members = ["crates/domain", "crates/protocol", "crates/persistence", "crates/server", "crates/agent-linux", "crates/agent-windows", "crates/agent", "crates/privileged-helper"] +members = ["crates/domain", "crates/protocol", "crates/persistence", "crates/server", "crates/agent-linux", "crates/agent-core", "crates/agent-windows", "crates/agent", "crates/privileged-helper"] [workspace.package] version = "0.1.0" diff --git a/crates/agent-core/Cargo.toml b/crates/agent-core/Cargo.toml new file mode 100644 index 0000000..810367e --- /dev/null +++ b/crates/agent-core/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "rmm-agent-core" +version.workspace = true +edition.workspace = true +license.workspace = true + +[features] +default = [] +openssl-verifier = ["dep:openssl"] + +[dependencies] +anyhow.workspace = true +chrono.workspace = true +hex.workspace = true +prost.workspace = true +serde.workspace = true +serde_json.workspace = true +uuid.workspace = true +rmm-domain = { path = "../domain" } +rmm-protocol = { path = "../protocol" } +rusqlite = { version = "0.32.1", features = ["bundled"] } +ed25519-dalek = "=2.2.0" + +[target.'cfg(unix)'.dependencies] +openssl = { workspace = true, optional = true } + +[target.'cfg(unix)'.dev-dependencies] +openssl.workspace = true diff --git a/crates/agent-core/src/auth.rs b/crates/agent-core/src/auth.rs new file mode 100644 index 0000000..6404bc1 --- /dev/null +++ b/crates/agent-core/src/auth.rs @@ -0,0 +1,140 @@ +//! Exact historical helper request/binding codecs and signed permit rules. +use crate::crypto::{DefaultVerifier, Ed25519Verifier}; +use anyhow::{Result, ensure}; +use chrono::{DateTime, Utc}; +use rmm_domain::jobs::{ExecutionPermitPayload, JobPayload, hash}; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Binding { + pub tenant_id: Uuid, + pub asset_id: Uuid, + pub agent_id: Uuid, + pub enrollment_generation: u64, + pub assignment_version: u64, + pub public_key_hex: String, +} +pub type AuthBinding = Binding; + +impl Binding { + pub fn job(&self, bytes: &[u8]) -> Result { + ensure!(bytes.len() <= 65536, "JOB_SIZE"); + let j: JobPayload = serde_json::from_slice(bytes)?; + ensure!( + j.schema_version == 1 + && j.tenant_id == self.tenant_id + && j.asset_id == self.asset_id + && j.agent_id == self.agent_id + && j.enrollment_generation == self.enrollment_generation + && j.assignment_version == self.assignment_version, + "IDENTITY_BINDING" + ); + ensure!( + j.action.validate() + && (1..=3600).contains(&j.max_runtime_seconds) + && (1..=262144).contains(&j.max_output_bytes) + && j.not_before < j.start_before, + "INVALID_JOB" + ); + Ok(j) + } + pub fn verify( + &self, + request: &Request, + now: DateTime, + ) -> Result<(JobPayload, ExecutionPermitPayload)> { + self.verify_with(request, now, &DefaultVerifier::default()) + } + pub fn verify_with( + &self, + request: &Request, + now: DateTime, + verifier: &impl Ed25519Verifier, + ) -> Result<(JobPayload, ExecutionPermitPayload)> { + let job = self.job(&request.job_bytes)?; + ensure!( + request.permit_bytes.len() <= 8192 && request.signature.len() == 64, + "PERMIT_SIZE" + ); + let raw = hex::decode(&self.public_key_hex)?; + ensure!(raw.len() == 32, "TRUST_KEY_SIZE"); + ensure!( + verifier.verify(&raw, &request.permit_bytes, &request.signature)?, + "INVALID_SIGNATURE" + ); + let p: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes)?; + ensure!( + p.domain == "rmm.execution-permit" + && p.schema_version == 1 + && p.key_id == hash(&raw) + && !p.grant_id.is_nil() + && !p.authorization_id.is_nil(), + "PERMIT_DOMAIN" + ); + ensure!( + p.authorization_id == request.authorization_id + && p.plan_hash == request.plan_hash + && p.plan_hash.len() == 64 + && hex::decode(&p.plan_hash)?.len() == 32 + && p.job_payload_hash == hash(&request.job_bytes) + && p.tenant_id == job.tenant_id + && p.asset_id == job.asset_id + && p.agent_id == job.agent_id + && p.enrollment_generation == job.enrollment_generation + && p.assignment_version == job.assignment_version + && p.job_id == job.job_id + && p.attempt_id == job.attempt_id + && p.action == job.action.name() + && p.execution_identity == job.execution_identity + && p.max_runtime_seconds == job.max_runtime_seconds, + "PERMIT_BINDING" + ); + ensure!( + p.not_before >= job.not_before + && p.start_before <= job.start_before + && p.not_before < p.start_before + && (p.start_before - p.not_before).num_seconds() <= 60 + && now >= p.not_before + && now < p.start_before, + "PERMIT_TIME" + ); + Ok((job, p)) + } +} + +#[derive(Clone, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Request { + pub job_bytes: Vec, + pub permit_bytes: Vec, + pub signature: Vec, + pub plan_hash: String, + pub authorization_id: Uuid, +} +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Outcome { + pub state: String, + pub stdout: Vec, + pub stderr: Vec, + pub exit_code: Option, + pub output_truncated: bool, + pub reboot_required: bool, + pub evidence: serde_json::Value, + pub error_code: String, +} +impl Outcome { + pub fn unknown(code: &str) -> Self { + Self { + 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: code.into(), + } + } +} diff --git a/crates/agent-core/src/crypto.rs b/crates/agent-core/src/crypto.rs new file mode 100644 index 0000000..e4f2548 --- /dev/null +++ b/crates/agent-core/src/crypto.rs @@ -0,0 +1,54 @@ +//! Fixed Ed25519 verification; no algorithm negotiation or trust-key discovery. +use anyhow::Result; +use ed25519_dalek::{Signature, VerifyingKey}; + +pub trait Ed25519Verifier { + fn verify(&self, public_key: &[u8], message: &[u8], signature: &[u8]) -> Result; +} + +#[derive(Clone, Copy, Debug, Default)] +pub struct DalekVerifier; + +impl Ed25519Verifier for DalekVerifier { + 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); + }; + let Ok(key) = VerifyingKey::from_bytes(raw) else { + return Ok(false); + }; + let Ok(signature) = Signature::from_slice(signature) else { + return Ok(false); + }; + // Reject weak points and scalar/point malleability. This is raw Ed25519, + // never Ed25519ph: the exact stored bytes are the signed message. + Ok(key.verify_strict(message, &signature).is_ok()) + } +} + +/// Existing Linux verifier semantics, retained explicitly during extraction. +#[cfg(all(unix, feature = "openssl-verifier"))] +#[derive(Clone, Copy, Debug, Default)] +pub struct OpenSslVerifier; + +#[cfg(all(unix, feature = "openssl-verifier"))] +impl Ed25519Verifier for OpenSslVerifier { + fn verify(&self, public_key: &[u8], message: &[u8], signature: &[u8]) -> Result { + use openssl::{ + pkey::{Id, PKey}, + sign::Verifier, + }; + if public_key.len() != 32 || signature.len() != 64 { + return Ok(false); + } + let key = PKey::public_key_from_raw_bytes(public_key, Id::ED25519)?; + Ok(Verifier::new_without_digest(&key)?.verify_oneshot(signature, message)?) + } +} + +/// Preserve the existing Linux OpenSSL profile when selected by its adapter; +/// Windows never depends on OpenSSL, including workspace feature unification. +#[cfg(all(unix, feature = "openssl-verifier"))] +pub type DefaultVerifier = OpenSslVerifier; +#[cfg(not(all(unix, feature = "openssl-verifier")))] +pub type DefaultVerifier = DalekVerifier; diff --git a/crates/agent-core/src/jobs.rs b/crates/agent-core/src/jobs.rs new file mode 100644 index 0000000..0c4605b --- /dev/null +++ b/crates/agent-core/src/jobs.rs @@ -0,0 +1,274 @@ +//! Durable job persistence without capability discovery or OS execution. +//! Caller performs fresh Sync/Clock/permit checks before consuming or executing. +use crate::auth::{Binding, Outcome, Request}; +use anyhow::{Result, ensure}; +use prost::Message; +use rmm_domain::jobs::{JobPayload, hash}; +use rmm_protocol::*; +use rusqlite::{Connection, OptionalExtension, params}; +use uuid::Uuid; + +pub fn accept( + db: &Connection, + binding: &Binding, + offer: &JobOffer, + supported: impl FnOnce(&JobPayload) -> bool, +) -> Result { + let job = binding.job(&offer.payload_bytes)?; + ensure!( + offer.job_id == job.job_id.to_string() + && offer.attempt_id == job.attempt_id.to_string() + && offer.job_payload_hash == hash(&offer.payload_bytes) + && Uuid::parse_str(&offer.authorization_id).is_ok_and(|id| !id.is_nil()) + && offer.plan_hash.len() == 64 + && hex_hash(&offer.plan_hash), + "OFFER_BINDING" + ); + ensure!(supported(&job), "UNSUPPORTED_CAPABILITY"); + let bytes = offer.encode_to_vec(); + let existing: Option> = db + .query_row( + "SELECT offer FROM job_attempts WHERE job=?1 AND attempt=?2", + params![offer.job_id, offer.attempt_id], + |r| r.get(0), + ) + .optional()?; + if let Some(old) = existing { + ensure!(old == bytes, "OFFER_CONFLICT"); + return Ok(false); + } + let count: i64 = db.query_row("SELECT count(*) FROM job_attempts", [], |r| r.get(0))?; + ensure!(count < 10000, "JOURNAL_CAPACITY_NEW_EFFECTS_BLOCKED"); + db.execute( + "INSERT INTO job_attempts(job,attempt,offer,state) VALUES(?1,?2,?3,'accepted')", + params![offer.job_id, offer.attempt_id, bytes], + )?; + Ok(true) +} +fn hex_hash(s: &str) -> bool { + s.bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) +} + +/// Inspect ONLY at a new process startup, never on a transport reconnect: no +/// executor thread from the previous process can still deliver its outcome. +/// This does not execute, release consumption or invent a successful outcome. +pub fn interrupted( + db: &Connection, + ledger: &Connection, + binding: &Binding, +) -> Result> { + interrupted_with( + db, + ledger, + binding, + &crate::crypto::DefaultVerifier::default(), + ) +} +pub fn interrupted_with( + db: &Connection, + ledger: &Connection, + binding: &Binding, + verifier: &impl crate::crypto::Ed25519Verifier, +) -> Result> { + let mut statement = db.prepare( + "SELECT offer,grant_id FROM job_attempts WHERE state IN ('requesting','running','verifying') ORDER BY job,attempt", + )?; + let rows = statement.query_map([], |r| { + Ok((r.get::<_, Vec>(0)?, r.get::<_, String>(1)?)) + })?; + let mut interrupted = Vec::new(); + for row in rows { + let (bytes, grant) = row?; + let offer = JobOffer::decode(bytes.as_slice())?; + let job = binding.job(&offer.payload_bytes)?; + ensure!( + offer.job_id == job.job_id.to_string() + && offer.attempt_id == job.attempt_id.to_string() + && offer.job_payload_hash == hash(&offer.payload_bytes) + && Uuid::parse_str(&offer.authorization_id).is_ok_and(|id| !id.is_nil()) + && offer.plan_hash.len() == 64 + && hex_hash(&offer.plan_hash), + "INTERRUPTED_OFFER_BINDING" + ); + if !grant.is_empty() { + let row: Option<(String, u64, String, String, Vec)> = ledger + .query_row( + "SELECT c.agent,c.generation,c.job,c.attempt,e.start FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?1", + [&grant], + |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?)), + ) + .optional()?; + let (agent, generation, job_id, attempt, start) = + row.ok_or_else(|| anyhow::anyhow!("INTERRUPTED_START_EVIDENCE_MISSING"))?; + ensure!( + agent == binding.agent_id.to_string() + && generation == binding.enrollment_generation + && job_id == offer.job_id + && attempt == offer.attempt_id, + "INTERRUPTED_CONSUMPTION_BINDING" + ); + let start: serde_json::Value = serde_json::from_slice(&start)?; + let request: Request = serde_json::from_value(start["request"].clone())?; + let permit: rmm_domain::jobs::ExecutionPermitPayload = + serde_json::from_slice(&request.permit_bytes)?; + // Verify the historical signed request at its original valid time; + // this is evidence only, never permission to start it again. + let (_, verified) = binding.verify_with(&request, permit.not_before, verifier)?; + ensure!( + verified == permit + && permit.grant_id.to_string() == grant + && request.job_bytes == offer.payload_bytes + && request.plan_hash == offer.plan_hash + && request.authorization_id.to_string() == offer.authorization_id, + "INTERRUPTED_START_BINDING" + ); + } else { + // A request may have committed remotely without its reply arriving. + // It can only become local unknown, never a fabricated grant/result. + let state: String = db.query_row( + "SELECT state FROM job_attempts WHERE job=?1 AND attempt=?2", + params![offer.job_id, offer.attempt_id], + |r| r.get(0), + )?; + ensure!(state == "requesting", "INTERRUPTED_RUNNING_GRANT_MISSING"); + } + interrupted.push((offer, grant)); + } + Ok(interrupted) +} + +pub fn interrupted_request(db: &Connection, offer: &JobOffer) -> Result<()> { + ensure!( + db.execute( + "UPDATE job_attempts SET state='unknown',revision=revision+1 WHERE job=?1 AND attempt=?2 AND state='requesting' AND grant_id=''", + params![offer.job_id, offer.attempt_id], + )? == 1, + "INTERRUPTED_REQUEST_STATE" + ); + Ok(()) +} +pub fn event(db: &Connection, envelope: &Envelope) -> Result<()> { + let count: i64 = db.query_row("SELECT count(*) FROM job_events", [], |r| r.get(0))?; + ensure!(count < 1000, "UNACKNOWLEDGED_EVENT_LIMIT"); + db.execute( + "INSERT INTO job_events(id,payload) VALUES(?1,?2)", + params![envelope.message_id, envelope.encode_to_vec()], + )?; + Ok(()) +} +pub fn events(db: &Connection) -> Result> { + let mut statement = db.prepare("SELECT payload FROM job_events ORDER BY rowid LIMIT 100")?; + let bytes = statement + .query_map([], |r| r.get::<_, Vec>(0))? + .collect::>>()?; + bytes + .into_iter() + .map(|b| Ok(Envelope::decode(b.as_slice())?)) + .collect() +} +pub fn ack(db: &mut Connection, id: &str) -> Result<()> { + let tx = db.transaction()?; + crate::reconcile::archive_events(&tx)?; + tx.execute("DELETE FROM job_events WHERE id=?1", [id])?; + tx.commit()?; + Ok(()) +} +pub fn next(db: &Connection) -> Result> { + next_at(db, chrono::Utc::now()) +} +/// Scheduling only; the qualified clock/Sync/permit gate still precedes effects. +pub fn next_at(db: &Connection, now: chrono::DateTime) -> Result> { + // One device-wide effect at a time; unknown/reserved attempts retain locks. + let busy: i64 = db.query_row("SELECT count(*) FROM job_attempts WHERE state IN ('requesting','running','unknown','awaiting_reboot')", [], |r| r.get(0))?; + if busy != 0 { + return Ok(None); + } + let mut statement = db.prepare("SELECT offer FROM job_attempts WHERE state='accepted' AND cancel_requested=0 ORDER BY rowid")?; + let rows = statement.query_map([], |r| r.get::<_, Vec>(0))?; + for bytes in rows { + let offer = JobOffer::decode(bytes?.as_slice())?; + let job: JobPayload = serde_json::from_slice(&offer.payload_bytes)?; + if now >= job.not_before || now >= job.start_before { + return Ok(Some(offer)); + } + } + Ok(None) +} +pub fn requesting(db: &Connection) -> Result> { + let bytes: Option> = db + .query_row( + "SELECT offer FROM job_attempts WHERE state='requesting' LIMIT 1", + [], + |r| r.get(0), + ) + .optional()?; + bytes + .map(|b| Ok(JobOffer::decode(b.as_slice())?)) + .transpose() +} +pub fn reserve_request(db: &Connection, offer: &JobOffer) -> Result<()> { + ensure!(db.execute("UPDATE job_attempts SET state='requesting' WHERE job=?1 AND attempt=?2 AND state='accepted' AND cancel_requested=0", + params![offer.job_id,offer.attempt_id])? == 1, "ATTEMPT_NOT_STARTABLE"); + Ok(()) +} +pub fn transition(db: &Connection, offer: &JobOffer, state: &str, grant: &str) -> Result { + let previous: String = db.query_row( + "SELECT state FROM job_attempts WHERE job=?1 AND attempt=?2", + params![offer.job_id, offer.attempt_id], + |r| r.get(0), + )?; + let allowed = match previous.as_str() { + "accepted" | "requesting" => matches!( + state, + "running" | "failed" | "cancelled" | "expired" | "unknown" + ), + "running" | "verifying" => matches!( + state, + "verifying" | "succeeded" | "failed" | "cancelled" | "unknown" | "awaiting_reboot" + ), + // Only a definitive completion of the already-running executor calls + // this reconciliation path. No new effect is started from unknown. + "unknown" | "awaiting_reboot" => matches!( + state, + "succeeded" | "failed" | "cancelled" | "unknown" | "awaiting_reboot" + ), + _ => false, + }; + ensure!(allowed, "INVALID_LOCAL_STATE_TRANSITION"); + let rev: i64 = db.query_row("UPDATE job_attempts SET state=?3,grant_id=?4,revision=revision+1 WHERE job=?1 AND attempt=?2 RETURNING revision", + params![offer.job_id,offer.attempt_id,state,grant], |r| r.get(0))?; + Ok(u64::try_from(rev)?) +} +pub fn result(offer: &JobOffer, revision: u64, grant: &str, outcome: Outcome) -> JobResult { + JobResult { + job_id: offer.job_id.clone(), + attempt_id: offer.attempt_id.clone(), + revision, + state: outcome.state, + grant_id: grant.into(), + stdout: outcome.stdout, + stderr: outcome.stderr, + output_truncated: outcome.output_truncated, + reboot_required: outcome.reboot_required, + exit_code: outcome.exit_code, + evidence_json: outcome.evidence.to_string(), + error_code: outcome.error_code, + } +} +pub fn no_start(state: &str, code: &str) -> Outcome { + Outcome { + state: state.into(), + evidence: serde_json::json!({"not_started":true,"postconditions_passed":false}), + ..Outcome::unknown(code) + } +} +pub fn permit_request(offer: &JobOffer, permit: &ExecutionPermit) -> Result { + Ok(Request { + job_bytes: offer.payload_bytes.clone(), + permit_bytes: permit.payload_bytes.clone(), + signature: permit.signature.clone(), + plan_hash: offer.plan_hash.clone(), + authorization_id: Uuid::parse_str(&offer.authorization_id)?, + }) +} diff --git a/crates/agent-core/src/ledger.rs b/crates/agent-core/src/ledger.rs new file mode 100644 index 0000000..f295d41 --- /dev/null +++ b/crates/agent-core/src/ledger.rs @@ -0,0 +1,103 @@ +//! Connection-level ledger rules, with OS path/lock/ACL handling left to adapters. +use crate::auth::{Outcome, Request}; +use anyhow::{Result, ensure}; +use chrono::{DateTime, Utc}; +use rmm_domain::jobs::{ExecutionPermitPayload, JobPayload}; +use uuid::Uuid; + +/// Finite 1 GiB bound for durable agent/helper history. Unexpected page sizes +/// are refused rather than silently multiplying the byte limit. No compaction +/// or tombstone deletion occurs here; SQLITE_FULL must still fail closed. +pub fn bounded_journal_capacity(db: &rusqlite::Connection) -> Result<()> { + let page_size: u32 = db.query_row("PRAGMA page_size", [], |r| r.get(0))?; + ensure!(page_size == 4096, "JOURNAL_PAGE_SIZE_UNSUPPORTED"); + let maximum: u32 = db.query_row("PRAGMA max_page_count=262144", [], |r| r.get(0))?; + ensure!(maximum == 262144, "JOURNAL_STORAGE_CAPACITY_BLOCKED"); + Ok(()) +} + +/// The platform adapter must securely open/lock the file before calling this. +/// No file creation, ACL repair or journal reset is performed here. +pub fn initialize_ledger(db: &rusqlite::Connection) -> Result<()> { + bounded_journal_capacity(db)?; + db.execute_batch("PRAGMA synchronous=FULL; PRAGMA journal_mode=DELETE; + CREATE TABLE IF NOT EXISTS consumed(grant_id TEXT PRIMARY KEY, agent TEXT NOT NULL, generation INTEGER NOT NULL, + job TEXT NOT NULL, attempt TEXT NOT NULL, result BLOB, UNIQUE(agent,generation,job,attempt)); + CREATE TABLE IF NOT EXISTS execution_evidence(grant_id TEXT PRIMARY KEY, start BLOB NOT NULL, outcome BLOB);")?; + let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?; + ensure!(check == "ok", "JOURNAL_INTEGRITY"); + Ok(()) +} + +pub fn consume(db: &mut rusqlite::Connection, p: &ExecutionPermitPayload) -> Result<()> { + let tx = db.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?; + // Unknown effects retain the global device lock, including across restart. + let unresolved: i64 = tx.query_row( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get(0), + )?; + ensure!(unresolved == 0, "UNRESOLVED_EFFECT_RESOURCE_LOCKED"); + tx.execute( + "INSERT INTO consumed(grant_id,agent,generation,job,attempt) VALUES(?1,?2,?3,?4,?5)", + rusqlite::params![ + p.grant_id.to_string(), + p.agent_id.to_string(), + p.enrollment_generation, + p.job_id.to_string(), + p.attempt_id.to_string() + ], + )?; + tx.commit()?; + Ok(()) +} +pub fn finish( + db: &rusqlite::Connection, + p: &ExecutionPermitPayload, + outcome: &Outcome, +) -> Result<()> { + db.execute( + "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", + rusqlite::params![p.grant_id.to_string(), serde_json::to_vec(outcome)?], + )?; + // Unknown / awaiting reboot keep the resource reserved until a real sync + // protocol exists. Never unlock on a process exit or a network reconnect. + if outcome.state != "unknown" && outcome.state != "awaiting_reboot" { + ensure!( + db.execute( + "UPDATE consumed SET result=?2 WHERE grant_id=?1 AND result IS NULL", + rusqlite::params![p.grant_id.to_string(), serde_json::to_vec(outcome)?] + )? == 1, + "CONSUMPTION_NOT_FOUND" + ); + } + Ok(()) +} + +/// Persist the original boot and exact signed request BEFORE any reboot/effect. +/// Awaiting/unknown outcomes never release consumed.result's resource lock. +pub fn save_start( + db: &rusqlite::Connection, + permit: &ExecutionPermitPayload, + request: &Request, + previous_boot_id: Uuid, + recorded_at: DateTime, +) -> Result<()> { + let job: JobPayload = serde_json::from_slice(&request.job_bytes)?; + ensure!(!previous_boot_id.is_nil(), "ORIGINAL_BOOT_MISSING"); + let deadline = match job.action { + rmm_domain::jobs::Action::Reboot { + reconnect_timeout_seconds, + .. + } => Some(recorded_at + chrono::Duration::seconds(reconnect_timeout_seconds.into())), + _ => None, + }; + let evidence = serde_json::json!({"previous_boot_id":previous_boot_id.to_string(), + "reserved_at":recorded_at,"reconnect_deadline":deadline,"request":request, + "reauthentication_required":true,"postconditions_passed":false}); + db.execute( + "INSERT INTO execution_evidence(grant_id,start) VALUES(?1,?2)", + rusqlite::params![permit.grant_id.to_string(), serde_json::to_vec(&evidence)?], + )?; + Ok(()) +} diff --git a/crates/agent-core/src/lib.rs b/crates/agent-core/src/lib.rs new file mode 100644 index 0000000..f25a6b4 --- /dev/null +++ b/crates/agent-core/src/lib.rs @@ -0,0 +1,9 @@ +//! Platform-neutral signed authorization, durable job state and reconciliation. +//! This crate performs no OS effects, enrollment, ACL changes or clock bootstrap. +//! Callers must authenticate their transport, protect/lock their own journals and +//! supply a qualified clock. A successful signature is never UI approval itself. +pub mod auth; +pub mod crypto; +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 new file mode 100644 index 0000000..d3b490c --- /dev/null +++ b/crates/agent-core/src/reconcile.rs @@ -0,0 +1,1241 @@ +//! Reconnect reconciliation, deliberately independent of main/jobs and transport. +//! No method executes a job, deletes consumption tombstones or issues a permit. +//! 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; +use crate::crypto::{DefaultVerifier, Ed25519Verifier}; +use anyhow::{Context, Result, ensure}; +use chrono::{DateTime, Utc}; +use prost::Message; +use rmm_domain::{ + jobs::{JobPayload, hash}, + reconciliation::{ + AttemptDecision, ReconcileDecisionPayload, ReconciledOutcome, canonical_outcome_bytes, + }, +}; +use rmm_protocol::{ + Envelope, JobOffer, JobResult, Payload, SyncAttempt, SyncRequest, SyncResponse, +}; +use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params}; +use std::collections::{BTreeMap, BTreeSet}; +use uuid::Uuid; + +pub trait SyncClock { + fn now(&self) -> Result>; +} +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Channel { + pub connection_generation: u64, + pub boot_id: Uuid, +} +#[derive(Clone, Debug)] +pub struct HelperSnapshot { + pub journal_id: Uuid, + pub checkpoint: u64, + pub previous_decision_id: Option, + /// Complete consumed/unsettled summaries from the root-owned ledger. + pub attempts: Vec, +} +#[derive(Clone)] +pub struct PreparedSync { + pub envelope: Envelope, + pub request: SyncRequest, +} +/// Pass the ORIGINAL signed response to the helper. It must independently verify +/// it, apply targeted unlocks and commit its checkpoint before acknowledging. +#[derive(Clone)] +pub struct HelperInstruction { + pub response: SyncResponse, + pub decision_id: Uuid, + pub journal_id: Uuid, + pub checkpoint: u64, +} +pub struct Applied { + pub decision_id: Uuid, + pub acknowledged_message_ids: Vec, + pub helper_instruction: Option, + pub reason: String, +} +struct Gate { + channel: Channel, + until: DateTime, + ready: bool, + helper_ready: bool, + helper: Option<(Uuid, u64, Uuid)>, + local_applied: bool, + request: SyncRequest, + response: SyncResponse, +} +pub struct ClientSync { + binding: Binding, + verifier: V, + pending: Option, + channel: Option, + gate: Option, +} +impl ClientSync { + /// Every new process starts closed, independently of any persisted ready bit. + pub fn new(binding: Binding) -> Self { + Self::with_verifier(binding, DefaultVerifier::default()) + } +} +impl ClientSync { + pub fn with_verifier(binding: Binding, verifier: V) -> Self { + Self { + binding, + verifier, + pending: None, + channel: None, + gate: None, + } + } + /// Invoke on every authenticated/fenced ServerHello or transport loss. + pub fn close(&mut self) { + self.gate = None; + self.channel = None; + self.pending = None; + } + pub fn validity_deadline(&self) -> Option> { + self.gate.as_ref().map(|gate| gate.until) + } + pub fn can_start(&self, channel: &Channel, root: bool, clock: &impl SyncClock) -> bool { + let Ok(now) = clock.now() else { + return false; + }; + self.gate.as_ref().is_some_and(|g| { + g.channel == *channel + && g.ready + && g.local_applied + && now < g.until + && (!root || (g.helper_ready && g.helper.is_none())) + }) + } + /// Only call with a fresh independently authenticated helper snapshot. There + /// is no unsigned ready/unlock flag: the exact signed decision/checkpoint + /// must already be durably applied by that helper. + pub fn confirm_helper(&mut self, snapshot: &HelperSnapshot) -> Result<()> { + let gate = self.gate.as_mut().context("NO_SIGNED_DECISION")?; + let expected = gate.helper.context("NO_HELPER_APPLY_PENDING")?; + ensure!( + snapshot.journal_id == expected.0 + && snapshot.checkpoint == expected.1 + && snapshot.previous_decision_id == Some(expected.2), + "HELPER_CHECKPOINT_NOT_APPLIED" + ); + ensure!( + snapshot + .attempts + .iter() + .all(|a| terminal(&a.state) + || (a.consumption == "unconsumed" && a.state == "accepted")), + "HELPER_EFFECT_UNRESOLVED" + ); + gate.helper = None; + Ok(()) + } + pub fn prepare( + &mut self, + db: &mut Connection, + ledger: &Connection, + channel: Channel, + helper: Option<&HelperSnapshot>, + clock: &impl SyncClock, + ) -> Result { + let result = self.prepare_inner(db, ledger, channel, helper, clock); + if let Err(error) = &result { + let text = error.to_string(); + if [ + "ROLLBACK", + "REGRESSION", + "DISAPPEARED", + "MISSING_FROM_AGENT_JOURNAL", + "MISSING_FROM_HELPER_JOURNAL", + ] + .iter() + .any(|s| text.contains(s)) + { + let _ = db.execute( + "UPDATE reconcile_meta SET fault=COALESCE(fault,?1) WHERE id=1", + [text], + ); + } + } + result + } + fn prepare_inner( + &mut self, + db: &mut Connection, + ledger: &Connection, + channel: Channel, + helper: Option<&HelperSnapshot>, + clock: &impl SyncClock, + ) -> Result { + self.close(); + let now = clock.now()?; + ensure!( + channel.connection_generation > 0 && !channel.boot_id.is_nil(), + "UNAUTHENTICATED_CHANNEL" + ); + initialize(db)?; + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + let meta = metadata(&tx)?; + ensure!(meta.fault.is_none(), "SYNC_MANUAL_RECOVERY_REQUIRED"); + let sequence: i64 = tx.query_row("SELECT seq FROM counter WHERE id=1", [], |r| r.get(0))?; + ensure!( + sequence >= meta.checkpoint && sequence >= meta.high_sequence, + "LOCAL_CHECKPOINT_ROLLBACK" + ); + ensure!( + channel.connection_generation >= meta.generation, + "CONNECTION_GENERATION_REGRESSION" + ); + if let Some(h) = helper { + ensure!( + !h.journal_id.is_nil() && h.checkpoint <= i64::MAX as u64, + "HELPER_CHECKPOINT" + ); + if let Some(old) = meta.helper_id { + ensure!( + old == h.journal_id && h.checkpoint >= meta.helper_checkpoint, + "HELPER_JOURNAL_ROLLBACK" + ); + } + } else { + ensure!(meta.helper_id.is_none(), "HELPER_JOURNAL_DISAPPEARED"); + } + if let Some(h) = helper { + ingest_helper(&tx, ledger, &self.binding, &channel, h, now)?; + } + archive_events(&tx)?; + tx.execute( + "UPDATE job_attempts SET state='accepted' WHERE state='queued' AND grant_id=''", + [], + )?; + // A possibly started attempt is NEVER returned to accepted. Preserve + // revisions and results; only ambiguous no-result states become unknown. + tx.execute("UPDATE job_attempts SET state='unknown',revision=revision+1 WHERE state IN ('requesting','running','verifying') AND NOT EXISTS(SELECT 1 FROM reconcile_results r WHERE r.job=job_attempts.job AND r.attempt=job_attempts.attempt AND r.revision>=job_attempts.revision)",[])?; + let mut attempts = attempts(&tx, ledger, &self.binding)?; + if let Some(h) = helper { + merge_helper(&mut attempts, h)?; + } + ensure!(attempts.len() <= 256, "SYNC_ATTEMPT_CAPACITY_NO_COMPACTION"); + let ids = pending_ids(&tx)?; + ensure!(ids.len() <= 256, "SYNC_RECEIPT_CAPACITY"); + let next: i64 = tx.query_row( + "UPDATE counter SET seq=seq+1 WHERE id=1 RETURNING seq", + [], + |r| r.get(0), + )?; + let request = SyncRequest { + request_id: Uuid::new_v4().to_string(), + journal_id: meta.journal_id.to_string(), + checkpoint: u64::try_from(next)?, + // A pending request may have committed with a lost reply, or may + // never have reached the server. Preserve both witnesses: the + // server resolves the pending request first and otherwise checks + // this last applied decision against its authoritative head. + previous_decision_id: meta.decision.map(|id| id.to_string()).unwrap_or_default(), + assignment_version: self.binding.assignment_version, + attempts, + durable_message_ids: ids, + journal_complete: true, + helper_journal_id: helper.map(|h| h.journal_id.to_string()).unwrap_or_default(), + helper_checkpoint: helper.map_or(0, |h| h.checkpoint), + helper_previous_decision_id: helper + .and_then(|h| h.previous_decision_id) + .map(|id| id.to_string()) + .unwrap_or_default(), + previous_request_id: meta + .pending_request + .map(|id| id.to_string()) + .unwrap_or_default(), + }; + let envelope = Envelope { + protocol_major: 1, + protocol_minor: 0, + message_id: Uuid::new_v4().to_string(), + agent_id: self.binding.agent_id.to_string(), + enrollment_generation: self.binding.enrollment_generation, + connection_generation: channel.connection_generation, + boot_id: channel.boot_id.to_string(), + sequence: u64::try_from(next)?, + observed_at: now.timestamp_millis(), + payload: Some(Payload::SyncRequest(request.clone())), + }; + ensure!( + envelope.encoded_len() <= rmm_domain::MAX_MESSAGE, + "SYNC_ENVELOPE_SIZE" + ); + tx.execute( + "INSERT INTO reconcile_requests(id,request,envelope,generation) VALUES(?1,?2,?3,?4)", + params![ + request.request_id, + request.encode_to_vec(), + envelope.encode_to_vec(), + channel.connection_generation + ], + )?; + tx.execute("UPDATE reconcile_meta SET checkpoint=?1,high_sequence=?1,generation=?2,pending_request=?3 WHERE id=1",params![next,channel.connection_generation,request.request_id])?; + tx.commit()?; + let prepared = PreparedSync { envelope, request }; + self.pending = Some(prepared.clone()); + self.channel = Some(channel); + Ok(prepared) + } + /// All changes, concrete ACKs, result archives and checkpoint chain commit + /// together. A rejection closes only mutation authority; telemetry may run. + pub fn apply( + &mut self, + db: &mut Connection, + response: &SyncResponse, + clock: &impl SyncClock, + ) -> Result { + if self.pending.is_none() { + let result = self.replay_applied(db, response, clock); + if result.is_err() { + self.gate = None; + } + return result; + } + self.gate = None; + let result = self.apply_inner(db, response, clock); + if result.is_err() { + self.gate = None; + } + result + } + fn replay_applied( + &self, + db: &Connection, + response: &SyncResponse, + clock: &impl SyncClock, + ) -> Result { + let gate = self.gate.as_ref().context("NO_CURRENT_SYNC_CHALLENGE")?; + ensure!(gate.response == *response, "REPLAYED_DIFFERENT_DECISION"); + let d = verify_response_with( + &self.verifier, + &self.binding, + &gate.request, + &gate.channel, + response, + clock.now()?, + )?; + let meta = metadata(db)?; + let sequence: i64 = db.query_row("SELECT seq FROM counter WHERE id=1", [], |r| r.get(0))?; + ensure!( + meta.decision == Some(d.decision_id) + && meta.pending_request.is_none() + && meta.checkpoint == i64::try_from(d.checkpoint)? + && sequence >= meta.high_sequence, + "APPLIED_CHECKPOINT_REGRESSION" + ); + Ok(Applied { + decision_id: d.decision_id, + acknowledged_message_ids: d.acknowledged_message_ids, + helper_instruction: d.helper_journal_id.map(|journal_id| HelperInstruction { + response: response.clone(), + decision_id: d.decision_id, + journal_id, + checkpoint: d.helper_checkpoint, + }), + reason: d.reason, + }) + } + fn apply_inner( + &mut self, + db: &mut Connection, + response: &SyncResponse, + clock: &impl SyncClock, + ) -> Result { + let now = clock.now()?; + let prepared = self.pending.as_ref().context("NO_CURRENT_SYNC_CHALLENGE")?; + let channel = self.channel.as_ref().context("NO_AUTHENTICATED_CHANNEL")?; + let decision = verify_response_with( + &self.verifier, + &self.binding, + &prepared.request, + channel, + response, + now, + )?; + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + let meta = metadata(&tx)?; + ensure!( + meta.fault.is_none() + && meta.pending_request == Some(decision.request_id) + && meta.checkpoint == i64::try_from(decision.checkpoint)? + && meta.generation == channel.connection_generation, + "SYNC_CHECKPOINT_CHANGED" + ); + let seq: i64 = tx.query_row("SELECT seq FROM counter WHERE id=1", [], |r| r.get(0))?; + ensure!( + seq >= meta.high_sequence && seq >= meta.checkpoint, + "LOCAL_SEQUENCE_ROLLBACK" + ); + let current = tx.query_row( + "SELECT request FROM reconcile_requests WHERE id=?1", + [decision.request_id.to_string()], + |r| r.get::<_, Vec>(0), + )?; + ensure!( + current == prepared.request.encode_to_vec(), + "DURABLE_CHALLENGE_CHANGED" + ); + let summaries: BTreeMap<_, _> = prepared + .request + .attempts + .iter() + .map(|a| ((a.job_id.clone(), a.attempt_id.clone()), a)) + .collect(); + let actual: BTreeSet<_> = decision + .attempts + .iter() + .map(|a| (a.job_id.to_string(), a.attempt_id.to_string())) + .collect(); + ensure!( + actual.len() == decision.attempts.len() + && actual == summaries.keys().cloned().collect(), + "SYNC_ATTEMPT_OMITTED_OR_ADDED" + ); + let mut safe = true; + let mut fault = recovery_reason(&decision.reason).then(|| decision.reason.clone()); + for d in &decision.attempts { + let local = summaries + .get(&(d.job_id.to_string(), d.attempt_id.to_string())) + .context("LOCAL_ATTEMPT_MISSING")?; + if recovery_reason(&d.reason) { + fault = Some(d.reason.clone()); + } + let offer_bytes: Vec = tx.query_row( + "SELECT offer FROM job_attempts WHERE job=?1 AND attempt=?2", + params![local.job_id, local.attempt_id], + |r| r.get(0), + )?; + let offer = JobOffer::decode(offer_bytes.as_slice())?; + let job = self.binding.job(&offer.payload_bytes)?; + // Missing-server decisions intentionally have empty action fields; + // persist their sticky denial without treating it as start authority. + if recovery_reason(&d.reason) { + safe = false; + continue; + } + let outcome = validate_attempt(&job, &offer, local, d, channel.boot_id)?; + safe &= (d.outcome_state == "accepted" + && local.consumption == "unconsumed" + && local.grant_id.is_empty()) + || (terminal(&d.outcome_state) && (local.consumption == "unconsumed" || d.unlock)); + let (stored_state, revision): (String, u64) = tx.query_row( + "SELECT state,revision FROM job_attempts WHERE job=?1 AND attempt=?2", + params![local.job_id, local.attempt_id], + |r| Ok((r.get(0)?, r.get(1)?)), + )?; + ensure!( + revision == local.revision && stored_state == local.state, + "LOCAL_ATTEMPT_CHANGED_DURING_SYNC" + ); + let stored = if d.outcome_state == "unknown" && local.state == "awaiting_reboot" { + "awaiting_reboot" + } else { + d.outcome_state.as_str() + }; + tx.execute( + "UPDATE job_attempts SET state=?3,revision=?4 WHERE job=?1 AND attempt=?2", + params![ + local.job_id, + local.attempt_id, + stored, + d.revision.max(local.revision) + ], + )?; + if let Some(bytes) = outcome { + tx.execute("INSERT INTO reconcile_outcomes(job,attempt,outcome,decision_id) VALUES(?1,?2,?3,?4) ON CONFLICT(job,attempt) DO UPDATE SET outcome=excluded.outcome,decision_id=excluded.decision_id",params![local.job_id,local.attempt_id,bytes,decision.decision_id.to_string()])?; + } + } + let requested: BTreeSet<_> = prepared + .request + .durable_message_ids + .iter() + .map(String::as_str) + .collect(); + let mut receipts = BTreeSet::new(); + for id in &decision.acknowledged_message_ids { + ensure!( + receipts.insert(*id) && requested.contains(id.to_string().as_str()), + "ACK_NOT_IN_REQUEST" + ); + archive_events(&tx)?; + tx.execute("DELETE FROM job_events WHERE id=?1", [id.to_string()])?; + tx.execute( + "INSERT OR IGNORE INTO reconcile_receipts(id,decision_id) VALUES(?1,?2)", + params![id.to_string(), decision.decision_id.to_string()], + )?; + } + tx.execute( + "UPDATE reconcile_requests SET response=?2 WHERE id=?1 AND response IS NULL", + params![decision.request_id.to_string(), response.encode_to_vec()], + )?; + tx.execute("UPDATE reconcile_meta SET last_decision=?1,pending_request=NULL,helper_id=?2,helper_checkpoint=?3,fault=COALESCE(fault,?4),high_sequence=?5 WHERE id=1", + params![decision.decision_id.to_string(),decision.helper_journal_id.map(|id|id.to_string()),decision.helper_checkpoint,fault,seq])?; + tx.commit()?; + let helper_instruction = decision + .helper_journal_id + .map(|journal_id| HelperInstruction { + response: response.clone(), + decision_id: decision.decision_id, + journal_id, + checkpoint: decision.helper_checkpoint, + }); + self.gate = Some(Gate { + channel: channel.clone(), + until: decision.valid_until, + ready: decision.ready && safe && fault.is_none(), + helper_ready: decision.helper_ready, + helper: helper_instruction + .as_ref() + .map(|h| (h.journal_id, h.checkpoint, h.decision_id)), + local_applied: !decision.attempts.iter().any(|d| d.unlock), + request: prepared.request.clone(), + response: response.clone(), + }); + self.pending = None; + Ok(Applied { + decision_id: decision.decision_id, + acknowledged_message_ids: decision.acknowledged_message_ids, + helper_instruction, + reason: decision.reason, + }) + } + /// Apply ONLY this process's independently owned consumption ledger. Root + /// helper unlocks use HelperInstruction over authenticated IPC instead. + /// A crash between journal and ledger transactions leaves the gate closed; + /// resync/reapply never deletes the consumed attempt or repeats an effect. + pub fn apply_consumption( + &mut self, + ledger: &mut Connection, + clock: &impl SyncClock, + ) -> Result<()> { + let gate = self.gate.as_mut().context("NO_SIGNED_DECISION")?; + gate.local_applied = false; + let decision = verify_response_with( + &self.verifier, + &self.binding, + &gate.request, + &gate.channel, + &gate.response, + clock.now()?, + )?; + let tx = ledger.transaction_with_behavior(TransactionBehavior::Immediate)?; + tx.execute_batch("CREATE TABLE IF NOT EXISTS reconcile_consumption(decision_id TEXT PRIMARY KEY,response BLOB NOT NULL);")?; + let applied: Option> = tx + .query_row( + "SELECT response FROM reconcile_consumption WHERE decision_id=?1", + [decision.decision_id.to_string()], + |r| r.get(0), + ) + .optional()?; + if let Some(old) = applied { + ensure!( + old == gate.response.encode_to_vec(), + "CONSUMPTION_DECISION_CONFLICT" + ); + } else { + for d in decision.attempts.iter().filter(|d| d.unlock) { + let grant = d.grant_id.context("UNLOCK_GRANT_MISSING")?; + let (agent,generation,job,attempt,previous):(String,u64,String,String,Option>)=tx.query_row( + "SELECT agent,generation,job,attempt,COALESCE((SELECT outcome FROM execution_evidence WHERE execution_evidence.grant_id=consumed.grant_id),result) FROM consumed WHERE grant_id=?1", + [grant.to_string()],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?)))?; + ensure!( + agent == self.binding.agent_id.to_string() + && generation == self.binding.enrollment_generation + && job == d.job_id.to_string() + && attempt == d.attempt_id.to_string(), + "LOCAL_UNLOCK_CONSUMPTION_BINDING" + ); + let previous = previous.context("LOCAL_UNLOCK_PREVIOUS_OUTCOME_MISSING")?; + let prior = canonical_outcome_bytes(&serde_json::from_slice::( + &previous, + )?)?; + ensure!( + d.previous_outcome_sha256.as_deref() == Some(hash(&prior).as_str()), + "LOCAL_UNLOCK_PREVIOUS_OUTCOME_MISMATCH" + ); + let bytes = if d.outcome_bytes.is_empty() { + prior + } else { + d.outcome_bytes.clone() + }; + let outcome: ReconciledOutcome = serde_json::from_slice(&bytes)?; + ensure!( + terminal(&outcome.state) + && outcome.state == d.outcome_state + && canonical_outcome_bytes(&outcome)? == bytes + && d.outcome_sha256.as_deref() == Some(hash(&bytes).as_str()), + "LOCAL_UNLOCK_CANONICAL_OUTCOME" + ); + tx.execute( + "UPDATE consumed SET result=?2 WHERE grant_id=?1", + params![grant.to_string(), bytes], + )?; + tx.execute( + "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", + params![grant.to_string(), bytes], + )?; + } + tx.execute( + "INSERT INTO reconcile_consumption(decision_id,response) VALUES(?1,?2)", + params![ + decision.decision_id.to_string(), + gate.response.encode_to_vec() + ], + )?; + } + tx.commit()?; + gate.local_applied = true; + Ok(()) + } +} + +pub fn initialize(db: &Connection) -> Result<()> { + let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?; + ensure!(check == "ok", "JOURNAL_INTEGRITY"); + db.execute_batch("PRAGMA synchronous=FULL; + CREATE TABLE IF NOT EXISTS reconcile_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,high_sequence INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,last_decision TEXT,pending_request TEXT,helper_id TEXT,helper_checkpoint INTEGER NOT NULL DEFAULT 0,fault TEXT); + CREATE TABLE IF NOT EXISTS reconcile_requests(id TEXT PRIMARY KEY,request BLOB NOT NULL,envelope BLOB NOT NULL,generation INTEGER NOT NULL,response BLOB); + CREATE TABLE IF NOT EXISTS reconcile_results(id TEXT PRIMARY KEY,job TEXT NOT NULL,attempt TEXT NOT NULL,revision INTEGER NOT NULL,envelope BLOB NOT NULL); + CREATE TABLE IF NOT EXISTS reconcile_receipts(id TEXT PRIMARY KEY,decision_id TEXT NOT NULL); + CREATE TABLE IF NOT EXISTS reconcile_outcomes(job TEXT NOT NULL,attempt TEXT NOT NULL,outcome BLOB NOT NULL,decision_id TEXT NOT NULL,PRIMARY KEY(job,attempt));")?; + db.execute( + "INSERT OR IGNORE INTO reconcile_meta(id,journal_id) VALUES(1,?1)", + [Uuid::new_v4().to_string()], + )?; + Ok(()) +} +struct Meta { + journal_id: Uuid, + checkpoint: i64, + high_sequence: i64, + generation: u64, + decision: Option, + pending_request: Option, + helper_id: Option, + helper_checkpoint: u64, + fault: Option, +} +type MetadataRow = ( + String, + i64, + i64, + u64, + Option, + Option, + Option, + u64, + Option, +); +fn metadata(db: &Connection) -> Result { + let row:MetadataRow=db.query_row("SELECT journal_id,checkpoint,high_sequence,generation,last_decision,pending_request,helper_id,helper_checkpoint,fault FROM reconcile_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)?,r.get(7)?,r.get(8)?)))?; + Ok(Meta { + journal_id: Uuid::parse_str(&row.0)?, + checkpoint: row.1, + high_sequence: row.2, + generation: row.3, + decision: row.4.map(|s| Uuid::parse_str(&s)).transpose()?, + pending_request: row.5.map(|s| Uuid::parse_str(&s)).transpose()?, + helper_id: row.6.map(|s| Uuid::parse_str(&s)).transpose()?, + helper_checkpoint: row.7, + fault: row.8, + }) +} +/// Call inside the same transaction BEFORE deleting an ACKed job event. This +/// retains the original result identity/output for future reconciliation. +pub fn archive_events(db: &Connection) -> Result<()> { + let mut statement = db.prepare("SELECT id,payload FROM job_events ORDER BY rowid")?; + for row in statement.query_map([], |r| { + Ok((r.get::<_, String>(0)?, r.get::<_, Vec>(1)?)) + })? { + let (id, bytes) = row?; + let envelope = Envelope::decode(bytes.as_slice())?; + ensure!(id == envelope.message_id, "EVENT_IDENTITY_CONFLICT"); + if let Some(Payload::JobResult(r)) = &envelope.payload { + let existing: Option> = db + .query_row( + "SELECT envelope FROM reconcile_results WHERE id=?1", + [&id], + |r| r.get(0), + ) + .optional()?; + ensure!( + existing.as_ref().is_none_or(|b| *b == bytes), + "RESULT_IDENTITY_CONFLICT" + ); + db.execute("INSERT OR IGNORE INTO reconcile_results(id,job,attempt,revision,envelope) VALUES(?1,?2,?3,?4,?5)",params![id,r.job_id,r.attempt_id,r.revision,bytes])?; + } + } + Ok(()) +} +fn pending_ids(db: &Connection) -> Result> { + let mut statement = db.prepare("SELECT id FROM job_events ORDER BY id")?; + let ids = statement + .query_map([], |r| r.get::<_, String>(0))? + .collect::>>()?; + for id in &ids { + ensure!(!Uuid::parse_str(id)?.is_nil(), "INVALID_DURABLE_MESSAGE_ID"); + } + Ok(ids) +} +fn outcome(r: &JobResult) -> Result { + Ok(ReconciledOutcome { + state: r.state.clone(), + stdout: r.stdout.clone(), + stderr: r.stderr.clone(), + exit_code: r.exit_code, + output_truncated: r.output_truncated, + reboot_required: r.reboot_required, + evidence: serde_json::from_str(&r.evidence_json)?, + error_code: r.error_code.clone(), + }) +} +fn terminal(state: &str) -> bool { + matches!(state, "succeeded" | "failed" | "cancelled" | "expired") +} +fn optional_uuid(s: &str) -> Result> { + if s.is_empty() { + Ok(None) + } else { + let id = Uuid::parse_str(s)?; + ensure!(!id.is_nil(), "NIL_ID"); + Ok(Some(id)) + } +} +fn recovery_reason(reason: &str) -> bool { + matches!( + reason, + "MISSING_SERVER_JOB" + | "ATTEMPT_BINDING_MISMATCH" + | "MANUAL_SERVER_LEDGER_RECOVERY_REQUIRED" + | "SERVER_STATE_ROLLBACK" + | "ASSIGNMENT_MISMATCH" + | "JOURNAL_SEQUENCE_REGRESSION" + | "SERVER_CHECKPOINT_MISSING" + | "JOURNAL_ROLLBACK_OR_REPLACEMENT" + | "CHECKPOINT_CHAIN_MISMATCH" + | "HELPER_JOURNAL_ROLLBACK" + | "HELPER_CHECKPOINT_MISMATCH" + | "HELPER_CHECKPOINT_MISSING" + | "HELPER_CHECKPOINT_REQUIRED" + | "JOURNAL_HISTORY_MISSING" + | "LOCAL_ATTEMPT_MISSING" + ) +} + +fn attempts(db: &Connection, ledger: &Connection, binding: &Binding) -> Result> { + let mut statement = + db.prepare("SELECT offer,state,revision,grant_id FROM job_attempts ORDER BY job,attempt")?; + let rows = statement.query_map([], |r| { + Ok(( + r.get::<_, Vec>(0)?, + r.get::<_, String>(1)?, + r.get::<_, u64>(2)?, + r.get::<_, String>(3)?, + )) + })?; + let mut summaries = Vec::new(); + let mut known = BTreeSet::new(); + for row in rows { + let (bytes, state, revision, mut grant_id) = row?; + let offer = JobOffer::decode(bytes.as_slice())?; + let job = binding.job(&offer.payload_bytes)?; + ensure!( + offer.job_id == job.job_id.to_string() + && offer.attempt_id == job.attempt_id.to_string() + && offer.job_payload_hash == hash(&offer.payload_bytes), + "LOCAL_OFFER_BINDING" + ); + known.insert((offer.job_id.clone(), offer.attempt_id.clone())); + let consumed: Option<(String, String, u64, Option>)> = ledger + .query_row( + "SELECT grant_id,agent,generation,result FROM consumed WHERE job=?1 AND attempt=?2", + params![offer.job_id, offer.attempt_id], + |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)), + ) + .optional()?; + // A signed definitive nonstart can make an accepted no-grant row + // terminal without a JobResult. Preserve that fact on the next Sync; + // requesting/running/unknown still cannot claim non-consumption. An + // actual durable consumption row below always overrides this label. + let mut consumption = if grant_id.is_empty() + && matches!(state.as_str(), "accepted" | "expired" | "cancelled") + { + "unconsumed" + } else { + "uncertain" + }; + let mut original_boot_id = String::new(); + let mut canonical = Vec::new(); + if let Some((id, agent, generation, result)) = consumed { + ensure!( + agent == binding.agent_id.to_string() + && generation == binding.enrollment_generation + && (grant_id.is_empty() || grant_id == id), + "CONSUMPTION_BINDING" + ); + grant_id = id; + consumption = "consumed"; + let evidence: Option<(Vec, Option>)> = ledger + .query_row( + "SELECT start,outcome FROM execution_evidence WHERE grant_id=?1", + [&grant_id], + |r| Ok((r.get(0)?, r.get(1)?)), + ) + .optional()?; + let mut stored = result; + if let Some((start, last)) = evidence { + let start: serde_json::Value = serde_json::from_slice(&start)?; + original_boot_id = start["previous_boot_id"] + .as_str() + .context("ORIGINAL_BOOT_MISSING")? + .to_string(); + optional_uuid(&original_boot_id)?; + let request: crate::auth::Request = + serde_json::from_value(start["request"].clone())?; + ensure!( + request.job_bytes == offer.payload_bytes + && request.plan_hash == offer.plan_hash + && request.authorization_id.to_string() == offer.authorization_id, + "START_EVIDENCE_BINDING" + ); + if last.is_some() { + stored = last; + } + } + if let Some(stored) = stored { + let o: ReconciledOutcome = serde_json::from_slice(&stored)?; + canonical = canonical_outcome_bytes(&o)?; + } + } + let original:Option>=db.query_row("SELECT envelope FROM reconcile_results WHERE job=?1 AND attempt=?2 ORDER BY revision DESC LIMIT 1",params![offer.job_id,offer.attempt_id],|r|r.get(0)).optional()?; + let mut result_envelope = None; + let mut result = None; + let mut result_message_id = String::new(); + if let Some(bytes) = original { + let envelope = Envelope::decode(bytes.as_slice())?; + let Some(Payload::JobResult(r)) = &envelope.payload else { + anyhow::bail!("INVALID_ARCHIVED_RESULT") + }; + if r.revision == revision && r.state == state { + ensure!( + envelope.agent_id == binding.agent_id.to_string() + && envelope.enrollment_generation == binding.enrollment_generation + && r.grant_id == grant_id, + "RESULT_BINDING" + ); + let bytes = canonical_outcome_bytes(&outcome(r)?)?; + ensure!( + canonical.is_empty() || canonical == bytes, + "LOCAL_OUTCOME_LEDGER_MISMATCH" + ); + canonical = bytes; + result_message_id = envelope.message_id.clone(); + result = Some(r.clone()); + result_envelope = Some(envelope); + } + } + if !canonical.is_empty() { + let o: ReconciledOutcome = serde_json::from_slice(&canonical)?; + ensure!(o.state == state, "LOCAL_OUTCOME_STATE_MISMATCH"); + ensure!(canonical.len() <= 512 * 1024, "SYNC_CANONICAL_OUTCOME_SIZE"); + if grant_id.is_empty() && terminal(&state) && o.evidence["not_started"] == true { + consumption = "unconsumed"; + } + } + summaries.push(SyncAttempt { + job_id: offer.job_id, + attempt_id: offer.attempt_id, + job_payload_hash: offer.job_payload_hash, + grant_id, + consumption: consumption.into(), + state, + revision, + original_boot_id, + result, + result_message_id, + outcome_sha256: if canonical.is_empty() { + String::new() + } else { + hash(&canonical) + }, + outcome_bytes: canonical, + result_envelope, + }); + } + let mut statement = ledger.prepare("SELECT job,attempt FROM consumed")?; + for row in statement.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))? { + ensure!( + known.contains(&row?), + "CONSUMED_ATTEMPT_MISSING_FROM_AGENT_JOURNAL" + ); + } + Ok(summaries) +} +fn ingest_helper( + db: &Connection, + ledger: &Connection, + binding: &Binding, + channel: &Channel, + helper: &HelperSnapshot, + now: DateTime, +) -> Result<()> { + let mut statement = db.prepare("SELECT job,attempt,offer FROM job_attempts")?; + for row in statement.query_map([], |r| { + Ok(( + r.get::<_, String>(0)?, + r.get::<_, String>(1)?, + r.get::<_, Vec>(2)?, + )) + })? { + let (job, attempt, bytes) = row?; + let offer = JobOffer::decode(bytes.as_slice())?; + if binding.job(&offer.payload_bytes)?.execution_identity == "root" { + let consumed: bool = ledger.query_row( + "SELECT EXISTS(SELECT 1 FROM consumed WHERE job=?1 AND attempt=?2)", + params![job, attempt], + |r| r.get(0), + )?; + ensure!( + !consumed + || helper + .attempts + .iter() + .any(|a| a.job_id == job && a.attempt_id == attempt), + "ROOT_CONSUMED_ATTEMPT_MISSING_FROM_HELPER_JOURNAL" + ); + } + } + let mut seen = BTreeSet::new(); + for h in &helper.attempts { + ensure!( + seen.insert((&h.job_id, &h.attempt_id)), + "DUPLICATE_HELPER_ATTEMPT" + ); + let row: Option<(Vec, u64, String)> = db + .query_row( + "SELECT offer,revision,grant_id FROM job_attempts WHERE job=?1 AND attempt=?2", + params![h.job_id, h.attempt_id], + |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)), + ) + .optional()?; + let (bytes, revision, grant) = row.context("HELPER_ATTEMPT_MISSING_FROM_AGENT_JOURNAL")?; + let offer = JobOffer::decode(bytes.as_slice())?; + let job = binding.job(&offer.payload_bytes)?; + ensure!( + job.execution_identity == "root" + && h.job_payload_hash == offer.job_payload_hash + && (grant.is_empty() || grant == h.grant_id), + "HELPER_ATTEMPT_BINDING" + ); + if h.outcome_bytes.is_empty() { + continue; + } + let o: ReconciledOutcome = serde_json::from_slice(&h.outcome_bytes)?; + ensure!( + canonical_outcome_bytes(&o)? == h.outcome_bytes + && hash(&h.outcome_bytes) == h.outcome_sha256 + && o.state == h.state + && (terminal(&o.state) + || matches!(o.state.as_str(), "unknown" | "awaiting_reboot")) + && h.consumption == "consumed" + && optional_uuid(&h.grant_id)?.is_some(), + "HELPER_CANONICAL_OUTCOME" + ); + let existing: Option> = ledger + .query_row( + "SELECT outcome FROM execution_evidence WHERE grant_id=?1", + [&h.grant_id], + |r| r.get(0), + ) + .optional()? + .flatten(); + let same = existing + .as_ref() + .map(|b| { + serde_json::from_slice::(b) + .and_then(|o| canonical_outcome_bytes(&o)) + }) + .transpose()? + .is_some_and(|b| b == h.outcome_bytes); + let state: String = db.query_row( + "SELECT state FROM job_attempts WHERE job=?1 AND attempt=?2", + params![h.job_id, h.attempt_id], + |r| r.get(0), + )?; + if same && state == h.state { + continue; + } + let consumed: Option<(String, u64, String, String)> = ledger + .query_row( + "SELECT agent,generation,job,attempt FROM consumed WHERE grant_id=?1", + [&h.grant_id], + |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)), + ) + .optional()?; + ensure!( + consumed + == Some(( + binding.agent_id.to_string(), + binding.enrollment_generation, + h.job_id.clone(), + h.attempt_id.clone() + )), + "HELPER_CONSUMPTION_NOT_KNOWN_LOCALLY" + ); + // Store actual helper evidence without unlocking result/consumption. A + // crash here still leaves both gates closed and the original helper + // result available; the next sync imports it again idempotently. + ensure!( + ledger.execute( + "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", + params![h.grant_id, h.outcome_bytes] + )? == 1, + "LOCAL_START_EVIDENCE_MISSING" + ); + let next_revision = revision + .max(h.revision) + .checked_add(1) + .context("REVISION_OVERFLOW")?; + i64::try_from(next_revision)?; + let seq: i64 = db.query_row( + "UPDATE counter SET seq=seq+1 WHERE id=1 RETURNING seq", + [], + |r| r.get(0), + )?; + let result = JobResult { + job_id: h.job_id.clone(), + attempt_id: h.attempt_id.clone(), + revision: next_revision, + state: o.state.clone(), + grant_id: h.grant_id.clone(), + stdout: o.stdout, + stderr: o.stderr, + output_truncated: o.output_truncated, + reboot_required: o.reboot_required, + exit_code: o.exit_code, + evidence_json: o.evidence.to_string(), + error_code: o.error_code, + }; + let envelope = Envelope { + protocol_major: 1, + protocol_minor: 0, + message_id: Uuid::new_v4().to_string(), + agent_id: binding.agent_id.to_string(), + enrollment_generation: binding.enrollment_generation, + connection_generation: channel.connection_generation, + boot_id: channel.boot_id.to_string(), + sequence: u64::try_from(seq)?, + observed_at: now.timestamp_millis(), + payload: Some(Payload::JobResult(result)), + }; + ensure!( + envelope.encoded_len() <= rmm_domain::MAX_MESSAGE, + "HELPER_RESULT_SIZE" + ); + db.execute( + "INSERT INTO job_events(id,payload) VALUES(?1,?2)", + params![envelope.message_id, envelope.encode_to_vec()], + )?; + db.execute( + "UPDATE job_attempts SET state=?3,revision=?4,grant_id=?5 WHERE job=?1 AND attempt=?2", + params![h.job_id, h.attempt_id, o.state, next_revision, h.grant_id], + )?; + } + Ok(()) +} +fn merge_helper(local: &mut [SyncAttempt], helper: &HelperSnapshot) -> Result<()> { + let mut seen = BTreeSet::new(); + for h in &helper.attempts { + ensure!( + seen.insert((&h.job_id, &h.attempt_id)), + "DUPLICATE_HELPER_ATTEMPT" + ); + let a = local + .iter_mut() + .find(|a| a.job_id == h.job_id && a.attempt_id == h.attempt_id) + .context("HELPER_ATTEMPT_MISSING_FROM_AGENT_JOURNAL")?; + ensure!( + a.job_payload_hash == h.job_payload_hash + && (a.grant_id.is_empty() || a.grant_id == h.grant_id), + "HELPER_ATTEMPT_BINDING" + ); + if h.consumption != "unconsumed" { + a.grant_id = h.grant_id.clone(); + a.consumption = h.consumption.clone(); + ensure!( + a.outcome_sha256 == h.outcome_sha256, + "HELPER_OUTCOME_MISMATCH" + ); + if a.original_boot_id.is_empty() { + a.original_boot_id = h.original_boot_id.clone(); + } + ensure!( + a.original_boot_id == h.original_boot_id, + "HELPER_ORIGINAL_BOOT_MISMATCH" + ); + } + } + Ok(()) +} +pub fn verify_response( + binding: &Binding, + request: &SyncRequest, + channel: &Channel, + response: &SyncResponse, + now: DateTime, +) -> Result { + verify_response_with( + &DefaultVerifier::default(), + binding, + request, + channel, + response, + now, + ) +} +pub fn verify_response_with( + verifier: &impl Ed25519Verifier, + binding: &Binding, + request: &SyncRequest, + channel: &Channel, + response: &SyncResponse, + now: DateTime, +) -> Result { + ensure!( + response.decision_bytes.len() <= 512 * 1024 && response.signature.len() == 64, + "SYNC_SIGNATURE_SIZE" + ); + let raw = decode_key(&binding.public_key_hex)?; + ensure!( + verifier.verify(&raw, &response.decision_bytes, &response.signature)?, + "INVALID_SYNC_SIGNATURE" + ); + let d: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?; + ensure!( + d.valid_profile(now) + && d.valid_until - d.issued_at <= chrono::Duration::seconds(60) + && d.key_id == hash(&raw), + "SYNC_PROFILE_OR_CLOCK" + ); + ensure!( + 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 + && d.connection_generation == channel.connection_generation + && d.boot_id == channel.boot_id + && d.request_id.to_string() == request.request_id + && d.request_hash == hash(&request.encode_to_vec()) + && d.journal_id.to_string() == request.journal_id + && d.checkpoint == request.checkpoint + && d.helper_journal_id == optional_uuid(&request.helper_journal_id)? + && d.helper_checkpoint == request.helper_checkpoint, + "SYNC_DECISION_BINDING" + ); + Ok(d) +} +fn decode_key(hex: &str) -> Result> { + ensure!(hex.len() == 64, "PINNED_KEY_SIZE"); + (0..32) + .map(|i| { + Ok(u8::from_str_radix( + hex.get(i * 2..i * 2 + 2).context("PINNED_KEY_ENCODING")?, + 16, + )?) + }) + .collect() +} +fn validate_attempt( + job: &JobPayload, + offer: &JobOffer, + local: &SyncAttempt, + d: &AttemptDecision, + boot: Uuid, +) -> Result>> { + ensure!( + d.job_payload_hash == offer.job_payload_hash + && d.grant_id == optional_uuid(&local.grant_id)? + && d.action == job.action.name() + && d.execution_identity == job.execution_identity + && (d.revision >= local.revision || d.outcome_state == "unknown") + && d.original_boot_id == optional_uuid(&local.original_boot_id)? + && d.previous_outcome_sha256.as_deref() + == (!local.outcome_sha256.is_empty()).then_some(local.outcome_sha256.as_str()), + "SYNC_ATTEMPT_BINDING" + ); + if d.grant_id.is_some() { + ensure!( + d.authorization_id == Some(Uuid::parse_str(&offer.authorization_id)?) + && d.plan_hash.as_deref() == Some(offer.plan_hash.as_str()), + "SYNC_AUTHORIZATION_BINDING" + ); + } + ensure!( + !terminal(&local.state) || d.outcome_state == local.state, + "TERMINAL_STATE_REGRESSION" + ); + if d.outcome_state == "accepted" { + ensure!( + local.state == "accepted" + && local.consumption == "unconsumed" + && d.grant_id.is_none() + && !d.unlock + && d.outcome_bytes.is_empty(), + "SYNC_CANNOT_RESTART_ATTEMPT" + ); + return Ok(None); + } + if d.outcome_state == "unknown" { + ensure!(!d.unlock, "UNKNOWN_CANNOT_UNLOCK"); + return Ok(None); + } + ensure!(terminal(&d.outcome_state), "UNSUPPORTED_SYNC_STATE"); + if d.outcome_bytes.is_empty() && d.outcome_sha256.is_none() { + ensure!( + local.consumption == "unconsumed" + && d.grant_id.is_none() + && !d.unlock + && d.outcome_state != "succeeded", + "TERMINAL_WITHOUT_EVIDENCE" + ); + return Ok(None); + } + let bytes = if d.outcome_bytes.is_empty() { + local.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, + "SYNC_CANONICAL_OUTCOME" + ); + if bytes != local.outcome_bytes { + ensure!( + matches!(job.action, rmm_domain::jobs::Action::Reboot { .. }) + && d.outcome_state == "succeeded" + && d.original_boot_id.is_some_and(|id| id != boot) + && o.evidence["postconditions_passed"] == true + && o.evidence["reconciled_after_reboot"] == true + && o.evidence["new_boot_id"].as_str() == Some(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())), + "SYNC_REBOOT_REAUTH_EVIDENCE" + ); + } + ensure!( + !d.unlock || (local.consumption == "consumed" && d.grant_id.is_some()), + "UNLOCK_WITHOUT_CONSUMPTION" + ); + Ok(Some(bytes)) +} diff --git a/crates/agent-core/tests/contracts.rs b/crates/agent-core/tests/contracts.rs new file mode 100644 index 0000000..1304552 --- /dev/null +++ b/crates/agent-core/tests/contracts.rs @@ -0,0 +1,292 @@ +//! Signature/codec/consumption contracts, with no transport or OS execution. +use chrono::{DateTime, Duration, Utc}; +use ed25519_dalek::{Signer, SigningKey}; +#[cfg(unix)] +use rmm_agent_core::crypto::{DalekVerifier, Ed25519Verifier}; +use rmm_agent_core::{ + auth::{Binding, Outcome, Request}, + jobs, ledger, +}; +use rmm_domain::jobs::{Action, ExecutionPermitPayload, JobPayload, encode, hash}; +use rmm_protocol::JobOffer; +use rusqlite::Connection; +use uuid::Uuid; + +fn fixture() -> (Binding, Request, ExecutionPermitPayload, DateTime) { + let key = SigningKey::from_bytes(&[9; 32]); + let id = Uuid::from_u128(1); + let now = DateTime::parse_from_rfc3339("2026-11-14T00:00:00Z") + .unwrap() + .with_timezone(&Utc); + let binding = Binding { + tenant_id: id, + asset_id: Uuid::from_u128(2), + agent_id: Uuid::from_u128(3), + enrollment_generation: 2, + assignment_version: 4, + public_key_hex: hex::encode(key.verifying_key().as_bytes()), + }; + let job = JobPayload { + schema_version: 1, + tenant_id: binding.tenant_id, + asset_id: binding.asset_id, + agent_id: binding.agent_id, + enrollment_generation: 2, + assignment_version: 4, + job_id: Uuid::from_u128(5), + attempt_id: Uuid::from_u128(6), + action: Action::RunScript { + interpreter: "posix_sh".into(), + script_utf8: "printf 'Hello\\n'\n".into(), + arguments: vec![], + expected_stdout: Some("Hello\n".into()), + }, + execution_identity: "agent_service".into(), + not_before: now - Duration::minutes(1), + start_before: now + Duration::minutes(5), + max_runtime_seconds: 10, + max_output_bytes: 4096, + }; + let job_bytes = encode(&job).unwrap(); + let permit = ExecutionPermitPayload { + domain: "rmm.execution-permit".into(), + schema_version: 1, + grant_id: Uuid::from_u128(7), + authorization_id: Uuid::from_u128(8), + tenant_id: binding.tenant_id, + asset_id: binding.asset_id, + agent_id: binding.agent_id, + enrollment_generation: 2, + assignment_version: 4, + job_id: job.job_id, + attempt_id: job.attempt_id, + plan_hash: hash(b"immutable approved plan"), + job_payload_hash: hash(&job_bytes), + action: job.action.name().into(), + execution_identity: job.execution_identity, + max_runtime_seconds: 10, + not_before: now - Duration::seconds(1), + start_before: now + Duration::seconds(59), + key_id: hash(key.verifying_key().as_bytes()), + }; + let permit_bytes = encode(&permit).unwrap(); + let signature = key.sign(&permit_bytes).to_bytes().to_vec(); + let request = Request { + job_bytes, + permit_bytes, + signature, + plan_hash: permit.plan_hash.clone(), + authorization_id: permit.authorization_id, + }; + (binding, request, permit, now) +} + +#[test] +fn historical_request_and_binding_codecs_keep_field_order_arrays_and_strictness() { + let request = Request { + job_bytes: vec![0, 255], + permit_bytes: vec![1], + signature: vec![2], + plan_hash: "ab".into(), + authorization_id: Uuid::nil(), + }; + let expected = r#"{"job_bytes":[0,255],"permit_bytes":[1],"signature":[2],"plan_hash":"ab","authorization_id":"00000000-0000-0000-0000-000000000000"}"#; + assert_eq!(serde_json::to_vec(&request).unwrap(), expected.as_bytes()); + let parsed: Request = serde_json::from_str(expected).unwrap(); + assert_eq!(serde_json::to_string(&parsed).unwrap(), expected); + assert!( + serde_json::from_str::( + &expected.replace("\"job_bytes\"", "\"extra\":0,\"job_bytes\"") + ) + .is_err() + ); + let binding = Binding { + tenant_id: Uuid::nil(), + asset_id: Uuid::nil(), + agent_id: Uuid::nil(), + enrollment_generation: 1, + assignment_version: 2, + public_key_hex: "key".into(), + }; + assert_eq!( + serde_json::to_string(&binding).unwrap(), + r#"{"tenant_id":"00000000-0000-0000-0000-000000000000","asset_id":"00000000-0000-0000-0000-000000000000","agent_id":"00000000-0000-0000-0000-000000000000","enrollment_generation":1,"assignment_version":2,"public_key_hex":"key"}"# + ); +} + +#[test] +fn exact_permit_bytes_time_and_all_identity_hash_bindings_are_required() { + let (binding, request, permit, now) = fixture(); + assert_eq!(binding.verify(&request, now).unwrap().1, permit); + assert!(binding.verify(&request, permit.not_before).is_ok()); + assert!( + binding + .verify(&request, permit.not_before - Duration::nanoseconds(1)) + .is_err() + ); + assert!(binding.verify(&request, permit.start_before).is_err()); + let key = SigningKey::from_bytes(&[9; 32]); + let value = serde_json::to_value(&permit).unwrap(); + for (field, replacement) in [ + ("domain", serde_json::json!("rmm.reconciliation-decision")), + ("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!(3)), + ("assignment_version", serde_json::json!(5)), + ("job_id", serde_json::json!(Uuid::new_v4())), + ("attempt_id", serde_json::json!(Uuid::new_v4())), + ("authorization_id", serde_json::json!(Uuid::new_v4())), + ("plan_hash", serde_json::json!(hash(b"other plan"))), + ( + "job_payload_hash", + serde_json::json!(hash(b"different bytes")), + ), + ("action", serde_json::json!("reboot")), + ("execution_identity", serde_json::json!("root")), + ("max_runtime_seconds", serde_json::json!(11)), + ("key_id", serde_json::json!(hash(b"otherkey"))), + ("grant_id", serde_json::json!(Uuid::nil())), + ( + "start_before", + serde_json::json!(now + Duration::seconds(60)), + ), + ] { + let mut value = value.clone(); + value[field] = replacement; + let changed: ExecutionPermitPayload = serde_json::from_value(value).unwrap(); + let mut changed_request = request.clone(); + changed_request.permit_bytes = encode(&changed).unwrap(); + changed_request.signature = key.sign(&changed_request.permit_bytes).to_bytes().to_vec(); + assert!(binding.verify(&changed_request, now).is_err(), "{field}"); + } + let mut changed = request.clone(); + changed.job_bytes.push(b' '); + assert!(binding.verify(&changed, now).is_err()); + let mut changed = request.clone(); + changed.signature[0] ^= 1; + assert!(binding.verify(&changed, now).is_err()); + let mut changed = request; + changed.signature.truncate(63); + assert!(binding.verify(&changed, now).is_err()); +} + +#[cfg(unix)] +#[test] +fn openssl_and_portable_ed25519_agree_on_current_keys_bytes_and_tampering() { + use openssl::{ + pkey::PKey, + sign::{Signer as OpenSigner, Verifier}, + }; + let key = PKey::generate_ed25519().unwrap(); + let public = key.raw_public_key().unwrap(); + let bytes = b"exact stored signed payload\r\n"; + let signature = OpenSigner::new_without_digest(&key) + .unwrap() + .sign_oneshot_to_vec(bytes) + .unwrap(); + let verify_openssl = |message: &[u8], signature: &[u8]| { + Verifier::new_without_digest(&key) + .unwrap() + .verify_oneshot(signature, message) + .unwrap_or(false) + }; + for message in [bytes.as_slice(), b"exact stored signed payload\n", b""] { + assert_eq!( + DalekVerifier.verify(&public, message, &signature).unwrap(), + verify_openssl(message, &signature) + ); + } + let mut changed = signature.clone(); + changed[0] ^= 1; + for signature in [&signature[..], &changed[..], &signature[..63]] { + assert_eq!( + DalekVerifier.verify(&public, bytes, signature).unwrap(), + verify_openssl(bytes, signature) + ); + } + assert!(!DalekVerifier.verify(&[0; 32], bytes, &[0; 64]).unwrap()); + assert!(!DalekVerifier.verify(&[0; 31], bytes, &signature).unwrap()); + // Reverse direction uses raw Ed25519, never prehashes the message. + let signing = SigningKey::from_bytes(&[11; 32]); + let p = PKey::public_key_from_raw_bytes( + signing.verifying_key().as_bytes(), + openssl::pkey::Id::ED25519, + ) + .unwrap(); + assert!( + Verifier::new_without_digest(&p) + .unwrap() + .verify_oneshot(&signing.sign(bytes).to_bytes(), bytes) + .unwrap() + ); +} + +#[test] +fn consumed_attempt_and_unknown_lock_survive_reopen_without_reexecution() { + let (_, request, permit, now) = fixture(); + let path = std::env::temp_dir().join(format!("rmm-core-ledger-{}", Uuid::new_v4())); + { + let mut db = Connection::open(&path).unwrap(); + ledger::initialize_ledger(&db).unwrap(); + ledger::consume(&mut db, &permit).unwrap(); + ledger::save_start(&db, &permit, &request, Uuid::from_u128(9), now).unwrap(); + ledger::finish(&db, &permit, &Outcome::unknown("EFFECT_UNKNOWN")).unwrap(); + } + { + let mut db = Connection::open(&path).unwrap(); + ledger::initialize_ledger(&db).unwrap(); + assert!(ledger::consume(&mut db, &permit).is_err()); + let mut replacement = permit.clone(); + replacement.grant_id = Uuid::new_v4(); + assert!(ledger::consume(&mut db, &replacement).is_err()); + assert_eq!( + db.query_row( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get::<_, u32>(0) + ) + .unwrap(), + 1 + ); + assert_eq!( + db.query_row("PRAGMA max_page_count", [], |r| r.get::<_, u32>(0)) + .unwrap(), + 262144 + ); + } + std::fs::remove_file(path).unwrap(); +} + +#[test] +fn portable_acceptance_requires_platform_capability_and_exact_redelivery() { + let (binding, request, permit, _) = fixture(); + let offer = JobOffer { + job_id: permit.job_id.to_string(), + attempt_id: permit.attempt_id.to_string(), + payload_bytes: request.job_bytes, + job_payload_hash: permit.job_payload_hash, + plan_hash: permit.plan_hash, + authorization_id: permit.authorization_id.to_string(), + }; + let db = Connection::open_in_memory().unwrap(); + db.execute_batch("CREATE TABLE job_attempts(job TEXT,attempt TEXT,offer BLOB,state TEXT,revision INTEGER DEFAULT 0,grant_id TEXT DEFAULT '',cancel_requested INTEGER DEFAULT 0,PRIMARY KEY(job,attempt))").unwrap(); + assert!(jobs::accept(&db, &binding, &offer, |_| false).is_err()); + assert_eq!( + db.query_row("SELECT count(*) FROM job_attempts", [], |r| r + .get::<_, u32>(0)) + .unwrap(), + 0 + ); + assert!(jobs::accept(&db, &binding, &offer, |_| true).unwrap()); + assert!(!jobs::accept(&db, &binding, &offer, |_| true).unwrap()); + let mut changed = offer.clone(); + changed.authorization_id = Uuid::new_v4().to_string(); + assert!(jobs::accept(&db, &binding, &changed, |_| true).is_err()); + jobs::reserve_request(&db, &offer).unwrap(); + assert!(jobs::reserve_request(&db, &offer).is_err()); + assert_eq!(jobs::transition(&db, &offer, "unknown", "").unwrap(), 1); + assert!(jobs::next(&db).unwrap().is_none()); + assert!(jobs::transition(&db, &offer, "running", "").is_err()); +} diff --git a/crates/agent-core/tests/reconcile.rs b/crates/agent-core/tests/reconcile.rs new file mode 100644 index 0000000..e2334f6 --- /dev/null +++ b/crates/agent-core/tests/reconcile.rs @@ -0,0 +1,1145 @@ +//! Existing Linux Sync semantic cases, run through the shared implementation +//! with only OS fixtures/signing adapters changed; no OS effects are performed. +use rmm_agent_core::reconcile; + +use chrono::{DateTime, Duration, Utc}; +use ed25519_dalek::{Signer, SigningKey}; +use prost::Message; +use reconcile::*; +use rmm_agent_core::auth::{Binding, Request}; +use rmm_domain::{jobs::*, reconciliation::*}; +use rmm_protocol::*; +use rusqlite::{Connection, params}; +use std::{fs, path::PathBuf}; +use uuid::Uuid; + +struct Clock { + now: DateTime, + trusted: bool, +} +impl SyncClock for Clock { + fn now(&self) -> anyhow::Result> { + anyhow::ensure!(self.trusted, "CLOCK_UNTRUSTED"); + Ok(self.now) + } +} +struct Fixture { + dir: PathBuf, + db: Connection, + ledger: Connection, + key: SigningKey, + binding: Binding, + channel: Channel, + clock: Clock, +} +impl Drop for Fixture { + fn drop(&mut self) { + // Windows refuses removal of open SQLite files; close only our fixtures. + drop(std::mem::replace( + &mut self.db, + Connection::open_in_memory().unwrap(), + )); + drop(std::mem::replace( + &mut self.ledger, + 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!("reconcile-{}", Uuid::new_v4())); + fs::create_dir(&dir).unwrap(); + let db = Connection::open(dir.join("journal.sqlite")).unwrap(); + db.execute_batch("PRAGMA synchronous=FULL;CREATE TABLE counter(id INTEGER PRIMARY KEY,seq INTEGER);INSERT INTO counter VALUES(1,10); + CREATE TABLE job_attempts(job TEXT,attempt TEXT,offer BLOB,state TEXT,revision INTEGER,grant_id TEXT DEFAULT '',cancel_requested INTEGER DEFAULT 0,PRIMARY KEY(job,attempt)); + CREATE TABLE job_events(id TEXT PRIMARY KEY,payload BLOB);CREATE TABLE safety(key TEXT PRIMARY KEY,value TEXT);INSERT INTO safety VALUES('connected','1');").unwrap(); + let ledger = Connection::open(dir.join("consumption.sqlite")).unwrap(); + rmm_agent_core::ledger::initialize_ledger(&ledger).unwrap(); + let key = SigningKey::from_bytes(&[42; 32]); + let public = key.verifying_key().to_bytes(); + 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: public.iter().map(|b| format!("{b:02x}")).collect(), + }; + let channel = Channel { + connection_generation: 1, + boot_id: Uuid::new_v4(), + }; + Self { + dir, + db, + ledger, + key, + binding, + channel, + clock: Clock { + now: Utc::now(), + trusted: true, + }, + } + } + fn client(&self) -> ClientSync { + ClientSync::new(self.binding.clone()) + } + fn prepare(&mut self, client: &mut ClientSync) -> PreparedSync { + client + .prepare( + &mut self.db, + &self.ledger, + self.channel.clone(), + None, + &self.clock, + ) + .unwrap() + } + fn decision(&self, prepared: &PreparedSync, ready: bool) -> ReconcileDecisionPayload { + let request = &prepared.request; + let attempts = request + .attempts + .iter() + .map(|local| { + let bytes: Vec = self + .db + .query_row( + "SELECT offer FROM job_attempts WHERE job=?1 AND attempt=?2", + params![local.job_id, local.attempt_id], + |r| r.get(0), + ) + .unwrap(); + let offer = JobOffer::decode(bytes.as_slice()).unwrap(); + let job: JobPayload = serde_json::from_slice(&offer.payload_bytes).unwrap(); + let grant = if local.grant_id.is_empty() { + None + } else { + Some(Uuid::parse_str(&local.grant_id).unwrap()) + }; + let final_state = matches!( + local.state.as_str(), + "succeeded" | "failed" | "cancelled" | "expired" + ); + AttemptDecision { + job_id: job.job_id, + attempt_id: job.attempt_id, + job_payload_hash: local.job_payload_hash.clone(), + grant_id: grant, + authorization_id: grant + .map(|_| Uuid::parse_str(&offer.authorization_id).unwrap()), + plan_hash: grant.map(|_| offer.plan_hash), + action: job.action.name().into(), + execution_identity: job.execution_identity, + revision: local.revision, + outcome_state: if local.state == "accepted" { + "accepted".into() + } else if final_state { + local.state.clone() + } else { + "unknown".into() + }, + previous_outcome_sha256: (!local.outcome_sha256.is_empty()) + .then(|| local.outcome_sha256.clone()), + outcome_sha256: final_state + .then(|| local.outcome_sha256.clone()) + .filter(|s| !s.is_empty()), + outcome_bytes: vec![], + original_boot_id: (!local.original_boot_id.is_empty()) + .then(|| Uuid::parse_str(&local.original_boot_id).unwrap()), + unlock: final_state && local.consumption == "consumed", + reason: if local.state == "accepted" { + "ACCEPTED_NOT_STARTED" + } else if final_state { + "DEFINITIVE_OUTCOME_RECONCILED" + } else { + "EFFECT_UNRESOLVED" + } + .into(), + } + }) + .collect(); + ReconcileDecisionPayload { + domain: "rmm.reconciliation-decision".into(), + schema_version: 1, + decision_id: Uuid::new_v4(), + request_id: Uuid::parse_str(&request.request_id).unwrap(), + request_hash: hash(&request.encode_to_vec()), + tenant_id: self.binding.tenant_id, + asset_id: self.binding.asset_id, + agent_id: self.binding.agent_id, + enrollment_generation: self.binding.enrollment_generation, + assignment_version: self.binding.assignment_version, + connection_generation: self.channel.connection_generation, + boot_id: self.channel.boot_id, + journal_id: Uuid::parse_str(&request.journal_id).unwrap(), + checkpoint: request.checkpoint, + helper_journal_id: (!request.helper_journal_id.is_empty()) + .then(|| Uuid::parse_str(&request.helper_journal_id).unwrap()), + helper_checkpoint: request.helper_checkpoint, + issued_at: self.clock.now - Duration::milliseconds(1), + valid_until: self.clock.now + Duration::seconds(59), + ready, + helper_ready: ready && !request.helper_journal_id.is_empty(), + reason: if ready { + "CONSISTENT" + } else { + "ATTEMPT_RECONCILIATION_REQUIRED" + } + .into(), + attempts, + acknowledged_message_ids: vec![], + key_id: hash(&self.key.verifying_key().to_bytes()), + } + } + fn sign(&self, decision: &ReconcileDecisionPayload) -> SyncResponse { + let bytes = encode(decision).unwrap(); + SyncResponse { + signature: self.key.sign(&bytes).to_bytes().to_vec(), + decision_bytes: bytes, + } + } + fn apply( + &mut self, + client: &mut ClientSync, + decision: &ReconcileDecisionPayload, + ) -> anyhow::Result { + let response = self.sign(decision); + client.apply(&mut self.db, &response, &self.clock) + } + fn add( + &mut self, + state: &str, + consume: bool, + outcome: Option, + action: Action, + ) -> (JobOffer, Option, Option) { + let execution_identity = if matches!(action, Action::SetServiceState { .. }) { + "root" + } else { + "agent_service" + }; + 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: execution_identity.into(), + not_before: self.clock.now - Duration::seconds(5), + start_before: self.clock.now + Duration::minutes(5), + max_runtime_seconds: 120, + max_output_bytes: 4096, + }; + let offer = JobOffer { + job_id: job.job_id.to_string(), + attempt_id: job.attempt_id.to_string(), + authorization_id: Uuid::new_v4().to_string(), + plan_hash: hash(b"approved immutable plan"), + job_payload_hash: hash(&encode(&job).unwrap()), + payload_bytes: encode(&job).unwrap(), + }; + let mut grant = None; + if consume { + let p = ExecutionPermitPayload { + domain: "rmm.execution-permit".into(), + schema_version: 1, + grant_id: Uuid::new_v4(), + authorization_id: Uuid::parse_str(&offer.authorization_id).unwrap(), + 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: offer.plan_hash.clone(), + job_payload_hash: offer.job_payload_hash.clone(), + action: job.action.name().into(), + execution_identity: job.execution_identity.clone(), + not_before: self.clock.now - Duration::milliseconds(1), + start_before: self.clock.now + Duration::seconds(59), + max_runtime_seconds: 120, + key_id: hash(&self.key.verifying_key().to_bytes()), + }; + let bytes = encode(&p).unwrap(); + let request = Request { + job_bytes: offer.payload_bytes.clone(), + permit_bytes: bytes.clone(), + signature: self.key.sign(&bytes).to_bytes().to_vec(), + plan_hash: offer.plan_hash.clone(), + authorization_id: p.authorization_id, + }; + rmm_agent_core::ledger::consume(&mut self.ledger, &p).unwrap(); + rmm_agent_core::ledger::save_start( + &self.ledger, + &p, + &request, + self.channel.boot_id, + self.clock.now, + ) + .unwrap(); + if let Some(o) = &outcome { + self.ledger + .execute( + "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", + params![p.grant_id.to_string(), canonical_outcome_bytes(o).unwrap()], + ) + .unwrap(); + } + grant = Some(p); + } + self.db.execute("INSERT INTO job_attempts(job,attempt,offer,state,revision,grant_id) VALUES(?1,?2,?3,?4,1,?5)",params![offer.job_id,offer.attempt_id,offer.encode_to_vec(),state,grant.as_ref().map(|p|p.grant_id.to_string()).unwrap_or_default()]).unwrap(); + let envelope = outcome.map(|o| { + let sequence: i64 = self + .db + .query_row("UPDATE counter SET seq=seq+1 RETURNING seq", [], |r| { + r.get(0) + }) + .unwrap(); + let r = JobResult { + job_id: offer.job_id.clone(), + attempt_id: offer.attempt_id.clone(), + revision: 1, + state: o.state, + grant_id: grant + .as_ref() + .map(|p| p.grant_id.to_string()) + .unwrap_or_default(), + stdout: o.stdout, + stderr: o.stderr, + output_truncated: o.output_truncated, + reboot_required: o.reboot_required, + exit_code: o.exit_code, + evidence_json: o.evidence.to_string(), + error_code: o.error_code, + }; + let e = Envelope { + protocol_major: 1, + protocol_minor: 0, + message_id: Uuid::new_v4().to_string(), + agent_id: self.binding.agent_id.to_string(), + enrollment_generation: 1, + connection_generation: self.channel.connection_generation, + boot_id: self.channel.boot_id.to_string(), + sequence: sequence as u64, + observed_at: self.clock.now.timestamp_millis(), + payload: Some(Payload::JobResult(r)), + }; + self.db + .execute( + "INSERT INTO job_events VALUES(?1,?2)", + params![e.message_id, e.encode_to_vec()], + ) + .unwrap(); + e + }); + (offer, grant, envelope) + } +} +#[test] +fn authenticated_helper_outcome_is_imported_as_new_durable_evidence_not_reexecuted() { + let mut f = Fixture::new(); + 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 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); +} +#[test] +fn helper_omitting_a_consumed_root_attempt_is_a_sticky_restore_fault() { + let mut f = Fixture::new(); + f.add( + "running", + true, + None, + Action::SetServiceState { + unit: "rmm-acceptance.service".into(), + state: "running".into(), + }, + ); + let helper = HelperSnapshot { + journal_id: Uuid::new_v4(), + checkpoint: 1, + previous_decision_id: None, + attempts: vec![], + }; + let mut c = f.client(); + assert!( + c.prepare( + &mut f.db, + &f.ledger, + f.channel.clone(), + Some(&helper), + &f.clock + ) + .is_err() + ); + assert!( + c.prepare(&mut f.db, &f.ledger, f.channel.clone(), None, &f.clock) + .is_err() + ); + assert!(!c.can_start(&f.channel, true, &f.clock)); +} +#[test] +fn definite_nonstart_result_is_unconsumed_not_ambiguous() { + let mut f = Fixture::new(); + let mut cancelled = success(); + cancelled.state = "cancelled".into(); + cancelled.stdout.clear(); + cancelled.exit_code = None; + cancelled.evidence = serde_json::json!({"not_started":true,"postconditions_passed":false}); + f.add("cancelled", false, Some(cancelled), script()); + let mut c = f.client(); + let p = f.prepare(&mut c); + assert_eq!(p.request.attempts[0].consumption, "unconsumed"); + let d = f.decision(&p, true); + f.apply(&mut c, &d).unwrap(); + assert!(c.can_start(&f.channel, false, &f.clock)); +} +#[test] +fn signed_terminal_nonstart_stays_unconsumed_across_sync_and_pending_denial() { + for terminal in ["expired", "cancelled"] { + let mut f = Fixture::new(); + let (offer, _, _) = f.add("accepted", false, None, script()); + let mut client = f.client(); + let first = f.prepare(&mut client); + assert_eq!(first.request.attempts[0].consumption, "unconsumed"); + let mut decision = f.decision(&first, true); + decision.attempts[0].outcome_state = terminal.into(); + decision.attempts[0].reason = "DEFINITIVE_NONSTART".into(); + f.apply(&mut client, &decision).unwrap(); + assert!(client.can_start(&f.channel, false, &f.clock)); + + let second = f.prepare(&mut client); + let local = &second.request.attempts[0]; + assert_eq!(local.state, terminal); + assert_eq!(local.consumption, "unconsumed"); + assert!(local.grant_id.is_empty() && local.outcome_bytes.is_empty()); + let mut repeated = f.decision(&second, true); + repeated.attempts[0].reason = "DEFINITIVE_NONSTART".into(); + f.apply(&mut client, &repeated).unwrap(); + assert!(client.can_start(&f.channel, false, &f.clock)); + + // Reproduce the old server's rejected terminal->unknown response. + // Keep its pending request and the last applied signed anchor, then + // recover through a fresh channel/request with the ordinary protocol. + let pending = f.prepare(&mut client); + let mut denied = f.decision(&pending, false); + denied.attempts[0].outcome_state = "unknown".into(); + denied.attempts[0].reason = "GRANT_OR_EFFECT_UNRESOLVED".into(); + assert!(f.apply(&mut client, &denied).is_err()); + assert!(!client.can_start(&f.channel, false, &f.clock)); + client = f.client(); + f.channel.connection_generation += 1; + let fresh = f.prepare(&mut client); + assert_eq!( + fresh.request.previous_request_id, + pending.request.request_id + ); + assert_eq!( + fresh.request.previous_decision_id, + repeated.decision_id.to_string() + ); + assert_eq!(fresh.request.attempts[0].consumption, "unconsumed"); + let mut recovered = f.decision(&fresh, true); + recovered.attempts[0].reason = "DEFINITIVE_NONSTART".into(); + f.apply(&mut client, &recovered).unwrap(); + assert!(client.can_start(&f.channel, false, &f.clock)); + let stored: (String, String, String) = + f.db.query_row("SELECT job,attempt,state FROM job_attempts", [], |r| { + Ok((r.get(0)?, r.get(1)?, r.get(2)?)) + }) + .unwrap(); + assert_eq!(stored, (offer.job_id, offer.attempt_id, terminal.into())); + assert_eq!( + f.ledger + .query_row("SELECT count(*) FROM consumed", [], |r| r.get::<_, i64>(0)) + .unwrap(), + 0 + ); + assert_eq!( + f.db.query_row("SELECT count(*) FROM reconcile_outcomes", [], |r| r + .get::<_, i64>(0)) + .unwrap(), + 0 + ); + } +} +#[test] +fn terminal_labels_never_override_grant_consumption_or_unclear_attempts() { + for state in ["requesting", "running", "unknown"] { + let mut f = Fixture::new(); + f.add(state, false, None, script()); + let mut client = f.client(); + let prepared = f.prepare(&mut client); + assert_eq!(prepared.request.attempts[0].consumption, "uncertain"); + let decision = f.decision(&prepared, true); + f.apply(&mut client, &decision).unwrap(); + assert!(!client.can_start(&f.channel, false, &f.clock)); + } + for terminal in ["expired", "cancelled"] { + for consumed in [false, true] { + let mut f = Fixture::new(); + f.add(terminal, consumed, None, script()); + // A surviving grant ID also forbids the nonstart shortcut when + // no consumption row exists. A real row always remains consumed. + if !consumed { + f.db.execute( + "UPDATE job_attempts SET grant_id=?1", + [Uuid::new_v4().to_string()], + ) + .unwrap(); + } + let mut client = f.client(); + let prepared = f.prepare(&mut client); + assert_eq!( + prepared.request.attempts[0].consumption, + if consumed { "consumed" } else { "uncertain" } + ); + let mut decision = f.decision(&prepared, true); + decision.attempts[0].unlock = false; + assert!(f.apply(&mut client, &decision).is_err()); + assert!(!client.can_start(&f.channel, false, &f.clock)); + assert_eq!( + f.ledger + .query_row( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get::<_, i64>(0) + ) + .unwrap(), + i64::from(consumed) + ); + } + } +} +#[test] +fn negative_server_revision_never_rolls_back_local_unknown_revision() { + let mut f = Fixture::new(); + f.add("running", true, None, script()); + let mut c = f.client(); + let p = f.prepare(&mut c); + assert_eq!(p.request.attempts[0].revision, 2); + let mut d = f.decision(&p, false); + d.attempts[0].revision = 1; + f.apply(&mut c, &d).unwrap(); + let revision: u64 = + f.db.query_row("SELECT revision FROM job_attempts", [], |r| r.get(0)) + .unwrap(); + assert_eq!(revision, 2); + assert!(!c.can_start(&f.channel, false, &f.clock)); +} +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,"z":{"b":2,"a":1}}), + error_code: String::new(), + } +} + +#[test] +fn clean_reconnect_and_reopen_require_new_signed_challenge() { + let mut f = Fixture::new(); + let mut client = f.client(); + assert!(!client.can_start(&f.channel, false, &f.clock)); + let prepared = f.prepare(&mut client); + let decision = f.decision(&prepared, true); + let response = f.sign(&decision); + client.apply(&mut f.db, &response, &f.clock).unwrap(); + assert!(client.can_start(&f.channel, false, &f.clock)); + assert_eq!(client.validity_deadline(), Some(decision.valid_until)); + assert!(!client.can_start(&f.channel, true, &f.clock)); + let mut reopened = Connection::open(f.dir.join("journal.sqlite")).unwrap(); + let mut process = f.client(); + assert!(!process.can_start(&f.channel, false, &f.clock)); + f.channel.connection_generation += 1; + let next = process + .prepare(&mut reopened, &f.ledger, f.channel.clone(), None, &f.clock) + .unwrap(); + assert_eq!( + next.request.previous_decision_id, + decision.decision_id.to_string() + ); + assert_eq!(next.request.journal_id, prepared.request.journal_id); + assert!(next.request.checkpoint > prepared.request.checkpoint); + assert!(process.apply(&mut reopened, &response, &f.clock).is_err()); + let next_decision = f.decision(&next, true); + process + .apply(&mut reopened, &f.sign(&next_decision), &f.clock) + .unwrap(); + assert!(process.can_start(&f.channel, false, &f.clock)); + let safety: String = reopened + .query_row("SELECT value FROM safety WHERE key='connected'", [], |r| { + r.get(0) + }) + .unwrap(); + assert_eq!(safety, "1"); +} +#[test] +fn exact_signatures_all_identity_channel_checkpoint_and_request_bindings() { + let mut f = Fixture::new(); + let mut client = f.client(); + let prepared = f.prepare(&mut client); + let d = f.decision(&prepared, true); + assert!( + verify_response( + &f.binding, + &prepared.request, + &f.channel, + &f.sign(&d), + f.clock.now + ) + .is_ok() + ); + let value = serde_json::to_value(&d).unwrap(); + for (field, replacement) 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!(0)), + ("request_id", serde_json::json!(Uuid::new_v4())), + ("request_hash", serde_json::json!(hash(b"different"))), + ("helper_journal_id", serde_json::json!(Uuid::new_v4())), + ("helper_checkpoint", serde_json::json!(1)), + ("key_id", serde_json::json!(hash(b"wrongkey"))), + ] { + let mut changed = value.clone(); + changed[field] = replacement; + let changed: ReconcileDecisionPayload = serde_json::from_value(changed).unwrap(); + assert!( + verify_response( + &f.binding, + &prepared.request, + &f.channel, + &f.sign(&changed), + f.clock.now + ) + .is_err(), + "{field}" + ); + } + let mut forged = f.sign(&d); + forged.signature[0] ^= 1; + assert!(client.apply(&mut f.db, &forged, &f.clock).is_err()); + let fake = SigningKey::from_bytes(&[43; 32]); + let mut forged = f.sign(&d); + forged.signature = fake.sign(&forged.decision_bytes).to_bytes().to_vec(); + assert!(client.apply(&mut f.db, &forged, &f.clock).is_err()); + let mut changed = f.sign(&d); + changed.decision_bytes.push(b' '); + assert!(client.apply(&mut f.db, &changed, &f.clock).is_err()); + let mut expired = d.clone(); + expired.valid_until = f.clock.now; + assert!(f.apply(&mut client, &expired).is_err()); + let mut unbounded = d; + unbounded.valid_until = unbounded.issued_at + Duration::seconds(60) + Duration::milliseconds(1); + assert!(f.apply(&mut client, &unbounded).is_err()); + assert!(!client.can_start(&f.channel, false, &f.clock)); +} +#[test] +fn accepted_unstarted_keeps_ids_and_never_consumes_or_executes() { + let mut f = Fixture::new(); + let (offer, _, _) = f.add("accepted", false, None, script()); + let mut c = f.client(); + let p = f.prepare(&mut c); + assert_eq!(p.request.attempts[0].consumption, "unconsumed"); + let d = f.decision(&p, true); + let r = f.sign(&d); + c.apply(&mut f.db, &r, &f.clock).unwrap(); + assert!(c.can_start(&f.channel, false, &f.clock)); + let count: i64 = f + .ledger + .query_row("SELECT count(*) FROM consumed", [], |r| r.get(0)) + .unwrap(); + assert_eq!(count, 0); + let ids: (String, String, u64) = + f.db.query_row("SELECT job,attempt,revision FROM job_attempts", [], |r| { + Ok((r.get(0)?, r.get(1)?, r.get(2)?)) + }) + .unwrap(); + assert_eq!(ids, (offer.job_id, offer.attempt_id, 1)); + c.apply(&mut f.db, &r, &f.clock).unwrap(); + assert!(c.can_start(&f.channel, false, &f.clock)); + let mut forged = r; + forged.signature[0] ^= 1; + assert!(c.apply(&mut f.db, &forged, &f.clock).is_err()); + assert!(!c.can_start(&f.channel, false, &f.clock)); +} +#[test] +fn requesting_running_and_omitted_attempts_cannot_regain_readiness() { + for state in ["requesting", "running"] { + let mut f = Fixture::new(); + f.add(state, state == "running", None, script()); + let mut c = f.client(); + let p = f.prepare(&mut c); + assert_eq!(p.request.attempts[0].state, "unknown"); + let mut d = f.decision(&p, true); + d.attempts.clear(); + assert!(f.apply(&mut c, &d).is_err()); + let d = f.decision(&p, true); + f.apply(&mut c, &d).unwrap(); + assert!(!c.can_start(&f.channel, false, &f.clock)); + let state: String = + f.db.query_row("SELECT state FROM job_attempts", [], |r| r.get(0)) + .unwrap(); + assert_eq!(state, "unknown"); + if p.request.attempts[0].consumption == "consumed" { + let locked: i64 = f + .ledger + .query_row( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get(0), + ) + .unwrap(); + assert_eq!(locked, 1); + } + } +} +#[test] +fn final_result_preserves_original_envelope_outputs_and_exact_acks() { + let mut f = Fixture::new(); + let (_, _, original) = f.add("succeeded", true, Some(success()), script()); + let original = original.unwrap(); + let mut c = f.client(); + let p = f.prepare(&mut c); + assert_eq!( + p.request.attempts[0].result_envelope, + Some(original.clone()) + ); + assert_eq!( + p.request.attempts[0].outcome_bytes, + canonical_outcome_bytes(&success()).unwrap() + ); + let unrelated = Envelope { + message_id: Uuid::new_v4().to_string(), + ..Default::default() + }; + f.db.execute( + "INSERT INTO job_events VALUES(?1,?2)", + params![unrelated.message_id, unrelated.encode_to_vec()], + ) + .unwrap(); + let mut d = f.decision(&p, true); + d.acknowledged_message_ids = vec![Uuid::parse_str(&original.message_id).unwrap()]; + let r = f.sign(&d); + c.apply(&mut f.db, &r, &f.clock).unwrap(); + assert!(!c.can_start(&f.channel, false, &f.clock)); + c.apply_consumption(&mut f.ledger, &f.clock).unwrap(); + c.apply_consumption(&mut f.ledger, &f.clock).unwrap(); + assert!(c.can_start(&f.channel, false, &f.clock)); + let pending: Vec = + f.db.prepare("SELECT id FROM job_events") + .unwrap() + .query_map([], |r| r.get(0)) + .unwrap() + .collect::>() + .unwrap(); + assert_eq!(pending, vec![unrelated.message_id]); + let archived: Vec = + f.db.query_row( + "SELECT envelope FROM reconcile_results WHERE id=?1", + [&original.message_id], + |r| r.get(0), + ) + .unwrap(); + assert_eq!(Envelope::decode(archived.as_slice()).unwrap(), original); + assert_eq!( + f.ledger + .query_row( + "SELECT count(*) FROM consumed WHERE result IS NOT NULL", + [], + |r| r.get::<_, i64>(0) + ) + .unwrap(), + 1 + ); +} +#[test] +fn foreign_ack_and_noncanonical_outcome_are_transactionally_rejected() { + let mut f = Fixture::new(); + f.add("succeeded", true, Some(success()), script()); + let mut c = f.client(); + let p = f.prepare(&mut c); + let mut d = f.decision(&p, true); + d.acknowledged_message_ids = vec![Uuid::new_v4()]; + assert!(f.apply(&mut c, &d).is_err()); + let mut d = f.decision(&p, 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 c, &d).is_err()); + assert_eq!( + f.db.query_row("SELECT count(*) FROM job_events", [], |r| r + .get::<_, i64>(0)) + .unwrap(), + 1 + ); +} +#[test] +fn missing_server_job_fault_is_sticky_even_if_attempt_disappears_later() { + let mut f = Fixture::new(); + f.add("accepted", false, None, script()); + let mut c = f.client(); + let p = f.prepare(&mut c); + let mut d = f.decision(&p, false); + d.reason = "MISSING_SERVER_JOB".into(); + d.attempts[0].reason = "MISSING_SERVER_JOB".into(); + d.attempts[0].action.clear(); + d.attempts[0].execution_identity.clear(); + f.apply(&mut c, &d).unwrap(); + f.db.execute("DELETE FROM job_attempts", []).unwrap(); + let mut fresh = f.client(); + assert!( + fresh + .prepare(&mut f.db, &f.ledger, f.channel.clone(), None, &f.clock) + .is_err() + ); + assert!(!fresh.can_start(&f.channel, false, &f.clock)); +} +#[test] +fn rollback_and_false_clock_close_mutations_but_do_not_destroy_journal() { + let mut f = Fixture::new(); + let mut c = f.client(); + let p = f.prepare(&mut c); + let d = f.decision(&p, true); + f.apply(&mut c, &d).unwrap(); + f.clock.trusted = false; + assert!(!c.can_start(&f.channel, false, &f.clock)); + assert!( + c.prepare(&mut f.db, &f.ledger, f.channel.clone(), None, &f.clock) + .is_err() + ); + f.clock.trusted = true; + f.db.execute("UPDATE counter SET seq=0", []).unwrap(); + assert!( + c.prepare(&mut f.db, &f.ledger, f.channel.clone(), None, &f.clock) + .is_err() + ); + f.db.execute("UPDATE counter SET seq=999", []).unwrap(); + assert!( + c.prepare(&mut f.db, &f.ledger, f.channel.clone(), None, &f.clock) + .is_err() + ); +} +#[test] +fn uncertain_request_preserves_pending_request_and_last_applied_anchor() { + let mut f = Fixture::new(); + let mut c = f.client(); + let first = f.prepare(&mut c); + let d = f.decision(&first, true); + f.apply(&mut c, &d).unwrap(); + let lost = f.prepare(&mut c); + let _server_decision = f.sign(&f.decision(&lost, true)); + c.close(); + f.channel.connection_generation += 1; + let recovered = f.prepare(&mut c); + assert_ne!(lost.request.request_id, recovered.request.request_id); + assert_eq!( + recovered.request.previous_request_id, + lost.request.request_id + ); + assert_eq!( + recovered.request.previous_decision_id, + d.decision_id.to_string() + ); +} + +#[test] +fn helper_gate_requires_exact_signed_instruction_and_applied_checkpoint() { + let mut f = Fixture::new(); + let mut c = f.client(); + let mut helper = HelperSnapshot { + journal_id: Uuid::new_v4(), + checkpoint: 42, + previous_decision_id: None, + attempts: vec![], + }; + let p = c + .prepare( + &mut f.db, + &f.ledger, + f.channel.clone(), + Some(&helper), + &f.clock, + ) + .unwrap(); + assert_eq!(p.envelope.sequence, p.request.checkpoint); + assert_eq!( + p.envelope.payload, + Some(Payload::SyncRequest(p.request.clone())) + ); + let d = f.decision(&p, true); + let response = f.sign(&d); + let applied = c.apply(&mut f.db, &response, &f.clock).unwrap(); + assert_eq!(applied.decision_id, d.decision_id); + assert_eq!(applied.reason, "CONSISTENT"); + assert!(applied.acknowledged_message_ids.is_empty()); + let instruction = applied.helper_instruction.unwrap(); + assert_eq!(instruction.response, response); + assert_eq!(instruction.checkpoint, 42); + assert_eq!(instruction.journal_id, helper.journal_id); + assert!(c.can_start(&f.channel, false, &f.clock)); + assert!(!c.can_start(&f.channel, true, &f.clock)); + assert!(c.confirm_helper(&helper).is_err()); + helper.previous_decision_id = Some(instruction.decision_id); + c.confirm_helper(&helper).unwrap(); + assert!(c.can_start(&f.channel, true, &f.clock)); + f.clock.now += Duration::seconds(60); + assert!(!c.can_start(&f.channel, false, &f.clock)); + assert!(!c.can_start(&f.channel, true, &f.clock)); +} +#[test] +fn independent_helper_checkpoint_regression_is_sticky() { + let mut f = Fixture::new(); + let mut c = f.client(); + let mut helper = HelperSnapshot { + journal_id: Uuid::new_v4(), + checkpoint: 5, + previous_decision_id: None, + attempts: vec![], + }; + let p = c + .prepare( + &mut f.db, + &f.ledger, + f.channel.clone(), + Some(&helper), + &f.clock, + ) + .unwrap(); + let d = f.decision(&p, true); + f.apply(&mut c, &d).unwrap(); + helper.previous_decision_id = Some(d.decision_id); + helper.checkpoint = 4; + f.channel.connection_generation += 1; + assert!( + c.prepare( + &mut f.db, + &f.ledger, + f.channel.clone(), + Some(&helper), + &f.clock + ) + .is_err() + ); + helper.checkpoint = 5; + assert!( + c.prepare( + &mut f.db, + &f.ledger, + f.channel.clone(), + Some(&helper), + &f.clock + ) + .is_err() + ); +} +#[test] +fn reboot_keeps_lock_until_signed_new_authenticated_boot_inventory_evidence() { + let mut f = Fixture::new(); + let previous_boot = f.channel.boot_id; + let mut awaiting = success(); + awaiting.state = "awaiting_reboot".into(); + awaiting.stdout.clear(); + awaiting.exit_code = None; + awaiting.reboot_required = true; + awaiting.evidence = + serde_json::json!({"postconditions_passed":false,"previous_boot_id":previous_boot}); + let (_, grant, _) = f.add( + "awaiting_reboot", + true, + Some(awaiting), + Action::Reboot { + reason: "mock only".into(), + reconnect_timeout_seconds: 120, + dependency_group: "acceptance".into(), + }, + ); + let mut c = f.client(); + let p = f.prepare(&mut c); + assert_eq!( + p.request.attempts[0].original_boot_id, + previous_boot.to_string() + ); + let d = f.decision(&p, false); + f.apply(&mut c, &d).unwrap(); + assert!(!c.can_start(&f.channel, false, &f.clock)); + let preserved: String = + f.db.query_row("SELECT state FROM job_attempts", [], |r| r.get(0)) + .unwrap(); + assert_eq!(preserved, "awaiting_reboot"); + f.channel.connection_generation += 1; + f.channel.boot_id = Uuid::new_v4(); + let p = f.prepare(&mut c); + let mut d = f.decision(&p, true); + let mut definitive = success(); + definitive.stdout.clear(); + definitive.exit_code = None; + definitive.evidence = serde_json::json!({"postconditions_passed":true,"new_boot_id":previous_boot,"reconciled_after_reboot":true,"inventory_message_id":Uuid::new_v4()}); + d.attempts[0].outcome_state = "succeeded".into(); + d.attempts[0].revision += 1; + d.attempts[0].unlock = true; + d.attempts[0].outcome_bytes = canonical_outcome_bytes(&definitive).unwrap(); + d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); + assert!(f.apply(&mut c, &d).is_err()); + definitive.evidence["new_boot_id"] = serde_json::json!(f.channel.boot_id); + d.attempts[0].outcome_bytes = canonical_outcome_bytes(&definitive).unwrap(); + d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); + f.apply(&mut c, &d).unwrap(); + assert!(!c.can_start(&f.channel, false, &f.clock)); + c.apply_consumption(&mut f.ledger, &f.clock).unwrap(); + assert!(c.can_start(&f.channel, false, &f.clock)); + let grant = grant.unwrap(); + let result: Vec = f + .ledger + .query_row( + "SELECT result FROM consumed WHERE grant_id=?1", + [grant.grant_id.to_string()], + |r| r.get(0), + ) + .unwrap(); + assert_eq!(result, canonical_outcome_bytes(&definitive).unwrap()); + assert_eq!( + f.ledger + .query_row("SELECT count(*) FROM consumed", [], |r| r.get::<_, i64>(0)) + .unwrap(), + 1 + ); +} +#[test] +fn independent_local_outcome_mismatch_never_unlocks() { + let mut f = Fixture::new(); + let (_, grant, _) = f.add("succeeded", true, Some(success()), script()); + let mut c = f.client(); + let p = f.prepare(&mut c); + let d = f.decision(&p, true); + f.apply(&mut c, &d).unwrap(); + let mut altered = success(); + altered.stdout = b"forged result".to_vec(); + f.ledger + .execute( + "UPDATE execution_evidence SET outcome=?1", + [canonical_outcome_bytes(&altered).unwrap()], + ) + .unwrap(); + assert!(c.apply_consumption(&mut f.ledger, &f.clock).is_err()); + assert!(!c.can_start(&f.channel, false, &f.clock)); + let unresolved: i64 = f + .ledger + .query_row( + "SELECT count(*) FROM consumed WHERE grant_id=?1 AND result IS NULL", + [grant.unwrap().grant_id.to_string()], + |r| r.get(0), + ) + .unwrap(); + assert_eq!(unresolved, 1); +} +#[test] +fn signed_attempt_mutation_or_terminal_regression_is_rejected() { + let mut f = Fixture::new(); + f.add("succeeded", true, Some(success()), script()); + let mut c = f.client(); + let p = f.prepare(&mut c); + let d = f.decision(&p, true); + let mut changed = d.clone(); + changed.attempts[0].outcome_state = "unknown".into(); + changed.attempts[0].unlock = false; + assert!(f.apply(&mut c, &changed).is_err()); + let mut changed = d.clone(); + changed.attempts[0].authorization_id = Some(Uuid::new_v4()); + assert!(f.apply(&mut c, &changed).is_err()); + let mut changed = d.clone(); + changed.attempts[0].plan_hash = Some(hash(b"changed plan")); + assert!(f.apply(&mut c, &changed).is_err()); + let mut changed = d; + changed.attempts[0].previous_outcome_sha256 = Some(hash(b"other outcome")); + assert!(f.apply(&mut c, &changed).is_err()); +} diff --git a/crates/agent-windows/Cargo.toml b/crates/agent-windows/Cargo.toml index 1697c38..97efd3b 100644 --- a/crates/agent-windows/Cargo.toml +++ b/crates/agent-windows/Cargo.toml @@ -20,10 +20,14 @@ tokio.workspace = true tokio-tungstenite.workspace = true uuid.workspace = true rmm-domain = { path = "../domain" } +rmm-agent-core = { path = "../agent-core" } rmm-protocol = { path = "../protocol" } rusqlite = { version = "0.32.1", features = ["bundled"] } fs2 = "0.4.3" rcgen = { version = "=0.14.10", default-features = false, features = ["crypto", "ring", "pem"] } +[build-dependencies] +cc = "=1.11.1" + [target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_SystemInformation", "Win32_System_Registry", "Win32_System_Memory", "Win32_System_Services", "Win32_System_Threading"] } +windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_JobObjects", "Win32_System_Pipes", "Win32_System_SystemInformation", "Win32_System_Registry", "Win32_System_Memory", "Win32_System_Services", "Win32_System_Threading"] } diff --git a/crates/agent/Cargo.toml b/crates/agent/Cargo.toml index 284ad3e..9189642 100644 --- a/crates/agent/Cargo.toml +++ b/crates/agent/Cargo.toml @@ -5,6 +5,7 @@ edition.workspace = true license.workspace = true default-run = "rmm-agent" [dependencies] +rmm-agent-core = { path = "../agent-core", features = ["openssl-verifier"] } anyhow.workspace = true chrono.workspace = true futures-util.workspace = true diff --git a/crates/agent/src/jobs.rs b/crates/agent/src/jobs.rs index 6ed5713..f680f2c 100644 --- a/crates/agent/src/jobs.rs +++ b/crates/agent/src/jobs.rs @@ -1,9 +1,13 @@ use anyhow::{Result, ensure}; +#[cfg(test)] use prost::Message; -use rmm_domain::jobs::{Action, JobPayload, hash}; -use rmm_privileged_helper::{Binding, Outcome, Request}; +#[cfg(test)] +use rmm_domain::jobs::hash; +use rmm_domain::jobs::{Action, JobPayload}; +use rmm_privileged_helper::{Binding, Outcome}; use rmm_protocol::*; -use rusqlite::{Connection, OptionalExtension, params}; +use rusqlite::Connection; +#[cfg(test)] use uuid::Uuid; pub fn supported(job: &JobPayload) -> bool { @@ -53,242 +57,14 @@ fn supported_as(job: &JobPayload, uid: u32, helper_configured: bool) -> bool { _ => false, } } +pub use rmm_agent_core::jobs::{ + ack, event, events, interrupted, interrupted_request, next, no_start, permit_request, + requesting, reserve_request, result, transition, +}; pub fn accept(db: &Connection, binding: &Binding, offer: &JobOffer) -> Result { - let job = binding.job(&offer.payload_bytes)?; - ensure!( - offer.job_id == job.job_id.to_string() - && offer.attempt_id == job.attempt_id.to_string() - && offer.job_payload_hash == hash(&offer.payload_bytes) - && Uuid::parse_str(&offer.authorization_id).is_ok_and(|id| !id.is_nil()) - && offer.plan_hash.len() == 64 - && hex_hash(&offer.plan_hash), - "OFFER_BINDING" - ); - ensure!( - supported_with_binding(&job, Some(binding)), - "UNSUPPORTED_CAPABILITY" - ); - let bytes = offer.encode_to_vec(); - let existing: Option> = db - .query_row( - "SELECT offer FROM job_attempts WHERE job=?1 AND attempt=?2", - params![offer.job_id, offer.attempt_id], - |r| r.get(0), - ) - .optional()?; - if let Some(old) = existing { - ensure!(old == bytes, "OFFER_CONFLICT"); - return Ok(false); - } - let count: i64 = db.query_row("SELECT count(*) FROM job_attempts", [], |r| r.get(0))?; - ensure!(count < 10000, "JOURNAL_CAPACITY_NEW_EFFECTS_BLOCKED"); - db.execute( - "INSERT INTO job_attempts(job,attempt,offer,state) VALUES(?1,?2,?3,'accepted')", - params![offer.job_id, offer.attempt_id, bytes], - )?; - Ok(true) -} -fn hex_hash(s: &str) -> bool { - s.bytes() - .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) -} - -/// Inspect ONLY at a new process startup, never on a transport reconnect: no -/// executor thread from the previous process can still deliver its outcome. -/// This does not execute, release consumption or invent a successful outcome. -pub fn interrupted( - db: &Connection, - ledger: &Connection, - binding: &Binding, -) -> Result> { - let mut statement = db.prepare( - "SELECT offer,grant_id FROM job_attempts WHERE state IN ('requesting','running','verifying') ORDER BY job,attempt", - )?; - let rows = statement.query_map([], |r| { - Ok((r.get::<_, Vec>(0)?, r.get::<_, String>(1)?)) - })?; - let mut interrupted = Vec::new(); - for row in rows { - let (bytes, grant) = row?; - let offer = JobOffer::decode(bytes.as_slice())?; - let job = binding.job(&offer.payload_bytes)?; - ensure!( - offer.job_id == job.job_id.to_string() - && offer.attempt_id == job.attempt_id.to_string() - && offer.job_payload_hash == hash(&offer.payload_bytes) - && Uuid::parse_str(&offer.authorization_id).is_ok_and(|id| !id.is_nil()) - && offer.plan_hash.len() == 64 - && hex_hash(&offer.plan_hash), - "INTERRUPTED_OFFER_BINDING" - ); - if !grant.is_empty() { - let row: Option<(String, u64, String, String, Vec)> = ledger - .query_row( - "SELECT c.agent,c.generation,c.job,c.attempt,e.start FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?1", - [&grant], - |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?)), - ) - .optional()?; - let (agent, generation, job_id, attempt, start) = - row.ok_or_else(|| anyhow::anyhow!("INTERRUPTED_START_EVIDENCE_MISSING"))?; - ensure!( - agent == binding.agent_id.to_string() - && generation == binding.enrollment_generation - && job_id == offer.job_id - && attempt == offer.attempt_id, - "INTERRUPTED_CONSUMPTION_BINDING" - ); - let start: serde_json::Value = serde_json::from_slice(&start)?; - let request: Request = serde_json::from_value(start["request"].clone())?; - let permit: rmm_domain::jobs::ExecutionPermitPayload = - serde_json::from_slice(&request.permit_bytes)?; - // Verify the historical signed request at its original valid time; - // this is evidence only, never permission to start it again. - let (_, verified) = binding.verify(&request, permit.not_before)?; - ensure!( - verified == permit - && permit.grant_id.to_string() == grant - && request.job_bytes == offer.payload_bytes - && request.plan_hash == offer.plan_hash - && request.authorization_id.to_string() == offer.authorization_id, - "INTERRUPTED_START_BINDING" - ); - } else { - // A request may have committed remotely without its reply arriving. - // It can only become local unknown, never a fabricated grant/result. - let state: String = db.query_row( - "SELECT state FROM job_attempts WHERE job=?1 AND attempt=?2", - params![offer.job_id, offer.attempt_id], - |r| r.get(0), - )?; - ensure!(state == "requesting", "INTERRUPTED_RUNNING_GRANT_MISSING"); - } - interrupted.push((offer, grant)); - } - Ok(interrupted) -} - -pub fn interrupted_request(db: &Connection, offer: &JobOffer) -> Result<()> { - ensure!( - db.execute( - "UPDATE job_attempts SET state='unknown',revision=revision+1 WHERE job=?1 AND attempt=?2 AND state='requesting' AND grant_id=''", - params![offer.job_id, offer.attempt_id], - )? == 1, - "INTERRUPTED_REQUEST_STATE" - ); - Ok(()) -} -pub fn event(db: &Connection, envelope: &Envelope) -> Result<()> { - let count: i64 = db.query_row("SELECT count(*) FROM job_events", [], |r| r.get(0))?; - ensure!(count < 1000, "UNACKNOWLEDGED_EVENT_LIMIT"); - db.execute( - "INSERT INTO job_events(id,payload) VALUES(?1,?2)", - params![envelope.message_id, envelope.encode_to_vec()], - )?; - Ok(()) -} -pub fn events(db: &Connection) -> Result> { - let mut statement = db.prepare("SELECT payload FROM job_events ORDER BY rowid LIMIT 100")?; - let bytes = statement - .query_map([], |r| r.get::<_, Vec>(0))? - .collect::>>()?; - bytes - .into_iter() - .map(|b| Ok(Envelope::decode(b.as_slice())?)) - .collect() -} -pub fn ack(db: &mut Connection, id: &str) -> Result<()> { - let tx = db.transaction()?; - crate::reconcile::archive_events(&tx)?; - tx.execute("DELETE FROM job_events WHERE id=?1", [id])?; - tx.commit()?; - Ok(()) -} -pub fn next(db: &Connection) -> Result> { - // One device-wide effect at a time; unknown/reserved attempts retain locks. - let busy: i64 = db.query_row("SELECT count(*) FROM job_attempts WHERE state IN ('requesting','running','unknown','awaiting_reboot')", [], |r| r.get(0))?; - if busy != 0 { - return Ok(None); - } - let mut statement = db.prepare("SELECT offer FROM job_attempts WHERE state='accepted' AND cancel_requested=0 ORDER BY rowid")?; - let rows = statement.query_map([], |r| r.get::<_, Vec>(0))?; - let now = chrono::Utc::now(); - for bytes in rows { - let offer = JobOffer::decode(bytes?.as_slice())?; - let job: JobPayload = serde_json::from_slice(&offer.payload_bytes)?; - if now >= job.not_before || now >= job.start_before { - return Ok(Some(offer)); - } - } - Ok(None) -} -pub fn requesting(db: &Connection) -> Result> { - let bytes: Option> = db - .query_row( - "SELECT offer FROM job_attempts WHERE state='requesting' LIMIT 1", - [], - |r| r.get(0), - ) - .optional()?; - bytes - .map(|b| Ok(JobOffer::decode(b.as_slice())?)) - .transpose() -} -pub fn reserve_request(db: &Connection, offer: &JobOffer) -> Result<()> { - ensure!(db.execute("UPDATE job_attempts SET state='requesting' WHERE job=?1 AND attempt=?2 AND state='accepted' AND cancel_requested=0", - params![offer.job_id,offer.attempt_id])? == 1, "ATTEMPT_NOT_STARTABLE"); - Ok(()) -} -pub fn transition(db: &Connection, offer: &JobOffer, state: &str, grant: &str) -> Result { - let previous: String = db.query_row( - "SELECT state FROM job_attempts WHERE job=?1 AND attempt=?2", - params![offer.job_id, offer.attempt_id], - |r| r.get(0), - )?; - let allowed = match previous.as_str() { - "accepted" | "requesting" => matches!( - state, - "running" | "failed" | "cancelled" | "expired" | "unknown" - ), - "running" | "verifying" => matches!( - state, - "verifying" | "succeeded" | "failed" | "cancelled" | "unknown" | "awaiting_reboot" - ), - // Only a definitive completion of the already-running executor calls - // this reconciliation path. No new effect is started from unknown. - "unknown" | "awaiting_reboot" => matches!( - state, - "succeeded" | "failed" | "cancelled" | "unknown" | "awaiting_reboot" - ), - _ => false, - }; - ensure!(allowed, "INVALID_LOCAL_STATE_TRANSITION"); - let rev: i64 = db.query_row("UPDATE job_attempts SET state=?3,grant_id=?4,revision=revision+1 WHERE job=?1 AND attempt=?2 RETURNING revision", - params![offer.job_id,offer.attempt_id,state,grant], |r| r.get(0))?; - Ok(u64::try_from(rev)?) -} -pub fn result(offer: &JobOffer, revision: u64, grant: &str, outcome: Outcome) -> JobResult { - JobResult { - job_id: offer.job_id.clone(), - attempt_id: offer.attempt_id.clone(), - revision, - state: outcome.state, - grant_id: grant.into(), - stdout: outcome.stdout, - stderr: outcome.stderr, - output_truncated: outcome.output_truncated, - reboot_required: outcome.reboot_required, - exit_code: outcome.exit_code, - evidence_json: outcome.evidence.to_string(), - error_code: outcome.error_code, - } -} -pub fn no_start(state: &str, code: &str) -> Outcome { - Outcome { - state: state.into(), - evidence: serde_json::json!({"not_started":true,"postconditions_passed":false}), - ..Outcome::unknown(code) - } + rmm_agent_core::jobs::accept(db, binding, offer, |job| { + supported_with_binding(job, Some(binding)) + }) } pub fn execute(job: &JobPayload) -> Outcome { let result = match &job.action { @@ -397,16 +173,6 @@ fn query_logs(job: &JobPayload) -> Result { }, }) } -pub fn permit_request(offer: &JobOffer, permit: &ExecutionPermit) -> Result { - Ok(Request { - job_bytes: offer.payload_bytes.clone(), - permit_bytes: permit.payload_bytes.clone(), - signature: permit.signature.clone(), - plan_hash: offer.plan_hash.clone(), - authorization_id: Uuid::parse_str(&offer.authorization_id)?, - }) -} - /// With no backend SyncResponse contract, a reconnect/restart cannot prove an /// earlier reservation never started. Telemetry and evidence replay continue; /// all new effects stop. Do not paper over this with a new start request. diff --git a/crates/agent/src/reconcile.rs b/crates/agent/src/reconcile.rs index eda2d6e..aaecb5d 100644 --- a/crates/agent/src/reconcile.rs +++ b/crates/agent/src/reconcile.rs @@ -1,31 +1,8 @@ -//! Reconnect reconciliation, deliberately independent of main/jobs and transport. -//! No method executes a job, deletes consumption tombstones or issues a permit. -//! Call only while holding the existing agent.lock. HelperSnapshot must come -//! from the independently authenticated root-helper IPC, never from the server. -use anyhow::{Context, Result, ensure}; +//! Linux adapter for the common durable signed reconciliation state machine. +//! The existing Linux native clock and OpenSSL verification profile remain. +use anyhow::{Result, ensure}; use chrono::{DateTime, Utc}; -use openssl::{ - pkey::{Id, PKey}, - sign::Verifier, -}; -use prost::Message; -use rmm_domain::{ - jobs::{JobPayload, hash}, - reconciliation::{ - AttemptDecision, ReconcileDecisionPayload, ReconciledOutcome, canonical_outcome_bytes, - }, -}; -use rmm_privileged_helper::Binding; -use rmm_protocol::{ - Envelope, JobOffer, JobResult, Payload, SyncAttempt, SyncRequest, SyncResponse, -}; -use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params}; -use std::collections::{BTreeMap, BTreeSet}; -use uuid::Uuid; - -pub trait SyncClock { - fn now(&self) -> Result>; -} +pub use rmm_agent_core::reconcile::*; pub struct NativeClock(rmm_privileged_helper::TrustedClock); impl Default for NativeClock { fn default() -> Self { @@ -38,1188 +15,3 @@ impl SyncClock for NativeClock { Ok(Utc::now()) } } -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Channel { - pub connection_generation: u64, - pub boot_id: Uuid, -} -#[derive(Clone, Debug)] -pub struct HelperSnapshot { - pub journal_id: Uuid, - pub checkpoint: u64, - pub previous_decision_id: Option, - /// Complete consumed/unsettled summaries from the root-owned ledger. - pub attempts: Vec, -} -#[derive(Clone)] -pub struct PreparedSync { - pub envelope: Envelope, - pub request: SyncRequest, -} -/// Pass the ORIGINAL signed response to the helper. It must independently verify -/// it, apply targeted unlocks and commit its checkpoint before acknowledging. -#[derive(Clone)] -pub struct HelperInstruction { - pub response: SyncResponse, - pub decision_id: Uuid, - pub journal_id: Uuid, - pub checkpoint: u64, -} -pub struct Applied { - pub decision_id: Uuid, - pub acknowledged_message_ids: Vec, - pub helper_instruction: Option, - pub reason: String, -} -struct Gate { - channel: Channel, - until: DateTime, - ready: bool, - helper_ready: bool, - helper: Option<(Uuid, u64, Uuid)>, - local_applied: bool, - request: SyncRequest, - response: SyncResponse, -} -pub struct ClientSync { - binding: Binding, - pending: Option, - channel: Option, - gate: Option, -} -impl ClientSync { - /// Every new process starts closed, independently of any persisted ready bit. - pub fn new(binding: Binding) -> Self { - Self { - binding, - pending: None, - channel: None, - gate: None, - } - } - /// Invoke on every authenticated/fenced ServerHello or transport loss. - pub fn close(&mut self) { - self.gate = None; - self.channel = None; - self.pending = None; - } - pub fn validity_deadline(&self) -> Option> { - self.gate.as_ref().map(|gate| gate.until) - } - pub fn can_start(&self, channel: &Channel, root: bool, clock: &impl SyncClock) -> bool { - let Ok(now) = clock.now() else { - return false; - }; - self.gate.as_ref().is_some_and(|g| { - g.channel == *channel - && g.ready - && g.local_applied - && now < g.until - && (!root || (g.helper_ready && g.helper.is_none())) - }) - } - /// Only call with a fresh independently authenticated helper snapshot. There - /// is no unsigned ready/unlock flag: the exact signed decision/checkpoint - /// must already be durably applied by that helper. - pub fn confirm_helper(&mut self, snapshot: &HelperSnapshot) -> Result<()> { - let gate = self.gate.as_mut().context("NO_SIGNED_DECISION")?; - let expected = gate.helper.context("NO_HELPER_APPLY_PENDING")?; - ensure!( - snapshot.journal_id == expected.0 - && snapshot.checkpoint == expected.1 - && snapshot.previous_decision_id == Some(expected.2), - "HELPER_CHECKPOINT_NOT_APPLIED" - ); - ensure!( - snapshot - .attempts - .iter() - .all(|a| terminal(&a.state) - || (a.consumption == "unconsumed" && a.state == "accepted")), - "HELPER_EFFECT_UNRESOLVED" - ); - gate.helper = None; - Ok(()) - } - pub fn prepare( - &mut self, - db: &mut Connection, - ledger: &Connection, - channel: Channel, - helper: Option<&HelperSnapshot>, - clock: &impl SyncClock, - ) -> Result { - let result = self.prepare_inner(db, ledger, channel, helper, clock); - if let Err(error) = &result { - let text = error.to_string(); - if [ - "ROLLBACK", - "REGRESSION", - "DISAPPEARED", - "MISSING_FROM_AGENT_JOURNAL", - "MISSING_FROM_HELPER_JOURNAL", - ] - .iter() - .any(|s| text.contains(s)) - { - let _ = db.execute( - "UPDATE reconcile_meta SET fault=COALESCE(fault,?1) WHERE id=1", - [text], - ); - } - } - result - } - fn prepare_inner( - &mut self, - db: &mut Connection, - ledger: &Connection, - channel: Channel, - helper: Option<&HelperSnapshot>, - clock: &impl SyncClock, - ) -> Result { - self.close(); - let now = clock.now()?; - ensure!( - channel.connection_generation > 0 && !channel.boot_id.is_nil(), - "UNAUTHENTICATED_CHANNEL" - ); - initialize(db)?; - let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; - let meta = metadata(&tx)?; - ensure!(meta.fault.is_none(), "SYNC_MANUAL_RECOVERY_REQUIRED"); - let sequence: i64 = tx.query_row("SELECT seq FROM counter WHERE id=1", [], |r| r.get(0))?; - ensure!( - sequence >= meta.checkpoint && sequence >= meta.high_sequence, - "LOCAL_CHECKPOINT_ROLLBACK" - ); - ensure!( - channel.connection_generation >= meta.generation, - "CONNECTION_GENERATION_REGRESSION" - ); - if let Some(h) = helper { - ensure!( - !h.journal_id.is_nil() && h.checkpoint <= i64::MAX as u64, - "HELPER_CHECKPOINT" - ); - if let Some(old) = meta.helper_id { - ensure!( - old == h.journal_id && h.checkpoint >= meta.helper_checkpoint, - "HELPER_JOURNAL_ROLLBACK" - ); - } - } else { - ensure!(meta.helper_id.is_none(), "HELPER_JOURNAL_DISAPPEARED"); - } - if let Some(h) = helper { - ingest_helper(&tx, ledger, &self.binding, &channel, h, now)?; - } - archive_events(&tx)?; - tx.execute( - "UPDATE job_attempts SET state='accepted' WHERE state='queued' AND grant_id=''", - [], - )?; - // A possibly started attempt is NEVER returned to accepted. Preserve - // revisions and results; only ambiguous no-result states become unknown. - tx.execute("UPDATE job_attempts SET state='unknown',revision=revision+1 WHERE state IN ('requesting','running','verifying') AND NOT EXISTS(SELECT 1 FROM reconcile_results r WHERE r.job=job_attempts.job AND r.attempt=job_attempts.attempt AND r.revision>=job_attempts.revision)",[])?; - let mut attempts = attempts(&tx, ledger, &self.binding)?; - if let Some(h) = helper { - merge_helper(&mut attempts, h)?; - } - ensure!(attempts.len() <= 256, "SYNC_ATTEMPT_CAPACITY_NO_COMPACTION"); - let ids = pending_ids(&tx)?; - ensure!(ids.len() <= 256, "SYNC_RECEIPT_CAPACITY"); - let next: i64 = tx.query_row( - "UPDATE counter SET seq=seq+1 WHERE id=1 RETURNING seq", - [], - |r| r.get(0), - )?; - let request = SyncRequest { - request_id: Uuid::new_v4().to_string(), - journal_id: meta.journal_id.to_string(), - checkpoint: u64::try_from(next)?, - // A pending request may have committed with a lost reply, or may - // never have reached the server. Preserve both witnesses: the - // server resolves the pending request first and otherwise checks - // this last applied decision against its authoritative head. - previous_decision_id: meta.decision.map(|id| id.to_string()).unwrap_or_default(), - assignment_version: self.binding.assignment_version, - attempts, - durable_message_ids: ids, - journal_complete: true, - helper_journal_id: helper.map(|h| h.journal_id.to_string()).unwrap_or_default(), - helper_checkpoint: helper.map_or(0, |h| h.checkpoint), - helper_previous_decision_id: helper - .and_then(|h| h.previous_decision_id) - .map(|id| id.to_string()) - .unwrap_or_default(), - previous_request_id: meta - .pending_request - .map(|id| id.to_string()) - .unwrap_or_default(), - }; - let envelope = Envelope { - protocol_major: 1, - protocol_minor: 0, - message_id: Uuid::new_v4().to_string(), - agent_id: self.binding.agent_id.to_string(), - enrollment_generation: self.binding.enrollment_generation, - connection_generation: channel.connection_generation, - boot_id: channel.boot_id.to_string(), - sequence: u64::try_from(next)?, - observed_at: now.timestamp_millis(), - payload: Some(Payload::SyncRequest(request.clone())), - }; - ensure!( - envelope.encoded_len() <= rmm_domain::MAX_MESSAGE, - "SYNC_ENVELOPE_SIZE" - ); - tx.execute( - "INSERT INTO reconcile_requests(id,request,envelope,generation) VALUES(?1,?2,?3,?4)", - params![ - request.request_id, - request.encode_to_vec(), - envelope.encode_to_vec(), - channel.connection_generation - ], - )?; - tx.execute("UPDATE reconcile_meta SET checkpoint=?1,high_sequence=?1,generation=?2,pending_request=?3 WHERE id=1",params![next,channel.connection_generation,request.request_id])?; - tx.commit()?; - let prepared = PreparedSync { envelope, request }; - self.pending = Some(prepared.clone()); - self.channel = Some(channel); - Ok(prepared) - } - /// All changes, concrete ACKs, result archives and checkpoint chain commit - /// together. A rejection closes only mutation authority; telemetry may run. - pub fn apply( - &mut self, - db: &mut Connection, - response: &SyncResponse, - clock: &impl SyncClock, - ) -> Result { - if self.pending.is_none() { - let result = self.replay_applied(db, response, clock); - if result.is_err() { - self.gate = None; - } - return result; - } - self.gate = None; - let result = self.apply_inner(db, response, clock); - if result.is_err() { - self.gate = None; - } - result - } - fn replay_applied( - &self, - db: &Connection, - response: &SyncResponse, - clock: &impl SyncClock, - ) -> Result { - let gate = self.gate.as_ref().context("NO_CURRENT_SYNC_CHALLENGE")?; - ensure!(gate.response == *response, "REPLAYED_DIFFERENT_DECISION"); - let d = verify_response( - &self.binding, - &gate.request, - &gate.channel, - response, - clock.now()?, - )?; - let meta = metadata(db)?; - let sequence: i64 = db.query_row("SELECT seq FROM counter WHERE id=1", [], |r| r.get(0))?; - ensure!( - meta.decision == Some(d.decision_id) - && meta.pending_request.is_none() - && meta.checkpoint == i64::try_from(d.checkpoint)? - && sequence >= meta.high_sequence, - "APPLIED_CHECKPOINT_REGRESSION" - ); - Ok(Applied { - decision_id: d.decision_id, - acknowledged_message_ids: d.acknowledged_message_ids, - helper_instruction: d.helper_journal_id.map(|journal_id| HelperInstruction { - response: response.clone(), - decision_id: d.decision_id, - journal_id, - checkpoint: d.helper_checkpoint, - }), - reason: d.reason, - }) - } - fn apply_inner( - &mut self, - db: &mut Connection, - response: &SyncResponse, - clock: &impl SyncClock, - ) -> Result { - let now = clock.now()?; - let prepared = self.pending.as_ref().context("NO_CURRENT_SYNC_CHALLENGE")?; - let channel = self.channel.as_ref().context("NO_AUTHENTICATED_CHANNEL")?; - let decision = verify_response(&self.binding, &prepared.request, channel, response, now)?; - let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; - let meta = metadata(&tx)?; - ensure!( - meta.fault.is_none() - && meta.pending_request == Some(decision.request_id) - && meta.checkpoint == i64::try_from(decision.checkpoint)? - && meta.generation == channel.connection_generation, - "SYNC_CHECKPOINT_CHANGED" - ); - let seq: i64 = tx.query_row("SELECT seq FROM counter WHERE id=1", [], |r| r.get(0))?; - ensure!( - seq >= meta.high_sequence && seq >= meta.checkpoint, - "LOCAL_SEQUENCE_ROLLBACK" - ); - let current = tx.query_row( - "SELECT request FROM reconcile_requests WHERE id=?1", - [decision.request_id.to_string()], - |r| r.get::<_, Vec>(0), - )?; - ensure!( - current == prepared.request.encode_to_vec(), - "DURABLE_CHALLENGE_CHANGED" - ); - let summaries: BTreeMap<_, _> = prepared - .request - .attempts - .iter() - .map(|a| ((a.job_id.clone(), a.attempt_id.clone()), a)) - .collect(); - let actual: BTreeSet<_> = decision - .attempts - .iter() - .map(|a| (a.job_id.to_string(), a.attempt_id.to_string())) - .collect(); - ensure!( - actual.len() == decision.attempts.len() - && actual == summaries.keys().cloned().collect(), - "SYNC_ATTEMPT_OMITTED_OR_ADDED" - ); - let mut safe = true; - let mut fault = recovery_reason(&decision.reason).then(|| decision.reason.clone()); - for d in &decision.attempts { - let local = summaries - .get(&(d.job_id.to_string(), d.attempt_id.to_string())) - .context("LOCAL_ATTEMPT_MISSING")?; - if recovery_reason(&d.reason) { - fault = Some(d.reason.clone()); - } - let offer_bytes: Vec = tx.query_row( - "SELECT offer FROM job_attempts WHERE job=?1 AND attempt=?2", - params![local.job_id, local.attempt_id], - |r| r.get(0), - )?; - let offer = JobOffer::decode(offer_bytes.as_slice())?; - let job = self.binding.job(&offer.payload_bytes)?; - // Missing-server decisions intentionally have empty action fields; - // persist their sticky denial without treating it as start authority. - if recovery_reason(&d.reason) { - safe = false; - continue; - } - let outcome = validate_attempt(&job, &offer, local, d, channel.boot_id)?; - safe &= (d.outcome_state == "accepted" - && local.consumption == "unconsumed" - && local.grant_id.is_empty()) - || (terminal(&d.outcome_state) && (local.consumption == "unconsumed" || d.unlock)); - let (stored_state, revision): (String, u64) = tx.query_row( - "SELECT state,revision FROM job_attempts WHERE job=?1 AND attempt=?2", - params![local.job_id, local.attempt_id], - |r| Ok((r.get(0)?, r.get(1)?)), - )?; - ensure!( - revision == local.revision && stored_state == local.state, - "LOCAL_ATTEMPT_CHANGED_DURING_SYNC" - ); - let stored = if d.outcome_state == "unknown" && local.state == "awaiting_reboot" { - "awaiting_reboot" - } else { - d.outcome_state.as_str() - }; - tx.execute( - "UPDATE job_attempts SET state=?3,revision=?4 WHERE job=?1 AND attempt=?2", - params![ - local.job_id, - local.attempt_id, - stored, - d.revision.max(local.revision) - ], - )?; - if let Some(bytes) = outcome { - tx.execute("INSERT INTO reconcile_outcomes(job,attempt,outcome,decision_id) VALUES(?1,?2,?3,?4) ON CONFLICT(job,attempt) DO UPDATE SET outcome=excluded.outcome,decision_id=excluded.decision_id",params![local.job_id,local.attempt_id,bytes,decision.decision_id.to_string()])?; - } - } - let requested: BTreeSet<_> = prepared - .request - .durable_message_ids - .iter() - .map(String::as_str) - .collect(); - let mut receipts = BTreeSet::new(); - for id in &decision.acknowledged_message_ids { - ensure!( - receipts.insert(*id) && requested.contains(id.to_string().as_str()), - "ACK_NOT_IN_REQUEST" - ); - archive_events(&tx)?; - tx.execute("DELETE FROM job_events WHERE id=?1", [id.to_string()])?; - tx.execute( - "INSERT OR IGNORE INTO reconcile_receipts(id,decision_id) VALUES(?1,?2)", - params![id.to_string(), decision.decision_id.to_string()], - )?; - } - tx.execute( - "UPDATE reconcile_requests SET response=?2 WHERE id=?1 AND response IS NULL", - params![decision.request_id.to_string(), response.encode_to_vec()], - )?; - tx.execute("UPDATE reconcile_meta SET last_decision=?1,pending_request=NULL,helper_id=?2,helper_checkpoint=?3,fault=COALESCE(fault,?4),high_sequence=?5 WHERE id=1", - params![decision.decision_id.to_string(),decision.helper_journal_id.map(|id|id.to_string()),decision.helper_checkpoint,fault,seq])?; - tx.commit()?; - let helper_instruction = decision - .helper_journal_id - .map(|journal_id| HelperInstruction { - response: response.clone(), - decision_id: decision.decision_id, - journal_id, - checkpoint: decision.helper_checkpoint, - }); - self.gate = Some(Gate { - channel: channel.clone(), - until: decision.valid_until, - ready: decision.ready && safe && fault.is_none(), - helper_ready: decision.helper_ready, - helper: helper_instruction - .as_ref() - .map(|h| (h.journal_id, h.checkpoint, h.decision_id)), - local_applied: !decision.attempts.iter().any(|d| d.unlock), - request: prepared.request.clone(), - response: response.clone(), - }); - self.pending = None; - Ok(Applied { - decision_id: decision.decision_id, - acknowledged_message_ids: decision.acknowledged_message_ids, - helper_instruction, - reason: decision.reason, - }) - } - /// Apply ONLY this process's independently owned consumption ledger. Root - /// helper unlocks use HelperInstruction over authenticated IPC instead. - /// A crash between journal and ledger transactions leaves the gate closed; - /// resync/reapply never deletes the consumed attempt or repeats an effect. - pub fn apply_consumption( - &mut self, - ledger: &mut Connection, - clock: &impl SyncClock, - ) -> Result<()> { - let gate = self.gate.as_mut().context("NO_SIGNED_DECISION")?; - gate.local_applied = false; - let decision = verify_response( - &self.binding, - &gate.request, - &gate.channel, - &gate.response, - clock.now()?, - )?; - let tx = ledger.transaction_with_behavior(TransactionBehavior::Immediate)?; - tx.execute_batch("CREATE TABLE IF NOT EXISTS reconcile_consumption(decision_id TEXT PRIMARY KEY,response BLOB NOT NULL);")?; - let applied: Option> = tx - .query_row( - "SELECT response FROM reconcile_consumption WHERE decision_id=?1", - [decision.decision_id.to_string()], - |r| r.get(0), - ) - .optional()?; - if let Some(old) = applied { - ensure!( - old == gate.response.encode_to_vec(), - "CONSUMPTION_DECISION_CONFLICT" - ); - } else { - for d in decision.attempts.iter().filter(|d| d.unlock) { - let grant = d.grant_id.context("UNLOCK_GRANT_MISSING")?; - let (agent,generation,job,attempt,previous):(String,u64,String,String,Option>)=tx.query_row( - "SELECT agent,generation,job,attempt,COALESCE((SELECT outcome FROM execution_evidence WHERE execution_evidence.grant_id=consumed.grant_id),result) FROM consumed WHERE grant_id=?1", - [grant.to_string()],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?)))?; - ensure!( - agent == self.binding.agent_id.to_string() - && generation == self.binding.enrollment_generation - && job == d.job_id.to_string() - && attempt == d.attempt_id.to_string(), - "LOCAL_UNLOCK_CONSUMPTION_BINDING" - ); - let previous = previous.context("LOCAL_UNLOCK_PREVIOUS_OUTCOME_MISSING")?; - let prior = canonical_outcome_bytes(&serde_json::from_slice::( - &previous, - )?)?; - ensure!( - d.previous_outcome_sha256.as_deref() == Some(hash(&prior).as_str()), - "LOCAL_UNLOCK_PREVIOUS_OUTCOME_MISMATCH" - ); - let bytes = if d.outcome_bytes.is_empty() { - prior - } else { - d.outcome_bytes.clone() - }; - let outcome: ReconciledOutcome = serde_json::from_slice(&bytes)?; - ensure!( - terminal(&outcome.state) - && outcome.state == d.outcome_state - && canonical_outcome_bytes(&outcome)? == bytes - && d.outcome_sha256.as_deref() == Some(hash(&bytes).as_str()), - "LOCAL_UNLOCK_CANONICAL_OUTCOME" - ); - tx.execute( - "UPDATE consumed SET result=?2 WHERE grant_id=?1", - params![grant.to_string(), bytes], - )?; - tx.execute( - "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", - params![grant.to_string(), bytes], - )?; - } - tx.execute( - "INSERT INTO reconcile_consumption(decision_id,response) VALUES(?1,?2)", - params![ - decision.decision_id.to_string(), - gate.response.encode_to_vec() - ], - )?; - } - tx.commit()?; - gate.local_applied = true; - Ok(()) - } -} - -pub fn initialize(db: &Connection) -> Result<()> { - let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?; - ensure!(check == "ok", "JOURNAL_INTEGRITY"); - db.execute_batch("PRAGMA synchronous=FULL; - CREATE TABLE IF NOT EXISTS reconcile_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,high_sequence INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,last_decision TEXT,pending_request TEXT,helper_id TEXT,helper_checkpoint INTEGER NOT NULL DEFAULT 0,fault TEXT); - CREATE TABLE IF NOT EXISTS reconcile_requests(id TEXT PRIMARY KEY,request BLOB NOT NULL,envelope BLOB NOT NULL,generation INTEGER NOT NULL,response BLOB); - CREATE TABLE IF NOT EXISTS reconcile_results(id TEXT PRIMARY KEY,job TEXT NOT NULL,attempt TEXT NOT NULL,revision INTEGER NOT NULL,envelope BLOB NOT NULL); - CREATE TABLE IF NOT EXISTS reconcile_receipts(id TEXT PRIMARY KEY,decision_id TEXT NOT NULL); - CREATE TABLE IF NOT EXISTS reconcile_outcomes(job TEXT NOT NULL,attempt TEXT NOT NULL,outcome BLOB NOT NULL,decision_id TEXT NOT NULL,PRIMARY KEY(job,attempt));")?; - db.execute( - "INSERT OR IGNORE INTO reconcile_meta(id,journal_id) VALUES(1,?1)", - [Uuid::new_v4().to_string()], - )?; - Ok(()) -} -struct Meta { - journal_id: Uuid, - checkpoint: i64, - high_sequence: i64, - generation: u64, - decision: Option, - pending_request: Option, - helper_id: Option, - helper_checkpoint: u64, - fault: Option, -} -type MetadataRow = ( - String, - i64, - i64, - u64, - Option, - Option, - Option, - u64, - Option, -); -fn metadata(db: &Connection) -> Result { - let row:MetadataRow=db.query_row("SELECT journal_id,checkpoint,high_sequence,generation,last_decision,pending_request,helper_id,helper_checkpoint,fault FROM reconcile_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)?,r.get(7)?,r.get(8)?)))?; - Ok(Meta { - journal_id: Uuid::parse_str(&row.0)?, - checkpoint: row.1, - high_sequence: row.2, - generation: row.3, - decision: row.4.map(|s| Uuid::parse_str(&s)).transpose()?, - pending_request: row.5.map(|s| Uuid::parse_str(&s)).transpose()?, - helper_id: row.6.map(|s| Uuid::parse_str(&s)).transpose()?, - helper_checkpoint: row.7, - fault: row.8, - }) -} -/// Call inside the same transaction BEFORE deleting an ACKed job event. This -/// retains the original result identity/output for future reconciliation. -pub fn archive_events(db: &Connection) -> Result<()> { - let mut statement = db.prepare("SELECT id,payload FROM job_events ORDER BY rowid")?; - for row in statement.query_map([], |r| { - Ok((r.get::<_, String>(0)?, r.get::<_, Vec>(1)?)) - })? { - let (id, bytes) = row?; - let envelope = Envelope::decode(bytes.as_slice())?; - ensure!(id == envelope.message_id, "EVENT_IDENTITY_CONFLICT"); - if let Some(Payload::JobResult(r)) = &envelope.payload { - let existing: Option> = db - .query_row( - "SELECT envelope FROM reconcile_results WHERE id=?1", - [&id], - |r| r.get(0), - ) - .optional()?; - ensure!( - existing.as_ref().is_none_or(|b| *b == bytes), - "RESULT_IDENTITY_CONFLICT" - ); - db.execute("INSERT OR IGNORE INTO reconcile_results(id,job,attempt,revision,envelope) VALUES(?1,?2,?3,?4,?5)",params![id,r.job_id,r.attempt_id,r.revision,bytes])?; - } - } - Ok(()) -} -fn pending_ids(db: &Connection) -> Result> { - let mut statement = db.prepare("SELECT id FROM job_events ORDER BY id")?; - let ids = statement - .query_map([], |r| r.get::<_, String>(0))? - .collect::>>()?; - for id in &ids { - ensure!(!Uuid::parse_str(id)?.is_nil(), "INVALID_DURABLE_MESSAGE_ID"); - } - Ok(ids) -} -fn outcome(r: &JobResult) -> Result { - Ok(ReconciledOutcome { - state: r.state.clone(), - stdout: r.stdout.clone(), - stderr: r.stderr.clone(), - exit_code: r.exit_code, - output_truncated: r.output_truncated, - reboot_required: r.reboot_required, - evidence: serde_json::from_str(&r.evidence_json)?, - error_code: r.error_code.clone(), - }) -} -fn terminal(state: &str) -> bool { - matches!(state, "succeeded" | "failed" | "cancelled" | "expired") -} -fn optional_uuid(s: &str) -> Result> { - if s.is_empty() { - Ok(None) - } else { - let id = Uuid::parse_str(s)?; - ensure!(!id.is_nil(), "NIL_ID"); - Ok(Some(id)) - } -} -fn recovery_reason(reason: &str) -> bool { - matches!( - reason, - "MISSING_SERVER_JOB" - | "ATTEMPT_BINDING_MISMATCH" - | "MANUAL_SERVER_LEDGER_RECOVERY_REQUIRED" - | "SERVER_STATE_ROLLBACK" - | "ASSIGNMENT_MISMATCH" - | "JOURNAL_SEQUENCE_REGRESSION" - | "SERVER_CHECKPOINT_MISSING" - | "JOURNAL_ROLLBACK_OR_REPLACEMENT" - | "CHECKPOINT_CHAIN_MISMATCH" - | "HELPER_JOURNAL_ROLLBACK" - | "HELPER_CHECKPOINT_MISMATCH" - | "HELPER_CHECKPOINT_MISSING" - | "HELPER_CHECKPOINT_REQUIRED" - | "JOURNAL_HISTORY_MISSING" - | "LOCAL_ATTEMPT_MISSING" - ) -} - -fn attempts(db: &Connection, ledger: &Connection, binding: &Binding) -> Result> { - let mut statement = - db.prepare("SELECT offer,state,revision,grant_id FROM job_attempts ORDER BY job,attempt")?; - let rows = statement.query_map([], |r| { - Ok(( - r.get::<_, Vec>(0)?, - r.get::<_, String>(1)?, - r.get::<_, u64>(2)?, - r.get::<_, String>(3)?, - )) - })?; - let mut summaries = Vec::new(); - let mut known = BTreeSet::new(); - for row in rows { - let (bytes, state, revision, mut grant_id) = row?; - let offer = JobOffer::decode(bytes.as_slice())?; - let job = binding.job(&offer.payload_bytes)?; - ensure!( - offer.job_id == job.job_id.to_string() - && offer.attempt_id == job.attempt_id.to_string() - && offer.job_payload_hash == hash(&offer.payload_bytes), - "LOCAL_OFFER_BINDING" - ); - known.insert((offer.job_id.clone(), offer.attempt_id.clone())); - let consumed: Option<(String, String, u64, Option>)> = ledger - .query_row( - "SELECT grant_id,agent,generation,result FROM consumed WHERE job=?1 AND attempt=?2", - params![offer.job_id, offer.attempt_id], - |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)), - ) - .optional()?; - // A signed definitive nonstart can make an accepted no-grant row - // terminal without a JobResult. Preserve that fact on the next Sync; - // requesting/running/unknown still cannot claim non-consumption. An - // actual durable consumption row below always overrides this label. - let mut consumption = if grant_id.is_empty() - && matches!(state.as_str(), "accepted" | "expired" | "cancelled") - { - "unconsumed" - } else { - "uncertain" - }; - let mut original_boot_id = String::new(); - let mut canonical = Vec::new(); - if let Some((id, agent, generation, result)) = consumed { - ensure!( - agent == binding.agent_id.to_string() - && generation == binding.enrollment_generation - && (grant_id.is_empty() || grant_id == id), - "CONSUMPTION_BINDING" - ); - grant_id = id; - consumption = "consumed"; - let evidence: Option<(Vec, Option>)> = ledger - .query_row( - "SELECT start,outcome FROM execution_evidence WHERE grant_id=?1", - [&grant_id], - |r| Ok((r.get(0)?, r.get(1)?)), - ) - .optional()?; - let mut stored = result; - if let Some((start, last)) = evidence { - let start: serde_json::Value = serde_json::from_slice(&start)?; - original_boot_id = start["previous_boot_id"] - .as_str() - .context("ORIGINAL_BOOT_MISSING")? - .to_string(); - optional_uuid(&original_boot_id)?; - let request: rmm_privileged_helper::Request = - serde_json::from_value(start["request"].clone())?; - ensure!( - request.job_bytes == offer.payload_bytes - && request.plan_hash == offer.plan_hash - && request.authorization_id.to_string() == offer.authorization_id, - "START_EVIDENCE_BINDING" - ); - if last.is_some() { - stored = last; - } - } - if let Some(stored) = stored { - let o: ReconciledOutcome = serde_json::from_slice(&stored)?; - canonical = canonical_outcome_bytes(&o)?; - } - } - let original:Option>=db.query_row("SELECT envelope FROM reconcile_results WHERE job=?1 AND attempt=?2 ORDER BY revision DESC LIMIT 1",params![offer.job_id,offer.attempt_id],|r|r.get(0)).optional()?; - let mut result_envelope = None; - let mut result = None; - let mut result_message_id = String::new(); - if let Some(bytes) = original { - let envelope = Envelope::decode(bytes.as_slice())?; - let Some(Payload::JobResult(r)) = &envelope.payload else { - anyhow::bail!("INVALID_ARCHIVED_RESULT") - }; - if r.revision == revision && r.state == state { - ensure!( - envelope.agent_id == binding.agent_id.to_string() - && envelope.enrollment_generation == binding.enrollment_generation - && r.grant_id == grant_id, - "RESULT_BINDING" - ); - let bytes = canonical_outcome_bytes(&outcome(r)?)?; - ensure!( - canonical.is_empty() || canonical == bytes, - "LOCAL_OUTCOME_LEDGER_MISMATCH" - ); - canonical = bytes; - result_message_id = envelope.message_id.clone(); - result = Some(r.clone()); - result_envelope = Some(envelope); - } - } - if !canonical.is_empty() { - let o: ReconciledOutcome = serde_json::from_slice(&canonical)?; - ensure!(o.state == state, "LOCAL_OUTCOME_STATE_MISMATCH"); - ensure!(canonical.len() <= 512 * 1024, "SYNC_CANONICAL_OUTCOME_SIZE"); - if grant_id.is_empty() && terminal(&state) && o.evidence["not_started"] == true { - consumption = "unconsumed"; - } - } - summaries.push(SyncAttempt { - job_id: offer.job_id, - attempt_id: offer.attempt_id, - job_payload_hash: offer.job_payload_hash, - grant_id, - consumption: consumption.into(), - state, - revision, - original_boot_id, - result, - result_message_id, - outcome_sha256: if canonical.is_empty() { - String::new() - } else { - hash(&canonical) - }, - outcome_bytes: canonical, - result_envelope, - }); - } - let mut statement = ledger.prepare("SELECT job,attempt FROM consumed")?; - for row in statement.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))? { - ensure!( - known.contains(&row?), - "CONSUMED_ATTEMPT_MISSING_FROM_AGENT_JOURNAL" - ); - } - Ok(summaries) -} -fn ingest_helper( - db: &Connection, - ledger: &Connection, - binding: &Binding, - channel: &Channel, - helper: &HelperSnapshot, - now: DateTime, -) -> Result<()> { - let mut statement = db.prepare("SELECT job,attempt,offer FROM job_attempts")?; - for row in statement.query_map([], |r| { - Ok(( - r.get::<_, String>(0)?, - r.get::<_, String>(1)?, - r.get::<_, Vec>(2)?, - )) - })? { - let (job, attempt, bytes) = row?; - let offer = JobOffer::decode(bytes.as_slice())?; - if binding.job(&offer.payload_bytes)?.execution_identity == "root" { - let consumed: bool = ledger.query_row( - "SELECT EXISTS(SELECT 1 FROM consumed WHERE job=?1 AND attempt=?2)", - params![job, attempt], - |r| r.get(0), - )?; - ensure!( - !consumed - || helper - .attempts - .iter() - .any(|a| a.job_id == job && a.attempt_id == attempt), - "ROOT_CONSUMED_ATTEMPT_MISSING_FROM_HELPER_JOURNAL" - ); - } - } - let mut seen = BTreeSet::new(); - for h in &helper.attempts { - ensure!( - seen.insert((&h.job_id, &h.attempt_id)), - "DUPLICATE_HELPER_ATTEMPT" - ); - let row: Option<(Vec, u64, String)> = db - .query_row( - "SELECT offer,revision,grant_id FROM job_attempts WHERE job=?1 AND attempt=?2", - params![h.job_id, h.attempt_id], - |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)), - ) - .optional()?; - let (bytes, revision, grant) = row.context("HELPER_ATTEMPT_MISSING_FROM_AGENT_JOURNAL")?; - let offer = JobOffer::decode(bytes.as_slice())?; - let job = binding.job(&offer.payload_bytes)?; - ensure!( - job.execution_identity == "root" - && h.job_payload_hash == offer.job_payload_hash - && (grant.is_empty() || grant == h.grant_id), - "HELPER_ATTEMPT_BINDING" - ); - if h.outcome_bytes.is_empty() { - continue; - } - let o: ReconciledOutcome = serde_json::from_slice(&h.outcome_bytes)?; - ensure!( - canonical_outcome_bytes(&o)? == h.outcome_bytes - && hash(&h.outcome_bytes) == h.outcome_sha256 - && o.state == h.state - && (terminal(&o.state) - || matches!(o.state.as_str(), "unknown" | "awaiting_reboot")) - && h.consumption == "consumed" - && optional_uuid(&h.grant_id)?.is_some(), - "HELPER_CANONICAL_OUTCOME" - ); - let existing: Option> = ledger - .query_row( - "SELECT outcome FROM execution_evidence WHERE grant_id=?1", - [&h.grant_id], - |r| r.get(0), - ) - .optional()? - .flatten(); - let same = existing - .as_ref() - .map(|b| { - serde_json::from_slice::(b) - .and_then(|o| canonical_outcome_bytes(&o)) - }) - .transpose()? - .is_some_and(|b| b == h.outcome_bytes); - let state: String = db.query_row( - "SELECT state FROM job_attempts WHERE job=?1 AND attempt=?2", - params![h.job_id, h.attempt_id], - |r| r.get(0), - )?; - if same && state == h.state { - continue; - } - let consumed: Option<(String, u64, String, String)> = ledger - .query_row( - "SELECT agent,generation,job,attempt FROM consumed WHERE grant_id=?1", - [&h.grant_id], - |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)), - ) - .optional()?; - ensure!( - consumed - == Some(( - binding.agent_id.to_string(), - binding.enrollment_generation, - h.job_id.clone(), - h.attempt_id.clone() - )), - "HELPER_CONSUMPTION_NOT_KNOWN_LOCALLY" - ); - // Store actual helper evidence without unlocking result/consumption. A - // crash here still leaves both gates closed and the original helper - // result available; the next sync imports it again idempotently. - ensure!( - ledger.execute( - "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", - params![h.grant_id, h.outcome_bytes] - )? == 1, - "LOCAL_START_EVIDENCE_MISSING" - ); - let next_revision = revision - .max(h.revision) - .checked_add(1) - .context("REVISION_OVERFLOW")?; - i64::try_from(next_revision)?; - let seq: i64 = db.query_row( - "UPDATE counter SET seq=seq+1 WHERE id=1 RETURNING seq", - [], - |r| r.get(0), - )?; - let result = JobResult { - job_id: h.job_id.clone(), - attempt_id: h.attempt_id.clone(), - revision: next_revision, - state: o.state.clone(), - grant_id: h.grant_id.clone(), - stdout: o.stdout, - stderr: o.stderr, - output_truncated: o.output_truncated, - reboot_required: o.reboot_required, - exit_code: o.exit_code, - evidence_json: o.evidence.to_string(), - error_code: o.error_code, - }; - let envelope = Envelope { - protocol_major: 1, - protocol_minor: 0, - message_id: Uuid::new_v4().to_string(), - agent_id: binding.agent_id.to_string(), - enrollment_generation: binding.enrollment_generation, - connection_generation: channel.connection_generation, - boot_id: channel.boot_id.to_string(), - sequence: u64::try_from(seq)?, - observed_at: now.timestamp_millis(), - payload: Some(Payload::JobResult(result)), - }; - ensure!( - envelope.encoded_len() <= rmm_domain::MAX_MESSAGE, - "HELPER_RESULT_SIZE" - ); - db.execute( - "INSERT INTO job_events(id,payload) VALUES(?1,?2)", - params![envelope.message_id, envelope.encode_to_vec()], - )?; - db.execute( - "UPDATE job_attempts SET state=?3,revision=?4,grant_id=?5 WHERE job=?1 AND attempt=?2", - params![h.job_id, h.attempt_id, o.state, next_revision, h.grant_id], - )?; - } - Ok(()) -} -fn merge_helper(local: &mut [SyncAttempt], helper: &HelperSnapshot) -> Result<()> { - let mut seen = BTreeSet::new(); - for h in &helper.attempts { - ensure!( - seen.insert((&h.job_id, &h.attempt_id)), - "DUPLICATE_HELPER_ATTEMPT" - ); - let a = local - .iter_mut() - .find(|a| a.job_id == h.job_id && a.attempt_id == h.attempt_id) - .context("HELPER_ATTEMPT_MISSING_FROM_AGENT_JOURNAL")?; - ensure!( - a.job_payload_hash == h.job_payload_hash - && (a.grant_id.is_empty() || a.grant_id == h.grant_id), - "HELPER_ATTEMPT_BINDING" - ); - if h.consumption != "unconsumed" { - a.grant_id = h.grant_id.clone(); - a.consumption = h.consumption.clone(); - ensure!( - a.outcome_sha256 == h.outcome_sha256, - "HELPER_OUTCOME_MISMATCH" - ); - if a.original_boot_id.is_empty() { - a.original_boot_id = h.original_boot_id.clone(); - } - ensure!( - a.original_boot_id == h.original_boot_id, - "HELPER_ORIGINAL_BOOT_MISMATCH" - ); - } - } - Ok(()) -} -pub fn verify_response( - binding: &Binding, - request: &SyncRequest, - channel: &Channel, - response: &SyncResponse, - now: DateTime, -) -> Result { - ensure!( - response.decision_bytes.len() <= 512 * 1024 && response.signature.len() == 64, - "SYNC_SIGNATURE_SIZE" - ); - let raw = decode_key(&binding.public_key_hex)?; - 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.valid_profile(now) - && d.valid_until - d.issued_at <= chrono::Duration::seconds(60) - && d.key_id == hash(&raw), - "SYNC_PROFILE_OR_CLOCK" - ); - ensure!( - 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 - && d.connection_generation == channel.connection_generation - && d.boot_id == channel.boot_id - && d.request_id.to_string() == request.request_id - && d.request_hash == hash(&request.encode_to_vec()) - && d.journal_id.to_string() == request.journal_id - && d.checkpoint == request.checkpoint - && d.helper_journal_id == optional_uuid(&request.helper_journal_id)? - && d.helper_checkpoint == request.helper_checkpoint, - "SYNC_DECISION_BINDING" - ); - Ok(d) -} -fn decode_key(hex: &str) -> Result> { - ensure!(hex.len() == 64, "PINNED_KEY_SIZE"); - (0..32) - .map(|i| { - Ok(u8::from_str_radix( - hex.get(i * 2..i * 2 + 2).context("PINNED_KEY_ENCODING")?, - 16, - )?) - }) - .collect() -} -fn validate_attempt( - job: &JobPayload, - offer: &JobOffer, - local: &SyncAttempt, - d: &AttemptDecision, - boot: Uuid, -) -> Result>> { - ensure!( - d.job_payload_hash == offer.job_payload_hash - && d.grant_id == optional_uuid(&local.grant_id)? - && d.action == job.action.name() - && d.execution_identity == job.execution_identity - && (d.revision >= local.revision || d.outcome_state == "unknown") - && d.original_boot_id == optional_uuid(&local.original_boot_id)? - && d.previous_outcome_sha256.as_deref() - == (!local.outcome_sha256.is_empty()).then_some(local.outcome_sha256.as_str()), - "SYNC_ATTEMPT_BINDING" - ); - if d.grant_id.is_some() { - ensure!( - d.authorization_id == Some(Uuid::parse_str(&offer.authorization_id)?) - && d.plan_hash.as_deref() == Some(offer.plan_hash.as_str()), - "SYNC_AUTHORIZATION_BINDING" - ); - } - ensure!( - !terminal(&local.state) || d.outcome_state == local.state, - "TERMINAL_STATE_REGRESSION" - ); - if d.outcome_state == "accepted" { - ensure!( - local.state == "accepted" - && local.consumption == "unconsumed" - && d.grant_id.is_none() - && !d.unlock - && d.outcome_bytes.is_empty(), - "SYNC_CANNOT_RESTART_ATTEMPT" - ); - return Ok(None); - } - if d.outcome_state == "unknown" { - ensure!(!d.unlock, "UNKNOWN_CANNOT_UNLOCK"); - return Ok(None); - } - ensure!(terminal(&d.outcome_state), "UNSUPPORTED_SYNC_STATE"); - if d.outcome_bytes.is_empty() && d.outcome_sha256.is_none() { - ensure!( - local.consumption == "unconsumed" - && d.grant_id.is_none() - && !d.unlock - && d.outcome_state != "succeeded", - "TERMINAL_WITHOUT_EVIDENCE" - ); - return Ok(None); - } - let bytes = if d.outcome_bytes.is_empty() { - local.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, - "SYNC_CANONICAL_OUTCOME" - ); - if bytes != local.outcome_bytes { - ensure!( - matches!(job.action, rmm_domain::jobs::Action::Reboot { .. }) - && d.outcome_state == "succeeded" - && d.original_boot_id.is_some_and(|id| id != boot) - && o.evidence["postconditions_passed"] == true - && o.evidence["reconciled_after_reboot"] == true - && o.evidence["new_boot_id"].as_str() == Some(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())), - "SYNC_REBOOT_REAUTH_EVIDENCE" - ); - } - ensure!( - !d.unlock || (local.consumption == "consumed" && d.grant_id.is_some()), - "UNLOCK_WITHOUT_CONSUMPTION" - ); - Ok(Some(bytes)) -} diff --git a/crates/domain/src/jobs.rs b/crates/domain/src/jobs.rs index ffb0280..741e0c8 100644 --- a/crates/domain/src/jobs.rs +++ b/crates/domain/src/jobs.rs @@ -100,7 +100,10 @@ impl Action { arguments, expected_stdout, } => { - matches!(interpreter.as_str(), "posix_sh" | "bash") + matches!(interpreter.as_str(), "posix_sh" | "bash" | "powershell") + && (interpreter != "powershell" + || script_utf8.is_ascii() + || script_utf8.starts_with('\u{feff}')) && !script_utf8.is_empty() && script_utf8.len() <= 32768 && !script_utf8.contains('\0') @@ -110,7 +113,9 @@ impl Action { .all(|a| a.len() <= 4096 && !a.contains('\0')) && expected_stdout.as_ref().is_none_or(|s| s.len() <= 65536) } - Self::RefreshInventory { scope } => scope == "linux.base", + Self::RefreshInventory { scope } => { + matches!(scope.as_str(), "linux.base" | "windows.base") + } Self::QueryLogs { unit: u, since, @@ -193,6 +198,45 @@ impl Action { } } } + /// Interpreter identity is part of the immutable action, never inferred at execution. + pub fn script_platform(&self) -> Option<&'static str> { + match self { + Self::RunScript { interpreter, .. } => match interpreter.as_str() { + "posix_sh" | "bash" => Some("linux"), + "powershell" => Some("windows"), + _ => None, + }, + _ => None, + } + } + /// Shared plan/revalidation/start gate. Passive Windows never implies job support. + pub fn available_for(&self, identity: &str, capabilities: &[String]) -> bool { + let has = |cap: &str| capabilities.iter().any(|c| c == cap); + if !has(self.name()) { + return false; + } + match self { + Self::RunScript { interpreter, .. } if interpreter == "powershell" => { + identity == "agent_service" + && has("windows.base/1") + && !has("linux.base/1") + && has("rmm.sync.v1") + && has("script.powershell/1") + } + Self::RunScript { .. } => { + self.script_platform() == Some("linux") + && has("linux.base/1") + && !has("windows.base/1") + } + Self::RefreshInventory { scope } => { + (identity == "agent_service" || scope == "linux.base") + && has(&format!("{scope}/1")) + && !(has("linux.base/1") && has("windows.base/1")) + && (scope != "windows.base" || has("rmm.sync.v1")) + } + _ => !has("windows.base/1"), // Windows adapters are separately qualified. + } + } pub fn resource(&self) -> &'static str { match self { Self::EnsurePackage { .. } @@ -387,6 +431,81 @@ impl JobState { mod tests { use super::*; #[test] + fn windows_interpreter_and_capability_contract() { + let mut action = Action::RunScript { + interpreter: "powershell".into(), + script_utf8: "Write-Output 'Hello'\r\n".into(), + arguments: vec![], + expected_stdout: Some("Hello\r\n".into()), + }; + let caps: Vec = [ + "windows.base/1", + "rmm.sync.v1", + "run_script", + "script.powershell/1", + ] + .into_iter() + .map(str::to_string) + .collect(); + assert!(action.validate()); + assert!(action.available_for("agent_service", &caps)); + for missing in 0..caps.len() { + let mut short = caps.clone(); + short.remove(missing); + assert!(!action.available_for("agent_service", &short)); + } + assert!(!action.available_for("root", &caps)); + assert!(!action.available_for("SYSTEM", &caps)); + assert!(!action.available_for( + "agent_service", + &["linux.base/1".into(), "run_script".into()] + )); + let mut both = caps.clone(); + both.push("linux.base/1".into()); + assert!(!action.available_for("agent_service", &both)); + if let Action::RunScript { + interpreter, + script_utf8, + .. + } = &mut action + { + *script_utf8 = "Write-Output 'é'".into(); + assert!(interpreter == "powershell"); + } + assert!(!action.validate()); + if let Action::RunScript { script_utf8, .. } = &mut action { + *script_utf8 = "\u{feff}Write-Output 'é'\r\n".into(); + } + assert!(action.validate()); + let bytes = encode(&action).unwrap(); + assert_eq!(serde_json::from_slice::(&bytes).unwrap(), action); + if let Action::RunScript { interpreter, .. } = &mut action { + *interpreter = "pwsh".into(); + } + assert!(!action.validate()); + assert!(!action.available_for("agent_service", &caps)); + let refresh = Action::RefreshInventory { + scope: "windows.base".into(), + }; + assert!(refresh.validate()); + assert!(refresh.available_for( + "agent_service", + &[ + "windows.base/1".into(), + "rmm.sync.v1".into(), + "refresh_inventory".into() + ] + )); + assert!(!refresh.available_for( + "root", + &[ + "windows.base/1".into(), + "rmm.sync.v1".into(), + "refresh_inventory".into() + ] + )); + } + #[test] fn codec_golden_and_exact_script_bytes() { let a = Action::RunScript { interpreter: "posix_sh".into(), diff --git a/crates/privileged-helper/Cargo.toml b/crates/privileged-helper/Cargo.toml index bcf9664..5d8291b 100644 --- a/crates/privileged-helper/Cargo.toml +++ b/crates/privileged-helper/Cargo.toml @@ -5,6 +5,7 @@ edition.workspace = true license.workspace = true [dependencies] +rmm-agent-core = { path = "../agent-core", features = ["openssl-verifier"] } anyhow.workspace = true chrono.workspace = true hex.workspace = true diff --git a/crates/privileged-helper/src/lib.rs b/crates/privileged-helper/src/lib.rs index 88d6a92..d6fddd0 100644 --- a/crates/privileged-helper/src/lib.rs +++ b/crates/privileged-helper/src/lib.rs @@ -11,10 +11,7 @@ pub mod reconcile; mod tests; use anyhow::{Context, Result, ensure}; use chrono::{DateTime, Utc}; -use openssl::{ - pkey::{Id, PKey}, - sign::Verifier, -}; +use openssl::pkey::{Id, PKey}; use rmm_domain::jobs::{ExecutionPermitPayload, JobPayload, hash}; use serde::{Deserialize, Serialize}; use std::{fs, os::unix::fs::MetadataExt, path::Path}; @@ -50,130 +47,7 @@ impl SigningKey { } } -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct Binding { - pub tenant_id: Uuid, - pub asset_id: Uuid, - pub agent_id: Uuid, - pub enrollment_generation: u64, - pub assignment_version: u64, - pub public_key_hex: String, -} -impl Binding { - pub fn job(&self, bytes: &[u8]) -> Result { - ensure!(bytes.len() <= 65536, "JOB_SIZE"); - let j: JobPayload = serde_json::from_slice(bytes)?; - ensure!( - j.schema_version == 1 - && j.tenant_id == self.tenant_id - && j.asset_id == self.asset_id - && j.agent_id == self.agent_id - && j.enrollment_generation == self.enrollment_generation - && j.assignment_version == self.assignment_version, - "IDENTITY_BINDING" - ); - ensure!( - j.action.validate() - && (1..=3600).contains(&j.max_runtime_seconds) - && (1..=262144).contains(&j.max_output_bytes) - && j.not_before < j.start_before, - "INVALID_JOB" - ); - Ok(j) - } - pub fn verify( - &self, - request: &Request, - now: DateTime, - ) -> Result<(JobPayload, ExecutionPermitPayload)> { - let job = self.job(&request.job_bytes)?; - ensure!( - request.permit_bytes.len() <= 8192 && request.signature.len() == 64, - "PERMIT_SIZE" - ); - let raw = hex::decode(&self.public_key_hex)?; - ensure!(raw.len() == 32, "TRUST_KEY_SIZE"); - let key = PKey::public_key_from_raw_bytes(&raw, Id::ED25519)?; - ensure!( - Verifier::new_without_digest(&key)? - .verify_oneshot(&request.signature, &request.permit_bytes)?, - "INVALID_SIGNATURE" - ); - let p: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes)?; - ensure!( - p.domain == "rmm.execution-permit" - && p.schema_version == 1 - && p.key_id == hash(&raw) - && !p.grant_id.is_nil() - && !p.authorization_id.is_nil(), - "PERMIT_DOMAIN" - ); - ensure!( - p.authorization_id == request.authorization_id - && p.plan_hash == request.plan_hash - && p.plan_hash.len() == 64 - && hex::decode(&p.plan_hash)?.len() == 32 - && p.job_payload_hash == hash(&request.job_bytes) - && p.tenant_id == job.tenant_id - && p.asset_id == job.asset_id - && p.agent_id == job.agent_id - && p.enrollment_generation == job.enrollment_generation - && p.assignment_version == job.assignment_version - && p.job_id == job.job_id - && p.attempt_id == job.attempt_id - && p.action == job.action.name() - && p.execution_identity == job.execution_identity - && p.max_runtime_seconds == job.max_runtime_seconds, - "PERMIT_BINDING" - ); - ensure!( - p.not_before >= job.not_before - && p.start_before <= job.start_before - && p.not_before < p.start_before - && (p.start_before - p.not_before).num_seconds() <= 60 - && now >= p.not_before - && now < p.start_before, - "PERMIT_TIME" - ); - Ok((job, p)) - } -} - -#[derive(Clone, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct Request { - pub job_bytes: Vec, - pub permit_bytes: Vec, - pub signature: Vec, - pub plan_hash: String, - pub authorization_id: Uuid, -} -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Outcome { - pub state: String, - pub stdout: Vec, - pub stderr: Vec, - pub exit_code: Option, - pub output_truncated: bool, - pub reboot_required: bool, - pub evidence: serde_json::Value, - pub error_code: String, -} -impl Outcome { - pub fn unknown(code: &str) -> Self { - Self { - 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: code.into(), - } - } -} +pub use rmm_agent_core::auth::{Binding, Outcome, Request}; /// Config and journal directories must already be bootstrapped by the intended /// owner; do not silently chmod a hostile existing path or follow symlinks. @@ -204,16 +78,7 @@ pub fn lock_state(dir: &Path, uid: u32) -> Result { f.try_lock_exclusive()?; Ok(f) } -/// Finite 1 GiB bound for durable agent/helper history. Unexpected page sizes -/// are refused rather than silently multiplying the byte limit. No compaction -/// or tombstone deletion occurs here; SQLITE_FULL must still fail closed. -pub fn bounded_journal_capacity(db: &rusqlite::Connection) -> Result<()> { - let page_size: u32 = db.query_row("PRAGMA page_size", [], |r| r.get(0))?; - ensure!(page_size == 4096, "JOURNAL_PAGE_SIZE_UNSUPPORTED"); - let maximum: u32 = db.query_row("PRAGMA max_page_count=262144", [], |r| r.get(0))?; - ensure!(maximum == 262144, "JOURNAL_STORAGE_CAPACITY_BLOCKED"); - Ok(()) -} +pub use rmm_agent_core::ledger::{bounded_journal_capacity, consume, finish}; pub fn open_ledger(dir: &Path, uid: u32) -> Result { use std::os::unix::fs::OpenOptionsExt; @@ -229,85 +94,23 @@ pub fn open_ledger(dir: &Path, uid: u32) -> Result { f.sync_all()?; private_path(&path, uid, false)?; let db = rusqlite::Connection::open(path)?; - bounded_journal_capacity(&db)?; - db.execute_batch("PRAGMA synchronous=FULL; PRAGMA journal_mode=DELETE; - CREATE TABLE IF NOT EXISTS consumed(grant_id TEXT PRIMARY KEY, agent TEXT NOT NULL, generation INTEGER NOT NULL, - job TEXT NOT NULL, attempt TEXT NOT NULL, result BLOB, UNIQUE(agent,generation,job,attempt)); - CREATE TABLE IF NOT EXISTS execution_evidence(grant_id TEXT PRIMARY KEY, start BLOB NOT NULL, outcome BLOB);")?; - let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?; - ensure!(check == "ok", "JOURNAL_INTEGRITY"); + rmm_agent_core::ledger::initialize_ledger(&db)?; fs::File::open(dir)?.sync_all()?; Ok(db) } -pub fn consume(db: &mut rusqlite::Connection, p: &ExecutionPermitPayload) -> Result<()> { - let tx = db.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?; - // Unknown effects retain the global device lock, including across restart. - let unresolved: i64 = tx.query_row( - "SELECT count(*) FROM consumed WHERE result IS NULL", - [], - |r| r.get(0), - )?; - ensure!(unresolved == 0, "UNRESOLVED_EFFECT_RESOURCE_LOCKED"); - tx.execute( - "INSERT INTO consumed(grant_id,agent,generation,job,attempt) VALUES(?1,?2,?3,?4,?5)", - rusqlite::params![ - p.grant_id.to_string(), - p.agent_id.to_string(), - p.enrollment_generation, - p.job_id.to_string(), - p.attempt_id.to_string() - ], - )?; - tx.commit()?; - Ok(()) -} -pub fn finish( - db: &rusqlite::Connection, - p: &ExecutionPermitPayload, - outcome: &Outcome, -) -> Result<()> { - db.execute( - "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", - rusqlite::params![p.grant_id.to_string(), serde_json::to_vec(outcome)?], - )?; - // Unknown / awaiting reboot keep the resource reserved until a real sync - // protocol exists. Never unlock on a process exit or a network reconnect. - if outcome.state != "unknown" && outcome.state != "awaiting_reboot" { - ensure!( - db.execute( - "UPDATE consumed SET result=?2 WHERE grant_id=?1 AND result IS NULL", - rusqlite::params![p.grant_id.to_string(), serde_json::to_vec(outcome)?] - )? == 1, - "CONSUMPTION_NOT_FOUND" - ); - } - Ok(()) -} - -/// Persist the original boot and exact signed request BEFORE any reboot/effect. -/// Awaiting/unknown outcomes never release consumed.result's resource lock. +/// Persist the same exact request/boot/time evidence through the shared ledger. pub fn save_start( db: &rusqlite::Connection, permit: &ExecutionPermitPayload, request: &Request, ) -> Result<()> { - let job: JobPayload = serde_json::from_slice(&request.job_bytes)?; - let recorded_at = Utc::now(); - let deadline = match job.action { - rmm_domain::jobs::Action::Reboot { - reconnect_timeout_seconds, - .. - } => Some(recorded_at + chrono::Duration::seconds(reconnect_timeout_seconds.into())), - _ => None, - }; - let evidence = serde_json::json!({"previous_boot_id":rmm_agent_linux::boot_id()?, - "reserved_at":recorded_at,"reconnect_deadline":deadline,"request":request, - "reauthentication_required":true,"postconditions_passed":false}); - db.execute( - "INSERT INTO execution_evidence(grant_id,start) VALUES(?1,?2)", - rusqlite::params![permit.grant_id.to_string(), serde_json::to_vec(&evidence)?], - )?; - Ok(()) + rmm_agent_core::ledger::save_start( + db, + permit, + request, + Uuid::parse_str(&rmm_agent_linux::boot_id()?)?, + Utc::now(), + ) } pub fn script(job: &JobPayload) -> Result { diff --git a/crates/server/src/job_gateway.rs b/crates/server/src/job_gateway.rs index 4ed5f10..1176e8c 100644 --- a/crates/server/src/job_gateway.rs +++ b/crates/server/src/job_gateway.rs @@ -262,11 +262,11 @@ pub async fn permit( }; let payload: JobPayload = serde_json::from_slice(&row.get::, _>("payload"))?; let plan: PlanPayload = serde_json::from_slice(&row.get::, _>("plan_payload"))?; - let capabilities: Value = row.get("capabilities"); + let capabilities: Vec = serde_json::from_value(row.get("capabilities"))?; if row.get::("bound_asset") != payload.asset_id - || !capabilities - .as_array() - .is_some_and(|c| c.iter().any(|v| v.as_str() == Some(payload.action.name()))) + || !payload + .action + .available_for(&payload.execution_identity, &capabilities) || hash(&row.get::, _>("plan_payload")) != row.get::("plan_hash") || !plan.jobs.iter().any(|j| { j.payload.job_id == payload.job_id @@ -491,11 +491,20 @@ pub async fn next( if let Some(r)=sqlx::query("SELECT job_id,attempt_id FROM jobs WHERE agent_id=$1 AND cancel_requested AND state IN ('accepted','running','verifying','awaiting_reboot') ORDER BY job_id LIMIT 1").bind(agent).fetch_optional(&mut *tx).await?{ tx.commit().await?;return Ok(Some(Payload::CancelRequest(CancelRequest{job_id:r.get::("job_id").to_string(),attempt_id:r.get::("attempt_id").to_string(),reason:"operator_cancel_requested".into()}))) } - let row=sqlx::query("SELECT j.job_id,j.attempt_id,j.payload,j.payload_hash,a.authorization_id,p.plan_hash FROM outbox b JOIN jobs j USING(tenant_id,job_id,attempt_id) JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id JOIN authorizations a ON a.tenant_id=o.tenant_id AND a.authorization_id=o.authorization_id JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id JOIN operation_plans p ON p.tenant_id=j.tenant_id AND p.plan_id=r.plan_id JOIN tenants t ON t.tenant_id=j.tenant_id WHERE j.agent_id=$1 AND j.state='queued' AND NOT j.cancel_requested AND NOT t.changes_stopped AND a.revoked_at IS NULL AND r.approval_state='approved' AND p.expires_at>now() AND b.acknowledged_at IS NULL AND (b.lease_until IS NULL OR b.lease_untilnow() AND b.acknowledged_at IS NULL AND (b.lease_until IS NULL OR b.lease_until, _>("payload"))?; + let caps: Vec = serde_json::from_value(row.get("capabilities"))?; + if !payload + .action + .available_for(&payload.execution_identity, &caps) + { + tx.commit().await?; + return Ok(None); + } sqlx::query("UPDATE outbox SET lease_until=now()+interval '30 seconds' WHERE job_id=$1") .bind(row.get::("job_id")) .execute(&mut *tx) diff --git a/crates/server/src/jobs.rs b/crates/server/src/jobs.rs index aa08fb9..25d9037 100644 --- a/crates/server/src/jobs.rs +++ b/crates/server/src/jobs.rs @@ -107,6 +107,8 @@ pub struct Rollout { pub struct PlanInput { pub targets: Targets, pub action: Action, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub script_variants: Vec, pub execution_identity: String, pub schedule: Schedule, pub max_runtime_seconds: u32, @@ -144,7 +146,7 @@ struct WebSchedule { } #[derive(Clone, Deserialize, Serialize, schemars::JsonSchema)] #[serde(deny_unknown_fields)] -struct Variant { +pub struct Variant { os_family: String, interpreter: String, encoding: String, @@ -200,21 +202,17 @@ impl PlanRequest { |value: Option| value.ok_or(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR")); let action = match w.action.as_str() { "run_script" => { - let mut variants = p + let variants = p .variants + .as_ref() .ok_or(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"))?; - if variants.len() != 1 { - return Err(fail("AMBIGUOUS_TARGET")); - } - let v = variants.remove(0); - if v.os_family != "linux" || v.encoding != "utf-8" { - return Err(fail("UNSUPPORTED_CAPABILITY")); - } + validate_variants(variants)?; + let v = &variants[0]; Action::RunScript { - interpreter: v.interpreter, - script_utf8: v.script_utf8, - arguments: p.arguments.unwrap_or_default(), - expected_stdout: p.expected_stdout, + interpreter: v.interpreter.clone(), + script_utf8: v.script_utf8.clone(), + arguments: p.arguments.clone().unwrap_or_default(), + expected_stdout: p.expected_stdout.clone(), } } "refresh_inventory" => Action::RefreshInventory { @@ -287,6 +285,7 @@ impl PlanRequest { Ok(PlanInput { targets: w.targets, action, + script_variants: p.variants.unwrap_or_default(), execution_identity: p.execution_identity, schedule: Schedule { not_before: w.schedule.not_before, @@ -300,6 +299,71 @@ impl PlanRequest { }) } } +fn validate_variants(variants: &[Variant]) -> Result<()> { + if variants.is_empty() || variants.len() > 2 { + return Err(fail("AMBIGUOUS_TARGET")); + } + let mut platforms = std::collections::HashSet::new(); + for v in variants { + let action = Action::RunScript { + interpreter: v.interpreter.clone(), + script_utf8: v.script_utf8.clone(), + arguments: vec![], + expected_stdout: None, + }; + if v.encoding != "utf-8" + || action.script_platform() != Some(v.os_family.as_str()) + || !action.validate() + { + return Err(fail("UNSUPPORTED_CAPABILITY")); + } + if !platforms.insert(v.os_family.as_str()) { + return Err(fail("AMBIGUOUS_TARGET")); + } + } + Ok(()) +} +fn capabilities(value: Value) -> Result> { + serde_json::from_value(value).map_err(|_| fail("UNSUPPORTED_CAPABILITY")) +} +fn target_action(input: &PlanInput, caps: &[String]) -> Result { + if input.script_variants.is_empty() { + return Ok(input.action.clone()); + } + validate_variants(&input.script_variants)?; + let Action::RunScript { + interpreter, + script_utf8, + arguments, + expected_stdout, + } = &input.action + else { + return Err(fail("VALIDATION_ERROR")); + }; + let first = &input.script_variants[0]; + if interpreter != &first.interpreter || script_utf8 != &first.script_utf8 { + return Err(fail("VALIDATION_ERROR")); + } + let platform = match ( + caps.iter().any(|c| c == "linux.base/1"), + caps.iter().any(|c| c == "windows.base/1"), + ) { + (true, false) => "linux", + (false, true) => "windows", + _ => return Err(fail("UNSUPPORTED_CAPABILITY")), + }; + let v = input + .script_variants + .iter() + .find(|v| v.os_family == platform) + .ok_or_else(|| fail("UNSUPPORTED_CAPABILITY"))?; + Ok(Action::RunScript { + interpreter: v.interpreter.clone(), + script_utf8: v.script_utf8.clone(), + arguments: arguments.clone(), + expected_stdout: expected_stdout.clone(), + }) +} async fn policy(tx: &mut Transaction<'_, Postgres>) -> Result<()> { let stopped: bool = sqlx::query_scalar("SELECT changes_stopped FROM tenants") .fetch_one(&mut **tx) @@ -353,7 +417,7 @@ pub async fn plan( if matches!(input.action, Action::Reboot { .. }) && input.rollout.max_parallel != 1 { return Err(fail("POLICY_DENIED")); } - let mut ids = input.targets.asset_ids; + let mut ids = input.targets.asset_ids.clone(); ids.sort(); ids.dedup(); let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?; @@ -384,11 +448,9 @@ pub async fn plan( })); } let row = &rows[0]; - let caps: Value = row.get("capabilities"); - if !caps - .as_array() - .is_some_and(|c| c.iter().any(|v| v.as_str() == Some(input.action.name()))) - { + let caps = capabilities(row.get("capabilities"))?; + let action = target_action(&input, &caps)?; + if !action.available_for(&input.execution_identity, &caps) { return Err(fail("UNSUPPORTED_CAPABILITY")); } let payload = JobPayload { @@ -400,7 +462,7 @@ pub async fn plan( assignment_version: row.get::("assignment_version") as u64, job_id: Uuid::new_v4(), attempt_id: Uuid::new_v4(), - action: input.action.clone(), + action, execution_identity: input.execution_identity.clone(), not_before: input.schedule.not_before, start_before: input.schedule.start_before, @@ -530,13 +592,19 @@ async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Resu { return Err(fail("AUTHORIZATION_REVOKED")); } - let caps: Value = r.get("capabilities"); - if !caps.as_array().is_some_and(|c| { - c.iter() - .any(|v| v.as_str() == Some(job.payload.action.name())) - }) { + let caps = capabilities(r.get("capabilities"))?; + if !job + .payload + .action + .available_for(&job.payload.execution_identity, &caps) + { return Err(fail("UNSUPPORTED_CAPABILITY")); } + if let Action::RunScript { script_utf8, .. } = &job.payload.action + && job.script_sha256.as_deref() != Some(hash(script_utf8.as_bytes()).as_str()) + { + return Err(fail("PLAN_HASH_MISMATCH")); + } if hash(&encode(&job.payload)?) != job.job_payload_hash { return Err(fail("PLAN_HASH_MISMATCH")); } @@ -921,3 +989,213 @@ async fn operations_http( json!({"items":items,"next_cursor":next,"completeness":if truncated{"partial"}else{"complete"},"truncated":truncated,"as_of":page.as_of}), )) } + +#[cfg(test)] +mod windows_contract_tests { + use super::*; + fn input() -> PlanInput { + let tenant = Uuid::new_v4(); + let now = Utc::now(); + serde_json::from_value::(json!({"tenant_id":tenant,"action":"run_script","targets":{"asset_ids":[Uuid::new_v4()]},"parameters":{"execution_identity":"agent_service","timeout_seconds":60,"max_output_bytes":4096,"variants":[{"os_family":"linux","interpreter":"posix_sh","encoding":"utf-8","script_utf8":"printf hi\n"},{"os_family":"windows","interpreter":"powershell","encoding":"utf-8","script_utf8":"Write-Output 'hi'\r\n"}]},"schedule":{"not_before":now,"start_before":now+chrono::Duration::minutes(10),"offline_policy":"wait_until_deadline"},"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"fixed mixed plan"})).unwrap().resolve(tenant).unwrap() + } + #[test] + fn exact_fixed_variants_and_typed_bypass_denials() { + assert!(capabilities(json!(["windows.base/1", 42])).is_err()); + assert!(capabilities(json!({"run_script": true})).is_err()); + let mut i = input(); + let linux = target_action(&i, &["linux.base/1".into()]).unwrap(); + assert_eq!(linux, i.action); + let windows = target_action(&i, &["windows.base/1".into()]).unwrap(); + assert_eq!(windows.script_platform(), Some("windows")); + assert!( + matches!(windows,Action::RunScript{ref script_utf8,..} if script_utf8.as_bytes()==b"Write-Output 'hi'\r\n") + ); + assert!(target_action(&i, &[]).is_err()); + assert!(target_action(&i, &["linux.base/1".into(), "windows.base/1".into()]).is_err()); + i.script_variants.push(i.script_variants[0].clone()); + assert!(target_action(&i, &["linux.base/1".into()]).is_err()); + let mut i = input(); + i.script_variants[1].interpreter = "bash".into(); + assert!(target_action(&i, &["windows.base/1".into()]).is_err()); + let mut i = input(); + i.script_variants[0].script_utf8.push('x'); + assert!(target_action(&i, &["linux.base/1".into()]).is_err()); + let mut i = input(); + i.script_variants.remove(1); + assert!(target_action(&i, &["windows.base/1".into()]).is_err()); + } + #[tokio::test] + #[ignore = "requires dedicated isolated PostgreSQL; tests/windows_plan_database.py"] + async fn windows_plan_caps_persisted_database() { + let admin = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_ADMIN_URL").unwrap()) + .await + .unwrap(); + let pool = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_APP_URL").unwrap()) + .await + .unwrap(); + rmm_persistence::verify_application_role(&pool) + .await + .unwrap(); + let tenant = Uuid::new_v4(); + sqlx::query("INSERT INTO tenants(tenant_id,name) VALUES($1,'isolated Windows contract')") + .bind(tenant) + .execute(&admin) + .await + .unwrap(); + let principal = Principal { + tenant, + name: "windows-contract".into(), + operator: true, + session_hash: None, + csrf: None, + scopes: None, + testsimulation: false, + identity: None, + tenants: vec![tenant], + }; + sqlx::query("INSERT INTO memberships VALUES($1,$2,'automation',ARRAY['operations:plan','operations:submit','operations:read','scripts:execute'],NULL)").bind(tenant).bind(&principal.name).execute(&admin).await.unwrap(); + let windows_caps = json!([ + "windows.base/1", + "rmm.sync.v1", + "run_script", + "script.powershell/1" + ]); + let mut assets = Vec::new(); + let mut agents = Vec::new(); + for caps in [json!(["linux.base/1", "run_script"]), windows_caps.clone()] { + let asset = Uuid::new_v4(); + let agent = Uuid::new_v4(); + assets.push(asset); + agents.push(agent); + sqlx::query("INSERT INTO assets(tenant_id,asset_id) VALUES($1,$2)") + .bind(tenant) + .bind(asset) + .execute(&admin) + .await + .unwrap(); + sqlx::query("INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256,capabilities,connection_generation) VALUES($1,$2,$3,$4,$5,1)").bind(tenant).bind(agent).bind(asset).bind(hash(agent.as_bytes())).bind(caps).execute(&admin).await.unwrap(); + } + let temp = std::path::PathBuf::from(format!("/tmp/windows-contract-{}", Uuid::new_v4())); + let state = crate::State { + pool, + signer: None, + viewer_a: [0; 32], + viewer_b: [1; 32], + auth: std::sync::Arc::new(crate::auth::Auth::from_env("development").await.unwrap()), + job_signer: std::sync::Arc::new( + crate::job_signer::JobSigner::load_or_create(&temp.join("key.pem")).unwrap(), + ), + }; + let mut request = input(); + request.targets.asset_ids = assets.clone(); + let view = plan(&state, &principal, tenant, request.clone()) + .await + .unwrap(); + let id = Uuid::parse_str(view["plan_id"].as_str().unwrap()).unwrap(); + let original: Vec = + sqlx::query_scalar("SELECT payload FROM operation_plans WHERE plan_id=$1") + .bind(id) + .fetch_one(&admin) + .await + .unwrap(); + let bound: PlanPayload = serde_json::from_slice(&original).unwrap(); + assert_eq!(bound.jobs.len(), 2); + for job in &bound.jobs { + assert_eq!(hash(&encode(&job.payload).unwrap()), job.job_payload_hash); + let expected = if job.payload.asset_id == assets[0] { + "linux" + } else { + "windows" + }; + assert_eq!(job.payload.action.script_platform(), Some(expected)); + } + let pending = submit( + &state, + &principal, + tenant, + id, + Submit { + idempotency_key: "fixed-mixed".into(), + }, + ) + .await + .unwrap(); + assert_eq!(pending["approval_state"], "pending_approval"); + assert_eq!(pending["job_count"], 0); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + recheck(&mut tx, &bound).await.unwrap(); + tx.rollback().await.unwrap(); + sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1") + .bind(agents[1]) + .bind(json!(["windows.base/1", "run_script", "rmm.sync.v1"])) + .execute(&admin) + .await + .unwrap(); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert_eq!( + recheck(&mut tx, &bound).await.unwrap_err().1, + "UNSUPPORTED_CAPABILITY" + ); + tx.rollback().await.unwrap(); + assert_eq!( + plan(&state, &principal, tenant, request.clone()) + .await + .unwrap_err() + .1, + "UNSUPPORTED_CAPABILITY" + ); + sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1") + .bind(agents[1]) + .bind(&windows_caps) + .execute(&admin) + .await + .unwrap(); + let mut changed = bound.clone(); + changed.jobs[0].script_sha256 = Some("00".repeat(32)); + let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant) + .await + .unwrap(); + assert_eq!( + recheck(&mut tx, &changed).await.unwrap_err().1, + "PLAN_HASH_MISMATCH" + ); + tx.rollback().await.unwrap(); + request.script_variants.clear(); + assert_eq!( + plan(&state, &principal, tenant, request.clone()) + .await + .unwrap_err() + .1, + "UNSUPPORTED_CAPABILITY" + ); + request.targets.asset_ids = vec![assets[0]]; + plan(&state, &principal, tenant, request).await.unwrap(); + assert_eq!( + plan(&state, &principal, Uuid::new_v4(), input()) + .await + .unwrap_err() + .1, + "NOT_FOUND" + ); + assert!( + crate::job_gateway::next(&state, tenant, agents[1], 1) + .await + .unwrap() + .is_none() + ); + let counts:(i64,i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM jobs WHERE tenant_id=$1),(SELECT count(*) FROM outbox WHERE tenant_id=$1),(SELECT count(*) FROM execution_permits WHERE tenant_id=$1)").bind(tenant).fetch_one(&admin).await.unwrap(); + assert_eq!(counts, (0, 0, 0)); + let after: Vec = + sqlx::query_scalar("SELECT payload FROM operation_plans WHERE plan_id=$1") + .bind(id) + .fetch_one(&admin) + .await + .unwrap(); + assert_eq!(original, after); + std::fs::remove_dir_all(temp).unwrap(); + } +} diff --git a/crates/server/src/monitoring.rs b/crates/server/src/monitoring.rs index 251db0f..fe336ab 100644 --- a/crates/server/src/monitoring.rs +++ b/crates/server/src/monitoring.rs @@ -78,6 +78,7 @@ pub async fn query( asset_ids: vec![i.asset_id], }, action, + script_variants: vec![], execution_identity: "agent_service".into(), schedule: crate::jobs::Schedule { not_before: Utc::now(), diff --git a/deploy/shared-core-acceptance.md b/deploy/shared-core-acceptance.md new file mode 100644 index 0000000..f3f5cd1 --- /dev/null +++ b/deploy/shared-core-acceptance.md @@ -0,0 +1,47 @@ +# Native gemeinsame Core-Unitabnahme + +`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, +Enrollment, Schlüsselzugriffe, Runtimeersetzung, Dienste oder Serverkontakte. +Die Tests enthalten keine Netzwerkstrecke; der Runner behauptet keine zusätzliche +OS-Netzwerksandbox. Bootstraptransport bleibt vorhandenes hostkeygeprüftes SSH/SFTP. + +**Tatsächlich ausgeführt, 2026-11-14:** + +```sh +python3 deploy/shared_core_acceptance.py linux +python3 deploy/shared_core_acceptance.py windows --case-id 4bc4db12-2990-4e93-bb86-8fbd94b691fb +``` + +Fall `4bc4db12-2990-4e93-bb86-8fbd94b691fb`: Linux 25/25 und danach Windows +24/24 nativ bestanden, jeweils ohne Skip. Permanente Dienste wurden nicht +ersetzt; insbesondere kein Windows-Job- oder Dienst-Syncnachweis daraus. + +Der Runner prüft beide privaten Qualifikationsmanifeste, sämtliche gebundenen +Quellhashes und genau zwei Artefakte. Linux läuft zuerst (5+20 Fälle). Windows +(4+20 Fälle) verlangt denselben gespeicherten vollständigen Linux-Nativbeleg und +bindet dessen SHA256. Alle Fälle müssen erfolgreich und ohne Skip ausgeführt sein. +Exakte Testargumente: ausschließlich `--test-threads=1`. + +Lokale Intent-/Start-/Readback-/PASS-Dateien unter +`devstate/shared-core/acceptance//` entstehen create-only. Remote nur eigene +geschützte Bootstrap-Testverzeichnisse: Linux +`/var/lib/rmm-shared-core-acceptance///`, Windows bestehendes +`C:\ProgramData\RustDiscoveryBootstrap\\windows-core-*.exe` und eigener +`shared-core-`-Fall. Ausgaben/Fehler bleiben privat; stdout enthält nur +sanitisierten Status und bei PASS Fall-/Artefakt-/Nachweishashes. + +Bei verlorener Ausführungsantwort **nicht erneut starten**. Ausschließlich +vorhandene Remotequittungen lesen: + +```sh +# GEPLANT, NICHT AUSGEFÜHRT: keine erneute Testausführung. +python3 deploy/shared_core_acceptance.py finish-linux --case-id +python3 deploy/shared_core_acceptance.py finish-windows --case-id +``` + +Fehlt eine Quittung, bleibt die Abnahme unvollständig; kein automatisches Resume, +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. diff --git a/deploy/shared_core_acceptance.py b/deploy/shared_core_acceptance.py new file mode 100644 index 0000000..44fd35e --- /dev/null +++ b/deploy/shared_core_acceptance.py @@ -0,0 +1,252 @@ +#!/usr/bin/env python3 +"""Fixed native shared-core unit qualification: Linux FIRST, then Windows. + +No RMM jobs, enrollment, runtime replacement, daemon changes or server contact. +A lost execution reply permits readback only, never an automatic test rerun. +""" +import argparse +import base64 +import contextlib +import hashlib +import io +import json +import os +from pathlib import Path +import re +import stat +import subprocess +import uuid + +import remote_client_bootstrap as linux +import vm_mvp_acceptance as contract +from windows import client_bootstrap as windows + +ROOT = Path(__file__).resolve().parents[1] +STATE = ROOT / 'devstate/shared-core/acceptance' +KINDS = ('contracts', 'reconcile') +REMOTE_LINUX = '/var/lib/rmm-shared-core-acceptance' + + +def sha(data): + return hashlib.sha256(data).hexdigest() + + +def write_new(path, value): + path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + 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') + sources = [ROOT/'Cargo.toml', ROOT/'Cargo.lock', ROOT/'rust-toolchain.toml'] + for name in ('agent-core', 'domain', 'protocol'): + 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} + 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): + 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') + artifacts[kind] = item + return dict(manifest_sha256=sha((folder/'manifest.json').read_bytes()), artifacts=artifacts) + + +# Functions sent by the existing pinned SSH/bootstrap RPC. Only fixed test files +# are touched, never production journals, keys, packages, services or endpoints. +def linux_case(payload): + import stat + root = Path('/var/lib/rmm-shared-core-acceptance') + 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') + 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(os.geteuid() == 0, 'bootstrap root only') + def safe(path, directory): + m = path.lstat() + require(not stat.S_ISLNK(m.st_mode) and m.st_uid == 0 and not m.st_mode & 0o022 and (stat.S_ISDIR(m.st_mode) if directory else stat.S_ISREG(m.st_mode)), 'private fixed test path') + def save(path, value): + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) + with os.fdopen(fd, 'w') as out: + json.dump(value, out); out.flush(); os.fsync(out.fileno()) + if not root.exists(): root.mkdir(mode=0o700) + safe(root, True) + 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)) + 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') + result_path = case/(kind+'-result.json') + if payload['action'] == 'read': + safe(result_path, False) + return json.loads(result_path.read_bytes()) + save(case/(kind+'-started.json'), dict(kind=kind, binary_sha256=entries[kind]['binary_sha256'])) + path = case/entries[kind]['binary_sha256']/kind + safe(path.parent, True); safe(path, False) + require(hashlib.sha256(path.read_bytes()).hexdigest() == entries[kind]['binary_sha256'], 'pinned test hash') + path.chmod(0o500) + outputs = [] + for label in ('stdout', 'stderr'): + outputs.append(open(case/(kind+'-'+label+'.bin'), 'xb')) + os.chmod(outputs[-1].name, 0o600) + timed_out = False + try: + try: + result = subprocess.run([str(path), '--test-threads=1'], stdin=subprocess.DEVNULL, stdout=outputs[0], stderr=outputs[1], timeout=25) + code = result.returncode + except subprocess.TimeoutExpired: + timed_out = True; code = None + finally: + for out in outputs: out.flush(); os.fsync(out.fileno()); out.close() + raw = [(case/(kind+'-'+label+'.bin')).read_bytes() for label in ('stdout', 'stderr')] + require(all(len(b) <= 1024*1024 for b in raw), 'fixed unit output bound') + report = dict(kind=kind, binary_sha256=entries[kind]['binary_sha256'], exit_code=code, timed_out=timed_out, stdout_b64=base64.b64encode(raw[0]).decode(), stderr_b64=base64.b64encode(raw[1]).decode(), native_platform='linux') + save(result_path, report) + return report + + +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'} +$root='C:\ProgramData\RustDiscoveryBootstrap' +$case=Join-Path $root ('shared-core-'+$p.id) +function Safe([string]$path,[bool]$directory) { + $i=Get-Item -LiteralPath $path -Force + if ($i.PSIsContainer -ne $directory -or ($i.Attributes -band [IO.FileAttributes]::ReparsePoint)) {throw 'Fixed test path type'} + $a=Get-Acl -LiteralPath $path + if ($a.GetOwner([Security.Principal.SecurityIdentifier]).Value -notin @('S-1-5-18','S-1-5-32-544')) {throw 'Private test owner'} + $rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])) + if (!$rules.Count) {throw 'Empty test DACL'} + foreach($r in $rules) {if ($r.AccessControlType -ne 'Allow' -or $r.IdentityReference.Value -notin @('S-1-5-18','S-1-5-32-544')) {throw 'Broad test DACL'}} +} +function Write-New([string]$path,$value) { + $b=[Text.Encoding]::UTF8.GetBytes(($value | ConvertTo-Json -Compress -Depth 8)) + $f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read) + try {$f.Write($b,0,$b.Length);$f.Flush($true)} finally {$f.Dispose()} +} +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} +} +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'} +$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} +$folder=Join-Path $root $p.sha256;Safe $folder $true +$exe=Join-Path $folder ('windows-core-'+$p.kind+'.exe');Safe $exe $false +if ((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant() -cne $p.sha256) {throw 'Pinned unit hash'} +$out=Join-Path $case ($p.kind+'-stdout.bin');$err=Join-Path $case ($p.kind+'-stderr.bin') +if ((Test-Path -LiteralPath $out) -or (Test-Path -LiteralPath $err)) {throw 'Existing native output'} +$proc=Start-Process -FilePath $exe -ArgumentList @('--test-threads=1') -PassThru -WindowStyle Hidden -RedirectStandardOutput $out -RedirectStandardError $err +$handle=$proc.Handle;$timeout=$false +if (!$proc.WaitForExit(25000)) {$timeout=$true;$proc.Kill();$proc.WaitForExit()} +$proc.Refresh();$code=$proc.ExitCode +if ((Get-Item -LiteralPath $out).Length -gt 1048576 -or (Get-Item -LiteralPath $err).Length -gt 1048576) {throw 'Fixed unit output bound'} +$value=@{kind=$p.kind;binary_sha256=$p.sha256;exit_code=$code;timed_out=$timeout;stdout_b64=[Convert]::ToBase64String([IO.File]::ReadAllBytes($out));stderr_b64=[Convert]::ToBase64String([IO.File]::ReadAllBytes($err));native_platform='windows'} +Write-New $result $value +$value | ConvertTo-Json -Compress -Depth 8 +''' + + +def upload_linux(case, kind, item): + cfg = linux.access.load_config(ROOT/'credentials/linux-vm.conf') + args = ['sftp', '-F', '/dev/null', '-P', cfg.get('port', '22'), '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()), '-b', '-', cfg['username']+'@'+cfg['address']] + path = ROOT/item['path'] + remote = REMOTE_LINUX+'/'+case.name+'/'+item['binary_sha256']+'/'+kind + batch = 'put '+json.dumps(str(path))+' '+json.dumps(remote)+'\n' + result = subprocess.run(args, input=batch.encode(), capture_output=True, timeout=120) + write_new(case/('linux-'+kind+'-sftp.json'), dict(returncode=result.returncode, stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode())) + contract.require(result.returncode == 0, 'private Linux SFTP failure; inspect case') + + +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']) + + +def verify_result(platform, kind, result, expected): + contract.require(result['native_platform'] == platform and result['kind'] == kind and result['binary_sha256'] == expected['binary_sha256'] and result['exit_code'] == 0 and result['timed_out'] is False, 'native test exit/hash') + out = base64.b64decode(result['stdout_b64'], validate=True) + err = base64.b64decode(result['stderr_b64'], validate=True) + contract.require(len(out) <= 1024*1024 and len(err) <= 1024*1024, 'private unit output limit') + pattern = rb'test result: ok\. '+str(expected['test_count']).encode()+rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;' + contract.require(re.search(pattern, out) is not None, 'exact native unit counts, no skipped cases') + + +def main(): + os.umask(0o077) + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('action', choices=('linux', 'windows', 'finish-linux', 'finish-windows')) + parser.add_argument('--case-id') + 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')) + 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') + 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]) + 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)) + 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]) + results = {} + for kind in KINDS: + if platform == 'linux': + result = linux.rpc('linux-vm.conf', linux_case, payload(intent, 'read' if finish else 'run', kind)) + else: + item = intent['windows']['artifacts'][kind] + # Existing bootstrap prints only sanitized failures; keep even these + # in the protected case so this CLI never prints remote diagnostics. + captured = io.StringIO() + 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) + 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) + verify_result(platform, kind, result, intent[platform]['artifacts'][kind]); results[kind] = result + proof = dict(id=intent['id'], passed=True, native_platform=platform, qualification=intent[platform], linux_evidence_sha256=linux_sha, results=results) + destination = case/(platform+'-passed.json') + if destination.exists(): contract.require(contract.protected_json(destination) == proof, 'identical original native proof') + else: write_new(destination, proof) + print(json.dumps(dict(result='PASS', native_platform=platform, case_id=intent['id'], evidence_sha256=sha(destination.read_bytes()), artifact_sha256={k:v['binary_sha256'] for k,v in intent[platform]['artifacts'].items()}))) + + +if __name__ == '__main__': + try: main() + except Exception as error: + print('SHARED_CORE_ACCEPTANCE_INCOMPLETE:'+type(error).__name__) + raise SystemExit(2) from None diff --git a/deploy/windows/client_bootstrap.py b/deploy/windows/client_bootstrap.py index 32c6e63..dcbd9be 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))\.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))\.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))\.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))\.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 f4c0f3e..7dc90d1 100644 --- a/docs/handover.md +++ b/docs/handover.md @@ -52,6 +52,17 @@ Zugänge und private Rohbelege bleiben gitignoriert. ## Aktuelle Windowsfortsetzung +**Fortsetzung 2026-11-14:** Gemeinsamer `rmm-agent-core` und Linuxadapter +implementiert. Eigene Core-Unittests zuerst auf Cloud-Linux (25) und danach auf +Windows (24) nativ ohne Skip bestanden; Fall +`4bc4db12-2990-4e93-bb86-8fbd94b691fb`. Geschützte Manifest-/Quell-/Artefaktbindung +in `devstate/shared-core/acceptance//`. Keine Dienst-/Serverersetzung oder +Jobwirkung durch diese Tests. Native Dienstintegration, vertrauenswürdige Uhr +und erste tatsächliche WebUI-genehmigte PowerShell-Strecke bleiben offen. +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). + 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 bc7a34c..1f4ca13 100644 --- a/docs/implementation-status.md +++ b/docs/implementation-status.md @@ -1,5 +1,37 @@ # Tatsächlicher Implementierungsstand +## Gemeinsamer Jobkern nativ auf Linux und Windows geprüft — 2026-11-14 + +Der neue `rmm-agent-core` enthält die gemeinsame signierte Sync-/Checkpoint- +Maschine, Permit-/Request-/Outcome-Codecs, Verbrauchsledger und portable +Jobpersistenz. Linux verwendet diese Implementierung über dünne Adapter; +seine bisherige OpenSSL-Verifikation bleibt erhalten. Windows verwendet Dalek, +ohne OpenSSL-Abhängigkeit. Die bisherigen Linux-Codecbytes bleiben unverändert. + +Tatsächlich bestanden: 151 betroffene lokale Rusttests und Clippy; danach im +eigenen Fall `4bc4db12-2990-4e93-bb86-8fbd94b691fb` zuerst 25 native +Core-Unittests auf der Cloud-Linux-VM, anschließend 24 auf der Windows-VM, +jeweils ohne Skip. Quell-/Artefakthashes und vollständige Ausgabe bleiben in +geschützten Manifesten und `devstate/shared-core/acceptance//` erhalten. +Die unterschiedlichen Zahlen entstehen durch den zusätzlichen Unix-OpenSSL- +Paritytest. Diese Programme verwenden ausschließlich eigene SQLite-Fixturen, +Testsignaturen und simulierte Clock/Channel-Objekte. Kein RMM-Job, Dienstwechsel, +Enrollment oder Netzwerkpfad wurde damit ausgeführt. Ein nativer Sync des +laufenden Dienstes mit dem extrahierten Kern bleibt separat nachzuweisen. + +Server/Domain/WebUI sind additiv für feste Windows-/Linux-Skriptvarianten und +Windows-Basisinventur vorbereitet. Gemeinsame Plattform-/Capabilityprüfung bei +Planung, erneuter Prüfung, Grant und Zustellung; vor Genehmigung unverändert +0 Jobs/Outbox/Grants. Exakte stdout-Nachbedingungen lassen sich als JSON-String +einschließlich CRLF eingeben. Lokal bestanden: 14 Domainfälle einschließlich +Linux-Goldenbytes, Variantenprüfung, isolierter PostgreSQL-Test mit Migrationen +und Tenant-/Capability-Verweigerungen, Clippy sowie 29 Webtests/Typecheck. +Dies ist noch kein Clouddeployment oder Windows-Ausführungsnachweis. + +Windows-Dienst, Identität und bisherige passive Capabilities blieben bei diesem +Schritt unverändert. Clock-, Executor-, Helper-, Paket- und übrige native +Windows-Abnahmen laufen weiter; die Plattform meldet dafür keinen Erfolg. + ## Windows: dauerhaftes Auto auf bestehender VM nativ bestanden — 2026-11-13 Separater Fall `422562e8-18eb-4865-977d-ab2af65f510b` verifiziert und abgeschlossen. diff --git a/docs/windows-gates.md b/docs/windows-gates.md index b10183c..e9f3464 100644 --- a/docs/windows-gates.md +++ b/docs/windows-gates.md @@ -11,7 +11,7 @@ bleibt eine eigenständige Abnahme. Dieser Bericht erweitert dessen Umfang nicht | Passiver Client | SCM-Dienst, Service-SID/ACLs, Enrollment/mTLS/WSS, Inventar mit Quelle/Zeit, Heartbeats und Mandantentrennung nativ bestanden; sieben native Rusttests. | Bestehende Identität und Telemetriehistorie bei allen weiteren Schritten erhalten. | | HTTPS/Fallback | Explizites HTTPS und automatischer Fallback nach zwei tatsächlichen mTLS-WSS503 unter LocalService nativ bestanden, jeweils mit frischen Heartbeats und Inventar-ACK. Testhooks geschlossen. Anschließend bestehende VM separat dauerhaft auf `auto` gestellt und bevorzugtes WSS erneut mit zwei frischen Heartbeats/ACK geprüft; Identität und ACLs erhalten, 0 Jobs/Grants. | Transportnachweise abgeschlossen; kein automatischer HTTPS→WSS-Switchback nachgewiesen. Installerdefault ebenfalls `auto`, aber keine neue Installationsabnahme. | | TLS-Lifecycle | Falsche CA, falscher Name und abgelaufenes Clientzertifikat mit gültigen Kontrollen nativ als getrennte Admin-TLS-Proben bestanden. Permanente Identität/Dienst unverändert. | Rotation und Widerruf noch gesondert sicher implementieren und prüfen. Permanente Identität nicht widerrufen oder ersetzen. | -| Signierter Jobkern | Windows verweigert Jobangebote/Grants ausdrücklich und meldet keine Ausführungscapability. | Gemeinsamen Sync-/Checkpoint-/Einmalverbrauchskern portieren; vertrauenswürdige Uhr, Journalmigration ohne Reset, Schlüsselbindung und Fehlerpfade qualifizieren. | +| Signierter Jobkern | Gemeinsamer Core und Linuxadapter implementiert; native Core-Unittests zuerst Linux (25), dann Windows (24) bestanden. Bestehender Windows-Dienst bleibt passiv und verweigert Jobs/Grants. | Native Dienstintegration, vertrauenswürdige Uhr, erhaltende Journalmigration und vollständige Startgrenze qualifizieren. Unit-Fixturen ersetzen keinen tatsächlichen Jobnachweis. | | PowerShell | Noch kein Executor oder freigegebener Interpreter. Server/UI lösen derzeit Linuxvarianten auf. | Exakte Windows-PowerShell-5.1-Bytes, Ausgabeencoding, Prozessgrenzen und unprivilegierte Ausführungsidentität binden. Erst nach vollständigem Antrag und tatsächlichem WebUI-Klick ausführen. | | Privilegierte Aktionen | Kein Windows-Helper, keine Named-Pipe-Aktionsschnittstelle. | Eigenständiger restriktiver Helper mit geprüfter Pipe-Identität und unabhängiger Signatur-/Plan-/Grantprüfung; keine SYSTEM-Netzwerkkomponente. | | Pakete/Dienste/Logs | Noch keine native Windows-Aktionsabnahme. | Begrenzte geprüfte MSI-/EXE-Rezepte, Dienst-/Eventlogadapter und unabhängige Nachbedingungen. Keine allgemeinen Installershellstrings oder impliziten Reboots. | diff --git a/docs/windows-remaining-implementation.md b/docs/windows-remaining-implementation.md new file mode 100644 index 0000000..f62e76c --- /dev/null +++ b/docs/windows-remaining-implementation.md @@ -0,0 +1,237 @@ +# Windows: verbleibende Umsetzung nach der passiven Foundation + +Stand 2026-11-14, Spezifikation1.15. **Umsetzungsplan, keine native Abnahme.** +Linux-M0–M5 bleiben separat abgeschlossen. Der Windows-Testgast ist ein eigenes +Gerät; das Notebook bleibt Entwicklungsrechner. Server bleibt +`discovery.ease-technologies.de`. RustDesk, PVE und M7 werden nicht vorgezogen. +Diese Planung erstellt weder Jobs noch Grants und verändert keine Zielmaschine. + +Die passive Windowsstrecke umfasst bereits LocalService, Enrollment/mTLS, +WSS/HTTPS/Auto-Fallback und gespeicherte Telemetrie. TLS-Negativproben als +Bootstrapadministrator ersetzen keine LocalService-Negativprobe oder Rotation. +Der gemeinsame Agentkern und die additive Plattform-/Interpreterplanung werden +aktuell separat qualifiziert. Keine daraus abgeleitete Windowscapability oder +Jobbereitschaft behaupten. Vor dem ersten Hello gelten weiterhin +[Jobgrenze](windows-job-boundary.md) und [Windows-Gates](windows-gates.md). + +## Reihenfolge mit konkreter Lieferung + +| Schritt | Kleinste lauffähige Lieferung | Eigener nativer Nachweis | +| --- | --- | --- | +| W1 unprivilegierte Jobs | Konfigurierte Signingbindung, TrustedClock, gemeinsamer signierter Sync, erhaltendes Jobjournal und begrenzter PowerShell5.1-Executor unter LocalService. | Fester UI-Hello mit exakten Rohbytes; Live-Inventur; ACK/Redelivery, Offline/Frist/Entzug, Prozessabbruch und gesperrter Wiederanlauf. Keine SYSTEM-Ausnahme. | +| W2 Helpergrenze | Ein lokaler SYSTEM-Dienst ohne Netzwerklistener, wechselseitig authentisierte Named Pipe, eigenes geschütztes Ledger, unabhängige Signatur-/Sync-/Policyprüfung. Zunächst alle Wirkungen verweigert. | Erreichbarkeit aus dem eigenen LocalService, fremde SID/PID, Pipebesetzung, falsche Signatur/Generation/Hash/Frist und volle Journale verweigert; keine Wirkung. | +| W3 Dienst und Logs | Nur eigener ungefährlicher Fixturedienst; SCM-Aktion und begrenzter Event-Log-Provider. | Je eigener UI-Plan für Start/Stop/Nachprüfung und Live-Logfenster, unabhängige OS-Nachprüfung; sensible/fremde Quellen verweigert. | +| W4 MSI und EXE | Je ein geprüftes, kleines Fixture-Rezept, feste Versionen1.0/1.1, eigener Produkt-/Dateinachweis, kein Reboot. | Native Installation, Upgrade und Noop je festem UI-Plan; Paketbestand/Nutzdateien unabhängig prüfen. MSI und EXE sind getrennte Providerabnahmen. | +| W5 Wiederaufnahme und Reboot | Bewahrte Originalaufträge/Marker; genau ein ausdrücklich erlaubter Windows-Reboot mit ursprünglichem Grant und neuer Bootkennung. | Kein blindes Wiederholen nach Wirkung; rechtzeitiger unabhängiger Wiederkontakt, zusätzlicher frischer Verifikationsbeleg und normaler signierter Abschluss. | +| W6 Abschluss der Windowsmatrix | Rotation/Widerruf auf getrennter Testidentität, Windows/Linux-Mischplan AT-15, geprüfte unterstützte Inventarquellen, Windows-Installer-/Signiernachweis. | Keine Ableitung aus Linux-/Cross-Build-/Adminproben. Offene Betriebssystemupdate-/Patchfähigkeiten sichtbar lassen, bis ihr eigener Provider und Umfang geprüft sind. | + +Jeder Schritt endet mit tatsächlichem Bericht und eigenem Commit/Push. Neue +Wirkungsversuche benötigen neue UI-Pläne. Ein fehlgeschlagener Versuch bleibt +fehlgeschlagen, Unknown bleibt gesperrt bis zum gebundenen Abgleich. Die Tabelle +ist keine pauschale Erlaubnis, Installationen oder einen Windows-Reboot auszuführen. + +## Gemeinsamer Nachweisvertrag, Windowsadapter + +Bestehende Bytes von `agent-core::auth::{Binding,Request,Outcome}`, +`ExecutionPermitPayload`, Job-/Planhashes und Signaturdomain bleiben erhalten. +Der Helper erhält denselben vollständigen Request wie der Agent und prüft ihn +selbst: Gerät/Kunde/Zuordnung, genaue Jobbytes, Genehmigungsreferenz, +Signierschlüssel, kurze Startfrist und Clock. Vor Wirkung hält er die eigene +Ressourcensperre und persistiert Grant-/Versuchsverbrauch samt Originalboot und +Originalrequest. Gleicher Versuch mit neuer Grant-ID ist keine neue Erlaubnis. +Ergebnis bleibt bis konkretem ACK; `unknown` und `awaiting_reboot` geben den +Verbrauch nicht frei. Der aktuelle gemeinsame Ledgerkern begrenzt SQLite bereits +auf1GiB/4096-Byte-Seiten und ignoriert keine Kapazitätsfehler. + +`privileged-helper/src/reconcile.rs` und `control.rs` enthalten derzeit weiterhin +Unix-Datei-/Peerzugriff, OpenSSL und Linux-Boot/Clock. Sie sind kein fertiger +Windowshelper. Deren reine Helper-Abgleichregeln werden wie der Agentkern in +einen gemeinsamen Kern mit Ports für Dateisperre/Dateiidentität, Clock, Boot und +Verifier extrahiert; Linuxbytes und vorhandene Goldenfixtures bleiben identisch. +Keine zweite abweichende Syncmaschine, kein lokales `ready=true` als Ersatz. +Windows bindet Volume-/File-ID der offenen Ledger-/Sperrhandles; Unix-`dev/ino` +werden nicht erfunden. Helper-Snapshot/Apply prüfen die ursprünglichen signierten +Entscheidungsbytes und frische Challenge-/Generationsbindung unabhängig. + +Die lokale Windowskonfiguration ist ein neuer, ausdrücklich benannter +SYSTEM-Policyvertrag, kein Linux-Rootpolicyprofil unter anderem Namen. Sie bindet +festes Gerät, Signierschlüssel, Agent-/Helperservice-SIDs, erlaubte Aktionen, +exakte Fixturedienste/Rezepthashes/Logkanäle, Laufzeitgrenzen und +`allow_reboot=false` als Default. Policyhash und Helperbuild müssen im +Readinessnachweis und in der zentralen Zielpolicy übereinstimmen. + +## Named Pipe und Identitäten + +Ein fest benannter lokaler Pipeendpunkt verwendet explizite DACL, +`PIPE_REJECT_REMOTE_CLIENTS`, `FILE_FLAG_FIRST_PIPE_INSTANCE` beim ersten Server- +Handle und zeitbegrenzte Overlapped-I/O. Keine Default-DACL: Microsoft beschreibt +sonst zusätzliche Lesezugriffe für Everyone/Anonymous. Die Agent-SID erhält nur +die konkreten benötigten Daten-/Attributrechte, kein generisches Schreibrecht, +das zugleich die Erstellung weiterer Pipeinstanzen erlauben kann. +[Pipe-Rechte](https://learn.microsoft.com/en-us/windows/win32/ipc/named-pipe-security-and-access-rights), +[CreateNamedPipeW](https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-createnamedpipew). + +Der Helper liest zunächst nur einen begrenzten Frame, prüft den tatsächlichen +Client-PID und impersonierten Token: LocalService **und** eigene aktivierte +Service-SID, SCM-Prozess und gehaltenes Prozesshandle/Startidentität. LocalService +allein genügt nicht, da weitere Dienste dieses Konto nutzen können. Der Agent +prüft umgekehrt Server-PID, SYSTEM-Token, erwartete Helperservice-SID, SCM-Bindung +und geschützte Helperbinary. PID allein ist wegen Wiederverwendung kein Beleg. +Diese Kombination ist die vorgeschlagene Projektprüfung, keine Garantie einer +Einzel-API. Fehlgeschlagene Impersonation verweigert; vor Handlerausführung +`RevertToSelf` sicherstellen. Keine Kindprozesse unter versehentlich verbliebener +Impersonation starten. +[Client-PID](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getnamedpipeclientprocessid), +[Server-PID](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getnamedpipeserverprocessid), +[Impersonation und Fehlerfall](https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-impersonatenamedpipeclient). + +Helperstate und Konfiguration sind SYSTEM/Administrators-geschützt; der +Netzwerkagent kann sie nicht ersetzen. Bestehende Gerätekeys bleiben beim Agenten, +der Helper erhält ausschließlich die provisionierte öffentliche Signingbindung. +Ein eigener Windowshelper-Binarypfad innerhalb der Windowscrate vermeidet +Unix-Abhängigkeiten des Linuxhelper-Binaries. Bereits vor dem ersten autorisierten +Systemjob müssen Pipeauthentisierung und unabhängiger Sync nativ bestanden sein. + +## Minimaler additiver Aktions-/Manifestvertrag + +Vorgeschlagen ist ein optionales `windows_manifest` am `JobPayload` mit +`default`/`skip_serializing_if=None`; Linuxjobs behalten exakt ihre historischen +Bytes. Noch keine neue produktive Schemaversion oder Policyentscheidung. +Manifesttyp `rmm.windows.execution`, eigene Version1, strikte Felder: + +| Feldgruppe | Inhalt/Prüfung | +| --- | --- | +| Ziel und Auftrag | Kunde, Asset, Agent, Enrollment-/Assignmentgeneration, Job, Versuch und Action; müssen exakt dem Job entsprechen. | +| Runtime/Policy | Plattform/Architektur, Manifestversion, Helperpolicyhash und abgenommene Providerkennung. Keine vom Agenten gewählte Policy. | +| Rezept | Bei Paketaktionen unveränderliche Rezept-ID/-Version/-SHA, Produkt/Version/Installationskontext, Artifact-ID/-SHA/-Länge, fester strukturierter Argumentvektor, feste Exitklassifikation und typisierte Nachbedingungen. | +| Systemaktion | Bei Dienst/Log/Reboot ausschließlich die erforderlichen typisierten Felder und Nachbedingungen; kein Paketmanifest als allgemeiner Shellcontainer. | + +Kein `job_payload_hash` innerhalb des Manifests: dieser würde sich selbst +referenzieren. Der Startgrant bindet den Hash des **gesamten** Jobpayloads samt +Manifest. Unbekannte Felder/Provider, widersprüchliche Actionparameter, +abweichende Recipe-/Artifactbytes oder fehlende Nachbedingungen verweigern vor +Verbrauch/Wirkung. Neue Windowsfelder sind Pflicht nur für die entsprechenden +Windowsaktionen; alte APT-Manifeste werden nicht umgedeutet. + +Bestehende Fachaktionen bleiben `ensure_package`, `apply_updates`, +`set_service_state`, `query_logs` und `reboot`. Windowsprovider werden additiv +validiert, mit eindeutiger Plattformbindung. `system` wäre ein neuer expliziter +Ausführungsidentitätswert mit eigenem `system:execute`-Recht und eigener +SYSTEM-Policy; Linux-`root` bedeutet niemals Windows-SYSTEM. Freie SYSTEM-Skripte +sind kein notwendiger Ersatz für typisierte Aktionen. Capabilitynamen und genaue +Manifestfelder erst gemeinsam mit Helper/Server festschreiben; keine +Capabilities anbieten, bevor der tatsächliche Adapter bereit ist. + +Für Windowsrezepte empfiehlt sich eine eigene immutable Tabelle mit Tenant-RLS, +zusammengesetzten Tenant-FKs und getrennten MSI-/EXE-Kennungen. Der aktuelle +APT-Katalog ist auf `architecture='amd64'` und APT-Abfragen beschränkt; seine +Schlüssel dürfen nicht still für Windowsprodukte wiederverwendet werden. +Bestehende Artifactreferenzen/Hashes können erhalten bleiben. Das aktuelle +Artifactlimit beträgt256KiB: kleine Fixtureartefakte zunächst dagegen prüfen; +eine notwendige größere, begrenzte Übertragung wäre eine eigene implementierte +und getestete Vertragsänderung, kein Bootstrap-SFTP als RMM-Paketnachweis. + +## Providerverhalten und überprüfbare Wirkung + +**MSI:** Feste systemweite Installation aus dem Helper-eigenen geschützten Cache; +`msiexec /i /qn /norestart REBOOT=ReallySuppress`, ergänzt nur +um die im Rezept gebundenen Properties. Keine URL, UNC-Quelle, frei gewählte +Transformdatei oder weitere Installerargumente. Nach dem Kopieren erneut hashen, +Pfadkomponenten/Reparsepoints und Schreibrechte prüfen, während der Nutzung +Ersetzung/Änderung verweigern. `0` ist nur ein Prozessresultat; `3010` bedeutet +Neustartbedarf. `1641` meldet bereits eingeleiteten Neustart und verletzt diesen +No-Reboot-Rezeptvertrag; nicht als normale erfolgreiche Abnahme werten. +`1618` respektiert fremde Installation, kein Beenden fremder Prozesse. +[Installeroptionen](https://learn.microsoft.com/en-us/windows/win32/msi/standard-installer-command-line-options), +[REBOOT](https://learn.microsoft.com/en-us/windows/win32/msi/reboot), +[Exitcodes](https://learn.microsoft.com/en-us/windows/win32/msi/error-codes). + +Nachprüfung kombiniert `MsiGetProductInfoExW` im expliziten Machine-Kontext +mit installiertem Produktzustand/Version und eigener Fixture-Nutzdatei samt Hash. +Ein beworbenes Produkt ist nicht automatisch installiert. Keine +`Win32_Product`-Erhebung. MSI-Fehler, Timeout oder verlorener Installerkontakt +belegen keine Abwesenheit einer Teilwirkung: gegebenenfalls Unknown und ein +gebundener Nur-Lese-Abgleich, keine automatische neue Installation. +[Produktinformation und Kontext](https://learn.microsoft.com/en-us/windows/win32/api/msi/nf-msi-msigetproductinfoexw). + +**EXE:** Eigenes, nachvollziehbar gebautes Fixture mit genau dokumentierten +Quiet-/No-Reboot-Argumenten, Hash, Architektur und Exitsemantik. MSI-Codes gelten +nicht automatisch für EXE. Ausschließlich verifizierte feste Rezeptparameter; +kein beliebiges EXE als SYSTEM ausführen. Nachprüfung über eigene Registry- +Produktkennung und Nutzdatei/Version/Hash. No-Reboot-Flags schützen nicht gegen +beliebige bösartige SYSTEM-Customactions: die ausdrücklich geprüfte Herkunft und +der begrenzte Rezeptumfang bleiben erforderlich. Ein Timeout beendet nicht +pauschal einen systemweiten Installationsdienst oder fremde Installer. + +**Dienste:** `OpenSCManager/OpenService` mit benötigten Einzelrechten, +`StartServiceW`/`ControlService`, danach begrenztes Warten und unabhängiges +`QueryServiceStatusEx`. Startannahme ist nicht gleich Running. Zustandsübergänge, +Checkpoint/WaitHint, Dienstexit und fehlende Rechte strukturiert melden. +Abnahme zunächst nur am eigenen Fixturedienst mit festem Binaryhash; nicht am +RMM-Agenten, SSH oder einer fremden Produktionskomponente. +[StartServiceW](https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicew), +[Status](https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-queryservicestatusex), +[SCM-Rechte](https://learn.microsoft.com/en-us/windows/win32/services/service-security-and-access-rights). + +**Event Log:** Eigener Kanal-/Providerfilter und fixes Zeitfenster; keine freie +Datei, kein roher vom Modell gelieferter XPath. `EvtQuery/EvtNext/EvtRender`, +maximal1000Events/256KiB und Laufzeitlimit. Quelle, EventRecordID, Provider, +UTC-Zeit, Fehlermeldungen und Trunkierung erhalten; fehlender Zugang bleibt +permission_denied. Bookmark erst nach dauerhafter Übernahme/konkretem ACK +fortschreiben. Einzelnes Livefenster ist ein UI-genehmigter Diagnosejob; +gespeicherte Ergebnisse danach tenantgebunden lesen. Kein allgemeiner Zugriff +auf Security-Kanäle durch die Abnahme eines Fixturekanals. +[Eventabfragen](https://learn.microsoft.com/en-us/windows/win32/wes/querying-for-events), +[Bookmark](https://learn.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtupdatebookmark). + +**Reboot:** Nur lokales `InitiateSystemShutdownExW`, fest gebundene Begründung, +keine erzwungene Schließung fremder Anwendungen, eigenes gezielt aktiviertes +Shutdownprivileg im Helper. Dauerhaften Originalverbrauch und Bootbeleg zuerst +speichern. APIannahme ist asynchron und kein Erfolgsnachweis; Anwendungen können +den Vorgang verzögern/verhindern. Konservative zentrale Frist ab Startreservierung +und beide bestehenden Inventarbelege gelten unverändert. Die bisherige +CIM-LastBootUpTime-basierte Windowskennung muss einen tatsächlichen OS-Reboot von +Agent-/Helperneustart und Zeitänderung unterscheiden; aktuelle wiederholte +Abfrage allein beweist das noch nicht. +[Shutdown-API und Einschränkungen](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-initiatesystemshutdownexw). + +## Dateien und Freigaben vor nativer Wirkung + +Geplante Änderungen, **hier nicht umgesetzt**: + +- `crates/domain/src/windows_execution.rs`, `jobs.rs`: strikte Manifest-/Rezept- + und Identitätsprüfung, optionale Jobfelder, erhaltene Linuxgoldenbytes. +- `crates/agent-core`: reiner Helper-Abgleich mit Plattformports; + `crates/privileged-helper`: bestehender Linuxadapter gegen unveränderte Verträge. +- `crates/agent-windows/src/{helper,pipe,service_provider,event_log,package,reboot}.rs` + und eigenes Helperbinary: nur tatsächlich gelieferte Provider anlegen, keine + leeren Stubs; SCM-/IPC-/Datei-/Clockadapter und eigenes Ledger. +- Server `jobs`, `root_policy` oder neue explizite `windows_policy`, `catalog`, + `job_gateway`, `synchronization`: Rechte/Caps an jeder Grenze und unabhängiger + Rebootabschluss; neue RLS-Migration nur für tatsächlich benötigte Windowsrezepte. +- WebUI/Schemas: vollständige unveränderliche Rezept-/Manifest-/Rebootpreview, + keine neue Genehmigungsabkürzung; dieselben Anwendungsfälle für HTTP und MCP. +- `deploy/windows`: erhaltender Helperbootstrap, kleine geprüfte Fixturequelle/ + Rezeptartefakte, geschützte native Nachweisrunner und tatsächliche Requalifikation. + +Reversible Entwicklung, Builds und Bootstrapvorbereitung sind im Auftrag. +Konkrete native Paket-/Dienstwirkungen brauchen jeweils den fertig einsehbaren +festen dev/test-UI-Plan. Ein Windows-VM-Reboot ist eine gesondert konkret zu +vereinbarende Zielaktion; der frühere einzelne Linux-Reboot erlaubt ihn nicht. +Kein pauschales „alles okay“ anfordern: zuerst Artefakt, Rezept, Ziel, Plan, +No-Reboot-Vertrag, Nachprüfung und Wiederaufnahme vorbereiten, dann nur die +wirklich fehlende konkrete Freigabe klären. Ein Installerfehler berechtigt weder +Markerreset noch zweiten Versuch. Nach jedem Abnahmeschritt dokumentieren/pushen. + +Zur vollständigen Windowsabnahme zählen außerdem bisher offene Rotation/ +Widerruf, Installer-/Signierstatus und plattformspezifische Fehlerfälle. +MSI-/EXE-Fixtureupdates sind kein Nachweis von Windows-OS-/Kernelupdates. +Ein WUA-Provider benötigt eigene COM-Threads, versionierte Scan-/Update-IDs, +Updateeinzelergebnisse und eigene Start-/Nachprüfung; solange nicht geliefert +bleibt diese Capability unsupported. Microsofts Beispiele sind keine fertigen +Produktprovider: [WUA-Vertrag](https://learn.microsoft.com/en-us/windows/win32/wua_sdk/using-the-windows-update-agent-api). +Diese Grenzen dürfen bei einem späteren „Windows vollständig“ nicht verschwinden. + +Für diesen Plan wurden ausschließlich Repositoryquellen und die verlinkten +Microsoft-Primärquellen gelesen. Keine neuen Tests, Builds, Dienste, +Installationen, Genehmigungen oder VM-/Serverkontakte ausgeführt. diff --git a/tests/windows_plan_database.py b/tests/windows_plan_database.py new file mode 100644 index 0000000..b9b06cb --- /dev/null +++ b/tests/windows_plan_database.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +"""Fixed Linux/Windows plan guards against isolated PostgreSQL; no approval/job.""" +from pathlib import Path +import subprocess +import time +import uuid + +ROOT=Path(__file__).resolve().parents[1] +NAME='rmm-windows-plan-'+uuid.uuid4().hex[:10] + + +def command(args, data=None, timeout=120): + result=subprocess.run(args,input=data,capture_output=True,timeout=timeout) + if result.returncode: + raise RuntimeError(result.stderr.decode(errors='replace')[-4000:]) + return result.stdout + + +def main(): + command(['docker','run','-d','--network','none','--name',NAME, + '-e','POSTGRES_HOST_AUTH_METHOD=trust','postgres:17.6-bookworm']) + try: + for _ in range(100): + logs=command(['docker','logs',NAME]) + ready=subprocess.run(['docker','exec',NAME,'pg_isready','-U','postgres'],capture_output=True) + if b'init process complete' in logs and ready.returncode==0:break + time.sleep(.2) + else:raise RuntimeError('ISOLATED_POSTGRES_NOT_READY') + def sql(raw): + return command(['docker','exec','-i',NAME,'psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres'],raw.encode()) + sql('CREATE ROLE rmm_app LOGIN NOINHERIT NOSUPERUSER NOBYPASSRLS;') + for path in sorted((ROOT/'migrations').glob('*.sql')):sql(path.read_text()) + output=command(['docker','run','--rm','--network','container:'+NAME, + '--read-only','--tmpfs','/tmp:rw,size=512m','-v',str(ROOT)+':/src:ro', + '-v',str(ROOT/'devstate/windows-server-contract/target')+':/target','-e','CARGO_TARGET_DIR=/target', + '-e','RMM_TEST_REPAIR_ADMIN_URL=postgres://postgres@127.0.0.1/postgres', + '-e','RMM_TEST_REPAIR_APP_URL=postgres://rmm_app@127.0.0.1/postgres', + '--entrypoint','cargo','rust-discovery-windows-tls-full:local', + 'test','--locked','--offline','-p','rmm-server','windows_plan_caps_persisted_database', + '--','--include-ignored','--nocapture'],timeout=300) + print(output.decode()) + print('PASS: real isolated PostgreSQL, fixed mixed variants/hashes, missing interpreter capability, submit/recheck, tenant denial; zero jobs/outbox/permits, no approval.') + finally: + subprocess.run(['docker','rm','-f',NAME],capture_output=True) + + +if __name__=='__main__':main() diff --git a/web/src/api.test.ts b/web/src/api.test.ts index 5e51c27..7860755 100644 --- a/web/src/api.test.ts +++ b/web/src/api.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; -import { approvalBlockers, page, PlanInput, request, session, validateInput } from './api'; +import { expectedStdout, approvalBlockers, page, PlanInput, request, session, validateInput } from './api'; afterEach(() => vi.unstubAllGlobals()); describe('JSON contract boundary', () => { it('refuses missing simulation identity metadata', () => { @@ -49,6 +49,22 @@ describe('JSON contract boundary', () => { validateInput(input); expect(input.parameters.variants).toEqual([variant]); expect(() => validateInput({ ...input, targets: { asset_ids: ['a', 'a'] } })).toThrow('eindeutige'); }); + it('binds explicitly escaped stdout CRLF before planning and leaves text unchanged', () => { + expect(expectedStdout('"Hello\\r\\n"', 'json')).toBe('Hello\r\n'); + expect(expectedStdout('Hello\n', 'text')).toBe('Hello\n'); + expect(expectedStdout('', 'json')).toBeNull(); + expect(() => expectedStdout('{}', 'json')).toThrow(); + expect(() => expectedStdout('x', 'unknown')).toThrow(); + }); + it('binds explicit Windows and mixed variants without normalizing script bytes', () => { + const windows = { os_family: 'windows', interpreter: 'powershell', encoding: 'utf-8', script_utf8: "Write-Output 'hi'\r\n" }; + const linux = { os_family: 'linux', interpreter: 'posix_sh', encoding: 'utf-8', script_utf8: "printf hi\n" }; + const input: PlanInput = { tenant_id: 't', action: 'run_script', targets: { asset_ids: ['a', 'b'] }, parameters: { variants: [linux, windows], execution_identity: 'agent_service', timeout_seconds: 60, max_output_bytes: 4096 }, schedule: { not_before: new Date().toISOString(), start_before: new Date(Date.now() + 60000).toISOString(), offline_policy: 'wait_until_deadline' }, rollout: { max_parallel: 1, halt_after_failures: 1 }, reason: 'fixed mixed plan' }; + const original = JSON.stringify(input); validateInput(input); expect(JSON.stringify(input)).toBe(original); + for (const variants of [[windows, windows], [{ ...windows, interpreter: 'pwsh' }], [{ ...windows, interpreter: 'bash' }], [{ ...windows, script_utf8: "Write-Output 'é'" }]]) expect(() => validateInput({ ...input, parameters: { ...input.parameters, variants } })).toThrow(); + expect(() => validateInput({ ...input, parameters: { ...input.parameters, execution_identity: 'root' } })).toThrow('agent_service'); + validateInput({ ...input, parameters: { ...input.parameters, variants: [{ ...windows, script_utf8: "\ufeffWrite-Output 'é'\r\n" }] } }); + }); it('accepts explicit existing dev simulation evidence but not a principal-name guess', () => { expect(session({ principal: 'sim-test', tenant_id: 't', testsimulation: true, csrf_token: 'csrf' }).principal.simulated).toBe(true); expect(() => session({ principal: 'sim-test', tenant_id: 't', csrf_token: 'csrf' })).toThrow(); diff --git a/web/src/api.ts b/web/src/api.ts index 08d5978..2028afb 100644 --- a/web/src/api.ts +++ b/web/src/api.ts @@ -60,6 +60,13 @@ export const pretty = (v: unknown) => JSON.stringify(v, null, 2); export const actions = ['run_script', 'refresh_inventory', 'query_logs', 'set_service_state', 'ensure_package', 'apply_updates', 'reboot', 'apt_native_acceptance'] as const; export type Action = typeof actions[number]; export interface PlanInput { tenant_id: string; action: Action; targets: { asset_ids: string[] }; parameters: RecordValue; artifact_ids?: string[]; schedule: { not_before: string; start_before: string; offline_policy: 'wait_until_deadline' }; rollout: { max_parallel: number; halt_after_failures: number }; reason: string } +/** Decode explicit escaped CRLF before planning; never alter approved output bytes. */ +export function expectedStdout(value: string, format: string): string | null { + if (value === '') return null; + const output: unknown = format === 'json' ? JSON.parse(value) : format === 'text' ? value : undefined; + if (typeof output !== 'string' || new TextEncoder().encode(output).length > 65536) throw new Error('stdout-Nachbedingung benötigt einen String (max.64KiB UTF-8)'); + return output; +} export function validateInput(input: PlanInput): void { if (!input.targets.asset_ids.length || new Set(input.targets.asset_ids).size !== input.targets.asset_ids.length) throw new Error('Feste eindeutige Ziele erforderlich'); if (!input.reason.trim()) throw new Error('Begründung erforderlich'); @@ -68,10 +75,15 @@ export function validateInput(input: PlanInput): void { for (const n of [input.rollout.max_parallel, input.rollout.halt_after_failures, input.parameters.timeout_seconds, input.parameters.max_output_bytes]) if (typeof n !== 'number' || !Number.isSafeInteger(n) || n < 1) throw new Error('Limits müssen positive Ganzzahlen sein'); if (input.action === 'run_script') { const variants = input.parameters.variants; - if (!Array.isArray(variants) || !variants.length || !variants.every(v => { - const variant = record(v), bytes = typeof variant.script_utf8 === 'string' ? new TextEncoder().encode(variant.script_utf8).length : 0; - return bytes > 0 && bytes <= 32768 && !String(variant.script_utf8).includes('\0') && variant.os_family === 'linux' && variant.encoding === 'utf-8' && ['posix_sh', 'bash'].includes(String(variant.interpreter)); - })) throw new Error('Exakte Linux UTF-8 Skriptbytes und Interpreter erforderlich (max. 32 KiB)'); + if (!Array.isArray(variants) || !variants.length || variants.length > 2 || !variants.every(v => { + const variant = record(v), script = typeof variant.script_utf8 === 'string' ? variant.script_utf8 : ''; + const bytes = new TextEncoder().encode(script).length; + const platform = variant.os_family === 'linux' && ['posix_sh', 'bash'].includes(String(variant.interpreter)) + || variant.os_family === 'windows' && variant.interpreter === 'powershell'; + return bytes > 0 && bytes <= 32768 && !script.includes('\0') && variant.encoding === 'utf-8' && platform + && (variant.os_family !== 'windows' || /^[\x00-\x7f]*$/.test(script) || script.startsWith('\ufeff')); + }) || new Set(variants.map(v => record(v).os_family)).size !== variants.length) throw new Error('Je Plattform genau eine passende UTF-8 Variante (max.32KiB); PowerShell5.1: ASCII oder explizites UTF-8-BOM vor Hashbildung'); + if (variants.some(v => record(v).os_family === 'windows') && input.parameters.execution_identity !== 'agent_service') throw new Error('Windows PowerShell benötigt agent_service; kein SYSTEM-/Root-Fallback'); } if (!['agent_service', 'root'].includes(String(input.parameters.execution_identity))) throw new Error('Explizite Ausführungsidentität erforderlich'); const required = (key: string) => text(input.parameters[key], `parameters.${key}`); diff --git a/web/src/main.tsx b/web/src/main.tsx index d0db092..4cc98c1 100644 --- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react'; import { createRoot } from 'react-dom/client'; -import { actions, Action, ApiError, authConfig, approvalBlockers, objectId, page, Page, PlanInput, pretty, record, RecordValue, request, session, Session, stringField, tenant, tenantPath, validateInput } from './api'; +import { actions, Action, ApiError, authConfig, approvalBlockers, expectedStdout, objectId, page, Page, PlanInput, pretty, record, RecordValue, request, session, Session, stringField, tenant, tenantPath, validateInput } from './api'; import './style.css'; import { AdminControl, Catalog, TenantControls } from './management'; import { allowed, availableActions, parseStrictJson, rootWarning, validateSensitiveInput, verifyPlanIntegrity } from './safety'; @@ -37,19 +37,20 @@ const initialParameters: Record = { run_script: {}, refresh_inventory: { scope: 'linux.base' }, query_logs: { unit: '', since: '', until: '', max_lines: 1000 }, set_service_state: { unit: '', state: 'running' }, ensure_package: { provider: 'apt', recipe_version: '', package: '', version: '', architecture: 'amd64', installation_context: 'system', apt_manifest: null }, apply_updates: { provider: 'apt', packages: [], reboot_policy: 'never', apt_manifest: null }, reboot: { reason: '', dependency_group: '', reconnect_timeout_seconds: 300 }, apt_native_acceptance: { recipe_id: 'rmm-apt-fixture-install-1.0', recipe_version: '1', apt_manifest: null }, }; function Planner({ tenantId, auth, selected, showPlan }: { tenantId: string; auth: Session; selected: string[]; showPlan: (id: string) => void }) { + const [scriptPlatform, setScriptPlatform] = useState('linux'), [mixedVariants, setMixedVariants] = useState('[]'); const [action, setAction] = useState('run_script'), [parameters, setParameters] = useState('{}'), [error, setError] = useState(null), [busy, setBusy] = useState(false); const [identity, setIdentity] = useState('agent_service'); return

Neuen unveränderlichen Plan vorbereiten

Nur fest ausgewählte Asset-IDs. Keine Ausführung durch Planung oder Antrag; jede Aktion einschließlich Live-Inventar benötigt Genehmigung.

{ e.preventDefault(); setBusy(true); setError(null); const f = new FormData(e.currentTarget); try { const common = { execution_identity: String(f.get('identity')), timeout_seconds: Number(f.get('timeout')), max_output_bytes: Number(f.get('output')) }; - const params = action === 'run_script' ? { ...common, variants: [{ os_family: 'linux', interpreter: String(f.get('interpreter')), encoding: 'utf-8', script_utf8: String(f.get('script')) }], arguments: [], expected_stdout: f.get('expected') === '' ? null : String(f.get('expected')) } : { ...record(parseStrictJson(parameters)), ...common }; + const params = action === 'run_script' ? { ...common, variants: scriptPlatform === 'mixed' ? parseStrictJson(mixedVariants) : [{ os_family: scriptPlatform, interpreter: String(f.get('interpreter')), encoding: 'utf-8', script_utf8: String(f.get('script')) }], arguments: [], expected_stdout: expectedStdout(String(f.get('expected')), String(f.get('expected_format'))) } : { ...record(parseStrictJson(parameters)), ...common }; const input: PlanInput = { tenant_id: tenantId, action, targets: { asset_ids: [...selected] }, parameters: params, schedule: { not_before: new Date(String(f.get('not_before'))).toISOString(), start_before: new Date(String(f.get('expires'))).toISOString(), offline_policy: 'wait_until_deadline' }, rollout: { max_parallel: Number(f.get('parallel')), halt_after_failures: Number(f.get('failures')) }, reason: String(f.get('reason')) }; const ids = String(f.get('artifact_ids') ?? '').split(/[\s,]+/).filter(Boolean); if (ids.length) { if (ids.length > 16 || new Set(ids).size !== ids.length || ids.some(id => !/^[0-9a-f-]{36}$/i.test(id))) throw new Error('Maximal 16 eindeutige feste Artefakt-UUIDs'); input.artifact_ids = ids; } validateInput(input); await validateSensitiveInput(input, auth); const plan = record(await request(tenantPath(tenantId, '/operation-plans'), { body: input, csrf: auth.csrf_token })); showPlan(objectId(plan, 'plan')); } catch (e) { setError(errorText(e)); } finally { setBusy(false); } }}> {action === 'apt_native_acceptance' &&

NUR SIMULIERTE DEV/TEST-NATIVABNAHME. In Vorbereitung; Backend/Helper/Scope müssen diese eigene Aktion unterstützen. Nur rmm-apt-fixture-install-1.0 oder rmm-apt-fixture-update-1.1, Rezeptversion 1, vollständiges Schema-2-Manifest. null/Platzhalter sind nicht konfiguriert, keine Produktionsaktion, keine automatische Ausführung.

} - {action === 'run_script' ? <>