2021-04-01 14:27:31 +00:00
|
|
|
from ipaddress import ip_network
|
|
|
|
|
|
|
|
from bundlewrap.exceptions import NoSuchNode
|
2021-03-21 14:26:29 +00:00
|
|
|
from bundlewrap.metadata import atomic
|
|
|
|
|
2021-04-01 14:27:31 +00:00
|
|
|
|
2020-11-21 14:38:38 +00:00
|
|
|
defaults = {
|
|
|
|
'apt': {
|
|
|
|
'packages': {
|
|
|
|
'wireguard': {},
|
|
|
|
},
|
|
|
|
'repos': {
|
|
|
|
'backports': {
|
|
|
|
'install_gpg_key': False, # default debian signing key
|
2021-04-30 10:49:59 +00:00
|
|
|
'items': {
|
2020-11-21 14:38:38 +00:00
|
|
|
'deb http://deb.debian.org/debian {os_release}-backports main',
|
2021-04-30 10:49:59 +00:00
|
|
|
},
|
2020-11-21 14:38:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'wireguard': {
|
|
|
|
'privatekey': repo.libs.keys.gen_privkey(repo, f'{node.name} wireguard privatekey'),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-23 17:31:28 +00:00
|
|
|
if node.has_bundle('telegraf'):
|
|
|
|
defaults['telegraf'] = {
|
|
|
|
'input_plugins': {
|
|
|
|
'builtin': {
|
|
|
|
'wireguard': [{}],
|
|
|
|
},
|
|
|
|
},
|
2021-04-24 09:45:58 +00:00
|
|
|
'additional_capabilities': {
|
|
|
|
'CAP_NET_ADMIN',
|
|
|
|
},
|
2021-04-23 17:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-21 14:38:38 +00:00
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
2021-04-01 14:27:31 +00:00
|
|
|
'wireguard/peers',
|
2021-01-07 17:44:38 +00:00
|
|
|
)
|
2021-04-01 14:27:31 +00:00
|
|
|
def peer_psks(metadata):
|
|
|
|
peers = {}
|
2020-11-21 14:38:38 +00:00
|
|
|
|
2021-04-01 14:27:31 +00:00
|
|
|
for peer_name in metadata.get('wireguard/peers', {}):
|
|
|
|
peers[peer_name] = {}
|
2020-11-21 14:38:38 +00:00
|
|
|
|
2021-04-01 14:27:31 +00:00
|
|
|
if node.name < peer_name:
|
|
|
|
peers[peer_name] = {
|
|
|
|
'psk': repo.vault.random_bytes_as_base64_for(f'{node.name} wireguard {peer_name}'),
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
peers[peer_name] = {
|
|
|
|
'psk': repo.vault.random_bytes_as_base64_for(f'{peer_name} wireguard {node.name}'),
|
|
|
|
}
|
2020-11-21 14:38:38 +00:00
|
|
|
|
2021-04-01 14:27:31 +00:00
|
|
|
return {
|
|
|
|
'wireguard': {
|
|
|
|
'peers': peers,
|
|
|
|
},
|
|
|
|
}
|
2020-11-21 14:38:38 +00:00
|
|
|
|
|
|
|
|
2021-01-07 17:44:38 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'wireguard/peers',
|
|
|
|
)
|
2021-04-01 14:27:31 +00:00
|
|
|
def peer_pubkeys(metadata):
|
2020-11-21 14:38:38 +00:00
|
|
|
peers = {}
|
|
|
|
|
2021-04-01 14:27:31 +00:00
|
|
|
for peer_name in metadata.get('wireguard/peers', {}):
|
|
|
|
try:
|
|
|
|
rnode = repo.get_node(peer_name)
|
|
|
|
except NoSuchNode:
|
2020-11-21 14:38:38 +00:00
|
|
|
continue
|
|
|
|
|
2021-04-01 14:27:31 +00:00
|
|
|
peers[peer_name] = {
|
|
|
|
'pubkey': repo.libs.keys.get_pubkey_from_privkey(
|
|
|
|
repo,
|
|
|
|
f'{rnode.name} wireguard pubkey',
|
|
|
|
rnode.metadata.get('wireguard/privatekey'),
|
|
|
|
),
|
|
|
|
}
|
2020-11-21 14:38:38 +00:00
|
|
|
|
2021-04-01 14:27:31 +00:00
|
|
|
return {
|
|
|
|
'wireguard': {
|
|
|
|
'peers': peers,
|
|
|
|
},
|
|
|
|
}
|
2020-11-21 14:38:38 +00:00
|
|
|
|
|
|
|
|
2021-04-01 14:27:31 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'wireguard/peers',
|
|
|
|
)
|
2021-09-29 17:27:13 +00:00
|
|
|
def peer_ips_and_ports(metadata):
|
2021-04-01 14:27:31 +00:00
|
|
|
peers = {}
|
2021-09-29 17:27:13 +00:00
|
|
|
base_port = 51820
|
2021-04-01 14:27:31 +00:00
|
|
|
|
2021-09-29 17:27:13 +00:00
|
|
|
for number, peer_name in enumerate(sorted(metadata.get('wireguard/peers', {}).keys())):
|
2021-04-01 14:27:31 +00:00
|
|
|
try:
|
|
|
|
rnode = repo.get_node(peer_name)
|
|
|
|
except NoSuchNode:
|
|
|
|
continue
|
|
|
|
|
2021-09-29 17:27:13 +00:00
|
|
|
ip_a, ip_b = repo.libs.s2s.get_subnet_for_connection(repo, *sorted({node.name, peer_name}))
|
|
|
|
|
|
|
|
if peer_name < node.name:
|
|
|
|
my_ip = ip_a
|
|
|
|
their_ip = ip_b
|
|
|
|
else:
|
|
|
|
my_ip = ip_b
|
|
|
|
their_ip = ip_a
|
2021-04-01 14:27:31 +00:00
|
|
|
|
|
|
|
peers[rnode.name] = {
|
2021-09-29 17:27:13 +00:00
|
|
|
'my_ip': str(my_ip),
|
|
|
|
'my_port': base_port + number,
|
|
|
|
'their_ip': str(their_ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'wireguard': {
|
|
|
|
'peers': peers,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@metadata_reactor.provides(
|
|
|
|
'wireguard/peers',
|
|
|
|
)
|
|
|
|
def peer_endpoints(metadata):
|
|
|
|
peers = {}
|
|
|
|
|
|
|
|
for name, config in metadata.get('wireguard/peers', {}).items():
|
|
|
|
try:
|
|
|
|
rnode = repo.get_node(name)
|
|
|
|
except NoSuchNode:
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
peers[rnode.name] = {
|
|
|
|
'endpoint': '{}:{}'.format(
|
|
|
|
rnode.metadata.get('wireguard/external_hostname', rnode.hostname),
|
|
|
|
rnode.metadata.get(f'wireguard/peers/{node.name}/my_port', 51820),
|
|
|
|
),
|
2021-04-01 14:27:31 +00:00
|
|
|
}
|
2020-11-21 14:38:38 +00:00
|
|
|
|
|
|
|
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 = {}
|
|
|
|
|
2021-09-29 17:27:13 +00:00
|
|
|
for number, (peer, config) in enumerate(sorted(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}'] = {
|
2021-09-29 17:27:13 +00:00
|
|
|
'command_on_monitored_host': config['pubkey'].format_into(f'sudo /usr/local/share/icinga/plugins/check_wireguard_connected wg{number} {{}}'),
|
2020-11-30 05:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'icinga2_api': {
|
|
|
|
'wireguard': {
|
|
|
|
'services': services,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-03-21 14:26:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@metadata_reactor.provides(
|
2021-06-03 11:59:15 +00:00
|
|
|
'firewall/port_rules',
|
2021-03-21 14:26:29 +00:00
|
|
|
)
|
2021-06-03 11:59:15 +00:00
|
|
|
def firewall(metadata):
|
2021-09-29 17:27:13 +00:00
|
|
|
ports = {}
|
|
|
|
for name, config in metadata.get('wireguard/peers').items():
|
2021-04-01 14:27:31 +00:00
|
|
|
try:
|
2021-09-29 17:27:13 +00:00
|
|
|
rnode = repo.get_node(name)
|
2021-04-01 14:27:31 +00:00
|
|
|
except NoSuchNode: # roadwarrior
|
2021-09-29 17:27:13 +00:00
|
|
|
ports['{}/udp'.format(config['my_port'])] = atomic(set(metadata.get('wireguard/restrict-to', set())))
|
2021-04-01 14:27:31 +00:00
|
|
|
else:
|
2021-09-29 17:27:13 +00:00
|
|
|
ports['{}/udp'.format(config['my_port'])] = atomic({name})
|
2021-04-01 14:27:31 +00:00
|
|
|
|
2021-03-21 14:26:29 +00:00
|
|
|
return {
|
2021-06-03 11:59:15 +00:00
|
|
|
'firewall': {
|
2021-09-29 17:27:13 +00:00
|
|
|
'port_rules': ports,
|
2021-04-01 14:27:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@metadata_reactor.provides(
|
2021-09-29 17:27:13 +00:00
|
|
|
'interfaces',
|
2021-04-01 14:27:31 +00:00
|
|
|
)
|
|
|
|
def interface_ips(metadata):
|
2021-09-29 17:27:13 +00:00
|
|
|
interfaces = {}
|
|
|
|
for number, (peer, config) in enumerate(sorted(metadata.get('wireguard/peers', {}).items())):
|
2022-03-09 12:05:01 +00:00
|
|
|
if '/' in config['my_ip']:
|
|
|
|
my_ip = config['my_ip']
|
|
|
|
else:
|
|
|
|
my_ip = '{}/31'.format(config['my_ip'])
|
2021-09-29 17:27:13 +00:00
|
|
|
interfaces[f'wg{number}'] = {
|
|
|
|
'ips': {
|
2022-03-09 12:05:01 +00:00
|
|
|
my_ip,
|
2021-04-01 14:27:31 +00:00
|
|
|
},
|
2021-09-29 17:27:13 +00:00
|
|
|
}
|
2021-04-01 14:27:31 +00:00
|
|
|
return {
|
2021-09-29 17:27:13 +00:00
|
|
|
'interfaces': interfaces,
|
2021-03-21 14:26:29 +00:00
|
|
|
}
|
2021-09-29 17:43:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@metadata_reactor.provides(
|
2021-12-14 13:03:13 +00:00
|
|
|
'nftables/rules/10-wireguard',
|
2021-09-29 17:43:29 +00:00
|
|
|
)
|
|
|
|
def snat(metadata):
|
2022-03-09 12:05:01 +00:00
|
|
|
if not node.has_bundle('nftables') or node.os == 'arch':
|
2021-09-29 17:43:29 +00:00
|
|
|
raise DoNotRunAgain
|
|
|
|
|
2022-03-13 14:15:08 +00:00
|
|
|
rules = set()
|
|
|
|
for number, (peer, config) in enumerate(sorted(metadata.get('wireguard/peers', {}).items())):
|
|
|
|
rules.add(f'inet filter forward iif wg{number} accept')
|
|
|
|
rules.add(f'inet filter forward oif wg{number} accept')
|
2021-09-29 17:43:29 +00:00
|
|
|
|
|
|
|
if 'snat_to' in config:
|
2021-12-14 13:03:13 +00:00
|
|
|
rules.add('nat postrouting ip saddr {} ip daddr != {} snat to {}'.format(
|
2021-09-29 17:43:29 +00:00
|
|
|
config['my_ip'],
|
|
|
|
config['their_ip'],
|
|
|
|
config['snat_to'],
|
|
|
|
))
|
|
|
|
|
|
|
|
return {
|
|
|
|
'nftables': {
|
|
|
|
'rules': {
|
2021-12-14 13:03:13 +00:00
|
|
|
'10-wireguard': sorted(rules),
|
2021-09-29 17:43:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|