Implement and natively verify the human-reviewed Windows recovery tool
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,337 @@
|
||||
//! Local operator CLI. Verifies the scoped OIDC review; a fresh ordinary Sync is still required.
|
||||
#[cfg(any(windows, test))]
|
||||
#[path = "windows_reviewed_recovery/local.rs"]
|
||||
mod local;
|
||||
|
||||
#[cfg(windows)]
|
||||
mod windows {
|
||||
use super::local::{LocalJournals, Reviewed};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use fs2::FileExt;
|
||||
use rmm_agent_core::{
|
||||
auth::Binding,
|
||||
windows_sync_history::{BINDING_SHA256, BOOT, FAULT, verify_local_history},
|
||||
};
|
||||
use rmm_agent_windows::{
|
||||
SERVICE_NAME,
|
||||
helper_ipc::{self as ipc, PinnedFile},
|
||||
helper_runtime::intent,
|
||||
helper_system, native,
|
||||
};
|
||||
use rmm_domain::{jobs::hash, windows_sync_history::case_witness};
|
||||
use rusqlite::{Connection, OpenFlags};
|
||||
use std::{
|
||||
fs::{File, OpenOptions},
|
||||
io::{Read, Seek, SeekFrom},
|
||||
os::windows::{
|
||||
fs::{MetadataExt, OpenOptionsExt},
|
||||
io::AsRawHandle,
|
||||
},
|
||||
path::Path,
|
||||
};
|
||||
use windows_sys::Win32::{
|
||||
Storage::FileSystem::{BY_HANDLE_FILE_INFORMATION, GetFileInformationByHandle},
|
||||
System::Services::*,
|
||||
};
|
||||
const ROOT: &str = r"C:\ProgramData\RustDiscovery";
|
||||
const STATE: &str = r"C:\ProgramData\RustDiscovery\state";
|
||||
const RECEIPT: &str = r"C:\ProgramData\RustDiscoveryBootstrap\windows-checkpoint-review.json";
|
||||
const PASSIVE: &str = "7c9f6f3642ef84337c3cd761d8a4788c4babe81522fdd987a3eb9b819bc8d851";
|
||||
const AGENT_DB: &str = r"C:\ProgramData\RustDiscovery\state\telemetry.sqlite";
|
||||
const LOCAL_DB: &str = r"C:\ProgramData\RustDiscovery\state\consumption.sqlite";
|
||||
const HELPER_DB: &str = r"C:\ProgramData\RustDiscoverySystem\state\ledger.sqlite";
|
||||
|
||||
struct ScHandle(SC_HANDLE);
|
||||
impl Drop for ScHandle {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
CloseServiceHandle(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn is_stopped(name: &str) -> Result<bool> {
|
||||
let mgr = ScHandle(unsafe {
|
||||
OpenSCManagerW(std::ptr::null(), std::ptr::null(), SC_MANAGER_CONNECT)
|
||||
});
|
||||
ensure!(!mgr.0.is_null(), "SCM unreadable");
|
||||
let name: Vec<u16> = name.encode_utf16().chain(Some(0)).collect();
|
||||
let svc = ScHandle(unsafe { OpenServiceW(mgr.0, name.as_ptr(), SERVICE_QUERY_STATUS) });
|
||||
ensure!(!svc.0.is_null(), "service missing");
|
||||
let mut status: SERVICE_STATUS_PROCESS = unsafe { std::mem::zeroed() };
|
||||
let mut size = 0;
|
||||
ensure!(
|
||||
unsafe {
|
||||
QueryServiceStatusEx(
|
||||
svc.0,
|
||||
SC_STATUS_PROCESS_INFO,
|
||||
(&mut status as *mut SERVICE_STATUS_PROCESS).cast(),
|
||||
size_of::<SERVICE_STATUS_PROCESS>() as u32,
|
||||
&mut size,
|
||||
)
|
||||
} != 0,
|
||||
"service status unreadable"
|
||||
);
|
||||
Ok(status.dwCurrentState == SERVICE_STOPPED && status.dwProcessId == 0)
|
||||
}
|
||||
fn both_stopped() -> Result<bool> {
|
||||
Ok(is_stopped(SERVICE_NAME)? && is_stopped(ipc::HELPER_SERVICE)?)
|
||||
}
|
||||
fn leaf(path: &Path) -> Result<File> {
|
||||
for a in path.ancestors() {
|
||||
ensure!(
|
||||
a.symlink_metadata()?.file_attributes() & 0x400 == 0,
|
||||
"reparse path"
|
||||
);
|
||||
}
|
||||
let f = OpenOptions::new().read(true).share_mode(3).open(path)?;
|
||||
let mut i: BY_HANDLE_FILE_INFORMATION = unsafe { std::mem::zeroed() };
|
||||
ensure!(
|
||||
unsafe { GetFileInformationByHandle(f.as_raw_handle(), &mut i) } != 0
|
||||
&& i.nNumberOfLinks == 1
|
||||
&& i.dwFileAttributes & 0x410 == 0,
|
||||
"unexpected file/hardlink"
|
||||
);
|
||||
Ok(f)
|
||||
}
|
||||
struct Db {
|
||||
db: Connection,
|
||||
_handles: Vec<File>,
|
||||
}
|
||||
impl Db {
|
||||
fn open(path: &str, write: bool, stopped: bool) -> Result<Self> {
|
||||
let mut main = leaf(Path::new(path))?;
|
||||
let mut header = [0u8; 20];
|
||||
main.read_exact(&mut header)?;
|
||||
ensure!(
|
||||
header.starts_with(b"SQLite format 3\0")
|
||||
&& header[18] == header[19]
|
||||
&& matches!(header[18], 1 | 2),
|
||||
"unexpected SQLite header"
|
||||
);
|
||||
let mut files = vec![main];
|
||||
let mut has_wal = false;
|
||||
let mut has_shm = false;
|
||||
let mut has_journal = false;
|
||||
for (suffix, present) in [
|
||||
("-wal", &mut has_wal),
|
||||
("-shm", &mut has_shm),
|
||||
("-journal", &mut has_journal),
|
||||
] {
|
||||
let name = format!("{path}{suffix}");
|
||||
if Path::new(&name).try_exists()? {
|
||||
files.push(leaf(Path::new(&name))?);
|
||||
*present = true;
|
||||
}
|
||||
}
|
||||
let flags = if write {
|
||||
OpenFlags::SQLITE_OPEN_READ_WRITE
|
||||
} else {
|
||||
OpenFlags::SQLITE_OPEN_READ_ONLY
|
||||
};
|
||||
let db = if !write && header[18] == 2 && !(has_wal && has_shm) {
|
||||
// Only under both exclusive lifetime service locks, after a clean WAL close.
|
||||
ensure!(
|
||||
stopped && !has_wal && !has_shm && !has_journal,
|
||||
"read-only WAL snapshot unavailable"
|
||||
);
|
||||
let url = format!("file:///{}?mode=ro&immutable=1", path.replace('\\', "/"));
|
||||
Connection::open_with_flags(url, flags | OpenFlags::SQLITE_OPEN_URI)?
|
||||
} else {
|
||||
Connection::open_with_flags(path, flags)?
|
||||
};
|
||||
db.busy_timeout(std::time::Duration::from_secs(2))?;
|
||||
db.execute_batch("PRAGMA trusted_schema=OFF")?;
|
||||
if write {
|
||||
db.execute_batch("PRAGMA synchronous=FULL")?;
|
||||
} else {
|
||||
db.execute_batch("PRAGMA query_only=ON; BEGIN;")?;
|
||||
}
|
||||
Ok(Self {
|
||||
db,
|
||||
_handles: files,
|
||||
})
|
||||
}
|
||||
}
|
||||
fn data(file: &mut File, max: u64) -> Result<Vec<u8>> {
|
||||
ensure!(file.metadata()?.len() <= max, "public file too large");
|
||||
file.seek(SeekFrom::Start(0))?;
|
||||
let mut bytes = vec![];
|
||||
file.take(max + 1).read_to_end(&mut bytes)?;
|
||||
ensure!(bytes.len() as u64 <= max, "public file changed");
|
||||
Ok(bytes)
|
||||
}
|
||||
pub fn run() -> Result<()> {
|
||||
let args: Vec<_> = std::env::args().skip(1).collect();
|
||||
ensure!(
|
||||
args.len() == 1
|
||||
&& matches!(
|
||||
args[0].as_str(),
|
||||
"prepare" | "read" | "verify-receipt" | "apply"
|
||||
),
|
||||
"use prepare, read, verify-receipt or apply"
|
||||
);
|
||||
let apply = args[0] == "apply";
|
||||
let key_readers = [ipc::service_sid(SERVICE_NAME)?];
|
||||
let _root = PinnedFile::open(Path::new(ROOT), true, &key_readers, &[], false)?;
|
||||
let mut binding_file = PinnedFile::open(
|
||||
&Path::new(ROOT).join("binding.json"),
|
||||
false,
|
||||
&key_readers,
|
||||
&[],
|
||||
false,
|
||||
)?;
|
||||
let bytes = data(&mut binding_file.file, 8192)?;
|
||||
ensure!(hash(&bytes) == BINDING_SHA256, "changed job binding");
|
||||
let agent: Binding = serde_json::from_slice(&bytes)?;
|
||||
let (settings, _helper_pins) = helper_system::read_settings()?;
|
||||
let witness = case_witness();
|
||||
ensure!(
|
||||
!settings.enable_effects && !settings.allow_reboot,
|
||||
"expected passive helper policy"
|
||||
);
|
||||
let mut config = PinnedFile::open(
|
||||
&Path::new(ROOT).join("config.json"),
|
||||
false,
|
||||
&key_readers,
|
||||
&[],
|
||||
false,
|
||||
)?;
|
||||
ensure!(
|
||||
config.sha256()? == PASSIVE,
|
||||
"operator must restore passive config first"
|
||||
);
|
||||
let boot = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?
|
||||
.block_on(native::boot_id())?;
|
||||
ensure!(boot == BOOT, "unexpected Windows boot");
|
||||
let review = if matches!(args[0].as_str(), "verify-receipt" | "apply") {
|
||||
let _folder = PinnedFile::open(
|
||||
Path::new(RECEIPT).parent().context("bootstrap path")?,
|
||||
true,
|
||||
&[],
|
||||
&[],
|
||||
false,
|
||||
)?;
|
||||
let mut file = PinnedFile::open(Path::new(RECEIPT), false, &[], &[], false)?;
|
||||
Some(Reviewed::verify(
|
||||
data(&mut file.file, 65536)?,
|
||||
&agent,
|
||||
&settings.binding,
|
||||
&witness,
|
||||
)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut locks = vec![];
|
||||
let quiescent = both_stopped()?;
|
||||
ensure!(
|
||||
!apply || quiescent,
|
||||
"both services must be stopped by the operator"
|
||||
);
|
||||
if quiescent {
|
||||
for path in [
|
||||
Path::new(STATE).join("agent.lock"),
|
||||
Path::new(helper_system::STATE_ROOT).join("executor.lock"),
|
||||
] {
|
||||
let f = leaf(&path)?;
|
||||
f.try_lock_exclusive().context("service lock busy")?;
|
||||
locks.push(f);
|
||||
}
|
||||
ensure!(both_stopped()?, "service started during recovery");
|
||||
}
|
||||
native::private_path(Path::new(STATE), true)?;
|
||||
for p in [AGENT_DB, LOCAL_DB] {
|
||||
native::private_path(Path::new(p), false)?;
|
||||
}
|
||||
let helper_sid = [ipc::service_sid(ipc::HELPER_SERVICE)?];
|
||||
let _helper_state = PinnedFile::open_read_shared(
|
||||
Path::new(helper_system::STATE_ROOT),
|
||||
true,
|
||||
&[],
|
||||
&helper_sid,
|
||||
)?;
|
||||
let helper_file =
|
||||
PinnedFile::open_read_shared(Path::new(HELPER_DB), false, &[], &helper_sid)?;
|
||||
let mut a = Db::open(AGENT_DB, apply, quiescent)?;
|
||||
let c = Db::open(LOCAL_DB, false, quiescent)?;
|
||||
let h = Db::open(HELPER_DB, false, quiescent)?;
|
||||
let (ledger, lock, pin): (Vec<u8>, Vec<u8>, String) = h.db.query_row(
|
||||
"SELECT ledger,executor_lock,binding_sha256 FROM windows_helper_files WHERE id=1",
|
||||
[],
|
||||
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
|
||||
)?;
|
||||
ensure!(
|
||||
serde_json::from_slice::<ipc::FileIdentity>(&ledger)? == helper_file.identity
|
||||
&& pin == ipc::binding_hash(&settings.binding)?,
|
||||
"helper journal or binding changed"
|
||||
);
|
||||
let _intents = if Path::new(intent::DB).try_exists()? {
|
||||
Some(intent::Store::open_empty_history_snapshot()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if apply {
|
||||
ensure!(
|
||||
serde_json::from_slice::<ipc::FileIdentity>(&lock)?
|
||||
== ipc::file_identity(&locks[1])?,
|
||||
"helper lock identity changed"
|
||||
);
|
||||
ensure!(both_stopped()?, "service started during recovery");
|
||||
let mut db = LocalJournals {
|
||||
agent: &mut a.db,
|
||||
consumption: &c.db,
|
||||
helper: &h.db,
|
||||
agent_binding: &agent,
|
||||
helper_binding: &settings.binding,
|
||||
};
|
||||
println!(
|
||||
"{}",
|
||||
db.apply(
|
||||
review.as_ref().context("missing real human receipt")?,
|
||||
chrono::Utc::now
|
||||
)?
|
||||
);
|
||||
} else {
|
||||
let fault: Option<String> =
|
||||
a.db.query_row("SELECT fault FROM reconcile_meta WHERE id=1", [], |r| {
|
||||
r.get(0)
|
||||
})?;
|
||||
ensure!(
|
||||
fault.is_none() || fault.as_deref() == Some(FAULT),
|
||||
"different fault"
|
||||
);
|
||||
let decisions = verify_local_history(
|
||||
&a.db,
|
||||
&c.db,
|
||||
&h.db,
|
||||
&agent,
|
||||
&settings.binding,
|
||||
fault.as_deref(),
|
||||
)?;
|
||||
ensure!(
|
||||
serde_json::Value::Array(decisions) == witness["decisions"],
|
||||
"unreviewed current history"
|
||||
);
|
||||
if let Some(review) = &review {
|
||||
review.timely(chrono::Utc::now())?;
|
||||
println!("valid human review for the verified local history");
|
||||
}
|
||||
println!(
|
||||
"verified contracted history; fault={}",
|
||||
fault.as_deref().unwrap_or("none")
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn main() -> anyhow::Result<()> {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
windows::run()
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
anyhow::bail!("native Windows operator executable")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,412 @@
|
||||
//! Transactional local half of the human-reviewed, zero-job test incident.
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
use prost::Message;
|
||||
use rmm_agent_core::{
|
||||
auth::Binding,
|
||||
crypto::{DalekVerifier, Ed25519Verifier},
|
||||
windows_sync_history::{FAULT, LAST, verify_local_history},
|
||||
};
|
||||
use rmm_domain::{checkpoint_repair::SignedReceipt, jobs::hash, windows_sync_history::Receipt};
|
||||
use rmm_protocol::{Envelope, Payload};
|
||||
use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params, types::ValueRef};
|
||||
use serde_json::Value;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const AUDIT: &str = "windows_human_checkpoint_recovery";
|
||||
const AUDIT_SQL: &str = "CREATE TABLE windows_human_checkpoint_recovery(id INTEGER PRIMARY KEY CHECK(id=1),review_id TEXT NOT NULL,receipt_id TEXT NOT NULL,old_fault TEXT NOT NULL,original_receipt BLOB NOT NULL,inventory_sha256 TEXT NOT NULL,applied_at TEXT NOT NULL)";
|
||||
pub struct Reviewed {
|
||||
receipt: Receipt,
|
||||
original: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Reviewed {
|
||||
pub fn verify(
|
||||
raw: Vec<u8>,
|
||||
agent: &Binding,
|
||||
helper: &Binding,
|
||||
expected: &Value,
|
||||
) -> Result<Self> {
|
||||
ensure!(raw.len() <= 65536, "recovery envelope size");
|
||||
let envelope: SignedReceipt = serde_json::from_slice(&raw)?;
|
||||
ensure!(
|
||||
envelope.payload.len() <= 16384 && envelope.signature.len() == 64,
|
||||
"recovery signature size"
|
||||
);
|
||||
for b in [agent, helper] {
|
||||
let key = hex_key(&b.public_key_hex)?;
|
||||
ensure!(
|
||||
expected["verification_key_id"] == hash(&key)
|
||||
&& expected["tenant_id"] == b.tenant_id.to_string()
|
||||
&& expected["agent_id"] == b.agent_id.to_string()
|
||||
&& expected["asset_id"] == b.asset_id.to_string()
|
||||
&& expected["enrollment_generation"] == b.enrollment_generation
|
||||
&& expected["assignment_version"] == b.assignment_version,
|
||||
"pinned independent role identity"
|
||||
);
|
||||
ensure!(
|
||||
DalekVerifier.verify(&key, &envelope.payload, &envelope.signature)?,
|
||||
"recovery signature"
|
||||
);
|
||||
}
|
||||
let receipt: Receipt = serde_json::from_slice(&envelope.payload)?;
|
||||
// Historical validity is sufficient for looking up a previously committed receipt.
|
||||
ensure!(
|
||||
receipt.matches_reviewed_witness(expected, receipt.issued_at),
|
||||
"historical human review"
|
||||
);
|
||||
Ok(Self {
|
||||
receipt,
|
||||
original: raw,
|
||||
})
|
||||
}
|
||||
pub fn timely(&self, now: DateTime<Utc>) -> Result<()> {
|
||||
ensure!(
|
||||
self.receipt.issued_at <= now && now < self.receipt.valid_until,
|
||||
"human receipt expired or premature"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn hex_key(s: &str) -> Result<Vec<u8>> {
|
||||
ensure!(s.len() == 64 && s.is_ascii(), "job pin shape");
|
||||
(0..64)
|
||||
.step_by(2)
|
||||
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).map_err(Into::into))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn include(hash: &mut Sha256, tag: u8, bytes: &[u8]) {
|
||||
hash.update([tag]);
|
||||
hash.update((bytes.len() as u64).to_le_bytes());
|
||||
hash.update(bytes);
|
||||
}
|
||||
|
||||
/// Compare the full logical contents and schema, excluding only the receipt and normalized fault.
|
||||
fn inventory(db: &Connection) -> Result<String> {
|
||||
let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?;
|
||||
ensure!(check == "ok", "journal integrity");
|
||||
let mut statement = db.prepare(
|
||||
"SELECT type,name,tbl_name,sql FROM sqlite_schema WHERE tbl_name<>?1 ORDER BY type,name",
|
||||
)?;
|
||||
let mut rows = statement.query([AUDIT])?;
|
||||
let mut tables = Vec::new();
|
||||
let mut digest = Sha256::new();
|
||||
while let Some(row) = rows.next()? {
|
||||
let kind: String = row.get(0)?;
|
||||
let name: String = row.get(1)?;
|
||||
let table: String = row.get(2)?;
|
||||
let sql: Option<String> = row.get(3)?;
|
||||
ensure!(
|
||||
matches!(kind.as_str(), "table" | "index" | "trigger")
|
||||
&& !(kind == "trigger" && table == "reconcile_meta")
|
||||
&& !sql
|
||||
.as_deref()
|
||||
.unwrap_or_default()
|
||||
.to_ascii_uppercase()
|
||||
.contains("VIRTUAL TABLE"),
|
||||
"unexpected journal schema program"
|
||||
);
|
||||
for s in [
|
||||
kind.as_str(),
|
||||
name.as_str(),
|
||||
table.as_str(),
|
||||
sql.as_deref().unwrap_or_default(),
|
||||
] {
|
||||
include(&mut digest, 0, s.as_bytes());
|
||||
}
|
||||
if kind == "table" {
|
||||
ensure!(
|
||||
tables.len() < 256
|
||||
&& !name.is_empty()
|
||||
&& name.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'_'),
|
||||
"unsupported journal table"
|
||||
);
|
||||
tables.push(name);
|
||||
}
|
||||
}
|
||||
for table in tables {
|
||||
include(&mut digest, 1, table.as_bytes());
|
||||
let mut q = db.prepare(&format!("SELECT rowid,* FROM \"{table}\" ORDER BY rowid"))?;
|
||||
let cols: Vec<_> = q.column_names().iter().map(|s| s.to_string()).collect();
|
||||
let mut rows = q.query([])?;
|
||||
while let Some(row) = rows.next()? {
|
||||
include(&mut digest, 2, &[]);
|
||||
for (i, name) in cols.iter().enumerate() {
|
||||
let value = row.get_ref(i)?;
|
||||
if table == "reconcile_meta" && name == "fault" {
|
||||
ensure!(
|
||||
value == ValueRef::Null || value == ValueRef::Text(FAULT.as_bytes()),
|
||||
"foreign fault"
|
||||
);
|
||||
include(&mut digest, 3, FAULT.as_bytes());
|
||||
continue;
|
||||
}
|
||||
match value {
|
||||
ValueRef::Null => include(&mut digest, 4, &[]),
|
||||
ValueRef::Integer(v) => include(&mut digest, 5, &v.to_le_bytes()),
|
||||
ValueRef::Real(v) => include(&mut digest, 6, &v.to_bits().to_le_bytes()),
|
||||
ValueRef::Text(bytes) => include(&mut digest, 7, bytes),
|
||||
ValueRef::Blob(bytes) => include(&mut digest, 8, bytes),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(format!("{:x}", digest.finalize()))
|
||||
}
|
||||
|
||||
pub struct LocalJournals<'a> {
|
||||
pub agent: &'a mut Connection,
|
||||
pub consumption: &'a Connection,
|
||||
pub helper: &'a Connection,
|
||||
pub agent_binding: &'a Binding,
|
||||
pub helper_binding: &'a Binding,
|
||||
}
|
||||
|
||||
impl LocalJournals<'_> {
|
||||
/// Only one database changes. Both service locks must span this transaction on Windows.
|
||||
pub fn apply(
|
||||
&mut self,
|
||||
reviewed: &Reviewed,
|
||||
now: impl Fn() -> DateTime<Utc>,
|
||||
) -> Result<&'static str> {
|
||||
let tx = self
|
||||
.agent
|
||||
.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||
let prior: Option<String> = tx
|
||||
.query_row(
|
||||
"SELECT sql FROM sqlite_schema WHERE name=?1",
|
||||
[AUDIT],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
let applied = prior.is_some();
|
||||
if let Some(schema) = prior {
|
||||
ensure!(
|
||||
schema == AUDIT_SQL
|
||||
&& tx.query_row(
|
||||
"SELECT count(*) FROM sqlite_schema WHERE tbl_name=?1",
|
||||
[AUDIT],
|
||||
|r| r.get::<_, usize>(0)
|
||||
)? == 1,
|
||||
"foreign archive"
|
||||
);
|
||||
let identical:bool=tx.query_row("SELECT EXISTS(SELECT 1 FROM windows_human_checkpoint_recovery WHERE id=1 AND review_id=? AND receipt_id=? AND old_fault=? AND original_receipt=?)",params![reviewed.receipt.request_id.to_string(),reviewed.receipt.receipt_id.to_string(),FAULT,reviewed.original],|r|r.get(0))?;
|
||||
ensure!(identical, "another recovery already exists");
|
||||
}
|
||||
let original = verify_local_history(
|
||||
&tx,
|
||||
self.consumption,
|
||||
self.helper,
|
||||
self.agent_binding,
|
||||
self.helper_binding,
|
||||
(!applied).then_some(FAULT),
|
||||
)?;
|
||||
ensure!(
|
||||
reviewed.receipt.witness["decisions"] == Value::Array(original),
|
||||
"receipt differs from local original decisions"
|
||||
);
|
||||
let current:bool=tx.query_row("SELECT EXISTS(SELECT 1 FROM reconcile_meta WHERE id=1 AND journal_id=? AND helper_id=? AND last_decision=? AND checkpoint=6756 AND helper_checkpoint=1 AND pending_request IS NULL)", params![reviewed.receipt.witness["agent_journal_id"].as_str().context("agent journal")?,reviewed.receipt.witness["helper_journal_id"].as_str().context("helper journal")?,LAST],|r|r.get(0))?;
|
||||
ensure!(current, "unreviewed journal identities");
|
||||
let count: u64 =
|
||||
self.helper
|
||||
.query_row("SELECT count(*) FROM helper_sync_rows", [], |r| r.get(0))?;
|
||||
ensure!(count == 0, "helper consumption rows");
|
||||
for db in [&*tx, self.consumption, self.helper] {
|
||||
let n:i64=db.query_row("SELECT count(*) FROM sqlite_schema WHERE type='view' OR (type='table' AND instr(upper(sql),'VIRTUAL TABLE')>0)",[],|r|r.get(0))?;
|
||||
ensure!(n == 0, "unexpected journal program");
|
||||
}
|
||||
let health_rows: i64 =
|
||||
tx.query_row("SELECT count(*) FROM update_health_inventory", [], |r| {
|
||||
r.get(0)
|
||||
})?;
|
||||
ensure!(health_rows == 0, "unreviewed update grant");
|
||||
for sql in [
|
||||
"SELECT id,payload FROM events",
|
||||
"SELECT id,payload FROM job_events",
|
||||
] {
|
||||
let mut q = tx.prepare(sql)?;
|
||||
let mut rows = q.query([])?;
|
||||
while let Some(row) = rows.next()? {
|
||||
let id: String = row.get(0)?;
|
||||
let bytes = row.get_ref(1)?.as_blob()?;
|
||||
ensure!(bytes.len() <= rmm_domain::MAX_MESSAGE, "event size");
|
||||
let e = Envelope::decode(bytes)?;
|
||||
ensure!(
|
||||
e.message_id == id && matches!(e.payload, Some(Payload::InventorySnapshot(_))),
|
||||
"unreviewed event"
|
||||
);
|
||||
}
|
||||
}
|
||||
if applied {
|
||||
let saved: String = tx.query_row(
|
||||
"SELECT inventory_sha256 FROM windows_human_checkpoint_recovery WHERE id=1",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(inventory(&tx)? == saved, "archive inventory changed");
|
||||
return Ok("already applied");
|
||||
}
|
||||
reviewed.timely(now())?;
|
||||
let before = inventory(&tx)?;
|
||||
tx.execute(AUDIT_SQL, [])?;
|
||||
tx.execute(
|
||||
"INSERT INTO windows_human_checkpoint_recovery VALUES(1,?,?,?,?,?,?)",
|
||||
params![
|
||||
reviewed.receipt.request_id.to_string(),
|
||||
reviewed.receipt.receipt_id.to_string(),
|
||||
FAULT,
|
||||
reviewed.original,
|
||||
before,
|
||||
now().to_rfc3339()
|
||||
],
|
||||
)?;
|
||||
ensure!(tx.execute("UPDATE reconcile_meta SET fault=NULL WHERE id=1 AND fault=?1 AND last_decision=?2 AND checkpoint=6756 AND generation=150 AND pending_request IS NULL",params![FAULT,LAST])?==1,"journal head changed");
|
||||
let cleared: bool = tx.query_row(
|
||||
"SELECT fault IS NULL FROM reconcile_meta WHERE id=1",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(
|
||||
cleared && inventory(&tx)? == before,
|
||||
"unrelated journal data changed"
|
||||
);
|
||||
reviewed.timely(now())?;
|
||||
tx.commit()?;
|
||||
Ok("review archived; normal signed sync required")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use chrono::Duration;
|
||||
use ed25519_dalek::{Signer, SigningKey};
|
||||
use rmm_domain::{
|
||||
checkpoint_repair::inventory_sha256,
|
||||
windows_sync_history::{RECEIPT_DOMAIN, case_witness},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
mod data {
|
||||
include!(concat!(
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/../agent-core/tests/support/windows_review_fixture.rs"
|
||||
));
|
||||
}
|
||||
fn signed(r: &Receipt, k: &SigningKey) -> Result<Vec<u8>> {
|
||||
let payload = serde_json::to_vec(r)?;
|
||||
Ok(serde_json::to_vec(&SignedReceipt {
|
||||
signature: k.sign(&payload).to_bytes().to_vec(),
|
||||
payload,
|
||||
})?)
|
||||
}
|
||||
#[test]
|
||||
fn archived_human_review_preserves_both_journals() -> Result<()> {
|
||||
let (mut a, l, h, binding, key) = data::verified_pair()?;
|
||||
let now = Utc::now();
|
||||
let mut witness = case_witness();
|
||||
let (journal, peer): (String, String) = a.query_row(
|
||||
"SELECT journal_id,helper_id FROM reconcile_meta WHERE id=1",
|
||||
[],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
witness["agent_journal_id"] = journal.into();
|
||||
witness["helper_journal_id"] = peer.into();
|
||||
witness["verification_key_id"] = hash(&key.verifying_key().to_bytes()).into();
|
||||
witness["tenant_id"] = binding.tenant_id.to_string().into();
|
||||
witness["asset_id"] = binding.asset_id.to_string().into();
|
||||
witness["decisions"] =
|
||||
verify_local_history(&a, &l, &h, &binding, &binding, Some(FAULT))?.into();
|
||||
let r = Receipt {
|
||||
domain: RECEIPT_DOMAIN.into(),
|
||||
schema_version: 1,
|
||||
request_id: Uuid::new_v4(),
|
||||
receipt_id: Uuid::new_v4(),
|
||||
key_id: hash(&key.verifying_key().to_bytes()),
|
||||
issued_at: now,
|
||||
valid_until: now + Duration::minutes(10),
|
||||
witness: witness.clone(),
|
||||
witness_sha256: inventory_sha256(&witness)?,
|
||||
confirmed_by: format!("oidc:{}", "a".repeat(64)),
|
||||
approval_origin: "human_webui".into(),
|
||||
testsimulation: false,
|
||||
ready: false,
|
||||
helper_ready: false,
|
||||
unlock: false,
|
||||
};
|
||||
let raw = signed(&r, &key)?;
|
||||
let ok = Reviewed::verify(raw.clone(), &binding, &binding, &witness)?;
|
||||
for e in 0..7 {
|
||||
let mut bad = r.clone();
|
||||
match e {
|
||||
0 => bad.testsimulation = true,
|
||||
1 => bad.ready = true,
|
||||
2 => bad.confirmed_by = "simulated-testprincipal-a".into(),
|
||||
3 => bad.approval_origin = "dev_test_browser_simulation".into(),
|
||||
4 => bad.unlock = true,
|
||||
5 => bad.witness["local_head_checkpoint"] = 6755.into(),
|
||||
_ => bad.domain = "rmm.reconciliation-decision".into(),
|
||||
}
|
||||
assert!(Reviewed::verify(signed(&bad, &key)?, &binding, &binding, &witness).is_err());
|
||||
}
|
||||
let foreign = SigningKey::from_bytes(&[7; 32]);
|
||||
assert!(Reviewed::verify(signed(&r, &foreign)?, &binding, &binding, &witness).is_err());
|
||||
let mut different = binding.clone();
|
||||
different.public_key_hex = "00".repeat(32);
|
||||
assert!(Reviewed::verify(raw.clone(), &binding, &different, &witness).is_err());
|
||||
let prior = inventory(&a)?;
|
||||
let local_before = inventory(&l)?;
|
||||
let helper_before = inventory(&h)?;
|
||||
let mut db = LocalJournals {
|
||||
agent: &mut a,
|
||||
consumption: &l,
|
||||
helper: &h,
|
||||
agent_binding: &binding,
|
||||
helper_binding: &binding,
|
||||
};
|
||||
assert!(db.apply(&ok, || now + Duration::minutes(10)).is_err());
|
||||
db.agent.execute_batch("CREATE TRIGGER unexpected AFTER UPDATE ON reconcile_meta BEGIN DELETE FROM job_attempts; END;")?;
|
||||
assert!(db.apply(&ok, || now).is_err());
|
||||
db.agent.execute_batch("DROP TRIGGER unexpected")?;
|
||||
db.agent
|
||||
.execute("INSERT INTO job_attempts VALUES('test')", [])?;
|
||||
assert!(db.apply(&ok, || now).is_err());
|
||||
db.agent.execute("DELETE FROM job_attempts", [])?;
|
||||
assert_eq!(
|
||||
db.apply(&ok, || now)?,
|
||||
"review archived; normal signed sync required"
|
||||
);
|
||||
assert_eq!(inventory(db.agent)?, prior);
|
||||
assert_eq!(inventory(&l)?, local_before);
|
||||
assert_eq!(inventory(&h)?, helper_before);
|
||||
let (fault, bytes): (String, Vec<u8>) = db.agent.query_row(
|
||||
"SELECT old_fault,original_receipt FROM windows_human_checkpoint_recovery",
|
||||
[],
|
||||
|q| Ok((q.get(0)?, q.get(1)?)),
|
||||
)?;
|
||||
assert_eq!(fault, FAULT);
|
||||
assert_eq!(bytes, raw);
|
||||
let status: Option<String> =
|
||||
db.agent
|
||||
.query_row("SELECT fault FROM reconcile_meta WHERE id=1", [], |q| {
|
||||
q.get(0)
|
||||
})?;
|
||||
assert!(status.is_none());
|
||||
assert_eq!(
|
||||
db.apply(&ok, || now + Duration::days(1))?,
|
||||
"already applied"
|
||||
);
|
||||
db.agent.execute_batch("CREATE TABLE unrelated(n)")?;
|
||||
assert!(db.apply(&ok, || now).is_err());
|
||||
db.agent.execute_batch("DROP TABLE unrelated")?;
|
||||
let mut other = r.clone();
|
||||
other.receipt_id = Uuid::new_v4();
|
||||
let other = Reviewed::verify(signed(&other, &key)?, &binding, &binding, &witness)?;
|
||||
assert!(db.apply(&other, || now).is_err());
|
||||
db.agent
|
||||
.execute("INSERT INTO job_attempts VALUES('later')", [])?;
|
||||
assert!(db.apply(&ok, || now).is_err());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user