From 5bac50269ff0dd9c5bbffc921c1ac552682854b4 Mon Sep 17 00:00:00 2001 From: lhahn Date: Sun, 17 Sep 2023 10:56:26 +0200 Subject: [PATCH] Git initial commit --- LICENSE | 9 ++++ README.md | 3 ++ defaults/main.yml | 31 +++++++++++ handlers/main.yml | 7 +++ meta/main.yml | 16 ++++++ tasks/main.yml | 53 +++++++++++++++++++ .../clients/wireguard-client.conf.j2 | 11 ++++ templates/etc/wireguard/private.key.j2 | 1 + templates/etc/wireguard/public.key.j2 | 1 + templates/etc/wireguard/wireguard.conf.j2 | 25 +++++++++ vars/main.yml | 0 11 files changed, 157 insertions(+) create mode 100755 LICENSE create mode 100755 README.md create mode 100755 defaults/main.yml create mode 100755 handlers/main.yml create mode 100755 meta/main.yml create mode 100755 tasks/main.yml create mode 100644 templates/etc/wireguard/clients/wireguard-client.conf.j2 create mode 100644 templates/etc/wireguard/private.key.j2 create mode 100644 templates/etc/wireguard/public.key.j2 create mode 100644 templates/etc/wireguard/wireguard.conf.j2 create mode 100755 vars/main.yml diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +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. diff --git a/README.md b/README.md new file mode 100755 index 0000000..66c9474 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# cloud-vpn + +Ansible role to provide an OpenVPN server; relies on easyrsa for certificates. \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100755 index 0000000..2f70cca --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,31 @@ +--- +cloud_apps: /opt +cloud_storage: /opt/storage +cloud_stage: prod +cloud_update: false + +vpn_internal_dns: 1.1.1.1 + +vpn_allow_adjacent_client_traffic: True +vpn_keepalive: 25 + +vpn_gateway_clientfolder: /etc/wireguard/clients +vpn_gateway_interface: eth0 +vpn_gateway_host: my-wireguard-server.tld +vpn_gateway_port: 51820 +vpn_gateway_net_prefix: 10.10.123 +vpn_gateway_net_cidr: 28 +vpn_gateway_public_key: your-public-key +vpn_gateway_private_key: your-privat-key + +vpn_gateway_forward: [] +# - server_port: 22 +# client_port: "{{ ssh_port }}" +# client_index: 0 + + +vpn_clients: [] +# - name: +# index: 1 +# private_key: +# public_key: diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100755 index 0000000..ee6d4f6 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,7 @@ +--- +- name: restart wireguard service + service: + name: wg-quick@{{ cloud_name }} + state: restarted + enabled: yes + when: wireguard_installed is defined and not wireguard_installed.changed \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml new file mode 100755 index 0000000..1313445 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,16 @@ +--- +galaxy_info: + role_name: vpn + namespace: opendevchain + author: Lars Hahn + company: OpenDevChain + license: MIT + description: Role to setup a wireguard server. + min_ansible_version: 2.7 + platforms: + - name: Debian + versions: + - 12 + galaxy_tags: + - vpn +dependencies: [] diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100755 index 0000000..3b89a06 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,53 @@ +--- +- name: install fail2ban service + apt: + update_cache: yes + state: "{% if cloud_update | bool %}latest{% else %}present{% endif %}" + install_recommends: yes + pkg: wireguard + register: wireguard_installed + +- name: setup key files + template: + src: "etc/wireguard/{{ item }}.j2" + dest: "/etc/wireguard/{{ item }}" + owner: root + mode: 0600 + loop: + - private.key + - public.key + notify: restart wireguard service + +- name: setup wireguard config + template: + src: "etc/wireguard/wireguard.conf.j2" + dest: "/etc/wireguard/{{ cloud_name }}.conf" + owner: root + mode: 0600 + notify: restart wireguard service + +- name: setup client folder + file: + state: directory + mode: 0600 + owner: root + path: "{{ vpn_gateway_clientfolder }}" + +- name: setup client configs + template: + src: "etc/wireguard/clients/wireguard-client.conf.j2" + dest: "{{ vpn_gateway_clientfolder }}/{{ vpn_client.name }}.conf" + owner: root + mode: 0600 + loop: "{{ vpn_clients }}" + loop_control: + loop_var: vpn_client + label: "{{ vpn_client.name }}" + + +- name: enable wireguard systemd unit + systemd: + name: wg-quick@{{ cloud_name }} + enabled: yes + daemon_reload: yes + state: started diff --git a/templates/etc/wireguard/clients/wireguard-client.conf.j2 b/templates/etc/wireguard/clients/wireguard-client.conf.j2 new file mode 100644 index 0000000..1d1b91c --- /dev/null +++ b/templates/etc/wireguard/clients/wireguard-client.conf.j2 @@ -0,0 +1,11 @@ +[Interface] +Address = {{ vpn_gateway_net_prefix }}.{{ vpn_client.index }}/32 +PrivateKey = {{ vpn_client.private_key }} +DNS = {{ vpn_internal_dns }} + +[Peer] +PublicKey = {{ vpn_gateway_public_key }} +Endpoint = {{ vpn_gateway_host }}:{{ vpn_gateway_port }} +AllowedIPs = {{ vpn_gateway_net_prefix }}.1/{% if vpn_allow_adjacent_client_traffic %}{{ vpn_gateway_net_cidr }}{% else %}32{% endif %} + +PersistentKeepalive = {{ vpn_keepalive }} diff --git a/templates/etc/wireguard/private.key.j2 b/templates/etc/wireguard/private.key.j2 new file mode 100644 index 0000000..3fc053d --- /dev/null +++ b/templates/etc/wireguard/private.key.j2 @@ -0,0 +1 @@ +{{ vpn_gateway_private_key }} \ No newline at end of file diff --git a/templates/etc/wireguard/public.key.j2 b/templates/etc/wireguard/public.key.j2 new file mode 100644 index 0000000..7a794c7 --- /dev/null +++ b/templates/etc/wireguard/public.key.j2 @@ -0,0 +1 @@ +{{ vpn_gateway_public_key }} \ No newline at end of file diff --git a/templates/etc/wireguard/wireguard.conf.j2 b/templates/etc/wireguard/wireguard.conf.j2 new file mode 100644 index 0000000..0401194 --- /dev/null +++ b/templates/etc/wireguard/wireguard.conf.j2 @@ -0,0 +1,25 @@ +[Interface] +Address = {{ vpn_gateway_net_prefix }}.1/{{ vpn_gateway_net_cidr }} +ListenPort = {{ vpn_gateway_port }} +PrivateKey = {{ vpn_gateway_private_key }} + +{% if vpn_gateway_forward is defined and vpn_gateway_forward | length > 0 %} +PreUp = sysctl -w net.ipv4.ip_forward=1 +PreUp = sysctl -w net.ipv6.conf.all.forwarding=1 + +{% for config in vpn_gateway_forward %} +PreUp = iptables -t nat -A PREROUTING -i {{ vpn_gateway_interface }} -p {{ config.protocol | default('tcp') }} --dport {{ config.server_port }} -j DNAT --to-destination {{ vpn_gateway_net_prefix }}.{{ config.client_index }}:{{ config.client_port }} +PostDown = iptables -t nat -D PREROUTING -i {{ vpn_gateway_interface }} -p {{ config.protocol | default('tcp') }} --dport {{ config.server_port }} -j DNAT --to-destination {{ vpn_gateway_net_prefix }}.{{ config.client_index }}:{{ config.client_port }} + +{% endfor %} +PreUp = iptables -t nat -A POSTROUTING -o {{ cloud_name }} -j MASQUERADE +PostDown = iptables -t nat -D POSTROUTING -o {{ cloud_name }} -j MASQUERADE +{% endif %} + +{% for vpn_client in vpn_clients %} +## vpn {{ cloud_name }} - {{ vpn_client.name }} ## +[Peer] +PublicKey = {{ vpn_client.public_key }} +AllowedIPs = {{ vpn_gateway_net_prefix }}.{{ vpn_client.index }}/32 + +{% endfor %} \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml new file mode 100755 index 0000000..e69de29