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
-62
View File
@@ -1,62 +0,0 @@
---
- name: Create JWT identity directory
ansible.builtin.file:
path: "{{ openvpn_pki_jwt_directory }}"
state: directory
owner: root
group: root
mode: "0700"
- name: Check for existing JWT private key
ansible.builtin.stat:
path: "{{ openvpn_pki_jwt_private_key_path }}"
register: openvpn_pki_jwt_private_key_stat
- name: Check for existing JWT public key
ansible.builtin.stat:
path: "{{ openvpn_pki_jwt_public_key_path }}"
register: openvpn_pki_jwt_public_key_stat
- name: Refuse incomplete JWT identity
ansible.builtin.assert:
that:
- openvpn_pki_jwt_private_key_stat.stat.exists == openvpn_pki_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_pki_jwt_private_key_path }}"
type: RSA
size: "{{ openvpn_pki_jwt_rsa_bits }}"
owner: root
group: root
mode: "0600"
when: not openvpn_pki_jwt_private_key_stat.stat.exists
- name: Generate JWT public key from existing private key
community.crypto.openssl_publickey:
path: "{{ openvpn_pki_jwt_public_key_path }}"
privatekey_path: "{{ openvpn_pki_jwt_private_key_path }}"
format: PEM
owner: root
group: root
mode: "0644"
when: not openvpn_pki_jwt_public_key_stat.stat.exists
- name: Create public key artifact directory on controller
ansible.builtin.file:
path: "{{ openvpn_pki_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_pki_jwt_public_key_path }}"
dest: "{{ openvpn_pki_public_key_artifact_directory }}/{{ inventory_hostname }}.pem"
flat: true
validate_checksum: true
@@ -0,0 +1,21 @@
---
- name: Check whether the OpenVPN server certificate exists
ansible.builtin.stat:
path: "{{ openvpn_pki_certificate_dir }}/server.crt"
register: openvpn_pki_server_certificate
- name: Issue the initial OpenVPN server certificate
ansible.builtin.command:
argv:
- /usr/bin/flock
- -n
- /run/lock/openbao-vpn-cert.lock
- "{{ openvpn_pki_script_directory }}/openbao-vpn-cert.sh"
- "{{ openvpn_pki_config_path }}"
- --skip-openvpn-restart
when: not openvpn_pki_server_certificate.stat.exists
register: openvpn_pki_initial_certificate
changed_when: "'CERTIFICATE_INSTALLED' in openvpn_pki_initial_certificate.stdout"
no_log: true
notify: Restart openvpn
+18 -2
View File
@@ -1,3 +1,19 @@
---
- name: Import JWT identity preparation tasks
ansible.builtin.import_tasks: identity.yml
- name: Install OpenVPN PKI runtime
ansible.builtin.import_tasks: runtime.yml
- name: Prepare certificate files and OpenBao configuration
ansible.builtin.import_tasks: setup.yml
- name: Install certificate lifecycle service and timer units
ansible.builtin.import_tasks: service.yml
- name: Issue certificate only when it is absent
ansible.builtin.import_tasks: initial_certificate.yml
- name: Enable and start daily certificate lifecycle timer
ansible.builtin.systemd:
name: openbao-vpn-cert.timer
enabled: true
state: started
daemon_reload: true
+29
View File
@@ -0,0 +1,29 @@
---
- name: Install OpenBao CLI
ansible.builtin.import_role:
name: openbao_cli
- name: Install OpenVPN PKI runtime dependencies
ansible.builtin.apt:
name:
- openssl
- jq
- python3
- util-linux
state: present
update_cache: true
cache_valid_time: 3600
- name: Calculate deployed company VPN Root CA fingerprint
ansible.builtin.command:
argv:
- openssl
- x509
- -in
- "{{ openvpn_pki_deployed_ca_source }}"
- -noout
- -fingerprint
- -sha256
register: openvpn_pki_company_ca_fingerprint
changed_when: false
- name: Normalize deployed company VPN Root CA fingerprint
ansible.builtin.set_fact:
openvpn_pki_company_ca_fingerprint_value: "{{ openvpn_pki_company_ca_fingerprint.stdout | regex_replace('^.*=', '') | replace(':', '') | upper }}"
+24
View File
@@ -0,0 +1,24 @@
---
- name: Deploy lifecycle script configuration
ansible.builtin.template:
src: openbao-vpn.conf.j2
dest: "{{ openvpn_pki_config_path }}"
owner: root
group: root
mode: "0600"
- name: Deploy certificate lifecycle service
ansible.builtin.template:
src: openbao-vpn-cert.service.j2
dest: /etc/systemd/system/openbao-vpn-cert.service
owner: root
group: root
mode: "0644"
- name: Deploy daily certificate lifecycle timer
ansible.builtin.template:
src: openbao-vpn-cert.timer.j2
dest: /etc/systemd/system/openbao-vpn-cert.timer
owner: root
group: root
mode: "0644"
+62
View File
@@ -0,0 +1,62 @@
---
- name: Create OpenVPN PKI directories
ansible.builtin.file:
path: "{{ item.path }}"
state: directory
owner: root
group: root
mode: "{{ item.mode }}"
loop:
- { path: "{{ openvpn_pki_certificate_dir }}", mode: "0755" }
- { path: "{{ openvpn_pki_working_directory }}", mode: "0700" }
- { path: "{{ openvpn_pki_script_directory }}", mode: "0755" }
- { path: "{{ openvpn_pki_config_path | dirname }}", mode: "0755" }
- name: Require certificate staging directory on certificate filesystem
ansible.builtin.assert:
that:
- openvpn_pki_working_directory == openvpn_pki_certificate_dir or openvpn_pki_working_directory.startswith(openvpn_pki_certificate_dir ~ '/')
fail_msg: >-
openvpn_pki_working_directory must be inside openvpn_pki_certificate_dir
so certificate and key replacement stays on one filesystem.
- name: Install OpenBao API CA certificate
ansible.builtin.copy:
src: certs/openbao-ca.crt
dest: "{{ openvpn_pki_bao_cacert }}"
owner: root
group: root
mode: "0644"
- name: Validate OpenBao API CA certificate
ansible.builtin.command:
argv:
- openssl
- x509
- -in
- "{{ openvpn_pki_bao_cacert }}"
- -noout
- -checkend
- "0"
changed_when: false
- name: Deploy OpenVPN certificate lifecycle script
ansible.builtin.copy:
src: scripts/openbao-vpn-cert.sh
dest: "{{ openvpn_pki_script_directory }}/openbao-vpn-cert.sh"
owner: root
group: root
mode: "0750"
- name: Deploy PEM chain helper
ansible.builtin.copy:
src: scripts/split-pem-chain.py
dest: "{{ openvpn_pki_script_directory }}/split-pem-chain.py"
owner: root
group: root
mode: "0750"