//! Passive signed-release observations and retained update evidence. use anyhow::{Context, Result, ensure}; use rmm_domain::agent_update::Platform; use rmm_privileged_helper::Binding; use std::path::Path; /// Opaque public metadata authenticated by the current protected Full peer, /// with complete guarded history verified there. No caller JSON constructor. #[derive(Clone)] pub struct GuardedRecord { raw: Vec, observation: rmm_domain::agent_update::AgentUpdateObservation, } impl GuardedRecord { pub fn bytes(&self, policy_raw: &[u8], keys_raw: &[u8]) -> Result<&[u8]> { use rmm_domain::agent_update::ObservationQuality; let o = &self.observation; let age = chrono::Utc::now() - o.observed_at; let p = o .predecessor .as_ref() .context("UPDATE_CATALOG_PREDECESSOR")?; let u = o.updater.as_ref().context("UPDATE_CATALOG_UPDATER")?; ensure!( o.validate() && o.quality == ObservationQuality::Valid && o.platform == Platform::LinuxX64 && age >= chrono::Duration::zero() && age <= chrono::Duration::seconds(90) && rmm_domain::jobs::hash(policy_raw) == u.policy_sha256 && rmm_domain::jobs::hash(keys_raw) == u.keyring_sha256 && rmm_domain::jobs::hash(&self.raw) == p.installed_record_sha256, "UPDATE_CATALOG_GUARDED_RECORD" ); Ok(&self.raw) } } /// Passive cached Full observations only; never a live MCP collection. type Observation = rmm_domain::agent_update::AgentUpdateObservation; struct Observing { receiver: tokio::sync::oneshot::Receiver)>>, task: tokio::task::JoinHandle<()>, } impl Drop for Observing { fn drop(&mut self) { self.task.abort(); } } #[derive(Default)] pub struct Observer { current: Option, record: Option, pending: Option, } impl Observer { pub fn cached(&self) -> Option<&Observation> { let value = self.current.as_ref()?; let age = chrono::Utc::now() - value.observed_at; (value.validate() && age >= chrono::Duration::zero() && age <= chrono::Duration::seconds(90)) .then_some(value) } pub fn poll( &mut self, binding: Option<&Binding>, candidate: Option<(rmm_domain::agent_update::ReleaseEnvelope, u64)>, ) -> Observation { if let Some(pending) = &mut self.pending { match pending.receiver.try_recv() { Ok(Ok((value, raw))) if value.validate() => { self.record = Some(GuardedRecord { raw, observation: value.clone(), }); self.current = Some(value); self.pending = None; } Ok(_) | Err(tokio::sync::oneshot::error::TryRecvError::Closed) => { self.current = None; self.record = None; self.pending = None; } Err(tokio::sync::oneshot::error::TryRecvError::Empty) => {} } } if self.pending.is_none() && let Some(binding) = binding.cloned() { let (sender, receiver) = tokio::sync::oneshot::channel(); let task = tokio::task::spawn_blocking(move || { let (envelope, generation) = candidate.map_or((None, None), |(e, g)| (Some(e), Some(g))); let value = rmm_privileged_helper::update_admission::observe_record( Path::new("/run/rmm-helper-maintenance/control.sock"), &binding, envelope, generation, ); let _ = sender.send(value); }); self.pending = Some(Observing { receiver, task }); } self.cached() .cloned() .unwrap_or_else(|| Observation::not_configured(Platform::NATIVE_LINUX, chrono::Utc::now())) } pub fn catalog_record(&self) -> Option { self.cached()?; self.record.clone() } }