Project native FreeBSD packages, interfaces, hardware and login observations
This commit is contained in:
@@ -18,6 +18,35 @@ struct Snapshot {
|
||||
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)]
|
||||
@@ -47,6 +76,14 @@ pub(super) fn validate(bytes: &[u8]) -> Result<Value, &'static str> {
|
||||
|| 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)
|
||||
}
|
||||
|
||||
@@ -321,6 +321,7 @@ fn device_type(hardware: &Value) -> Option<DeviceType> {
|
||||
fn inventory_device_type(data: &Value) -> Option<DeviceType> {
|
||||
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> {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user