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

99 lines
3.6 KiB
YAML

---
# Part of role: pia-gateway
# Included by: tasks/main.yaml (before registration — the shared
# registration backend is this program)
# Description: Installs the on-host controller (files/pia_gateway.py), its
# root-only runtime config and credentials, and the systemd units for
# the recovery / renewal timers. Timers are only ENABLED at the end of
# tasks/main.yaml, after the kill switch, interface and a verified
# registration exist — this file never starts them.
- name: Create the controller config directory (root-only)
ansible.builtin.file:
path: "{{ pia_gateway_etc_dir }}"
state: directory
mode: "0700"
owner: root
group: root
- name: Create the controller state directory (root-only)
ansible.builtin.file:
path: "{{ pia_gateway_state_dir }}"
state: directory
mode: "0700"
owner: root
group: root
- name: Install the pia-gateway program
ansible.builtin.copy:
src: pia_gateway.py
dest: "{{ pia_gateway_program }}"
mode: "0700"
owner: root
group: root
- name: Render the controller runtime config
ansible.builtin.template:
src: pia-gateway-config.json.j2
dest: "{{ pia_gateway_etc_dir }}/config.json"
mode: "0600"
owner: root
group: root
register: pia_gateway_config_result
- name: Validate the rendered config loads
ansible.builtin.command:
cmd: "{{ pia_gateway_program }} --config {{ pia_gateway_etc_dir }}/config.json status"
register: pia_gateway_config_check
changed_when: false
failed_when: pia_gateway_config_check.rc not in [0, 1, 2]
# `status`'s own source (cmd_status in files/pia_gateway.py) only ever
# returns one of three documented exits: 0 (healthy), 1 (unhealthy —
# the expected result on a fresh install, since the kill switch and
# pia-wg interface don't exist yet at this point in the run), or 2
# (state file corrupt — not a config problem either). Allowlisting
# those three and failing on EVERYTHING else (5 = bad config JSON/
# unknown key from main()'s own Config.load(), 3/4 = exits this
# command cannot legitimately return, 126/127/a crash) means a real
# config regression is never masked by matching only one "bad" code —
# the previous version of this task only failed on rc==5 and silently
# ignored any other unexpected exit.
- name: Provision PIA credentials for unattended re-registration (root-only)
ansible.builtin.template:
src: pia-gateway-credentials.env.j2
dest: "{{ pia_gateway_etc_dir }}/credentials.env"
mode: "0600"
owner: root
group: root
no_log: true
when: not (pia_remove_host_credentials | bool)
# Only PIA_USER/PIA_PASSWORD (and the alert-bridge key when set) — never
# the whole .env, the vault password, or any Kubernetes secret. This is
# the deliberate availability/security tradeoff documented in the README:
# a host that must re-register while the laptop is off needs the account
# secret locally.
- name: Remove the on-host PIA credentials (explicit request only)
ansible.builtin.file:
path: "{{ pia_gateway_etc_dir }}/credentials.env"
state: absent
when: pia_remove_host_credentials | bool
- name: Deploy controller systemd units
ansible.builtin.template:
src: "{{ item }}.j2"
dest: "/etc/systemd/system/{{ item }}"
mode: "0644"
owner: root
group: root
loop:
- pia-gateway-recover.service
- pia-gateway-recover.timer
- pia-gateway-renew.service
- pia-gateway-renew.timer
- pia-gateway-onfailure.service
notify: Reload systemd daemon
# The reload runs at the first flush_handlers in main.yaml, before any
# of these units is enabled; the enable tasks also pass daemon_reload.