Wire preserved update control, health evidence and record publication
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
//! consumption, schema reset, outcome healing or readiness transition.
|
||||
use crate::{
|
||||
clock::NativeClock, files::FileWitnesses, original::Original, participants::StableParticipant,
|
||||
retained::RetainedSet, trust::TrustState,
|
||||
retained::RetainedSet, returned::PreparedReservation, trust::TrustState,
|
||||
};
|
||||
use anyhow::{Result, ensure};
|
||||
use rmm_agent_core::{
|
||||
@@ -22,8 +22,22 @@ use rmm_updater_linux::services::StoppedServices;
|
||||
use rmm_updater_linux::{Budget, files::LedgerGuard};
|
||||
use rusqlite::{Connection, OpenFlags};
|
||||
use std::time::Duration;
|
||||
fn observation_budget() -> Result<Budget> {
|
||||
let boot = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
|
||||
.trim()
|
||||
.parse()?;
|
||||
Budget::new(boot, Duration::from_secs(5))
|
||||
}
|
||||
|
||||
const LEDGER: &str = "/var/lib/rmm-helper/consumption.sqlite";
|
||||
fn file_role(role: NativeFileRole) -> rmm_updater_handoff::FileRole {
|
||||
match role {
|
||||
NativeFileRole::AgentBinary => rmm_updater_handoff::FileRole::AgentBinary,
|
||||
NativeFileRole::HelperBinary => rmm_updater_handoff::FileRole::HelperBinary,
|
||||
NativeFileRole::FullConfig => rmm_updater_handoff::FileRole::LinuxFullConfig,
|
||||
NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin,
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OwnedGuard<'a, 'stopped> {
|
||||
os: &'a LedgerGuard<'stopped>,
|
||||
@@ -56,7 +70,7 @@ pub struct OwnedLedger<'a, 'stopped> {
|
||||
}
|
||||
impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
|
||||
pub fn open(input: OwnedInputs<'a, 'stopped>) -> Result<Self> {
|
||||
input.os.verify_for_effect(input.budget)?;
|
||||
input.os.verify_for_effect(&observation_budget()?)?;
|
||||
ensure!(
|
||||
input.os.identity().sha256()? == input.original.handoff().ledger_identity_sha256
|
||||
&& input.retained.sha256() == input.original.handoff().retained_state_sha256,
|
||||
@@ -93,12 +107,45 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
|
||||
// Replay the complete original retained chain and its durable head.
|
||||
update_handoff::read(&db, &guard, input.original.permit().grant_id)?;
|
||||
db.execute_batch("PRAGMA foreign_keys=ON")?;
|
||||
ensure!(
|
||||
db.query_row("PRAGMA foreign_keys", [], |r| r.get::<_, bool>(0))?,
|
||||
"UPDATE_LEDGER_FOREIGN_KEYS_REQUIRED"
|
||||
);
|
||||
rmm_agent_core::ledger::bounded_journal_capacity(&db)?;
|
||||
Ok(Self { db, guard })
|
||||
}
|
||||
pub fn journal(&self) -> Result<Journal> {
|
||||
update_handoff::read(&self.db, &self.guard, self.guard.original.permit().grant_id)
|
||||
}
|
||||
pub fn qualify_for_effects(&self) -> Result<()> {
|
||||
self.guard.check(&self.db)?;
|
||||
self.guard.original.check_current(
|
||||
self.guard.binding,
|
||||
self.guard.clock,
|
||||
self.guard.budget,
|
||||
)?;
|
||||
self.guard
|
||||
.trust
|
||||
.recheck(self.guard.original, self.guard.clock)?;
|
||||
self.guard
|
||||
.trust
|
||||
.qualify_history(&self.db, &self.guard, self.guard.binding)
|
||||
}
|
||||
pub fn mark_unknown(
|
||||
&mut self,
|
||||
observed_at: u64,
|
||||
reason: rmm_updater_handoff::UnknownReason,
|
||||
) -> Result<()> {
|
||||
let journal = self.journal()?;
|
||||
update_handoff::mark_unknown(
|
||||
&mut self.db,
|
||||
&self.guard,
|
||||
self.guard.original.permit(),
|
||||
journal.head_sha256(),
|
||||
observed_at,
|
||||
reason,
|
||||
)
|
||||
}
|
||||
pub fn append_observed(&mut self, evidence: Evidence) -> Result<()> {
|
||||
let journal = self.journal()?;
|
||||
let event = journal.prepare(
|
||||
@@ -125,6 +172,37 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
|
||||
)
|
||||
}
|
||||
pub fn observe_native(&mut self, claim: &NativeEffectClaim, observation: &str) -> Result<()> {
|
||||
let expected = match claim.effect() {
|
||||
NativeEffect::StopService { service } => {
|
||||
let role = match service {
|
||||
update_handoff::NativeService::Agent => {
|
||||
rmm_updater_linux::services::Service::Agent
|
||||
}
|
||||
update_handoff::NativeService::General => {
|
||||
rmm_updater_linux::services::Service::General
|
||||
}
|
||||
update_handoff::NativeService::Full => {
|
||||
rmm_updater_linux::services::Service::Full
|
||||
}
|
||||
};
|
||||
let proof = self
|
||||
.guard
|
||||
.stopped
|
||||
.proofs()
|
||||
.into_iter()
|
||||
.find(|p| p.service == role)
|
||||
.ok_or_else(|| anyhow::anyhow!("UPDATE_STOP_WITNESS_MISSING"))?;
|
||||
hash(&serde_json::to_vec(&proof)?)
|
||||
}
|
||||
NativeEffect::SavePredecessor { role } => {
|
||||
self.native_observation_sha256(file_role(*role), false)?
|
||||
}
|
||||
NativeEffect::PublishReplacement { role } => {
|
||||
self.native_observation_sha256(file_role(*role), true)?
|
||||
}
|
||||
_ => anyhow::bail!("UPDATE_OWNED_OBSERVATION_NOT_ALLOWED"),
|
||||
};
|
||||
ensure!(expected == observation, "UPDATE_NATIVE_OBSERVATION_CHANGED");
|
||||
update_handoff::observe_native_effect(
|
||||
&mut self.db,
|
||||
&self.guard,
|
||||
@@ -153,17 +231,66 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
|
||||
pub fn verify_pair(&self) -> Result<()> {
|
||||
self.guard.files.verify_pair(self.guard.budget)
|
||||
}
|
||||
pub fn inflight_proof(&self) -> Result<rmm_domain::agent_update::UpdateInflightProof> {
|
||||
let journal = self.journal()?;
|
||||
let mut statement = self.db.prepare(
|
||||
"SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence",
|
||||
)?;
|
||||
let events = statement
|
||||
.query_map([self.guard.original.permit().grant_id.to_string()], |r| {
|
||||
r.get(0)
|
||||
})?
|
||||
.collect::<rusqlite::Result<Vec<Vec<u8>>>>()?;
|
||||
rmm_domain::agent_update::UpdateInflightProof::from_raw(
|
||||
&self.guard.original.handoff().bytes()?,
|
||||
self.guard.original.delegation().clone(),
|
||||
&events,
|
||||
journal.head_sha256().into(),
|
||||
)
|
||||
.map_err(anyhow::Error::msg)
|
||||
}
|
||||
pub fn prepare_return_and_close(mut self) -> Result<PreparedReservation> {
|
||||
let journal = self.journal()?;
|
||||
ensure!(
|
||||
journal.phase() == Phase::PairVerified,
|
||||
"UPDATE_RETURN_REQUIRES_PAIR_VERIFIED"
|
||||
);
|
||||
let now = self.guard.clock.now()?;
|
||||
let event = journal.prepare(
|
||||
u64::try_from(now.timestamp())?,
|
||||
Evidence::LedgerReturnIntent,
|
||||
)?;
|
||||
let claims = update_handoff::prepare_return_and_starts(
|
||||
&mut self.db,
|
||||
&self.guard,
|
||||
self.guard.original.permit().grant_id,
|
||||
journal.head_sha256(),
|
||||
&event,
|
||||
now,
|
||||
)?;
|
||||
let proof = self.inflight_proof()?;
|
||||
let ledger = self.guard.os.identity().clone();
|
||||
let old_services = self.guard.stopped.proofs();
|
||||
self.close()?;
|
||||
Ok(PreparedReservation {
|
||||
claims,
|
||||
proof,
|
||||
ledger,
|
||||
old_services,
|
||||
})
|
||||
}
|
||||
/// Close the actual SQLite connection BEFORE returning the OS flock. An
|
||||
/// open/failed-close connection never produces a released-ledger witness.
|
||||
pub fn close(self) -> Result<()> {
|
||||
self.guard.check(&self.db)?;
|
||||
self.db.close().map_err(|(_, e)| e)?;
|
||||
self.guard.os.verify_for_effect(self.guard.budget)
|
||||
self.guard.os.verify_for_effect(&observation_budget()?)
|
||||
}
|
||||
}
|
||||
impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|
||||
fn check(&self, db: &Connection) -> Result<()> {
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
let observation = observation_budget()?;
|
||||
self.os.verify_for_effect(&observation)?;
|
||||
ensure!(
|
||||
unsafe { db.handle() } as usize == self.sqlite_handle,
|
||||
"UPDATE_SQLITE_CONNECTION_CHANGED"
|
||||
@@ -174,11 +301,16 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(path == LEDGER, "UPDATE_SQLITE_PATH_CHANGED");
|
||||
self.original
|
||||
.check_current(self.binding, self.clock, self.budget)?;
|
||||
self.trust.recheck(self.original, self.clock)?;
|
||||
Original::verify_retained(
|
||||
self.binding,
|
||||
self.original.start_bytes(),
|
||||
&self.original.handoff().bytes()?,
|
||||
self.original.delegation().clone(),
|
||||
)?;
|
||||
self.trust.recheck_protected()?;
|
||||
self.trust.recheck_original_binding(self.original)?;
|
||||
self.updater
|
||||
.verify_delegation(self.original.delegation(), self.budget)?;
|
||||
.verify_delegation(self.original.delegation(), &observation)?;
|
||||
self.retained.verify()?;
|
||||
let grant = self.original.permit().grant_id.to_string();
|
||||
let start: Vec<u8> = db.query_row(
|
||||
@@ -196,7 +328,7 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(unresolved == 0, "UPDATE_OTHER_UNRESOLVED_ATTEMPT");
|
||||
self.os.verify_for_effect(self.budget)
|
||||
self.os.verify_for_effect(&observation)
|
||||
}
|
||||
fn check_update_owner(
|
||||
&self,
|
||||
@@ -217,6 +349,9 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|
||||
&& delegation == self.original.delegation(),
|
||||
"UPDATE_OWNED_WRITER_CONTEXT_CHANGED"
|
||||
);
|
||||
self.original
|
||||
.check_current(self.binding, self.clock, self.budget)?;
|
||||
self.trust.recheck(self.original, self.clock)?;
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
self.updater.verify_delegation(delegation, self.budget)
|
||||
}
|
||||
@@ -236,8 +371,17 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
|
||||
&& delegation == self.original.delegation(),
|
||||
"UPDATE_EVENT_ORIGINAL_CHANGED"
|
||||
);
|
||||
if matches!(event.evidence, Evidence::Unknown { .. }) {
|
||||
let observation = observation_budget()?;
|
||||
self.os.verify_for_effect(&observation)?;
|
||||
self.updater.verify_delegation(delegation, &observation)?;
|
||||
self.trust.recheck_protected()?;
|
||||
self.retained.verify()?;
|
||||
return Ok(());
|
||||
}
|
||||
self.original
|
||||
.check_current(self.binding, self.clock, self.budget)?;
|
||||
self.trust.recheck(self.original, self.clock)?;
|
||||
self.updater.verify_delegation(delegation, self.budget)?;
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
self.retained.verify()?;
|
||||
@@ -304,6 +448,7 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
|
||||
);
|
||||
self.original
|
||||
.check_current(self.binding, self.clock, self.budget)?;
|
||||
self.trust.recheck(self.original, self.clock)?;
|
||||
self.updater.verify_delegation(delegation, self.budget)?;
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
self.retained.verify()?;
|
||||
@@ -314,6 +459,9 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
|
||||
NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin,
|
||||
};
|
||||
match (effect, stage) {
|
||||
(NativeEffect::StopService { .. }, NativeEffectStage::Observe) => {
|
||||
self.stopped.verify_still_stopped(self.budget)?
|
||||
}
|
||||
(NativeEffect::SavePredecessor { role: r }, NativeEffectStage::Claim) => {
|
||||
self.files.verify_original_and_stage(role(r), self.budget)?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user