Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bf6e2539c | ||
|
|
50b1fa4033 | ||
|
|
b7d9046996 | ||
|
|
d05caf6fd3 | ||
|
|
89a58c609c | ||
|
|
ac6f940e2e | ||
|
|
577a6eea75 | ||
|
|
58236bc8f0 | ||
|
|
2070ae95e7 |
@@ -12,7 +12,7 @@ Der Agent meldet den Gerätetyp mit dem passiven Hardwareinventar. `physical` be
|
||||
|
||||
Für aktuelle virtuelle Geräte kann die WebUI einen registrierten Hypervisor aus demselben Kundenbestand zuordnen. Die Geräteübersicht zeigt diese Verbindung unter dem Hostnamen der VM und verlinkt auf die Gerätedetails des Hypervisors. Die Zuordnung gilt, solange der verknüpfte Host im Bestand ist und sich als Hypervisor meldet. Eine VM mit gemeldetem Hersteller Hetzner erhält bei fehlendem konkreten Host den Anzeigeplatzhalter „Hetzner Cloud“. Der Platzhalter hat keine Kopplung und zählt nicht als registriertes Gerät.
|
||||
|
||||
Die RAM-Spalte zeigt den für Anwendungen verfügbaren und den gesamten physischen Arbeitsspeicher in GiB. Wie beim Speicherplatz zeigt ein Balken den belegten Anteil. Die CPU-Spalte ergänzt die zuletzt gemessene Systemauslastung als Prozentwert. Die Agenten gewinnen sie aus den CPU-Zeitdifferenzen zwischen zwei Heartbeats im Abstand von ungefähr 30 Sekunden. Linux liefert systemweite Werte. Windows liefert den Wert, wenn alle logischen CPUs zu einer einzigen Prozessorgruppe gehören. Vor dem ersten aussagekräftigen Intervall bleibt die Auslastung offen. Die WebUI und MCP lesen dieselbe letzte Messung. Für Agenten ohne Ressourcenmeldung stammen verfügbare RAM-Werte aus dem vorhandenen Inventar. [Inventarprojektion](crates/server/src/devices.rs)
|
||||
Die RAM-Spalte zeigt den für Anwendungen verfügbaren und den gesamten physischen Arbeitsspeicher in GiB. Im RAM-Balken ist der verfügbare Anteil (links) grün und der belegte Anteil grau. Die sichtbare Prozentangabe benennt den verfügbaren Anteil. Ohne gültiges Wertepaar erscheint kein Balken. Der Laufwerksbalken hebt weiterhin den belegten Anteil hervor. Die CPU-Spalte ergänzt die zuletzt gemessene Systemauslastung als Prozentwert. Die Agenten gewinnen sie aus den CPU-Zeitdifferenzen zwischen zwei Heartbeats im Abstand von ungefähr 30 Sekunden. Linux liefert systemweite Werte. Windows liefert den Wert, wenn alle logischen CPUs zu einer einzigen Prozessorgruppe gehören. Vor dem ersten aussagekräftigen Intervall bleibt die Auslastung offen. Die WebUI und MCP lesen dieselbe letzte Messung. Für Agenten ohne Ressourcenmeldung stammen verfügbare RAM-Werte aus dem vorhandenen Inventar. [Inventarprojektion](crates/server/src/devices.rs)
|
||||
|
||||
Kunden lassen sich in der WebUI anlegen und umbenennen. Unter **Gerät verwalten → Kunden wechseln** wählt eine berechtigte Person den Zielkunden. Der Server prüft die Rechte zur Geräteverwaltung im bisherigen Kunden und zur Geräteaufnahme im Zielkunden. Der bereits installierte Agent empfängt den Wechsel über seine authentisierte Verbindung und erhält ein neues Zertifikat für eine neue Kopplung. Mit der ersten gültigen Inventarmeldung der authentisierten neuen Kopplung schließt der Server den Wechsel automatisch ab. Er sperrt die bisherige Geräteidentität und schaltet den Bestand in derselben Transaktion auf den Zielkunden um. Inventar und Auftragsergebnisse der bisherigen Kopplung bleiben beim bisherigen Kunden. Noch nicht abgeschlossene Aufträge, ungeklärte Versuche und gültige offene Genehmigungen müssen vor dem Wechsel abgeschlossen werden. Gerätegebundene erhöhte Rechte erfordern eine eigene Bindung für die neue Kopplung.
|
||||
|
||||
@@ -20,6 +20,8 @@ Eine ausgeschiedene Installation kann nach Widerruf und persönlicher Bestätigu
|
||||
|
||||
In den Gerätedetails erscheinen OS-Installationszeit, letzter Systemstart, Hersteller, BIOS-Seriennummer, Netzwerkkarten samt IPv4- und IPv6-Adressen, installierte Software und die bei der letzten Inventur angemeldeten Benutzer. Windows meldet den vom Betriebssystem erfassten Installationszeitpunkt. Linux kennzeichnet den Zeitstempel aus dem Installer-Verzeichnis oder dem Root-Dateisystem als Schätzung. Das Softwareinventar erfasst Linux-Pakete aus dpkg und Windows-Systeminstallationen aus der Uninstall-Registrierung. Begrenzte Meldungen werden ausgewiesen. Herstellergarantie und Firmwaredaten erscheinen bei verfügbarem Befund. Die Daten gehören zum kundengebundenen Inventar des Geräts. [Geräteansicht](web/src/device-inventory.tsx)
|
||||
|
||||
Die Spalte **Zweck** und das Gerätedetail zeigen optionalen Freitext. Unter **Zweck bearbeiten** können Betreiber mit Inventarlese- und Geräteverwaltungsrecht für den betreffenden Kunden bis zu 500 Zeichen speichern oder den Text entfernen. Der Zweck gehört zum Gerät beim bisherigen Kunden und wird beim Kundenwechsel nicht übernommen. Die Änderung wird ohne den Text auditiert und erzeugt keinen Geräteauftrag.
|
||||
|
||||
## Vom Skript zum Ergebnis
|
||||
|
||||
| Schritt | Verhalten |
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
//! Additive bounded passive metadata; the strict original Inventory stays intact.
|
||||
mod opnsense;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
@@ -64,6 +65,9 @@ pub fn validate_for_scope(
|
||||
now: DateTime<Utc>,
|
||||
scope: &str,
|
||||
) -> Result<Value, &'static str> {
|
||||
if scope == "opnsense.base" {
|
||||
return opnsense::validate(bytes);
|
||||
}
|
||||
if scope == "agent.update" {
|
||||
if bytes.len() > 65536 {
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -160,7 +160,7 @@ pub async fn ingest(
|
||||
ensure!(
|
||||
matches!(
|
||||
s.scope.as_str(),
|
||||
"linux.base" | "windows.base" | "agent.update"
|
||||
"linux.base" | "windows.base" | "opnsense.base" | "agent.update"
|
||||
) && s.schema_version == 1
|
||||
&& s.chunk_count == 1
|
||||
&& s.chunk_index == 0
|
||||
|
||||
@@ -3,7 +3,7 @@ use axum::{
|
||||
Json, Router,
|
||||
extract::{Path, Query, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
routing::{delete, post},
|
||||
routing::{delete, patch, post},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
@@ -34,6 +34,10 @@ pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
|
||||
"/api/v1/tenants/{tenant}/devices/{asset}/archive",
|
||||
post(archive_device),
|
||||
)
|
||||
.route(
|
||||
"/api/v1/tenants/{tenant}/devices/{asset}/purpose",
|
||||
patch(update_device_purpose),
|
||||
)
|
||||
.route(
|
||||
"/api/v1/tenants/{tenant}/operation-plans/{plan}/revocation",
|
||||
post(revoke_plan),
|
||||
@@ -132,6 +136,66 @@ async fn archive_device(
|
||||
tx.commit().await?;
|
||||
Ok(Json(json!({"asset_id":asset, "archived":input.archived})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Purpose {
|
||||
purpose: String,
|
||||
}
|
||||
|
||||
fn normalize_purpose(raw: &str) -> Result<Option<String>, Error> {
|
||||
let normalized = raw.replace("\r\n", "\n");
|
||||
let text = normalized.trim();
|
||||
if text.chars().count() > 500
|
||||
|| normalized
|
||||
.chars()
|
||||
.any(|c| c.is_control() && !matches!(c, '\n' | '\t'))
|
||||
{
|
||||
return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
||||
}
|
||||
Ok((!text.is_empty()).then(|| text.to_owned()))
|
||||
}
|
||||
|
||||
async fn update_device_purpose(
|
||||
State(s): State<crate::State>,
|
||||
h: HeaderMap,
|
||||
Path((tenant, asset)): Path<(Uuid, Uuid)>,
|
||||
Json(input): Json<Purpose>,
|
||||
) -> Result<Json<Value>, Error> {
|
||||
const EVENT: &str = "device_purpose_updated";
|
||||
let (p, mut tx) = context(&s, &h, tenant, "devices:revoke", EVENT).await?;
|
||||
crate::permissions::require_principal(&mut tx, &p, "inventory:read").await?;
|
||||
let purpose = normalize_purpose(&input.purpose)?;
|
||||
let current: Option<String> = sqlx::query_scalar(
|
||||
"SELECT purpose FROM assets WHERE tenant_id=$1 AND asset_id=$2
|
||||
AND EXISTS (SELECT 1 FROM agent_installations a WHERE a.tenant_id=$1 AND a.asset_id=$2
|
||||
AND NOT a.customer_transfer_pending)
|
||||
FOR UPDATE",
|
||||
)
|
||||
.bind(tenant)
|
||||
.bind(asset)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?
|
||||
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
||||
if current != purpose {
|
||||
sqlx::query("UPDATE assets SET purpose=$3 WHERE tenant_id=$1 AND asset_id=$2")
|
||||
.bind(tenant)
|
||||
.bind(asset)
|
||||
.bind(&purpose)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
audit(
|
||||
&mut tx,
|
||||
&p,
|
||||
EVENT,
|
||||
json!({"asset_id":asset,"cleared":purpose.is_none()}),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
tx.commit().await?;
|
||||
Ok(Json(json!({"asset_id":asset,"purpose":purpose})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Pairing {
|
||||
|
||||
@@ -3,7 +3,7 @@ WITH classified AS (
|
||||
a.last_authenticated_at,a.last_authenticated_certificate_sha256,a.certificate_sha256,
|
||||
a.enrollment_generation,a.assignment_version,a.capabilities,a.reported_agent_version,
|
||||
a.system_usage,a.system_usage_observed_at,
|
||||
asset.archived_at,asset.hypervisor_asset_id,t.name AS customer_name,
|
||||
asset.archived_at,asset.hypervisor_asset_id,asset.purpose,t.name AS customer_name,
|
||||
CASE
|
||||
WHEN a.revoked_at IS NOT NULL THEN 'revoked'
|
||||
WHEN asset.archived_at IS NOT NULL THEN 'archived'
|
||||
@@ -34,7 +34,7 @@ SELECT c.*,o.observed_at,o.received_at,o.data,o.source_id FROM classified c
|
||||
LEFT JOIN LATERAL (
|
||||
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
|
||||
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
|
||||
) o ON true
|
||||
WHERE ($6::text IS NULL OR (c.lifecycle='current')=($6='current'))
|
||||
|
||||
@@ -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> {
|
||||
@@ -517,7 +518,8 @@ fn project(row: &PgRow, now: DateTime<Utc>, full: bool, hosts: &HashMap<Uuid, Ho
|
||||
let ram_total = usage["ram_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 last = row.get::<Option<DateTime<Utc>>, _>("last_contact_at");
|
||||
let revoked = row.get::<Option<DateTime<Utc>>, _>("revoked_at");
|
||||
@@ -533,10 +535,11 @@ fn project(row: &PgRow, now: DateTime<Utc>, full: bool, hosts: &HashMap<Uuid, Ho
|
||||
"agent_id":row.get::<Uuid,_>("agent_id"),"asset_id":row.get::<Uuid,_>("asset_id"),
|
||||
"status":row.get::<String,_>("status"),"lifecycle":row.get::<String,_>("lifecycle"),
|
||||
"archived_at":row.get::<Option<DateTime<Utc>>,_>("archived_at"),"revoked_at":revoked,
|
||||
"hostname":data["hostname"].as_str(),"device_type":device_type,"os":os_label(&data),
|
||||
"hostname":data["hostname"].as_str(),"purpose":row.get::<Option<String>,_>("purpose"),
|
||||
"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(),
|
||||
"cpu_model":cpu["model"].as_str().or_else(||windows["cpu_model"].as_str()),
|
||||
"cpu_logical_count":cpu["logical_processors"].as_u64().or_else(||windows["logical_processors_visible"].as_u64()),
|
||||
"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()).or_else(||data["logical_processors"].as_u64()),
|
||||
"cpu_usage_percent":cpu_usage,
|
||||
"cpu_usage_observed_at":cpu_observed_at,
|
||||
"ram_total_bytes":ram_total,
|
||||
|
||||
@@ -492,6 +492,8 @@ mod tests {
|
||||
"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"test customer authorization"});
|
||||
for request in [
|
||||
client.get(format!("{base}/{b}/devices/{asset}")),
|
||||
client.patch(format!("{base}/{b}/devices/{asset}/purpose"))
|
||||
.json(&json!({"purpose":"foreign write"})),
|
||||
client.delete(format!("{base}/{b}/devices/{asset}?agent_id={id}")),
|
||||
client
|
||||
.post(format!("{base}/{b}/operation-plans"))
|
||||
@@ -530,6 +532,14 @@ mod tests {
|
||||
.send().await.unwrap().json::<Value>().await.unwrap();
|
||||
let deletion = || client.delete(&pair).header("origin", origin).header("cookie", cookie);
|
||||
let csrf = session["csrf_token"].as_str().unwrap();
|
||||
let detail = format!("{base}/{a}/devices/{asset}");
|
||||
let purpose = format!("{detail}/purpose");
|
||||
let update = |text: &str| client.patch(&purpose).header("origin", origin)
|
||||
.header("cookie", cookie).json(&json!({"purpose":text}));
|
||||
assert_eq!(client.patch(&purpose).bearer_auth(&token).json(&json!({"purpose":"token write"}))
|
||||
.send().await.unwrap().status(), StatusCode::FORBIDDEN);
|
||||
assert_eq!(update("reader denied").header("x-csrf-token", csrf)
|
||||
.send().await.unwrap().status(), StatusCode::FORBIDDEN);
|
||||
assert_eq!(deletion().header("x-csrf-token", csrf).send().await.unwrap().status(), StatusCode::FORBIDDEN);
|
||||
sqlx::query("UPDATE memberships SET permissions=array_append(permissions,'devices:revoke') WHERE tenant_id=$1 AND principal=$2")
|
||||
.bind(a).bind(&issuer).execute(&admin).await.unwrap();
|
||||
@@ -537,6 +547,25 @@ mod tests {
|
||||
.bind(a).bind(asset).execute(&admin).await.unwrap();
|
||||
sqlx::query("INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256) VALUES($1,$2,$3,$4)")
|
||||
.bind(a).bind(id).bind(asset).bind(hash(id.as_bytes())).execute(&admin).await.unwrap();
|
||||
assert_eq!(update("missing CSRF").send().await.unwrap().status(), StatusCode::FORBIDDEN);
|
||||
assert_eq!(update(&"ü".repeat(501)).header("x-csrf-token", csrf)
|
||||
.send().await.unwrap().status(), StatusCode::BAD_REQUEST);
|
||||
let text = "Dateiserver für Nürnberg\nNur Entwicklung";
|
||||
let saved: Value = update(" Dateiserver für Nürnberg\r\nNur Entwicklung ")
|
||||
.header("x-csrf-token", csrf).send().await.unwrap().error_for_status().unwrap().json().await.unwrap();
|
||||
assert_eq!(saved["purpose"], text);
|
||||
let device: Value = client.get(&detail).bearer_auth(&token)
|
||||
.send().await.unwrap().error_for_status().unwrap().json().await.unwrap();
|
||||
assert_eq!(device["purpose"], text);
|
||||
let other: Option<String> = sqlx::query_scalar("SELECT purpose FROM assets WHERE tenant_id=$1 AND asset_id=$2")
|
||||
.bind(b).bind(asset).fetch_one(&admin).await.unwrap();
|
||||
assert!(other.is_none());
|
||||
let cleared: Value = update(" \n ").header("x-csrf-token", csrf)
|
||||
.send().await.unwrap().error_for_status().unwrap().json().await.unwrap();
|
||||
assert_eq!(cleared["purpose"], Value::Null);
|
||||
let events: Vec<Value> = sqlx::query_scalar("SELECT data->'decision' FROM audit_events WHERE tenant_id=$1 AND event_type='device_purpose_updated' ORDER BY created_at")
|
||||
.bind(a).fetch_all(&admin).await.unwrap();
|
||||
assert_eq!(events, [json!({"asset_id":asset,"cleared":false}), json!({"asset_id":asset,"cleared":true})]);
|
||||
assert_eq!(deletion().send().await.unwrap().status(), StatusCode::FORBIDDEN);
|
||||
assert_eq!(deletion().header("x-csrf-token",csrf).send().await.unwrap().status(), StatusCode::CONFLICT);
|
||||
let revoked = client.post(format!("{base}/{a}/devices/{asset}/revocation?agent_id={id}"))
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# 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.
|
||||
|
||||
**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.
|
||||
|
||||
**Grenzen:** Das passive Profil meldet keine Skript-, Root- oder Release-Capability. Der bestehende Linux-Jobpfad verwendet Linux-spezifische Uhren, Helper und Interpreterprüfung. Für Parität benötigt OPNsense eigene native Adapter für den signierten Abgleich, den Startgrant und das Ausführungsjournal sowie einen eigenen signierten Installer-/Updatekanal. Der OPNsense-Gast darf nicht als Linuxziel für APT- oder Bash-Vorlagen angeboten werden.
|
||||
@@ -0,0 +1,215 @@
|
||||
"""Unprivileged OPNsense telemetry channel. Input never selects a local command."""
|
||||
import hashlib
|
||||
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.4'
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
def session(db, identity, boot, instance):
|
||||
ctx = context()
|
||||
ctx.load_cert_chain(STATE/'device.pem', STATE/'device.key')
|
||||
generation = 0
|
||||
|
||||
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))
|
||||
if reply.protocol_major != 1 or len(reply.messages) != 1:
|
||||
raise ValueError('unexpected reply')
|
||||
r = reply.messages[0]
|
||||
if (r.protocol_major,r.agent_id,r.enrollment_generation,r.boot_id,r.sequence) != (
|
||||
1,e.agent_id,1,e.boot_id,e.sequence):
|
||||
raise ValueError('reply identity')
|
||||
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')
|
||||
generation = send('client_hello',pb.ClientHello(agent_version=VERSION, capabilities=['heartbeat/1','opnsense.base/1']))
|
||||
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
|
||||
time.sleep(20)
|
||||
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
if sys.argv[1:] == ['enroll']:
|
||||
return enroll()
|
||||
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()
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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"
|
||||
@@ -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
|
||||
@@ -0,0 +1,67 @@
|
||||
"""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_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()
|
||||
@@ -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()
|
||||
@@ -11,6 +11,11 @@
|
||||
| Windows | `discwindowstest`, Windows 11 Pro x64, Agent `0.1.16` aus dem offiziellen GNU-Kanal |
|
||||
| Quellstand mit Dokumentation | Gitea-Branch `main` |
|
||||
|
||||
## Ergänzungen zur Geräteansicht
|
||||
|
||||
- **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)
|
||||
|
||||
## Geräteaktionen über MCP
|
||||
|
||||
Alle acht Versuche lieferten Exitcode 0. Bei beiden Neustarts wurde die Wiederanmeldung derselben Kopplung nach einem tatsächlichen Bootwechsel geprüft. Der Ping wurde jeweils erst nach dieser Wiederanmeldung eingereicht. Die Zeiten in der Tabelle sind UTC.
|
||||
|
||||
@@ -8,6 +8,10 @@ Die öffentliche WebUI verwendet ein lokales Benutzerkonto mit Passwort und eine
|
||||
|
||||
Berechtigte Benutzer können Kunden anlegen und umbenennen. Zur Aufnahme den Zielkunden wählen und unter **Verwaltung → Geräteaufnahme** einen Einmalcode erzeugen. Der Code gilt für diesen Kunden und verfällt bei erfolgreicher Aufnahme oder nach 15 Minuten. Die WebUI zeigt für die veröffentlichten Plattformen Download und Installationsbefehl. Auf dem neuen Gerät das HTTPS-Skript mit Administratorrechten aufrufen und den Code am Prompt eingeben. Für lokale Administrator-Skripte die dafür angebotene Installationsoption wählen. Der Windows-Kanal ist `x86_64-pc-windows-gnu`. [Linux-Installer](https://discovery.ease-technologies.de:8443/install/linux.sh) · [Windows-Installer](https://discovery.ease-technologies.de:8443/install/windows.ps1)
|
||||
|
||||
### Zweck eines Geräts pflegen
|
||||
|
||||
Unter **Geräte** den Hostnamen öffnen und im Abschnitt **Zweck** auf **Zweck bearbeiten** klicken. Der Freitext darf bis zu 500 Zeichen enthalten. Speichern übernimmt ihn auch in die Übersicht. Zum Entfernen das Feld leeren und erneut speichern. Das Bearbeiten erfordert `inventory:read` und `devices:revoke` im betreffenden Kunden.
|
||||
|
||||
### Hypervisor einer VM zuordnen
|
||||
|
||||
Eine VM unter **Geräte** öffnen und unter **Gerät verwalten → Hypervisor zuordnen** einen Host aus demselben Kundenbestand auswählen. Die Zuordnung speichern. Der Hostname erscheint unter dem VM-Namen in der Übersicht und öffnet die Details des Hypervisors. **Kein konkreter Hypervisor** entfernt die gespeicherte Zuordnung.
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Operator metadata belongs to the asset, not the installation or its inventory.
|
||||
ALTER TABLE assets ADD COLUMN purpose text CHECK (char_length(purpose) BETWEEN 1 AND 500);
|
||||
GRANT UPDATE(purpose) ON assets TO rmm_app;
|
||||
@@ -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;
|
||||
@@ -20,6 +20,63 @@ test.beforeAll(async () => {
|
||||
|
||||
test.afterAll(async () => { await server?.close(); });
|
||||
|
||||
test('draws the free RAM share accurately on desktop and mobile', async ({ page }) => {
|
||||
const customer = { tenant_id: '11111111-1111-4111-8111-111111111111', name: 'RAM-Test' };
|
||||
const gib = 2 ** 30;
|
||||
const readings = [
|
||||
['mostly-free', 12 * gib, 16 * gib],
|
||||
['all-used', 0, 16 * gib],
|
||||
['all-free', 16 * gib, 16 * gib],
|
||||
['missing-available', null, 16 * gib],
|
||||
['invalid-range', 17 * gib, 16 * gib],
|
||||
['invalid-total', 0, 0],
|
||||
] as const;
|
||||
const listing = (items: unknown[]) => ({
|
||||
items, next_cursor: null, truncated: false, completeness: 'complete', as_of: '2026-11-19T12:00:00Z'
|
||||
});
|
||||
await page.route(`${origin}/auth/config`, route => route.fulfill({
|
||||
json: { local_login_enabled: true, oidc_configured: false, oidc_login_url: null, dev_login_enabled: false }
|
||||
}));
|
||||
await page.route(`${origin}/auth/session`, route => route.fulfill({
|
||||
json: { principal: 'ram-reader', testsimulation: false, tenant_id: customer.tenant_id,
|
||||
csrf_token: 'ram-fixture', permissions: ['inventory:read'] }
|
||||
}));
|
||||
await page.route(`${origin}/api/v1/**`, route => {
|
||||
const pathname = new URL(route.request().url()).pathname;
|
||||
if (pathname === '/api/v1/tenants') return route.fulfill({ json: listing([customer]) });
|
||||
if (pathname === '/api/v1/devices') return route.fulfill({
|
||||
json: listing(readings.map(([hostname, free, total]) => ({
|
||||
tenant_id: customer.tenant_id, customer_name: customer.name, asset_id: hostname, agent_id: hostname,
|
||||
lifecycle: 'current', status: 'online', hostname, ram_available_bytes: free, ram_total_bytes: total,
|
||||
disk_free_bytes: 12 * gib, disk_total_bytes: 16 * gib
|
||||
})))
|
||||
});
|
||||
return route.fulfill({ status: 404 });
|
||||
});
|
||||
await page.goto(`${origin}/#geraete`);
|
||||
const row = (host: string) => page.getByRole('row').filter({ has: page.getByRole('button', { name: host, exact: true }) });
|
||||
const ram = (host: string) => row(host).locator('[data-label="RAM frei / gesamt"]');
|
||||
await expect(ram('mostly-free')).toContainText('12 / 16 GiB');
|
||||
await expect(ram('mostly-free').locator('.capacity-caption')).toHaveText('75 % frei');
|
||||
await expect(row('mostly-free').locator('[data-label="Speicher frei / gesamt"] .capacity-track i'))
|
||||
.toHaveAttribute('style', 'width: 25%;');
|
||||
for (const width of [1280, 900, 390]) {
|
||||
await page.setViewportSize({ width, height: 844 });
|
||||
for (const [host, free] of [['mostly-free', 75], ['all-used', 0], ['all-free', 100]] as const) {
|
||||
const bar = ram(host).getByRole('img', { name: `${free} % frei, ${100 - free} % belegt` });
|
||||
await expect(bar).toBeVisible();
|
||||
const [track = 0, freeWidth = 0, usedWidth = 0] = await bar.evaluate(el => [el, ...Array.from(el.children)]
|
||||
.map(part => part.getBoundingClientRect().width));
|
||||
expect(track).toBeGreaterThan(0);
|
||||
expect(freeWidth / track).toBeCloseTo(free / 100, 2);
|
||||
expect(usedWidth / track).toBeCloseTo((100 - free) / 100, 2);
|
||||
}
|
||||
}
|
||||
for (const host of ['missing-available', 'invalid-range', 'invalid-total']) {
|
||||
await expect(ram(host).locator('.capacity-track')).toHaveCount(0);
|
||||
}
|
||||
});
|
||||
|
||||
test('creates and renames a customer, opens its inventory, and hides editing for a reader', async ({ page }) => {
|
||||
const home = { tenant_id: '11111111-1111-4111-8111-111111111111', name: 'Testzentrale' };
|
||||
const customerId = '22222222-2222-4222-8222-222222222222';
|
||||
@@ -108,12 +165,12 @@ test('creates and renames a customer, opens its inventory, and hides editing for
|
||||
const headers = page.locator('.fleet-table thead th[scope="col"]');
|
||||
await expect(headers.nth(0)).toHaveText('Hostname');
|
||||
await expect(headers.nth(1)).toHaveText('Type');
|
||||
await expect(device.getByRole('cell').nth(2)).toHaveText('hypervisor');
|
||||
await expect(device.getByRole('cell').nth(3)).toHaveText('Proxmox VE');
|
||||
await expect(device.locator('[data-label="Type"]')).toHaveText('hypervisor');
|
||||
await expect(device.locator('[data-label="OS"]')).toHaveText('Proxmox VE');
|
||||
const memory = device.locator('[data-label="RAM frei / gesamt"]');
|
||||
await expect(memory).toHaveText('6 / 16 GiB');
|
||||
await expect(memory.locator('.capacity-track')).toHaveAttribute('title', '37,5 % frei');
|
||||
await expect(memory.locator('.capacity-track i')).toHaveAttribute('style', 'width: 62.5%;');
|
||||
await expect(memory).toContainText('6 / 16 GiB');
|
||||
await expect(memory.getByRole('img', { name: '37,5 % frei, 62,5 % belegt' })).toBeVisible();
|
||||
await expect(memory.locator('.capacity-free')).toHaveAttribute('style', 'width: 37.5%;');
|
||||
await expect(device.locator('[data-label="Speicher frei / gesamt"] .capacity-track i'))
|
||||
.toHaveAttribute('style', 'width: 25%;');
|
||||
await expect(device.getByText('Auslastung 0 %', { exact: true })).toBeVisible();
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import { test, expect } from '../../web/node_modules/@playwright/test/index.js';
|
||||
import { createServer, type ViteDevServer } from '../../web/node_modules/vite/dist/node/index.js';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
let server: ViteDevServer;
|
||||
let origin: string;
|
||||
test.beforeAll(async () => {
|
||||
server = await createServer({
|
||||
root: resolve(__dirname, '../../web'), configFile: false,
|
||||
server: { host: '127.0.0.1', port: 0 }, logLevel: 'silent'
|
||||
});
|
||||
await server.listen();
|
||||
const address = server.httpServer!.address();
|
||||
if (!address || typeof address === 'string') throw new Error('Missing local fixture address');
|
||||
origin = `http://127.0.0.1:${address.port}`;
|
||||
});
|
||||
test.afterAll(async () => { await server?.close(); });
|
||||
|
||||
test('shows, edits and clears device purpose without giving readers an editor', async ({ page }) => {
|
||||
const customer = { tenant_id: '11111111-1111-4111-8111-111111111111', name: 'Fixture' };
|
||||
const path = `/api/v1/tenants/${customer.tenant_id}/devices/device-1`;
|
||||
const listing = (items: unknown[]) => ({
|
||||
items, next_cursor: null, truncated: false, completeness: 'complete', as_of: '2026-11-19T12:00:00Z'
|
||||
});
|
||||
let purpose: string | null = null;
|
||||
let writer = true;
|
||||
let reject = true;
|
||||
const writes: string[] = [];
|
||||
const device = () => ({
|
||||
tenant_id: customer.tenant_id, customer_name: customer.name, asset_id: 'device-1', agent_id: 'agent-1',
|
||||
lifecycle: 'current', status: 'online', hostname: 'DATEN-SRV', os: 'Linux', purpose
|
||||
});
|
||||
await page.route(`${origin}/auth/config`, route => route.fulfill({
|
||||
json: { local_login_enabled: true, oidc_configured: false, oidc_login_url: null, dev_login_enabled: false }
|
||||
}));
|
||||
await page.route(`${origin}/auth/session`, route => route.fulfill({
|
||||
json: {
|
||||
principal: writer ? 'device-operator' : 'inventory-reader', testsimulation: false,
|
||||
tenant_id: customer.tenant_id, csrf_token: 'purpose-csrf',
|
||||
permissions: writer ? ['inventory:read', 'devices:revoke'] : ['inventory:read']
|
||||
}
|
||||
}));
|
||||
await page.route(`${origin}/api/v1/**`, route => {
|
||||
const request = route.request();
|
||||
const pathname = new URL(request.url()).pathname;
|
||||
if (request.method() === 'GET') {
|
||||
if (pathname === '/api/v1/tenants') return route.fulfill({ json: listing([customer]) });
|
||||
if (pathname === '/api/v1/devices') return route.fulfill({ json: listing([device()]) });
|
||||
if (pathname === path) return route.fulfill({ json: device() });
|
||||
}
|
||||
if (request.method() === 'PATCH' && pathname === `${path}/purpose`) {
|
||||
expect(writer).toBe(true);
|
||||
expect(request.headers()['x-csrf-token']).toBe('purpose-csrf');
|
||||
expect(request.headers()['x-rmm-tenant']).toBe(customer.tenant_id);
|
||||
const text = request.postDataJSON().purpose;
|
||||
writes.push(text);
|
||||
if (reject) return route.fulfill({ status: 403, json: { code: 'FORBIDDEN', message: 'Speichern abgelehnt' } });
|
||||
purpose = text.trim() || null;
|
||||
return route.fulfill({ json: { asset_id: 'device-1', purpose } });
|
||||
}
|
||||
return route.fulfill({ status: 404, json: { code: 'UNEXPECTED_FIXTURE_REQUEST', message: pathname } });
|
||||
});
|
||||
|
||||
await page.goto(`${origin}/#geraete`);
|
||||
await expect(page.getByRole('columnheader', { name: 'Zweck' })).toBeVisible();
|
||||
await expect(page.getByRole('cell', { name: 'Nicht hinterlegt' })).toBeVisible();
|
||||
await page.getByRole('button', { name: 'DATEN-SRV' }).click();
|
||||
await page.getByRole('button', { name: 'Zweck bearbeiten' }).click();
|
||||
const editor = page.getByRole('textbox', { name: 'Zweck des Geräts' });
|
||||
const save = page.getByRole('button', { name: 'Zweck speichern' });
|
||||
await editor.fill('ü'.repeat(501));
|
||||
await expect(save).toBeDisabled();
|
||||
expect(writes).toEqual([]);
|
||||
const text = 'Dateien für Nürnberg\n<intern>';
|
||||
await editor.fill(text);
|
||||
await save.click();
|
||||
await expect(page.getByRole('alert')).toContainText('Speichern abgelehnt');
|
||||
await expect(editor).toHaveValue(text);
|
||||
reject = false;
|
||||
await save.click();
|
||||
await expect(page.getByText('Zweck gespeichert.')).toBeVisible();
|
||||
await page.getByRole('button', { name: 'Zur Geräteliste' }).click();
|
||||
await expect(page.locator('td[data-label="Zweck"]')).toHaveText(text.replace('\n', ' '));
|
||||
await page.setViewportSize({ width: 390, height: 844 });
|
||||
await expect(page.locator('td[data-label="Zweck"]')).toBeVisible();
|
||||
await page.getByRole('button', { name: 'DATEN-SRV' }).click();
|
||||
await page.getByRole('button', { name: 'Zweck bearbeiten' }).click();
|
||||
await expect(editor).toHaveValue(text);
|
||||
await editor.fill('');
|
||||
await save.click();
|
||||
await expect(page.getByText('Nicht hinterlegt', { exact: true })).toBeVisible();
|
||||
expect(writes).toEqual([text, text, '']);
|
||||
writer = false;
|
||||
purpose = text;
|
||||
await page.reload();
|
||||
await expect(page.locator('.device-purpose .purpose-text')).toHaveText(text.replace('\n', ' '));
|
||||
await expect(page.getByRole('button', { name: 'Zweck bearbeiten' })).toHaveCount(0);
|
||||
});
|
||||
@@ -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)
|
||||
|
||||
+105
-18
@@ -17,6 +17,7 @@ import { allowed } from './safety';
|
||||
import { CatalogCheckNotice } from './catalog-check';
|
||||
import { devicePlatform } from './script-catalog';
|
||||
import { DeviceInventoryPanels, systemDetails } from './device-inventory';
|
||||
import { OPNsenseDetails } from './opnsense';
|
||||
import { DeviceCustomerTransfer } from './device-customer-transfer';
|
||||
import { HypervisorAssignment, HypervisorLabel } from './device-hypervisor';
|
||||
import { ErrorBox, errorText, useResource } from './resource';
|
||||
@@ -29,6 +30,7 @@ import { SyncAlignmentReview } from './sync-alignment-review';
|
||||
import { SystemNonstartReview } from './system-nonstart-review';
|
||||
|
||||
const unknown = 'Nicht gemeldet';
|
||||
const noPurpose = 'Nicht hinterlegt';
|
||||
const number = new Intl.NumberFormat('de-DE', { maximumFractionDigits: 1 });
|
||||
const date = new Intl.DateTimeFormat('de-DE', { dateStyle: 'medium', timeStyle: 'short' });
|
||||
function label(value: unknown) {
|
||||
@@ -88,24 +90,35 @@ function Presence({ d }: { d: RecordValue }) {
|
||||
</span>
|
||||
);
|
||||
}
|
||||
function Capacity({ free, total }: { free: unknown; total: unknown }) {
|
||||
function Capacity({ free, total, showBreakdown = false }: {
|
||||
free: unknown;
|
||||
total: unknown;
|
||||
showBreakdown?: boolean;
|
||||
}) {
|
||||
const freePercent = typeof free === 'number' && typeof total === 'number' &&
|
||||
Number.isFinite(total) && free >= 0 && free <= total && total > 0
|
||||
? (free / total) * 100
|
||||
: null;
|
||||
const displayFree = freePercent === null ? null : Math.round(freePercent * 10) / 10;
|
||||
const freeLabel = displayFree === null ? '' : `${number.format(displayFree)} % frei`;
|
||||
const breakdown = displayFree === null ? '' : `${freeLabel}, ${number.format(100 - displayFree)} % belegt`;
|
||||
return (
|
||||
<>
|
||||
<span>{capacity(free, total)}</span>
|
||||
{typeof free === 'number' &&
|
||||
typeof total === 'number' &&
|
||||
Number.isFinite(total) &&
|
||||
free >= 0 &&
|
||||
free <= total &&
|
||||
total > 0 && (
|
||||
<span
|
||||
className="capacity-track"
|
||||
title={`${number.format((100 * free) / total)} % frei`}
|
||||
aria-hidden="true"
|
||||
>
|
||||
<i style={{ width: `${(100 * (total - free)) / total}%` }} />
|
||||
{freePercent !== null && (showBreakdown ? (
|
||||
<>
|
||||
<span className="capacity-track capacity-breakdown" role="img"
|
||||
aria-label={breakdown} title={breakdown}>
|
||||
<span className="capacity-free" style={{ width: `${freePercent}%` }} />
|
||||
<span className="capacity-used" />
|
||||
</span>
|
||||
)}
|
||||
<small className="capacity-caption" aria-hidden="true">{freeLabel}</small>
|
||||
</>
|
||||
) : (
|
||||
<span className="capacity-track" title={freeLabel} aria-hidden="true">
|
||||
<i style={{ width: `${100 - freePercent}%` }} />
|
||||
</span>
|
||||
))}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -127,6 +140,64 @@ function devicePath(customer: string, asset: string) {
|
||||
return tenantPath(customer, '/devices/' + encodeURIComponent(asset));
|
||||
}
|
||||
|
||||
function DevicePurpose({ value, path, customer, editor, success, onSaved }: {
|
||||
value: string;
|
||||
path: string;
|
||||
customer: string;
|
||||
editor: Session | null;
|
||||
success: boolean;
|
||||
onSaved: () => void;
|
||||
}) {
|
||||
const [draft, setDraft] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const tooLong = Array.from(draft?.trim() ?? '').length > 500;
|
||||
const close = () => { setDraft(null); setError(null); };
|
||||
return (
|
||||
<section className="device-purpose">
|
||||
<h2>Zweck</h2>
|
||||
{editor && draft !== null ? (
|
||||
<form onSubmit={async (e) => {
|
||||
e.preventDefault();
|
||||
if (busy || tooLong) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
await request(path + '/purpose', {
|
||||
method: 'PATCH', tenantId: customer, csrf: editor.csrf_token, body: { purpose: draft }
|
||||
});
|
||||
close();
|
||||
onSaved();
|
||||
} catch (e) {
|
||||
setError(errorText(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}}>
|
||||
<label>
|
||||
Zweck des Geräts
|
||||
<textarea autoFocus rows={3} value={draft} disabled={busy}
|
||||
aria-describedby="device-purpose-help" aria-invalid={tooLong}
|
||||
onChange={(e) => setDraft(e.target.value)} />
|
||||
</label>
|
||||
<p id="device-purpose-help" className={tooLong ? 'error' : 'muted'}>
|
||||
{tooLong ? 'Der Zweck ist zu lang. ' : ''}Maximal 500 Zeichen. Zum Entfernen das Feld leeren.
|
||||
</p>
|
||||
<ErrorBox error={error} />
|
||||
<button type="submit" disabled={busy || tooLong}>{busy ? 'Speichert …' : 'Zweck speichern'}</button>{' '}
|
||||
<button type="button" className="quiet" disabled={busy} onClick={close}>Abbrechen</button>
|
||||
</form>
|
||||
) : (
|
||||
<>
|
||||
<p className={'purpose-text' + (value ? '' : ' muted')}>{value || noPurpose}</p>
|
||||
{editor && <button className="quiet" onClick={() => setDraft(value)}>Zweck bearbeiten</button>}
|
||||
{success && <p role="status">Zweck gespeichert.</p>}
|
||||
</>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
function reportedScriptProfiles(device: RecordValue) {
|
||||
const os = devicePlatform(device);
|
||||
if (!os || !Array.isArray(device.capabilities)) return 'Nicht gemeldet';
|
||||
@@ -176,6 +247,7 @@ function DeviceDetail({
|
||||
[error, setError] = useState<string | null>(null);
|
||||
const [confirmDelete, setConfirmDelete] = useState(false);
|
||||
const [managing, setManaging] = useState(false);
|
||||
const [purposeSaved, setPurposeSaved] = useState(false);
|
||||
const d = detail.data;
|
||||
const scoped = membership.data;
|
||||
const volumes = Array.isArray(d?.volumes) ? d.volumes.map((v) => record(v)) : [];
|
||||
@@ -221,6 +293,15 @@ function DeviceDetail({
|
||||
))}
|
||||
</dl>
|
||||
</section>
|
||||
<DevicePurpose
|
||||
value={typeof d.purpose === 'string' ? d.purpose : ''}
|
||||
path={path}
|
||||
customer={customer}
|
||||
editor={scoped && allowed(scoped, 'devices:revoke') && allowed(scoped, 'inventory:read') ? scoped : null}
|
||||
success={purposeSaved}
|
||||
onSaved={() => { setPurposeSaved(true); detail.reload(); }}
|
||||
/>
|
||||
<OPNsenseDetails device={d} />
|
||||
<DeviceInventoryPanels device={d} />
|
||||
{volumes.length > 0 && (
|
||||
<section>
|
||||
@@ -557,9 +638,9 @@ export function Devices({
|
||||
<span className="sr-only">Ziel auswählen</span>
|
||||
</th>
|
||||
)}
|
||||
{['Hostname', 'Type', 'OS', 'Status', 'Speicher frei / gesamt', 'RAM frei / gesamt', 'CPU', 'Kunde'].map(
|
||||
{['Hostname', 'Type', 'Zweck', 'OS', 'Status', 'Speicher frei / gesamt', 'RAM frei / gesamt', 'CPU', 'Kunde'].map(
|
||||
(h) => (
|
||||
<th scope="col" key={h}>
|
||||
<th scope="col" key={h} className={h === 'Zweck' ? 'purpose-col' : undefined}>
|
||||
{h}
|
||||
</th>
|
||||
)
|
||||
@@ -571,7 +652,8 @@ export function Devices({
|
||||
const asset = objectId(d, 'asset'),
|
||||
agent = objectId(d, 'agent'),
|
||||
customer = objectId(d, 'tenant'),
|
||||
model = cpu(d);
|
||||
model = cpu(d),
|
||||
purpose = typeof d.purpose === 'string' ? d.purpose : '';
|
||||
const show = () => {
|
||||
setDeletionNotice('');
|
||||
window.location.hash = ['geraete', customer, asset, agent]
|
||||
@@ -608,6 +690,11 @@ export function Devices({
|
||||
<HypervisorLabel device={d} />
|
||||
</td>
|
||||
<td data-label="Type">{label(d.device_type)}</td>
|
||||
<td data-label="Zweck" className="purpose-col">
|
||||
<span className={'purpose-text' + (purpose ? '' : ' muted')} title={purpose || undefined}>
|
||||
{purpose || noPurpose}
|
||||
</span>
|
||||
</td>
|
||||
<td data-label="OS">{label(d.os)}</td>
|
||||
<td data-label="Status" className="device-presence">
|
||||
<Presence d={d} />
|
||||
@@ -619,7 +706,7 @@ export function Devices({
|
||||
<Capacity free={d.disk_free_bytes} total={d.disk_total_bytes} />
|
||||
</td>
|
||||
<td data-label="RAM frei / gesamt" className="capacity-cell" title={measuredAt(d.ram_observed_at)}>
|
||||
<Capacity free={d.ram_available_bytes} total={d.ram_total_bytes} />
|
||||
<Capacity free={d.ram_available_bytes} total={d.ram_total_bytes} showBreakdown />
|
||||
</td>
|
||||
<td data-label="CPU">
|
||||
{model.primary}
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
@@ -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>
|
||||
</>;
|
||||
}
|
||||
+10
-1
@@ -97,12 +97,20 @@ section.fleet { padding:0; overflow:hidden; }
|
||||
.vm-hypervisor { display:flex; align-items:baseline; gap:6px; margin-top:5px; color:var(--subtle); font-size:.78rem; overflow-wrap:anywhere; }
|
||||
.vm-hypervisor .nav-icon { width:13px; height:13px; flex:none; align-self:flex-start; margin-top:2px; }
|
||||
.fleet-table .target-col { width:35px; }
|
||||
.fleet-table th.purpose-col { width:17%; }
|
||||
.purpose-text { white-space:pre-wrap; overflow-wrap:anywhere; }
|
||||
.fleet-table .purpose-text { display:-webkit-box; -webkit-box-orient:vertical; -webkit-line-clamp:3; overflow:hidden; }
|
||||
.device-purpose textarea { font-family:inherit; }
|
||||
.fleet-table td[data-label="Type"] { overflow-wrap:anywhere; }
|
||||
.fleet-table input[type=checkbox] { margin:0; }
|
||||
.host-link { padding:0; background:none; color:var(--accent); text-align:left; font-weight:680; margin:0; overflow-wrap:anywhere; }
|
||||
.fleet-table .capacity-cell { font-variant-numeric:tabular-nums; }
|
||||
.capacity-track { display:block; width:min(90px,100%); border-radius:2px; background:#e1ebe6; height:4px; margin-top:6px; overflow:hidden; }
|
||||
.capacity-track i { display:block; height:100%; background:#659d84; }
|
||||
.capacity-breakdown { display:flex; height:6px; }
|
||||
.capacity-free { flex:none; background:#2e7657; }
|
||||
.capacity-used { flex:1; background:#b5c2be; }
|
||||
.capacity-caption { display:block; color:var(--subtle); }
|
||||
.presence { display:inline-flex; align-items:center; gap:5px; border-radius:20px; padding:2px 8px; background:#edf1ef; color:#4d5f59; font-size:.8rem; font-weight:650; }
|
||||
.presence i { width:6px; height:6px; border-radius:50%; background:currentColor; }
|
||||
.presence.online { color:#236343; background:#e6f4e9; }
|
||||
@@ -156,7 +164,8 @@ section.fleet { padding:0; overflow:hidden; }
|
||||
.fleet-table td,.fleet-table td:first-child,.fleet-table td:last-child {display:block;padding:0;border:0;font-size:.9rem;}
|
||||
.fleet-table td[data-label]::before {content:attr(data-label);display:block;color:var(--subtle);font-size:.73rem;}
|
||||
.fleet-table .hostname {grid-column:1/-1;padding-right:28px;} .host-link {font-size:1rem;}
|
||||
.fleet-table .target-col {position:absolute;right:14px;top:14px;width:auto;} .capacity-track {width:76px;}
|
||||
.fleet-table .purpose-col {grid-column:1/-1;}
|
||||
.fleet-table .target-col {position:absolute;right:14px;top:14px;width:auto;} .capacity-track {width:min(76px,100%);}
|
||||
.list-footer {border-top:1px solid var(--line);flex-wrap:wrap;} .facts {grid-template-columns:1fr 1fr;gap:12px;}
|
||||
}
|
||||
@media(max-width:450px) { .sidebar nav a {flex-direction:column;gap:2px;} .crumb span,.crumb i {display:none;} .login {margin:18px 12px;} .facts {grid-template-columns:1fr;} }
|
||||
|
||||
Reference in New Issue
Block a user