2021-04-01 14:27:31 +00:00
|
|
|
from ipaddress import ip_network
|
2023-09-09 11:54:27 +00:00
|
|
|
from re import sub
|
2021-04-01 14:27:31 +00:00
|
|
|
|
|
|
|
from bundlewrap.exceptions import NoSuchNode
|
2021-03-21 14:26:29 +00:00
|
|
|
from bundlewrap.metadata import atomic
|
|
|
|
|
2020-11-21 14:38:38 +00:00
|
|
|
defaults = {
|
|
|
|
'apt': {
|
|
|
|
'packages': {
|
|
|
|
'wireguard': {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'wireguard': {
|
|
|
|
'privatekey': repo.libs.keys.gen_privkey(repo, f'{node.name} wireguard privatekey'),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-12 10:00:43 +00:00
|
|
|
if node.os_version <= (11,):
|
|
|
|
defaults['apt']['repos'] = {
|
|
|
|
'backports': {
|
|
|
|
'install_gpg_key': False, # default debian signing key
|
|
|
|
'items': {
|
|
|
|
'deb http://deb.debian.org/debian {os_release}-backports main',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
defaults['apt']['packages']['wireguard-dkms'] = {
|
|
|
|
'installed': False,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-23 13:06:16 +00:00
|
|
|
@metadata_reactor.provides(
|
|
|
|
'wireguard/peers',
|
|
|
|
)
|
|
|
|
def peers_auto_full_mesh(metadata):
|
|
|
|
peers = {}
|
|
|
|
|
2023-09-24 16:55:29 +00:00
|
|
|
if node.name not in repo.libs.s2s.WG_AUTOGEN_NODES:
|
|
|
|
return {}
|
|
|
|
|
2023-09-23 13:06:16 +00:00
|
|
|
for rnode in repo.libs.s2s.WG_AUTOGEN_NODES:
|
|
|
|
if rnode is None or rnode == node.name:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
rnode = repo.get_node(rnode)
|
|
|
|
except NoSuchNode:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if rnode.dummy:
|
|
|
|
continue
|
|
|
|
|
|
|
|
peers[rnode.name] = {}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'wireguard': {
|
|
|
|
'peers': peers,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
2023-09-23 13:06:16 +00:00
|
|
|
def peer_psks(metadata):
|
2021-04-01 14:27:31 +00:00
|
|
|
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', {}):
|
2023-09-09 11:54:27 +00:00
|
|
|
peers[peer_name] = {
|
2023-09-09 14:10:49 +00:00
|
|
|
'iface': sub('[^a-z0-9-_]+', '_', peer_name)[:12],
|
2023-09-09 11:54:27 +00:00
|
|
|
}
|
2020-11-21 14:38:38 +00:00
|
|
|
|
2024-03-23 09:19:15 +00:00
|
|
|
try:
|
|
|
|
repo.get_node(peer_name)
|
|
|
|
|
|
|
|
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}')
|
|
|
|
except NoSuchNode:
|
|
|
|
pass
|
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):
|
2023-09-23 13:04:11 +00:00
|
|
|
if node.name not in repo.libs.s2s.WG_AUTOGEN_NODES:
|
|
|
|
raise DoNotRunAgain
|
|
|
|
|
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
|
|
|
|
|
2023-09-23 13:04:11 +00:00
|
|
|
if rnode.name not in repo.libs.s2s.WG_AUTOGEN_NODES:
|
|
|
|
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),
|
2023-09-23 13:04:47 +00:00
|
|
|
'my_port': base_port + repo.libs.s2s.WG_AUTOGEN_NODES.index(rnode.name),
|
2021-09-29 17:27:13 +00:00
|
|
|
'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
|
|
|
|
|
2023-12-10 14:20:05 +00:00
|
|
|
if repo.libs.s2s.WG_AUTOGEN_SETTINGS.get(name, {}).get('no_autoconnect'):
|
|
|
|
continue
|
2021-09-29 17:27:13 +00:00
|
|
|
|
|
|
|
peers[rnode.name] = {
|
|
|
|
'endpoint': '{}:{}'.format(
|
2023-12-10 14:20:05 +00:00
|
|
|
rnode.hostname,
|
|
|
|
rnode.metadata.get(f'wireguard/peers/{node.name}/my_port'),
|
2021-09-29 17:27:13 +00:00
|
|
|
),
|
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 = {}
|
|
|
|
|
2023-09-09 11:54:27 +00:00
|
|
|
for peer, config in 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}'] = {
|
2023-09-09 11:54:27 +00:00
|
|
|
'command_on_monitored_host': config['pubkey'].format_into(f'sudo /usr/local/share/icinga/plugins/check_wireguard_connected wg_{config["iface"]} {{}}'),
|
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:
|
2023-12-10 15:57:19 +00:00
|
|
|
ports['{}/udp'.format(config['my_port'])] = atomic(
|
|
|
|
set(repo.libs.s2s.WG_AUTOGEN_SETTINGS.get(name, {}).get('firewall', set())) | {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 = {}
|
2023-09-10 19:19:23 +00:00
|
|
|
snat_ip = metadata.get('wireguard/snat_ip', None)
|
|
|
|
|
2023-09-09 11:54:27 +00:00
|
|
|
for peer, config in sorted(metadata.get('wireguard/peers', {}).items()):
|
2023-09-24 16:56:50 +00:00
|
|
|
routes = {}
|
|
|
|
|
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'])
|
2023-09-10 19:19:23 +00:00
|
|
|
|
|
|
|
ips = {my_ip}
|
2024-03-22 21:52:12 +00:00
|
|
|
if snat_ip and peer in repo.libs.s2s.WG_AUTOGEN_NODES:
|
2023-09-10 19:19:23 +00:00
|
|
|
ips.add(snat_ip)
|
2023-09-24 16:56:50 +00:00
|
|
|
|
|
|
|
their_ip = config['their_ip']
|
|
|
|
if '/' in their_ip:
|
|
|
|
their_ip = their_ip.split('/')[0]
|
|
|
|
|
|
|
|
for route in config.get('routes', set()):
|
|
|
|
routes[route] = {'via': their_ip}
|
|
|
|
|
2023-09-09 11:54:27 +00:00
|
|
|
interfaces[f'wg_{config["iface"]}'] = {
|
2023-09-24 16:56:50 +00:00
|
|
|
'activation_policy': 'up' if config.get('auto_connection', True) else 'manual',
|
2023-09-10 19:19:23 +00:00
|
|
|
'ips': ips,
|
2023-09-24 16:56:50 +00:00
|
|
|
'routes': routes,
|
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(
|
2023-09-24 18:59:58 +00:00
|
|
|
'nftables/forward/10-wireguard',
|
|
|
|
'nftables/postrouting/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
|
|
|
|
|
2023-09-10 19:19:23 +00:00
|
|
|
snat_ip = metadata.get('wireguard/snat_ip', None)
|
|
|
|
|
2023-09-24 18:59:58 +00:00
|
|
|
forward = set()
|
|
|
|
postrouting = set()
|
2023-09-09 11:54:27 +00:00
|
|
|
for peer, config in sorted(metadata.get('wireguard/peers', {}).items()):
|
2023-09-24 18:59:58 +00:00
|
|
|
forward.add(f'iifname wg_{config["iface"]} accept')
|
|
|
|
forward.add(f'oifname wg_{config["iface"]} accept')
|
2021-09-29 17:43:29 +00:00
|
|
|
|
2024-03-22 21:52:12 +00:00
|
|
|
if snat_ip and peer in repo.libs.s2s.WG_AUTOGEN_NODES:
|
2023-09-24 18:59:58 +00:00
|
|
|
postrouting.add('ip saddr {} ip daddr != {} snat to {}'.format(
|
2021-09-29 17:43:29 +00:00
|
|
|
config['my_ip'],
|
|
|
|
config['their_ip'],
|
2023-09-10 19:19:23 +00:00
|
|
|
snat_ip,
|
2021-09-29 17:43:29 +00:00
|
|
|
))
|
2024-03-22 21:52:12 +00:00
|
|
|
elif config.get('masquerade', False):
|
|
|
|
postrouting.add(f'oifname wg_{peer} masquerade')
|
2021-09-29 17:43:29 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'nftables': {
|
2023-09-24 18:59:58 +00:00
|
|
|
'forward': {
|
|
|
|
'10-wireguard': sorted(forward),
|
|
|
|
},
|
|
|
|
'postrouting': {
|
|
|
|
'10-wireguard': sorted(postrouting),
|
2021-09-29 17:43:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|