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:
2026-11-21 13:05:36 +01:00
co-authored by Claude Opus 5.5
parent b6d7bf4a93
commit f3a471c1c5
5 changed files with 44 additions and 12 deletions
+15 -5
View File
@@ -16,11 +16,11 @@ WITH current_installation AS (
WHERE asset.tenant_id=$1 AND asset.archived_at IS NULL
),
latest AS (
SELECT c.*, inventory.data
SELECT c.*, inventory.source_id, inventory.data
FROM current_installation c
JOIN LATERAL (
SELECT data FROM observations
WHERE tenant_id=$1 AND agent_id=c.agent_id AND source_id IN ('linux.base','windows.base')
SELECT source_id, data FROM observations
WHERE tenant_id=$1 AND agent_id=c.agent_id AND source_id IN ('linux.base','windows.base','opnsense.base')
ORDER BY observed_at DESC, received_at DESC, sequence DESC
LIMIT 1
) inventory ON true
@@ -31,6 +31,15 @@ scopes AS (
jsonb_array_elements(CASE WHEN jsonb_typeof(l.data->'observations')='array'
THEN l.data->'observations' ELSE '[]'::jsonb END) o
WHERE o->>'quality'='valid'
UNION ALL
-- An OPNsense snapshot is flat: hardware and interfaces sit at the top level.
SELECT l.asset_id, jsonb_build_object('scope','opnsense.hardware','quality','valid','data',l.data->'hardware')
FROM latest l
WHERE l.source_id='opnsense.base' AND jsonb_typeof(l.data->'hardware')='object'
UNION ALL
SELECT l.asset_id, jsonb_build_object('scope','opnsense.interfaces','quality','valid','data',l.data->'interfaces')
FROM latest l
WHERE l.source_id='opnsense.base' AND l.data#>>'{interfaces,status}'='ok'
),
proxmox_hosts AS (
SELECT asset_id, o->'data' AS data FROM scopes WHERE o->>'scope'='linux.proxmox'
@@ -43,7 +52,8 @@ guest_macs AS (
),
virtual_devices AS (
SELECT asset_id FROM scopes
WHERE o->>'scope' IN ('linux.hardware','windows.hardware') AND o#>>'{data,device_type}'='virtual'
WHERE o->>'scope' IN ('linux.hardware','windows.hardware','opnsense.hardware')
AND o#>>'{data,device_type}'='virtual'
),
device_macs AS (
SELECT DISTINCT s.asset_id, lower(i->>'mac') AS mac
@@ -53,7 +63,7 @@ device_macs AS (
WHEN jsonb_typeof(s.o->'data')='array' THEN s.o->'data'
WHEN jsonb_typeof(s.o#>'{data,entries}')='array' THEN s.o#>'{data,entries}'
ELSE '[]'::jsonb END) i
WHERE s.o->>'scope' IN ('linux.interfaces','windows.interfaces')
WHERE s.o->>'scope' IN ('linux.interfaces','windows.interfaces','opnsense.interfaces')
AND i->>'mac' ~* '^[0-9a-f]{2}(:[0-9a-f]{2}){5}$'
AND lower(i->>'mac') <> '00:00:00:00:00:00'
),