2020-11-21 14:38:38 +00:00
|
|
|
defaults = {
|
|
|
|
'apt': {
|
|
|
|
'packages': {
|
|
|
|
'wireguard': {},
|
|
|
|
},
|
|
|
|
'repos': {
|
|
|
|
'backports': {
|
|
|
|
'install_gpg_key': False, # default debian signing key
|
|
|
|
'items': [
|
|
|
|
'deb http://deb.debian.org/debian {os_release}-backports main',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'iptables': {
|
|
|
|
'bundle_rules': {
|
|
|
|
'wireguard': [
|
|
|
|
'iptables_both -A INPUT -p udp --dport 51820 -j ACCEPT',
|
|
|
|
'iptables_both -A FORWARD -i wg0 -j ACCEPT',
|
2021-03-15 08:00:35 +00:00
|
|
|
'iptables_both -A FORWARD -o wg0 -j ACCEPT',
|
2020-11-21 14:38:38 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'wireguard': {
|
|
|
|
'privatekey': repo.libs.keys.gen_privkey(repo, f'{node.name} wireguard privatekey'),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'wireguard/network',
|
|
|
|
)
|
2020-11-21 14:38:38 +00:00
|
|
|
def get_wireguard_network_from_server(metadata):
|
|
|
|
# FIXME This will break if more than one node sets 'wireguard/network'
|
|
|
|
for rnode in repo.nodes:
|
|
|
|
if not rnode.has_bundle('wireguard'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if node.name in rnode.metadata.get('wireguard/peers', {}).keys():
|
|
|
|
network = rnode.metadata.get('wireguard/network', None)
|
|
|
|
|
|
|
|
if network:
|
|
|
|
return {
|
|
|
|
'wireguard': {
|
|
|
|
'network': network,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'wireguard/peers',
|
|
|
|
)
|
2020-11-21 14:38:38 +00:00
|
|
|
def get_my_wireguard_peers(metadata):
|
|
|
|
peers = {}
|
|
|
|
|
|
|
|
for rnode in repo.nodes:
|
|
|
|
if not rnode.has_bundle('wireguard'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if node.name in rnode.metadata.get('wireguard/peers', {}).keys():
|
|
|
|
peers[rnode.name] = {
|
|
|
|
'pubkey': repo.libs.keys.get_pubkey_from_privkey(repo, f'{node.name} wireguard {rnode.name}', rnode.metadata.get('wireguard/privatekey')),
|
|
|
|
'psk': rnode.metadata.get('wireguard/psk', metadata.get('wireguard/psk', None)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if not rnode.metadata.get(f'wireguard/peers/{node.name}/do_not_initiate_a_connection_from_your_side', False):
|
|
|
|
peers[rnode.name]['endpoint'] = f'{rnode.hostname}:51820'
|
|
|
|
|
|
|
|
peers[rnode.name]['ips'] = rnode.metadata.get('wireguard/subnets', set())
|
|
|
|
|
|
|
|
your_ip = rnode.metadata.get('wireguard/my_ip', None)
|
|
|
|
if your_ip:
|
|
|
|
peers[rnode.name]['ips'].add(your_ip)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'wireguard': {
|
|
|
|
'peers': peers,
|
|
|
|
},
|
|
|
|
}
|
2020-11-30 05:43:35 +00:00
|
|
|
|
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'icinga2_api/wireguard/services',
|
|
|
|
)
|
2020-11-30 05:43:35 +00:00
|
|
|
def icinga2(metadata):
|
|
|
|
services = {}
|
|
|
|
|
|
|
|
for peer, config in metadata.get('wireguard/peers', {}).items():
|
2021-02-14 20:35:37 +00:00
|
|
|
if config.get('exclude_from_monitoring', False):
|
|
|
|
continue
|
|
|
|
|
2020-11-30 05:43:35 +00:00
|
|
|
services[f'WIREGUARD CONNECTION {peer}'] = {
|
2020-12-10 15:39:26 +00:00
|
|
|
'command_on_monitored_host': config['pubkey'].format_into('sudo /usr/local/share/icinga/plugins/check_wireguard_connected wg0 {}'),
|
2020-11-30 05:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'icinga2_api': {
|
|
|
|
'wireguard': {
|
|
|
|
'services': services,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|