Git initial commit

This commit is contained in:
Lars Hahn 2023-08-20 10:31:35 +02:00
commit bc6109213c
10 changed files with 306 additions and 0 deletions

9
LICENSE Executable file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

3
README.md Executable file
View File

@ -0,0 +1,3 @@
# cloud-vpn
Ansible role to provide an OpenVPN server; relies on easyrsa for certificates.

29
defaults/main.yml Executable file
View File

@ -0,0 +1,29 @@
---
cloud_apps: /opt
cloud_storage: /opt/storage
cloud_stage: prod
cloud_update: false
sysctl_configs:
- net.ipv4.ip_forward=1
vpn_host: "vpn.{{ domain_external | default('my-domain.tld') }}"
vpn_protocol: "udp"
vpn_server: "cloud-openvpn"
vpn_cidr: 10.10.10.0
vpn_mask: 255.255.255.0
vpn_port: 1194
vpn_log: /var/log/openvpn/openvpn.log
vpn_dns:
- 208.67.222.222
- 208.67.220.220
vpn_clients:
- name: username
state: present
easy_rsa_home: "/usr/share/easy-rsa"
easy_rsa_clients:
- name: easyrsa_username
state: present

9
handlers/main.yml Executable file
View File

@ -0,0 +1,9 @@
---
- name: restart sysctl
command: sysctl -p
- name: restart openvpn
systemd:
name: openvpn@server
state: restarted
enabled: yes

17
meta/main.yml Executable file
View File

@ -0,0 +1,17 @@
---
galaxy_info:
role_name: vpn
namespace: hahn-cloud
author: Lars Hahn
company: OpenDevChain
license: MIT
description: Role to setup an OpenVPN server, to make e.g. personal networks available from remote.
min_ansible_version: 2.7
platforms:
- name: Debian
versions:
- 10
galaxy_tags:
- vpn
dependencies:
- easy-rsa

57
tasks/clients.yml Executable file
View File

@ -0,0 +1,57 @@
---
- name: install client ovpn configs
template:
src: etc/openvpn/client/client.ovpn.j2
dest: "{{ vpn_home }}/client/{{ client }}.ovpn"
mode: 0600
owner: root
group: vpn
loop: "{{ vpn_clients_active }}"
loop_control:
loop_var: client
label: "{{ client }}"
- name: find abstent clients ovpn config
find:
paths: "{{ vpn_home }}/client/"
pattern: "{{ client }}.*"
loop: "{{ vpn_clients_passive }}"
loop_control:
loop_var: client
label: "{{ client }}"
register: absent_clients
- name: remove absent clients ovpn config
file:
state: absent
path: "{{ client }}"
loop: "{{ absent_clients.results | json_query('[*].files[*].path') | flatten }}"
loop_control:
loop_var: client
label: "{{ client | basename }}"
when: absent_clients.results | length > 0
- name: setup OpenVPN config folder for each vpn client
file:
state: directory
path: "/home/{{ user }}/.openvpn"
mode: 0700
owner: "{{ user }}"
group: "{{ user }}"
loop: "{{ vpn_clients_active | map('regex_replace','\\.[^\\.]+$','') | list | unique }}"
loop_control:
loop_var: user
label: "{{ user }}"
- name: rollout .ovpn single-file config for active clients
copy:
src: "{{ vpn_home }}/client/{{ client }}.ovpn"
dest: "/home/{{ client.split('.')[0] }}/.openvpn/"
mode: 0400
owner: "{{ client.split('.')[0] }}"
group: "{{ client.split('.')[0] }}"
loop: "{{ vpn_clients_active }}"
loop_control:
loop_var: client
label: "{{ client }}"

32
tasks/main.yml Executable file
View File

@ -0,0 +1,32 @@
---
- name: install openvpn and recommendations
apt:
update_cache: yes
state: "{% if cloud_update | bool %}latest{% else %}present{% endif %}"
install_recommends: yes
pkg:
- "openvpn"
- name: setup sysctl config for openvpn
lineinfile:
path: "{{ sysctl_conf }}"
regexp: '^#?{{ configline.split("=")[0] }}='
line: "{{ configline }}"
backrefs: yes
owner: root
group: root
mode: 0644
loop: "{{ sysctl_configs }}"
loop_control:
loop_var: configline
label: "{{ configline }}"
notify: restart sysctl
# run in a block only when for every vpn_client user there is a certificate
# otherwise ignore ...
# It is better to have a matching config (dependency currently only to vpn)
# then to catch spectacular cases.
- block:
- include_tasks: server.yml
- include_tasks: clients.yml
when: vpn_clients | difference(easy_rsa_clients) | length == 0

78
tasks/server.yml Executable file
View File

@ -0,0 +1,78 @@
---
- name: find installed openvpn clients
find:
paths: "{{ vpn_home }}/client/"
patterns: "*.crt"
register: easyrsa_key_file
#- name: Setup default OpenVPN configuration
# shell:
# cmd: "gunzip -c {{ vpn_doc_examples }}/server.conf.gz > {{ vpn_home }}/server.conf"
# creates: "{{ vpn_home }}/server.conf"
- name: Setup default OpenVPN configuration
copy:
src: "{{ vpn_doc_examples }}/server.conf"
dest: "{{ vpn_home }}/server.conf"
owner: root
group: root
force: no
- name: find server TLS-Auth key
find:
paths: "{{ vpn_home }}/server/"
patterns: "ta.key"
register: tlsauth_key_files
- name: generate TLS-Auth key
command: "openvpn --genkey --secret {{ vpn_tlsauth_key_file }}"
when: tlsauth_key_files.matched == 0
- name: install easy-rsa CA and server certs
copy:
src: "{{ easy_rsa_home }}/pki/{{ item }}"
dest: "{{ vpn_home }}/server/"
mode: 0600
owner: root
group: root
loop:
- "ca.crt"
- "dh.pem"
- "private/{{ vpn_server }}.key"
- "issued/{{ vpn_server }}.crt"
- name: setup OpenVPN configuration
lineinfile:
path: "{{ vpn_home }}/server.conf"
regexp: '^;?{{ configline.split(" ")[0] }}{% if configline.split(" ") | length > 1 %} {% endif %}'
line: "{{ configline }}"
owner: root
group: root
mode: 0644
loop: "{{ vpn_server_conf }}"
loop_control:
loop_var: configline
label: "{{ configline }}"
notify: restart openvpn
- name: off-setup OpenVPN configuration
lineinfile:
path: "{{ vpn_home }}/server.conf"
regexp: '^{{ configline.split(" ")[0] }}{% if configline.split(" ") | length > 1 %} {% endif %}'
line: ";{{ configline }}"
backrefs: yes
owner: root
group: root
mode: 0644
loop: "{{ vpn_server_conf_off }}"
loop_control:
loop_var: configline
label: ";{{ configline }}"
notify: restart openvpn
- name: enable openvpn@server systemd unit
systemd:
name: openvpn@server
enabled: yes
daemon_reload: yes
state: started

View File

@ -0,0 +1,29 @@
client
dev tun
proto {{ vpn_protocol }}
remote {{ vpn_host }} {{ vpn_port }}
remote-cert-tls server
key-direction 1
cipher AES-256-CBC
auth SHA512
auth-nocache
tls-version-min 1.2
tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-CBC-SHA256
comp-lzo
nobind
persist-key
persist-tun
mute-replay-warnings
verb 3
<ca>
{{ lookup("file", easy_rsa_home + "/pki/ca.crt") }}
</ca>
<cert>
{{ lookup("file", easy_rsa_home + "/pki/issued/" + client + ".crt") }}
</cert>
<key>
{{ lookup("file", easy_rsa_home + "/pki/private/" + client + ".key") }}
</key>
<tls-auth>
{{ lookup("file", vpn_tlsauth_key_file) }}
</tls-auth>

43
vars/main.yml Executable file
View File

@ -0,0 +1,43 @@
---
sysctl_path: "/etc/"
sysctl_conf: "{{ sysctl_path }}/sysctl.conf"
vpn_doc_examples: /usr/share/doc/openvpn/examples/sample-config-files/
vpn_home: "/etc/openvpn"
vpn_clients_active: "{{ vpn_clients | json_query('[?state==`present`].name') }}"
vpn_clients_passive: "{{ vpn_clients | json_query('[?state!=`present`].name') }}"
vpn_tlsauth_key: ta.key
vpn_tlsauth_key_file: "{{ vpn_home }}/server/{{ vpn_tlsauth_key }}"
vpn_server_conf:
- "port {{ vpn_port }}"
- "proto {{ vpn_protocol }}"
- dev tun
- "ca {{ vpn_home }}/server/ca.crt"
- "cert {{ vpn_home }}/server/{{ vpn_server }}.crt"
- "key {{ vpn_home }}/server/{{ vpn_server }}.key"
- "dh {{ vpn_home }}/server/dh.pem"
- topology subnet
- "server {{ vpn_cidr }} {{ vpn_mask }}"
- cipher AES-256-CBC
- "tls-auth {{ vpn_home }}/server/ta.key 0"
- tls-version-min 1.2
- tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-CBC-SHA256
- auth SHA512
- auth-nocache
- keepalive 20 60
- persist-key
- persist-tun
- client-to-client
- comp-lzo
- user nobody
- group nogroup
- "log-append {{ vpn_log }}"
- verb 3
vpn_server_conf_off:
- explicit-exit-notify 1