diff --git a/crates/domain/src/inventory_document/opnsense.rs b/crates/domain/src/inventory_document/opnsense.rs index 0ed3e51..cf3360c 100644 --- a/crates/domain/src/inventory_document/opnsense.rs +++ b/crates/domain/src/inventory_document/opnsense.rs @@ -18,6 +18,35 @@ struct Snapshot { wireguard: Rows, openvpn: Rows, ipsec: Rows, + hardware: Option, + software: Option>, + interfaces: Option>, + users: Option, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Hardware { + device_type: String, + booted_at: chrono::DateTime, + manufacturer: Option, + bios_serial_number: Option, +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Records { status: String, entries: Vec } +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Software { name: String, version: String } +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Interface { name: String, mac: Option, addresses: Vec } +impl Records { + 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)] @@ -47,6 +76,14 @@ pub(super) fn validate(bytes: &[u8]) -> Result { || 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) } diff --git a/crates/server/src/devices.rs b/crates/server/src/devices.rs index a5256cd..57156f8 100644 --- a/crates/server/src/devices.rs +++ b/crates/server/src/devices.rs @@ -321,6 +321,7 @@ fn device_type(hardware: &Value) -> Option { fn inventory_device_type(data: &Value) -> Option { device_type(observed(data, "linux.hardware")) .or_else(|| device_type(observed(data, "windows.hardware"))) + .or_else(|| device_type(&data["hardware"])) } fn hostname_filter(query: Option<&str>) -> Result<&str, Error> { diff --git a/web/src/device-inventory.tsx b/web/src/device-inventory.tsx index 748d564..07aa0b1 100644 --- a/web/src/device-inventory.tsx +++ b/web/src/device-inventory.tsx @@ -14,7 +14,15 @@ function values(value: unknown) { 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; return Array.isArray(observations) ? observations.map(object).find(item => item.quality === 'valid' && (item.scope === 'linux.' + name || item.scope === 'windows.' + name)