Share signed job core and verify native Linux then Windows contracts
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
//! Connection-level ledger rules, with OS path/lock/ACL handling left to adapters.
|
||||
use crate::auth::{Outcome, Request};
|
||||
use anyhow::{Result, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
use rmm_domain::jobs::{ExecutionPermitPayload, JobPayload};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Finite 1 GiB bound for durable agent/helper history. Unexpected page sizes
|
||||
/// are refused rather than silently multiplying the byte limit. No compaction
|
||||
/// or tombstone deletion occurs here; SQLITE_FULL must still fail closed.
|
||||
pub fn bounded_journal_capacity(db: &rusqlite::Connection) -> Result<()> {
|
||||
let page_size: u32 = db.query_row("PRAGMA page_size", [], |r| r.get(0))?;
|
||||
ensure!(page_size == 4096, "JOURNAL_PAGE_SIZE_UNSUPPORTED");
|
||||
let maximum: u32 = db.query_row("PRAGMA max_page_count=262144", [], |r| r.get(0))?;
|
||||
ensure!(maximum == 262144, "JOURNAL_STORAGE_CAPACITY_BLOCKED");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The platform adapter must securely open/lock the file before calling this.
|
||||
/// No file creation, ACL repair or journal reset is performed here.
|
||||
pub fn initialize_ledger(db: &rusqlite::Connection) -> Result<()> {
|
||||
bounded_journal_capacity(db)?;
|
||||
db.execute_batch("PRAGMA synchronous=FULL; PRAGMA journal_mode=DELETE;
|
||||
CREATE TABLE IF NOT EXISTS consumed(grant_id TEXT PRIMARY KEY, agent TEXT NOT NULL, generation INTEGER NOT NULL,
|
||||
job TEXT NOT NULL, attempt TEXT NOT NULL, result BLOB, UNIQUE(agent,generation,job,attempt));
|
||||
CREATE TABLE IF NOT EXISTS execution_evidence(grant_id TEXT PRIMARY KEY, start BLOB NOT NULL, outcome BLOB);")?;
|
||||
let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?;
|
||||
ensure!(check == "ok", "JOURNAL_INTEGRITY");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn consume(db: &mut rusqlite::Connection, p: &ExecutionPermitPayload) -> Result<()> {
|
||||
let tx = db.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
|
||||
// Unknown effects retain the global device lock, including across restart.
|
||||
let unresolved: i64 = tx.query_row(
|
||||
"SELECT count(*) FROM consumed WHERE result IS NULL",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(unresolved == 0, "UNRESOLVED_EFFECT_RESOURCE_LOCKED");
|
||||
tx.execute(
|
||||
"INSERT INTO consumed(grant_id,agent,generation,job,attempt) VALUES(?1,?2,?3,?4,?5)",
|
||||
rusqlite::params![
|
||||
p.grant_id.to_string(),
|
||||
p.agent_id.to_string(),
|
||||
p.enrollment_generation,
|
||||
p.job_id.to_string(),
|
||||
p.attempt_id.to_string()
|
||||
],
|
||||
)?;
|
||||
tx.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn finish(
|
||||
db: &rusqlite::Connection,
|
||||
p: &ExecutionPermitPayload,
|
||||
outcome: &Outcome,
|
||||
) -> Result<()> {
|
||||
db.execute(
|
||||
"UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1",
|
||||
rusqlite::params![p.grant_id.to_string(), serde_json::to_vec(outcome)?],
|
||||
)?;
|
||||
// Unknown / awaiting reboot keep the resource reserved until a real sync
|
||||
// protocol exists. Never unlock on a process exit or a network reconnect.
|
||||
if outcome.state != "unknown" && outcome.state != "awaiting_reboot" {
|
||||
ensure!(
|
||||
db.execute(
|
||||
"UPDATE consumed SET result=?2 WHERE grant_id=?1 AND result IS NULL",
|
||||
rusqlite::params![p.grant_id.to_string(), serde_json::to_vec(outcome)?]
|
||||
)? == 1,
|
||||
"CONSUMPTION_NOT_FOUND"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Persist the original boot and exact signed request BEFORE any reboot/effect.
|
||||
/// Awaiting/unknown outcomes never release consumed.result's resource lock.
|
||||
pub fn save_start(
|
||||
db: &rusqlite::Connection,
|
||||
permit: &ExecutionPermitPayload,
|
||||
request: &Request,
|
||||
previous_boot_id: Uuid,
|
||||
recorded_at: DateTime<Utc>,
|
||||
) -> Result<()> {
|
||||
let job: JobPayload = serde_json::from_slice(&request.job_bytes)?;
|
||||
ensure!(!previous_boot_id.is_nil(), "ORIGINAL_BOOT_MISSING");
|
||||
let deadline = match job.action {
|
||||
rmm_domain::jobs::Action::Reboot {
|
||||
reconnect_timeout_seconds,
|
||||
..
|
||||
} => Some(recorded_at + chrono::Duration::seconds(reconnect_timeout_seconds.into())),
|
||||
_ => None,
|
||||
};
|
||||
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(())
|
||||
}
|
||||
Reference in New Issue
Block a user