Add gated agent update planning, preserved handoff and Linux worker ports
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -0,0 +1,335 @@
|
||||
//! The one existing SQLite file, opened without CREATE only after the real
|
||||
//! OS guard confirms every reader stopped. This module performs no bootstrap,
|
||||
//! consumption, schema reset, outcome healing or readiness transition.
|
||||
use crate::{
|
||||
clock::NativeClock, files::FileWitnesses, original::Original, participants::StableParticipant,
|
||||
retained::RetainedSet, trust::TrustState,
|
||||
};
|
||||
use anyhow::{Result, ensure};
|
||||
use rmm_agent_core::{
|
||||
auth::Binding,
|
||||
helper_reconcile::ExecutorLedgerGuard,
|
||||
reconcile::SyncClock,
|
||||
update_handoff::{
|
||||
self, NativeEffect, NativeEffectClaim, NativeEffectStage, NativeFileRole, Participant,
|
||||
UpdateParticipantGuard,
|
||||
},
|
||||
};
|
||||
use rmm_domain::agent_update::UpdateDelegation;
|
||||
use rmm_domain::jobs::hash;
|
||||
use rmm_updater_handoff::{Event, Evidence, Handoff, Journal, Phase};
|
||||
use rmm_updater_linux::services::StoppedServices;
|
||||
use rmm_updater_linux::{Budget, files::LedgerGuard};
|
||||
use rusqlite::{Connection, OpenFlags};
|
||||
use std::time::Duration;
|
||||
|
||||
const LEDGER: &str = "/var/lib/rmm-helper/consumption.sqlite";
|
||||
|
||||
pub struct OwnedGuard<'a, 'stopped> {
|
||||
os: &'a LedgerGuard<'stopped>,
|
||||
original: &'a Original,
|
||||
binding: &'a Binding,
|
||||
clock: &'a NativeClock,
|
||||
budget: &'a Budget,
|
||||
updater: &'a StableParticipant,
|
||||
retained: &'a RetainedSet,
|
||||
files: &'a FileWitnesses,
|
||||
stopped: &'a StoppedServices,
|
||||
trust: &'a TrustState,
|
||||
sqlite_handle: usize,
|
||||
}
|
||||
pub struct OwnedInputs<'a, 'stopped> {
|
||||
pub os: &'a LedgerGuard<'stopped>,
|
||||
pub original: &'a Original,
|
||||
pub binding: &'a Binding,
|
||||
pub clock: &'a NativeClock,
|
||||
pub budget: &'a Budget,
|
||||
pub updater: &'a StableParticipant,
|
||||
pub retained: &'a RetainedSet,
|
||||
pub files: &'a FileWitnesses,
|
||||
pub stopped: &'a StoppedServices,
|
||||
pub trust: &'a TrustState,
|
||||
}
|
||||
pub struct OwnedLedger<'a, 'stopped> {
|
||||
db: Connection,
|
||||
guard: OwnedGuard<'a, 'stopped>,
|
||||
}
|
||||
impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
|
||||
pub fn open(input: OwnedInputs<'a, 'stopped>) -> Result<Self> {
|
||||
input.os.verify_for_effect(input.budget)?;
|
||||
ensure!(
|
||||
input.os.identity().sha256()? == input.original.handoff().ledger_identity_sha256
|
||||
&& input.retained.sha256() == input.original.handoff().retained_state_sha256,
|
||||
"UPDATE_OWNED_LEDGER_OR_STATE_BINDING"
|
||||
);
|
||||
let db = Connection::open_with_flags(
|
||||
LEDGER,
|
||||
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_NO_MUTEX,
|
||||
)?;
|
||||
db.busy_timeout(Duration::ZERO)?;
|
||||
// Per-connection durability/finite capacity, preserving DELETE journal
|
||||
// mode, all existing schema/rows, and the already held file identities.
|
||||
let mode: String = db.query_row("PRAGMA journal_mode", [], |r| r.get(0))?;
|
||||
let sync: u32 = db.query_row("PRAGMA synchronous", [], |r| r.get(0))?;
|
||||
ensure!(
|
||||
mode == "delete" && sync >= 2,
|
||||
"UPDATE_LEDGER_DURABILITY_REQUIRED"
|
||||
);
|
||||
input.files.bind(input.original.handoff())?;
|
||||
let guard = OwnedGuard {
|
||||
os: input.os,
|
||||
original: input.original,
|
||||
binding: input.binding,
|
||||
clock: input.clock,
|
||||
budget: input.budget,
|
||||
updater: input.updater,
|
||||
retained: input.retained,
|
||||
files: input.files,
|
||||
stopped: input.stopped,
|
||||
trust: input.trust,
|
||||
sqlite_handle: unsafe { db.handle() } as usize,
|
||||
};
|
||||
guard.check(&db)?;
|
||||
// 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")?;
|
||||
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 append_observed(&mut self, evidence: Evidence) -> Result<()> {
|
||||
let journal = self.journal()?;
|
||||
let event = journal.prepare(
|
||||
u64::try_from(self.guard.clock.now()?.timestamp())?,
|
||||
evidence,
|
||||
)?;
|
||||
update_handoff::append(
|
||||
&mut self.db,
|
||||
&self.guard,
|
||||
self.guard.original.permit().grant_id,
|
||||
journal.head_sha256(),
|
||||
&event,
|
||||
)
|
||||
}
|
||||
pub fn claim_native(&mut self, effect: &NativeEffect) -> Result<NativeEffectClaim> {
|
||||
let journal = self.journal()?;
|
||||
update_handoff::claim_native_effect(
|
||||
&mut self.db,
|
||||
&self.guard,
|
||||
self.guard.original.permit().grant_id,
|
||||
journal.head_sha256(),
|
||||
effect,
|
||||
self.guard.clock.now()?,
|
||||
)
|
||||
}
|
||||
pub fn observe_native(&mut self, claim: &NativeEffectClaim, observation: &str) -> Result<()> {
|
||||
update_handoff::observe_native_effect(
|
||||
&mut self.db,
|
||||
&self.guard,
|
||||
claim,
|
||||
observation,
|
||||
self.guard.clock.now()?,
|
||||
)
|
||||
}
|
||||
pub fn native_observation_sha256(
|
||||
&self,
|
||||
role: rmm_updater_handoff::FileRole,
|
||||
replaced: bool,
|
||||
) -> Result<String> {
|
||||
self.guard.files.native_observation_sha256(role, replaced)
|
||||
}
|
||||
pub fn target_sha256(&self, role: rmm_updater_handoff::FileRole) -> Result<String> {
|
||||
self.guard
|
||||
.original
|
||||
.handoff()
|
||||
.files
|
||||
.iter()
|
||||
.find(|f| f.role == role)
|
||||
.map(|f| f.new_sha256.clone())
|
||||
.ok_or_else(|| anyhow::anyhow!("UPDATE_TARGET_ROLE"))
|
||||
}
|
||||
pub fn verify_pair(&self) -> Result<()> {
|
||||
self.guard.files.verify_pair(self.guard.budget)
|
||||
}
|
||||
/// 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)
|
||||
}
|
||||
}
|
||||
impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|
||||
fn check(&self, db: &Connection) -> Result<()> {
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
ensure!(
|
||||
unsafe { db.handle() } as usize == self.sqlite_handle,
|
||||
"UPDATE_SQLITE_CONNECTION_CHANGED"
|
||||
);
|
||||
let path: String = db.query_row(
|
||||
"SELECT file FROM pragma_database_list WHERE name='main'",
|
||||
[],
|
||||
|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)?;
|
||||
self.updater
|
||||
.verify_delegation(self.original.delegation(), self.budget)?;
|
||||
self.retained.verify()?;
|
||||
let grant = self.original.permit().grant_id.to_string();
|
||||
let start: Vec<u8> = db.query_row(
|
||||
"SELECT start FROM execution_evidence WHERE grant_id=?1",
|
||||
[&grant],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(
|
||||
start == self.original.start_bytes(),
|
||||
"UPDATE_RETAINED_START_CHANGED"
|
||||
);
|
||||
let unresolved: u64 = db.query_row(
|
||||
"SELECT count(*) FROM consumed WHERE result IS NULL AND grant_id<>?1",
|
||||
[grant],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(unresolved == 0, "UPDATE_OTHER_UNRESOLVED_ATTEMPT");
|
||||
self.os.verify_for_effect(self.budget)
|
||||
}
|
||||
fn check_update_owner(
|
||||
&self,
|
||||
handoff: &Handoff,
|
||||
delegation: &UpdateDelegation,
|
||||
phase: Phase,
|
||||
_events: &[Event],
|
||||
) -> Result<()> {
|
||||
ensure!(
|
||||
!matches!(
|
||||
phase,
|
||||
Phase::HandoffConsumed
|
||||
| Phase::ServicesStarted
|
||||
| Phase::HealthVerified
|
||||
| Phase::OutcomePersisted
|
||||
| Phase::Unknown
|
||||
) && handoff.bytes()? == self.original.handoff().bytes()?
|
||||
&& delegation == self.original.delegation(),
|
||||
"UPDATE_OWNED_WRITER_CONTEXT_CHANGED"
|
||||
);
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
self.updater.verify_delegation(delegation, self.budget)
|
||||
}
|
||||
}
|
||||
impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
|
||||
fn participant(&self) -> Participant {
|
||||
Participant::StableUpdater
|
||||
}
|
||||
fn verify_update_event(
|
||||
&self,
|
||||
handoff: &Handoff,
|
||||
delegation: &UpdateDelegation,
|
||||
event: &Event,
|
||||
) -> Result<()> {
|
||||
ensure!(
|
||||
handoff.bytes()? == self.original.handoff().bytes()?
|
||||
&& delegation == self.original.delegation(),
|
||||
"UPDATE_EVENT_ORIGINAL_CHANGED"
|
||||
);
|
||||
self.original
|
||||
.check_current(self.binding, self.clock, self.budget)?;
|
||||
self.updater.verify_delegation(delegation, self.budget)?;
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
self.retained.verify()?;
|
||||
match &event.evidence {
|
||||
Evidence::AllStopped { observation_sha256 } => {
|
||||
self.stopped.verify_still_stopped(self.budget)?;
|
||||
ensure!(
|
||||
*observation_sha256 == hash(&serde_json::to_vec(&self.stopped.proofs())?),
|
||||
"UPDATE_ALL_STOPPED_OBSERVATION"
|
||||
);
|
||||
}
|
||||
Evidence::UpdaterOwnsLedger {
|
||||
process,
|
||||
ledger_identity_sha256,
|
||||
} => ensure!(
|
||||
process.pid == delegation.updater.pid
|
||||
&& process.birth == delegation.updater.birth
|
||||
&& process.image_sha256 == delegation.updater.image_sha256
|
||||
&& *ledger_identity_sha256 == self.os.identity().sha256()?,
|
||||
"UPDATE_OWNER_EVENT_OBSERVATION"
|
||||
),
|
||||
Evidence::ReplaceIntent { role } => {
|
||||
self.files.verify_original_and_stage(*role, self.budget)?
|
||||
}
|
||||
Evidence::Replaced {
|
||||
role,
|
||||
observed_sha256,
|
||||
} => {
|
||||
self.files.verify_replaced(*role, self.budget)?;
|
||||
ensure!(
|
||||
handoff
|
||||
.files
|
||||
.iter()
|
||||
.any(|f| f.role == *role && f.new_sha256 == *observed_sha256),
|
||||
"UPDATE_REPLACED_OBSERVATION"
|
||||
);
|
||||
}
|
||||
Evidence::PairVerified {
|
||||
pair,
|
||||
retained_state_sha256,
|
||||
} => {
|
||||
self.files.verify_pair(self.budget)?;
|
||||
ensure!(
|
||||
*pair == handoff.target && retained_state_sha256 == self.retained.sha256(),
|
||||
"UPDATE_PAIR_OBSERVATION"
|
||||
);
|
||||
}
|
||||
Evidence::LedgerReturnIntent => self.files.verify_pair(self.budget)?,
|
||||
_ => anyhow::bail!("UPDATE_EVENT_WRITER_NOT_ALLOWED"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn verify_native_effect(
|
||||
&self,
|
||||
handoff: &Handoff,
|
||||
delegation: &UpdateDelegation,
|
||||
effect: &NativeEffect,
|
||||
stage: NativeEffectStage,
|
||||
) -> Result<()> {
|
||||
ensure!(
|
||||
handoff.bytes()? == self.original.handoff().bytes()?
|
||||
&& delegation == self.original.delegation(),
|
||||
"UPDATE_NATIVE_ORIGINAL_CHANGED"
|
||||
);
|
||||
self.original
|
||||
.check_current(self.binding, self.clock, self.budget)?;
|
||||
self.updater.verify_delegation(delegation, self.budget)?;
|
||||
self.os.verify_for_effect(self.budget)?;
|
||||
self.retained.verify()?;
|
||||
let role = |role: &NativeFileRole| 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,
|
||||
};
|
||||
match (effect, stage) {
|
||||
(NativeEffect::SavePredecessor { role: r }, NativeEffectStage::Claim) => {
|
||||
self.files.verify_original_and_stage(role(r), self.budget)?
|
||||
}
|
||||
(NativeEffect::SavePredecessor { role: r }, NativeEffectStage::Observe)
|
||||
| (NativeEffect::PublishReplacement { role: r }, NativeEffectStage::Claim) => {
|
||||
self.files.verify_saved(role(r), self.budget)?
|
||||
}
|
||||
(NativeEffect::PublishReplacement { role: r }, NativeEffectStage::Observe) => {
|
||||
self.files.verify_replaced(role(r), self.budget)?
|
||||
}
|
||||
(NativeEffect::ReturnLedger, NativeEffectStage::Claim)
|
||||
| (NativeEffect::StartService { .. }, NativeEffectStage::ReserveFutureStart) => {
|
||||
self.files.verify_pair(self.budget)?
|
||||
}
|
||||
_ => anyhow::bail!("UPDATE_NATIVE_OWNED_EFFECT_DENIED"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user