Link virtual OPNsense firewalls to their Proxmox host automatically
The MAC matching only read linux.base and windows.base snapshots and ran only after those. OPNsense sends a flat opnsense.base snapshot (hardware.device_type, interfaces.entries); it is now normalised into the same scopes, triggers the reconciliation and appears in the host's guest list. Manual decisions still win. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -724,6 +724,7 @@ mod tests {
|
||||
let (tenant, other) = (Uuid::new_v4(), Uuid::new_v4());
|
||||
let (host, vm, manual_vm, foreign_vm, twin_a, twin_b) =
|
||||
(Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4());
|
||||
let (fw, foreign_fw, physical_fw) = (Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4());
|
||||
for customer in [tenant, other] {
|
||||
sqlx::query("INSERT INTO tenants(tenant_id,name) VALUES($1,'Proxmox link test')")
|
||||
.bind(customer).execute(&admin).await.unwrap();
|
||||
@@ -740,15 +741,22 @@ mod tests {
|
||||
}
|
||||
json!({"hostname":"h","os":"Debian","root_available_bytes":1,"root_total_bytes":2,"observations":scopes})
|
||||
};
|
||||
// OPNsense snapshots are flat; MACs come from `interfaces.entries`.
|
||||
let firewall = |kind: &str, macs: &[&str]| json!({"hostname":"fw","hardware":{"device_type":kind},
|
||||
"interfaces":{"status":"ok","entries":macs.iter().enumerate().map(|(i, m)|
|
||||
json!({"name":format!("vtnet{i}"),"mac":m})).chain([json!({"name":"lo0","mac":null})])
|
||||
.collect::<Vec<_>>()}});
|
||||
let insert = |customer: Uuid, id: Uuid, data: Value, sequence: i64| {
|
||||
let admin = admin.clone();
|
||||
async move {
|
||||
sqlx::query("INSERT INTO observations(tenant_id,agent_id,message_id,boot_id,sequence,observed_at,source_id,quality,completeness,snapshot_id,data,payload_sha256) VALUES($1,$2,$3,$4,$5,now(),'linux.base','valid','partial',$6,$7,$8)")
|
||||
let source = if data.get("interfaces").is_some() { "opnsense.base" } else { "linux.base" };
|
||||
sqlx::query("INSERT INTO observations(tenant_id,agent_id,message_id,boot_id,sequence,observed_at,source_id,quality,completeness,snapshot_id,data,payload_sha256) VALUES($1,$2,$3,$4,$5,now(),$9,'valid','partial',$6,$7,$8)")
|
||||
.bind(customer).bind(id).bind(Uuid::new_v4()).bind(Uuid::new_v4()).bind(sequence)
|
||||
.bind(Uuid::new_v4()).bind(data).bind("0".repeat(64)).execute(&admin).await.unwrap();
|
||||
.bind(Uuid::new_v4()).bind(data).bind("0".repeat(64)).bind(source).execute(&admin).await.unwrap();
|
||||
}
|
||||
};
|
||||
let all = ["bc:24:11:00:00:01", "bc:24:11:00:00:02", "bc:24:11:00:00:03", "bc:24:11:00:00:04"];
|
||||
let all = ["bc:24:11:00:00:01", "bc:24:11:00:00:02", "bc:24:11:00:00:03", "bc:24:11:00:00:04",
|
||||
"bc:24:11:00:00:05", "bc:24:11:00:00:06", "bc:24:11:00:00:07"];
|
||||
for (customer, id, data) in [
|
||||
(tenant, host, device("hypervisor", "52:54:00:00:00:99", Some(guests(&all)))),
|
||||
(tenant, vm, device("virtual", "BC:24:11:00:00:01", None)),
|
||||
@@ -756,6 +764,9 @@ mod tests {
|
||||
(other, foreign_vm, device("virtual", "bc:24:11:00:00:03", None)),
|
||||
(tenant, twin_a, device("virtual", "bc:24:11:00:00:04", None)),
|
||||
(tenant, twin_b, device("virtual", "bc:24:11:00:00:04", None)),
|
||||
(tenant, fw, firewall("virtual", &["BC:24:11:00:00:05", "02:00:00:00:00:01"])),
|
||||
(other, foreign_fw, firewall("virtual", &["bc:24:11:00:00:06"])),
|
||||
(tenant, physical_fw, firewall("physical", &["bc:24:11:00:00:07"])),
|
||||
] {
|
||||
sqlx::query("INSERT INTO assets(tenant_id,asset_id) VALUES($1,$2)")
|
||||
.bind(customer).bind(id).execute(&admin).await.unwrap();
|
||||
@@ -782,9 +793,12 @@ mod tests {
|
||||
assert_eq!(link(manual_vm).await, (None, Some("manual".into())));
|
||||
assert_eq!(link(foreign_vm).await, (None, None));
|
||||
assert_eq!(link(twin_a).await, (None, None), "duplicate MACs are ambiguous");
|
||||
assert_eq!(link(fw).await, (Some(host), Some("proxmox".into())), "virtual OPNsense guest");
|
||||
assert_eq!(link(foreign_fw).await, (None, None));
|
||||
assert_eq!(link(physical_fw).await, (None, None), "only virtual devices are guests");
|
||||
let audits: i64 = sqlx::query_scalar("SELECT count(*) FROM audit_events WHERE tenant_id=$1 AND event_type='device_hypervisor_detected'")
|
||||
.bind(tenant).fetch_one(&admin).await.unwrap();
|
||||
assert_eq!(audits, 1);
|
||||
assert_eq!(audits, 2);
|
||||
|
||||
// Idempotent: a second pass changes nothing.
|
||||
let mut tx = rmm_persistence::tenant_tx(&app, tenant).await.unwrap();
|
||||
@@ -792,7 +806,7 @@ mod tests {
|
||||
tx.commit().await.unwrap();
|
||||
let audits: i64 = sqlx::query_scalar("SELECT count(*) FROM audit_events WHERE tenant_id=$1")
|
||||
.bind(tenant).fetch_one(&admin).await.unwrap();
|
||||
assert_eq!(audits, 1);
|
||||
assert_eq!(audits, 2);
|
||||
|
||||
// The guest left the host: the automatic link is released.
|
||||
insert(tenant, host, device("hypervisor", "52:54:00:00:00:99", Some(guests(&all[1..]))), 2).await;
|
||||
|
||||
Reference in New Issue
Block a user