Compare commits

..
Author SHA1 Message Date
kevin_janssen 6792b0dd11 Verify signed OPNsense kernel probes on the service account and retain exactly-once evidence
Rust development slice / linux (push) Failing after 13s
2026-11-19 20:55:29 +01:00
kevin_janssen 2e060e9507 Plan an exact service-account OPNsense kernel probe through normal approval 2026-11-19 20:48:41 +01:00
kevin_janssen 71adc2adf6 Record live OPNsense enrollment, WireGuard handshakes and remaining job parity
Rust development slice / linux (push) Failing after 14s
2026-11-19 20:14:34 +01:00
kevin_janssen 8acbee6a60 Qualify FreeBSD kernel time and supervise bounded POSIX process groups natively 2026-11-19 20:14:34 +01:00
kevin_janssen 6bf6e2539c Exercise appliance secret projection, truncation and device-only UI states
Rust development slice / linux (push) Failing after 12s
2026-11-19 20:10:36 +01:00
kevin_janssen 50b1fa4033 Run bounded OPNsense inventory and durable mTLS heartbeats as separate FreeBSD services
Rust development slice / linux (push) Failing after 11s
2026-11-19 20:07:49 +01:00
kevin_janssen b7d9046996 Project native FreeBSD packages, interfaces, hardware and login observations 2026-11-19 20:06:56 +01:00
kevin_janssen d05caf6fd3 Allow OPNsense snapshots in the tenant-bound observations table 2026-11-19 19:54:55 +01:00
kevin_janssen 89a58c609c Accept separate OPNsense inventory and show VPN and PF observations in device details
Rust development slice / linux (push) Failing after 13s
2026-11-19 19:53:51 +01:00
37 changed files with 1809 additions and 13 deletions
Generated
+20
View File
@@ -2187,6 +2187,7 @@ dependencies = [
"chrono", "chrono",
"ed25519-dalek", "ed25519-dalek",
"hex", "hex",
"libc",
"openssl", "openssl",
"prost", "prost",
"reqwest", "reqwest",
@@ -2285,6 +2286,25 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "rmm-opnsense-jobs"
version = "0.1.0"
dependencies = [
"anyhow",
"chrono",
"ed25519-dalek",
"hex",
"libc",
"prost",
"rmm-agent-core",
"rmm-agent-linux",
"rmm-domain",
"rmm-protocol",
"rusqlite",
"serde_json",
"uuid",
]
[[package]] [[package]]
name = "rmm-persistence" name = "rmm-persistence"
version = "0.1.0" version = "0.1.0"
+1 -1
View File
@@ -1,7 +1,7 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
exclude = ["crates/oidc-auth"] exclude = ["crates/oidc-auth"]
members = ["crates/domain", "crates/protocol", "crates/persistence", "crates/server", "crates/agent-linux", "crates/agent-core", "crates/agent-windows", "crates/agent", "crates/privileged-helper", "crates/updater", "crates/updater-handoff", "crates/updater-linux", "crates/updater-worker", "crates/update-history", "crates/time-attestation", "crates/time-source-linux", "crates/time-broker-linux"] members = ["crates/domain", "crates/protocol", "crates/persistence", "crates/server", "crates/agent-linux", "crates/agent-core", "crates/agent-windows", "crates/agent", "crates/agent-opnsense-jobs", "crates/privileged-helper", "crates/updater", "crates/updater-handoff", "crates/updater-linux", "crates/updater-worker", "crates/update-history", "crates/time-attestation", "crates/time-source-linux", "crates/time-broker-linux"]
[workspace.package] [workspace.package]
version = "0.1.0" version = "0.1.0"
@@ -24,6 +24,8 @@ Die Spalte **Zweck** und das Gerätedetail zeigen optionalen Freitext. Unter **Z
## Vom Skript zum Ergebnis ## Vom Skript zum Ergebnis
**OPNsense-Testprofil:** Das Gerätegateway akzeptiert `opnsense.base/1` als eigene Inventarplattform. Im Gerätedetail erscheinen aktive PF-Filterregeln sowie getrennte Listen für konfigurierte VPNs und native Laufzeitbefunde. Fehlende Berechtigung bzw. ausgefallene Quellen sind kein Nachweis einer leeren Liste. Nur mit ausdrücklich eingerichteter Capability `opnsense.uname/1`, `run_script` und signiertem Abgleich kann die feste POSIX-Kernelabfrage auf dem Dienstkonto angefordert werden. Sie nutzt die reguläre persönliche Genehmigung und genau einen gebundenen Versuch. [OPNsense-Betrieb](deploy/opnsense/README.md)
| Schritt | Verhalten | | Schritt | Verhalten |
| --- | --- | | --- | --- |
| Skript veröffentlichen | In der Bibliothek eines Kunden erhält ein Skript einen Namen, einen Interpreter, einen Standard-Ausführungskontext und eine unveränderliche Version. Eine Bearbeitung veröffentlicht eine weitere Version. | | Skript veröffentlichen | In der Bibliothek eines Kunden erhält ein Skript einen Namen, einen Interpreter, einen Standard-Ausführungskontext und eine unveränderliche Version. Eine Bearbeitung veröffentlicht eine weitere Version. |
+3
View File
@@ -31,3 +31,6 @@ openssl = { workspace = true, optional = true }
[target.'cfg(unix)'.dev-dependencies] [target.'cfg(unix)'.dev-dependencies]
openssl.workspace = true openssl.workspace = true
[target.'cfg(target_os = "freebsd")'.dependencies]
libc = "0.2"
+81
View File
@@ -0,0 +1,81 @@
//! Read-only FreeBSD kernel discipline for the signed reconciliation clock.
//! Never adjust the guest clock. No cached qualification across a failed read.
use crate::reconcile::{ClockReading, SyncClock};
use anyhow::{Context, Result, ensure};
use chrono::{DateTime, TimeDelta, Utc};
use std::{mem::MaybeUninit, time::Instant};
pub struct NativeClock {
origin: DateTime<Utc>,
monotonic: Instant,
}
impl Default for NativeClock {
fn default() -> Self {
Self {
origin: Utc::now(),
monotonic: Instant::now(),
}
}
}
impl NativeClock {
fn observation(&self) -> Result<(DateTime<Utc>, TimeDelta)> {
let start = Instant::now();
let mut raw = MaybeUninit::<libc::ntptimeval>::zeroed();
// SAFETY: the read-only syscall writes the platform's ntptimeval.
let result = unsafe { libc::ntp_gettime(raw.as_mut_ptr()) };
ensure!(result == libc::TIME_OK, "FREEBSD_CLOCK_UNQUALIFIED");
let sample = unsafe { raw.assume_init() };
let wall =
DateTime::from_timestamp(sample.time.tv_sec, u32::try_from(sample.time.tv_nsec)?)
.context("FREEBSD_CLOCK_INVALID")?;
let duration = TimeDelta::from_std(start.elapsed())?;
ensure!(
sample.time_state == libc::TIME_OK
&& (0..=5_000_000).contains(&sample.maxerror)
&& (0..=sample.maxerror).contains(&sample.esterror)
&& sample.time.tv_nsec < 1_000_000_000
&& duration <= TimeDelta::milliseconds(100)
&& ((wall - self.origin) - TimeDelta::from_std(self.monotonic.elapsed())?)
.num_milliseconds()
.unsigned_abs()
<= 5000,
"FREEBSD_CLOCK_UNQUALIFIED"
);
Ok((wall, TimeDelta::microseconds(sample.maxerror) + duration))
}
}
impl SyncClock for NativeClock {
fn now(&self) -> Result<DateTime<Utc>> {
Ok(self.observation()?.0)
}
fn reading(&self) -> Result<ClockReading> {
let (wall, error) = self.observation()?;
Ok(ClockReading {
own_wall: wall,
upper: wall + error,
})
}
fn bounds(&self) -> Result<(DateTime<Utc>, DateTime<Utc>)> {
let (wall, error) = self.observation()?;
Ok((wall - error, wall + error))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore = "requires a FreeBSD guest synchronized by ntpd"]
fn native_bounds_and_monotonic_fence() -> Result<()> {
let mut clock = NativeClock::default();
let (low, high) = clock.bounds()?;
ensure!(low <= high && high - low <= TimeDelta::milliseconds(10200));
clock.origin += TimeDelta::minutes(1);
ensure!(clock.bounds().is_err());
Ok(())
}
}
+2
View File
@@ -5,6 +5,8 @@
pub mod auth; pub mod auth;
pub mod customer_transfer; pub mod customer_transfer;
pub mod crypto; pub mod crypto;
#[cfg(target_os = "freebsd")]
pub mod freebsd_clock;
pub mod helper_reconcile; pub mod helper_reconcile;
pub mod jobs; pub mod jobs;
pub mod ledger; pub mod ledger;
+44 -1
View File
@@ -114,7 +114,7 @@ pub(crate) fn run_controlled(
let state = unsafe { let state = unsafe {
libc::waitid( libc::waitid(
libc::P_PID, libc::P_PID,
child.id(), child.id() as libc::id_t,
info.as_mut_ptr(), info.as_mut_ptr(),
libc::WEXITED | libc::WNOHANG | libc::WNOWAIT, libc::WEXITED | libc::WNOHANG | libc::WNOWAIT,
) )
@@ -232,6 +232,7 @@ fn drain(
} }
Ok(false) Ok(false)
} }
#[cfg(not(target_os = "freebsd"))]
fn group_has_no_live_members(pgid: u32) -> bool { fn group_has_no_live_members(pgid: u32) -> bool {
let Ok(entries) = std::fs::read_dir("/proc") else { let Ok(entries) = std::fs::read_dir("/proc") else {
return false; return false;
@@ -265,6 +266,48 @@ fn group_has_no_live_members(pgid: u32) -> bool {
true true
} }
#[cfg(target_os = "freebsd")]
fn group_has_no_live_members(pgid: u32) -> bool {
// FreeBSD has no Linux /proc/<pid>/stat. Ask the kernel for this
// process group while its original leader's PID is still reserved.
use std::{mem, ptr};
let mib = [
libc::CTL_KERN,
libc::KERN_PROC,
libc::KERN_PROC_PGRP,
pgid as i32,
];
let mut size = 0;
let query = |out, n: &mut usize| unsafe {
libc::sysctl(mib.as_ptr(), mib.len() as _, out, n, ptr::null(), 0)
};
let unit = mem::size_of::<libc::kinfo_proc>();
if query(ptr::null_mut(), &mut size) != 0 || size == 0 || size > 16 * 1024 * 1024 {
return false;
}
let mut rows = vec![mem::MaybeUninit::<libc::kinfo_proc>::zeroed(); size.div_ceil(unit) + 1];
size = rows.len() * unit;
if query(rows.as_mut_ptr().cast(), &mut size) != 0
|| !size.is_multiple_of(unit)
|| size / unit > rows.len()
{
return false;
}
let mut leader = false;
for row in &rows[..size / unit] {
// A successful MIB read returned this complete aligned record.
let row = unsafe { row.assume_init_ref() };
if row.ki_structsize as usize != unit
|| row.ki_pgid != pgid as i32
|| row.ki_stat != libc::SZOMB
{
return false;
}
leader |= row.ki_pid == pgid as i32;
}
leader
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
+22
View File
@@ -0,0 +1,22 @@
[package]
name = "rmm-opnsense-jobs"
version.workspace = true
edition.workspace = true
license.workspace = true
[target.'cfg(target_os = "freebsd")'.dependencies]
anyhow.workspace = true
chrono.workspace = true
hex.workspace = true
libc = "0.2"
prost.workspace = true
rmm-agent-core = { path = "../agent-core" }
rmm-agent-linux = { path = "../agent-linux" }
rmm-domain = { path = "../domain" }
rmm-protocol = { path = "../protocol" }
rusqlite.workspace = true
serde_json.workspace = true
uuid.workspace = true
[target.'cfg(target_os = "freebsd")'.dev-dependencies]
ed25519-dalek = "=2.2.0"
+11
View File
@@ -0,0 +1,11 @@
#[cfg(target_os = "freebsd")]
mod native;
#[cfg(target_os = "freebsd")]
fn main() -> anyhow::Result<()> {
native::main()
}
#[cfg(not(target_os = "freebsd"))]
fn main() {
eprintln!("FreeBSD required");
std::process::exit(1);
}
+398
View File
@@ -0,0 +1,398 @@
//! One worker per authenticated Python HTTPS channel, never a network listener.
//! Only the fixed unprivileged probe is executable. The shared Rust core owns
//! all approval/permit verification, receipt, consumption and sync transitions.
use anyhow::{Context, Result, bail, ensure};
use chrono::Utc;
use prost::Message;
use rmm_agent_core::{
auth::{Binding, Outcome},
freebsd_clock::NativeClock,
jobs, ledger,
reconcile::{self, Channel, ClientSync, SyncClock},
};
use rmm_agent_linux::actions::{Interpreter, ScriptRequest, run_script_controlled};
use rmm_domain::jobs::{Action, JobPayload, hash};
use rmm_protocol::*;
use rusqlite::{Connection, params};
use std::{
env, fs,
io::{self, BufRead, Read, Write},
os::{
fd::AsRawFd,
unix::fs::{MetadataExt, OpenOptionsExt},
},
path::Path,
time::{Duration, Instant},
};
use uuid::Uuid;
#[cfg(test)]
mod tests;
fn supported(job: &JobPayload) -> bool {
job.execution_identity == "agent_service"
&& job.action.opnsense_kernel_probe()
&& job.max_runtime_seconds == 10
&& job.max_output_bytes == 1024
}
struct Worker {
binding: Binding,
channel: Channel,
sync: ClientSync,
clock: NativeClock,
db: Connection,
ledger: Connection,
sync_due: Instant,
permit_after: Instant,
}
impl Worker {
fn envelope(&self, payload: Payload) -> Result<Envelope> {
let seq = self.db.query_row(
"UPDATE counter SET seq=seq+1 WHERE id=1 RETURNING seq",
[],
|r| r.get(0),
)?;
Ok(Envelope {
protocol_major: 1,
message_id: Uuid::new_v4().to_string(),
agent_id: self.binding.agent_id.to_string(),
enrollment_generation: self.binding.enrollment_generation,
connection_generation: self.channel.connection_generation,
boot_id: self.channel.boot_id.to_string(),
sequence: seq,
observed_at: Utc::now().timestamp_millis(),
payload: Some(payload),
..Default::default()
})
}
fn result(&mut self, offer: &JobOffer, grant: &str, outcome: Outcome) -> Result<()> {
let mut e = self.envelope(Payload::JobResult(Default::default()))?;
let tx = self.db.transaction()?;
let rev = jobs::transition(&tx, offer, &outcome.state, grant)?;
e.payload = Some(Payload::JobResult(jobs::result(offer, rev, grant, outcome)));
jobs::event(&tx, &e)?;
tx.commit()?;
self.sync_due = Instant::now();
Ok(())
}
fn recover(&mut self) -> Result<()> {
for (offer, grant) in jobs::interrupted(&self.db, &self.ledger, &self.binding)? {
if grant.is_empty() {
jobs::interrupted_request(&self.db, &offer)?;
} else {
let saved =
jobs::recover_interrupted_outcome(&self.ledger, &self.binding, &offer, &grant)?;
self.result(
&offer,
&grant,
saved.unwrap_or_else(|| Outcome::unknown("AGENT_PROCESS_INTERRUPTED")),
)?;
}
}
Ok(())
}
fn next(&mut self) -> Result<Option<Envelope>> {
if let Some(mut e) = jobs::events(&self.db)?.into_iter().next() {
e.connection_generation = self.channel.connection_generation;
return Ok(Some(e));
}
// A refused or unqualified clock closes the effect gate, not telemetry.
if Instant::now() >= self.sync_due {
self.sync_due = Instant::now() + Duration::from_secs(40);
if self.clock.now().is_err() {
self.sync.close();
return Ok(None);
}
let p = self.sync.prepare(
&mut self.db,
&self.ledger,
self.channel.clone(),
None,
&self.clock,
)?;
return Ok(Some(p.envelope));
}
if self.sync.can_start(&self.channel, false, &self.clock)
&& Instant::now() >= self.permit_after
&& let Some(offer) = jobs::next(&self.db)?
{
let job = self.binding.job(&offer.payload_bytes)?;
if self.clock.now()? >= job.start_before {
self.result(
&offer,
"",
jobs::no_start("expired", "START_DEADLINE_EXPIRED"),
)?;
return self.next();
}
ensure!(supported(&job), "UNSUPPORTED_JOB");
jobs::reserve_request(&self.db, &offer)?;
return self
.envelope(Payload::ExecutionPermitRequest(ExecutionPermitRequest {
job_id: offer.job_id,
attempt_id: offer.attempt_id,
job_payload_hash: offer.job_payload_hash,
assignment_version: self.binding.assignment_version,
}))
.map(Some);
}
Ok(None)
}
fn receive(&mut self, e: Envelope) -> Result<()> {
ensure!(
valid_envelope(
&e,
&self.binding.agent_id.to_string(),
self.channel.connection_generation
) && e.enrollment_generation == self.binding.enrollment_generation,
"WRONG_CHANNEL"
);
match e.payload.context("EMPTY_REPLY")? {
Payload::Ack(a) => jobs::ack(&mut self.db, &a.message_id)?,
Payload::SyncResponse(s) => {
let applied = self.sync.apply(&mut self.db, &s, &self.clock)?;
ensure!(applied.helper_instruction.is_none(), "UNEXPECTED_HELPER");
self.sync.apply_consumption(&mut self.ledger, &self.clock)?;
}
Payload::JobOffer(o) => {
let receipt = self.envelope(Payload::JobAccepted(JobAccepted {
job_id: o.job_id.clone(),
attempt_id: o.attempt_id.clone(),
job_payload_hash: o.job_payload_hash.clone(),
journal_proof: hash(&o.encode_to_vec()),
}))?;
let tx = self.db.transaction()?;
let fresh = jobs::accept(&tx, &self.binding, &o, supported)?;
jobs::event(&tx, &receipt)?;
tx.commit()?;
if fresh {
self.sync_due = Instant::now();
}
}
Payload::ExecutionPermit(p) => self.permit(p)?,
Payload::CancelRequest(c) => {
let offer:Option<Vec<u8>>=rusqlite::OptionalExtension::optional(self.db.query_row(
"UPDATE job_attempts SET cancel_requested=1 WHERE job=?1 AND attempt=?2 AND state='accepted' RETURNING offer",
params![c.job_id,c.attempt_id],|r|r.get(0)))?;
if let Some(bytes) = offer {
self.result(
&JobOffer::decode(bytes.as_slice())?,
"",
jobs::no_start("cancelled", "CANCELLED_BEFORE_START"),
)?;
}
}
_ => bail!("UNEXPECTED_JOB_MESSAGE"),
}
Ok(())
}
fn permit(&mut self, p: ExecutionPermit) -> Result<()> {
let offer = jobs::requesting(&self.db)?.context("UNSOLICITED_PERMIT")?;
if !p.denial_code.is_empty() {
jobs::denied_request(&self.db, &offer, &p)?;
self.sync.close();
self.sync_due = Instant::now();
self.permit_after = Instant::now() + Duration::from_secs(30);
return Ok(());
}
let req = jobs::permit_request(&offer, &p)?;
let verify = || -> Result<_> {
ensure!(
self.sync.can_start(&self.channel, false, &self.clock),
"SYNC_REQUIRED"
);
let (low, high) = self.clock.bounds()?;
self.binding.verify(&req, high)?;
self.binding.verify(&req, low)
};
// The signed not_before is the reservation instant. Wait within the
// qualified uncertainty interval before checking both ends at start.
let began = Instant::now();
let (job, grant) = loop {
match verify() {
Ok(x) => break x,
Err(err)
if err.to_string() == "PERMIT_TIME"
&& began.elapsed() < Duration::from_secs(6) =>
{
std::thread::sleep(Duration::from_millis(50));
}
Err(e) => return Err(e),
}
};
ensure!(supported(&job), "UNSUPPORTED_JOB");
let Action::RunScript { script_utf8, .. } = &job.action else {
bail!("SCRIPT_REQUIRED")
};
let script = ScriptRequest::new(
script_utf8.as_bytes().to_vec(),
Interpreter::PosixSh,
vec![],
Duration::from_secs(job.max_runtime_seconds.into()),
job.max_output_bytes as usize,
)?;
ledger::begin_execution(
&mut self.ledger,
&grant,
&req,
self.channel.boot_id,
self.clock.now()?,
)?;
let id = grant.grant_id.to_string();
let mut running = self.envelope(Payload::JobStateChanged(Default::default()))?;
let tx = self.db.transaction()?;
let revision = jobs::transition(&tx, &offer, "running", &id)?;
running.payload = Some(Payload::JobStateChanged(JobStateChanged {
job_id: offer.job_id.clone(),
attempt_id: offer.attempt_id.clone(),
grant_id: id.clone(),
revision,
state: "running".into(),
}));
jobs::event(&tx, &running)?;
tx.commit()?;
let mut first = true;
let result = run_script_controlled(&script, || {
if first {
ensure!(
self.sync.can_start(&self.channel, false, &self.clock),
"SYNC_EXPIRED"
);
let (lo, hi) = self.clock.bounds()?;
self.binding.verify(&req, lo)?;
self.binding.verify(&req, hi)?;
first = false;
} else {
self.clock.now()?;
}
Ok(())
});
let outcome = match result {
Ok(o) => {
let ok = o.code == Some(0)
&& !o.timed_out
&& !o.interrupted
&& !o.truncated
&& o.stdout == b"FreeBSD\n";
Outcome {
state: if ok { "succeeded" } else { "failed" }.into(),
stdout: o.stdout,
stderr: o.stderr,
output_truncated: o.truncated,
reboot_required: false,
exit_code: o.code,
evidence: serde_json::json!({"postconditions_passed":ok,"effective_uid":unsafe{libc::geteuid()}}),
error_code: if ok { "" } else { "PROBE_POSTCONDITION_FAILED" }.into(),
}
}
Err(_) => Outcome::unknown("PROBE_EFFECT_UNCERTAIN"),
};
ledger::persist_outcome(&self.ledger, &grant, &outcome)?;
let saved = serde_json::to_vec(&outcome)?;
self.result(&offer, &id, outcome)?;
ledger::finish(&self.ledger, &grant, &serde_json::from_slice(&saved)?)?;
Ok(())
}
}
pub fn main() -> Result<()> {
let args = env::args().skip(1).collect::<Vec<_>>();
ensure!(args.len() == 4, "bound channel required");
let uid = unsafe { libc::geteuid() };
ensure!(
uid != 0 && uid == unsafe { libc::getuid() },
"SERVICE_ACCOUNT_REQUIRED"
);
unsafe { libc::umask(0o077) };
let state = Path::new("/var/db/discovery-agent");
let m = fs::symlink_metadata(state)?;
ensure!(
m.is_dir() && m.uid() == uid && m.mode() & 0o077 == 0,
"PRIVATE_STATE_REQUIRED"
);
let lock = fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(false)
.mode(0o600)
.custom_flags(libc::O_NOFOLLOW)
.open(state.join("job-worker.lock"))?;
ensure!(
unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0,
"ACTIVE_JOB_WORKER"
);
let config = Path::new("/usr/local/etc/discovery-agent/job-binding.json");
for path in [config.parent().unwrap(), config] {
let m = fs::symlink_metadata(path)?;
ensure!(
!m.is_symlink() && m.uid() == 0 && m.mode() & 0o022 == 0,
"PROTECTED_BINDING_REQUIRED"
);
}
let binding: Binding = serde_json::from_slice(&fs::read(config)?)?;
ensure!(
args[0] == binding.agent_id.to_string() && args[1] == binding.asset_id.to_string(),
"WRONG_DEVICE"
);
let channel = Channel {
boot_id: args[2].parse()?,
connection_generation: args[3].parse()?,
};
ensure!(
channel.connection_generation > 0 && !channel.boot_id.is_nil(),
"CHANNEL_REQUIRED"
);
let db = Connection::open(state.join("channel.sqlite"))?;
ledger::bounded_journal_capacity(&db)?;
db.execute_batch("PRAGMA synchronous=FULL;
CREATE TABLE IF NOT EXISTS counter(id INTEGER PRIMARY KEY CHECK(id=1),seq INTEGER NOT NULL);
INSERT OR IGNORE INTO counter SELECT id,n FROM sequence;
UPDATE counter SET seq=max(seq,(SELECT n FROM sequence WHERE id=1)) WHERE id=1;
CREATE TRIGGER IF NOT EXISTS telemetry_sequence AFTER UPDATE ON sequence WHEN NEW.n>(SELECT seq FROM counter WHERE id=1)
BEGIN UPDATE counter SET seq=NEW.n WHERE id=1; END;
CREATE TRIGGER IF NOT EXISTS job_sequence AFTER UPDATE ON counter WHEN NEW.seq>(SELECT n FROM sequence WHERE id=1)
BEGIN UPDATE sequence SET n=NEW.seq WHERE id=1; END;
CREATE TABLE IF NOT EXISTS job_attempts(job TEXT NOT NULL,attempt TEXT NOT NULL,offer BLOB NOT NULL,state TEXT NOT NULL,
revision INTEGER NOT NULL DEFAULT 0,grant_id TEXT NOT NULL DEFAULT '',cancel_requested INTEGER NOT NULL DEFAULT 0,PRIMARY KEY(job,attempt));
CREATE TABLE IF NOT EXISTS job_events(id TEXT PRIMARY KEY,payload BLOB NOT NULL);")?;
reconcile::initialize(&db)?;
let ledger = Connection::open(state.join("consumption.sqlite"))?;
ledger::initialize_ledger(&ledger)?;
let mut w = Worker {
sync: ClientSync::new(binding.clone()),
binding,
db,
ledger,
channel,
clock: NativeClock::default(),
sync_due: Instant::now(),
permit_after: Instant::now(),
};
w.recover()?;
let mut input = io::stdin().lock();
println!("ready");
io::stdout().flush()?;
loop {
let mut line = String::new();
let n = (&mut input).take(2_100_000).read_line(&mut line)?;
if n == 0 {
return Ok(());
}
ensure!(line.ends_with('\n'), "FRAME_LIMIT");
let line = line.trim_end();
if line == "next" {
println!(
"{}",
w.next()?
.map(|e| hex::encode(e.encode_to_vec()))
.unwrap_or_default()
);
} else {
let data = hex::decode(line)?;
w.receive(Envelope::decode(data.as_slice())?)?;
println!("ok");
}
io::stdout().flush()?;
}
}
@@ -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(())
}
+4
View File
@@ -1,4 +1,5 @@
//! Additive bounded passive metadata; the strict original Inventory stays intact. //! Additive bounded passive metadata; the strict original Inventory stays intact.
mod opnsense;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
@@ -64,6 +65,9 @@ pub fn validate_for_scope(
now: DateTime<Utc>, now: DateTime<Utc>,
scope: &str, scope: &str,
) -> Result<Value, &'static str> { ) -> Result<Value, &'static str> {
if scope == "opnsense.base" {
return opnsense::validate(bytes);
}
if scope == "agent.update" { if scope == "agent.update" {
if bytes.len() > 65536 { if bytes.len() > 65536 {
return Err("AGENT_UPDATE_INVENTORY_SIZE"); return Err("AGENT_UPDATE_INVENTORY_SIZE");
@@ -0,0 +1,107 @@
//! Allowlisted wire document. No appliance configuration or VPN secrets on the wire.
use serde::Deserialize;
use serde_json::Value;
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Snapshot {
os: String,
hostname: String,
architecture: String,
root_total_bytes: u64,
root_available_bytes: u64,
cpu_model: String,
logical_processors: u32,
memory_total_bytes: u64,
firewall: Rows,
vpn_configured: Rows,
wireguard: Rows,
openvpn: Rows,
ipsec: Rows,
hardware: Option<Hardware>,
software: Option<Records<Software>>,
interfaces: Option<Records<Interface>>,
users: Option<Rows>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Hardware {
device_type: String,
booted_at: chrono::DateTime<chrono::Utc>,
manufacturer: Option<String>,
bios_serial_number: Option<String>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Records<T> { status: String, entries: Vec<T> }
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Software { name: String, version: String }
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Interface { name: String, mac: Option<String>, addresses: Vec<String> }
impl<T> Records<T> {
fn valid(&self, limit: usize, check: impl Fn(&T) -> bool) -> bool {
matches!(self.status.as_str(),"ok"|"partial"|"unavailable")
&& (self.status != "unavailable" || self.entries.is_empty())
&& self.entries.len() <= limit && self.entries.iter().all(check)
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Rows {
status: String,
entries: Vec<String>,
}
impl Rows {
fn valid(&self) -> bool {
matches!(self.status.as_str(), "ok" | "partial" | "unavailable")
&& (self.status != "unavailable" || self.entries.is_empty())
&& self.entries.len() <= 128
&& self.entries.iter().all(|s| clean(s, 512))
}
}
fn clean(s: &str, limit: usize) -> bool {
!s.trim().is_empty() && s.len() <= limit && !s.chars().any(char::is_control)
}
pub(super) fn validate(bytes: &[u8]) -> Result<Value, &'static str> {
let err = "INVALID_OPNSENSE_INVENTORY";
if bytes.len() > 384 * 1024 { return Err(err); }
let s: Snapshot = serde_json::from_slice(bytes).map_err(|_| err)?;
if !s.os.starts_with("OPNsense ") || !clean(&s.os, 128)
|| !clean(&s.hostname, 255) || s.architecture != "amd64"
|| s.root_total_bytes == 0 || s.root_available_bytes > s.root_total_bytes
|| !clean(&s.cpu_model, 256) || s.logical_processors == 0 || s.memory_total_bytes == 0
|| ![&s.firewall,&s.vpn_configured,&s.wireguard,&s.openvpn,&s.ipsec].iter().all(|r| r.valid())
|| s.users.is_some_and(|r| !r.valid())
|| s.hardware.is_some_and(|h| !matches!(h.device_type.as_str(),"physical"|"virtual")
|| h.booted_at.timestamp() < 0
|| [h.manufacturer,h.bios_serial_number].into_iter().flatten().any(|v| !clean(&v,256)))
|| s.software.is_some_and(|r| !r.valid(1024, |p| clean(&p.name,256) && clean(&p.version,256)))
|| s.interfaces.is_some_and(|r| !r.valid(128, |i| clean(&i.name,128)
&& i.mac.as_ref().is_none_or(|m| clean(m,64)) && i.addresses.len()<=256
&& i.addresses.iter().all(|s| clean(s,128))))
{ return Err(err); }
serde_json::from_slice(bytes).map_err(|_| err)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn no_config_blob_and_no_false_empty_on_error() {
let rows = json!({"status":"ok","entries":[]});
let mut doc = json!({"os":"OPNsense 26.7","hostname":"test","architecture":"amd64",
"root_total_bytes":20,"root_available_bytes":10,"cpu_model":"Test","logical_processors":2,
"memory_total_bytes":10,"firewall":rows,"vpn_configured":rows,"wireguard":rows,"ipsec":rows,"openvpn":rows});
assert!(validate(doc.to_string().as_bytes()).is_ok());
doc["wireguard"]["private_key"] = json!("not-permitted");
assert!(validate(doc.to_string().as_bytes()).is_err());
doc["wireguard"] = json!({"status":"unavailable","entries":["peer"]});
assert!(validate(doc.to_string().as_bytes()).is_err());
}
}
+31 -1
View File
@@ -480,7 +480,16 @@ impl Action {
_ => None, _ => None,
} }
} }
/// Shared plan/revalidation/start gate. Passive Windows never implies job support. /// The first FreeBSD profile is one exact POSIX script. It does not make
/// an appliance eligible for the Linux library, arbitrary scripts or root.
pub fn opnsense_kernel_probe(&self) -> bool {
matches!(self, Self::RunScript { interpreter, script_utf8, arguments,
expected_stdout: Some(expected), windows_execution_profile: None, privileged_script_profile: None }
if interpreter == "posix_sh" && script_utf8 == "uname -s\n"
&& arguments.is_empty() && expected == "FreeBSD\n")
}
/// Shared plan/revalidation/start gate. Passive inventory never implies job support.
pub fn available_for(&self, identity: &str, capabilities: &[String]) -> bool { pub fn available_for(&self, identity: &str, capabilities: &[String]) -> bool {
if !self.valid_execution_identity(identity) if !self.valid_execution_identity(identity)
|| (identity == "system" || (identity == "system"
@@ -490,6 +499,11 @@ impl Action {
return false; return false;
} }
let has = |cap: &str| capabilities.iter().any(|c| c == cap); let has = |cap: &str| capabilities.iter().any(|c| c == cap);
if has("opnsense.base/1") {
return identity == "agent_service" && has("run_script") && has("rmm.sync.v1")
&& has("opnsense.uname/1") && !has("windows.base/1") && !has("linux.base/1")
&& self.opnsense_kernel_probe();
}
if !has(self.name()) if !has(self.name())
|| (self.full_maintenance() || (self.full_maintenance()
&& !((has(FULL_MAINTENANCE_CAPABILITY) && !((has(FULL_MAINTENANCE_CAPABILITY)
@@ -828,6 +842,22 @@ impl JobState {
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn appliance_kernel_probe_rejects_unbound_or_changed_profiles() {
let caps: Vec<String> = ["run_script","opnsense.base/1","rmm.sync.v1","opnsense.uname/1"].map(String::from).into();
let mut a = Action::RunScript { interpreter:"posix_sh".into(),script_utf8:"uname -s\n".into(),
arguments:vec![],expected_stdout:Some("FreeBSD\n".into()),windows_execution_profile:None,privileged_script_profile:None };
assert!(a.available_for("agent_service",&caps));
for excluded in &caps {
let fewer = caps.iter().filter(|v| *v!=excluded).cloned().collect::<Vec<_>>();
assert!(!a.available_for("agent_service",&fewer));
}
assert!(!a.available_for("root",&caps));
let mut mixed = caps.clone(); mixed.push("linux.base/1".into());
assert!(!a.available_for("agent_service",&mixed));
if let Action::RunScript { script_utf8,.. } = &mut a { script_utf8.push(' '); }
assert!(!a.available_for("agent_service",&caps));
}
#[test]
fn windows_update_final_report_and_policy_are_exact() { fn windows_update_final_report_and_policy_are_exact() {
assert_eq!( assert_eq!(
windows_update_reboot_report( windows_update_reboot_report(
+1 -1
View File
@@ -160,7 +160,7 @@ pub async fn ingest(
ensure!( ensure!(
matches!( matches!(
s.scope.as_str(), s.scope.as_str(),
"linux.base" | "windows.base" | "agent.update" "linux.base" | "windows.base" | "opnsense.base" | "agent.update"
) && s.schema_version == 1 ) && s.schema_version == 1
&& s.chunk_count == 1 && s.chunk_count == 1
&& s.chunk_index == 0 && s.chunk_index == 0
+1 -1
View File
@@ -34,7 +34,7 @@ SELECT c.*,o.observed_at,o.received_at,o.data,o.source_id FROM classified c
LEFT JOIN LATERAL ( LEFT JOIN LATERAL (
SELECT observed_at,received_at,data,source_id FROM observations SELECT observed_at,received_at,data,source_id FROM observations
WHERE tenant_id=c.tenant_id AND agent_id=c.agent_id AND received_at<=$2 WHERE tenant_id=c.tenant_id AND agent_id=c.agent_id AND received_at<=$2
AND source_id IN ('linux.base','windows.base') AND source_id IN ('linux.base','windows.base','opnsense.base')
ORDER BY observed_at DESC,received_at DESC,sequence DESC LIMIT 1 ORDER BY observed_at DESC,received_at DESC,sequence DESC LIMIT 1
) o ON true ) o ON true
WHERE ($6::text IS NULL OR (c.lifecycle='current')=($6='current')) WHERE ($6::text IS NULL OR (c.lifecycle='current')=($6='current'))
+5 -3
View File
@@ -321,6 +321,7 @@ fn device_type(hardware: &Value) -> Option<DeviceType> {
fn inventory_device_type(data: &Value) -> Option<DeviceType> { fn inventory_device_type(data: &Value) -> Option<DeviceType> {
device_type(observed(data, "linux.hardware")) device_type(observed(data, "linux.hardware"))
.or_else(|| device_type(observed(data, "windows.hardware"))) .or_else(|| device_type(observed(data, "windows.hardware")))
.or_else(|| device_type(&data["hardware"]))
} }
fn hostname_filter(query: Option<&str>) -> Result<&str, Error> { fn hostname_filter(query: Option<&str>) -> Result<&str, Error> {
@@ -517,7 +518,8 @@ fn project(row: &PgRow, now: DateTime<Utc>, full: bool, hosts: &HashMap<Uuid, Ho
let ram_total = usage["ram_total_bytes"] let ram_total = usage["ram_total_bytes"]
.as_u64() .as_u64()
.or_else(|| linux_memory["total_bytes"].as_u64()) .or_else(|| linux_memory["total_bytes"].as_u64())
.or_else(|| windows["memory_total_bytes"].as_u64()); .or_else(|| windows["memory_total_bytes"].as_u64())
.or_else(|| data["memory_total_bytes"].as_u64());
let device_type = inventory_device_type(&data); let device_type = inventory_device_type(&data);
let last = row.get::<Option<DateTime<Utc>>, _>("last_contact_at"); let last = row.get::<Option<DateTime<Utc>>, _>("last_contact_at");
let revoked = row.get::<Option<DateTime<Utc>>, _>("revoked_at"); let revoked = row.get::<Option<DateTime<Utc>>, _>("revoked_at");
@@ -536,8 +538,8 @@ fn project(row: &PgRow, now: DateTime<Utc>, full: bool, hosts: &HashMap<Uuid, Ho
"hostname":data["hostname"].as_str(),"purpose":row.get::<Option<String>,_>("purpose"), "hostname":data["hostname"].as_str(),"purpose":row.get::<Option<String>,_>("purpose"),
"device_type":device_type,"os":os_label(&data), "device_type":device_type,"os":os_label(&data),
"disk_free_bytes":data["root_available_bytes"].as_u64(),"disk_total_bytes":data["root_total_bytes"].as_u64(), "disk_free_bytes":data["root_available_bytes"].as_u64(),"disk_total_bytes":data["root_total_bytes"].as_u64(),
"cpu_model":cpu["model"].as_str().or_else(||windows["cpu_model"].as_str()), "cpu_model":cpu["model"].as_str().or_else(||windows["cpu_model"].as_str()).or_else(||data["cpu_model"].as_str()),
"cpu_logical_count":cpu["logical_processors"].as_u64().or_else(||windows["logical_processors_visible"].as_u64()), "cpu_logical_count":cpu["logical_processors"].as_u64().or_else(||windows["logical_processors_visible"].as_u64()).or_else(||data["logical_processors"].as_u64()),
"cpu_usage_percent":cpu_usage, "cpu_usage_percent":cpu_usage,
"cpu_usage_observed_at":cpu_observed_at, "cpu_usage_observed_at":cpu_observed_at,
"ram_total_bytes":ram_total, "ram_total_bytes":ram_total,
+10 -3
View File
@@ -292,7 +292,8 @@ fn validate_variants(variants: &[Variant]) -> Result<()> {
privileged_script_profile: None, privileged_script_profile: None,
}; };
if v.encoding != "utf-8" if v.encoding != "utf-8"
|| action.script_platform() != Some(v.os_family.as_str()) || !(action.script_platform() == Some(v.os_family.as_str())
|| (v.os_family == "opnsense" && v.interpreter == "posix_sh"))
|| !action.validate() || !action.validate()
{ {
return Err(fail("UNSUPPORTED_CAPABILITY")); return Err(fail("UNSUPPORTED_CAPABILITY"));
@@ -346,9 +347,11 @@ fn target_action(input: &PlanInput, caps: &[String]) -> Result<Action> {
let platform = match ( let platform = match (
caps.iter().any(|c| c == "linux.base/1"), caps.iter().any(|c| c == "linux.base/1"),
caps.iter().any(|c| c == "windows.base/1"), caps.iter().any(|c| c == "windows.base/1"),
caps.iter().any(|c| c == "opnsense.base/1"),
) { ) {
(true, false) => "linux", (true, false, false) => "linux",
(false, true) => "windows", (false, true, false) => "windows",
(false, false, true) => "opnsense",
_ => return Err(fail("UNSUPPORTED_CAPABILITY")), _ => return Err(fail("UNSUPPORTED_CAPABILITY")),
}; };
let v = input let v = input
@@ -438,6 +441,10 @@ pub(crate) async fn plan_resolved(
let row = &rows[0]; let row = &rows[0];
let caps = capabilities(row.get("capabilities"))?; let caps = capabilities(row.get("capabilities"))?;
let action = target_action(&input, &caps)?; let action = target_action(&input, &caps)?;
if caps.iter().any(|s| s=="opnsense.base/1") &&
(input.max_runtime_seconds != 10 || input.max_output_bytes != 1024) {
return Err(fail("UNSUPPORTED_CAPABILITY"));
}
if !action.valid_job_limits(input.max_runtime_seconds, input.max_output_bytes) { if !action.valid_job_limits(input.max_runtime_seconds, input.max_output_bytes) {
return Err(fail("VALIDATION_ERROR")); return Err(fail("VALIDATION_ERROR"));
} }
+22
View File
@@ -0,0 +1,22 @@
# OPNsense-Testclient
Test-VM `discovery-opnsense` auf `qemu:///system`, 2 vCPU, 1536 MiB RAM, 12 GiB virtuelle Platte. Basis: offizielles Nano-Image OPNsense 26.7 amd64; SHA-256 des komprimierten Images `28d5e2f37e40d87468a924e3006ef10e2ddc6de485b85333d9e3958c84d0cb9d`, gegen die Herstellerankündigung verglichen und die Signatur des entpackten Images geprüft. Start bei Bedarf: `virsh -c qemu:///system start discovery-opnsense`. Konsole: `virsh -c qemu:///system console discovery-opnsense`. Das LAN `192.168.247.0/24` ist eine isolierte libvirt-Bridge, das WAN `192.168.246.0/24` verwendet NAT. Management vom Notebook: `https://192.168.247.1`. Der Hostpin stammt aus der seriellen Konsole; Zugangswerte liegen ausschließlich im privaten Zugangsspeicher des ursprünglichen Checkouts.
## Quelle und Entwicklungsinstallation
Der OPNsense-eigene Python-Interpreter führt zwei Zipapps aus. Der lokale Collector läuft mit den für PF, VPN-Sockets und Konfiguration nötigen Rechten. Er veröffentlicht ausschließlich den ausgewählten Bestand atomar in `/var/db/discovery-observations/inventory.json`. Das Dienstkonto `discovery` kann die Datei lesen, aber weder Collector noch Cache verändern. Nur dieses Konto hält die Geräteidentität und nutzt den ausgehenden HTTPS-Kanal. Für die Erfassung wird keine eingehende Netzwerkverbindung benötigt.
1. In einer Build-venv `grpcio-tools==1.76.0` und `protobuf==6.33.5` installieren. `python deploy/opnsense/build.py ARTIFAKTORDNER` generiert die Protobuf-Bindung aus `proto/agent.proto` und packt die portable Python-Laufzeit mit ein. `python deploy/opnsense/test_agent.py ARTIFAKTORDNER/agent.pyz` prüft die Wiederholung einer nicht bestätigten Inventarmeldung mit unveränderter Nachrichtenidentität.
2. Über den gepinnten Entwicklungszugang `agent.pyz`, `collect.pyz`, `discovery_agent` und den **bestätigten öffentlichen CA-Anker des Gerätegateways** als `ca.pem` in einen privaten Stagingordner im Gast übertragen. `sh install-dev.sh STAGINGORDNER` auf dem Gast ausführen. Diese Strecke ist eine direkte Entwicklungsbereitstellung, kein signierter Releasekanal.
3. Bei einer frischen Installation in der Discovery-WebUI den Testkunden wählen und einen Einmalcode erzeugen. Den Code über geschützte Standardeingabe an `su -m discovery -c '/usr/local/bin/python3 -I /usr/local/libexec/discovery-agent/agent.pyz enroll'` übergeben. Danach `service discovery_agent start`. Bei vorhandener Identität nicht neu enrollen. Der Dienst verwaltet beide Prozesse; `rc.conf.local` aktiviert sie beim Start.
4. In **Geräte** den Hostnamen öffnen. Das Inventar wird ungefähr einmal pro Minute erhoben, der Heartbeat etwa alle 20 Sekunden gesendet. Das Betriebssystem liefert CPU-Zeitdifferenzen und eine RAM-Schätzung aus freien und inaktiven Seiten. Fehlgeschlagene Quellen erscheinen als „Nicht ermittelbar“, ein erfolgreicher leerer Befund gesondert. Pro Liste werden höchstens 128 Zeilen gemeldet; Auszüge sind gekennzeichnet.
**Systeminventar:** Kern-Bootzeit, VM-Erkennung, verfügbare SMBIOS-Daten, Netzwerkkarten mit MAC und IPs, angemeldete Benutzer und lokale FreeBSD-Pakete mit Version. Bei Software liegt die Obergrenze bei 1024 Paketen.
**VPN:** Beschreibungen und Aktivierung der OPNsense-Modelle für WireGuard-Instanzen/-Peers, OpenVPN-Instanzen und IPsec-Verbindungen. Separat: WireGuard-Handshakes (kein dauerhafter Online-Status), OpenVPN-Managementstatus und IKE-Sicherheitsassoziationen. Ohne erreichbaren IPsec-Dienst ist der Laufzeitstatus unbekannt. Keine privaten Schlüssel, PSKs, Zertifikate oder vollständige Gerätekonfiguration im Inventar.
**Firewall:** aktiver PF-Hauptregelsatz in Kernelreihenfolge, einschließlich automatisch erzeugter Regeln. Kein Editor und kein rekursives Auslesen der Anchor-Regelsätze.
**Profile:** Ohne Worker und Signaturbindung bleibt das Gerät passiv. Mit dem FreeBSD-Worker unterstützt `0.1.0-opnsense.5` genau die feste Kernelabfrage (`uname -s\n`, `posix_sh`, erwartete Ausgabe `FreeBSD\n`, keine Argumente, Dienstkonto, 10 s, 1024 Byte). Unter **Geräte → OPNsense-Kernelabfrage** lässt sie sich für diese Installation einreichen. Die persönliche WebUI-Genehmigung und der signierte Startgrant sind erforderlich. Root, beliebige Shell-/Bibliotheksskripte und signierte OPNsense-Releases bleiben offen. Das Gerät ist kein Linuxziel.
**Probe bereitstellen:** Im Gast steht die gepinnte Toolchain unter `/opt/discovery-rust`. `cargo build --locked -p rmm-opnsense-jobs` nativ bauen und `rmm-opnsense-jobs` root-eigen (0555) neben den Zipapps installieren. Auf der bereits aufgenommenen Testinstallation `python3 -I pin-probe.py TENANT ASSET AGENT ENROLLMENT_GENERATION ASSIGNMENT_VERSION` mit den bestätigten Kennungen aufrufen. Das Skript bezieht den öffentlichen Jobschlüssel über das bestehende Gerätezertifikat und überschreibt keine andere Bindung. Den Agentendienst anschließend neu starten. Der Worker nutzt bestehende Nachrichtenfolgen, den signierten Abgleich und getrennte dauerhafte Start-/Ergebnisjournale. Er läuft ohne Netzwerklistener als `discovery`. Der [native Integrationstest](../../crates/agent-opnsense-jobs/src/native/tests.rs) prüft mit eigener Testidentität und eigenem Schlüssel die Ablehnung einer defekten Signatur, erwartete Ausgabe, einen Verbrauch und keine zweite Ausführung bei Wiederzustellung. Das ist ein separater Beleg vom persönlich genehmigten Geräteauftrag.
+304
View File
@@ -0,0 +1,304 @@
"""Unprivileged OPNsense telemetry channel. Input never selects a local command."""
import hashlib
import contextlib
import fcntl
import http.client
import json
import os
from pathlib import Path
import re
import sqlite3
import ssl
import subprocess
import sys
import time
import uuid
import agent_pb2 as pb
STATE = Path('/var/db/discovery-agent')
CA = '/usr/local/etc/discovery-agent/ca.pem'
HOST = 'discovery.ease-technologies.de'
MEDIA = 'application/x-protobuf'
VERSION = '0.1.0-opnsense.5'
WORKER = Path('/usr/local/libexec/discovery-agent/rmm-opnsense-jobs')
BINDING = Path('/usr/local/etc/discovery-agent/job-binding.json')
def post(ctx, path, data, media):
conn = http.client.HTTPSConnection(HOST, context=ctx, timeout=15)
try:
conn.request('POST', '/agent/v1/'+path, data, {'Content-Type':media, 'Accept':media})
res = conn.getresponse()
if res.status != 200 or res.getheader('Content-Type', '').split(';')[0] != media:
raise ValueError(f'{path}: HTTP {res.status}')
body = res.read(1024*1024 + 1)
if len(body) > 1024*1024:
raise ValueError('response size')
return body
finally:
conn.close()
def context():
return ssl.create_default_context(cafile=CA)
def enroll():
# Existing keys are never overwritten, including after an ambiguous POST.
key = STATE/'device.key'
if key.exists() or (STATE/'identity.json').exists():
raise RuntimeError('installation already initialized')
token = sys.stdin.readline(128).strip()
if not re.fullmatch('[0-9a-f]{64}', token):
raise ValueError('invalid enrollment code')
csr = subprocess.check_output(['/usr/bin/openssl','req','-new','-newkey','ec','-pkeyopt',
'ec_paramgen_curve:P-256','-nodes','-subj','/CN=discovery-agent','-keyout',str(key)],
stderr=subprocess.DEVNULL)
result = json.loads(post(context(), 'enroll', json.dumps(dict(token=token, csr_pem=csr.decode())),
'application/json'))
for name in ('agent_id','asset_id'):
uuid.UUID(result[name])
(STATE/'device.pem').write_text(result.pop('certificate_pem'))
context().load_cert_chain(STATE/'device.pem', key)
(STATE/'identity.json').write_text(json.dumps(result))
def durable_write(path, data):
tmp = path.with_suffix('.new')
with tmp.open('wb') as f:
f.write(data)
f.flush()
os.fsync(f.fileno())
os.replace(tmp, path)
fd = os.open(STATE, os.O_DIRECTORY)
try:
os.fsync(fd)
finally:
os.close(fd)
def renew(ctx, identity):
# Reuse a durable request after a lost reply; gateway renewal is idempotent
# for the same CSR. Keep the currently working certificate until key match.
cert, key, csr = (STATE/n for n in ('device.pem','device.key','renewal.csr'))
if not csr.exists():
check = subprocess.run(['/usr/bin/openssl','x509','-in',str(cert),'-noout','-checkend','604800'],
stdout=subprocess.DEVNULL, check=False)
if check.returncode == 0:
return False
durable_write(csr,subprocess.check_output(['/usr/bin/openssl','req','-new','-key',str(key),
'-subj','/CN=discovery-agent']))
result = json.loads(post(ctx,'certificates/renew',json.dumps({'csr_pem':csr.read_text()}),'application/json'))
if result['agent_id'] != identity['agent_id']:
raise ValueError('renewal identity mismatch')
candidate = STATE/'candidate.pem'
durable_write(candidate,result['certificate_pem'].encode())
context().load_cert_chain(candidate,key)
durable_write(cert,candidate.read_bytes())
candidate.unlink()
csr.unlink()
return True
def usage(previous):
def counters(*keys):
return [int(x) for x in subprocess.check_output(['/sbin/sysctl', '-n', *keys],timeout=3).split()]
ticks = counters('kern.cp_time')
free, inactive, page, total = counters('vm.stats.vm.v_free_count','vm.stats.vm.v_inactive_count',
'hw.pagesize','hw.physmem')
sample = pb.SystemUsage(ram_available_bytes=min((free+inactive)*page,total),ram_total_bytes=total)
if previous and len(previous) == len(ticks) and all(x >= y for x,y in zip(ticks,previous)):
delta = sum(ticks)-sum(previous)
if delta > 0:
sample.cpu_usage_basis_points = 10000*(delta-ticks[-1]+previous[-1])//delta
return sample,ticks
class Jobs:
"""Private pipe to the non-root worker. The mTLS agent owns the transport."""
def __init__(self, process):
self.process = process
def read(self):
line = self.process.stdout.readline(2_100_000)
if not line.endswith(b'\n'):
raise ValueError('job worker disconnected')
return line[:-1]
def call(self, line):
self.process.stdin.write(line+b'\n')
self.process.stdin.flush()
return self.read()
def received(self, envelope):
if self.call(envelope.SerializeToString().hex().encode()) != b'ok':
raise ValueError('job response not committed')
def pump(self, ctx, agent, boot, generation):
for _ in range(12):
frame = self.call(b'next')
if not frame:
return
e = pb.Envelope.FromString(bytes.fromhex(frame.decode('ascii')))
kind = e.WhichOneof('payload')
if (e.protocol_major!=1 or e.agent_id!=agent or e.enrollment_generation!=1 or
e.connection_generation!=generation or kind not in (
'job_accepted','job_state_changed','job_result','sync_request','execution_permit_request')):
raise ValueError('invalid outbound job event')
batch = pb.MessageBatch(protocol_major=1,messages=[e])
reply = pb.MessageBatch.FromString(post(ctx,'messages',batch.SerializeToString(),MEDIA))
expected = {'sync_request':'sync_response','execution_permit_request':'execution_permit'}.get(kind,'ack')
check_reply(reply,e,expected)
for r in reply.messages:
self.received(r)
def check_reply(batch,e,expected):
if batch.protocol_major!=1 or not 1<=len(batch.messages)<=16:
raise ValueError('unexpected reply')
first = batch.messages[0]
generation = first.server_hello.connection_generation if expected=='server_hello' else e.connection_generation
for index,r in enumerate(batch.messages):
if (r.protocol_major,r.protocol_minor,r.agent_id,r.enrollment_generation,r.connection_generation,
r.boot_id,r.sequence)!=(1,0,e.agent_id,e.enrollment_generation,generation,e.boot_id,e.sequence):
raise ValueError('reply identity')
kind = r.WhichOneof('payload')
if (not index and kind!=expected) or (index and kind not in ('job_offer','cancel_request')):
raise ValueError('unexpected job reply')
if expected=='ack' and first.ack.message_id!=e.message_id:
raise ValueError('unconfirmed message')
return first
def session(db, identity, boot, instance):
with contextlib.ExitStack() as stack:
return run_channel(db, identity, boot, instance, stack)
def run_channel(db, identity, boot, instance, stack):
ctx = context()
ctx.load_cert_chain(STATE/'device.pem', STATE/'device.key')
generation = 0
jobs = None
enabled = BINDING.is_file() and WORKER.is_file()
def send(kind, payload, captured=None):
# A lost inventory ACK replays the exact durable envelope, including its
# original time, boot and sequence; only the connection fence changes.
with db:
pending = db.execute('SELECT envelope FROM pending WHERE id=1').fetchone() if kind=='inventory_snapshot' else None
if pending:
e = pb.Envelope.FromString(pending[0])
if e.agent_id != identity['agent_id']:
raise ValueError('pending snapshot identity')
e.connection_generation = generation
else:
seq = db.execute('UPDATE sequence SET n=n+1 RETURNING n').fetchone()[0]
e = pb.Envelope(protocol_major=1, message_id=str(uuid.uuid4()), agent_id=identity['agent_id'],
enrollment_generation=1, connection_generation=generation, boot_id=boot,
sequence=seq, observed_at=captured or time.time_ns()//1_000_000)
getattr(e, kind).CopyFrom(payload)
if kind == 'inventory_snapshot':
db.execute('INSERT INTO pending VALUES(1,?,?)',(e.SerializeToString(),e.observed_at))
batch = pb.MessageBatch(protocol_major=1, messages=[e])
reply = pb.MessageBatch.FromString(post(ctx, 'messages', batch.SerializeToString(), MEDIA))
r = check_reply(reply,e,'server_hello' if kind=='client_hello' else 'ack')
if len(reply.messages)>1 and jobs is None:
raise ValueError('work outside established job session')
for extra in reply.messages[1:]:
jobs.received(extra)
if kind == 'client_hello':
if (r.WhichOneof('payload') != 'server_hello' or r.connection_generation == 0 or
r.connection_generation != r.server_hello.connection_generation or
r.server_hello.max_message_bytes != 1024*1024):
raise ValueError('invalid server hello')
return r.connection_generation
if (r.connection_generation != generation or r.WhichOneof('payload') != 'ack'
or r.ack.message_id != e.message_id):
raise ValueError('unconfirmed message')
if kind == 'inventory_snapshot':
with db:
db.execute('DELETE FROM pending WHERE id=1')
if renew(ctx, identity):
ctx = context()
ctx.load_cert_chain(STATE/'device.pem', STATE/'device.key')
caps = ['heartbeat/1','opnsense.base/1']
if enabled:
caps.extend(['rmm.sync.v1','run_script','opnsense.uname/1'])
generation = send('client_hello',pb.ClientHello(agent_version=VERSION, capabilities=caps))
if enabled:
p = subprocess.Popen([WORKER,identity['agent_id'],identity['asset_id'],boot,str(generation)],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,close_fds=True)
def close():
p.stdin.close()
try:
p.wait(timeout=15)
except subprocess.TimeoutExpired:
p.kill(); p.wait()
finally:
p.stdout.close()
stack.callback(close)
jobs = Jobs(p)
if jobs.read()!=b'ready':
raise ValueError('job worker unavailable')
last = None
previous = None
check_cert = time.monotonic()+12*3600
while True:
if time.monotonic() >= check_cert:
if renew(ctx, identity):
return
check_cert = time.monotonic()+12*3600
sample,previous = usage(previous)
send('heartbeat',pb.Heartbeat(instance_id=instance,uptime_seconds=int(time.clock_gettime(time.CLOCK_UPTIME)),
system_usage=sample))
with db:
# The ingestion contract accepts observations for 72 hours.
db.execute('DELETE FROM pending WHERE observed < ?', (time.time_ns()//1_000_000 - 72*3600*1000,))
if db.execute('SELECT 1 FROM pending').fetchone():
send('inventory_snapshot',None)
file = Path('/var/db/discovery-observations/inventory.json')
if file.exists():
with file.open('rb') as f:
stamp = os.fstat(f.fileno()).st_mtime_ns
if stamp != last and 0 <= time.time()-stamp/1e9 <= 120:
raw = f.read(384*1024+1)
if len(raw) > 384*1024:
raise ValueError('snapshot size')
send('inventory_snapshot',pb.InventorySnapshot(snapshot_id=str(uuid.uuid4()),
scope='opnsense.base',schema_version=1,chunk_count=1,data=raw,
sha256=hashlib.sha256(raw).digest(),completeness='partial'),stamp//1_000_000)
last = stamp
if jobs:
jobs.pump(ctx,identity['agent_id'],boot,generation)
time.sleep(20)
def main():
os.umask(0o077)
if sys.argv[1:] == ['enroll']:
return enroll()
# Never allow a second network actor to fence or race an in-flight worker.
lock = (STATE/'channel.lock').open('ab')
fcntl.flock(lock,fcntl.LOCK_EX | fcntl.LOCK_NB)
identity = json.loads((STATE/'identity.json').read_bytes())
db = sqlite3.connect(STATE/'channel.sqlite')
with db:
db.execute('CREATE TABLE IF NOT EXISTS sequence(id INTEGER PRIMARY KEY CHECK(id=1),n INTEGER NOT NULL)')
db.execute('CREATE TABLE IF NOT EXISTS pending(id INTEGER PRIMARY KEY CHECK(id=1),envelope BLOB NOT NULL,observed INTEGER NOT NULL)')
db.execute('INSERT OR IGNORE INTO sequence VALUES(1,0)')
started = subprocess.check_output(['/sbin/sysctl','-n','kern.boottime']).decode().split('}')[0]
boot = str(uuid.uuid5(uuid.UUID(identity['agent_id']),started))
instance = str(uuid.uuid4())
while True:
try:
session(db, identity, boot, instance)
except Exception as exc:
print('OPNsense channel:', type(exc).__name__, str(exc)[:160], flush=True)
time.sleep(30)
if __name__ == '__main__':
main()
+33
View File
@@ -0,0 +1,33 @@
"""Build two FreeBSD Python zipapps; use grpcio-tools 1.76.0 + protobuf 6.33.5."""
import importlib.metadata as metadata
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import zipapp
here = Path(__file__).resolve().parent
out = Path(sys.argv[1])
out.mkdir(exist_ok=True, parents=True)
if metadata.version('protobuf') != '6.33.5' or metadata.version('grpcio-tools') != '1.76.0':
sys.exit('Unexpected protocol builder dependencies')
with tempfile.TemporaryDirectory() as tmp:
work = Path(tmp)
proto = here.parents[1]/'proto'
subprocess.run([sys.executable,'-m','grpc_tools.protoc','-I'+str(proto),'--python_out='+tmp,
str(proto/'agent.proto')],check=True)
dist = metadata.distribution('protobuf')
for f in dist.files:
if str(f).startswith('google/protobuf/') and f.suffix == '.py':
dst = work/f
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(dist.locate_file(f), dst)
(work/'google/__init__.py').touch()
license_file = next(f for f in dist.files if f.name=='LICENSE' and '.dist-info/' in str(f))
shutil.copyfile(dist.locate_file(license_file),work/'LICENSE-protobuf')
shutil.copyfile(here/'agent.py',work/'__main__.py')
zipapp.create_archive(work,out/'agent.pyz',compressed=True)
with tempfile.TemporaryDirectory() as tmp:
shutil.copyfile(here/'collect.py',Path(tmp)/'__main__.py')
zipapp.create_archive(tmp,out/'collect.pyz',compressed=True)
+161
View File
@@ -0,0 +1,161 @@
"""Bounded local inventory projection. Runs independently of the network agent."""
import datetime
import json
import os
from pathlib import Path
import re
import resource
import socket
import subprocess
import tempfile
import time
import xml.etree.ElementTree as ET
CACHE = Path('/var/db/discovery-observations')
def run(*args):
# Appliance commands may produce much more than one snapshot. Cap the
# producer before reading its output, including on a misconfigured host.
def limits():
resource.setrlimit(resource.RLIMIT_FSIZE, (2**21, 2**21))
with tempfile.TemporaryFile() as out:
subprocess.run(args, stdout=out, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL,
check=True, timeout=10, preexec_fn=limits)
out.seek(0)
return out.read(2**21).decode('utf8', errors='replace').strip()
def sysctl(name):
return run('/sbin/sysctl', '-n', name)
def rows(provider):
entries = []
partial = False
try:
for text in provider():
if len(entries) == 128:
partial = True
break
text = ' '.join(text.split())
if len(text.encode()) > 512:
partial = True
text = text.encode()[:509].decode(errors='ignore') + '…'
if text:
entries.append(text)
except (OSError, ValueError, KeyError, TypeError, ET.ParseError, subprocess.SubprocessError):
return dict(status='partial' if entries else 'unavailable', entries=entries)
return dict(status='partial' if partial else 'ok', entries=entries)
def configured(config_path='/conf/config.xml'):
# Project only these model paths and display fields. Never serialize XML.
with open(config_path, 'rb') as f:
data = f.read(8 * 1024 * 1024 + 1)
if len(data) > 8 * 1024 * 1024:
raise ValueError('config size')
root = ET.fromstring(data)
for title, path, field in [
('WireGuard-Instanz', 'wireguard/server/servers/server', 'name'),
('WireGuard-Peer', 'wireguard/client/clients/client', 'name'),
('IPsec', 'Swanctl/Connections/Connection', 'description'),
('OpenVPN', 'OpenVPN/Instances/Instance', 'description'),
]:
for item in root.findall('OPNsense/' + path):
enabled = {'0': 'deaktiviert', '1': 'aktiviert'}.get(item.findtext('enabled'), 'Status unbekannt')
yield f"{title} · {item.findtext(field) or item.get('uuid') or '–'} · {enabled}"
def wireguard():
for line in run('/usr/bin/wg', 'show', 'all', 'latest-handshakes').splitlines():
interface, public_key, epoch = line.split()
stamp = datetime.datetime.fromtimestamp(int(epoch), datetime.timezone.utc).isoformat() if int(epoch) else 'nie'
yield f'{interface} · {public_key} · {stamp}'
def openvpn():
data = json.loads(run('/usr/local/opnsense/scripts/openvpn/ovpn_status.py', '--options', 'client,server'))
for role in ('server', 'client'):
for instance, status in data.get(role, {}).items():
yield f"{role} {instance} · {status.get('status', 'unbekannt')}"
for peer in status.get('client_list', []):
yield f"{instance} · {peer.get('common_name', '–')} · {peer.get('real_address', '–')}"
def ipsec():
# Only IKE summary lines, not CHILD_SA details or authentication settings.
for line in run('/usr/local/sbin/swanctl', '--list-sas').splitlines():
if re.match(r'^\S.*: #\d+, ', line):
yield line
def interfaces():
item = None
for line in run('/sbin/ifconfig','-f','inet:cidr,inet6:cidr','-a').splitlines():
if line and not line[0].isspace():
if item:
yield item
item = dict(name=line.split(':',1)[0],mac=None,addresses=[])
elif item:
fields = line.split()
if len(fields)>1 and fields[0]=='ether':
item['mac'] = fields[1]
elif len(fields)>1 and fields[0] in ('inet','inet6'):
item['addresses'].append(fields[1])
if item:
yield item
def packages():
for line in run('/usr/sbin/pkg','query','%n\t%v').splitlines():
name,version = line.split('\t',1)
yield dict(name=name,version=version)
def inventory_rows(fn, limit):
entries = []
try:
for item in fn():
if len(entries) >= limit:
return dict(status='partial',entries=entries)
entries.append(item)
return dict(status='ok',entries=entries)
except (OSError,ValueError,subprocess.SubprocessError):
return dict(status='partial' if entries else 'unavailable',entries=entries)
def snapshot():
disk = os.statvfs('/')
boot = int(re.search(r'sec = (\d+)',sysctl('kern.boottime'))[1])
hardware = {'device_type':'physical' if sysctl('kern.vm_guest')=='none' else 'virtual',
'booted_at':datetime.datetime.fromtimestamp(boot,datetime.timezone.utc).isoformat()}
for key,name in [('smbios.system.maker','manufacturer'),('smbios.system.serial','bios_serial_number')]:
try:
value = run('/bin/kenv',key)
if value and value.lower() not in ('none','unknown','not specified','default string'):
hardware[name]=value[:128]
except (OSError,subprocess.SubprocessError):
pass
return dict(os=run('/usr/local/sbin/opnsense-version'), hostname=socket.gethostname(),
architecture=os.uname().machine, root_total_bytes=disk.f_blocks*disk.f_frsize,
root_available_bytes=max(0,disk.f_bavail)*disk.f_frsize, cpu_model=sysctl('hw.model'),
logical_processors=int(sysctl('hw.ncpu')), memory_total_bytes=int(sysctl('hw.physmem')),
firewall=rows(lambda: run('/sbin/pfctl', '-sr').splitlines()), vpn_configured=rows(configured),
wireguard=rows(wireguard), openvpn=rows(openvpn), ipsec=rows(ipsec),
hardware=hardware, interfaces=inventory_rows(interfaces,128), software=inventory_rows(packages,1024),
users=rows(lambda:sorted(set(run('/usr/bin/users').split()))))
if __name__ == '__main__':
while True:
try:
with tempfile.NamedTemporaryFile(dir=CACHE, delete=False) as f:
f.write(json.dumps(snapshot(), ensure_ascii=False).encode())
os.fchmod(f.fileno(), 0o640)
os.replace(f.name, CACHE/'inventory.json')
except Exception as exc:
print('OPNsense collection:', type(exc).__name__, flush=True)
if 'f' in locals():
Path(f.name).unlink(missing_ok=True)
time.sleep(60)
+29
View File
@@ -0,0 +1,29 @@
#!/bin/sh
# PROVIDE: discovery_agent
# REQUIRE: NETWORKING
# KEYWORD: shutdown
. /etc/rc.subr
name=discovery_agent
rcvar=discovery_agent_enable
load_rc_config "$name"
: "${discovery_agent_enable:=NO}"
start_cmd=discovery_start
stop_cmd=discovery_stop
discovery_start() {
for part in collect agent; do
pid=/var/run/discovery-$part.pid
[ -z "$(check_pidfile "$pid" /usr/sbin/daemon)" ] || continue
user=root
[ "$part" != agent ] || user=discovery
/usr/sbin/daemon -f -r -R 10 -P "$pid" -u "$user" -S -T "discovery-$part" \
/usr/local/bin/python3 -I /usr/local/libexec/discovery-agent/"$part".pyz || return 1
done
}
discovery_stop() {
for part in agent collect; do
pid=$(check_pidfile "/var/run/discovery-$part.pid" /usr/sbin/daemon)
[ -z "$pid" ] || kill -TERM "$pid"
done
}
run_rc_command "$1"
+42
View File
@@ -0,0 +1,42 @@
#!/bin/sh
# Direct artifact deployment in the test guest. Enrollment is a separate step.
set -eu
[ "$(id -u)" = 0 ] && [ "$(uname -s)" = FreeBSD ] && [ -x /usr/local/sbin/opnsense-version ]
[ "$#" = 1 ] || { echo 'Usage: install-dev.sh /path/to/staged-artifacts' >&2; exit 2; }
src=$1
for f in agent.pyz collect.pyz discovery_agent ca.pem; do [ -f "$src/$f" ]; done
/usr/bin/openssl x509 -in "$src/ca.pem" -noout -checkend 0 >/dev/null
pw groupshow discovery >/dev/null 2>&1 || pw groupadd discovery
pw usershow discovery >/dev/null 2>&1 || pw useradd discovery -g discovery -d /var/db/discovery-agent -s /usr/sbin/nologin
[ "$(id -gn discovery)" = discovery ] && [ "$(id -u discovery)" != 0 ]
app=/usr/local/libexec/discovery-agent
conf=/usr/local/etc/discovery-agent
rc=/usr/local/etc/rc.d/discovery_agent
install -d -o root -g wheel -m 0755 "$app" "$conf"
install -d -o discovery -g discovery -m 0700 /var/db/discovery-agent
install -d -o root -g discovery -m 2750 /var/db/discovery-observations
if [ -e "$conf/ca.pem" ]; then
cmp -s "$conf/ca.pem" "$src/ca.pem" || { echo 'Existing trust anchor differs' >&2; exit 1; }
fi
for part in collect agent; do
install -o root -g wheel -m 0444 "$src/$part.pyz" "$app/$part.next"
done
if [ -x "$rc" ]; then
"$rc" onestop
for _ in 1 2 3 4 5; do
[ -f /var/run/discovery-agent.pid ] || [ -f /var/run/discovery-collect.pid ] || break
sleep 1
done
if [ -f /var/run/discovery-agent.pid ] || [ -f /var/run/discovery-collect.pid ]; then
echo 'Existing service still running' >&2; exit 1
fi
fi
for part in collect agent; do mv -f "$app/$part.next" "$app/$part.pyz"; done
install -o root -g wheel -m 0444 "$src/ca.pem" "$conf/ca.pem"
install -o root -g wheel -m 0555 "$src/discovery_agent" "$rc"
sysrc -f /etc/rc.conf.local discovery_agent_enable=YES >/dev/null
if [ -s /var/db/discovery-agent/identity.json ]; then
"$rc" start
else
echo 'Ready for enrollment under the discovery service account.'
fi
+53
View File
@@ -0,0 +1,53 @@
"""Provision the first service-account probe on an existing development installation.
Run as root in FreeBSD with the operator-confirmed tenant/asset/agent UUIDs and
enrollment/assignment generations. Fetch only the public signing pin as discovery.
"""
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import sys
import uuid
def main():
if os.geteuid() or len(sys.argv)!=6:
raise SystemExit('pin-probe.py TENANT ASSET AGENT ENROLLMENT_GENERATION ASSIGNMENT_VERSION')
tenant,asset,agent = (str(uuid.UUID(s)) for s in sys.argv[1:4])
enroll,assignment = map(int,sys.argv[4:])
if min(enroll,assignment)<1:
raise ValueError('invalid generation')
# Device-owned inputs are opened only after dropping privileges.
source = """
import hashlib,http.client,json,ssl
from pathlib import Path
p=Path('/var/db/discovery-agent')
i=json.loads((p/'identity.json').read_bytes())
ctx=ssl.create_default_context(cafile='/usr/local/etc/discovery-agent/ca.pem')
ctx.load_cert_chain(p/'device.pem',p/'device.key')
c=http.client.HTTPSConnection('discovery.ease-technologies.de',context=ctx,timeout=10)
c.request('GET','/api/v1/job-signing-key')
r=c.getresponse()
if r.status!=200:raise ValueError('device trust endpoint rejected')
print(json.dumps([i['agent_id'],i['asset_id'],json.loads(r.read(4097))]))
"""
current,bound_asset,k=json.loads(subprocess.check_output(
['su','-m','discovery','-c','/usr/local/bin/python3 -I -'],input=source.encode(),timeout=15))
key = k['public_key_hex']
if (current,bound_asset)!=(agent,asset) or not re.fullmatch('[a-f0-9]{64}',key) or (
k['algorithm'],k['schema_version'],k['key_id'])!=('Ed25519',1,hashlib.sha256(bytes.fromhex(key)).hexdigest()):
raise ValueError('conflicting device or signing key')
payload=dict(agent_id=agent,asset_id=asset,tenant_id=tenant,enrollment_generation=enroll,
assignment_version=assignment,public_key_hex=key)
p=Path('/usr/local/etc/discovery-agent/job-binding.json')
try:
with p.open('x') as f:
os.fchmod(f.fileno(),0o444); json.dump(payload,f); f.flush(); os.fsync(f.fileno())
except FileExistsError:
if p.is_symlink() or json.loads(p.read_bytes())!=payload: raise ValueError('existing binding differs')
if __name__=='__main__':
main()
+80
View File
@@ -0,0 +1,80 @@
"""Run from the protocol-builder venv: python test_agent.py <built agent.pyz>."""
import importlib.util
import os
from pathlib import Path
import sqlite3
import sys
import tempfile
import unittest
from unittest.mock import patch
import uuid
sys.path.insert(0,sys.argv.pop(1))
spec = importlib.util.spec_from_file_location('opn_channel', Path(__file__).with_name('agent.py'))
a = importlib.util.module_from_spec(spec)
spec.loader.exec_module(a)
class Reconnect(unittest.TestCase):
def test_piggybacked_work_requires_same_authenticated_channel_and_exact_receipt(self):
e=a.pb.Envelope(protocol_major=1,enrollment_generation=1,connection_generation=3,
agent_id=str(uuid.uuid4()),boot_id=str(uuid.uuid4()),sequence=42,message_id=str(uuid.uuid4()))
ack=a.pb.Envelope();ack.CopyFrom(e);ack.ack.message_id=e.message_id
offer=a.pb.Envelope();offer.CopyFrom(e);offer.job_offer.job_id=str(uuid.uuid4())
batch=a.pb.MessageBatch(protocol_major=1,messages=[ack,offer])
self.assertEqual(a.check_reply(batch,e,'ack'),ack)
batch.messages[1].connection_generation=2
with self.assertRaisesRegex(ValueError,'identity'): a.check_reply(batch,e,'ack')
del batch.messages[1:]
batch.messages[0].ack.message_id=str(uuid.uuid4())
with self.assertRaisesRegex(ValueError,'unconfirmed'): a.check_reply(batch,e,'ack')
def test_lost_ack_keeps_original_identity_after_restart(self):
with tempfile.TemporaryDirectory() as temp:
db = sqlite3.connect(':memory:')
db.executescript('CREATE TABLE sequence(id INTEGER PRIMARY KEY,n INTEGER);INSERT INTO sequence VALUES(1,0);'
'CREATE TABLE pending(id INTEGER PRIMARY KEY,envelope BLOB,observed INTEGER);')
cache = Path(temp)/'inventory.json'
cache.write_text('{}')
identity = {'agent_id':str(uuid.uuid4())}
instance = str(uuid.uuid4())
captures = []
generation = 0
def gateway(_ctx,path,data,media):
nonlocal generation
req = a.pb.MessageBatch.FromString(data).messages[0]
reply = a.pb.Envelope()
reply.CopyFrom(req)
kind = req.WhichOneof('payload')
if kind == 'client_hello':
generation += 1
reply.connection_generation = generation
reply.server_hello.CopyFrom(a.pb.ServerHello(connection_generation=generation,max_message_bytes=1024*1024))
else:
if kind == 'inventory_snapshot':
captures.append(req)
if len(captures)==1: raise OSError('lost ACK')
reply.ack.CopyFrom(a.pb.Ack(message_id=req.message_id))
return a.pb.MessageBatch(protocol_major=1,messages=[reply]).SerializeToString()
with patch.object(a,'context'), patch.object(a,'renew',return_value=False), \
patch.object(a,'post',gateway), patch.object(a,'Path',return_value=cache), \
patch.object(a,'usage',return_value=(a.pb.SystemUsage(),[])), \
patch.object(a.time,'clock_gettime',return_value=100), \
patch.object(a.time,'CLOCK_UPTIME',1,create=True), patch.object(a.time,'sleep',side_effect=StopIteration):
with self.assertRaises(OSError):
a.session(db,identity,str(uuid.uuid4()),instance)
self.assertIsNotNone(db.execute('SELECT 1 FROM pending').fetchone())
os.unlink(cache)
with self.assertRaises(StopIteration):
a.session(db,identity,str(uuid.uuid4()),instance)
self.assertIsNone(db.execute('SELECT 1 FROM pending').fetchone())
self.assertEqual(len(captures),2)
self.assertNotEqual(captures[0].connection_generation,captures[1].connection_generation)
captures[0].connection_generation=captures[1].connection_generation
self.assertEqual(captures[0],captures[1])
if __name__=='__main__':
unittest.main()
+28
View File
@@ -0,0 +1,28 @@
import tempfile
import unittest
from pathlib import Path
import collect
class ApplianceProjection(unittest.TestCase):
def test_configured_disabled_is_not_a_connection_and_secrets_stay_local(self):
with tempfile.TemporaryDirectory() as root:
p = Path(root)/'config.xml'
p.write_text('<opnsense><OPNsense><wireguard><server><servers><server uuid="uuid">'
'<name>Test</name><enabled>0</enabled><privkey>do-not-export</privkey>'
'</server></servers></server></wireguard><OpenVPN><Instances><Instance>'
'<enabled>1</enabled><description>Lab</description><tlskey>do-not-export</tlskey>'
'</Instance></Instances></OpenVPN></OPNsense></opnsense>')
self.assertEqual(collect.rows(lambda:collect.configured(p)), {
'status':'ok','entries':['WireGuard-Instanz · Test · deaktiviert','OpenVPN · Lab · aktiviert']})
p.write_text('<broken')
self.assertEqual(collect.rows(lambda:collect.configured(p)),{'status':'unavailable','entries':[]})
def test_truncation_is_explicit_and_utf8_stays_within_the_wire_limit(self):
result = collect.rows(lambda:iter(['ü'*1000]*200))
self.assertEqual((result['status'],len(result['entries'])),('partial',128))
self.assertTrue(all(len(s.encode())<=512 for s in result['entries']))
if __name__=='__main__':
unittest.main()
@@ -0,0 +1,15 @@
{
"jobs": 0,
"state": "pending_approval",
"outbox": 0,
"script": "uname -s\n",
"target": "b4561fc5-d3b3-4ee4-810f-209d7e668e43",
"attempt": "ea526a57-d3f0-4ae6-8d52-d6ffdf89dfa9",
"plan_id": "c3aa157c-4908-45c1-b7f4-0f588879d1af",
"plan_hash": "f615d46f5ac6e1a4e81eb498f29609da36c77613fe7d7ab55ccf0a395f368143",
"checked_at": "2026-11-19T19:53:19.073901+00:00",
"expires_at": "2026-11-20T19:52:59.134+00:00",
"uid_profile": "agent_service",
"operation_id": "67205987-0fda-4635-ae7a-4b7979c274dc",
"start_grants": 0
}
@@ -0,0 +1,40 @@
{
"rules": 91,
"source": "opnsense.base",
"tenant": "Ease Testserver - For development",
"agent_id": "b4561fc5-d3b3-4ee4-810f-209d7e668e43",
"asset_id": "1d0b9642-9f31-4fba-95f3-8cc15a11aa34",
"tenant_id": "9d730e0a-ba31-402c-8b82-8995c2585f59",
"transport": "https",
"cpu_reported": true,
"inventory_at": "2026-11-19T19:11:42.453+00:00",
"ram_reported": true,
"agent_version": "0.1.0-opnsense.4",
"last_contact_at": "2026-11-19T19:12:03.492329+00:00",
"wireguard_after_cleanup": 0,
"current_certificate_authenticated": true,
"vm": "discovery-opnsense",
"native_runtime_checks": {
"rust_freebsd_toolchain": "1.99.1",
"shared_contract_and_reconciliation_tests_passed": 71,
"qualified_clock_and_step_rejection": true,
"owned_process_group_tests_as_service_account_passed": 2,
"approved_rmm_script_executed": false,
"signed_agent_self_update_verified": false
},
"native_os": "OPNsense 26.7 (amd64), FreeBSD 15.1-RELEASE-p1",
"agent_sha256": "1e545467e0e96b87210aa39c4ab7e0f3e0ca5bf0bbbf3cb0140e2f229875e1a4",
"server_revision": "b7d9046996fdf4778d04b24a8ab2effd19647db7",
"public_browser": {
"rules_rendered": 91,
"wireguard_loopback_test_peer_handshakes_rendered": 2,
"test_interfaces_removed": true,
"vpn_section_rendered": true,
"native_interface_rendered": true,
"native_package_search_verified": true
},
"packages": 169,
"interfaces": 6,
"device_type": "virtual",
"collector_sha256": "6c4b2fd8a6d2bb9d435a00e40bca63bd53b6430006f044b3edac3655efad30db"
}
+4
View File
@@ -13,6 +13,10 @@
## Ergänzungen zur Geräteansicht ## Ergänzungen zur Geräteansicht
- **OPNsense-Kernelabfrage, 19.11.2026:** Server/WebUI `2e060e9` und Client `0.1.0-opnsense.5` aktiv. Dieselbe Geräteidentität meldete einen angewendeten signierten Abgleich. Ein isolierter nativer Integrationstest auf dem Dienstkonto bestätigte `FreeBSD\n`/Exitcode 0 und genau eine Grant-Verwendung, auch nach doppelter Zustellung. Der getrennte, im öffentlichen Browser eingereichte [Geräteantrag](evidence/opnsense-probe-submission-20261119.json) hatte bei der Prüfung `pending_approval`, null Jobs, Outbox-Einträge und Startgrants. Für den fachlichen End-to-End-Nachweis fehlen persönliche Freigabe und das Ergebnis genau dieses Versuchs.
- **OPNsense-Testgerät:** OPNsense 26.7/FreeBSD 15.1 nativ in der Notebook-VM gestartet; Aufnahme per Kunden-Einmalcode, lokaler P-256-Schlüssel, ausgehendes mTLS. Die öffentliche Geräteansicht zeigte 91 aktive PF-Regeln und beide tatsächlichen Handshakes eines vorübergehenden lokalen WireGuard-Testpaars. Beide Testinterfaces wurden danach entfernt. OpenVPN ohne Verbindungen, IPsec-Dienst nicht erreichbar. Heartbeats, Inventar, CPU und RAM mit derselben Installation nach Agentwechsel geprüft. Version `0.1.0-opnsense.4` meldete Gerätetyp `virtual`, sechs Interfaces und 169 Pakete; Netzwerkkarte und Paketsuche wurden im öffentlichen Browser nachgelesen. Im Gast bestanden 71 Rust-Vertrags-/Abgleichtests, der Test der FreeBSD-Kerneluhr und zwei Prozessgruppenprüfungen als Dienstkonto. Noch offen: durchgängiger Skript-/Root-Jobpfad, signierter OPNsense-Releasekanal und öffentliche Erstinstallation. [Betrieb](../deploy/opnsense/README.md) · [Nachweis](evidence/opnsense-test-device-20261119.json)
- **RAM-Balken:** Der alte Balken hob bei „RAM frei / gesamt“ den belegten Anteil hervor. Der neue Balken färbt verfügbare Kapazität grün und belegte grau und beschriftet den freien Anteil direkt. Bei fehlenden oder ungültigen Werten wird kein Balken gezeichnet. Build, 80 Komponententests und sechs Browserprüfungen einschließlich 0 %, 75 % und 100 % frei bestanden. WebUI `577a6ee` am 19. November 2026 um 18:38 UTC ausgeliefert. Angemeldetes Chromium bestätigte für neun reale Geräte die gerenderten Segmentbreiten und Beschriftungen bei 1280, 900 und 390 px. API, Datenbank und Agenten mussten nicht geändert werden. [Browserregression](../tests/browser/customer-management.spec.ts) - **RAM-Balken:** Der alte Balken hob bei „RAM frei / gesamt“ den belegten Anteil hervor. Der neue Balken färbt verfügbare Kapazität grün und belegte grau und beschriftet den freien Anteil direkt. Bei fehlenden oder ungültigen Werten wird kein Balken gezeichnet. Build, 80 Komponententests und sechs Browserprüfungen einschließlich 0 %, 75 % und 100 % frei bestanden. WebUI `577a6ee` am 19. November 2026 um 18:38 UTC ausgeliefert. Angemeldetes Chromium bestätigte für neun reale Geräte die gerenderten Segmentbreiten und Beschriftungen bei 1280, 900 und 390 px. API, Datenbank und Agenten mussten nicht geändert werden. [Browserregression](../tests/browser/customer-management.spec.ts)
- **Zweck je Gerät:** Optionale Spalte im Bestand und in der Historie, Freitextpflege im Gerätedetail. WebUI-Build, 80 Komponententests, fünf Browserprüfungen und HTTP-Rundlauf auf isoliertem PostgreSQL mit Schreib-, Lese- und Fremdkundenfällen bestanden. Am 19. November 2026 um 14:10 UTC wurde `2070ae9` mit [Migration 45](../migrations/0045_device_purpose.sql) aktiviert. API, öffentliche API und Gateway bestanden die TLS-Healthchecks; der öffentliche WebUI-Index entsprach dem Build. Angemeldetes Chromium zeigte die Zweck-Spalte für sechs Geräte, den Editor im Gerätedetail und die Spalte bei 390 px Breite. Bei der Live-Sichtprüfung wurde nichts gespeichert; kein Agentwechsel. [Bedienung](public-access.md#zweck-eines-geräts-pflegen) · [Browserregression](../tests/browser/device-purpose.spec.ts) - **Zweck je Gerät:** Optionale Spalte im Bestand und in der Historie, Freitextpflege im Gerätedetail. WebUI-Build, 80 Komponententests, fünf Browserprüfungen und HTTP-Rundlauf auf isoliertem PostgreSQL mit Schreib-, Lese- und Fremdkundenfällen bestanden. Am 19. November 2026 um 14:10 UTC wurde `2070ae9` mit [Migration 45](../migrations/0045_device_purpose.sql) aktiviert. API, öffentliche API und Gateway bestanden die TLS-Healthchecks; der öffentliche WebUI-Index entsprach dem Build. Angemeldetes Chromium zeigte die Zweck-Spalte für sechs Geräte, den Editor im Gerätedetail und die Spalte bei 390 px Breite. Bei der Live-Sichtprüfung wurde nichts gespeichert; kein Agentwechsel. [Bedienung](public-access.md#zweck-eines-geräts-pflegen) · [Browserregression](../tests/browser/device-purpose.spec.ts)
+6
View File
@@ -0,0 +1,6 @@
SET LOCAL lock_timeout = '3s';
SET LOCAL statement_timeout = '30s';
ALTER TABLE observations DROP CONSTRAINT observations_source_id_check;
ALTER TABLE observations ADD CONSTRAINT observations_source_id_check
CHECK (source_id IN ('linux.base','windows.base','agent.update','opnsense.base')) NOT VALID;
ALTER TABLE observations VALIDATE CONSTRAINT observations_source_id_check;
+9 -1
View File
@@ -14,7 +14,15 @@ function values(value: unknown) {
return Array.isArray(value) ? value.flatMap(value => text(value) ?? []) : []; return Array.isArray(value) ? value.flatMap(value => text(value) ?? []) : [];
} }
function observation(device: RecordValue, name: string) { function observation(device: RecordValue, name: string): RecordValue | undefined {
if (device.source_id === 'opnsense.base') {
const inv = object(device.inventory);
if (name === 'hardware' || name === 'os') return { data: inv.hardware };
const report = object(inv[name]);
if (report.status === 'ok' || report.status === 'partial')
return { data: report.entries, truncated: report.status === 'partial' };
return undefined;
}
const observations = object(device.inventory).observations; const observations = object(device.inventory).observations;
return Array.isArray(observations) ? observations.map(object).find(item => return Array.isArray(observations) ? observations.map(object).find(item =>
item.quality === 'valid' && (item.scope === 'linux.' + name || item.scope === 'windows.' + name) item.quality === 'valid' && (item.scope === 'linux.' + name || item.scope === 'windows.' + name)
+4
View File
@@ -17,6 +17,8 @@ import { allowed } from './safety';
import { CatalogCheckNotice } from './catalog-check'; import { CatalogCheckNotice } from './catalog-check';
import { devicePlatform } from './script-catalog'; import { devicePlatform } from './script-catalog';
import { DeviceInventoryPanels, systemDetails } from './device-inventory'; import { DeviceInventoryPanels, systemDetails } from './device-inventory';
import { OPNsenseDetails } from './opnsense';
import { OPNsenseProbe } from './opnsense-probe';
import { DeviceCustomerTransfer } from './device-customer-transfer'; import { DeviceCustomerTransfer } from './device-customer-transfer';
import { HypervisorAssignment, HypervisorLabel } from './device-hypervisor'; import { HypervisorAssignment, HypervisorLabel } from './device-hypervisor';
import { ErrorBox, errorText, useResource } from './resource'; import { ErrorBox, errorText, useResource } from './resource';
@@ -300,6 +302,8 @@ function DeviceDetail({
success={purposeSaved} success={purposeSaved}
onSaved={() => { setPurposeSaved(true); detail.reload(); }} onSaved={() => { setPurposeSaved(true); detail.reload(); }}
/> />
<OPNsenseDetails device={d} />
{scoped && <OPNsenseProbe key={`${customer}/${asset}/${agent}`} device={d} auth={scoped}/>}
<DeviceInventoryPanels device={d} /> <DeviceInventoryPanels device={d} />
{volumes.length > 0 && ( {volumes.length > 0 && (
<section> <section>
+48
View File
@@ -0,0 +1,48 @@
import { useState } from 'react';
import { objectId, record, request, tenantPath, type RecordValue, type Session } from './api';
import { allowed } from './safety';
import { verifiedScriptOrder } from './order-composer';
import { ErrorBox, errorText } from './resource';
export function OPNsenseProbe({ device, auth }: {device: RecordValue; auth: Session}) {
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [ticket, setTicket] = useState<{id:string; hash:string} | null>(null);
const [operation, setOperation] = useState<string | null>(null);
if (device.source_id !== 'opnsense.base' || device.lifecycle !== 'current' ||
!Array.isArray(device.capabilities) || !device.capabilities.includes('opnsense.uname/1') ||
!allowed(auth,'operations:plan') || !allowed(auth,'scripts:execute')) return null;
const customer = objectId(device,'tenant'), asset = objectId(device,'asset'), agent = objectId(device,'agent');
const path = tenantPath(customer,'/operation-plans');
async function submit() {
setBusy(true); setError(null);
try {
let pending = ticket;
if (!pending) {
const now = Date.now(), start = new Date(now).toISOString(), end = new Date(now+86400000).toISOString();
const reason = 'OPNsense: Kernelname auf dem Dienstkonto prüfen';
const raw = await request(path, {csrf:auth.csrf_token,body:{tenant_id:customer,action:'run_script',
targets:{asset_ids:[asset]},parameters:{execution_identity:'agent_service',timeout_seconds:10,
max_output_bytes:1024,arguments:[],expected_stdout:'FreeBSD\n',
variants:[{os_family:'opnsense',interpreter:'posix_sh',encoding:'utf-8',script_utf8:'uname -s\n'}]},
schedule:{not_before:start,start_before:end,offline_policy:'wait_until_deadline'},
rollout:{max_parallel:1,halt_after_failures:1},reason}});
const checked = await verifiedScriptOrder(raw,customer,auth.principal.id), j = checked.jobs[0];
if (!j || checked.jobs.length!==1 || j.assetId!==asset || j.agentId!==agent || j.identity!=='agent_service' ||
j.script!=='uname -s\n' || j.expectedStdout!=='FreeBSD\n' || j.interpreter!=='posix_sh' ||
j.arguments.length || j.runtimeSeconds!==10 || j.outputBytes!==1024 || checked.reason!==reason ||
Date.parse(j.notBefore)!==now || Date.parse(j.startBefore)!==Date.parse(end) ||
checked.maxParallel!==1 || checked.halt!==1) throw Error('Plan weicht von der Kernelabfrage ab.');
pending = {id:objectId(checked.plan,'plan'),hash:String(checked.plan.plan_hash)};
setTicket(pending);
}
const r = record(await request(path+'/'+pending.id+'/submit',{csrf:auth.csrf_token,body:{idempotency_key:pending.id}}));
if (r.plan_id!==pending.id || r.plan_hash!==pending.hash) throw Error('Planbindung stimmt nicht.');
setOperation(objectId(r,'operation'));
} catch (e) { setError(errorText(e)); } finally { setBusy(false); }
}
return <section><h2>OPNsense-Kernelabfrage</h2><p><code>uname -s</code> als Dienstkonto · ein Versuch · 10 Sekunden · 1 KiB Ausgabe.</p>
{operation && ticket ? <p>Auftrag eingereicht. <a href={`#genehmigungen/pruefen/${customer}/${ticket.id}`}>Zur Genehmigung</a></p>
: <button disabled={busy} onClick={submit}>Kernelabfrage zur Genehmigung einreichen</button>}
<ErrorBox error={error}/></section>;
}
+24
View File
@@ -0,0 +1,24 @@
import { renderToStaticMarkup } from 'react-dom/server';
import { expect, it } from 'vitest';
import { OPNsenseDetails } from './opnsense';
import { DeviceInventoryPanels } from './device-inventory';
it('keeps missing, empty and partial appliance sources distinct and limits panels to OPNsense', () => {
const d = { source_id:'opnsense.base', inventory:{
ipsec:{status:'unavailable',entries:[]}, openvpn:{status:'ok',entries:[]},
firewall:{status:'partial', entries:['pass in on vtnet0']},
interfaces:{status:'ok', entries:[{name:'vtnet0',addresses:['192.0.2.1/24']}]},
software:{status:'ok',entries:[{name:'opnsense',version:'26.7'}]},
users:{status:'unavailable',entries:[]}
}};
const view = renderToStaticMarkup(<OPNsenseDetails device={d}/>);
expect(view).toMatch(/IPsec[^<]*<\/h3><p>Nicht ermittelbar/);
expect(view).toMatch(/OpenVPN[^<]*<\/h3><p>Keine Einträge/);
expect(view).toContain('unvollständige Liste');
expect(view).toContain('pass in on vtnet0');
expect(renderToStaticMarkup(<OPNsenseDetails device={{...d,source_id:'linux.base'}}/>)).toBe('');
const system = renderToStaticMarkup(<DeviceInventoryPanels device={d}/>);
expect(system).toContain('192.0.2.1/24');
expect(system).toContain('26.7');
expect(system).not.toContain('Keine Benutzer angemeldet');
});
+24
View File
@@ -0,0 +1,24 @@
import type { RecordValue } from './api';
export function OPNsenseDetails({ device }: { device: RecordValue }) {
if (device.source_id !== 'opnsense.base') return null;
const inventory = device.inventory as RecordValue | null;
function list(key: string, title: string) {
const report = inventory?.[key] as RecordValue | undefined;
const available = report?.status === 'ok' || report?.status === 'partial';
const rows = Array.isArray(report?.entries) ? report.entries.filter((x): x is string => typeof x === 'string') : [];
return <div key={key}><h3>{title}</h3>{!available ? <p>Nicht ermittelbar</p> : <>
{report?.status === 'partial' && <p>Auszug – unvollständige Liste</p>}
{rows.length ? <ol>{rows.map((line, i) => <li key={i}><code>{line}</code></li>)}</ol> : <p>Keine Einträge gemeldet.</p>}
</>}</div>;
}
return <>
<section><h2>VPN-Verbindungen</h2>
{list('vpn_configured', 'Konfigurierte VPNs')}
{list('wireguard', 'WireGuard · letzter Handshake je Peer')}
{list('openvpn', 'OpenVPN · Laufzeitstatus')}
{list('ipsec', 'IPsec · aktive Sicherheitsassoziationen')}
</section>
<section><h2>Firewallregeln</h2>{list('firewall', 'Aktive PF-Filterregeln · Hauptregelsatz in Auswertungsreihenfolge')}</section>
</>;
}
+1 -1
View File
@@ -81,7 +81,7 @@ function resumeTicket(key: string): Ticket | null {
} }
return data; return data;
} }
async function verifiedScriptOrder(value: unknown, tenantId: string, principal: string) { export async function verifiedScriptOrder(value: unknown, tenantId: string, principal: string) {
const plan = record(value, 'Auftragsplan'); const plan = record(value, 'Auftragsplan');
await verifyPlanIntegrity(plan); await verifyPlanIntegrity(plan);
await verifyScriptPlan(plan); await verifyScriptPlan(plan);