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 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 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 **up** and silently stops being post-quantum. These files make that state
visible in Prometheus. visible in Prometheus.
@ -41,20 +42,29 @@ Arch (siriussec).
``` ```
rosenpass_unit_active{node="rpi|siriussec"} 0|1 rosenpass_unit_active{node="rpi|siriussec"} 0|1
rosenpass_psk_rotation_age_seconds{node=...,interface="wg0"} rosenpass_psk_rotation_age_seconds{node=...,interface="wg0|wg-remote"}
rosenpass_psk_rotation_last_change_timestamp_seconds{node=...,interface="wg0"} 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 One series set per interface: `wg0` is Link A and `wg-remote` is Link B, and an
the SHA-256 digest plus the last-change timestamp in interface absent on that host (wg-remote on siriussec, where it does not exist)
`/var/lib/rosenpass-exporter/last-hash` (root-only). When the digest changes the is skipped. `RosenpassPskRotationStalled` is interface-agnostic
timestamp advances; otherwise only the age metric grows. **Key material is (`max by (node, interface) ... > 900`), so a stalled Link B pages as soon as the
never printed, logged or persisted.** The timer's own journal therefore carries `wg-remote` series exists — which is why hashing only `wg0` left Link B
no key bytes — do not "improve" the script by echoing the listing. unobserved.
`wg0` unreadable (interface down, no `CAP_NET_ADMIN`) leaves the age metric The script hashes the output of `wg show <interface> preshared-keys` and stores
unchanged rather than resetting it; `rosenpass_unit_active` and the `absent()` only the SHA-256 digest plus the last-change timestamp in
arms of the alerts cover that case, so a broken exporter still pages. `/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 ## Verify

View file

@ -2,9 +2,9 @@
# rosenpass-metrics.sh — emit Rosenpass health metrics for node-exporter's # rosenpass-metrics.sh — emit Rosenpass health metrics for node-exporter's
# textfile collector. # textfile collector.
# #
# Installed on rpi and siriussec ONLY (the two ends of Link A: rpi wg0 # Installed on rpi and siriussec (the two ends of Link A: rpi wg0 10.10.0.1 <->
# 10.10.0.1 <-> siriussec wg0 10.10.0.2), run every 60 s by # siriussec wg0 10.10.0.2), run every 60 s by rosenpass-metrics.timer as root.
# rosenpass-metrics.timer as root. See README.md in this directory. # See README.md in this directory.
# #
# Why a host script: kube-prometheus-stack-prometheus-node-exporter is # Why a host script: kube-prometheus-stack-prometheus-node-exporter is
# distroless and ships without --collector.textfile.directory (and without the # distroless and ships without --collector.textfile.directory (and without the
@ -17,13 +17,19 @@
# 1 when `systemctl is-active rosenpass.service` # 1 when `systemctl is-active rosenpass.service`
# says active, else 0. # says active, else 0.
# rosenpass_psk_rotation_age_seconds{node,interface} # rosenpass_psk_rotation_age_seconds{node,interface}
# seconds since the wg0 preshared-key set last # seconds since that interface's preshared-key set
# changed. Rotation runs about every two minutes. # last changed. Rotation runs about every two minutes.
# rosenpass_psk_rotation_last_change_timestamp_seconds{node,interface} # rosenpass_psk_rotation_last_change_timestamp_seconds{node,interface}
# unix timestamp of that last observed change. # unix timestamp of that last observed change.
# #
# The preshared key is NEVER printed, logged or stored: the key listing is # Interfaces are emitted independently: wg0 is Link A (rpi <-> siriussec) and
# piped straight into sha256sum and only the hex digest is kept, in a # 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. # root-only state file. Do not add an `echo` of the listing while debugging.
# #
# Deliberately independent of rosenpass.service: this script must keep # Deliberately independent of rosenpass.service: this script must keep
@ -31,11 +37,12 @@
set -eu set -eu
NODE=$(hostname -s) 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 UNIT=rosenpass.service
STATE_DIR=/var/lib/rosenpass-exporter STATE_DIR=/var/lib/rosenpass-exporter
STATE_FILE=$STATE_DIR/last-hash
OUT_DIR=/var/lib/node-exporter/textfile OUT_DIR=/var/lib/node-exporter/textfile
OUT_FILE=$OUT_DIR/rosenpass.prom OUT_FILE=$OUT_DIR/rosenpass.prom
@ -52,12 +59,28 @@ if [ "$(systemctl is-active "$UNIT" 2>/dev/null || true)" = "active" ]; then
active=1 active=1
fi fi
# --- 2. PSK rotation --------------------------------------------------------- # --- 2. PSK rotation, per interface -----------------------------------------
# `wg show ... preshared-keys` fails (and prints nothing) when wg0 is absent; # `wg show ... preshared-keys` fails (and prints nothing) when the interface is
# command substitution would otherwise hash an empty string, which looks like a # absent; command substitution would otherwise hash an empty string, which looks
# change on the first poll after the interface returns. Only hash real output. # like a change on the first poll after the interface returns. Only hash real
psk_list=$(wg show "$IFACE" preshared-keys 2>/dev/null || true) # 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"
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_hash=""
last_change="" last_change=""
if [ -f "$STATE_FILE" ]; then if [ -f "$STATE_FILE" ]; then
@ -65,7 +88,6 @@ if [ -f "$STATE_FILE" ]; then
last_change=$(sed -n '2p' "$STATE_FILE") last_change=$(sed -n '2p' "$STATE_FILE")
fi fi
if [ -n "$psk_list" ]; then
hash_now=$(printf '%s' "$psk_list" | sha256sum | awk '{print $1}') hash_now=$(printf '%s' "$psk_list" | sha256sum | awk '{print $1}')
if [ "$hash_now" != "$last_hash" ]; then if [ "$hash_now" != "$last_hash" ]; then
# First observation or an actual rotation: stamp it. # First observation or an actual rotation: stamp it.
@ -73,28 +95,13 @@ if [ -n "$psk_list" ]; then
printf '%s\n%s\n' "$hash_now" "$last_change" > "$STATE_FILE.tmp" printf '%s\n%s\n' "$hash_now" "$last_change" > "$STATE_FILE.tmp"
mv "$STATE_FILE.tmp" "$STATE_FILE" mv "$STATE_FILE.tmp" "$STATE_FILE"
fi fi
elif [ -z "$last_change" ]; then
# wg unreadable and no history: nothing to compare against yet.
last_change=""
fi
# --- 3. write the textfile atomically --------------------------------------- [ -n "$last_change" ] || continue
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=$((now - last_change))
[ "$age" -ge 0 ] || age=0 [ "$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 '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" printf 'rosenpass_psk_rotation_last_change_timestamp_seconds{node="%s",interface="%s"} %s\n' "$NODE" "$IFACE" "$last_change"
fi done
} > "$tmp" } > "$tmp"
chmod 0644 "$tmp" chmod 0644 "$tmp"
mv "$tmp" "$OUT_FILE" mv "$tmp" "$OUT_FILE"