2023-02-05 16:30:58 +00:00
|
|
|
from ipaddress import IPv4Address, IPv4Network, ip_address, ip_network
|
2020-10-16 20:19:45 +00:00
|
|
|
|
2023-02-05 16:30:58 +00:00
|
|
|
from bundlewrap.exceptions import BundleError, NoSuchGroup, NoSuchNode
|
2021-02-14 16:04:52 +00:00
|
|
|
from bundlewrap.utils.text import bold, red
|
|
|
|
from bundlewrap.utils.ui import io
|
|
|
|
|
2023-02-05 16:30:58 +00:00
|
|
|
|
2024-12-22 03:59:55 +00:00
|
|
|
def resolve_identifier(repo, identifier, linklocal=False, only_physical=False, allow_private=True):
|
2020-10-16 20:19:45 +00:00
|
|
|
"""
|
|
|
|
Try to resolve an identifier (group or node). Return a set of ip
|
|
|
|
addresses valid for this identifier.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
nodes = {repo.get_node(identifier)}
|
|
|
|
except NoSuchNode:
|
|
|
|
try:
|
|
|
|
nodes = repo.nodes_in_group(identifier)
|
|
|
|
except NoSuchGroup:
|
|
|
|
try:
|
2021-02-15 12:44:21 +00:00
|
|
|
ip = ip_network(identifier)
|
2021-02-14 16:04:52 +00:00
|
|
|
|
2021-02-15 12:44:21 +00:00
|
|
|
if isinstance(ip, IPv4Network):
|
2021-02-14 16:04:52 +00:00
|
|
|
return {'ipv4': {ip}, 'ipv6': set()}
|
|
|
|
else:
|
|
|
|
return {'ipv4': set(), 'ipv6': {ip}}
|
2021-12-14 12:24:26 +00:00
|
|
|
except ValueError:
|
|
|
|
try:
|
|
|
|
return repo.libs.firewall.named_networks[identifier]
|
|
|
|
except KeyError:
|
|
|
|
raise BundleError(
|
|
|
|
f'libs.tools.resolve_identifier(): Could not resolve {identifier}'
|
|
|
|
)
|
2020-10-16 20:19:45 +00:00
|
|
|
|
|
|
|
found_ips = set()
|
|
|
|
for node in nodes:
|
|
|
|
for interface, config in node.metadata.get('interfaces', {}).items():
|
2023-09-09 13:37:37 +00:00
|
|
|
if only_physical and not (
|
|
|
|
interface.startswith('bond') or
|
|
|
|
interface.startswith('br') or
|
|
|
|
interface.startswith('en') or
|
|
|
|
interface.startswith('et') or
|
|
|
|
interface == 'default' # dummy nodes use these
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
|
2020-11-09 14:23:44 +00:00
|
|
|
for ip in config.get('ips', set()):
|
|
|
|
if '/' in ip:
|
|
|
|
found_ips.add(ip_address(ip.split('/')[0]))
|
|
|
|
else:
|
|
|
|
found_ips.add(ip_address(ip))
|
2020-10-16 20:19:45 +00:00
|
|
|
|
2020-11-08 09:41:41 +00:00
|
|
|
if node.metadata.get('external_ipv4', None):
|
2020-10-16 20:19:45 +00:00
|
|
|
found_ips.add(ip_address(node.metadata.get('external_ipv4')))
|
|
|
|
|
2022-01-04 10:41:31 +00:00
|
|
|
try:
|
|
|
|
found_ips.add(ip_address(node.hostname))
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2020-11-09 17:46:37 +00:00
|
|
|
ip_dict = {
|
|
|
|
'ipv4': set(),
|
|
|
|
'ipv6': set(),
|
|
|
|
}
|
|
|
|
|
2024-12-22 03:59:55 +00:00
|
|
|
has_public_ips = bool([ip for ip in found_ips if not ip.is_private])
|
|
|
|
|
2020-11-09 17:46:37 +00:00
|
|
|
for ip in found_ips:
|
2023-09-09 13:37:37 +00:00
|
|
|
if ip.is_link_local and not linklocal:
|
|
|
|
continue
|
|
|
|
|
2024-12-22 03:59:55 +00:00
|
|
|
if ip.is_private and not allow_private and has_public_ips:
|
|
|
|
continue
|
|
|
|
|
2020-11-09 17:46:37 +00:00
|
|
|
if isinstance(ip, IPv4Address):
|
|
|
|
ip_dict['ipv4'].add(ip)
|
|
|
|
else:
|
|
|
|
ip_dict['ipv6'].add(ip)
|
|
|
|
|
|
|
|
return ip_dict
|
2021-04-01 14:27:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def remove_more_specific_subnets(input_subnets) -> list:
|
|
|
|
final_subnets = []
|
|
|
|
|
|
|
|
for subnet in sorted(input_subnets):
|
|
|
|
source = ip_network(subnet)
|
|
|
|
|
|
|
|
if not source in final_subnets:
|
|
|
|
subnet_found = False
|
|
|
|
|
|
|
|
for dest_subnet in final_subnets:
|
|
|
|
if source.subnet_of(dest_subnet):
|
|
|
|
subnet_found = True
|
|
|
|
|
|
|
|
if not subnet_found:
|
|
|
|
final_subnets.append(source)
|
|
|
|
|
|
|
|
out = []
|
|
|
|
for net in final_subnets:
|
|
|
|
out.append(str(net))
|
|
|
|
|
|
|
|
return out
|
2021-04-02 16:57:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def require_bundle(node, bundle, hint=''):
|
|
|
|
# It's considered bad style to use assert statements outside of tests.
|
|
|
|
# That's why this little helper function exists, so we have an easy
|
|
|
|
# way of defining bundle requirements in other bundles.
|
|
|
|
if not node.has_bundle(bundle):
|
|
|
|
raise BundleError(f'{node.name} requires bundle {bundle}, but wasn\'t found! {hint}')
|