Skip to content

eBPF-based security platform

backend: add the node query a UI reads

John Fastabend49274bd003a2
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 7eb0381a..a184ce00 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -977,7 +977,10 @@ trust from ``untrusted`` to ``enrolled``, each status check and the
history it reads, ``node_vulns`` and its inputs, the
retention script and the views that keep boot baselines, the tree
ingest job on a single publication and on a walk over sealed segments,
-and the SBOM scan job on a store that holds an image in every state.
+the SBOM scan job on a store that holds an image in every state, and
+the query of :src:`tools/xtop/sql/nodes.sql`, with the names and types
+of its columns, over nodes healthy, unhealthy, stale and without a tree
+or an image (``test/xtop-nodes``).
Coverage by area
----------------
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rst
index af824aa9..87ecc8b2 100644
--- a/Documentation/contribute/testing/testing.rst
+++ b/Documentation/contribute/testing/testing.rst
@@ -1181,7 +1181,8 @@ comparing what it prints with ``test/<name>.expected``:
``tools/backend/test/run.sh <name>...`` runs only the tests named. The
runner loads the schema as an installation does,
``sql/apply-retention.sh`` included. A test that needs other settings
-in place when that script runs puts them in ``test/<name>.setup.sql``.
+in place when that script runs puts them in ``test/<name>.setup.sql``,
+and so does a test whose script needs rows to read.
A test that has a ``test/<name>.sh`` runs that script before its SQL,
with ``CLICKHOUSE_CLIENT`` set to the client of the test's server, and
diff --git a/tools/backend/test/run.sh b/tools/backend/test/run.sh
index 233a9d22..2135e39c 100755
--- a/tools/backend/test/run.sh
+++ b/tools/backend/test/run.sh
@@ -11,7 +11,8 @@
# test/<name>.expected. Every test starts from an empty schema, loaded
# from sql/, as an installation loads it: the files in order, then
# sql/apply-retention.sh. A test of what that script applies puts the
-# settings it wants in test/<name>.setup.sql, which runs between the two.
+# settings it wants in test/<name>.setup.sql, which runs between the two;
+# a test whose script needs rows to read puts them there too.
# A test that has a test/<name>.sh runs it next, with CLICKHOUSE_CLIENT
# naming the client of the test's server, as the jobs under jobs/ read
# it; what the script prints comes before the output of the SQL in
diff --git a/tools/backend/test/xtop-nodes.expected b/tools/backend/test/xtop-nodes.expected
new file mode 100644
index 00000000..87496bd9
--- /dev/null
+++ b/tools/backend/test/xtop-nodes.expected
@@ -0,0 +1,8 @@
+node_id ak_name host_name status trust failed_check generation last_seq head_seq loaded kernel_release version_id image_id image_version shields_applied shields_pending shields_idle exposed unknown log seen updated
+String String String String String UInt8 Nullable(UInt64) Nullable(UInt64) Nullable(UInt64) UInt64 LowCardinality(String) String Nullable(String) Nullable(String) UInt64 UInt64 UInt64 UInt64 UInt64 Array(String) UInt8 UInt8
+node-a 000b23602ab98bfb edge-01 healthy untrusted 0 3 3 3 1 7.4.2 d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f fleet-os 2026.11.1 1 0 0 1 0 ['1 signed: pass, untrusted, 0 rejected in the last hour','2 fresh: pass, last record N s ago','3 reported: pass, verdict ok, cond 0x1555, flags 0, tamper +0, build d6d918ad','4 chain: pass, root 15452248','5 head: pass, seq 3 of 3, generation 3','6 set: pass, 1 loaded','7 peers: pass, 2 in group'] 1 1
+node-b 000b9d72e08380a1 core-01 healthy untrusted 0 3 3 3 0 7.4.2 d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f fleet-os 2026.11.1 0 0 1 2 0 ['1 signed: pass, untrusted, 0 rejected in the last hour','2 fresh: pass, last record N s ago','3 reported: pass, verdict ok, cond 0x1555, flags 0, tamper +0, build d6d918ad','4 chain: pass, root 00000000','5 head: pass, seq 3 of 3, generation 3','6 set: pass, 0 loaded','7 peers: pass, 2 in group'] 1 1
+node-c 000b0c lab-01 healthy untrusted 0 \N \N \N 0 7.5.1 d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f \N \N 0 0 0 0 1 ['1 signed: pass, untrusted, 0 rejected in the last hour','2 fresh: pass, last record N s ago','3 reported: pass, verdict ok, cond 0x1555, flags 0, tamper +0, build d6d918ad','4 chain: no tree','5 head: no tree','6 set: no tree','7 peers: no tree'] 1 1
+node-d 000b0d edge-02 unhealthy untrusted 3 3 3 3 1 7.4.2 d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f fleet-os 2026.11.1 1 0 0 1 0 ['1 signed: pass, untrusted, 0 rejected in the last hour','2 fresh: pass, last record N s ago','3 reported: FAIL, verdict degraded, cond 0x1555, flags 0, tamper +0, build d6d918ad','4 chain: pass, root 15452248','5 head: pass, seq 3 of 3, generation 3','6 set: pass, 1 loaded','7 peers: pass, 2 in group'] 1 1
+node-e 000b0e core-02 stale untrusted 2 3 3 3 0 7.4.2 d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f fleet-os 2026.11.1 0 0 1 2 0 ['1 signed: pass, untrusted, 0 rejected in the last hour','2 fresh: FAIL, last record N s ago','3 reported: pass, verdict ok, cond 0x1555, flags 0, tamper +0, build d6d918ad','4 chain: pass, root 00000000','5 head: pass, seq 3 of 3, generation 3','6 set: pass, 0 loaded','7 peers: pass, 2 in group'] 1 1
+nodes 5
diff --git a/tools/backend/test/xtop-nodes.setup.sql b/tools/backend/test/xtop-nodes.setup.sql
new file mode 100644
index 00000000..003e86c9
--- /dev/null
+++ b/tools/backend/test/xtop-nodes.setup.sql
@@ -0,0 +1,72 @@
+-- SPDX-License-Identifier: BSD-2-Clause
+-- Copyright (c) 2026 Authors of shirudo
+--
+-- The nodes of vulns-fixtures.sql, node A shielded, node B left out by
+-- the selector and node C with no image, and two more for the states the
+-- nodes view of tools/xtop tells apart: node D sends node A's record with
+-- a verdict that is not ok, so check 3 fails it, and node E sent node
+-- B's record an hour ago and turns stale.
+
+-- The publication of testing/fixtures/keepalive/tree-1.1.0.json, as the
+-- ingest job loads it: generation 3 with its three operations.
+INSERT INTO shirudo.tree_pubs (tree_id, generation, manifest_digest, parent_manifest_digest, head_seq, published_at) VALUES
+('fleet-base', 3, '1f327364233e3f2dcebcd13c0af8d84f886201531b135294bd7356a2f7ed680b', '4ee49208ad87695d862c8cc976d1c910170a3de611cfcee2ce181efd8e0a2942', 3, now64(3) - INTERVAL 10 MINUTE);
+INSERT INTO shirudo.tree_ops (tree_id, seq, op, id, selector, repository, manifest_digest, layer_digest, payload_sha256, operation_digest) VALUES
+('fleet-base', 1, 'add', 'xfs-fix', '{"labels":{"role":["edge"]}}', 'oci://registry.example.com/shirudo/fleet-base', '98f2c22f02ed3106f44cefae608868f529a38fada82d52a263f230cbda076144', '9ecc7c28f423abab4de35c3dbcb2af66dd44df59c0dcd52f465f7bc6338772e6', '59f8ad56dfc63ce2b91d4b2edd89218694bee7287eaafe06a8624869210cf885', '13b248d4a85d91150717e3e96dc9c2d7a8a43633e3b58f8f6168d408aed26c43'),
+('fleet-base', 2, 'delete', 'xfs-fix', '{"labels":{"role":["edge"]}}', 'oci://registry.example.com/shirudo/fleet-base', '4d4f3bf9f68bf0dcbd06bffe4aa9eaf83444430e60936b9b2d11ec6229130a1b', 'e80b73d1bf9f8b4d50e5037f317e68bc4c8b15532187b1a3d73b5152a1596c55', 'f6a8fbb53b29691f47d8e9059a3796ecce2a48e693ec444da690209edef000f1', '5ce215313f29f87bfb87aa0cc65c237bf21bdc7702606e9cc29be187f950074b'),
+('fleet-base', 3, 'add', 'xfs-fix', '{"labels":{"role":["edge"]}}', 'oci://registry.example.com/shirudo/fleet-base', 'b25c835b353ba101d51c4473d602764c2f8746a6e60342bcf49d510a868cb62a', '653919e8cd88d08fc0d7d3bee8ac27323fadac7ec0a8b8eb6e9dc5a68c36bc97', '82db4a710ddb53f6e3e245e01001242cfb2d628e542e9ffefbb343b01c48a7bf', 'bd98b27dd5cfc9deeb0e4f49a7d4bd0f991762ec784be9ea838a59966d9dd0dc');
+INSERT INTO shirudo.expected_builds (version_id, note) VALUES
+('d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f', 'shirudo 2026.11');
+
+-- The bodies of testing/fixtures/keepalive/, byte for byte.
+CREATE TEMPORARY TABLE fixture (name String, body String);
+INSERT INTO fixture VALUES
+('node-a-1.1.0', '{"schema":"shirudo.keepalive/1","boot_id":"168d20a1-5011-4e1b-9924-d8b73c85aa4e","version_id":"d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f","config_root":"1d8465c5b17838303fee92b3905420d294d2c22c576a0bdef0f69f190ed22807","kernel_release":"7.4.2","image_id":"fleet-os","image_version":"2026.11.1","image_root":"a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c","verdict":"ok","cond":5461,"flags":0,"tamper":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"tree_id":"fleet-base","generation":3,"base_seq":0,"last_seq":3,"base_root":"0000000000000000000000000000000000000000000000000000000000000000","projection_root":"154522480ff8884daa95589e80f26f20248bbf9c192dfbb3cee04e03b4f136a1","mask":"VA==","set_sha256":"74e5bd2c665109f6d8f926af5476c266ad70ab07293ff13b1a13d0b89aa5c1bf","count":1,"not_ok":0}'),
+('node-b-1.1.0', '{"schema":"shirudo.keepalive/1","boot_id":"9a6f14ea-0cc2-45e1-8e23-7b887120cf54","version_id":"d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f","config_root":"ff2cbeff5d0e9c41a7b36f28d14ce09b73a5182e6fc40d97b5e2a1683c0f7ff8","kernel_release":"7.4.2","image_id":"fleet-os","image_version":"2026.11.1","image_root":null,"verdict":"ok","cond":5461,"flags":0,"tamper":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"tree_id":"fleet-base","generation":3,"base_seq":0,"last_seq":3,"base_root":"0000000000000000000000000000000000000000000000000000000000000000","projection_root":"0000000000000000000000000000000000000000000000000000000000000000","mask":"qA==","set_sha256":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855","count":0,"not_ok":0}');
+
+
+-- What the tree ingest job takes from the fixes of operations 1 and 3.
+INSERT INTO shirudo.upatch_fixes (tree_id, seq, id, advisory, source) VALUES
+('fleet-base', 1, 'xfs-fix', 'EXAMPLE-0101', 'fixes'),
+('fleet-base', 3, 'xfs-fix', 'EXAMPLE-0101', 'fixes');
+
+-- What the sbom-scan job writes for the image and its one finding, in
+-- place of a scan, and the kernel advisory.
+CREATE TEMPORARY TABLE scan AS SELECT now() - INTERVAL 1 HOUR AS at;
+INSERT INTO shirudo.sbom_images (image_id, image_version, image_root, sbom_format, sbom_sha256, state, built_at, db_version, scanned_at)
+SELECT 'fleet-os', '2026.11.1', 'a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c', 'cyclonedx-json',
+ '5b0e2c8a4f6d1e3b7a9c0d2e4f6a8b1c3d5e7f9a0b2c4d6e8f1a3b5c7d9e91d4', 'ok', '2026-11-02 08:00:00', '2', at
+FROM scan;
+INSERT INTO shirudo.sbom_vulns (sbom_sha256, vuln_id, package, installed, fixed, severity, scanned_at)
+SELECT '5b0e2c8a4f6d1e3b7a9c0d2e4f6a8b1c3d5e7f9a0b2c4d6e8f1a3b5c7d9e91d4', 'EXAMPLE-0202', 'openssl', '3.5.1-1', '3.5.1-2', 'HIGH', at
+FROM scan;
+INSERT INTO shirudo.kernel_vulns (vuln_id, introduced, fixed, severity) VALUES
+('EXAMPLE-0101', [7, 0], [7, 4, 5], 'CRITICAL');
+
+INSERT INTO shirudo.checked (host_name, body, ok, ak_name, ak_public, reset_count, clock)
+SELECT 'edge-01', body, 1, '000b23602ab98bfb', 'QUtB', 7, 86523114 FROM fixture WHERE name = 'node-a-1.1.0';
+INSERT INTO shirudo.checked (host_name, body, ok, ak_name, ak_public, reset_count, clock)
+SELECT 'core-01', body, 1, '000b9d72e08380a1', 'QUtC', 4, 91200408 FROM fixture WHERE name = 'node-b-1.1.0';
+INSERT INTO shirudo.checked (host_name, body, ok, ak_name, ak_public, reset_count, clock)
+SELECT 'lab-01', replaceOne(replaceOne(replaceOne(body, '"tree_id":"fleet-base","generation":3,"base_seq":0,"last_seq":3,"base_root":"0000000000000000000000000000000000000000000000000000000000000000","projection_root":"0000000000000000000000000000000000000000000000000000000000000000","mask":"qA=="', '"tree_id":null,"generation":null,"base_seq":null,"last_seq":null,"base_root":null,"projection_root":null,"mask":null'),
+ '"kernel_release":"7.4.2","image_id":"fleet-os","image_version":"2026.11.1"', '"kernel_release":"7.5.1","image_id":null,"image_version":null'),
+ '9a6f14ea-0cc2-45e1-8e23-7b887120cf54', '5c1d7e2f-3a4b-4c5d-8e6f-7a8b9c0d1e2f'),
+ 1, '000b0c', 'QUtD', 1, 1000 FROM fixture WHERE name = 'node-b-1.1.0';
+
+INSERT INTO shirudo.checked (host_name, body, ok, ak_name, ak_public, reset_count, clock)
+SELECT 'edge-02', replaceOne(body, '"verdict":"ok"', '"verdict":"degraded"'), 1, '000b0d', 'QUtE', 2, 2000
+FROM fixture WHERE name = 'node-a-1.1.0';
+INSERT INTO shirudo.checked (received, host_name, body, ok, ak_name, ak_public, reset_count, clock)
+SELECT now64(3) - INTERVAL 1 HOUR, 'core-02', body, 1, '000b0e', 'QUtF', 3, 3000
+FROM fixture WHERE name = 'node-b-1.1.0';
+
+-- The operator's labels, as the page has them.
+INSERT INTO shirudo.nodes (ak_name, node_id)
+SELECT record.1, argMinState(record.2, toDateTime64(0, 3))
+FROM (SELECT arrayJoin([('000b23602ab98bfb', 'node-a'), ('000b9d72e08380a1', 'node-b'), ('000b0c', 'node-c'), ('000b0d', 'node-d'), ('000b0e', 'node-e')]) AS record)
+GROUP BY record.1;
+
+SYSTEM REFRESH VIEW shirudo.node_status_mv;
+SYSTEM WAIT VIEW shirudo.node_status_mv;
+SYSTEM REFRESH VIEW shirudo.node_vulns_mv;
+SYSTEM WAIT VIEW shirudo.node_vulns_mv;
diff --git a/tools/backend/test/xtop-nodes.sh b/tools/backend/test/xtop-nodes.sh
new file mode 100644
index 00000000..3af561cf
--- /dev/null
+++ b/tools/backend/test/xtop-nodes.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# The query the nodes view of tools/xtop sends, over the nodes of
+# xtop-nodes.setup.sql, with the names and types of its columns, which the
+# view reads by name. The times and the age in the line of check 2 change
+# from run to run and are left out.
+
+query=$(dirname "$0")/../../xtop/sql/nodes.sql
+
+{
+ printf '%s\n' "SELECT * EXCEPT (log, last_seen_ms, updated_ms),"
+ printf '%s\n' " arrayMap(line -> replaceRegexpOne(line, 'last record -?[0-9]+ s ago', 'last record N s ago'), log) AS log,"
+ printf '%s\n' " last_seen_ms > 0 AS seen, updated_ms > 0 AS updated"
+ printf '%s\n' "FROM ("
+ sed '/^--/d' "$query"
+ printf '%s\n' ") ORDER BY node_id FORMAT TSVWithNamesAndTypes"
+} | $CLICKHOUSE_CLIENT
diff --git a/tools/backend/test/xtop-nodes.sql b/tools/backend/test/xtop-nodes.sql
new file mode 100644
index 00000000..207788fc
--- /dev/null
+++ b/tools/backend/test/xtop-nodes.sql
@@ -0,0 +1,7 @@
+-- SPDX-License-Identifier: BSD-2-Clause
+-- Copyright (c) 2026 Authors of shirudo
+--
+-- Every node of node_status is a row of the nodes view; xtop-nodes.sh
+-- prints the rows.
+
+SELECT 'nodes', count() FROM shirudo.node_status;
diff --git a/tools/xtop/sql/nodes.sql b/tools/xtop/sql/nodes.sql
new file mode 100644
index 00000000..b4e866f7
--- /dev/null
+++ b/tools/xtop/sql/nodes.sql
@@ -0,0 +1,47 @@
+-- SPDX-License-Identifier: BSD-2-Clause
+-- Copyright (c) 2026 Authors of shirudo
+--
+-- One row per node for the nodes view of tools/xtop: the outcome of the
+-- checks from node_status, what the newest record says it runs from
+-- latest, and per node the counts of node_vulns. Times are epoch
+-- milliseconds, so that no client reads them in the server's time zone.
+
+WITH vulns AS
+(
+ SELECT
+ ak_name,
+ uniqIf(upatch_id, exposure = 'shielded') AS shields_applied,
+ uniqIf(upatch_id, exposure = 'shield_pending') AS shields_pending,
+ uniqIf(upatch_id, exposure IN ('shield_module_absent', 'shield_excludes_node', 'shield_withdrawn')) AS shields_idle,
+ uniqIf(vuln_id, exposure IN ('exposed_no_shield', 'shield_module_absent', 'shield_excludes_node', 'shield_withdrawn')) AS exposed,
+ countIf(exposure = 'unknown') AS unknown
+ FROM shirudo.node_vulns
+ GROUP BY ak_name
+)
+SELECT
+ s.node_id AS node_id,
+ s.ak_name AS ak_name,
+ s.host_name AS host_name,
+ toString(s.status) AS status,
+ toString(s.trust) AS trust,
+ s.failed_check AS failed_check,
+ s.log AS log,
+ toUnixTimestamp64Milli(s.last_seen) AS last_seen_ms,
+ toInt64(toUnixTimestamp(s.updated_at)) * 1000 AS updated_ms,
+ s.generation AS generation,
+ s.last_seq AS last_seq,
+ s.head_seq AS head_seq,
+ s.loaded AS loaded,
+ l.kernel_release AS kernel_release,
+ l.version_id AS version_id,
+ l.image_id AS image_id,
+ l.image_version AS image_version,
+ v.shields_applied AS shields_applied,
+ v.shields_pending AS shields_pending,
+ v.shields_idle AS shields_idle,
+ v.exposed AS exposed,
+ v.unknown AS unknown
+FROM shirudo.node_status AS s
+LEFT JOIN shirudo.latest AS l ON l.ak_name = s.ak_name
+LEFT JOIN vulns AS v ON v.ak_name = s.ak_name
+ORDER BY node_id, ak_name