in packages/aws-rfdk/lib/deadline/scripts/python/configure_identity_registration_settings.py [0:0]
def cidr_to_ipv4_match(cidr: str) -> str:
network = ipaddress.ip_network(cidr)
if not isinstance(network, ipaddress.IPv4Network):
raise TypeError(f'"{cidr}" is not an IPv4 network')
# Get the network address, net mask, and host mask as byte arrays
nw_address = network.network_address.packed
netmask = network.netmask.packed
hostmask = network.hostmask.packed
ipv4_match_octets: List[str] = []
for byte_netmask, byte_hostmask, byte_nw_address in zip(netmask, hostmask, nw_address):
if byte_netmask == BYTE_MASK:
# If all bits in the byte are part of the netmask, just set
# the byte as defined in the network address
ipv4_match_octets.append(str(byte_nw_address))
elif byte_netmask == 0:
# If none of the bits of the byte are part of the netmask, the
# byte is free and we can match any IP
ipv4_match_octets.append('*')
else:
# In this case, the byte is partially fixed and we need to find the
# range
byte_min = byte_netmask & byte_nw_address
byte_max = byte_min + byte_hostmask
ipv4_match_octets.append(f'{byte_min}-{byte_max}')
return '.'.join(ipv4_match_octets)