Files
algo/server.yml
T
a7b9b61265 Fix update-users: DNS IP, reboots, key regeneration, and local permissions (#14883)
* Fix DNS IP changing after running update-users

The local_service_ip variable is generated using a deterministic random
formula seeded by algo_server_name + ansible_fqdn. While algo_server_name
is persisted in .config.yml, ansible_fqdn is a runtime fact gathered from
the target host that can change if:

- Server's hostname configuration changes
- DNS resolution changes
- User runs ansible from a different control machine

When ansible_fqdn differs between initial deployment and users.yml, a
different local_service_ip is generated, causing new client configs to
point to a DNS IP that doesn't exist on the server.

This fix persists local_service_ip and local_service_ipv6 in .config.yml
during initial deployment, ensuring users.yml uses the correct DNS IP.

Fixes #14614

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Skip apt upgrade and reboot during update-users

Previously, running `./algo update-users` would trigger a full apt upgrade
and potentially reboot the server because ubuntu.yml was tagged with
`update-users`. This caused unexpected disconnections for all VPN clients
(gaming, downloads, etc.) during what should be a simple user management
operation.

Now only facts.yml runs during update-users, which provides the necessary
password facts for IPsec certificate generation without running apt upgrade
or triggering reboots.

Fixes #14518

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Add keys_clean_all support to WireGuard (parity with IPsec)

Previously, WireGuard had no option to force credential regeneration for
existing users. The keys_clean_all option only affected IPsec certificates.

Now WireGuard respects keys_clean_all the same way IPsec does:
- keys_clean_all: false (default) - preserve existing keys, only generate
  for new users
- keys_clean_all: true - delete all keys and regenerate for all users

Also improved the config.cfg documentation to clarify this option affects
both WireGuard and IPsec credentials.

Fixes #14610

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Add permission check for local deployment update-users

For local deployments, file ownership must be consistent between initial
deployment and subsequent update-users runs. When there's a mismatch
(e.g., initial deployment without sudo, update with sudo), files get
mixed ownership causing permission errors.

This adds a pre-flight check that:
- Detects local deployments (localhost or algo_provider: local)
- Compares config directory owner with current user
- Displays a warning with guidance if there's a mismatch
- Provides the exact chown command to fix permissions

Addresses #14551

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Address review feedback: explicit directory modes and clearer docs

- Add explicit mode to WireGuard directory creation in main.yml:
  - PKI directories (preshared, private, public): 0700
  - Config directories (apple/ios, apple/macos): 0755

- Enhance config.cfg keys_clean_all comment to clarify:
  - When false: new users added (not just preserved)
  - When true: ALL CLIENTS MUST RECONFIGURE (explicit impact warning)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Address review: fail on permission mismatch, clean up directory creation

1. Permission check now FAILS instead of warning
   - Prevents continuing with mismatched permissions
   - Provides clear fix command: sudo chown -R <user> configs/<server>/
   - Simpler condition: just compare owner with current user

2. Clean separation of directory creation
   - main.yml: Only config directories (apple/ios, apple/macos) with 0755
   - keys.yml: Only PKI directories (preshared, private, public) with 0700
   - Eliminates duplication and clarifies responsibility

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Address review: add empty users validation and improve docs

1. Add empty users list validation
   - Fails early with clear message if no users defined in config.cfg
   - Prevents confusing downstream errors

2. Improve config.cfg keys_clean_all documentation
   - Add example use cases: key compromise, removing untrusted users, security audit

3. Rename block for clarity
   - "Check local deployment permissions" → "Local deployment permission validation"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix jinja2 spacing lint warning

Remove extra spaces inside brackets in Jinja2 expression per ansible-lint
jinja[spacing] rule.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-28 01:19:36 -05:00

244 lines
9.3 KiB
YAML

---
- name: Configure the server and install required software
hosts: vpn-host
gather_facts: false
become: true
vars_files:
- config.cfg
tasks:
- block:
- name: Wait until the cloud-init completed
wait_for:
path: /var/lib/cloud/data/result.json
delay: 10 # Conservative 10 second initial delay
timeout: 480 # Reduce from 600 to 480 seconds (8 minutes)
sleep: 10 # Check every 10 seconds (less aggressive)
state: present
become: false
when: cloudinit
- block:
- name: Ensure the config directory exists
file:
dest: configs/{{ IP_subject_alt_name }}
state: directory
mode: "0700"
- name: Dump the ssh config
copy:
dest: configs/{{ IP_subject_alt_name }}/ssh_config
mode: "0600"
content: |
Host {{ IP_subject_alt_name }} {{ algo_server_name }}
HostName {{ IP_subject_alt_name }}
User {{ ansible_ssh_user }}
Port {{ ansible_ssh_port }}
IdentitiesOnly yes
IdentityFile {{ SSH_keys.private | realpath }}
KeepAlive yes
ServerAliveInterval 30
when: inventory_hostname != 'localhost'
become: false
delegate_to: localhost
- import_role:
name: common
tags: common
# Configure VPN services (parallel when performance_parallel_services enabled)
- block:
- name: Start DNS service configuration
import_role:
name: dns
async: 300
poll: 0
register: dns_job
when: algo_dns_adblocking or dns_encryption
tags: dns
- name: Start WireGuard service configuration
import_role:
name: wireguard
async: 300
poll: 0
register: wireguard_job
when: wireguard_enabled
tags: wireguard
- name: Start StrongSwan service configuration
import_role:
name: strongswan
async: 300
poll: 0
register: strongswan_job
when: ipsec_enabled
tags: ipsec
- name: Start SSH tunneling service configuration
import_role:
name: ssh_tunneling
async: 300
poll: 0
register: ssh_tunneling_job
when: algo_ssh_tunneling
tags: ssh_tunneling
- name: Wait for DNS service configuration to complete
async_status:
jid: "{{ dns_job.ansible_job_id }}"
register: dns_result
until: dns_result.finished
retries: 60
delay: 5
when: dns_job.ansible_job_id is defined
tags: dns
- name: Wait for WireGuard service configuration to complete
async_status:
jid: "{{ wireguard_job.ansible_job_id }}"
register: wireguard_result
until: wireguard_result.finished
retries: 60
delay: 5
when: wireguard_job.ansible_job_id is defined
tags: wireguard
- name: Wait for StrongSwan service configuration to complete
async_status:
jid: "{{ strongswan_job.ansible_job_id }}"
register: strongswan_result
until: strongswan_result.finished
retries: 60
delay: 5
when: strongswan_job.ansible_job_id is defined
tags: ipsec
- name: Wait for SSH tunneling service configuration to complete
async_status:
jid: "{{ ssh_tunneling_job.ansible_job_id }}"
register: ssh_tunneling_result
until: ssh_tunneling_result.finished
retries: 60
delay: 5
when: ssh_tunneling_job.ansible_job_id is defined
tags: ssh_tunneling
- name: Display VPN service completion status
debug:
msg: |
VPN Service Status Summary (Parallel Mode):
DNS: {{ 'COMPLETED' if (dns_result.rc | default(-1)) == 0 else 'FAILED' if dns_result.rc is defined else 'SKIPPED' }}
WireGuard: {{ 'COMPLETED' if (wireguard_result.rc | default(-1)) == 0 else 'FAILED' if wireguard_result.rc is defined else 'SKIPPED' }}
StrongSwan: {{ 'COMPLETED' if (strongswan_result.rc | default(-1)) == 0 else 'FAILED' if strongswan_result.rc is defined else 'SKIPPED' }}
SSH Tunneling: >-
{{ 'COMPLETED' if (ssh_tunneling_result.rc | default(-1)) == 0
else 'FAILED' if ssh_tunneling_result.rc is defined else 'SKIPPED' }}
tags: vpn_services
- name: Check for any VPN service failures
fail:
msg: |
One or more VPN services failed to configure properly.
Please check the detailed error messages above.
when: >
(dns_result.rc is defined and dns_result.rc != 0) or
(wireguard_result.rc is defined and wireguard_result.rc != 0) or
(strongswan_result.rc is defined and strongswan_result.rc != 0) or
(ssh_tunneling_result.rc is defined and ssh_tunneling_result.rc != 0)
tags: vpn_services
when: performance_parallel_services | default(true)
# Sequential service configuration (fallback)
- import_role:
name: dns
when:
- not (performance_parallel_services | default(true))
- algo_dns_adblocking or dns_encryption
tags: dns
- import_role:
name: wireguard
when:
- not (performance_parallel_services | default(true))
- wireguard_enabled
tags: wireguard
- import_role:
name: strongswan
when:
- not (performance_parallel_services | default(true))
- ipsec_enabled
tags: ipsec
- import_role:
name: ssh_tunneling
when:
- not (performance_parallel_services | default(true))
- algo_ssh_tunneling
tags: ssh_tunneling
- import_role:
name: privacy
when: privacy_enhancements_enabled | default(true)
tags: privacy
- block:
- name: Dump the configuration
copy:
dest: configs/{{ IP_subject_alt_name }}/.config.yml
mode: '0644'
content: |
server: {{ 'localhost' if inventory_hostname == 'localhost' else inventory_hostname }}
server_user: {{ ansible_ssh_user }}
ansible_ssh_port: "{{ ansible_ssh_port | default(22) }}"
{% if algo_provider != "local" %}
ansible_ssh_private_key_file: {{ SSH_keys.private }}
{% endif %}
algo_provider: {{ algo_provider }}
algo_server_name: {{ algo_server_name }}
algo_ondemand_cellular: {{ algo_ondemand_cellular }}
algo_ondemand_wifi: {{ algo_ondemand_wifi }}
algo_ondemand_wifi_exclude: {{ algo_ondemand_wifi_exclude }}
algo_dns_adblocking: {{ algo_dns_adblocking }}
algo_ssh_tunneling: {{ algo_ssh_tunneling }}
algo_store_pki: {{ algo_store_pki }}
IP_subject_alt_name: {{ IP_subject_alt_name }}
ipsec_enabled: {{ ipsec_enabled }}
wireguard_enabled: {{ wireguard_enabled }}
local_service_ip: {{ local_service_ip }}
local_service_ipv6: {{ local_service_ipv6 }}
{% if tests | default(false) | bool %}
ca_password: '{{ CA_password }}'
p12_password: '{{ p12_export_password }}'
{% endif %}
become: false
delegate_to: localhost
- name: Create a symlink if deploying to localhost
file:
src: "{{ IP_subject_alt_name }}"
dest: configs/localhost
state: link
force: true
when: inventory_hostname == 'localhost'
- name: Import tmpfs tasks
import_tasks: playbooks/tmpfs/umount.yml
become: false
delegate_to: localhost
vars:
facts: "{{ hostvars['localhost'] }}"
when:
- pki_in_tmpfs
- not algo_store_pki
- debug:
msg:
- "{{ congrats.common.split('\n') }}"
- " {{ congrats.p12_pass if algo_ssh_tunneling or ipsec_enabled else '' }}"
- " {{ congrats.ca_key_pass if algo_store_pki and ipsec_enabled else '' }}"
- " {{ congrats.ssh_access if algo_provider != 'local' else '' }}"
tags: always
rescue:
- include_tasks: playbooks/rescue.yml