Support custom allowed_ips field for services
Like allowed_prefixes, but for single IP addresses. Currently used just for DHCP server to allow (only) packets from relays.
This commit is contained in:
parent
01a27e45ce
commit
036f7c8b74
4 changed files with 26 additions and 22 deletions
|
@ -1,5 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import ipaddress
|
||||
import os
|
||||
import pynetbox
|
||||
|
||||
|
@ -38,12 +39,21 @@ class FilterModule(object):
|
|||
|
||||
def allowed_prefixes(self, service):
|
||||
'''Return a list of allowed IP prefixes for the given service'''
|
||||
# Depending on how service was obtained, actual data might be nested under custom_fields.
|
||||
if 'custom_fields' in service:
|
||||
service = service['custom_fields']
|
||||
|
||||
def ip2str(address, single=False):
|
||||
if single: # 1.2.3.4/24 → 1.2.3.4
|
||||
address = ipaddress.ip_interface(address).ip
|
||||
return str(ipaddress.ip_interface(address))
|
||||
|
||||
if ips := service.get('allowed_ips'):
|
||||
yield from (ip2str(ip['address'], single=True) for ip in ips)
|
||||
if prefixes := service.get('allowed_prefixes'):
|
||||
yield from self.nb.ipam.prefixes.filter(id=[prefix['id'] for prefix in prefixes])
|
||||
yield from (ip2str(e.prefix) for e in self.nb.ipam.prefixes.filter(id=[prefix['id'] for prefix in prefixes]))
|
||||
if vlans := service.get('allowed_vlans'):
|
||||
yield from self.nb.ipam.prefixes.filter(vlan_id=[vlan['id'] for vlan in vlans])
|
||||
yield from (ip2str(e.prefix) for e in self.nb.ipam.prefixes.filter(vlan_id=[vlan['id'] for vlan in vlans]))
|
||||
if clusters := service.get('allowed_clusters'):
|
||||
for device in self.nb.dcim.devices.filter(cluster_id=[cluster['id'] for cluster in clusters]):
|
||||
yield from self.nb.ipam.ip_addresses.filter(role='loopback', device_id=device.id)
|
||||
yield from (ip2str(e.address) for e in self.nb.ipam.ip_addresses.filter(role='loopback', device_id=device.id))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue