eBPF-based security platform
backend: let a label set name a config image
John Fastabend019af362815a
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex 2dc435ad..3039f8d1 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -1207,8 +1207,8 @@ is given. ``test/sbom-put`` stores two of those SBOMs with``jobs/sbom-put`` into a ``file://`` store and through the same AWS stubinto an ``s3://`` one, runs ``--scan`` on the first, and checks what itrefuses. ``test/labels-put`` loads label sets with ``jobs/labels-put``-by ``node_id`` and by ``ak_name``, replaces and clears some, and checks-what it refuses. A script that exits with status 77 skips its test and says+by ``node_id``, by ``ak_name`` and by ``config_root``, replaces and+clears some, and checks what it refuses. A script that exits with status 77 skips its test and sayswhy. ``test/sbom-scan-trivy`` is skipped unless ``trivy`` is on the hostand ``SBOM_TRIVY_CACHE`` names a Trivy cache directory with a database,since its findings would change with every database CI could fetch.diff --git a/Documentation/operate/backend/labels-put.rst b/Documentation/operate/backend/labels-put.rstindex 74c57073..c9b1be3f 100644--- a/Documentation/operate/backend/labels-put.rst+++ b/Documentation/operate/backend/labels-put.rst@@ -23,14 +23,16 @@ matches: tree sync on the node reads its own ``node.labels``, and nothingthe job loads reaches a node. Load them from the same source as theconfig images, after each change, or the two drift apart.-1. Write the sets, one per node, each named by its ``node_id`` or by its- ``ak_name``:+1. Write the sets, each named by a node's ``node_id`` or ``ak_name``, or+ by the ``config_root`` of a config image, for every node that booted+ it:.. code-block:: json{"nodes": [+ {"config_root": "6d5ed552f448a1c07e3b9d24f5a86c1e0b7d93f2c4e5a6b7180c9d2e3f4a5b6c",+ "labels": {"role": "edge"}},{"node_id": "edge-01", "labels": {"role": "edge", "site": "fra"}},- {"node_id": "edge-02", "labels": {"role": "edge", "site": "ams"}},{"ak_name": "000b9d72e08380a1", "labels": {"role": "core"}}]}@@ -45,8 +47,8 @@ config images, after each change, or the two drift apart... code-block:: console$ jobs/labels-put labels.json+ labels-put: config_root 6d5ed552f448a1c07e3b9d24f5a86c1e0b7d93f2c4e5a6b7180c9d2e3f4a5b6c: 1 labellabels-put: node_id edge-01: 2 labels- labels-put: node_id edge-02: 2 labelslabels-put: ak_name 000b9d72e08380a1: 1 labellabels-put: loaded sets: 3@@ -61,11 +63,19 @@ not name keeps its labels. The file is loaded in one insert, whole ornot at all.A node gets the set put under its ``ak_name``, else the one put under its-``node_id``, else none, as the view ``shirudo.node_labels`` reads them-(:doc:`tables/status`). Name a node by its ``node_id``, the name-:doc:`maintain` gives it, so that the labels stay with it when it gets a-new key; name it by its ``ak_name`` to give one key labels of its own. A-name no node has yet keeps its set until one has it.+``node_id``, else the one put under the ``config_root`` its newest+keepalive reports, else none, as the view ``shirudo.node_labels`` reads+them (:doc:`tables/status`): the most specific name wins.++Name the sets by ``config_root`` where you can. A config image holds+``etc/shirudo/node.labels``, so the pipeline that builds the image can+load the same labels under its root, and they are then the ones the+node's tree sync matches for as long as the node runs that image. A node+that boots another image takes that image's set, or none. Name a node by+its ``node_id``, the name :doc:`maintain` gives it, to label it apart+from its image, and so that the labels stay with it when it gets a new+key; name it by its ``ak_name`` to give one key labels of its own. A name+no node has yet keeps its set until one has it.What it refuses---------------@@ -77,9 +87,10 @@ refuses the whole file, with status 1 and a message naming the set, when:- it is not UTF-8 JSON, or not an object whose one key is ``nodes``, anarray; or an object has a key twice;- a set is not an object with ``labels`` and exactly one of ``node_id``,- 1 to 255 characters with no control character, and ``ak_name``, 1 to- 255 lowercase hex digits; or has another key;-- two sets name the same ``node_id`` or ``ak_name``;+ 1 to 255 characters with no control character, ``ak_name``, 1 to 255+ lowercase hex digits, and ``config_root``, 64 lowercase hex digits; or+ has another key;+- two sets name the same ``node_id``, ``ak_name`` or ``config_root``;- ``labels`` is not an object of strings, or holds more than 64 labels;- a key is empty, has ``=``, starts with ``#``, or a key or value has ablank at either end, a control character, or more than 255 bytes.diff --git a/Documentation/operate/backend/tables/reference.rst b/Documentation/operate/backend/tables/reference.rstindex cafe80d4..c8366786 100644--- a/Documentation/operate/backend/tables/reference.rst+++ b/Documentation/operate/backend/tables/reference.rst@@ -216,16 +216,17 @@ shirudo.label_sets:reference: 10,000 rows ≈ 2 MB:bytes: 2e+06- The labels the operator gives a node out of band, one set per node_id- or ak_name. Not what the node's node.labels holds: tree sync matches- selectors against that file, and nothing here reaches the node.+ The labels the operator gives a node out of band, one set per node_id,+ ak_name or config_root. Not what the node's node.labels holds: tree+ sync matches selectors against that file, and nothing here reaches the+ node; a set by config_root is the one place they can agree.- Read with FINAL, or through node_labels: the newest put of a namereplaces the set.- An empty map clears the set; node_labels then falls back to theother name, or none.- :by: Whether node names a node_id or an ak_name.- :node: The node_id or ak_name.+ :by: Whether node names a node_id, an ak_name, or the config_root of the nodes that booted that config image.+ :node: The node_id, ak_name or config_root.:labels: The whole set, key to value.:put_at: When labels-put loaded it; the version ReplacingMergeTree keeps the newest of.diff --git a/Documentation/operate/backend/tables/status.rst b/Documentation/operate/backend/tables/status.rstindex 5d998939..64a5ffcb 100644--- a/Documentation/operate/backend/tables/status.rst+++ b/Documentation/operate/backend/tables/status.rst@@ -33,9 +33,10 @@ Views, functions and jobs``shirudo.node_labels``view.- label_sets FINAL joined to nodes: per key, the labels put under its- ak_name, else those put under its node_id, else none, and by, which of- the three it is. An empty set counts as none.+ label_sets FINAL joined to nodes and keepalive_latest: per key, the+ labels put under its ak_name, else those put under its node_id, else+ those put under the config_root of its newest record, else none, and+ by, which of the four it is. An empty set counts as none.``shirudo.history_conflicts``view.diff --git a/tools/backend/jobs/labels-put b/tools/backend/jobs/labels-putindex 6a43a3e7..aaab2fd2 100755--- a/tools/backend/jobs/labels-put+++ b/tools/backend/jobs/labels-put@@ -7,9 +7,10 @@## jobs/labels-put [--check] <sets.json>|-#-# The file names each node by node_id or by ak_name and gives its whole-# set of labels, which replaces the one stored under that name; an empty-# set clears it. Nodes the file does not name keep theirs. The file is+# The file names each node by node_id or by ak_name, or every node booted+# from a config image by its config_root, and gives the whole set of+# labels, which replaces the one stored under that name; an empty set+# clears it. Nodes the file does not name keep theirs. The file is# loaded whole or not at all. --check reads and checks it, and loads# nothing.#diff --git a/tools/backend/jobs/labels-rows.py b/tools/backend/jobs/labels-rows.pyindex 2bd943f1..8cfbf4d6 100644--- a/tools/backend/jobs/labels-rows.py+++ b/tools/backend/jobs/labels-rows.py@@ -8,7 +8,8 @@# labels-rows.py <sets.json> <rows.jsonl> <put_at>## Reads {"nodes": [{"node_id": <name>, "labels": {<key>: <value>, ...}},-# {"ak_name": <key>, "labels": {...}}, ...]} and writes one row of+# {"ak_name": <key>, "labels": {...}}, {"config_root": <hash>, "labels":+# {...}}, ...]} and writes one row of# shirudo.label_sets per set, in ClickHouse's JSONEachRow format, with# put_at, milliseconds since the epoch, on every row. Prints a line per# set. A label is one that etc/shirudo/node.labels could hold, so that a@@ -28,6 +29,12 @@ MAX_LABELS = 64MAX_TEXT = 255NODE_ID = re.compile(r"[^\x00-\x1f\x7f]{1,255}")AK_NAME = re.compile(r"[0-9a-f]{1,255}")+CONFIG_ROOT = re.compile(r"[0-9a-f]{64}")+NAMES = {+ "node_id": (NODE_ID, "1 to 255 characters with no control character"),+ "ak_name": (AK_NAME, "1 to 255 lowercase hex digits"),+ "config_root": (CONFIG_ROOT, "64 lowercase hex digits"),+}CONTROL = re.compile(r"[\x00-\x1f\x7f]")@@ -92,21 +99,20 @@ def node_of(n, entry):"""The set's (by, node) from its one name."""if type(entry) is not dict:raise Refused("set %d is not an object" % n)- extra = set(entry) - {"node_id", "ak_name", "labels"}+ extra = set(entry) - set(NAMES) - {"labels"}if extra:raise Refused('set %d has the key "%s", which is not node_id,'- ' ak_name or labels' % (n, printable(sorted(extra)[0])))- names = [k for k in ("node_id", "ak_name") if k in entry]+ ' ak_name, config_root or labels'+ % (n, printable(sorted(extra)[0])))+ names = [k for k in NAMES if k in entry]if len(names) != 1:- raise Refused("set %d names its node by neither or both of node_id"- " and ak_name" % n)+ raise Refused("set %d does not name its node by exactly one of"+ " node_id, ak_name and config_root" % n)by = names[0]node = entry[by]- pattern = NODE_ID if by == "node_id" else AK_NAME+ pattern, form = NAMES[by]if type(node) is not str or not pattern.fullmatch(node):- raise Refused("set %d: %s is not %s" % (- n, by, "1 to 255 characters with no control character"- if by == "node_id" else "1 to 255 lowercase hex digits"))+ raise Refused("set %d: %s is not %s" % (n, by, form))return by, nodediff --git a/tools/backend/sql/20-reference.sql b/tools/backend/sql/20-reference.sqlindex 3d0f356c..399493d8 100644--- a/tools/backend/sql/20-reference.sql+++ b/tools/backend/sql/20-reference.sql@@ -103,12 +103,13 @@ ORDER BY (tree_id, seq, advisory, source);-- The labels the operator gives a node, out of band: what jobs/labels-put-- loads, not what the node's own node.labels holds, which only its tree--- sync reads. One row per set, by node_id or by ak_name; a newer put of--- the same name replaces it, and an empty map clears it. Read it with--- FINAL, or through shirudo.node_labels.+-- sync reads. One row per set, by node_id, by ak_name, or by config_root+-- for every node whose newest record names that config image; a newer+-- put of the same name replaces it, and an empty map clears it. Read it+-- with FINAL, or through shirudo.node_labels.CREATE TABLE IF NOT EXISTS shirudo.label_sets(- by Enum8('node_id' = 1, 'ak_name' = 2),+ by Enum8('node_id' = 1, 'ak_name' = 2, 'config_root' = 3),node String,labels Map(String, String),put_at DateTime64(3) DEFAULT now64(3)diff --git a/tools/backend/sql/40-status.sql b/tools/backend/sql/40-status.sqlindex 16f974b1..d30d9594 100644--- a/tools/backend/sql/40-status.sql+++ b/tools/backend/sql/40-status.sql@@ -28,7 +28,8 @@ INNER JOIN) AS n ON n.ak_name = k.ak_name;-- The labels of every node: the set put under its ak_name, else the one--- put under its node_id, else none. by says which.+-- put under its node_id, else the one put under the config_root of its+-- newest record, else none. by says which.CREATE VIEW IF NOT EXISTS shirudo.node_labels ASWITH sets AS(@@ -37,16 +38,23 @@ WITH sets ASSELECTn.ak_name AS ak_name,n.node_id AS node_id,- if(a.node != '', a.labels, i.labels) AS labels,- multiIf(a.node != '', 'ak_name', i.node != '', 'node_id', 'none') AS by+ multiIf(a.node != '', a.labels, i.node != '', i.labels, c.labels) AS labels,+ multiIf(a.node != '', 'ak_name', i.node != '', 'node_id', c.node != '', 'config_root', 'none') AS byFROM(SELECT ak_name, argMinMerge(node_id) AS node_idFROM shirudo.nodesGROUP BY ak_name) AS n+LEFT JOIN+(+ SELECT k.ak_name AS ak_name, ifNull(k.config_root, '') AS config_root+ FROM shirudo.keepalive_latest AS k FINAL+) AS r ON r.ak_name = n.ak_nameLEFT JOIN (SELECT node, labels FROM sets WHERE by = 'ak_name') AS a ON a.node = n.ak_name-LEFT JOIN (SELECT node, labels FROM sets WHERE by = 'node_id') AS i ON i.node = n.node_id;+LEFT JOIN (SELECT node, labels FROM sets WHERE by = 'node_id') AS i ON i.node = n.node_id+LEFT JOIN (SELECT node, labels FROM sets WHERE by = 'config_root') AS c+ ON c.node = r.config_root AND r.config_root != '';-- History: what each node did at every operation of its tree that the-- backend has seen it reach, so that checks 4 and 6 and node_vulns candiff --git a/tools/backend/test/labels-put.expected b/tools/backend/test/labels-put.expectedindex 57b0a92b..f99f0004 100644--- a/tools/backend/test/labels-put.expected+++ b/tools/backend/test/labels-put.expected@@ -1,7 +1,8 @@labels-put: node_id edge-01: 2 labelslabels-put: node_id edge-02: 2 labelslabels-put: ak_name 000b03: 1 label-labels-put: loaded sets: 3+labels-put: config_root c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1: 1 label+labels-put: loaded sets: 4first put: status 0labels-put: node_id edge-02: clearedlabels-put: ak_name 000b01: 3 labels@@ -10,6 +11,7 @@ second put: status 0labels-put: node_id edge-01: 2 labelslabels-put: node_id edge-02: 2 labelslabels-put: ak_name 000b03: 1 label+labels-put: config_root c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1: 1 labelcheck: status 0labels-put: the file is not a JSON value, nothing loadednot json: status 1@@ -17,13 +19,15 @@ labels-put: the file is not an object whose one key is "nodes", nothing loadednot the form: status 1labels-put: the file is not an object whose one key is "nodes", nothing loadedanother top key: status 1-labels-put: set 1 names its node by neither or both of node_id and ak_name, nothing loaded+labels-put: set 1 does not name its node by exactly one of node_id, ak_name and config_root, nothing loadedboth names: status 1-labels-put: set 1 names its node by neither or both of node_id and ak_name, nothing loaded+labels-put: set 1 does not name its node by exactly one of node_id, ak_name and config_root, nothing loadedno name: status 1+labels-put: set 1: config_root is not 64 lowercase hex digits, nothing loaded+short root: status 1labels-put: set 1: ak_name is not 1 to 255 lowercase hex digits, nothing loadedak not hex: status 1-labels-put: set 1 has the key "label", which is not node_id, ak_name or labels, nothing loaded+labels-put: set 1 has the key "label", which is not node_id, ak_name, config_root or labels, nothing loadedunknown key: status 1labels-put: node_id a has two sets, nothing loadedtwo sets: status 1@@ -47,5 +51,6 @@ usage: labels-put [--check] <sets.json>|-no argument: status 2core-01 000b03 {'role':'core'} ak_nameedge-01 000b01 {'role':'edge','site':'fra','rack':'r 7'} ak_name-edge-02 000b02 {} none-sets 5+edge-02 000b02 {'image':'base'} config_root+lab-01 000b04 {'image':'base'} config_root+sets 6diff --git a/tools/backend/test/labels-put.setup.sql b/tools/backend/test/labels-put.setup.sqlindex 89d20647..f69bb680 100644--- a/tools/backend/test/labels-put.setup.sql+++ b/tools/backend/test/labels-put.setup.sql@@ -1,9 +1,18 @@-- SPDX-License-Identifier: BSD-2-Clause-- Copyright (c) 2026 Authors of shirudo----- Three keys the backend knows, named edge-01, edge-02 and core-01.+-- Four keys the backend knows, named edge-01, edge-02, core-01 and+-- lab-01. The newest records of the two edge nodes and lab-01 name one+-- config image, that of core-01 another.INSERT INTO shirudo.nodes (ak_name, node_id)SELECT record.1, argMinState(record.2, toDateTime64(0, 3))-FROM (SELECT arrayJoin([('000b01', 'edge-01'), ('000b02', 'edge-02'), ('000b03', 'core-01')]) AS record)+FROM (SELECT arrayJoin([('000b01', 'edge-01'), ('000b02', 'edge-02'), ('000b03', 'core-01'),+ ('000b04', 'lab-01')]) AS record)GROUP BY record.1;++INSERT INTO shirudo.keepalive_latest (ak_name, order_key, config_root) VALUES+('000b01', 1, repeat('c1', 32)),+('000b02', 1, repeat('c1', 32)),+('000b03', 1, repeat('c2', 32)),+('000b04', 1, repeat('c1', 32));diff --git a/tools/backend/test/labels-put.sh b/tools/backend/test/labels-put.shindex 6b99f0dd..c042095c 100644--- a/tools/backend/test/labels-put.sh+++ b/tools/backend/test/labels-put.sh@@ -2,10 +2,10 @@# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo#-# jobs/labels-put: a file of sets by node_id and by ak_name loads whole,-# from a file and from standard input; a second put replaces the sets it-# names, clears one with an empty set, and keeps those it does not name;-# --check loads nothing. Then what it refuses, each with a message and+# jobs/labels-put: a file of sets by node_id, by ak_name and by+# config_root loads whole, from a file and from standard input; a second+# put replaces the sets it names, clears one with an empty set, and keeps+# those it does not name; --check loads nothing. Then what it refuses, each with a message and# nothing loaded: a file that is not JSON or not of the form, a set that# names its node twice or not at all, two sets for one name, a key or# value node.labels could not hold, too many labels, a missing file and@@ -40,7 +40,8 @@ cat >"$work/first.json" <<'SETS'{"nodes": [{"node_id": "edge-01", "labels": {"role": "edge", "site": "fra"}},{"node_id": "edge-02", "labels": {"role": "edge", "site": "ams"}},- {"ak_name": "000b03", "labels": {"role": "core"}}+ {"ak_name": "000b03", "labels": {"role": "core"}},+ {"config_root": "c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1", "labels": {"image": "base"}}]}SETSrun 'first put' "$job" "$work/first.json"@@ -61,6 +62,7 @@ refused 'not the form' '[]'refused 'another top key' '{"nodes": [], "extra": 1}'refused 'both names' '{"nodes": [{"node_id": "a", "ak_name": "0b", "labels": {}}]}'refused 'no name' '{"nodes": [{"labels": {}}]}'+refused 'short root' '{"nodes": [{"config_root": "c1c1", "labels": {}}]}'refused 'ak not hex' '{"nodes": [{"ak_name": "000B", "labels": {}}]}'refused 'unknown key' '{"nodes": [{"node_id": "a", "labels": {}, "label": {}}]}'refused 'two sets' '{"nodes": [{"node_id": "a", "labels": {}}, {"node_id": "a", "labels": {"x": "y"}}]}'diff --git a/tools/backend/test/labels-put.sql b/tools/backend/test/labels-put.sqlindex 3a1591bb..32906327 100644--- a/tools/backend/test/labels-put.sql+++ b/tools/backend/test/labels-put.sql@@ -2,9 +2,10 @@-- Copyright (c) 2026 Authors of shirudo---- The labels of each node after test/labels-put.sh: edge-01 by its key,--- which the second put gave it over its node_id's set, edge-02 cleared,--- core-01 by its key from the first put, which the second left alone.--- The refused files loaded nothing.+-- which the second put gave it over its node_id's set; edge-02, whose+-- node_id set the second put cleared, and lab-01, which has no set of its+-- own, by the config image they booted; core-01 by its key from the first+-- put, which the second left alone. The refused files loaded nothing.SELECT node_id, ak_name, labels, by FROM shirudo.node_labels ORDER BY node_id;SELECT 'sets', count() FROM shirudo.label_sets;