Skip to content

Lesson 8 of 9 · 24m

Automating Enrollment, Configuration, and Key Lifecycle

Build a reliable pipeline for device enrollment, peer generation, config distribution, revocation, rotation, and drift detection.

WireGuard configuration is simple enough that teams start with copy-paste. That simplicity becomes a liability at fleet scale because nothing in the protocol knows your inventory, offboarding process, naming conventions, or intended topology. Automation is not about generating more config faster. It is about making access lifecycle boring: enroll, approve, distribute, rotate, revoke, verify.

Model desired state separately from node config

The source of truth should describe identities and policy:

devices:
  - name: traven-framework-13
    owner: traven
    groups: [sre, infrastructure]
    public_key: q9fN...device-forums...=
    addresses: [10.44.1.37/32]
    expires: 2026-12-15T00:00:00Z
gateways:
  - name: prod-gw-1
    public_key: gwpublic...=
    endpoint: prod-gw-1.sudosecurity.org:51820
    serves: [10.20.0.0/16]

Render per-node configs from that inventory. Never edit production peer blocks by hand in ten places and hope they agree.

Key generation belongs at the edge

The device should generate its private key locally and submit only the public key during enrollment. The enrollment flow should bind the public key to a user, device inventory record, approval state, and expiry. If users can email keys around, your inventory will become fiction. A simple client-side bootstrap shape:

umask 077
wg genkey | tee /etc/wireguard/wg0-private.key | wg pubkey   | curl -fsS -X POST https://access.sudosecurity.org/enroll       -H "authorization: Bearer $DEVICE_ENROLL_TOKEN"       -H 'content-type: text/plain' --data-binary @-

The real service needs authentication, approval, audit, rate limiting, and idempotency. Do not accept unauthenticated public keys because the onboarding spreadsheet is convenient.

Render configs atomically and test them

Configuration management should write to a temporary file, validate syntax, compare semantic diff, then activate:

wg-quick strip /etc/wireguard/wg0.conf > /run/wg0.rendered
wg syncconf wg0 /run/wg0.rendered
systemctl reload-or-restart wg-quick@wg0 || systemctl restart wg-quick@wg0

syncconf avoids tearing down unaffected peers when only parts of the config changed. Still test your exact operating system and service unit behavior; reloading network configuration is rarely as graceful as documentation implies.

Revocation is a deployment pipeline test

When a laptop is lost or an employee leaves, remove the peer from every relevant gateway and verify no new handshake appears. Alert if a revoked key successfully handshakes. Rotation should follow the same path as initial enrollment, otherwise rotation becomes a manual exception and will quietly stop happening.

Detect drift and dead peers

Compare live wg show dump output with inventory. Look for unexpected public keys, stale endpoints, peers with no recent handshake, and addresses present in config but not in inventory. Drift is where unauthorized access hides after years of "temporary" exceptions.

Field notes

  • Put expiry in the enrollment system even if WireGuard itself does not enforce user expiration.
  • Backup rendered policy and inventory, not private keys, unless a documented escrow design truly requires otherwise.
  • Idempotency matters because enrollment clients retry on flaky networks.
  • Automation without ownership becomes another unreviewed control plane; name the team that can approve policy changes.

Was this lesson helpful?

Sign in to rate