diff --git a/.gitea/workflows/openvpn-pki-identity.yml b/.gitea/workflows/openvpn-pki-identity.yml new file mode 100644 index 0000000..18fb556 --- /dev/null +++ b/.gitea/workflows/openvpn-pki-identity.yml @@ -0,0 +1,71 @@ +name: OpenVPN JWT Identity Preparation + +on: + workflow_dispatch: + inputs: + vpn_host: + description: Inventory hostname of the VPN server to prepare + required: true + type: string + +jobs: + prepare-identity: + name: Prepare VPN JWT identity + runs-on: hetzner-ubuntu-latest + + container: + image: gitea.infrastructure.helmholz.cloud/gitea_admin/ansible-utilities:latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup SSH + env: + SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} + run: | + set -eu + mkdir -p ~/.ssh + printf '%s\n' "$SSH_PRIVATE_KEY" > ~/.ssh/id_ed25519 + chmod 600 ~/.ssh/id_ed25519 + ssh-keygen -y -f ~/.ssh/id_ed25519 > /dev/null + + - name: Install Ansible collections + run: ansible-galaxy collection install -r requirements.yml + + - name: Validate selected VPN host + env: + VPN_HOST: ${{ github.event.inputs.vpn_host }} + run: | + set -eu + ansible-inventory -i inventories/prod/hosts.ini --list | python3 -c ' + import json + import os + import sys + + inventory = json.load(sys.stdin) + selected_host = os.environ["VPN_HOST"] + vpn_hosts = inventory.get("vpn_nodes", {}).get("hosts", []) + if selected_host not in vpn_hosts: + print("vpn_host must exactly match a host in the vpn_nodes inventory group", file=sys.stderr) + sys.exit(1) + ' + + - name: Prepare JWT identity + env: + ANSIBLE_CONFIG: ${{ github.workspace }}/ansible.cfg + VPN_HOST: ${{ github.event.inputs.vpn_host }} + run: | + set -eu + ansible-playbook \ + -i inventories/prod/hosts.ini \ + playbooks/openvpn-pki-identity.yml \ + --limit "$VPN_HOST" + + - name: Upload public JWT key + uses: actions/upload-artifact@v4 + with: + name: openvpn-pki-public-keys + path: artifacts/openvpn-pki-public-keys/*.pem + if-no-files-found: error + retention-days: 7 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85b7c6d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/artifacts/openvpn-pki-public-keys/ diff --git a/playbooks/openvpn-pki-identity.yml b/playbooks/openvpn-pki-identity.yml new file mode 100644 index 0000000..079f384 --- /dev/null +++ b/playbooks/openvpn-pki-identity.yml @@ -0,0 +1,7 @@ +--- +- name: Prepare OpenVPN JWT identities + hosts: vpn_nodes + become: true + + roles: + - role: openvpn_pki diff --git a/requirements.yml b/requirements.yml index 92a0244..61d9e43 100644 --- a/requirements.yml +++ b/requirements.yml @@ -2,3 +2,4 @@ collections: - name: ansible.posix - name: community.general + - name: community.crypto diff --git a/roles/openvpn_pki/README.md b/roles/openvpn_pki/README.md new file mode 100644 index 0000000..b2b6ee8 --- /dev/null +++ b/roles/openvpn_pki/README.md @@ -0,0 +1,63 @@ +# OpenVPN PKI JWT identity preparation + +This role prepares one local JWT RSA identity per VPN server so an administrator +can register each public key with the existing OpenBao JWT configuration. It +does not configure OpenBao or request certificates. + +## Prepare an identity + +Run the dedicated playbook, limiting it to the new VPN server when appropriate: + +```bash +ansible-playbook \ + -i inventories/prod/hosts.ini \ + playbooks/openvpn-pki-identity.yml \ + --limit vpn01-test +``` + +The playbook targets `vpn_nodes` and uses privilege escalation. It creates the +identity on the selected server; it does not deploy certificates or alter the +OpenVPN service. + +## Key locations and retrieval + +The server stores its root-owned identity in `/etc/vault-jwt/` by default: + +- `private.pem`: RSA 3072-bit private key, mode `0600`. +- `public.pem`: corresponding PEM public key, mode `0644`. + +Only `public.pem` is fetched to the controller, as +`artifacts/openvpn-pki-public-keys/.pem`. The artifacts +directory is ignored by Git. The private key is generated on the VPN server +with `community.crypto.openssl_privatekey`; it is never fetched. + +## Register the public key in OpenBao + +Provide the fetched PEM public key to the OpenBao administrator using the +approved secure process. The administrator should combine the new PEM public +key with all currently registered PEM public keys into a bundle, then update +the existing JWT authentication configuration with that complete bundle. The +update must preserve every previously registered key; replacing the configured +key set with only the new key would invalidate identities already in use. +Public-key registration is a manual administrator operation and is not +performed by this role. + +## Existing identity behavior + +The role creates a missing identity only when both key files are absent. If +both already exist, it leaves them unchanged and fetches the existing public +key again. If exactly one key file exists, the role fails rather than +replacing or silently repairing the identity. Investigate and recover the +matching pair deliberately before rerunning. Do not delete or replace the +private key as a routine way to resolve errors: OpenBao registrations depend +on the corresponding public key. + +The private key must remain on its VPN server because it is the credential +used to prove that server's identity. Copying it to the controller or +registering it with OpenBao would defeat the separation between private key +custody and public-key registration. + +## Requirements + +Install the `community.crypto` collection on the Ansible controller. It is not +currently listed in the repository's `requirements.yml`. diff --git a/roles/openvpn_pki/defaults/main.yml b/roles/openvpn_pki/defaults/main.yml new file mode 100644 index 0000000..445bef5 --- /dev/null +++ b/roles/openvpn_pki/defaults/main.yml @@ -0,0 +1,6 @@ +--- +openvpn_pki_jwt_directory: /etc/vault-jwt +openvpn_pki_jwt_private_key_path: "{{ openvpn_pki_jwt_directory }}/private.pem" +openvpn_pki_jwt_public_key_path: "{{ openvpn_pki_jwt_directory }}/public.pem" +openvpn_pki_jwt_rsa_bits: 3072 +openvpn_pki_public_key_artifact_directory: "{{ playbook_dir }}/../artifacts/openvpn-pki-public-keys" diff --git a/roles/openvpn_pki/tasks/identity.yml b/roles/openvpn_pki/tasks/identity.yml new file mode 100644 index 0000000..08b3675 --- /dev/null +++ b/roles/openvpn_pki/tasks/identity.yml @@ -0,0 +1,62 @@ +--- +- 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 diff --git a/roles/openvpn_pki/tasks/main.yml b/roles/openvpn_pki/tasks/main.yml new file mode 100644 index 0000000..8fd9d2c --- /dev/null +++ b/roles/openvpn_pki/tasks/main.yml @@ -0,0 +1,3 @@ +--- +- name: Import JWT identity preparation tasks + ansible.builtin.import_tasks: identity.yml