Skip to content

Commit 7b2a5e8

Browse files
author
Michal Muransky
authored
Merge pull request #5 from lablabs/develop
Validate Public Keys
2 parents 191a545 + c8dbc71 commit 7b2a5e8

File tree

7 files changed

+53
-90
lines changed

7 files changed

+53
-90
lines changed

README.md

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This collection will:
2424

2525
## How to run Ansible playbooks from this collection
2626

27-
First make sure your future destination host is up and running and you have an access to SSH Private Key file.
27+
First make sure your future destination host is up and running and you have an access to SSH Private Key file.
2828
Prepare the Python prerequisites for Ansible Roles in this Collecion:
2929

3030
```bash
@@ -61,81 +61,9 @@ You may want to create a playbook to run all 3 playbooks in one run:
6161
tags: wireguard
6262
```
6363
64-
## Variables example
64+
## Variables
6565
66-
```yaml
67-
---
68-
# Directory to store WireGuard configuration on the remote hosts
69-
wireguard_dir: /etc/wireguard
70-
wireguard_clients_dir: "{{ wireguard_dir }}/clients"
71-
72-
wireguard_clients_download_dir: clients/
73-
wireguard_download_clients: false
74-
75-
# Predefined wireguard keys, this usually should be defined in ansible-vault
76-
wireguard_privatekey_path: "{{ wireguard_dir }}/privatekey"
77-
wireguard_publickey_path: "{{ wireguard_dir }}/publickey"
78-
wireguard_presharedkey_path: "{{ wireguard_dir }}/presharedkey"
79-
80-
wireguard_systemd_path: /etc/systemd/network
81-
82-
# Wireguard packages
83-
wireguard_repo_url: "{{ _repo_url }}"
84-
wireguard_distro_packages: "{{ _distro_packages }}"
85-
86-
wireguard_packages:
87-
- wireguard-dkms
88-
- wireguard-tools
89-
90-
# The default port WireGuard will listen if not specified otherwise.
91-
wireguard_port: 51820
92-
93-
# Client destination Hostname
94-
wireguard_hostname: "{{ inventory_hostname }}"
95-
96-
# The default interface name that wireguard should use if not specified otherwise.
97-
wireguard_interface: wg0
98-
99-
# Base wireguard subnet
100-
wireguard_address: 10.213.213.0/24
101-
102-
wireguard_server_ip: "{{ wireguard_address | ipaddr('network') | ipmath(1) }}"
103-
wireguard_subnetmask: "{{ wireguard_address | ipaddr('prefix') }}"
104-
105-
# XXX: This role only works with PrivateKeyFile/PresharedKeyFile it doesn't support variables.
106-
wireguard_systemd_netdev:
107-
- NetDev:
108-
- Name: "{{ wireguard_interface }}"
109-
- Kind: wireguard
110-
- Description: "wireguard server: {{ wireguard_interface}} server on {{ wireguard_address }}"
111-
- WireGuard:
112-
- PrivateKey: "{{ _privkey_value['content'] | b64decode }}"
113-
- ListenPort: "{{ wireguard_port }}"
114-
115-
wireguard_systemd_network:
116-
- Match:
117-
- Name: "{{ wireguard_interface }}"
118-
- Network:
119-
- Address: "{{ wireguard_server_ip }}/{{ wireguard_subnetmask }}"
120-
- Route:
121-
- Destination: "{{ wireguard_address }}"
122-
- Gateway: "{{ wireguard_server_ip }}"
123-
124-
wireguard_keepalive: 25
125-
126-
wireguard_peers_allowed_ips: "{{ ([(_wireguard_interface_addr | ipaddr('network/prefix'))] + wireguard_additional_routes) | join(\", \") }}"
127-
wireguard_peers: []
128-
# - name: user1
129-
# allowed_ip: "10.213.213.2/32"
130-
# publickey: "asdasdasdadsasdasd"
131-
# - name: user2
132-
# allowed_ip: "10.213.213.3/32"
133-
# publickey: "000000000000000000"
134-
# keepalive: 30
135-
# - name: user3
136-
# allowed_ip: "10.213.213.4/32"
137-
# publickey: "111111111111111111"
138-
```
66+
[Wireguard Role Variables](roles/wireguard/defaults/main.yml)
13967
14068
## License
14169
@@ -162,4 +90,4 @@ See [LICENSE](LICENSE) for full details.
16290
16391
## Author Information
16492
165-
Created in 2021 by [Labyrinth Labs](https://www.lablabs.io/)
93+
Created in 2021 by [Labyrinth Labs](https://www.lablabs.io/)

molecule/default/converge.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
wireguard_peers:
2727
- name: user1
2828
allowed_ip: "10.213.213.2"
29-
publickey: "asdasdasdadsasdasd"
29+
publickey: "YXNhc21hc2Fhc3Npc2FzYXNhc2FzYXNhc2FzYXNhc3N=" # Fake Pub Key
3030
- name: user2
3131
allowed_ip: "10.213.213.3"
32-
publickey: "000000000000000000"
32+
publickey: "YXNhc21hc2Fhc3Npc2FzYXNhc2FzYXNhc2FzYXNhc3N=" # Fake Pub Key
3333
keepalive: 30
3434
- name: user3
3535
allowed_ip: "10.213.213.4"
36-
publickey: "111111111111111111"
36+
publickey: "YXNhc21hc2Fhc3Npc2FzYXNhc2FzYXNhc2FzYXNhc3N=" # Fake Pub Key

roles/wireguard/README.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,26 @@ wireguard_systemd_network:
7272

7373
wireguard_keepalive: 25
7474

75+
# Additional IPs allowed to Wireguard
76+
# (Following list of IPs will be added to Wireguard AllowedIPs)
77+
wireguard_additional_routes: {}
78+
7579
wireguard_peers_allowed_ips: "{{ ([(_wireguard_interface_addr | ipaddr('network/prefix'))] + wireguard_additional_routes) | join(\", \") }}"
7680
wireguard_peers: []
77-
# - name: user1
78-
# allowed_ip: "10.213.213.2/32"
79-
# publickey: "asdasdasdadsasdasd"
80-
# - name: user2
81-
# allowed_ip: "10.213.213.3/32"
82-
# publickey: "000000000000000000"
83-
# keepalive: 30
84-
# - name: user3
85-
# allowed_ip: "10.213.213.4/32"
86-
# publickey: "111111111111111111"
81+
# - name: user1
82+
# allowed_ip: "10.213.213.2/32"
83+
# publickey: "asdasdasdadsasdasd"
84+
# - name: user2
85+
# allowed_ip: "10.213.213.3/32"
86+
# publickey: "000000000000000000"
87+
# keepalive: 30
88+
# - name: user3
89+
# allowed_ip: "10.213.213.4/32"
90+
# publickey: "111111111111111111"
91+
92+
# Test Wirequard public keys and find possible errors
93+
# This will check the lenght of the key (44 characters) and test if it's a valid base64 string.
94+
run_publickey_pre_check: true
8795

8896
```
8997

roles/wireguard/defaults/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ wireguard_systemd_network:
5757

5858
wireguard_keepalive: 25
5959

60+
# Additional IPs allowed to Wireguard
61+
# (Following list of IPs will be added to Wireguard AllowedIPs)
62+
wireguard_additional_routes: {}
63+
6064
wireguard_peers_allowed_ips: "{{ ([(_wireguard_interface_addr | ipaddr('network/prefix'))] + wireguard_additional_routes) | join(\", \") }}"
6165
wireguard_peers: []
6266
# - name: user1
@@ -69,3 +73,7 @@ wireguard_peers: []
6973
# - name: user3
7074
# allowed_ip: "10.213.213.4/32"
7175
# publickey: "111111111111111111"
76+
77+
# Test Wirequard public keys and find possible errors
78+
# This will check the lenght of the key (44 characters) and test if it's a valid base64 string.
79+
run_publickey_pre_check: true

roles/wireguard/tasks/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
---
2+
3+
- include_tasks: pre_check.yml
4+
when: run_publickey_pre_check | bool
5+
26
- name: Include OS specific variables
37
include_vars:
48
file: "{{ lookup('first_found', possible_files) }}"

roles/wireguard/tasks/pre_check.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
3+
- name: Check if Public Key is valid base64 string
4+
assert:
5+
that:
6+
- " item.publickey | b64decode "
7+
fail_msg: "The Public Key for user {{ item.name }} is invalid."
8+
with_items: "{{ wireguard_peers }}"
9+
10+
- name: Check if Public Key is valid base64 string
11+
assert:
12+
that:
13+
- " item.publickey | length == 44 "
14+
fail_msg: "The Lenght of Public Key for user {{ item.name }} is invalid."
15+
with_items: "{{ wireguard_peers }}"

roles/wireguard/vars/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ _system_arch_map:
55

66
_system_arch: "{{ _arch_map[ansible_architecture] | default(ansible_architecture) }}"
77

8-
_wireguard_interface_addr: "{{ ansible_default_ipv4.address }}/{{ ansible_default_ipv4.netmask }}"
8+
_wireguard_interface_addr: "{{ ansible_default_ipv4.address | default(ansible_all_ipv4_addresses[0]) }}/{{ ansible_default_ipv4.netmask }}"

0 commit comments

Comments
 (0)