Merge pull request 'feat(monitoring): Rosenpass unit-state + PSK-rotation alerts for Link A' (#84) from wt/t_20e420af into master

Reviewed-on: #84
This commit is contained in:
sirius 2026-12-03 07:22:41 +00:00
commit 2134f5a1d6
7 changed files with 365 additions and 0 deletions

View file

@ -0,0 +1,106 @@
# Rosenpass monitoring (host exporters for Link A)
Rosenpass rotates the WireGuard preshared key on **Link A** (`rpi` wg0
`10.10.0.1` ↔ `siriussec` wg0 `10.10.0.2`) about every two minutes. WireGuard
keeps using the last PSK it was given, so if the exchange dies the tunnel stays
**up** and silently stops being post-quantum. These files make that state
visible in Prometheus.
They are host configuration, not cluster manifests — nothing here is reconciled
by Flux. The cluster side is `monitoring/controllers/home/rosenpass.yaml`
(PrometheusRule) plus the `prometheus-node-exporter` block in
`monitoring/controllers/home/values.yaml`, which enables the textfile
collector.
## Why a host script instead of a collector
`kube-prometheus-stack-prometheus-node-exporter` is distroless and ships
without `--collector.textfile.directory`; `--collector.systemd` would need dbus
plumbing the DaemonSet does not have. So one POSIX `sh` script writes a
textfile that node-exporter serves from `/metrics`.
## Install (rpi and siriussec only — never linode)
```sh
install -m 0755 rosenpass-metrics.sh /usr/local/sbin/rosenpass-metrics.sh
install -m 0644 rosenpass-metrics.service /etc/systemd/system/
install -m 0644 rosenpass-metrics.timer /etc/systemd/system/
# Shared with the node-exporter DaemonSet's hostPath. The unit's sandbox sets
# ProtectSystem=strict, so this must exist before the first run (the kubelet's
# DirectoryOrCreate would normally make it once node-exporter rolls out).
install -d -m 0755 /var/lib/node-exporter/textfile
systemctl daemon-reload
systemctl enable --now rosenpass-metrics.timer
systemctl start rosenpass-metrics.service # first sample now, not in 60 s
```
Requires `wg`, `sha256sum`, `systemctl` and `awk` — present on Debian (rpi) and
Arch (siriussec).
## Series
```
rosenpass_unit_active{node="rpi|siriussec"} 0|1
rosenpass_psk_rotation_age_seconds{node=...,interface="wg0"}
rosenpass_psk_rotation_last_change_timestamp_seconds{node=...,interface="wg0"}
```
The script hashes the output of `wg show wg0 preshared-keys` and stores only
the SHA-256 digest plus the last-change timestamp in
`/var/lib/rosenpass-exporter/last-hash` (root-only). When the digest changes the
timestamp advances; otherwise only the age metric grows. **Key material is
never printed, logged or persisted.** The timer's own journal therefore carries
no key bytes — do not "improve" the script by echoing the listing.
`wg0` unreadable (interface down, no `CAP_NET_ADMIN`) leaves the age metric
unchanged rather than resetting it; `rosenpass_unit_active` and the `absent()`
arms of the alerts cover that case, so a broken exporter still pages.
## Verify
```sh
systemctl list-timers rosenpass-metrics.timer
cat /var/lib/node-exporter/textfile/rosenpass.prom
```
Both metrics should be present and the age well under 300 s once the cluster
scrapes the file (the file exists on the host either way; Prometheus only sees
it after the `values.yaml` change is reconciled).
## Alerts
`RosenpassDown` (unit inactive for 5 m) and `RosenpassPskRotationStalled`
(age > 900 s for 10 m), both `severity: warning`, both with `absent()` guards
so a missing series alerts instead of reading as healthy.
## Testing the rules without merging
`kustomize build monitoring/controllers/home` proves the rule renders; these two
prove the PromQL, before Flux ever sees it (a rule that loads but selects
nothing is the failure mode that matters here):
```sh
# 1. syntax + expression validation. promtool wants a bare `groups:` file, so
# strip the CRD wrapper first (PyYAML; jq cannot read YAML).
python3 - <<'PY'
import yaml
d = yaml.safe_load(open('monitoring/controllers/home/rosenpass.yaml'))
yaml.safe_dump({'groups': d['spec']['groups']}, open('/tmp/rosenpass-rules.yaml', 'w'))
PY
kubectl -n monitoring run promtool-check --rm -i --restart=Never \
--image=quay.io/prometheus/prometheus:v3.15.1-distroless \
--command -- /bin/promtool check rules /dev/stdin < /tmp/rosenpass-rules.yaml
```
```sh
# 2. behaviour: promtool test rules with input_series. Mount the rule file and
# a test file (rule_files: [rosenpass-rules.yaml]) from a ConfigMap and check
# four scenarios — healthy (no alerts), one unit down (only that node), age
# over threshold plus a missing peer series, and every series missing (both
# absent() arms). Expect `SUCCESS`. Note promtool compares annotations
# exactly, so expectations have to carry the rendered text.
```
Keep the test file out of the repo: it duplicates the annotation strings, so any
edit to the alerts would have to re-derive it from `rosenpass.yaml`.

View file

@ -0,0 +1,23 @@
[Unit]
Description=Rosenpass health metrics for node-exporter (textfile collector)
Documentation=file:///usr/local/sbin/rosenpass-metrics.sh
# No Requires=/After= on rosenpass.service or [email protected] on purpose:
# the recorder has to keep running and keep exporting while the unit it watches
# is dead or the interface is down — a hard dependency would take the metric
# away exactly when it becomes the alert.
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/rosenpass-metrics.sh
User=root
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
# /var/lib/rosenpass-exporter (hash state) is created by systemd.
StateDirectory=rosenpass-exporter
# The textfile dir is shared with the node-exporter DaemonSet (hostPath). It is
# created by the install step; the leading `-` keeps the unit startable if a
# node has lost it (the node-exporter DirectoryOrCreate recreates it), instead
# of failing the whole unit with 226/NAMESPACE.
ReadWritePaths=-/var/lib/node-exporter/textfile

View file

@ -0,0 +1,100 @@
#!/bin/sh
# rosenpass-metrics.sh — emit Rosenpass health metrics for node-exporter's
# textfile collector.
#
# Installed on rpi and siriussec ONLY (the two ends of Link A: rpi wg0
# 10.10.0.1 <-> siriussec wg0 10.10.0.2), run every 60 s by
# rosenpass-metrics.timer as root. See README.md in this directory.
#
# Why a host script: kube-prometheus-stack-prometheus-node-exporter is
# distroless and ships without --collector.textfile.directory (and without the
# dbus plumbing --collector.systemd needs), so nothing inside the cluster can
# observe a host systemd unit. This writes a textfile that the DaemonSet's
# textfile collector then serves on :9100/metrics.
#
# Signals:
# rosenpass_unit_active{node}
# 1 when `systemctl is-active rosenpass.service`
# says active, else 0.
# rosenpass_psk_rotation_age_seconds{node,interface}
# seconds since the wg0 preshared-key set last
# changed. Rotation runs about every two minutes.
# rosenpass_psk_rotation_last_change_timestamp_seconds{node,interface}
# unix timestamp of that last observed change.
#
# The preshared key is NEVER printed, logged or stored: the key listing is
# piped straight into sha256sum and only the hex digest is kept, in a
# root-only state file. Do not add an `echo` of the listing while debugging.
#
# Deliberately independent of rosenpass.service: this script must keep
# reporting (unit_active=0, age growing) while the thing it watches is dead.
set -eu
NODE=$(hostname -s)
IFACE=wg0
UNIT=rosenpass.service
STATE_DIR=/var/lib/rosenpass-exporter
STATE_FILE=$STATE_DIR/last-hash
OUT_DIR=/var/lib/node-exporter/textfile
OUT_FILE=$OUT_DIR/rosenpass.prom
# 0644 so the node-exporter container (nobody/65534) can read the file and the
# directory stays traversable.
umask 022
mkdir -p "$STATE_DIR" "$OUT_DIR"
now=$(date +%s)
# --- 1. unit state -----------------------------------------------------------
active=0
if [ "$(systemctl is-active "$UNIT" 2>/dev/null || true)" = "active" ]; then
active=1
fi
# --- 2. PSK rotation ---------------------------------------------------------
# `wg show ... preshared-keys` fails (and prints nothing) when wg0 is absent;
# command substitution would otherwise hash an empty string, which looks like a
# change on the first poll after the interface returns. Only hash real output.
psk_list=$(wg show "$IFACE" preshared-keys 2>/dev/null || true)
last_hash=""
last_change=""
if [ -f "$STATE_FILE" ]; then
last_hash=$(sed -n '1p' "$STATE_FILE")
last_change=$(sed -n '2p' "$STATE_FILE")
fi
if [ -n "$psk_list" ]; then
hash_now=$(printf '%s' "$psk_list" | sha256sum | awk '{print $1}')
if [ "$hash_now" != "$last_hash" ]; then
# First observation or an actual rotation: stamp it.
last_change=$now
printf '%s\n%s\n' "$hash_now" "$last_change" > "$STATE_FILE.tmp"
mv "$STATE_FILE.tmp" "$STATE_FILE"
fi
elif [ -z "$last_change" ]; then
# wg unreadable and no history: nothing to compare against yet.
last_change=""
fi
# --- 3. write the textfile atomically ---------------------------------------
tmp="$OUT_FILE.$$"
{
printf '# HELP rosenpass_unit_active rosenpass.service is active (1) or not (0).\n'
printf '# TYPE rosenpass_unit_active gauge\n'
printf 'rosenpass_unit_active{node="%s"} %s\n' "$NODE" "$active"
if [ -n "$last_change" ]; then
age=$((now - last_change))
[ "$age" -ge 0 ] || age=0
printf '# HELP rosenpass_psk_rotation_age_seconds Seconds since the wg0 preshared-key set last changed.\n'
printf '# TYPE rosenpass_psk_rotation_age_seconds gauge\n'
printf 'rosenpass_psk_rotation_age_seconds{node="%s",interface="%s"} %s\n' "$NODE" "$IFACE" "$age"
printf '# HELP rosenpass_psk_rotation_last_change_timestamp_seconds Unix timestamp of the last observed wg0 preshared-key change.\n'
printf '# TYPE rosenpass_psk_rotation_last_change_timestamp_seconds gauge\n'
printf 'rosenpass_psk_rotation_last_change_timestamp_seconds{node="%s",interface="%s"} %s\n' "$NODE" "$IFACE" "$last_change"
fi
} > "$tmp"
chmod 0644 "$tmp"
mv "$tmp" "$OUT_FILE"

View file

@ -0,0 +1,11 @@
[Unit]
Description=Collect Rosenpass health metrics every 60s
[Timer]
OnBootSec=30s
OnUnitActiveSec=60s
AccuracySec=5s
Unit=rosenpass-metrics.service
[Install]
WantedBy=timers.target

View file

@ -11,6 +11,10 @@ resources:
- prune-alerts.yaml
# Flow-loss gate + material-loss alerts for the Hubble sensor (audit F10).
- hubble-loss-gate.yaml
# Rosenpass unit-state + PSK-rotation alerts for Link A (rpi <-> siriussec).
# Series come from a host textfile exporter (infrastructure/networking/rosenpass/),
# so silently missing data is itself an alert.
- rosenpass.yaml
# Nightly consistent sqlite snapshot of Grafana's dashboards/alerts/users.
- grafana-backup.yaml
- dashboards

View file

@ -0,0 +1,94 @@
# Rosenpass (post-quantum PSK rotation) -> Prometheus
#
# Link A (rpi wg0 10.10.0.1 <-> siriussec wg0 10.10.0.2) runs rosenpass 0.2.3
# as a host systemd unit on both ends, rotating that WireGuard peer's
# preshared key roughly every two minutes. Nothing about the *tunnel* depends
# on it working: WireGuard keeps using the last PSK it received, so a dead or
# stalled exchange leaves the link up and silently non-post-quantum. These two
# alerts are the only thing that notices.
#
# Why a host script and not a collector: the
# kube-prometheus-stack-prometheus-node-exporter DaemonSet is distroless and
# has no --collector.textfile.directory (only /host/proc, /host/sys and
# /host/root are mounted), while --collector.systemd needs dbus plumbing it
# does not have. So /usr/local/sbin/rosenpass-metrics.sh on rpi and siriussec
# (committed under infrastructure/networking/rosenpass/, driven by
# rosenpass-metrics.timer every 60 s) writes the textfile, and the
# prometheus-node-exporter block in values.yaml turns the collector on.
#
# Series, emitted per host, never containing key material (the PSK set is only
# SHA-256 hashed, in a root-only state file):
# rosenpass_unit_active{node="rpi|siriussec"} 0|1
# rosenpass_psk_rotation_age_seconds{node=...,interface="wg0"}
# rosenpass_psk_rotation_last_change_timestamp_seconds{node=...,interface="wg0"}
#
# Both rules carry an absent() arm deliberately: a metric that simply stops
# arriving (timer disabled, script broken, node-exporter down, scrape lost)
# must alert, because "nothing happened" is exactly what a healthy rotation
# looks like from Prometheus' side. The absent arms name the two hosts that run
# the timer so linode — which has node-exporter but no rosenpass — does not page.
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: rosenpass
namespace: monitoring
labels:
release: kube-prometheus-stack
spec:
groups:
- name: rosenpass
rules:
- alert: RosenpassDown
expr: |
(
max by (node) (rosenpass_unit_active) == 0
)
or
(
absent(rosenpass_unit_active{node="rpi"})
or
absent(rosenpass_unit_active{node="siriussec"})
)
for: 5m
labels:
severity: warning
annotations:
summary: "Rosenpass is not active on {{ $labels.node }}"
description: >-
The unit on {{ $labels.node }} is not reporting active, or the
series is missing entirely (timer, script or scrape gone).
WireGuard on Link A keeps working on the last preshared key, so
traffic gives no hint — the tunnel has silently stopped being
post-quantum until this is fixed. On that host check
`systemctl status rosenpass` and
`systemctl list-timers rosenpass-metrics.timer`, then
`cat /var/lib/node-exporter/textfile/rosenpass.prom`. Do not
restart rosenpass on only one end: a one-sided restart leaves the
pair disagreeing until the next rekey.
- alert: RosenpassPskRotationStalled
expr: |
(
max by (node, interface) (rosenpass_psk_rotation_age_seconds) > 900
)
or
(
absent(rosenpass_psk_rotation_age_seconds{node="rpi", interface="wg0"})
or
absent(rosenpass_psk_rotation_age_seconds{node="siriussec", interface="wg0"})
)
for: 10m
labels:
severity: warning
annotations:
summary: >-
No rosenpass PSK rotation on {{ $labels.node }} {{ $labels.interface }}
for over 15 minutes
description: >-
Rotation runs about every two minutes, so a preshared-key set that
has not changed in 15 minutes means the exchange has stalled
(peer unreachable, unit wedged) or the metric has gone missing.
The link still passes traffic on the last key, which is why this
is otherwise invisible. Check `systemctl status rosenpass` and
`journalctl -u rosenpass -n 30` on both rpi and siriussec, and
confirm the Pi still admits 9999/udp from 192.168.1.179
(`sudo ufw status | grep 9999`).

View file

@ -255,6 +255,33 @@ prometheus-node-exporter:
# to linode. node-exporter is a read-only host-metrics DaemonSet (hostPath
# /proc + /sys, host network); it adds ~700 series per node, well inside the
# 8GB retentionSize cap, and rpi's PreferNoSchedule taint does not exclude it.
#
# Textfile collector: rosenpass-metrics.sh on rpi/siriussec writes
# /var/lib/node-exporter/textfile/rosenpass.prom and the DaemonSet serves it
# from /metrics (alerts: monitoring/controllers/home/rosenpass.yaml, host
# units: infrastructure/networking/rosenpass/). The image is distroless with
# no collector enabled, so the flag below is what turns it on.
#
# extraArgs REPLACES the subchart default list — Helm replaces lists, it does
# not merge them. The two filesystem exclusions are copied verbatim from
# prometheus-node-exporter 4.60.1 (the subchart pinned by kube-prometheus-stack
# 92.14.0); dropping them would add every container mountpoint to
# node_filesystem_* and change the disk alerts.
extraArgs:
- --collector.textfile.directory=/host/textfile
- --collector.filesystem.mount-points-exclude=^/(dev|proc|sys|run/containerd/.+|var/lib/docker/.+|var/lib/kubelet/.+)($|/)
- --collector.filesystem.fs-types-exclude=^(autofs|binfmt_misc|bpf|cgroup2?|configfs|debugfs|devpts|devtmpfs|fusectl|hugetlbfs|iso9660|mqueue|nsfs|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|selinuxfs|squashfs|sysfs|tracefs|erofs)$
extraVolumes:
- name: textfile
hostPath:
path: /var/lib/node-exporter/textfile
# DirectoryOrCreate: nodes with no rosenpass timer (linode) still get a
# mountable empty directory instead of a CreateContainerConfigError.
type: DirectoryOrCreate
extraVolumeMounts:
- name: textfile
mountPath: /host/textfile
readOnly: true
resources:
requests:
cpu: 20m