homelab/ansible/roles/pia-gateway/tasks/register-shared.yaml
Nik Afiq 87a9303cc3
Some checks failed
validate / lint (push) Failing after 2s
fix: address code review on pia-gateway auto-recovery controller
- Serialize every mutating command (recover, renew, register,
  rewrite-config, pause, resume, state-reset) on the shared lock,
  acquired BEFORE state is read rather than after: reading first and
  locking only around the write let a slower command silently clobber
  whatever a faster one wrote in between — in particular, an operator
  pause could be lost to an in-flight recovery pass that read state
  before the pause ran and only saved its own copy back afterwards.
- Fix emergency_close() to reposition its VLAN 50 DROP to the first
  effective FORWARD position when it exists but has been pushed down
  by something else (e.g. an out-of-band ufw reload) — a bare `-C`
  existence check said nothing about position.
- Tighten wait_for_verified()'s handshake freshness check from >= to >
  since_epoch: wg show and since_epoch are both whole seconds, so a
  handshake at exactly the same second as since_epoch is
  indistinguishable from a stale, pre-activation one and must not
  count as proof of a new one.
- Fix the Ansible config-validation task to fail on any exit code
  outside status's three documented health exits (0/1/2), not just on
  5 — verified live against the real program (no wg/ip/iptables on
  this dev machine reproduces the "nothing configured yet" first-run
  case; status correctly returns 1, and broken/missing config.json
  correctly returns 5 and is now caught).
- Correct the live fault-injection drill in the role README: port
  1337 is only the addKey registration API port, not the tunnel's own
  WireGuard UDP endpoint (derive that from `wg show pia-wg endpoints`
  instead), and make the packet-capture check direction-aware
  (`tcpdump -Q out`) so it can't conflate a real egress leak with
  VLAN 50's normal inbound arrival on enp1s0.

Added 6 tests (86 total, all passing): emergency-close repositioning
past a preceding ACCEPT, pause/resume blocking on an in-flight
lock holder and surviving into a subsequent recovery pass, and the
handshake boundary at exactly/one-second-either-side of since_epoch.

yamllint, ansible-playbook --syntax-check, and ansible-lint (diffed
against main — no new finding categories) all pass. No live changes;
nothing pushed to main.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 15:51:02 +09:00

65 lines
3.0 KiB
YAML

---
# Part of role: pia-gateway
# Included by: tasks/main.yaml when pia_registration_backend == "shared"
# Description: Registers with PIA through the on-host controller
# (`pia-gateway register`), i.e. the same token/addKey/stage/activate/
# verify transaction the unattended recovery and renewal timers use.
# Runs when there is no config yet, no VERIFIED registration baseline in
# the controller's state, or an explicit force flag. Reports success only
# after a fresh WireGuard handshake was observed on the activated config
# — never on `addKey` HTTP status alone.
#
# Requires the kill switch to be active (tasks/routing.yaml runs first in
# main.yaml); the program refuses to activate otherwise (exit 3) and the
# play fails loudly rather than opening anything.
- name: Check whether a PIA WireGuard config already exists
ansible.builtin.stat:
path: "{{ pia_wg_config_dir }}/{{ pia_wg_interface }}.conf"
register: pia_wg_conf_stat
- name: Check whether the controller has a verified registration baseline
ansible.builtin.stat:
path: "{{ pia_gateway_state_dir }}/state.json"
register: pia_state_stat
- name: Read the controller state (no secrets are stored in it)
ansible.builtin.slurp:
src: "{{ pia_gateway_state_dir }}/state.json"
register: pia_state_raw
when: pia_state_stat.stat.exists
- name: Decide whether registration is needed this run
ansible.builtin.set_fact:
pia_has_baseline: >-
{{ pia_state_stat.stat.exists
and ((pia_state_raw.content | b64decode | from_json).last_verified_registration is not none) }}
pia_registration_mode: "{{ 'new-key' if (pia_force_key_rotation | bool) else 'same-key' }}"
pia_region_args: "{{ ['--region', pia_region] if (pia_region is not none and (pia_region | string | trim | length) > 0) else [] }}"
- name: Run the shared PIA registration transaction
ansible.builtin.command:
argv: "{{ [pia_gateway_program, 'register', '--via', 'ansible', '--mode', pia_registration_mode] + pia_region_args }}"
register: pia_register_result
changed_when: true
failed_when: pia_register_result.rc != 0
when: >-
(pia_force_reregister | bool)
or (pia_force_key_rotation | bool)
or not pia_wg_conf_stat.stat.exists
or not (pia_has_baseline | bool)
# argv form: no shell, no secrets in arguments (credentials are read from
# the root-only file by the program itself), so this task is deliberately
# NOT no_log — its stdout is the secret-free JSON summary and the
# per-candidate diagnostics, which are exactly what an operator needs
# when a run fails. The lock is taken inside the program (waits up to
# pia_recovery_lock_wait_sec for an in-flight timer run to finish).
- name: Record that registration happened this run
ansible.builtin.set_fact:
pia_registered_this_run: "{{ pia_register_result is not skipped }}"
- name: Show the registration summary
ansible.builtin.debug:
msg: "{{ pia_register_result.stdout_lines | default(['skipped — config and verified baseline already present']) }}"