Permit proven closed-control fresh sync and retry only precommit clock reads
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
//! Fixed read-only fixture exporter / in-memory control contract. No executor.
|
||||
use anyhow::{Result, ensure};
|
||||
#[cfg(windows)]
|
||||
use serde_json::Value;
|
||||
#[cfg(windows)]
|
||||
fn export() -> Result<Value> {
|
||||
use prost::Message;
|
||||
use rusqlite::{Connection, OpenFlags, ToSql, types::ValueRef};
|
||||
use serde_json::json;
|
||||
use std::{ffi::c_void, os::windows::fs::MetadataExt, path::Path};
|
||||
#[link(name = "advapi32")]
|
||||
unsafe extern "system" {
|
||||
fn OpenProcessToken(p: *mut c_void, a: u32, t: *mut *mut c_void) -> i32;
|
||||
fn GetTokenInformation(t: *mut c_void, c: u32, v: *mut c_void, n: u32, r: *mut u32) -> i32;
|
||||
}
|
||||
#[link(name = "kernel32")]
|
||||
unsafe extern "system" {
|
||||
fn GetCurrentProcess() -> *mut c_void;
|
||||
fn CloseHandle(h: *mut c_void) -> i32;
|
||||
}
|
||||
let mut token = std::ptr::null_mut();
|
||||
ensure!(
|
||||
unsafe { OpenProcessToken(GetCurrentProcess(), 8, &mut token) } != 0,
|
||||
"FIXTURE_ADMIN_TOKEN"
|
||||
);
|
||||
let mut elevated = 0u32;
|
||||
let mut size = 0;
|
||||
let good =
|
||||
unsafe { GetTokenInformation(token, 20, (&mut elevated as *mut u32).cast(), 4, &mut size) };
|
||||
unsafe { CloseHandle(token) };
|
||||
ensure!(good != 0 && elevated != 0, "FIXTURE_ADMIN_REQUIRED");
|
||||
fn path(p: &str) -> Result<()> {
|
||||
for a in Path::new(p).ancestors() {
|
||||
ensure!(
|
||||
a.symlink_metadata()?.file_attributes() & 0x400 == 0,
|
||||
"FIXTURE_NO_REPARSE"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn open(p: &str) -> Result<Connection> {
|
||||
path(p)?;
|
||||
let d = Connection::open_with_flags(
|
||||
p,
|
||||
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
|
||||
)?;
|
||||
d.busy_timeout(std::time::Duration::from_millis(500))?;
|
||||
d.execute_batch("BEGIN")?;
|
||||
Ok(d)
|
||||
}
|
||||
fn rows(db: &Connection, sql: &str, args: &[&dyn ToSql]) -> Result<Value> {
|
||||
let mut q = db.prepare(sql)?;
|
||||
let n = q.column_count();
|
||||
let mut r = q.query(args)?;
|
||||
let mut out = vec![];
|
||||
while let Some(row) = r.next()? {
|
||||
ensure!(out.len() < 8, "FIXTURE_ROW_BOUND");
|
||||
let mut cols = vec![];
|
||||
for i in 0..n {
|
||||
cols.push(match row.get_ref(i)? {
|
||||
ValueRef::Null => json!(["null"]),
|
||||
ValueRef::Integer(v) => json!(["integer", v]),
|
||||
ValueRef::Text(v) => json!(["text", std::str::from_utf8(v)?]),
|
||||
ValueRef::Blob(v) => {
|
||||
ensure!(v.len() <= 1024 * 1024, "FIXTURE_BLOB_BOUND");
|
||||
json!(["blob", v])
|
||||
}
|
||||
_ => anyhow::bail!("FIXTURE_CELL_TYPE"),
|
||||
});
|
||||
}
|
||||
out.push(cols);
|
||||
}
|
||||
Ok(json!(out))
|
||||
}
|
||||
const A: &str = r"C:\ProgramData\RustDiscovery\state\telemetry.sqlite";
|
||||
const C: &str = r"C:\ProgramData\RustDiscovery\state\consumption.sqlite";
|
||||
const H: &str = r"C:\ProgramData\RustDiscoverySystem\state\ledger.sqlite";
|
||||
let a = open(A)?;
|
||||
let c = open(C)?;
|
||||
let h = open(H)?;
|
||||
let head_a:String=a.query_row("SELECT last_decision FROM reconcile_meta WHERE id=1 AND fault IS NULL AND pending_request IS NULL",[],|r|r.get(0))?;
|
||||
let (head_h, hcp): (String, u64) = h.query_row(
|
||||
"SELECT last_decision,checkpoint FROM helper_sync_meta WHERE id=1 AND fault IS NULL",
|
||||
[],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
ensure!(
|
||||
head_a == "7ea5dbda-2b61-4bb2-9c3c-3dbb4037e33a"
|
||||
&& head_h == "7245d5c4-e078-4c76-a55f-d4afbca405ba",
|
||||
"FIXTURE_EXACT_OBSERVED_HEADS"
|
||||
);
|
||||
let mut ids = vec![];
|
||||
for id in [&head_a, &head_h] {
|
||||
let raw: Vec<u8> = c.query_row(
|
||||
"SELECT response FROM reconcile_consumption WHERE decision_id=?1",
|
||||
[id],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let response = rmm_protocol::SyncResponse::decode(raw.as_slice())?;
|
||||
let d: rmm_domain::reconciliation::ReconcileDecisionPayload =
|
||||
serde_json::from_slice(&response.decision_bytes)?;
|
||||
ensure!(d.decision_id.to_string() == *id, "FIXTURE_ACK_ID");
|
||||
ids.push(d.request_id.to_string());
|
||||
}
|
||||
let binding_path = r"C:\ProgramData\RustDiscovery\binding.json";
|
||||
path(binding_path)?;
|
||||
let raw = std::fs::read(binding_path)?;
|
||||
ensure!(raw.len() <= 8192, "FIXTURE_PUBLIC_BINDING_BOUND");
|
||||
let binding: rmm_agent_core::auth::Binding = serde_json::from_slice(&raw)?;
|
||||
ensure!(
|
||||
binding.agent_id.to_string() == "63ea15b2-0643-403d-b5b4-cba1288dbea3"
|
||||
&& binding.asset_id.to_string() == "a176520e-8a91-4e93-a78b-8e7620f430e0",
|
||||
"FIXTURE_EXACT_TEST_GUEST"
|
||||
);
|
||||
let resolution = "SELECT grant_id,role,receipt,receipt_sha256,derived_outcome FROM windows_system_nonstart_221_resolution LIMIT 9";
|
||||
let value = json!({"schema_version":1,"read_only":true,"database_writes":false,"private_keys_read":false,"captured_at_utc":chrono::Utc::now(),"binding":binding,
|
||||
"agent":{
|
||||
"meta":rows(&a,"SELECT id,journal_id,checkpoint,high_sequence,generation,last_decision,pending_request,helper_id,helper_checkpoint,fault FROM reconcile_meta",&[])?,
|
||||
"counter":rows(&a,"SELECT id,seq FROM counter",&[])?,
|
||||
"requests":rows(&a,"SELECT id,request,envelope,generation,response FROM reconcile_requests WHERE id IN (?1,?2) ORDER BY rowid",&[&ids[0],&ids[1]])?,
|
||||
"jobs":rows(&a,"SELECT job,attempt,offer,state,revision,grant_id FROM job_attempts ORDER BY job LIMIT 9",&[])?,
|
||||
"outcomes":rows(&a,"SELECT job,attempt,outcome,decision_id FROM reconcile_outcomes ORDER BY job LIMIT 9",&[])?,
|
||||
"results":rows(&a,"SELECT id,job,attempt,revision,envelope FROM reconcile_results ORDER BY rowid LIMIT 9",&[])?,
|
||||
"events":rows(&a,"SELECT id,payload FROM job_events ORDER BY rowid LIMIT 9",&[])?,
|
||||
"legacy_count":a.query_row("SELECT count(*) FROM events",[],|r|r.get::<_,u64>(0))?,
|
||||
"resolution":rows(&a,resolution,&[])?},
|
||||
"ledger":{
|
||||
"consumed":rows(&c,"SELECT grant_id,agent,generation,job,attempt,result FROM consumed ORDER BY grant_id LIMIT 9",&[])?,
|
||||
"evidence":rows(&c,"SELECT grant_id,start,outcome FROM execution_evidence ORDER BY grant_id LIMIT 9",&[])?,
|
||||
"acks":rows(&c,"SELECT decision_id,response FROM reconcile_consumption WHERE decision_id IN (?1,?2)",&[&head_a,&head_h])?,
|
||||
"resolution":rows(&c,resolution,&[])?},
|
||||
"helper":{
|
||||
"meta":rows(&h,"SELECT journal_id,checkpoint,generation,last_decision,agent_journal_id,agent_checkpoint,fault FROM helper_sync_meta WHERE id=1",&[])?,
|
||||
"capture":rows(&h,"SELECT checkpoint,snapshot,fingerprint,captured_at FROM helper_sync_captures WHERE checkpoint=?1",&[&hcp])?,
|
||||
"decision":rows(&h,"SELECT decision_id,checkpoint,response,request,fingerprint FROM helper_sync_decisions WHERE decision_id=?1",&[&head_h])?}});
|
||||
a.execute_batch("ROLLBACK")?;
|
||||
c.execute_batch("ROLLBACK")?;
|
||||
h.execute_batch("ROLLBACK")?;
|
||||
ensure!(
|
||||
a.query_row(
|
||||
"SELECT last_decision FROM reconcile_meta WHERE id=1",
|
||||
[],
|
||||
|r| r.get::<_, String>(0)
|
||||
)? == head_a
|
||||
&& h.query_row(
|
||||
"SELECT last_decision FROM helper_sync_meta WHERE id=1",
|
||||
[],
|
||||
|r| r.get::<_, String>(0)
|
||||
)? == head_h,
|
||||
"FIXTURE_AUTHORITATIVE_HEAD_CHANGED"
|
||||
);
|
||||
ensure!(
|
||||
serde_json::to_vec(&value)?.len() <= 4 * 1024 * 1024,
|
||||
"FIXTURE_TOTAL_BOUND"
|
||||
);
|
||||
Ok(value)
|
||||
}
|
||||
fn main() -> Result<()> {
|
||||
let args = std::env::args().skip(1).collect::<Vec<_>>();
|
||||
if args == ["--export"] {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
println!("{}", export()?);
|
||||
return Ok(());
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
anyhow::bail!("FIXTURE_EXPORT_WINDOWS_ONLY");
|
||||
}
|
||||
ensure!(
|
||||
args.len() == 2 && args[0] == "--contract",
|
||||
"FIXTURE_FIXED_ARGUMENTS"
|
||||
);
|
||||
let raw = std::fs::read(&args[1])?;
|
||||
ensure!(raw.len() <= 4 * 1024 * 1024, "FIXTURE_TOTAL_BOUND");
|
||||
println!(
|
||||
"{}",
|
||||
rmm_agent_core::reconcile::closed_control_contract(&raw)?
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user