Qualify Windows system delegation and crash-safe mirror adapter on native VM
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -2,7 +2,12 @@
|
||||
//! advertised here. Each actual IPC runs on one bounded blocking lane.
|
||||
use crate::{helper_ipc as ipc, native};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use rmm_agent_core::{auth::Binding, helper_reconcile::Snapshot, reconcile::HelperSnapshot};
|
||||
use rmm_agent_core::{
|
||||
auth::{Binding, Outcome, Request},
|
||||
helper_reconcile::{NoStartAnchor, Snapshot},
|
||||
reconcile::HelperSnapshot,
|
||||
};
|
||||
use rmm_domain::jobs::{Action, JobPayload};
|
||||
use rmm_protocol::{SyncAttempt, SyncResponse};
|
||||
use serde_json::json;
|
||||
use std::{
|
||||
@@ -12,6 +17,8 @@ use std::{
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::sync::Semaphore;
|
||||
#[path = "helper_intent.rs"]
|
||||
pub mod intent;
|
||||
pub const PIN_PATH: &str = r"C:\ProgramData\RustDiscoverySystem\agent-pin.json";
|
||||
const PUBLIC_ROOT: &str = r"C:\ProgramData\RustDiscoverySystem";
|
||||
const PROBE_FILE: &str = "helper-probe.json";
|
||||
@@ -20,6 +27,16 @@ pub struct Client {
|
||||
pin: ipc::Pin,
|
||||
lane: Arc<Semaphore>,
|
||||
}
|
||||
pub struct ControlSnapshot {
|
||||
pub raw: Snapshot,
|
||||
pub no_start_anchor: Option<NoStartAnchor>,
|
||||
}
|
||||
pub struct BoundOutcome {
|
||||
pub job_id: uuid::Uuid,
|
||||
pub attempt_id: uuid::Uuid,
|
||||
pub grant_id: uuid::Uuid,
|
||||
pub outcome: Outcome,
|
||||
}
|
||||
impl Client {
|
||||
/// Missing pin is deliberately passive. A present but invalid pin is refused;
|
||||
/// no endpoint/key/policy is discovered from the pipe itself.
|
||||
@@ -72,12 +89,13 @@ impl Client {
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let _permit = permit;
|
||||
let result=(|| {
|
||||
let client=Self::discover(&agent_id,&asset_id,binding.as_ref(),lane)?;
|
||||
let client=Self::discover(&agent_id,&asset_id,binding.as_ref(),lane).context(ipc::Stage::DiscoverPin)?;
|
||||
if let Some(client)=&client {client.probe_blocking(&state)?;}
|
||||
Ok(client)
|
||||
})();
|
||||
if result.is_err() || matches!(&result,Ok(None)) && state.join(PROBE_FILE).try_exists()? {
|
||||
write_current_probe(&state,&json!({"schema_version":1,"source":"actual_local_service_runtime","agent_process_id":std::process::id(),"agent_id":agent_id,"asset_id":asset_id,"observed_at":chrono::Utc::now(),"authenticated_pipe_exchange":false,"system_capability_advertised":false,"helper_ready":false,"no_jobs":true,"code":"HELPER_NOT_AVAILABLE"}))?;
|
||||
let diagnostic=result.as_ref().err().map(ipc::diagnostic).unwrap_or(ipc::Diagnostic{stage:"discover_pin",code:"HELPER_PIN_NOT_CONFIGURED",win32:None});
|
||||
write_current_probe(&state,&json!({"schema_version":1,"source":"actual_local_service_runtime","agent_process_id":std::process::id(),"agent_id":agent_id,"asset_id":asset_id,"observed_at":chrono::Utc::now(),"authenticated_pipe_exchange":false,"system_capability_advertised":false,"helper_ready":false,"no_jobs":true,"code":"HELPER_NOT_AVAILABLE","diagnostic":diagnostic}))?;
|
||||
}
|
||||
result
|
||||
})
|
||||
@@ -100,7 +118,135 @@ impl Client {
|
||||
pub fn pin_hash(&self) -> &str {
|
||||
&self.pin.policy_sha256
|
||||
}
|
||||
pub fn helper_binary_hash(&self) -> &str {
|
||||
&self.pin.helper.binary_sha256
|
||||
}
|
||||
/// Import only independently authenticated raw helper evidence. c+start is
|
||||
/// atomic through the existing core API. A crash before outcome persistence
|
||||
/// stays locked; the next fresh snapshot repairs that exact start idempotently.
|
||||
pub fn mirror(
|
||||
&self,
|
||||
snapshot: &Snapshot,
|
||||
ledger: &mut rusqlite::Connection,
|
||||
observed_at: chrono::DateTime<chrono::Utc>,
|
||||
) -> Result<Vec<BoundOutcome>> {
|
||||
use rmm_agent_core::{crypto::DalekVerifier, ledger as core_ledger};
|
||||
use rusqlite::OptionalExtension;
|
||||
if !Path::new(intent::DB).try_exists()? {
|
||||
// Optional provisioning must not block the unprivileged provider.
|
||||
// Retained SYSTEM consumption, however, may never lose its store.
|
||||
let mut rows = ledger.prepare("SELECT start FROM execution_evidence")?;
|
||||
for row in rows.query_map([], |r| r.get::<_, Vec<u8>>(0))? {
|
||||
let value: serde_json::Value = serde_json::from_slice(&row?)?;
|
||||
let request: Request = serde_json::from_value(value["request"].clone())?;
|
||||
ensure!(
|
||||
self.pin.binding.job(&request.job_bytes)?.execution_identity != "system",
|
||||
"SYSTEM_INTENT_STORE_MISSING"
|
||||
);
|
||||
}
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let mut store = intent::Store::open(true)?;
|
||||
for intent in store.pending()? {
|
||||
self.pin.binding.verify_historical_for_denial_with(
|
||||
&intent.request,
|
||||
rmm_agent_core::auth::HelperIdentity::System,
|
||||
&DalekVerifier,
|
||||
)?;
|
||||
let job = self.pin.binding.job(&intent.request.job_bytes)?;
|
||||
let permit = intent.permit()?;
|
||||
ensure!(
|
||||
self.supports_system(&job)
|
||||
&& intent.policy_sha256 == self.pin.policy_sha256
|
||||
&& intent.helper_binary_sha256 == self.pin.helper.binary_sha256
|
||||
&& snapshot.boot_id == intent.original_boot_id
|
||||
&& snapshot.journal_id == intent.anchor.journal_id()
|
||||
&& snapshot.checkpoint >= intent.anchor.checkpoint()
|
||||
&& observed_at >= intent.recorded_at
|
||||
&& observed_at >= snapshot.captured_at,
|
||||
"SYSTEM_MIRROR_CONTEXT"
|
||||
);
|
||||
let mut matches = snapshot
|
||||
.attempts
|
||||
.iter()
|
||||
.filter(|a| a.job_id == job.job_id && a.attempt_id == job.attempt_id);
|
||||
let row = matches
|
||||
.next()
|
||||
.context("SYSTEM_DELEGATION_NO_HELPER_EVIDENCE")?;
|
||||
ensure!(matches.next().is_none(), "SYSTEM_DUPLICATE_HELPER_ATTEMPT");
|
||||
let outcome =
|
||||
verified_mirror_row(row, &job, &permit, &intent.request, intent.original_boot_id)?;
|
||||
let existing:Option<(String,u64,String,String,Vec<u8>)>=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",[permit.grant_id.to_string()],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?))).optional()?;
|
||||
if let Some((agent, generation, j, a, start)) = existing {
|
||||
let start: serde_json::Value = serde_json::from_slice(&start)?;
|
||||
let request: Request = serde_json::from_value(start["request"].clone())?;
|
||||
ensure!(
|
||||
agent == job.agent_id.to_string()
|
||||
&& generation == job.enrollment_generation
|
||||
&& j == job.job_id.to_string()
|
||||
&& a == job.attempt_id.to_string()
|
||||
&& serde_json::to_vec(&request)? == serde_json::to_vec(&intent.request)?
|
||||
&& start["previous_boot_id"] == intent.original_boot_id.to_string()
|
||||
&& serde_json::from_value::<chrono::DateTime<chrono::Utc>>(
|
||||
start["reserved_at"].clone()
|
||||
)? == intent.recorded_at,
|
||||
"SYSTEM_MIRROR_LOCAL_START_CONFLICT"
|
||||
);
|
||||
} else {
|
||||
core_ledger::begin_execution(
|
||||
ledger,
|
||||
&permit,
|
||||
&intent.request,
|
||||
intent.original_boot_id,
|
||||
intent.recorded_at,
|
||||
)?;
|
||||
}
|
||||
core_ledger::persist_outcome(ledger, &permit, &outcome)?;
|
||||
store.mirrored(intent.id, &row.outcome_sha256, observed_at)?;
|
||||
}
|
||||
let mut unknowns = Vec::new();
|
||||
for row in snapshot
|
||||
.attempts
|
||||
.iter()
|
||||
.filter(|row| row.state == "unknown")
|
||||
{
|
||||
let intent = store.by_grant(row.grant_id)?;
|
||||
self.pin.binding.verify_historical_for_denial_with(
|
||||
&intent.request,
|
||||
rmm_agent_core::auth::HelperIdentity::System,
|
||||
&DalekVerifier,
|
||||
)?;
|
||||
let job = self.pin.binding.job(&intent.request.job_bytes)?;
|
||||
let permit = intent.permit()?;
|
||||
ensure!(
|
||||
self.supports_system(&job)
|
||||
&& intent.policy_sha256 == self.pin.policy_sha256
|
||||
&& intent.helper_binary_sha256 == self.pin.helper.binary_sha256
|
||||
&& snapshot.boot_id == intent.original_boot_id
|
||||
&& snapshot.journal_id == intent.anchor.journal_id()
|
||||
&& snapshot.checkpoint >= intent.anchor.checkpoint(),
|
||||
"SYSTEM_MIRROR_CONTEXT"
|
||||
);
|
||||
let outcome =
|
||||
verified_mirror_row(row, &job, &permit, &intent.request, intent.original_boot_id)?;
|
||||
unknowns.push(BoundOutcome {
|
||||
job_id: job.job_id,
|
||||
attempt_id: job.attempt_id,
|
||||
grant_id: permit.grant_id,
|
||||
outcome,
|
||||
});
|
||||
}
|
||||
store.check()?;
|
||||
Ok(unknowns)
|
||||
}
|
||||
async fn call(&self, operation: ipc::Operation) -> Result<ipc::Reply> {
|
||||
self.call_bounded(operation, Duration::from_secs(5)).await
|
||||
}
|
||||
async fn call_bounded(
|
||||
&self,
|
||||
operation: ipc::Operation,
|
||||
timeout: Duration,
|
||||
) -> Result<ipc::Reply> {
|
||||
let permit = self
|
||||
.lane
|
||||
.clone()
|
||||
@@ -111,17 +257,20 @@ impl Client {
|
||||
let _permit = permit;
|
||||
client.check_pin()?;
|
||||
let call = ipc::Call::new(&client.pin, operation);
|
||||
let reply = ipc::exchange(&client.pin, &call, Duration::from_secs(5))?;
|
||||
let reply = ipc::exchange(&client.pin, &call, timeout)?;
|
||||
client.check_pin()?;
|
||||
Ok(reply)
|
||||
})
|
||||
.await?
|
||||
}
|
||||
fn probe_blocking(&self, state: &Path) -> Result<()> {
|
||||
self.check_pin()?;
|
||||
self.check_pin().context(ipc::Stage::DiscoverPin)?;
|
||||
let pin = &self.pin;
|
||||
let call = ipc::Call::new(pin, ipc::Operation::Probe {});
|
||||
let reply = ipc::exchange(pin, &call, Duration::from_secs(5))?;
|
||||
if let ipc::ReplyBody::Denied { code } = &reply.body {
|
||||
return Err(ipc::remote_denial(ipc::Stage::ClientReplyBinding, code));
|
||||
}
|
||||
let ipc::ReplyBody::Probe {
|
||||
clock_qualified,
|
||||
ready,
|
||||
@@ -133,22 +282,123 @@ impl Client {
|
||||
};
|
||||
let value = json!({"schema_version":1,"source":"actual_local_service_runtime","agent_process_id":std::process::id(),"agent_service":crate::SERVICE_NAME,"agent_id":pin.binding.agent_id,"asset_id":pin.binding.asset_id,"challenge":reply.challenge,"observed_at":chrono::Utc::now(),"helper_service":ipc::HELPER_SERVICE,"verified_helper_user_sid":pin.helper.user_sid,"verified_helper_service_sid":pin.helper.service_sid,"helper_binary_sha256":reply.helper_binary_sha256,"policy_sha256":reply.policy_sha256,"authenticated_pipe_exchange":true,"client_service_pid_verified_by_helper":true,"ledger_file_id":ledger,"executor_lock_file_id":lock,"helper_clock_qualified":clock_qualified,"helper_ready":ready,"system_capability_advertised":false,"no_jobs":true});
|
||||
self.check_pin()?;
|
||||
write_current_probe(state, &value)
|
||||
write_current_probe(state, &value).context(ipc::Stage::ClientReport)
|
||||
}
|
||||
pub async fn snapshot(&self, generation: u64, boot: uuid::Uuid) -> Result<HelperSnapshot> {
|
||||
/// Pure admission shape only; no capability or qualification is asserted.
|
||||
pub fn supports_system(&self, job: &JobPayload) -> bool {
|
||||
system_shape(job)
|
||||
&& self.pin.binding.tenant_id == job.tenant_id
|
||||
&& self.pin.binding.asset_id == job.asset_id
|
||||
&& self.pin.binding.agent_id == job.agent_id
|
||||
&& self.pin.binding.enrollment_generation == job.enrollment_generation
|
||||
&& self.pin.binding.assignment_version == job.assignment_version
|
||||
}
|
||||
/// Delegate ONCE. A lost reply is unknown and must be recovered by signed
|
||||
/// Snapshot/Apply, never by resending Execute. A timed-out blocking call
|
||||
/// retains its one lane permit until it actually returns; no replacement.
|
||||
pub async fn delegate(
|
||||
&self,
|
||||
intent: intent::Intent,
|
||||
admit: impl Fn() -> Result<()> + Send + 'static,
|
||||
) -> Result<()> {
|
||||
let job = self.pin.binding.job(&intent.request.job_bytes)?;
|
||||
ensure!(
|
||||
intent.connection_generation > 0 && self.supports_system(&job),
|
||||
"SYSTEM_TYPED_SERVICE_REQUIRED"
|
||||
);
|
||||
let timeout = execution_timeout(job.max_runtime_seconds)?;
|
||||
let permit = self
|
||||
.lane
|
||||
.clone()
|
||||
.try_acquire_owned()
|
||||
.context("HELPER_CONTROL_BUSY")?;
|
||||
let client = self.clone();
|
||||
tokio::time::timeout(
|
||||
timeout,
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let _permit = permit;
|
||||
client.check_pin()?;
|
||||
let mut store = intent::Store::open(true)?;
|
||||
let (retained, _) = store.load(intent.id)?;
|
||||
ensure!(
|
||||
retained.digest()? == intent.digest()?,
|
||||
"SYSTEM_INTENT_CHANGED"
|
||||
);
|
||||
let phase = store.decide(intent.id, admit().is_ok())?;
|
||||
let operation = match phase {
|
||||
intent::Phase::KnownNoDispatch => ipc::Operation::Abandon {
|
||||
anchor: Box::new(intent.anchor.clone()),
|
||||
request: intent.request.clone(),
|
||||
intent_id: intent.id,
|
||||
intent_sha256: intent.digest()?,
|
||||
},
|
||||
intent::Phase::MayHaveDispatched => {
|
||||
admit()?;
|
||||
ipc::Operation::Execute {
|
||||
generation: intent.connection_generation,
|
||||
request: intent.request.clone(),
|
||||
}
|
||||
}
|
||||
intent::Phase::Prepared => anyhow::bail!("SYSTEM_INTENT_NOT_DURABLE"),
|
||||
};
|
||||
let call = ipc::Call::new(&client.pin, operation);
|
||||
let reply = ipc::exchange(&client.pin, &call, timeout)?;
|
||||
client.check_pin()?;
|
||||
let outcome = match reply.body {
|
||||
ipc::ReplyBody::Executed { outcome }
|
||||
| ipc::ReplyBody::Abandoned { outcome } => outcome,
|
||||
_ => anyhow::bail!("SYSTEM_HELPER_DELEGATION_DENIED"),
|
||||
};
|
||||
checked_outcome(&job, &outcome)?;
|
||||
// A reply is never the consumption/mirror witness. Every outcome,
|
||||
// including a successful reply, requires a fresh raw snapshot.
|
||||
Ok::<_, anyhow::Error>(())
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.context("SYSTEM_HELPER_REPLY_UNKNOWN")??
|
||||
}
|
||||
pub async fn resume_denial(&self, intent: intent::Intent) -> Result<()> {
|
||||
let store = intent::Store::open(false)?;
|
||||
let (retained, phase) = store.load(intent.id)?;
|
||||
ensure!(
|
||||
phase == intent::Phase::KnownNoDispatch && retained.digest()? == intent.digest()?,
|
||||
"SYSTEM_DENIAL_RESUME_BINDING"
|
||||
);
|
||||
let operation = ipc::Operation::Abandon {
|
||||
anchor: Box::new(intent.anchor.clone()),
|
||||
request: intent.request.clone(),
|
||||
intent_id: intent.id,
|
||||
intent_sha256: intent.digest()?,
|
||||
};
|
||||
// Receipt is persisted by the runtime before this method. A timeout
|
||||
// cannot release the blocking lane or cause another Abandon this process.
|
||||
let reply = tokio::time::timeout(Duration::from_secs(5), self.call(operation))
|
||||
.await
|
||||
.context("SYSTEM_ABANDON_REPLY_UNKNOWN")??;
|
||||
ensure!(
|
||||
matches!(reply.body, ipc::ReplyBody::Abandoned { .. }),
|
||||
"SYSTEM_ABANDON_DENIED"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
pub async fn snapshot(&self, generation: u64, boot: uuid::Uuid) -> Result<ControlSnapshot> {
|
||||
let reply = self.call(ipc::Operation::Snapshot { generation }).await?;
|
||||
let ipc::ReplyBody::Snapshot { snapshot } = reply.body else {
|
||||
anyhow::bail!("HELPER_SNAPSHOT_DENIED");
|
||||
};
|
||||
ensure!(snapshot.boot_id == boot, "HELPER_BOOT_MISMATCH");
|
||||
Ok(map_snapshot(&snapshot))
|
||||
Ok(ControlSnapshot {
|
||||
raw: snapshot,
|
||||
no_start_anchor: None,
|
||||
})
|
||||
}
|
||||
pub async fn apply(
|
||||
&self,
|
||||
response: &SyncResponse,
|
||||
request_bytes: Vec<u8>,
|
||||
boot: uuid::Uuid,
|
||||
) -> Result<HelperSnapshot> {
|
||||
) -> Result<ControlSnapshot> {
|
||||
let reply = self
|
||||
.call(ipc::Operation::Apply {
|
||||
response: rmm_agent_core::helper_reconcile::SignedResponse {
|
||||
@@ -158,14 +408,112 @@ impl Client {
|
||||
request_bytes,
|
||||
})
|
||||
.await?;
|
||||
let ipc::ReplyBody::Applied { snapshot, .. } = reply.body else {
|
||||
let ipc::ReplyBody::Applied {
|
||||
snapshot,
|
||||
no_start_anchor,
|
||||
..
|
||||
} = reply.body
|
||||
else {
|
||||
anyhow::bail!("HELPER_APPLY_DENIED");
|
||||
};
|
||||
ensure!(snapshot.boot_id == boot, "HELPER_BOOT_MISMATCH");
|
||||
Ok(map_snapshot(&snapshot))
|
||||
if let Some(anchor) = &no_start_anchor {
|
||||
ensure!(
|
||||
anchor.journal_id() == snapshot.journal_id
|
||||
&& anchor.checkpoint() == snapshot.checkpoint
|
||||
&& Some(anchor.decision_id()) == snapshot.previous_decision_id
|
||||
&& anchor.boot_id() == boot,
|
||||
"SYSTEM_ANCHOR_SNAPSHOT_BINDING"
|
||||
);
|
||||
}
|
||||
Ok(ControlSnapshot {
|
||||
raw: snapshot,
|
||||
no_start_anchor: no_start_anchor.map(|a| *a),
|
||||
})
|
||||
}
|
||||
}
|
||||
fn map_snapshot(s: &Snapshot) -> HelperSnapshot {
|
||||
fn verified_mirror_row(
|
||||
row: &rmm_agent_core::helper_reconcile::LocalAttempt,
|
||||
job: &JobPayload,
|
||||
permit: &rmm_domain::jobs::ExecutionPermitPayload,
|
||||
request: &Request,
|
||||
boot: uuid::Uuid,
|
||||
) -> Result<Outcome> {
|
||||
use rmm_domain::{
|
||||
jobs::hash,
|
||||
reconciliation::{ReconciledOutcome, canonical_outcome_bytes},
|
||||
};
|
||||
ensure!(
|
||||
row.job_id == job.job_id
|
||||
&& row.attempt_id == job.attempt_id
|
||||
&& row.job_payload_hash == permit.job_payload_hash
|
||||
&& row.grant_id == permit.grant_id
|
||||
&& row.authorization_id == permit.authorization_id
|
||||
&& row.plan_hash == permit.plan_hash
|
||||
&& row.action == permit.action
|
||||
&& row.execution_identity == "system"
|
||||
&& row.original_boot_id == boot
|
||||
&& row.original_request_sha256 == hash(&serde_json::to_vec(request)?)
|
||||
&& row.consumption == "consumed"
|
||||
&& !row.outcome_bytes.is_empty(),
|
||||
"SYSTEM_MIRROR_ORIGINAL_TUPLE"
|
||||
);
|
||||
let canonical: ReconciledOutcome = serde_json::from_slice(&row.outcome_bytes)?;
|
||||
ensure!(
|
||||
canonical_outcome_bytes(&canonical)? == row.outcome_bytes
|
||||
&& hash(&row.outcome_bytes) == row.outcome_sha256
|
||||
&& canonical.state == row.state,
|
||||
"SYSTEM_MIRROR_CANONICAL_OUTCOME"
|
||||
);
|
||||
let outcome: Outcome = serde_json::from_slice(&row.outcome_bytes)?;
|
||||
checked_outcome(job, &outcome)?;
|
||||
Ok(outcome)
|
||||
}
|
||||
fn system_shape(job: &JobPayload) -> bool {
|
||||
job.execution_identity == "system"
|
||||
&& (1..=120).contains(&job.max_runtime_seconds)
|
||||
&& (1..=65536).contains(&job.max_output_bytes)
|
||||
&& matches!(&job.action,Action::SetServiceState{unit,state} if unit.starts_with("RmmDiscoveryFixture")&&unit.len()>"RmmDiscoveryFixture".len()&&unit.len()<=80&&unit.bytes().all(|b|b.is_ascii_alphanumeric()||b"_-".contains(&b))&&matches!(state.as_str(),"running"|"stopped"))
|
||||
}
|
||||
fn execution_timeout(seconds: u32) -> Result<Duration> {
|
||||
ensure!(
|
||||
(1..=120).contains(&seconds),
|
||||
"SYSTEM_EXECUTION_TIMEOUT_BOUND"
|
||||
);
|
||||
Ok(Duration::from_secs(u64::from(seconds) + 5))
|
||||
}
|
||||
pub(super) fn checked_outcome(job: &JobPayload, outcome: &Outcome) -> Result<()> {
|
||||
ensure!(
|
||||
matches!(
|
||||
outcome.state.as_str(),
|
||||
"succeeded" | "failed" | "cancelled" | "expired" | "unknown"
|
||||
) && !outcome.reboot_required
|
||||
&& outcome
|
||||
.stdout
|
||||
.len()
|
||||
.checked_add(outcome.stderr.len())
|
||||
.is_some_and(|n| n <= job.max_output_bytes as usize),
|
||||
"SYSTEM_EXECUTE_OUTCOME_SCOPE"
|
||||
);
|
||||
if outcome.state == "succeeded" {
|
||||
let Action::SetServiceState { unit, state } = &job.action else {
|
||||
anyhow::bail!("SYSTEM_TYPED_SERVICE_REQUIRED");
|
||||
};
|
||||
let wanted = if state == "running" { 4 } else { 1 };
|
||||
ensure!(
|
||||
outcome.exit_code == Some(0)
|
||||
&& !outcome.output_truncated
|
||||
&& outcome.evidence["postconditions_passed"] == true
|
||||
&& outcome.evidence["provider"] == "windows.scm/1"
|
||||
&& outcome.evidence["service"] == unit.as_str()
|
||||
&& outcome.evidence["requested_state"] == wanted
|
||||
&& outcome.evidence["final_status"]["state"] == wanted,
|
||||
"SYSTEM_SERVICE_POSTCONDITION_BINDING"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn map_snapshot(s: &Snapshot) -> HelperSnapshot {
|
||||
HelperSnapshot {
|
||||
journal_id: s.journal_id,
|
||||
checkpoint: s.checkpoint,
|
||||
@@ -221,3 +569,156 @@ fn write_current_probe(state: &Path, value: &serde_json::Value) -> Result<()> {
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn system_route_binds_original_request_shape_budget_and_reply_scope() {
|
||||
let id = uuid::Uuid::new_v4();
|
||||
let now = chrono::Utc::now();
|
||||
let mut job = JobPayload {
|
||||
schema_version: 1,
|
||||
tenant_id: id,
|
||||
asset_id: id,
|
||||
agent_id: id,
|
||||
enrollment_generation: 1,
|
||||
assignment_version: 1,
|
||||
job_id: id,
|
||||
attempt_id: id,
|
||||
action: Action::SetServiceState {
|
||||
unit: "RmmDiscoveryFixtureService".into(),
|
||||
state: "running".into(),
|
||||
},
|
||||
execution_identity: "system".into(),
|
||||
not_before: now,
|
||||
start_before: now + chrono::Duration::seconds(60),
|
||||
max_runtime_seconds: 30,
|
||||
max_output_bytes: 1024,
|
||||
};
|
||||
assert!(system_shape(&job));
|
||||
for identity in ["root", "agent_service"] {
|
||||
job.execution_identity = identity.into();
|
||||
assert!(!system_shape(&job));
|
||||
}
|
||||
job.execution_identity = "system".into();
|
||||
job.action = Action::SetServiceState {
|
||||
unit: "RmmDiscoveryFixtureService".into(),
|
||||
state: "restarted".into(),
|
||||
};
|
||||
assert!(!system_shape(&job));
|
||||
job.action = Action::SetServiceState {
|
||||
unit: "RmmDiscoveryWindows".into(),
|
||||
state: "stopped".into(),
|
||||
};
|
||||
assert!(!system_shape(&job));
|
||||
assert_eq!(execution_timeout(120).unwrap(), Duration::from_secs(125));
|
||||
assert!(execution_timeout(0).is_err());
|
||||
assert!(execution_timeout(121).is_err());
|
||||
let mut outcome = Outcome::unknown("LOST_REPLY");
|
||||
assert!(checked_outcome(&job, &outcome).is_ok());
|
||||
outcome.reboot_required = true;
|
||||
assert!(checked_outcome(&job, &outcome).is_err());
|
||||
outcome.reboot_required = false;
|
||||
outcome.stdout = vec![0; 1025];
|
||||
assert!(checked_outcome(&job, &outcome).is_err());
|
||||
job.action = Action::SetServiceState {
|
||||
unit: "RmmDiscoveryFixtureService".into(),
|
||||
state: "running".into(),
|
||||
};
|
||||
outcome.stdout.clear();
|
||||
outcome.state = "succeeded".into();
|
||||
outcome.exit_code = Some(0);
|
||||
assert!(checked_outcome(&job, &outcome).is_err());
|
||||
outcome.evidence = json!({"postconditions_passed":true,"provider":"windows.scm/1","service":"RmmDiscoveryFixtureService","requested_state":4,"final_status":{"state":4}});
|
||||
assert!(checked_outcome(&job, &outcome).is_ok());
|
||||
outcome.evidence["final_status"]["state"] = json!(1);
|
||||
assert!(checked_outcome(&job, &outcome).is_err());
|
||||
let request = Request {
|
||||
job_bytes: vec![b' ', b'{', b'}', b'\n'],
|
||||
permit_bytes: vec![0, 1, 2, 255],
|
||||
signature: vec![7; 64],
|
||||
plan_hash: "ab".repeat(32),
|
||||
authorization_id: id,
|
||||
};
|
||||
let operation = ipc::Operation::Execute {
|
||||
generation: 73,
|
||||
request: request.clone(),
|
||||
};
|
||||
let ipc::Operation::Execute {
|
||||
generation,
|
||||
request: retained,
|
||||
} = serde_json::from_slice(&serde_json::to_vec(&operation).unwrap()).unwrap()
|
||||
else {
|
||||
panic!("typed execute required")
|
||||
};
|
||||
assert_eq!(generation, 73);
|
||||
assert_eq!(
|
||||
serde_json::to_vec(&retained).unwrap(),
|
||||
serde_json::to_vec(&request).unwrap()
|
||||
);
|
||||
// Pure mirror-binding negatives, not a signature/OS authentication proof.
|
||||
let permit = rmm_domain::jobs::ExecutionPermitPayload {
|
||||
domain: "rmm.execution-permit".into(),
|
||||
schema_version: 1,
|
||||
grant_id: id,
|
||||
authorization_id: id,
|
||||
tenant_id: id,
|
||||
asset_id: id,
|
||||
agent_id: id,
|
||||
enrollment_generation: 1,
|
||||
assignment_version: 1,
|
||||
job_id: id,
|
||||
attempt_id: id,
|
||||
plan_hash: request.plan_hash.clone(),
|
||||
job_payload_hash: rmm_domain::jobs::hash(&request.job_bytes),
|
||||
action: "set_service_state".into(),
|
||||
execution_identity: "system".into(),
|
||||
not_before: now,
|
||||
start_before: now + chrono::Duration::seconds(60),
|
||||
max_runtime_seconds: 30,
|
||||
key_id: "cd".repeat(32),
|
||||
};
|
||||
let canonical: rmm_domain::reconciliation::ReconciledOutcome = serde_json::from_value(
|
||||
serde_json::to_value(Outcome::unknown("RETAINED_UNKNOWN")).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let bytes = rmm_domain::reconciliation::canonical_outcome_bytes(&canonical).unwrap();
|
||||
let row = rmm_agent_core::helper_reconcile::LocalAttempt {
|
||||
job_id: id,
|
||||
attempt_id: id,
|
||||
job_payload_hash: permit.job_payload_hash.clone(),
|
||||
grant_id: id,
|
||||
authorization_id: id,
|
||||
plan_hash: permit.plan_hash.clone(),
|
||||
action: permit.action.clone(),
|
||||
execution_identity: "system".into(),
|
||||
revision: 1,
|
||||
original_boot_id: id,
|
||||
consumption: "consumed".into(),
|
||||
state: "unknown".into(),
|
||||
outcome_sha256: rmm_domain::jobs::hash(&bytes),
|
||||
outcome_bytes: bytes,
|
||||
original_request_sha256: rmm_domain::jobs::hash(&serde_json::to_vec(&request).unwrap()),
|
||||
};
|
||||
assert!(verified_mirror_row(&row, &job, &permit, &request, id).is_ok());
|
||||
for field in [
|
||||
"authorization_id",
|
||||
"original_request_sha256",
|
||||
"original_boot_id",
|
||||
"outcome_sha256",
|
||||
] {
|
||||
let mut changed = serde_json::to_value(&row).unwrap();
|
||||
changed[field] = if field.ends_with("sha256") {
|
||||
json!("ef".repeat(32))
|
||||
} else {
|
||||
json!(uuid::Uuid::new_v4())
|
||||
};
|
||||
let changed = serde_json::from_value(changed).unwrap();
|
||||
assert!(
|
||||
verified_mirror_row(&changed, &job, &permit, &request, id).is_err(),
|
||||
"{field}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user