Skip to content

eBPF-based security platform

tools/backend: give no head to a node past the newest loaded publication

John Fastabend8c35953fa779
diff --git a/Documentation/operate/backend/status.rst b/Documentation/operate/backend/status.rst
index 2dcb6c14..ba6bbf7f 100644
--- a/Documentation/operate/backend/status.rst
+++ b/Documentation/operate/backend/status.rst
@@ -90,7 +90,7 @@ A UI reads ``node_status`` as often as it likes, as :doc:`xtop` does:
* - ``generation``, ``last_seq``
- From the newest record; ``NULL`` for a node that follows no tree.
* - ``head_seq``
- - The head of the newest publication loaded for the node's tree, ``NULL`` when there is none.
+ - The head of the newest publication loaded for the node's tree, ``NULL`` when there is none or when the node names a later generation, whose head is in a publication yet to be loaded.
* - ``loaded``, ``set_sha256``
- ``count`` and ``set_sha256`` of the newest record.
* - ``updated_at``
diff --git a/Documentation/operate/backend/tables/status.rst b/Documentation/operate/backend/tables/status.rst
index c7396bf7..ec960107 100644
--- a/Documentation/operate/backend/tables/status.rst
+++ b/Documentation/operate/backend/tables/status.rst
@@ -217,6 +217,7 @@ shirudo.node_status
:generation: From the newest record. NULL with no tree.
:last_seq: From the newest record.
:head_seq: Head of the newest loaded publication of the node's tree.
+ NULL with none, or when the node names a later generation.
:loaded: count from the record.
:set_sha256: From the record.
:updated_at: When the row was computed.
diff --git a/Documentation/operate/backend/xtop.rst b/Documentation/operate/backend/xtop.rst
index 4243d7be..082befbb 100644
--- a/Documentation/operate/backend/xtop.rst
+++ b/Documentation/operate/backend/xtop.rst
@@ -110,7 +110,9 @@ key the operator has recorded as a TPM's.
* - ``TREE``
- ``✓ #<head>`` at the head of its tree, ``#<last>/#<head>``
behind it, ``#<last>`` when the head of its tree is not known,
- ``no tree`` when it follows none.
+ as the backend has no publication of the tree or the node names
+ a generation later than those it has, ``no tree`` when it
+ follows none.
* - ``SHIELDS``
- From ``node_vulns``, counted by upatch: ``✓`` loaded and
shielding (``shielded``), ``…`` coming (``shield_pending``),
diff --git a/tools/backend/sql/40-status.sql b/tools/backend/sql/40-status.sql
index 087a877a..48622236 100644
--- a/tools/backend/sql/40-status.sql
+++ b/tools/backend/sql/40-status.sql
@@ -370,7 +370,8 @@ WHERE (ak_name, tree_id) NOT IN (SELECT ak_name, tree_id FROM shirudo.history_co
-- fails, 2 for stale and 0 for none, and log has one line per check, 1
-- to 7. A node's trust does not decide its status; check 7 reads it to
-- choose the node's peers. head_seq is the head of the newest publication
--- known for the node's tree.
+-- known for the node's tree, NULL where the node names a later one, whose
+-- head is not known yet.
CREATE TABLE IF NOT EXISTS shirudo.node_status
(
node_id String,
@@ -716,7 +717,7 @@ SELECT
received AS last_seen,
generation,
last_seq,
- if(newest = 0, NULL, head) AS head_seq,
+ if(newest = 0 OR ifNull(generation, 0) > newest, NULL, head) AS head_seq,
count AS loaded,
set_sha256,
now() AS updated_at
diff --git a/tools/backend/test/status-head.expected b/tools/backend/test/status-head.expected
index 78a79400..6051ff34 100644
--- a/tools/backend/test/status-head.expected
+++ b/tools/backend/test/status-head.expected
@@ -2,7 +2,7 @@ generation 3 is the newest
at-3 at-3 healthy untrusted 0 3 3 3 1
at-3 5 head: pass, seq 3 of 3, generation 3
generation 4, an hour allowed
-ahead-of-ingest ahead-of-ingest healthy untrusted 0 5 5 4 1
+ahead-of-ingest ahead-of-ingest healthy untrusted 0 5 5 \N 1
at-1-of-generation-1 at-1-of-generation-1 unhealthy untrusted 5 1 1 4 1
at-2-of-generation-2 at-2-of-generation-2 unhealthy untrusted 5 2 2 4 0
at-3 at-3 healthy untrusted 0 3 3 4 1
@@ -23,7 +23,7 @@ generation-1-wrong-root 6 set: pass, 1 loaded
generation-1-wrong-root 7 peers: pass, 2 in group
other-tree 5 head: publication unknown
generation 4, forty minutes allowed
-ahead-of-ingest ahead-of-ingest healthy untrusted 0 5 5 4 1
+ahead-of-ingest ahead-of-ingest healthy untrusted 0 5 5 \N 1
at-1-of-generation-1 at-1-of-generation-1 unhealthy untrusted 5 1 1 4 1
at-2-of-generation-2 at-2-of-generation-2 unhealthy untrusted 5 2 2 4 0
at-3 at-3 unhealthy untrusted 5 3 3 4 1
diff --git a/tools/xtop/src/lib/fleet.ts b/tools/xtop/src/lib/fleet.ts
index ec42da73..358ae348 100644
--- a/tools/xtop/src/lib/fleet.ts
+++ b/tools/xtop/src/lib/fleet.ts
@@ -134,8 +134,12 @@ export function healthPercent(nodes: readonly Pick<FleetNode, "status">[]): numb
*/
const UNKNOWN_LAG = 0.5;
-/** head − last: how far behind its own tree a member is. */
-const lagOf = (t: ImageGroup["tree"]) => (t.headSeq === null ? UNKNOWN_LAG : t.headSeq - t.lastSeq!);
+/**
+ * head − last: how far behind its own tree a member is. One past the
+ * head is at it, as check 5 of node_status has it.
+ */
+const lagOf = (t: ImageGroup["tree"]) =>
+ t.headSeq === null ? UNKNOWN_LAG : Math.max(0, t.headSeq - t.lastSeq!);
/**
* The tree position of the member furthest behind its own tree, all-null
diff --git a/tools/xtop/test/fleet.test.ts b/tools/xtop/test/fleet.test.ts
index f5a5b81f..ea0be2f0 100644
--- a/tools/xtop/test/fleet.test.ts
+++ b/tools/xtop/test/fleet.test.ts
@@ -274,6 +274,22 @@ describe("groupByImage", () => {
});
});
+describe("a member past its head", () => {
+ const member = (over: Partial<NodeRow>) => toFleetNode(row({ ...over }));
+
+ test("is at it: reported after a member with an unknown head", () => {
+ const g = groupByImage([
+ member({ ak_name: "000b001", last_seq: 5, head_seq: 3 }),
+ member({ ak_name: "000b002", last_seq: 7, head_seq: null }),
+ ]);
+ expect(g[0]!.tree).toEqual({ lastSeq: 7, headSeq: null });
+ });
+
+ test("sorts with the rows at their head", () => {
+ expect(treeSortKey({ lastSeq: 5, headSeq: 3 })).toBe(0);
+ });
+});
+
describe("treeSortKey", () => {
const sorted = (trees: { lastSeq: number | null; headSeq: number | null }[]) =>
[...trees].sort((a, b) => treeSortKey(a) - treeSortKey(b) || 0);