Skip to content

eBPF-based security platform

testing: verify kernel-obs OTLP exports against map snapshots

John Fastabendf3106052b2ad
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index c435d443..e34e75db 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -458,13 +458,13 @@ signed config image.
- The DNS packet builders the matrix test uses
* - ``t9101-kernel-obs``
- ``kernel-obs`` with eight serializers, records from real events,
- draining exports
+ draining exports and matching OTLP collector snapshots
* - ``t9102-kernel-obs-net``
- - Socket families
+ - Socket families and matching OTLP collector snapshots
* - ``t9103-kernel-obs-priv``
- - Privilege and executable-mapping records
+ - Privilege and executable-mapping records in maps and OTLP
* - ``t9104-kernel-obs-dns-matrix``
- - DNS over loopback
+ - DNS over loopback in maps and OTLP
* - ``t9105-delivery``
- Registry, tree sync, load, export and attestation end to end
* - ``t9106-policy-exec-graph``
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rst
index dee2a44c..18f979e3 100644
--- a/Documentation/contribute/testing/testing.rst
+++ b/Documentation/contribute/testing/testing.rst
@@ -529,7 +529,11 @@ appends it to a signed upatch tree with ``bpm tree append``. The target
activates a signed config that points at the tree and the collector,
and its units pull, verify, load and export. The host then verifies the
target's TPM evidence with ``attest verify`` and checks that the
-collector holds the package's records.
+collector holds the package's records. The earlier ``kernel-obs`` tests
+export each map snapshot as local JSON and OTLP from one read; the host
+compares the snapshots with the collector's JSON output. The comparison
+uses event times relative to each map's interval start and ignores the
+metric export time, which the two decoder calls sample independently.
.. code-block:: console
diff --git a/testing/end2end/check-otlp.py b/testing/end2end/check-otlp.py
new file mode 100644
index 00000000..27852038
--- /dev/null
+++ b/testing/end2end/check-otlp.py
@@ -0,0 +1,119 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+
+import collections
+import glob
+import json
+import os
+import sys
+
+
+def value(item):
+ if "stringValue" in item:
+ return item["stringValue"]
+ if "intValue" in item:
+ return int(item["intValue"])
+ if "doubleValue" in item:
+ return item["doubleValue"]
+ if "boolValue" in item:
+ return item["boolValue"]
+ if "arrayValue" in item:
+ return [value(entry) for entry in item["arrayValue"].get("values", [])]
+ if "kvlistValue" in item:
+ return {entry["key"]: value(entry["value"])
+ for entry in item["kvlistValue"].get("values", [])}
+ if not item:
+ return None
+ raise ValueError(f"unexpected OTLP value: {item}")
+
+
+def key(item):
+ return json.dumps(item, sort_keys=True, separators=(",", ":"))
+
+
+def log_body(body):
+ since = int(body["interval"]["since_unix_nano"])
+
+ def relative(item):
+ if isinstance(item, dict):
+ return {name: str(int(value) - since) if name.endswith("_unix_nano")
+ else relative(value) for name, value in item.items()}
+ if isinstance(item, list):
+ return [relative(value) for value in item]
+ return item
+
+ return relative(body)
+
+
+def metric_body(metric):
+ def without_export_time(item):
+ if isinstance(item, dict):
+ return {name: without_export_time(value)
+ for name, value in item.items() if name != "timeUnixNano"}
+ if isinstance(item, list):
+ return [without_export_time(value) for value in item]
+ return item
+
+ return without_export_time(metric)
+
+
+def collected(path):
+ logs = collections.Counter()
+ metrics = collections.Counter()
+ with open(path, encoding="utf-8") as stream:
+ for line in stream:
+ document = json.loads(line)
+ for resource in document.get("resourceLogs", []):
+ for scope in resource.get("scopeLogs", []):
+ for record in scope.get("logRecords", []):
+ attrs = {entry["key"]: value(entry["value"])
+ for entry in record.get("attributes", [])}
+ if attrs.get("shirudo.upatch.name") != "kernel-obs":
+ continue
+ logs[key((attrs.get("shirudo.map.name"),
+ log_body(value(record["body"]))))] += 1
+ for resource in document.get("resourceMetrics", []):
+ for scope in resource.get("scopeMetrics", []):
+ for metric in scope.get("metrics", []):
+ metrics[key(metric_body(metric))] += 1
+ return logs, metrics
+
+
+def expected(directory):
+ paths = sorted(glob.glob(os.path.join(directory, "t910[1-4]-*.ndjson")))
+ if not paths:
+ raise AssertionError("no t9101-t9104 JSON snapshots")
+ logs = collections.Counter()
+ metrics = collections.Counter()
+ for path in paths:
+ with open(path, encoding="utf-8") as stream:
+ for line in stream:
+ document = json.loads(line)
+ if isinstance(document, list):
+ for metric in document:
+ metrics[key(metric_body(metric))] += 1
+ else:
+ assert document["upatch"] == "kernel-obs", path
+ logs[key((document["map"], log_body(document)))] += 1
+ return logs, metrics
+
+
+def check(want, got, signal):
+ missing = want - got
+ if missing:
+ sample, count = next(iter(missing.items()))
+ raise AssertionError(f"missing {count} {signal} export(s): {sample[:500]}")
+
+
+def main():
+ want_logs, want_metrics = expected(sys.argv[1])
+ got_logs, got_metrics = collected(sys.argv[2])
+ check(want_logs, got_logs, "log")
+ check(want_metrics, got_metrics, "metric")
+ print(f"verified {sum(want_logs.values())} map logs and "
+ f"{sum(want_metrics.values())} metrics in the collector")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/testing/end2end/host.sh b/testing/end2end/host.sh
index d305e602..cb3bc4c2 100755
--- a/testing/end2end/host.sh
+++ b/testing/end2end/host.sh
@@ -179,6 +179,9 @@ assert ops[-1] == {key: published[key] for key in
PY
say "the collector holds what kernel-obs observed"
+python3 "$REPO/testing/end2end/check-otlp.py" \
+ "$REPO/build/test-vm/vms/$VM_NAME-end2end/result/end2end/otel" \
+ "$W/out/otlp.json" || die "the collector snapshots differ from the map exports"
python3 - "$W/out/otlp.json" <<'PY' || die "the collector did not get the export"
import json, sys
diff --git a/testing/end2end/otel-export.sh b/testing/end2end/otel-export.sh
new file mode 100644
index 00000000..170bd24f
--- /dev/null
+++ b/testing/end2end/otel-export.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+
+otel_setup()
+{
+ ca=$PWD/../build/test-vm/end2end/ca.pem
+ [ -s "$ca" ] || test_skip_all "no collector CA: run make -C testing end2end"
+ export OTEL_EXPORTER_OTLP_CERTIFICATE="$ca"
+ OTEL_OUT=/mnt/shirudo-out/end2end/otel
+ mkdir -p "$OTEL_OUT"
+}
+
+otel_export()
+{
+ "$SHIRUDO_BIN" upatch map "$NAME" "$1" --json="$2" --otel \
+ --otel-endpoint=https://10.0.2.2:14318 >/dev/null
+}
diff --git a/testing/end2end/t9101-kernel-obs.sh b/testing/end2end/t9101-kernel-obs.sh
index 516f44f4..6dc69017 100755
--- a/testing/end2end/t9101-kernel-obs.sh
+++ b/testing/end2end/t9101-kernel-obs.sh
@@ -14,6 +14,8 @@ UNLOAD="$FIXTURES/$NAME.unload.upatch"
PIN="/run/shirudo/bpffs/upatches/$NAME/link_0"
TABLES="exec net socket listen watch dns priv file"
WORK=$(mktemp -d)
+. ./end2end/otel-export.sh
+otel_setup
cleanup()
{
@@ -32,8 +34,7 @@ export_maps()
{
output="$1"
for map in $TABLES watch_stat; do
- "$SHIRUDO_BIN" upatch map "$NAME" "$map" \
- --json="$output" >/dev/null
+ otel_export "$map" "$output"
done
}
@@ -246,26 +247,26 @@ test_expect_success 'exec, net, listen, DNS, privilege and file events produce r
python3 end2end/kernel_obs_events.py all &&
python3 end2end/kernel_obs_events.py dns-long &&
python3 end2end/kernel_obs_events.py dns-many &&
- export_maps "$WORK/events.ndjson" &&
- python3 "$WORK/check.py" "$WORK/events.ndjson" &&
- python3 "$WORK/check_events.py" "$WORK/events.ndjson"
+ export_maps "$OTEL_OUT/t9101-events.ndjson" &&
+ python3 "$WORK/check.py" "$OTEL_OUT/t9101-events.ndjson" &&
+ python3 "$WORK/check_events.py" "$OTEL_OUT/t9101-events.ndjson"
'
test_expect_success 'watched destinations are recorded with the metric' '
python3 end2end/kernel_obs_events.py watch &&
- export_maps "$WORK/watch.ndjson" &&
- python3 "$WORK/check_watch.py" "$WORK/watch.ndjson"
+ export_maps "$OTEL_OUT/t9101-watch.ndjson" &&
+ python3 "$WORK/check_watch.py" "$OTEL_OUT/t9101-watch.ndjson"
'
test_expect_success 'an export drains the tables' '
- export_maps "$WORK/drained.ndjson" &&
- python3 "$WORK/check_drained.py" "$WORK/drained.ndjson"
+ export_maps "$OTEL_OUT/t9101-drained.ndjson" &&
+ python3 "$WORK/check_drained.py" "$OTEL_OUT/t9101-drained.ndjson"
'
test_expect_success 'only the touched tables carry records' '
python3 end2end/kernel_obs_events.py exec &&
- export_maps "$WORK/exec-only.ndjson" &&
- python3 "$WORK/check_exec_only.py" "$WORK/exec-only.ndjson"
+ export_maps "$OTEL_OUT/t9101-exec-only.ndjson" &&
+ python3 "$WORK/check_exec_only.py" "$OTEL_OUT/t9101-exec-only.ndjson"
'
test_done
diff --git a/testing/end2end/t9102-kernel-obs-net.sh b/testing/end2end/t9102-kernel-obs-net.sh
index b02e3568..32eef472 100755
--- a/testing/end2end/t9102-kernel-obs-net.sh
+++ b/testing/end2end/t9102-kernel-obs-net.sh
@@ -13,6 +13,8 @@ LOAD="$FIXTURES/$NAME.upatch"
UNLOAD="$FIXTURES/$NAME.unload.upatch"
PIN="/run/shirudo/bpffs/upatches/$NAME/link_0"
WORK=$(mktemp -d)
+. ./end2end/otel-export.sh
+otel_setup
cleanup()
{
@@ -31,8 +33,7 @@ trap cleanup EXIT
export_net_maps()
{
for map in net socket; do
- "$SHIRUDO_BIN" upatch map "$NAME" "$map" \
- --json="$WORK/events.ndjson" >/dev/null
+ otel_export "$map" "$OTEL_OUT/t9102-events.ndjson"
done
}
@@ -108,7 +109,8 @@ test_expect_success 'TCP, UDP and additional socket families are recorded' '
python3 end2end/kernel_obs_events.py sockets \
>"$WORK/expected.json" &&
export_net_maps &&
- python3 "$WORK/check.py" "$WORK/expected.json" "$WORK/events.ndjson"
+ python3 "$WORK/check.py" "$WORK/expected.json" \
+ "$OTEL_OUT/t9102-events.ndjson"
'
test_done
diff --git a/testing/end2end/t9103-kernel-obs-priv.sh b/testing/end2end/t9103-kernel-obs-priv.sh
index bf2414cf..599c6d41 100755
--- a/testing/end2end/t9103-kernel-obs-priv.sh
+++ b/testing/end2end/t9103-kernel-obs-priv.sh
@@ -18,6 +18,8 @@ BPF_FILTER_UNLOAD="$FIXTURES/shirudo-test-map-export.unload.upatch"
PIN="/run/shirudo/bpffs/upatches/$NAME/link_0"
MODPROBE_FILE=/proc/sys/kernel/modprobe
WORK=$(mktemp -d)
+. ./end2end/otel-export.sh
+otel_setup
MODPROBE_BACKUP="$WORK/kernel.modprobe"
EVENT_PID=
@@ -94,7 +96,7 @@ load_observability()
export_priv()
{
- "$SHIRUDO_BIN" upatch map "$NAME" priv --json="$1" >/dev/null
+ otel_export priv "$1"
}
test_expect_success 'kernel-obs upatch loads with the priv serializer' '
@@ -104,18 +106,18 @@ test_expect_success 'kernel-obs upatch loads with the priv serializer' '
test_expect_success 'non-executable mappings do not produce exec protection records' '
run_event negative &&
- export_priv "$WORK/negative.ndjson" &&
+ export_priv "$OTEL_OUT/t9103-negative.ndjson" &&
python3 end2end/priv_events.py check-negative \
- "$WORK/negative.ndjson" "$WORK/expected.json" &&
- export_priv "$WORK/negative-drained.ndjson"
+ "$OTEL_OUT/t9103-negative.ndjson" "$WORK/expected.json" &&
+ export_priv "$OTEL_OUT/t9103-negative-drained.ndjson"
'
test_expect_success 'mprotect does not duplicate an existing executable mapping' '
run_event existing-exec &&
- export_priv "$WORK/existing-exec.ndjson" &&
+ export_priv "$OTEL_OUT/t9103-existing-exec.ndjson" &&
python3 end2end/priv_events.py check-existing-exec \
- "$WORK/existing-exec.ndjson" "$WORK/expected.json" &&
- export_priv "$WORK/existing-exec-drained.ndjson"
+ "$OTEL_OUT/t9103-existing-exec.ndjson" "$WORK/expected.json" &&
+ export_priv "$OTEL_OUT/t9103-existing-exec-drained.ndjson"
'
test_expect_success 'privilege events produce attributed priv records' '
@@ -125,13 +127,12 @@ test_expect_success 'privilege events produce attributed priv records' '
--report=- >"$WORK/bpf-filter-load.log" 2>&1 &&
"$SHIRUDO_BIN" upatch unload "$BPF_FILTER_UNLOAD" --no-precheck \
>/dev/null &&
- export_priv "$WORK/priv.ndjson" &&
- "$SHIRUDO_BIN" upatch map "$NAME" exec \
- --json="$WORK/exec.ndjson" >/dev/null &&
+ export_priv "$OTEL_OUT/t9103-priv.ndjson" &&
+ otel_export exec "$OTEL_OUT/t9103-exec.ndjson" &&
python3 end2end/priv_events.py \
- check-exec-unknown "$WORK/exec.ndjson" &&
+ check-exec-unknown "$OTEL_OUT/t9103-exec.ndjson" &&
python3 end2end/priv_events.py check \
- "$WORK/expected.json" "$WORK/priv.ndjson"
+ "$WORK/expected.json" "$OTEL_OUT/t9103-priv.ndjson"
'
test_expect_success 'credential anomalies are recorded once per task' '
@@ -141,9 +142,10 @@ test_expect_success 'credential anomalies are recorded once per task' '
load_observability "$ANOMALY_LOAD" &&
python3 end2end/priv_events.py anomaly \
"$WORK/anomaly-expected.json" &&
- export_priv "$WORK/anomaly.ndjson" &&
+ export_priv "$OTEL_OUT/t9103-anomaly.ndjson" &&
python3 end2end/priv_events.py \
- check-anomaly "$WORK/anomaly-expected.json" "$WORK/anomaly.ndjson"
+ check-anomaly "$WORK/anomaly-expected.json" \
+ "$OTEL_OUT/t9103-anomaly.ndjson"
'
test_done
diff --git a/testing/end2end/t9104-kernel-obs-dns-matrix.sh b/testing/end2end/t9104-kernel-obs-dns-matrix.sh
index 9bbc3654..e662f898 100755
--- a/testing/end2end/t9104-kernel-obs-dns-matrix.sh
+++ b/testing/end2end/t9104-kernel-obs-dns-matrix.sh
@@ -13,6 +13,8 @@ LOAD="$FIXTURES/$NAME.upatch"
UNLOAD="$FIXTURES/$NAME.unload.upatch"
PIN="/run/shirudo/bpffs/upatches/$NAME/link_0"
WORK=$(mktemp -d)
+. ./end2end/otel-export.sh
+otel_setup
cleanup()
{
@@ -56,10 +58,10 @@ test_expect_success 'DNS observations use real loopback UDP traffic' '
"$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 &&
python3 end2end/kernel_obs_events.py dns-long &&
python3 end2end/kernel_obs_events.py dns-many &&
- "$SHIRUDO_BIN" upatch map "$NAME" dns --json="$WORK/dns.ndjson" >/dev/null &&
- python3 "$WORK/check.py" "$WORK/dns.ndjson" &&
- "$SHIRUDO_BIN" upatch map "$NAME" dns --json="$WORK/dns-empty.ndjson" >/dev/null &&
- python3 "$WORK/check-empty.py" "$WORK/dns-empty.ndjson"
+ otel_export dns "$OTEL_OUT/t9104-dns.ndjson" &&
+ python3 "$WORK/check.py" "$OTEL_OUT/t9104-dns.ndjson" &&
+ otel_export dns "$OTEL_OUT/t9104-dns-empty.ndjson" &&
+ python3 "$WORK/check-empty.py" "$OTEL_OUT/t9104-dns-empty.ndjson"
'
test_done