Add gated agent update planning, preserved handoff and Linux worker ports
Rust development slice / linux (push) Failing after 6s

This commit is contained in:
2026-11-14 21:48:21 +01:00
parent dc9cb1b096
commit 1e89c4577f
82 changed files with 25052 additions and 69 deletions
+4
View File
@@ -18,9 +18,13 @@ serde_json.workspace = true
uuid.workspace = true
rmm-domain = { path = "../domain" }
rmm-protocol = { path = "../protocol" }
rmm-updater-handoff = { path = "../updater-handoff" }
rusqlite = { version = "0.32.1", features = ["bundled"] }
ed25519-dalek = "=2.2.0"
[dev-dependencies]
base64 = "=0.22.1"
[target.'cfg(unix)'.dependencies]
openssl = { workspace = true, optional = true }
+5
View File
@@ -121,6 +121,11 @@ impl Binding {
&& p.job_id == job.job_id
&& p.attempt_id == job.attempt_id
&& p.action == job.action.name()
&& if matches!(job.action, rmm_domain::jobs::Action::UpdateAgent { .. }) {
p.agent_update_plan_id.is_some_and(|id| !id.is_nil())
} else {
p.agent_update_plan_id.is_none()
}
&& p.execution_identity == job.execution_identity
&& p.max_runtime_seconds == job.max_runtime_seconds,
"PERMIT_BINDING"
+280 -10
View File
@@ -26,6 +26,23 @@ pub trait BootSource {
/// The guard remains held through snapshot/apply/admission/consumption/effect.
pub trait ExecutorLedgerGuard {
fn check(&self, db: &Connection) -> Result<()>;
/// Only the specifically provisioned update authorizer may judge a live
/// owner. General readers stay closed without changing the original attempt.
fn is_update_owner_authority(&self) -> bool {
false
}
/// Default-deny live owner attestation for the one typed update handoff.
/// Implement only after qualifying fixed service/PID/birth/image and the
/// original immutable delegation under this SAME actual executor guard.
fn check_update_owner(
&self,
_handoff: &rmm_updater_handoff::Handoff,
_delegation: &rmm_domain::agent_update::UpdateDelegation,
_phase: rmm_updater_handoff::Phase,
_events: &[rmm_updater_handoff::Event],
) -> Result<()> {
anyhow::bail!("UPDATE_OWNER_NOT_QUALIFIED")
}
}
/// OS adapter contract, not an IPC assertion. The implementation must pin the
/// live helper PID/creation and ledger/lock file identities, and independently
@@ -120,6 +137,8 @@ pub struct LocalAttempt {
pub outcome_bytes: Vec<u8>,
/// Hash of the exact root-owned Request containing original job/permit bytes.
pub original_request_sha256: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub update_handoff_bytes: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
@@ -238,6 +257,78 @@ impl<V: Ed25519Verifier> HelperSync<V> {
self.verify_no_start_anchor(db, guard, &anchor, clock, boot)?;
Ok(anchor)
}
/// Consume exactly the approved update attempt and delegate its ORIGINAL
/// effect once. This is the only entry into update handoff persistence;
/// the independent live helper gate and all existing checks remain intact.
pub fn begin_update_handoff(
&mut self,
db: &mut Connection,
guard: &impl crate::update_handoff::UpdateAdmissionGuard,
generation: u64,
prepared: &crate::update_handoff::PreparedHandoff<'_>,
clock: &impl Clock,
boot: &impl BootSource,
) -> Result<()> {
let crate::update_handoff::PreparedHandoff {
request,
handoff,
recorded_at,
..
} = *prepared;
ensure!(
self.can_start(db, guard, generation, clock, boot),
"UPDATE_HELPER_READY_REQUIRED"
);
let now = clock.now()?;
ensure!(
recorded_at <= now && now - recorded_at <= chrono::Duration::seconds(1),
"UPDATE_STALE_PREPARED_HANDOFF"
);
let (job, permit) = self.binding.verify_with(request, now, &self.verifier)?;
ensure!(
matches!(job.action, Action::UpdateAgent { .. })
&& job.execution_identity == self.identity.as_str(),
"UPDATE_HELPER_IDENTITY"
);
let meta = metadata(db)?;
let lease = self
.lease
.as_ref()
.context("UPDATE_HELPER_READY_REQUIRED")?;
ensure!(
handoff.helper_journal_id == meta.journal_id
&& handoff.helper_checkpoint == meta.checkpoint
&& Some(handoff.agent_journal_id) == meta.agent_journal
&& Some(handoff.previous_decision_id) == meta.decision
&& handoff.previous_decision_id == lease.decision_id
&& handoff.connection_generation == generation
&& handoff.boot_id == boot.current_boot()?,
"UPDATE_HANDOFF_SYNC_BINDING"
);
let public_key = guard.health_public_key()?;
ensure!(
rmm_domain::agent_update::job_health_public_key(&job).map_err(anyhow::Error::msg)?
== public_key,
"UPDATE_HEALTH_PIN_BINDING"
);
crate::update_handoff::consume(db, guard, &permit, prepared, public_key, &|tx| {
let fresh_now = clock.now()?;
ensure!(
self.can_start(tx, guard, generation, clock, boot)
&& fresh_now >= recorded_at
&& fresh_now - recorded_at <= chrono::Duration::seconds(1)
&& boot.current_boot()? == handoff.boot_id,
"UPDATE_ADMISSION_CHANGED_BEFORE_CONSUMPTION"
);
let (_, fresh) = self
.binding
.verify_with(request, fresh_now, &self.verifier)?;
ensure!(fresh == permit, "UPDATE_ORIGINAL_GRANT_CHANGED");
Ok(())
})?;
self.close();
Ok(())
}
/// Deny-only: the grant may be expired, but no historical timestamp is ever
/// passed to an execute verifier. Existing consumption is immutable. A new
/// c/start/no-effect outcome is committed atomically and remains locked for
@@ -410,7 +501,18 @@ impl<V: Ed25519Verifier> HelperSync<V> {
ensure!(meta.checkpoint == highest, "HELPER_CHECKPOINT_ROLLBACK");
ensure!(generation >= meta.generation, "STALE_HELPER_CONNECTION");
validate_history(&tx, &self.binding, &meta, generation, &self.verifier)?;
let attempts = load_rows(&tx, &self.binding, true, self.identity, &self.verifier)?;
let attempts = load_rows(
&tx,
&self.binding,
true,
self.identity,
&self.verifier,
Some(LiveUpdateContext {
guard,
now,
boot: boot_id,
}),
)?;
ensure!(attempts.len() <= 256, "HELPER_SYNC_CAPACITY");
let fingerprint = ledger_fingerprint(&tx)?;
// Two independently gated profiles share one signed helper checkpoint.
@@ -512,6 +614,14 @@ impl<V: Ed25519Verifier> HelperSync<V> {
.as_ref()
.context("FRESH_PROCESS_SNAPSHOT_REQUIRED")?;
let decision = verify(&self.verifier, &self.binding, response, now)?;
ensure!(
!decision
.attempts
.iter()
.any(|d| d.update_inflight.is_some())
|| (!decision.ready && !decision.helper_ready),
"UPDATE_INFLIGHT_CANNOT_READY"
);
let request = RequestView::decode(raw_request)?;
let current_boot = boot.current_boot()?;
ensure!(
@@ -571,7 +681,7 @@ impl<V: Ed25519Verifier> HelperSync<V> {
if let Some(old)=tx.query_row("SELECT response,request,fingerprint FROM helper_sync_decisions WHERE decision_id=?1",[decision.decision_id.to_string()],|r|Ok((r.get::<_,Vec<u8>>(0)?,r.get::<_,Vec<u8>>(1)?,r.get::<_,String>(2)?))).optional()? {
ensure!(old.0==serde_json::to_vec(response)? && old.1==raw_request && meta.decision==Some(decision.decision_id)
&& ledger_fingerprint(&tx)?==old.2,"HELPER_REPLAY_OR_LEDGER_CHANGED");
let rows=load_rows(&tx,&self.binding,false, self.identity, &self.verifier)?;
let rows=load_rows(&tx,&self.binding,false, self.identity, &self.verifier,Some(LiveUpdateContext {guard,now,boot:current_boot}))?;
let ready=decision.ready && decision.helper_ready && rows.iter().all(|row|definitive(row)
&& decision.attempts.iter().any(|d|d.job_id==row.job_id && d.attempt_id==row.attempt_id
&& d.outcome_state==row.state && d.outcome_sha256.as_deref()==Some(row.outcome_sha256.as_str())))
@@ -591,7 +701,18 @@ impl<V: Ed25519Verifier> HelperSync<V> {
ledger_fingerprint(&tx)? == capture.fingerprint,
"LOCAL_LEDGER_CHANGED_DURING_SYNC"
);
let rows = load_rows(&tx, &self.binding, false, self.identity, &self.verifier)?;
let rows = load_rows(
&tx,
&self.binding,
false,
self.identity,
&self.verifier,
Some(LiveUpdateContext {
guard,
now,
boot: current_boot,
}),
)?;
ensure!(
serde_json::to_vec(&rows)? == serde_json::to_vec(&capture.snapshot.attempts)?,
"LOCAL_SNAPSHOT_CHANGED_DURING_SYNC"
@@ -658,7 +779,18 @@ impl<V: Ed25519Verifier> HelperSync<V> {
!decision.attempts.iter().any(|d| recovery_reason(&d.reason)),
"SIGNED_LEDGER_RECOVERY_REQUIRED"
);
let after = load_rows(&tx, &self.binding, true, self.identity, &self.verifier)?;
let after = load_rows(
&tx,
&self.binding,
true,
self.identity,
&self.verifier,
Some(LiveUpdateContext {
guard,
now,
boot: current_boot,
}),
)?;
let fingerprint = ledger_fingerprint(&tx)?;
tx.execute("INSERT INTO helper_sync_decisions(decision_id,checkpoint,response,request,fingerprint) VALUES(?1,?2,?3,?4,?5)",params![decision.decision_id.to_string(),decision.helper_checkpoint,serde_json::to_vec(response)?,raw_request,fingerprint])?;
tx.execute("UPDATE helper_sync_meta SET last_decision=?1,fault=COALESCE(fault,?2),agent_journal_id=?3,agent_checkpoint=?4 WHERE id=1",params![decision.decision_id.to_string(),fault,decision.journal_id.to_string(),decision.checkpoint])?;
@@ -988,7 +1120,7 @@ pub fn verify_checkpoint_repair_history_with(
previous_checkpoint = checkpoint;
previous_agent_checkpoint = decision.checkpoint;
}
let _ = load_rows(db, binding, false, identity, verifier)?;
let _ = load_rows(db, binding, false, identity, verifier, None)?;
if let Some((r, e)) = links {
verify_checkpoint_repair_links(db, binding, r, e, verifier)?;
}
@@ -1129,18 +1261,62 @@ fn ledger_rows(db: &Connection) -> Result<Vec<LedgerRow>> {
.collect::<rusqlite::Result<_>>()?)
}
fn ledger_fingerprint(db: &Connection) -> Result<String> {
Ok(hash(&serde_json::to_vec(&ledger_rows(db)?)?))
let rows = ledger_rows(db)?;
let exists:bool=db.query_row("SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='agent_update_handoffs')",[],|r|r.get(0))?;
if exists {
let mut statement=db.prepare("SELECT h.grant_id,h.record_sha256,h.head_sha256,h.event_count,h.phase,d.witness_sha256 FROM agent_update_handoffs h JOIN agent_update_delegations d USING(grant_id) ORDER BY h.grant_id")?;
type UpdateRow = (String, String, String, u32, String, String);
let updates = statement
.query_map([], |r| {
Ok((
r.get(0)?,
r.get(1)?,
r.get(2)?,
r.get(3)?,
r.get(4)?,
r.get(5)?,
))
})?
.collect::<rusqlite::Result<Vec<UpdateRow>>>()?;
if !updates.is_empty() {
let mut claims=db.prepare("SELECT grant_id,effect_key,claim,claim_sha256 FROM agent_update_native_claims ORDER BY grant_id,effect_key")?;
type NativeClaimRow = (String, String, Vec<u8>, String);
let claims = claims
.query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)))?
.collect::<rusqlite::Result<Vec<NativeClaimRow>>>()?;
let mut observations=db.prepare("SELECT grant_id,effect_key,claim_sha256,observation_sha256,observed_at FROM agent_update_native_observations ORDER BY grant_id,effect_key")?;
type NativeObservationRow = (String, String, String, String, String);
let observations = observations
.query_map([], |r| {
Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?))
})?
.collect::<rusqlite::Result<Vec<NativeObservationRow>>>()?;
return Ok(hash(&serde_json::to_vec(&(
rows,
updates,
claims,
observations,
))?));
}
}
Ok(hash(&serde_json::to_vec(&rows)?))
}
fn canonical(bytes: &[u8]) -> Result<Vec<u8>> {
let o: ReconciledOutcome = serde_json::from_slice(bytes)?;
Ok(canonical_outcome_bytes(&o)?)
}
struct LiveUpdateContext<'a> {
guard: &'a dyn ExecutorLedgerGuard,
now: DateTime<Utc>,
boot: Uuid,
}
fn load_rows(
db: &Connection,
binding: &Binding,
refresh: bool,
identity: HelperIdentity,
verifier: &impl Ed25519Verifier,
live: Option<LiveUpdateContext<'_>>,
) -> Result<Vec<LocalAttempt>> {
let mut result = Vec::new();
for (grant, agent, generation, job_id, attempt_id, finished, start, last) in ledger_rows(db)? {
@@ -1169,15 +1345,66 @@ fn load_rows(
if let (Some(a), Some(b)) = (&finished, &last) {
ensure!(canonical(a)? == canonical(b)?, "LOCAL_OUTCOME_CONFLICT");
}
let outcome_bytes = last
let mut outcome_bytes = last
.as_deref()
.or(finished.as_deref())
.map(canonical)
.transpose()?
.unwrap_or_default();
ensure!(outcome_bytes.len() <= 512 * 1024, "LOCAL_OUTCOME_SIZE");
let mut update_proof =
if outcome_bytes.is_empty() && matches!(job.action, Action::UpdateAgent { .. }) {
crate::update_handoff::proof_locked(db, permit.grant_id)?
} else {
None
};
if let Some(proof) = &update_proof {
let key = rmm_domain::agent_update::job_health_public_key(&job)
.map_err(anyhow::Error::msg)?;
let (handoff, journal) = crate::update_handoff::verify_inflight_proof(proof, key)?;
if let Some(live) = &live {
ensure!(
live.guard.is_update_owner_authority(),
"UPDATE_OWNER_AUTHORITY_REQUIRED"
);
let decoded = proof.decode().map_err(anyhow::Error::msg)?;
let events = decoded
.events
.iter()
.map(|raw| serde_json::from_slice::<rmm_updater_handoff::Event>(raw))
.collect::<std::result::Result<Vec<_>, _>>()?;
let reason = if live.boot != handoff.boot_id {
Some(rmm_updater_handoff::UnknownReason::BootChanged)
} else if u64::try_from(live.now.timestamp())? >= handoff.finish_before {
Some(rmm_updater_handoff::UnknownReason::DeadlineExpired)
} else if live
.guard
.check_update_owner(&handoff, &proof.delegation, journal.phase(), &events)
.is_err()
{
Some(rmm_updater_handoff::UnknownReason::OwnerLost)
} else {
None
};
if let Some(reason) = reason {
ensure!(refresh, "UPDATE_OWNER_LOST_FRESH_CAPTURE_REQUIRED");
outcome_bytes = crate::update_handoff::mark_unknown_locked(
db,
&permit,
u64::try_from(live.now.timestamp())?,
reason,
)?;
update_proof = None;
}
}
}
let state = if outcome_bytes.is_empty() {
"unknown".to_string()
if update_proof.is_some() {
"running"
} else {
"unknown"
}
.to_string()
} else {
let o: ReconciledOutcome = serde_json::from_slice(&outcome_bytes)?;
o.state
@@ -1185,7 +1412,13 @@ fn load_rows(
ensure!(
matches!(
state.as_str(),
"unknown" | "awaiting_reboot" | "succeeded" | "failed" | "cancelled" | "expired"
"unknown"
| "awaiting_reboot"
| "succeeded"
| "failed"
| "cancelled"
| "expired"
| "running"
),
"UNKNOWN_LOCAL_STATE"
);
@@ -1210,6 +1443,10 @@ fn load_rows(
}
}
let mut row = LocalAttempt {
update_handoff_bytes: update_proof
.map(|p| p.bytes().map_err(anyhow::Error::msg))
.transpose()?
.unwrap_or_default(),
job_id: job.job_id,
attempt_id: job.attempt_id,
job_payload_hash: hash(&request.job_bytes),
@@ -1280,12 +1517,37 @@ fn validate_summary(row: &LocalAttempt, a: &AttemptView) -> Result<()> {
&& a.revision >= row.revision
&& a.original_boot_id == row.original_boot_id.to_string()
&& a.outcome_sha256 == row.outcome_sha256
&& a.update_handoff_bytes == row.update_handoff_bytes
&& (a.outcome_bytes.is_empty() || a.outcome_bytes == row.outcome_bytes),
"ROOT_SNAPSHOT_REQUEST_MISMATCH"
);
Ok(())
}
fn validate_decision(row: &LocalAttempt, a: &AttemptView, d: &AttemptDecision) -> Result<()> {
if !row.update_handoff_bytes.is_empty() {
let proof =
rmm_domain::agent_update::UpdateInflightProof::from_bytes(&row.update_handoff_bytes)
.map_err(anyhow::Error::msg)?;
let raw = proof.decode().map_err(anyhow::Error::msg)?;
let h: rmm_updater_handoff::Handoff = serde_json::from_slice(&raw.handoff_record)?;
let expected = rmm_domain::agent_update::UpdateInflightDecision {
handoff_sha256: proof.delegation.handoff_sha256.clone(),
delegation_sha256: hash(&serde_json::to_vec(&proof.delegation)?),
head_sha256: proof.head_sha256,
owner_nonce: h.owner_nonce,
finish_before: h.finish_before,
};
ensure!(
d.update_inflight.as_ref() == Some(&expected)
&& d.outcome_state == "running"
&& !d.unlock
&& d.outcome_bytes.is_empty()
&& d.outcome_sha256.is_none(),
"UPDATE_SIGNED_INFLIGHT_BINDING"
);
} else {
ensure!(d.update_inflight.is_none(), "UPDATE_INFLIGHT_NOT_LOCAL");
}
ensure!(
d.job_payload_hash == row.job_payload_hash
&& d.grant_id == Some(row.grant_id)
@@ -1389,6 +1651,7 @@ struct AttemptView {
original_boot_id: String,
outcome_sha256: String,
outcome_bytes: Vec<u8>,
update_handoff_bytes: Vec<u8>,
}
enum Field<'a> {
Number(u64),
@@ -1517,7 +1780,7 @@ impl AttemptView {
let mut seen = BTreeSet::new();
for (tag, value) in fields(raw)? {
ensure!(
(1..=13).contains(&tag) && seen.insert(tag),
(1..=14).contains(&tag) && seen.insert(tag),
"PROTOBUF_ATTEMPT_FIELD"
);
match tag {
@@ -1540,6 +1803,13 @@ impl AttemptView {
r.outcome_bytes = bytes(value)?;
ensure!(r.outcome_bytes.len() <= 512 * 1024, "SYNC_OUTCOME_SIZE");
}
14 => {
r.update_handoff_bytes = bytes(value)?;
rmm_domain::agent_update::UpdateInflightProof::from_bytes(
&r.update_handoff_bytes,
)
.map_err(anyhow::Error::msg)?;
}
_ => unreachable!(),
}
}
+20 -8
View File
@@ -42,7 +42,7 @@ pub fn consume(db: &mut rusqlite::Connection, p: &ExecutionPermitPayload) -> Res
tx.commit()?;
Ok(())
}
fn consume_in(db: &rusqlite::Connection, p: &ExecutionPermitPayload) -> Result<()> {
pub(crate) fn consume_in(db: &rusqlite::Connection, p: &ExecutionPermitPayload) -> Result<()> {
let unresolved: i64 = db.query_row(
"SELECT count(*) FROM consumed WHERE result IS NULL",
[],
@@ -182,7 +182,7 @@ pub(crate) fn record_no_start_in(
persist_outcome_in(db, p, &outcome)?;
Ok(outcome)
}
fn validate_start_request(p: &ExecutionPermitPayload, request: &Request) -> Result<()> {
pub(crate) fn validate_start_request(p: &ExecutionPermitPayload, request: &Request) -> Result<()> {
ensure!(
request.job_bytes.len() <= 65536
&& request.permit_bytes.len() <= 8192
@@ -227,7 +227,7 @@ pub fn persist_outcome(
tx.commit()?;
Ok(())
}
fn persist_outcome_in(
pub(crate) fn persist_outcome_in(
db: &rusqlite::Connection,
p: &ExecutionPermitPayload,
outcome: &Outcome,
@@ -310,6 +310,22 @@ pub fn save_start(
previous_boot_id: Uuid,
recorded_at: DateTime<Utc>,
) -> Result<()> {
let bytes = start_witness_bytes(permit, request, previous_boot_id, recorded_at)?;
db.execute(
"INSERT INTO execution_evidence(grant_id,start) VALUES(?1,?2)",
rusqlite::params![permit.grant_id.to_string(), bytes],
)?;
Ok(())
}
/// Exact existing start codec, also used to bind the immutable update handoff
/// before atomic consumption. This does not persist or authorize anything.
pub fn start_witness_bytes(
_permit: &ExecutionPermitPayload,
request: &Request,
previous_boot_id: Uuid,
recorded_at: DateTime<Utc>,
) -> Result<Vec<u8>> {
let job: JobPayload = serde_json::from_slice(&request.job_bytes)?;
ensure!(!previous_boot_id.is_nil(), "ORIGINAL_BOOT_MISSING");
let deadline = match job.action {
@@ -322,9 +338,5 @@ pub fn save_start(
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(())
Ok(serde_json::to_vec(&evidence)?)
}
+1
View File
@@ -8,3 +8,4 @@ pub mod helper_reconcile;
pub mod jobs;
pub mod ledger;
pub mod reconcile;
pub mod update_handoff;
+200 -4
View File
@@ -142,6 +142,66 @@ impl<V: Ed25519Verifier> ClientSync<V> {
gate.helper = None;
Ok(())
}
/// Typed read-only ACK for exactly the own original update handoff. It
/// clears only the control delivery acknowledgement; both start flags stay
/// false and can_start remains false for ALL jobs, including this attempt.
pub fn confirm_update_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!(
!gate.ready
&& !gate.helper_ready
&& snapshot.journal_id == expected.0
&& snapshot.checkpoint == expected.1
&& snapshot.previous_decision_id == Some(expected.2),
"UPDATE_CONTROL_NOT_APPLIED"
);
let updates: Vec<_> = snapshot
.attempts
.iter()
.filter(|a| !a.update_handoff_bytes.is_empty())
.collect();
ensure!(updates.len() == 1, "UPDATE_CONTROL_EXACTLY_ONE_REQUIRED");
let local = updates[0];
let requested = gate
.request
.attempts
.iter()
.find(|a| a.job_id == local.job_id && a.attempt_id == local.attempt_id)
.context("UPDATE_CONTROL_NOT_REQUESTED")?;
ensure!(
local.state == "running"
&& local.consumption == "consumed"
&& local.outcome_bytes.is_empty()
&& local.update_handoff_bytes == requested.update_handoff_bytes
&& local.grant_id == requested.grant_id
&& local.job_payload_hash == requested.job_payload_hash,
"UPDATE_CONTROL_CHANGED"
);
let decision: ReconcileDecisionPayload =
serde_json::from_slice(&gate.response.decision_bytes)?;
ensure!(
decision
.attempts
.iter()
.any(|d| d.job_id.to_string() == local.job_id
&& d.attempt_id.to_string() == local.attempt_id
&& d.update_inflight.is_some()
&& !d.unlock),
"UPDATE_CONTROL_NOT_SIGNED"
);
ensure!(
snapshot
.attempts
.iter()
.filter(|a| a.job_id != local.job_id || a.attempt_id != local.attempt_id)
.all(|a| terminal(&a.state)
|| (a.state == "accepted" && a.consumption == "unconsumed")),
"UPDATE_OTHER_EFFECT_UNRESOLVED"
);
gate.helper = None;
Ok(())
}
pub fn prepare(
&mut self,
db: &mut Connection,
@@ -216,7 +276,7 @@ impl<V: Ed25519Verifier> ClientSync<V> {
ingest_helper(
&tx,
ledger,
&self.binding,
(&self.binding, &self.verifier),
&channel,
h,
now,
@@ -230,7 +290,17 @@ impl<V: Ed25519Verifier> ClientSync<V> {
)?;
// 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 inflight: Vec<_> = helper
.into_iter()
.flat_map(|h| h.attempts.iter())
.filter(|a| !a.update_handoff_bytes.is_empty())
.collect();
ensure!(inflight.len() <= 1, "UPDATE_MULTIPLE_INFLIGHT");
if let Some(update) = inflight.first() {
tx.execute("UPDATE job_attempts SET state='unknown',revision=revision+1 WHERE state IN ('requesting','running','verifying') AND NOT (job=?1 AND attempt=?2) 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)",params![update.job_id,update.attempt_id])?;
} else {
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)?;
@@ -436,7 +506,8 @@ impl<V: Ed25519Verifier> ClientSync<V> {
safe = false;
continue;
}
let outcome = validate_attempt(&job, &offer, local, d, channel.boot_id)?;
let outcome =
validate_attempt(&job, &offer, local, d, channel.boot_id, &self.binding, now)?;
safe &= (d.outcome_state == "accepted"
&& local.consumption == "unconsumed"
&& local.grant_id.is_empty())
@@ -866,6 +937,7 @@ fn attempts(db: &Connection, ledger: &Connection, binding: &Binding) -> Result<V
}
}
summaries.push(SyncAttempt {
update_handoff_bytes: Vec::new(),
job_id: offer.job_id,
attempt_id: offer.attempt_id,
job_payload_hash: offer.job_payload_hash,
@@ -897,12 +969,13 @@ fn attempts(db: &Connection, ledger: &Connection, binding: &Binding) -> Result<V
fn ingest_helper(
db: &Connection,
ledger: &Connection,
binding: &Binding,
binding_and_verifier: (&Binding, &impl Ed25519Verifier),
channel: &Channel,
helper: &HelperSnapshot,
now: DateTime<Utc>,
identity: HelperIdentity,
) -> Result<()> {
let (binding, verifier) = binding_and_verifier;
let mut statement = db.prepare("SELECT job,attempt,offer FROM job_attempts")?;
for row in statement.query_map([], |r| {
Ok((
@@ -952,6 +1025,65 @@ fn ingest_helper(
"HELPER_ATTEMPT_BINDING"
);
if h.outcome_bytes.is_empty() {
if !h.update_handoff_bytes.is_empty() {
let header =
validate_update_summary(binding, &job, &offer, h, channel.boot_id, now)?;
ensure!(
header.helper_journal_id == helper.journal_id
&& header.agent_journal_id == metadata(db)?.journal_id,
"UPDATE_HELPER_JOURNAL_BINDING"
);
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),
)?;
ensure!(
matches!(
state.as_str(),
"accepted" | "requesting" | "running" | "verifying"
),
"UPDATE_CANNOT_HEAL_UNKNOWN"
);
type OriginalEvidence = (
String,
u64,
String,
String,
Vec<u8>,
Option<Vec<u8>>,
Option<Vec<u8>>,
);
let original:Option<OriginalEvidence>=ledger.query_row(
"SELECT c.agent,c.generation,c.job,c.attempt,e.start,e.outcome,c.result FROM execution_evidence e JOIN consumed c USING(grant_id) WHERE grant_id=?1",[&h.grant_id],
|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?,r.get(5)?,r.get(6)?))).optional()?;
let (agent, generation, job_id, attempt_id, start, outcome, result) =
original.context("UPDATE_LOCAL_ORIGINAL_MISSING")?;
ensure!(
agent == binding.agent_id.to_string()
&& generation == binding.enrollment_generation
&& job_id == h.job_id
&& attempt_id == h.attempt_id,
"UPDATE_LOCAL_CONSUMPTION_BINDING"
);
ensure!(
outcome.is_none() && result.is_none(),
"UPDATE_CANNOT_REPLACE_OUTCOME"
);
let start: serde_json::Value = serde_json::from_slice(&start)?;
let request: crate::auth::Request =
serde_json::from_value(start["request"].clone())?;
let permit: rmm_domain::jobs::ExecutionPermitPayload =
serde_json::from_slice(&request.permit_bytes)?;
binding.verify_with(&request, permit.not_before, verifier)?;
ensure!(
hash(&serde_json::to_vec(&request)?) == header.attempt.original_request_sha256
&& permit.grant_id == header.attempt.grant_id
&& permit.agent_update_plan_id == Some(header.attempt.plan_id),
"UPDATE_LOCAL_ORIGINAL_CHANGED"
);
db.execute("UPDATE job_attempts SET state='running',revision=MAX(revision,?3) WHERE job=?1 AND attempt=?2",params![h.job_id,h.attempt_id,h.revision])?;
}
continue;
}
let o: ReconciledOutcome = serde_json::from_slice(&h.outcome_bytes)?;
@@ -1084,6 +1216,13 @@ fn merge_helper(local: &mut [SyncAttempt], helper: &HelperSnapshot) -> Result<()
"HELPER_ATTEMPT_BINDING"
);
if h.consumption != "unconsumed" {
if !h.update_handoff_bytes.is_empty() {
ensure!(
h.state == "running" && a.state == "running" && a.outcome_bytes.is_empty(),
"UPDATE_CANNOT_HEAL_UNKNOWN"
);
a.update_handoff_bytes.clone_from(&h.update_handoff_bytes);
}
a.grant_id = h.grant_id.clone();
a.consumption = h.consumption.clone();
ensure!(
@@ -1101,6 +1240,42 @@ fn merge_helper(local: &mut [SyncAttempt], helper: &HelperSnapshot) -> Result<()
}
Ok(())
}
fn validate_update_summary(
_binding: &Binding,
job: &JobPayload,
offer: &JobOffer,
local: &SyncAttempt,
boot: Uuid,
now: DateTime<Utc>,
) -> Result<rmm_updater_handoff::Handoff> {
let proof =
rmm_domain::agent_update::UpdateInflightProof::from_bytes(&local.update_handoff_bytes)
.map_err(anyhow::Error::msg)?;
let key = rmm_domain::agent_update::job_health_public_key(job).map_err(anyhow::Error::msg)?;
let (h, _) =
rmm_domain::agent_update::verify_inflight_proof(&proof, key).map_err(anyhow::Error::msg)?;
let now = u64::try_from(now.timestamp())?;
ensure!(
rmm_domain::agent_update::handoff_matches_job(&h, job)
&& h.boot_id == boot
&& h.attempt.grant_id.to_string() == local.grant_id
&& h.attempt.authorization_id.to_string() == offer.authorization_id
&& h.attempt.plan_sha256 == offer.plan_hash
&& h.attempt.job_payload_sha256 == offer.job_payload_hash
&& local.original_boot_id == boot.to_string()
&& local.consumption == "consumed"
&& local.state == "running"
&& local.outcome_bytes.is_empty()
&& local.outcome_sha256.is_empty()
&& local.result.is_none()
&& local.result_envelope.is_none()
&& h.consumed_at <= now
&& now < h.finish_before,
"UPDATE_INFLIGHT_SUMMARY_BINDING"
);
Ok(h)
}
pub fn verify_response(
binding: &Binding,
request: &SyncRequest,
@@ -1176,6 +1351,8 @@ fn validate_attempt(
local: &SyncAttempt,
d: &AttemptDecision,
boot: Uuid,
binding: &Binding,
now: DateTime<Utc>,
) -> Result<Option<Vec<u8>>> {
ensure!(
d.job_payload_hash == offer.job_payload_hash
@@ -1199,6 +1376,25 @@ fn validate_attempt(
!terminal(&local.state) || d.outcome_state == local.state,
"TERMINAL_STATE_REGRESSION"
);
if d.update_inflight.is_some() || !local.update_handoff_bytes.is_empty() {
validate_update_summary(binding, job, offer, local, boot, now)?;
let proof =
rmm_domain::agent_update::UpdateInflightProof::from_bytes(&local.update_handoff_bytes)
.map_err(anyhow::Error::msg)?;
let key =
rmm_domain::agent_update::job_health_public_key(job).map_err(anyhow::Error::msg)?;
let expected =
rmm_domain::agent_update::inflight_decision(&proof, key).map_err(anyhow::Error::msg)?;
ensure!(
d.update_inflight.as_ref() == Some(&expected)
&& d.outcome_state == "running"
&& !d.unlock
&& d.outcome_bytes.is_empty()
&& d.outcome_sha256.is_none(),
"UPDATE_INFLIGHT_DECISION_CHANGED"
);
return Ok(None);
}
if d.outcome_state == "accepted" {
ensure!(
local.state == "accepted"
File diff suppressed because it is too large Load Diff
+1
View File
@@ -51,6 +51,7 @@ fn fixture() -> (Binding, Request, ExecutionPermitPayload, DateTime<Utc>) {
};
let job_bytes = encode(&job).unwrap();
let permit = ExecutionPermitPayload {
agent_update_plan_id: None,
domain: "rmm.execution-permit".into(),
schema_version: 1,
grant_id: Uuid::from_u128(7),
+575
View File
@@ -122,6 +122,7 @@ fn denial_case() -> (
max_output_bytes: 4096,
};
let permit = ExecutionPermitPayload {
agent_update_plan_id: None,
domain: "rmm.execution-permit".into(),
schema_version: 1,
grant_id: Uuid::new_v4(),
@@ -594,6 +595,7 @@ impl Fixture {
.attempts
.iter()
.map(|a| AttemptDecision {
update_inflight: None,
job_id: a.job_id,
attempt_id: a.attempt_id,
job_payload_hash: a.job_payload_hash.clone(),
@@ -673,6 +675,7 @@ impl Fixture {
max_output_bytes: 4096,
};
let permit = ExecutionPermitPayload {
agent_update_plan_id: None,
domain: "rmm.execution-permit".into(),
schema_version: 1,
grant_id: Uuid::new_v4(),
@@ -957,6 +960,9 @@ fn request(
text(8, &a.original_boot_id.to_string(), &mut row);
text(11, &a.outcome_sha256, &mut row);
blob(12, &a.outcome_bytes, &mut row);
if !a.update_handoff_bytes.is_empty() {
blob(14, &a.update_handoff_bytes, &mut row);
}
blob(6, &row, &mut out);
}
num(8, 1, &mut out);
@@ -1399,3 +1405,572 @@ fn root_and_system_are_distinct_explicit_helper_domains() {
let changed = sync.with_helper_identity(HelperIdentity::Root);
assert!(!changed.can_start(&system.db, &system.guard, 1, &system.clock, &system.boot));
}
// Test-only update admission port. This verifies fixture identity/bytes, NOT
// real update trust files, service ownership, OS locks or a WebUI approval.
struct TestUpdateGuard {
ledger: ExecutorGuard,
handoff_hash: String,
delegation: rmm_domain::agent_update::UpdateDelegation,
owner_alive: bool,
owner_authority: bool,
health_public_key: [u8; 32],
}
impl ExecutorLedgerGuard for TestUpdateGuard {
fn is_update_owner_authority(&self) -> bool {
self.owner_authority
}
fn check(&self, db: &Connection) -> anyhow::Result<()> {
self.ledger.check(db)
}
fn check_update_owner(
&self,
handoff: &rmm_updater_handoff::Handoff,
delegation: &rmm_domain::agent_update::UpdateDelegation,
_: rmm_updater_handoff::Phase,
_: &[rmm_updater_handoff::Event],
) -> anyhow::Result<()> {
anyhow::ensure!(
self.owner_alive
&& hash(&handoff.bytes()?) == self.handoff_hash
&& delegation == &self.delegation,
"TEST_OWNER_LOST"
);
Ok(())
}
}
impl rmm_agent_core::update_handoff::UpdateAdmissionGuard for TestUpdateGuard {
fn health_public_key(&self) -> anyhow::Result<[u8; 32]> {
Ok(self.health_public_key)
}
fn verify_prepared_update(
&self,
_: &Request,
handoff: &rmm_updater_handoff::Handoff,
delegation: &rmm_domain::agent_update::UpdateDelegation,
_: DateTime<Utc>,
) -> anyhow::Result<()> {
anyhow::ensure!(
hash(&handoff.bytes()?) == self.handoff_hash && delegation == &self.delegation,
"TEST_HANDOFF_CHANGED"
);
Ok(())
}
}
fn update_case() -> (
Fixture,
HelperSync,
Request,
rmm_updater_handoff::Handoff,
TestUpdateGuard,
) {
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use rmm_domain::agent_update as u;
use rmm_updater_handoff as h;
let mut f = Fixture::new();
let mut sync = f.sync();
rmm_agent_core::update_handoff::initialize(&f.db, &f.guard).unwrap();
let snapshot = f.snapshot(&mut sync, 7);
let raw = f.request(&snapshot);
let ready = f.decision(&snapshot, &raw, 7, true);
assert!(f.apply(&mut sync, &ready, &raw).unwrap().ready);
let digest = |n: u8| format!("{n:02x}").repeat(32);
let mut manifest: u::ReleaseManifest = serde_json::from_slice(include_bytes!(
"../../updater/tests/fixtures/release_v1.json"
))
.unwrap();
manifest.published_at = f.clock.now.timestamp() as u64 - 1;
manifest.expires_at = f.clock.now.timestamp() as u64 + 3600;
let payload = encode(&manifest).unwrap();
let release_key = SigningKey::from_bytes(&[43; 32]);
let mut signed = b"rmm.agent-release.v1\0".to_vec();
signed.extend_from_slice(&payload);
let envelope = u::ReleaseEnvelope {
schema_version: 1,
key_id: hash(release_key.verifying_key().as_bytes()),
payload_bytes: URL_SAFE_NO_PAD.encode(&payload),
signature: URL_SAFE_NO_PAD.encode(release_key.sign(&signed).to_bytes()),
};
let update = u::ResolvedAgentUpdate {
schema_version: 1,
profile: u::UpdateProfile::FixedCodePairV1,
release_envelope_sha256: envelope.sha256().unwrap(),
manifest_sha256: hash(&payload),
release_envelope: envelope,
predecessor: u::Predecessor {
release_id: Uuid::from_u128(99),
release_sequence: 3,
manifest_sha256: digest(1),
installed_record_sha256: digest(2),
agent_sha256: digest(3),
helper_sha256: digest(4),
},
updater: u::UpdaterBinding {
binary_sha256: digest(5),
policy_sha256: digest(6),
keyring_generation: 1,
keyring_sha256: digest(7),
health_public_key_hex: hex::encode(
SigningKey::from_bytes(&[44; 32]).verifying_key().to_bytes(),
),
health_key_pin_sha256: digest(10),
},
peer_pin_transition: u::PeerPinTransition::UnchangedGeneralPinV1 {
general_pin_sha256: digest(8),
},
recovery: u::Recovery::RestoreExactPredecessorOnce,
health_profile: u::HealthProfile::MtlsInventorySyncV1,
health_timeout_seconds: 180,
no_reboot: true,
};
assert!(update.validate());
let job = JobPayload {
schema_version: 1,
tenant_id: f.binding.tenant_id,
asset_id: f.binding.asset_id,
agent_id: f.binding.agent_id,
enrollment_generation: 1,
assignment_version: 3,
job_id: Uuid::new_v4(),
attempt_id: Uuid::new_v4(),
action: Action::UpdateAgent {
update: Box::new(update.clone()),
},
execution_identity: "root".into(),
not_before: f.clock.now - Duration::seconds(1),
start_before: f.clock.now + Duration::minutes(5),
max_runtime_seconds: 600,
max_output_bytes: 4096,
};
let permit = ExecutionPermitPayload {
agent_update_plan_id: Some(Uuid::from_u128(1001)),
domain: "rmm.execution-permit".into(),
schema_version: 1,
grant_id: Uuid::new_v4(),
authorization_id: Uuid::new_v4(),
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: digest(9),
job_payload_hash: hash(&encode(&job).unwrap()),
action: "update_agent".into(),
execution_identity: "root".into(),
not_before: f.clock.now - Duration::seconds(1),
start_before: f.clock.now + Duration::seconds(59),
max_runtime_seconds: 600,
key_id: hash(f.key.verifying_key().as_bytes()),
};
let permit_bytes = encode(&permit).unwrap();
let request = Request {
job_bytes: encode(&job).unwrap(),
permit_bytes: permit_bytes.clone(),
signature: f.key.sign(&permit_bytes).to_bytes().to_vec(),
plan_hash: permit.plan_hash.clone(),
authorization_id: permit.authorization_id,
};
let handoff = h::Handoff {
schema_version: 1,
attempt: h::AttemptBinding {
tenant_id: job.tenant_id,
asset_id: job.asset_id,
agent_id: job.agent_id,
enrollment_generation: 1,
assignment_version: 3,
plan_id: Uuid::from_u128(1001),
plan_sha256: permit.plan_hash,
job_id: job.job_id,
attempt_id: job.attempt_id,
job_payload_sha256: permit.job_payload_hash,
grant_id: permit.grant_id,
authorization_id: permit.authorization_id,
original_request_sha256: hash(&encode(&request).unwrap()),
original_start_witness_sha256: hash(
&rmm_agent_core::ledger::start_witness_bytes(
&serde_json::from_slice(&request.permit_bytes).unwrap(),
&request,
f.boot.0,
f.clock.now,
)
.unwrap(),
),
},
agent_journal_id: f.agent_journal,
helper_journal_id: snapshot.journal_id,
helper_checkpoint: snapshot.checkpoint,
previous_decision_id: ready.decision_id,
boot_id: f.boot.0,
connection_generation: 7,
owner_nonce: Uuid::new_v4(),
updater_sha256: update.updater.binary_sha256,
updater_policy_sha256: update.updater.policy_sha256,
predecessor_record_sha256: update.predecessor.installed_record_sha256,
retained_state_sha256: digest(10),
ledger_identity_sha256: digest(11),
staged_files_sha256: digest(12),
predecessor: h::CodePair {
manifest_sha256: update.predecessor.manifest_sha256,
agent_sha256: update.predecessor.agent_sha256.clone(),
helper_sha256: update.predecessor.helper_sha256.clone(),
pin_sha256: digest(8),
},
target: h::CodePair {
manifest_sha256: update.manifest_sha256,
agent_sha256: manifest.artifacts[0].sha256.clone(),
helper_sha256: manifest.artifacts[1].sha256.clone(),
pin_sha256: digest(8),
},
pin_transition: h::PinTransition::UnchangedLinuxGeneralV1,
pin_transition_sha256: hash(&encode(&update.peer_pin_transition).unwrap()),
files: vec![
h::FileTransition {
role: h::FileRole::AgentBinary,
old_sha256: update.predecessor.agent_sha256,
new_sha256: manifest.artifacts[0].sha256.clone(),
},
h::FileTransition {
role: h::FileRole::HelperBinary,
old_sha256: update.predecessor.helper_sha256,
new_sha256: manifest.artifacts[1].sha256.clone(),
},
],
stop_set: vec![h::Service::LinuxAgent, h::Service::LinuxGeneralHelper],
consumed_at: f.clock.now.timestamp() as u64,
finish_before: f.clock.now.timestamp() as u64 + 600,
};
let guard = TestUpdateGuard {
ledger: ExecutorGuard {
directory: f.dir.clone(),
},
handoff_hash: hash(&handoff.bytes().unwrap()),
owner_alive: true,
owner_authority: true,
health_public_key: SigningKey::from_bytes(&[44; 32]).verifying_key().to_bytes(),
delegation: u::UpdateDelegation {
schema_version: 1,
handoff_sha256: hash(&handoff.bytes().unwrap()),
owner_nonce: handoff.owner_nonce,
boot_id: handoff.boot_id,
prepared_at: f.clock.now,
updater: u::ParticipantIdentity {
role: u::ParticipantRole::LinuxUpdater,
pid: 600,
birth: 1000,
image_sha256: handoff.updater_sha256.clone(),
security_principal: "uid:0".into(),
service_instance_sha256: digest(14),
},
original_helper: u::ParticipantIdentity {
role: u::ParticipantRole::LinuxGeneralHelper,
pid: 700,
birth: 2000,
image_sha256: handoff.predecessor.helper_sha256.clone(),
security_principal: "uid:0".into(),
service_instance_sha256: digest(15),
},
},
};
(f, sync, request, handoff, guard)
}
#[test]
fn update_handoff_consumption_start_and_stop_intent_are_one_transaction() {
for trigger in [
"",
"CREATE TRIGGER deny_handoff BEFORE INSERT ON agent_update_handoffs BEGIN SELECT RAISE(ABORT,'test'); END;",
"CREATE TRIGGER deny_event BEFORE INSERT ON agent_update_handoff_events BEGIN SELECT RAISE(ABORT,'test'); END;",
] {
let (mut f, mut sync, request, handoff, guard) = update_case();
f.db.execute_batch(trigger).unwrap();
let result = sync.begin_update_handoff(
&mut f.db,
&guard,
7,
&rmm_agent_core::update_handoff::PreparedHandoff {
request: &request,
handoff: &handoff,
delegation: &guard.delegation,
recorded_at: f.clock.now,
},
&f.clock,
&f.boot,
);
assert_eq!(result.is_ok(), trigger.is_empty(), "{result:?}");
for table in [
"consumed",
"execution_evidence",
"agent_update_handoffs",
"agent_update_delegations",
"agent_update_handoff_events",
] {
let count: u32 =
f.db.query_row(&format!("SELECT count(*) FROM {table}"), [], |r| r.get(0))
.unwrap();
assert_eq!(count, if trigger.is_empty() { 1 } else { 0 }, "{table}");
}
if trigger.is_empty() {
let journal =
rmm_agent_core::update_handoff::read(&f.db, &guard, handoff.attempt.grant_id)
.unwrap();
assert_eq!(journal.phase(), rmm_updater_handoff::Phase::StopIntent);
assert!(!sync.can_start(&f.db, &guard, 7, &f.clock, &f.boot));
assert!(
sync.begin_update_handoff(
&mut f.db,
&guard,
7,
&rmm_agent_core::update_handoff::PreparedHandoff {
request: &request,
handoff: &handoff,
delegation: &guard.delegation,
recorded_at: f.clock.now
},
&f.clock,
&f.boot
)
.is_err()
);
}
}
}
#[test]
fn update_inflight_negative_control_keeps_every_start_closed_and_owner_loss_is_terminal() {
let (mut f, mut sync, request, handoff, mut guard) = update_case();
let prepared = rmm_agent_core::update_handoff::PreparedHandoff {
request: &request,
handoff: &handoff,
delegation: &guard.delegation,
recorded_at: f.clock.now,
};
sync.begin_update_handoff(&mut f.db, &guard, 7, &prepared, &f.clock, &f.boot)
.unwrap();
let original: Vec<u8> =
f.db.query_row(
"SELECT start FROM execution_evidence WHERE grant_id=?1",
[handoff.attempt.grant_id.to_string()],
|r| r.get(0),
)
.unwrap();
f.clock.now += Duration::seconds(1);
let mut fresh = f.sync();
let snapshot = fresh
.snapshot(&mut f.db, &guard, 8, &f.clock, &f.boot)
.unwrap();
assert_eq!(snapshot.attempts[0].state, "running");
assert!(!snapshot.attempts[0].update_handoff_bytes.is_empty());
let raw = f.request(&snapshot);
let mut decision = f.decision(&snapshot, &raw, 8, false);
let proof = rmm_domain::agent_update::UpdateInflightProof::from_bytes(
&snapshot.attempts[0].update_handoff_bytes,
)
.unwrap();
let attempt = &mut decision.attempts[0];
attempt.outcome_state = "running".into();
attempt.update_inflight =
Some(rmm_domain::agent_update::inflight_decision(&proof, guard.health_public_key).unwrap());
attempt.reason = "AGENT_UPDATE_INFLIGHT".into();
let signed = f.sign(&decision);
let applied = fresh
.apply(&mut f.db, &guard, &signed, &raw, &f.clock, &f.boot)
.unwrap();
assert!(!applied.ready && applied.unlocked_grants.is_empty());
assert!(!fresh.can_start(&f.db, &guard, 8, &f.clock, &f.boot));
assert_eq!(
f.db.query_row::<u32, _, _>(
"SELECT count(*) FROM execution_evidence WHERE outcome IS NOT NULL",
[],
|r| r.get(0)
)
.unwrap(),
0
);
// A nondesignated General reader cannot attest this Full-owned update,
// and must never turn that lack of authority into terminal Unknown.
guard.owner_authority = false;
guard.owner_alive = false;
assert!(
fresh
.snapshot(&mut f.db, &guard, 8, &f.clock, &f.boot)
.is_err()
);
assert_eq!(
rmm_agent_core::update_handoff::read(&f.db, &guard, handoff.attempt.grant_id)
.unwrap()
.phase(),
rmm_updater_handoff::Phase::StopIntent
);
guard.owner_authority = true;
f.clock.now += Duration::seconds(1);
let lost = fresh
.snapshot(&mut f.db, &guard, 8, &f.clock, &f.boot)
.unwrap();
assert_eq!(lost.attempts[0].state, "unknown");
assert!(lost.attempts[0].update_handoff_bytes.is_empty());
assert_eq!(
rmm_agent_core::update_handoff::read(&f.db, &guard, handoff.attempt.grant_id)
.unwrap()
.phase(),
rmm_updater_handoff::Phase::Unknown
);
guard.owner_alive = true;
let retained = fresh
.snapshot(&mut f.db, &guard, 8, &f.clock, &f.boot)
.unwrap();
assert_eq!(
retained.attempts[0].outcome_bytes,
lost.attempts[0].outcome_bytes
);
assert_eq!(retained.attempts[0].state, "unknown");
assert_eq!(
f.db.query_row::<Vec<u8>, _, _>(
"SELECT start FROM execution_evidence WHERE grant_id=?1",
[handoff.attempt.grant_id.to_string()],
|r| r.get(0)
)
.unwrap(),
original
);
assert_eq!(
f.db.query_row::<u32, _, _>(
"SELECT count(*) FROM consumed WHERE result IS NULL",
[],
|r| r.get(0)
)
.unwrap(),
1
);
}
impl rmm_agent_core::update_handoff::UpdateParticipantGuard for TestUpdateGuard {
fn participant(&self) -> rmm_agent_core::update_handoff::Participant {
rmm_agent_core::update_handoff::Participant::OriginalHelper
}
fn verify_update_event(
&self,
_: &rmm_updater_handoff::Handoff,
_: &rmm_domain::agent_update::UpdateDelegation,
_: &rmm_updater_handoff::Event,
) -> anyhow::Result<()> {
anyhow::bail!("TEST_NATIVE_INTENTS_ONLY_NO_OS_EFFECTS")
}
fn verify_native_effect(
&self,
h: &rmm_updater_handoff::Handoff,
d: &rmm_domain::agent_update::UpdateDelegation,
_: &rmm_agent_core::update_handoff::NativeEffect,
_: rmm_agent_core::update_handoff::NativeEffectStage,
) -> anyhow::Result<()> {
anyhow::ensure!(
self.owner_alive
&& self.owner_authority
&& hash(&h.bytes()?) == self.handoff_hash
&& d == &self.delegation,
"TEST_NATIVE_DENIED"
);
Ok(())
}
}
#[test]
fn native_stop_intent_is_original_head_bound_create_once_and_transactional() {
use rmm_agent_core::update_handoff as u;
for fail_insert in [false, true] {
let (mut f, mut sync, request, handoff, mut guard) = update_case();
sync.begin_update_handoff(
&mut f.db,
&guard,
7,
&u::PreparedHandoff {
request: &request,
handoff: &handoff,
delegation: &guard.delegation,
recorded_at: f.clock.now,
},
&f.clock,
&f.boot,
)
.unwrap();
let head = u::read(&f.db, &guard, handoff.attempt.grant_id)
.unwrap()
.head_sha256()
.to_owned();
let effect = u::NativeEffect::StopService {
service: u::NativeService::Agent,
};
assert!(
u::claim_native_effect(
&mut f.db,
&guard,
handoff.attempt.grant_id,
&"0".repeat(64),
&effect,
f.clock.now
)
.is_err()
);
guard.owner_alive = false;
assert!(
u::claim_native_effect(
&mut f.db,
&guard,
handoff.attempt.grant_id,
&head,
&effect,
f.clock.now
)
.is_err()
);
guard.owner_alive = true;
if fail_insert {
f.db.execute_batch("CREATE TRIGGER deny_native BEFORE INSERT ON agent_update_native_claims BEGIN SELECT RAISE(ABORT,'test'); END;").unwrap();
}
let result = u::claim_native_effect(
&mut f.db,
&guard,
handoff.attempt.grant_id,
&head,
&effect,
f.clock.now,
);
assert_eq!(result.is_ok(), !fail_insert);
if let Ok(claim) = result {
assert_eq!(claim.grant_id(), handoff.attempt.grant_id);
assert_eq!(claim.owner_nonce(), handoff.owner_nonce);
assert_eq!(claim.head_sha256(), head);
assert_eq!(claim.effect(), &effect);
assert!(
u::claim_native_effect(
&mut f.db,
&guard,
handoff.attempt.grant_id,
&head,
&effect,
f.clock.now
)
.is_err()
);
}
assert_eq!(
f.db.query_row::<u32, _, _>(
"SELECT count(*) FROM agent_update_native_claims",
[],
|r| r.get(0)
)
.unwrap(),
u32::from(!fail_insert)
);
assert_eq!(
f.db.query_row::<u32, _, _>("SELECT count(*) FROM consumed", [], |r| r.get(0))
.unwrap(),
1
);
assert_eq!(
u::read(&f.db, &guard, handoff.attempt.grant_id)
.unwrap()
.head_sha256(),
head
);
}
}
+4
View File
@@ -125,6 +125,7 @@ impl Fixture {
"succeeded" | "failed" | "cancelled" | "expired"
);
AttemptDecision {
update_inflight: None,
job_id: job.job_id,
attempt_id: job.attempt_id,
job_payload_hash: local.job_payload_hash.clone(),
@@ -249,6 +250,7 @@ impl Fixture {
let mut grant = None;
if consume {
let p = ExecutionPermitPayload {
agent_update_plan_id: None,
domain: "rmm.execution-permit".into(),
schema_version: 1,
grant_id: Uuid::new_v4(),
@@ -370,6 +372,7 @@ fn authenticated_helper_outcome_is_imported_as_new_durable_evidence_not_reexecut
checkpoint: 3,
previous_decision_id: None,
attempts: vec![SyncAttempt {
update_handoff_bytes: Vec::new(),
job_id: offer.job_id.clone(),
attempt_id: offer.attempt_id.clone(),
job_payload_hash: offer.job_payload_hash.clone(),
@@ -1175,6 +1178,7 @@ fn system_helper_import_requires_explicit_system_identity_and_never_root_alias()
checkpoint: 3,
previous_decision_id: None,
attempts: vec![SyncAttempt {
update_handoff_bytes: Vec::new(),
job_id: offer.job_id.clone(),
attempt_id: offer.attempt_id.clone(),
job_payload_hash: offer.job_payload_hash.clone(),