Files
vpn-node-config/roles/openvpn_jwt/tasks/keys.yml
T
gitea_admin 4b8e7f3592
OpenVPN Ansible / Ansible Syntax Check (push) Successful in 43s
OpenVPN Ansible / Ansible Lint (push) Successful in 1m8s
OpenVPN Ansible / Deploy VPN Networking and OpenVPN (push) Successful in 3m32s
OpenVPN Ansible / Generate SBOM (push) Successful in 55s
OpenVPN Ansible / Scan SBOM with Trivy (push) Successful in 18s
refactor(vpn): split JWT identity and OpenVPN certificate lifecycle
- Add a dedicated role and workflow to generate VPN JWT identities and export public keys
- Run identity preparation automatically for VPN nodes awaiting OpenBao registration
- Gate OpenVPN and PKI setup until each node is registered
- Split PKI setup into runtime, configuration, service, and initial certificate tasks
- Restore TAP and bridge attachment after OpenVPN restarts
- Remove bundled server certificate and private key
2026-11-27 17:58:46 +01:00

155 lines
4.9 KiB
YAML

---
- name: Check for existing JWT private key
ansible.builtin.stat:
path: "{{ openvpn_jwt_private_key_path }}"
register: openvpn_jwt_private_key_stat
- name: Check for existing JWT public key
ansible.builtin.stat:
path: "{{ openvpn_jwt_public_key_path }}"
register: openvpn_jwt_public_key_stat
- name: Refuse incomplete JWT identity
ansible.builtin.assert:
that:
- openvpn_jwt_private_key_stat.stat.exists == openvpn_jwt_public_key_stat.stat.exists
fail_msg: >-
Only one JWT key file exists. Refusing to replace or repair an existing
identity automatically. Inspect the files and recover the matching key
pair deliberately before rerunning this playbook.
- name: Generate JWT RSA private key
community.crypto.openssl_privatekey:
path: "{{ openvpn_jwt_private_key_path }}"
type: RSA
size: "{{ openvpn_jwt_rsa_bits }}"
owner: root
group: root
mode: "0600"
when: not openvpn_jwt_private_key_stat.stat.exists
- name: Generate JWT public key from existing private key
community.crypto.openssl_publickey:
path: "{{ openvpn_jwt_public_key_path }}"
privatekey_path: "{{ openvpn_jwt_private_key_path }}"
format: PEM
owner: root
group: root
mode: "0644"
when: not openvpn_jwt_public_key_stat.stat.exists
- name: Validate existing JWT private key
ansible.builtin.command:
argv:
- openssl
- pkey
- -in
- "{{ openvpn_jwt_private_key_path }}"
- -check
- -noout
register: openvpn_jwt_private_key_validation
changed_when: false
failed_when: false
when: openvpn_jwt_private_key_stat.stat.exists
- name: Validate existing JWT public key
ansible.builtin.command:
argv:
- openssl
- pkey
- -pubin
- -in
- "{{ openvpn_jwt_public_key_path }}"
- -pubcheck
- -noout
register: openvpn_jwt_public_key_validation
changed_when: false
failed_when: false
when: openvpn_jwt_public_key_stat.stat.exists
- name: Fail clearly for invalid existing JWT identity keys
ansible.builtin.assert:
that:
- openvpn_jwt_private_key_validation.rc == 0
- openvpn_jwt_public_key_validation.rc == 0
fail_msg: Existing JWT identity contains an invalid private or public key. The identity was not replaced.
when: openvpn_jwt_private_key_stat.stat.exists
- name: Read existing JWT private key type and size
ansible.builtin.command:
argv: [openssl, pkey, -in, "{{ openvpn_jwt_private_key_path }}", -text, -noout]
register: openvpn_jwt_private_key_details
changed_when: false
when: openvpn_jwt_private_key_stat.stat.exists
no_log: true
- name: Read existing JWT public key type and size
ansible.builtin.command:
argv: [openssl, pkey, -pubin, -in, "{{ openvpn_jwt_public_key_path }}", -text, -noout]
register: openvpn_jwt_public_key_details
changed_when: false
when: openvpn_jwt_public_key_stat.stat.exists
no_log: true
- name: Require existing JWT identity to be configured-size RSA
ansible.builtin.assert:
that:
- "('Private-Key: (' ~ (openvpn_jwt_rsa_bits | string) ~ ' bit') in openvpn_jwt_private_key_details.stdout"
- "('Public-Key: (' ~ (openvpn_jwt_rsa_bits | string) ~ ' bit') in openvpn_jwt_public_key_details.stdout"
- "'modulus:' in openvpn_jwt_private_key_details.stdout"
- "'Modulus:' in openvpn_jwt_public_key_details.stdout"
fail_msg: Existing JWT identity must be an RSA key pair of the configured size. The identity was not replaced.
when: openvpn_jwt_private_key_stat.stat.exists
no_log: true
- name: Derive public key from existing JWT private key
ansible.builtin.command:
argv:
- openssl
- pkey
- -in
- "{{ openvpn_jwt_private_key_path }}"
- -pubout
register: openvpn_jwt_derived_public_key
changed_when: false
when: openvpn_jwt_private_key_stat.stat.exists
- name: Read stored JWT public key
ansible.builtin.command:
argv:
- openssl
- pkey
- -pubin
- -in
- "{{ openvpn_jwt_public_key_path }}"
- -pubout
register: openvpn_jwt_stored_public_key
changed_when: false
when: openvpn_jwt_public_key_stat.stat.exists
no_log: true
- name: Refuse mismatched existing JWT identity
ansible.builtin.assert:
that:
- openvpn_jwt_derived_public_key.stdout == openvpn_jwt_stored_public_key.stdout
fail_msg: Existing JWT public key does not match its private key. The identity was not replaced.
when: openvpn_jwt_private_key_stat.stat.exists
- name: Enforce JWT private key ownership and permissions
ansible.builtin.file:
path: "{{ openvpn_jwt_private_key_path }}"
state: file
owner: root
group: root
mode: "0600"
when: openvpn_jwt_private_key_stat.stat.exists
- name: Enforce JWT public key ownership and permissions
ansible.builtin.file:
path: "{{ openvpn_jwt_public_key_path }}"
state: file
owner: root
group: root
mode: "0644"
when: openvpn_jwt_public_key_stat.stat.exists