libs.tools.resolve_identifier: add support for named networks
All checks were successful
kunsi/bundlewrap/pipeline/head This commit looks good
All checks were successful
kunsi/bundlewrap/pipeline/head This commit looks good
This commit is contained in:
parent
969b45d9f7
commit
1742f51778
3 changed files with 707 additions and 8 deletions
35
libs/firewall.py
Normal file
35
libs/firewall.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
from os.path import abspath, dirname, join
|
||||
from ipaddress import ip_network, IPv4Network
|
||||
|
||||
REPO_PATH = dirname(dirname(abspath(__file__)))
|
||||
|
||||
def generate_ip_list_from_routes(filename):
|
||||
# generated using:
|
||||
# whois -i origin as8881 | awk '/^route/ {print $2}' > configs/as8881.txt
|
||||
with open(join(REPO_PATH, 'configs', f'{filename}.txt')) as f:
|
||||
networks = f.read().splitlines()
|
||||
|
||||
result = {
|
||||
'ipv4': set(),
|
||||
'ipv6': set(),
|
||||
}
|
||||
|
||||
for line in networks:
|
||||
line = line.strip()
|
||||
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
|
||||
ip = ip_network(line)
|
||||
|
||||
if isinstance(ip, IPv4Network):
|
||||
result['ipv4'].add(ip)
|
||||
else:
|
||||
result['ipv6'].add(ip)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
named_networks = {
|
||||
'versatel': generate_ip_list_from_routes('as8881'),
|
||||
}
|
|
@ -22,14 +22,13 @@ def resolve_identifier(repo, identifier):
|
|||
return {'ipv4': {ip}, 'ipv6': set()}
|
||||
else:
|
||||
return {'ipv4': set(), 'ipv6': {ip}}
|
||||
except Exception as e:
|
||||
io.stderr('{x} {t} Exception while resolving "{i}": {e}'.format(
|
||||
x=red('✘'),
|
||||
t=bold('libs.tools.resolve_identifier'),
|
||||
i=identifier,
|
||||
e=str(e),
|
||||
))
|
||||
raise
|
||||
except ValueError:
|
||||
try:
|
||||
return repo.libs.firewall.named_networks[identifier]
|
||||
except KeyError:
|
||||
raise BundleError(
|
||||
f'libs.tools.resolve_identifier(): Could not resolve {identifier}'
|
||||
)
|
||||
|
||||
found_ips = set()
|
||||
for node in nodes:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue