fix(rosenpass): per-interface PSK rotation metrics so Link B is observed

rosenpass-metrics.sh hashed wg0 only, so Link B (rpi wg-remote <->
linode wg0) could stall - the Pi's residential public address moving, or
the UPnP mapping for UDP 9999 being dropped - while every Link A series
stayed healthy. Iterate over an interface list, keep one state file per
interface, and emit age/last-change per interface. wg0's series keep the
same names and labels, so existing dashboards and the Link A alerts are
unchanged; RosenpassPskRotationStalled already keys on
(node, interface) and therefore starts covering wg-remote with no rule
change. A host without the interface (siriussec) simply skips it.
This commit is contained in:
Lance Walters 2026-12-03 02:43:17 -05:00
commit acdfdd7975
2 changed files with 73 additions and 56 deletions

View file

@ -1,7 +1,8 @@
# Rosenpass monitoring (host exporters for Link A)
# Rosenpass monitoring (host exporters for Link A and Link B)
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
`10.10.0.1` ↔ `siriussec` wg0 `10.10.0.2`) and on **Link B** (`rpi` wg-remote
`10.20.0.2` ↔ `linode` wg0 `10.20.0.1`) 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.
@ -41,20 +42,29 @@ Arch (siriussec).
```
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"}
rosenpass_psk_rotation_age_seconds{node=...,interface="wg0|wg-remote"}
rosenpass_psk_rotation_last_change_timestamp_seconds{node=...,interface="wg0|wg-remote"}
```
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.
One series set per interface: `wg0` is Link A and `wg-remote` is Link B, and an
interface absent on that host (wg-remote on siriussec, where it does not exist)
is skipped. `RosenpassPskRotationStalled` is interface-agnostic
(`max by (node, interface) ... > 900`), so a stalled Link B pages as soon as the
`wg-remote` series exists — which is why hashing only `wg0` left Link B
unobserved.
`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.
The script hashes the output of `wg show <interface> preshared-keys` and stores
only the SHA-256 digest plus the last-change timestamp in
`/var/lib/rosenpass-exporter/last-hash-<interface>` (root-only, one file per
interface so a Link B rotation is never recorded as a Link A one). 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.
An interface unreadable (down, no `CAP_NET_ADMIN`) emits no series for that
interface rather than resetting its age; `rosenpass_unit_active` and the
`absent()` arms of the alerts cover that case, so a broken exporter still pages.
## Verify

View file

@ -2,9 +2,9 @@
# 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.
# Installed on rpi and siriussec (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
@ -17,13 +17,19 @@
# 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.
# seconds since that interface's 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
# Interfaces are emitted independently: wg0 is Link A (rpi <-> siriussec) and
# wg-remote is Link B (rpi <-> linode, through a permanent UPnP mapping on a
# router with no admin access). Link B stalls silently if the Pi's residential
# public address moves, so it needs its own series — hashing only wg0 leaves
# Link B unobserved.
#
# The preshared key is NEVER printed, logged or stored: each interface's 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
@ -31,11 +37,12 @@
set -eu
NODE=$(hostname -s)
IFACE=wg0
# Interfaces rosenpass manages on this host. One absent here is skipped, so the
# same file is correct on rpi (wg0 + wg-remote) and siriussec (wg0 only).
IFACES="wg0 wg-remote"
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
@ -52,49 +59,49 @@ 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 ---------------------------------------
# --- 2. PSK rotation, per interface -----------------------------------------
# `wg show ... preshared-keys` fails (and prints nothing) when the interface 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, and keep one state file per interface so a Link B rotation is never
# recorded as a Link A one.
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
printf '# HELP rosenpass_psk_rotation_age_seconds Seconds since that interface preshared-key set last changed.\n'
printf '# TYPE rosenpass_psk_rotation_age_seconds gauge\n'
printf '# HELP rosenpass_psk_rotation_last_change_timestamp_seconds Unix timestamp of the last observed preshared-key change on that interface.\n'
printf '# TYPE rosenpass_psk_rotation_last_change_timestamp_seconds gauge\n'
for IFACE in $IFACES; do
psk_list=$(wg show "$IFACE" preshared-keys 2>/dev/null || true)
[ -n "$psk_list" ] || continue
STATE_FILE=$STATE_DIR/last-hash-$IFACE
last_hash=""
last_change=""
if [ -f "$STATE_FILE" ]; then
last_hash=$(sed -n '1p' "$STATE_FILE")
last_change=$(sed -n '2p' "$STATE_FILE")
fi
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
[ -n "$last_change" ] || continue
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
done
} > "$tmp"
chmod 0644 "$tmp"
mv "$tmp" "$OUT_FILE"