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
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:
@@ -0,0 +1,54 @@
|
||||
# OpenVPN JWT identity
|
||||
|
||||
This role manages each VPN node's local RSA identity and JWT generator. It is
|
||||
separate from `openvpn_pki`, which manages the OpenVPN server certificate.
|
||||
|
||||
`tasks/main.yml` delegates to three focused task files:
|
||||
|
||||
- `generator.yml` installs the configured generator and checks its Python
|
||||
interpreter and dependencies.
|
||||
- `keys.yml` creates or validates the RSA private/public key pair.
|
||||
- `public_key.yml` fetches only the public key to the controller.
|
||||
|
||||
Run the dedicated identity playbook for selected nodes:
|
||||
|
||||
```bash
|
||||
ansible-playbook \
|
||||
-i inventories/prod/hosts.ini \
|
||||
playbooks/openvpn-jwt-identity.yml \
|
||||
--limit vpn01-test
|
||||
```
|
||||
|
||||
The Gitea workflow `.gitea/workflows/openvpn-jwt-identity.yml` runs after the
|
||||
main OpenVPN workflow succeeds. It selects every
|
||||
`vpn_nodes` host whose
|
||||
`openvpn_jwt_registered` value is false or missing. It uploads the resulting
|
||||
PEM files as the `openvpn-jwt-public-keys` artifact. An administrator adds
|
||||
each new public key to OpenBao's existing trusted-key bundle while preserving
|
||||
the other keys. After registration is confirmed, set that host's
|
||||
`openvpn_jwt_registered` value to `true`; the regular OpenVPN deployment can
|
||||
then proceed for that VM.
|
||||
|
||||
The regular `openvpn.yml` deployment skips the `openvpn`, `openvpn_jwt`, and
|
||||
`openvpn_pki` roles when the flag is false or absent. Shared base roles still
|
||||
run.
|
||||
|
||||
The private key remains on the VPN node at `/etc/vault-jwt/private.pem` by
|
||||
default, with root ownership and mode `0600`.
|
||||
Only the public key is copied to
|
||||
`artifacts/openvpn-jwt-public-keys/<inventory_hostname>.pem` for manual
|
||||
registration with OpenBao. The role does not configure OpenBao.
|
||||
|
||||
Existing keys must be a matching RSA pair at the configured size. The role
|
||||
fails without replacing an incomplete, invalid, wrong-size, or mismatched
|
||||
identity.
|
||||
|
||||
Install the collections in `requirements.yml`, including
|
||||
`community.crypto`, on the Ansible controller before running this role.
|
||||
|
||||
The role creates the Python virtual environment and installs the pinned
|
||||
`PyJWT[crypto]` dependency. Override `openvpn_jwt_virtualenv` to change the
|
||||
environment location. If you also override `openvpn_jwt_python`, point it to
|
||||
an interpreter inside that same environment.
|
||||
`openvpn_jwt_role` and `openvpn_jwt_generator_args` configure the generator
|
||||
invocation used by the certificate lifecycle script.
|
||||
@@ -0,0 +1,15 @@
|
||||
openvpn_jwt_directory: /etc/vault-jwt
|
||||
openvpn_jwt_private_key_path: "{{ openvpn_jwt_directory }}/private.pem"
|
||||
openvpn_jwt_public_key_path: "{{ openvpn_jwt_directory }}/public.pem"
|
||||
openvpn_jwt_rsa_bits: 3072
|
||||
openvpn_jwt_public_key_artifact_directory: "{{ playbook_dir }}/../artifacts/openvpn-jwt-public-keys"
|
||||
|
||||
openvpn_jwt_generator_script: /usr/local/bin/generate-vault-jwt.py
|
||||
openvpn_jwt_virtualenv: /opt/vault-jwt/venv
|
||||
openvpn_jwt_python: "{{ openvpn_jwt_virtualenv }}/bin/python"
|
||||
openvpn_jwt_pyjwt_version: "2.15.1"
|
||||
openvpn_jwt_role: vpn-servers
|
||||
openvpn_jwt_auth_mount: jwt-vpn
|
||||
# The generator must write only the JWT to stdout.
|
||||
openvpn_jwt_generator_args: []
|
||||
openvpn_jwt_export_public_key: true
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import uuid
|
||||
import jwt
|
||||
|
||||
PRIVATE_KEY = "{{ openvpn_jwt_private_key_path }}"
|
||||
|
||||
now = int(time.time())
|
||||
|
||||
payload = {
|
||||
"iss": "helmholz-server-identity",
|
||||
"sub": "{{ inventory_hostname }}",
|
||||
"aud": "vault-pki",
|
||||
"iat": now,
|
||||
"nbf": now,
|
||||
"exp": now + 600,
|
||||
"jti": str(uuid.uuid4())
|
||||
}
|
||||
|
||||
with open(PRIVATE_KEY, "r") as f:
|
||||
private_key = f.read()
|
||||
|
||||
token = jwt.encode(
|
||||
payload,
|
||||
private_key,
|
||||
algorithm="RS256"
|
||||
)
|
||||
|
||||
print(token)
|
||||
Reference in New Issue
Block a user