refactor(vpn): split JWT identity and OpenVPN certificate lifecycle
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

- 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
This commit is contained in:
2026-11-27 17:58:46 +01:00
parent 36d2845a3a
commit 4b8e7f3592
35 changed files with 1322 additions and 303 deletions
+52
View File
@@ -0,0 +1,52 @@
---
- name: Create JWT identity directory
ansible.builtin.file:
path: "{{ openvpn_jwt_directory }}"
state: directory
owner: root
group: root
mode: "0700"
- name: Deploy JWT generator script
ansible.builtin.template:
src: generate-vault-jwt.py.j2
dest: "{{ openvpn_jwt_generator_script }}"
owner: root
group: root
mode: "0750"
- name: Check JWT generator script
ansible.builtin.stat:
path: "{{ openvpn_jwt_generator_script }}"
register: openvpn_jwt_generator_stat
- name: Require executable JWT generator
ansible.builtin.assert:
that:
- openvpn_jwt_generator_stat.stat.isreg | default(false)
- openvpn_jwt_generator_stat.stat.executable | default(false)
fail_msg: >-
JWT generator is missing or not executable:
{{ openvpn_jwt_generator_script }}
- name: Check JWT Python interpreter
ansible.builtin.stat:
path: "{{ openvpn_jwt_python }}"
register: openvpn_jwt_python_stat
- name: Require JWT Python interpreter
ansible.builtin.assert:
that:
- openvpn_jwt_python_stat.stat.exists
- openvpn_jwt_python_stat.stat.executable | default(false)
fail_msg: >-
JWT Python interpreter is missing or not executable:
{{ openvpn_jwt_python }}
- name: Validate JWT Python dependencies
ansible.builtin.command:
argv:
- "{{ openvpn_jwt_python }}"
- -c
- "import jwt; import cryptography; assert hasattr(jwt, 'encode')"
changed_when: false
+154
View File
@@ -0,0 +1,154 @@
---
- 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
+13
View File
@@ -0,0 +1,13 @@
---
- name: Provision the JWT Python environment
ansible.builtin.import_tasks: python_environment.yml
- name: Configure the JWT generator and runtime
ansible.builtin.import_tasks: generator.yml
- name: Create or validate the local JWT key pair
ansible.builtin.import_tasks: keys.yml
- name: Export the JWT public key for registration
ansible.builtin.import_tasks: public_key.yml
when: openvpn_jwt_export_public_key | bool
+15
View File
@@ -0,0 +1,15 @@
---
- name: Create public key artifact directory on controller
ansible.builtin.file:
path: "{{ openvpn_jwt_public_key_artifact_directory }}"
state: directory
mode: "0700"
delegate_to: localhost
become: false
- name: Fetch JWT public key to controller
ansible.builtin.fetch:
src: "{{ openvpn_jwt_public_key_path }}"
dest: "{{ openvpn_jwt_public_key_artifact_directory }}/{{ inventory_hostname }}.pem"
flat: true
validate_checksum: true
@@ -0,0 +1,14 @@
---
- name: Install Python virtual environment support
ansible.builtin.apt:
name: python3-venv
state: present
update_cache: true
cache_valid_time: 3600
- name: Install PyJWT and cryptography in the JWT virtual environment
ansible.builtin.pip:
name: "PyJWT[crypto]=={{ openvpn_jwt_pyjwt_version }}"
virtualenv: "{{ openvpn_jwt_virtualenv }}"
virtualenv_command: /usr/bin/python3 -m venv
state: present