Verify signed OPNsense kernel probes on the service account and retain exactly-once evidence
Rust development slice / linux (push) Failing after 13s
Rust development slice / linux (push) Failing after 13s
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
use super::*;
|
||||
use chrono::TimeDelta;
|
||||
use ed25519_dalek::{Signer, SigningKey};
|
||||
use serde_json::json;
|
||||
|
||||
/// Completely isolated identity, signing key, journal and grant. Never contacts
|
||||
/// the gateway or reads a device key; run the fixture under the guest service uid.
|
||||
#[test]
|
||||
fn native_probe_requires_signature_and_redelivery_does_not_rerun() -> Result<()> {
|
||||
ensure!(
|
||||
unsafe { libc::geteuid() } != 0,
|
||||
"run as unprivileged test account"
|
||||
);
|
||||
let key = SigningKey::from_bytes(&[31; 32]);
|
||||
let public = key.verifying_key().to_bytes();
|
||||
let b = Binding {
|
||||
tenant_id: Uuid::new_v4(),
|
||||
asset_id: Uuid::new_v4(),
|
||||
agent_id: Uuid::new_v4(),
|
||||
assignment_version: 1,
|
||||
enrollment_generation: 1,
|
||||
public_key_hex: hex::encode(public),
|
||||
};
|
||||
let db = Connection::open_in_memory()?;
|
||||
db.execute_batch("CREATE TABLE counter(id INTEGER PRIMARY KEY,seq INTEGER);INSERT INTO counter VALUES(1,0);
|
||||
CREATE TABLE job_attempts(job TEXT,attempt TEXT,offer BLOB,state TEXT,revision INTEGER DEFAULT 0,grant_id TEXT DEFAULT '',cancel_requested INTEGER DEFAULT 0,PRIMARY KEY(job,attempt));
|
||||
CREATE TABLE job_events(id TEXT PRIMARY KEY,payload BLOB);")?;
|
||||
reconcile::initialize(&db)?;
|
||||
let ledger = Connection::open_in_memory()?;
|
||||
ledger::initialize_ledger(&ledger)?;
|
||||
let mut w = Worker {
|
||||
channel: Channel {
|
||||
boot_id: Uuid::new_v4(),
|
||||
connection_generation: 2,
|
||||
},
|
||||
sync: ClientSync::new(b.clone()),
|
||||
binding: b.clone(),
|
||||
db,
|
||||
ledger,
|
||||
sync_due: Instant::now(),
|
||||
permit_after: Instant::now(),
|
||||
clock: NativeClock::default(),
|
||||
};
|
||||
let now = w.clock.now()?;
|
||||
let job = JobPayload {
|
||||
schema_version: 1,
|
||||
tenant_id: b.tenant_id,
|
||||
asset_id: b.asset_id,
|
||||
agent_id: b.agent_id,
|
||||
enrollment_generation: 1,
|
||||
assignment_version: 1,
|
||||
job_id: Uuid::new_v4(),
|
||||
attempt_id: Uuid::new_v4(),
|
||||
execution_identity: "agent_service".into(),
|
||||
max_runtime_seconds: 10,
|
||||
max_output_bytes: 1024,
|
||||
not_before: now - TimeDelta::minutes(1),
|
||||
start_before: now + TimeDelta::minutes(2),
|
||||
action: Action::RunScript {
|
||||
script_utf8: "uname -s\n".into(),
|
||||
expected_stdout: Some("FreeBSD\n".into()),
|
||||
arguments: vec![],
|
||||
interpreter: "posix_sh".into(),
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
},
|
||||
};
|
||||
let bytes = serde_json::to_vec(&job)?;
|
||||
let offer = JobOffer {
|
||||
job_id: job.job_id.to_string(),
|
||||
attempt_id: job.attempt_id.to_string(),
|
||||
plan_hash: hash(b"fixture"),
|
||||
authorization_id: Uuid::new_v4().to_string(),
|
||||
job_payload_hash: hash(&bytes),
|
||||
payload_bytes: bytes,
|
||||
};
|
||||
w.receive(w.envelope(Payload::JobOffer(offer.clone()))?)?;
|
||||
let event = w.next()?.unwrap();
|
||||
ensure!(matches!(event.payload, Some(Payload::JobAccepted(_))));
|
||||
jobs::ack(&mut w.db, &event.message_id)?;
|
||||
let Some(Payload::SyncRequest(r)) = w.next()?.unwrap().payload else {
|
||||
bail!("expected signed sync")
|
||||
};
|
||||
let decision = json!({"domain":"rmm.reconciliation-decision","schema_version":1,"decision_id":Uuid::new_v4(),
|
||||
"request_id":r.request_id,"request_hash":hash(&r.encode_to_vec()),"tenant_id":b.tenant_id,
|
||||
"asset_id":b.asset_id,"agent_id":b.agent_id,"enrollment_generation":1,"assignment_version":1,
|
||||
"connection_generation":2,"boot_id":w.channel.boot_id,"journal_id":r.journal_id,"checkpoint":r.checkpoint,
|
||||
"helper_journal_id":null,"helper_checkpoint":0,"issued_at":now-TimeDelta::seconds(1),"valid_until":now+TimeDelta::seconds(59),
|
||||
"ready":true,"helper_ready":false,"reason":"CONSISTENT","key_id":hash(&public),"acknowledged_message_ids":[],
|
||||
"attempts":[{"job_id":job.job_id,"attempt_id":job.attempt_id,"job_payload_hash":offer.job_payload_hash,
|
||||
"action":"run_script","execution_identity":"agent_service","revision":0,"outcome_state":"accepted","outcome_bytes":[],
|
||||
"unlock":false,"reason":"ACCEPTED_NOT_STARTED"}]});
|
||||
let raw = serde_json::to_vec(&decision)?;
|
||||
w.receive(w.envelope(Payload::SyncResponse(SyncResponse {
|
||||
signature: key.sign(&raw).to_bytes().to_vec(),
|
||||
decision_bytes: raw,
|
||||
}))?)?;
|
||||
ensure!(matches!(
|
||||
w.next()?.unwrap().payload,
|
||||
Some(Payload::ExecutionPermitRequest(_))
|
||||
));
|
||||
let raw = serde_json::to_vec(
|
||||
&json!({"domain":"rmm.execution-permit","schema_version":1,"grant_id":Uuid::new_v4(),
|
||||
"authorization_id":offer.authorization_id,"tenant_id":b.tenant_id,"asset_id":b.asset_id,"agent_id":b.agent_id,
|
||||
"enrollment_generation":1,"assignment_version":1,"job_id":job.job_id,"attempt_id":job.attempt_id,
|
||||
"plan_hash":offer.plan_hash,"job_payload_hash":offer.job_payload_hash,"action":"run_script",
|
||||
"execution_identity":"agent_service","not_before":now-TimeDelta::seconds(6),"start_before":now+TimeDelta::seconds(50),
|
||||
"max_runtime_seconds":10,"key_id":hash(&public)}),
|
||||
)?;
|
||||
let permit = ExecutionPermit {
|
||||
signature: key.sign(&raw).to_bytes().to_vec(),
|
||||
payload_bytes: raw,
|
||||
..Default::default()
|
||||
};
|
||||
let mut invalid = permit.clone();
|
||||
invalid.signature[0] ^= 1;
|
||||
ensure!(w.permit(invalid).is_err());
|
||||
let count =
|
||||
|db: &Connection| db.query_row("SELECT count(*) FROM consumed", [], |r| r.get::<_, i64>(0));
|
||||
ensure!(count(&w.ledger)? == 0);
|
||||
w.permit(permit.clone())?;
|
||||
w.receive(w.envelope(Payload::JobOffer(offer))?)?;
|
||||
ensure!(w.permit(permit).is_err() && count(&w.ledger)? == 1);
|
||||
let results = jobs::events(&w.db)?
|
||||
.into_iter()
|
||||
.filter_map(|e| match e.payload {
|
||||
Some(Payload::JobResult(r)) => Some(r),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
ensure!(
|
||||
results.len() == 1
|
||||
&& results[0].state == "succeeded"
|
||||
&& results[0].exit_code == Some(0)
|
||||
&& results[0].stdout == b"FreeBSD\n",
|
||||
"native output mismatch"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user