def __init__(self, seed: int = 0, preserve_v4_prefix: list = []): """Initiaize the anonymizer with a seed""" if not seed: seed = int(time.time()) preserve_v4_prefix.append('0.0.0.0/0') self.fakeit = Factory.create() self.fakeit.seed(seed) salt = self.fakeit.password(length=16) self.anonv4 = IpAnonymizer(salt, preserve_prefixes=preserve_v4_prefix) self.anonv6 = IpV6Anonymizer(salt) self.hostmap = {} self.macmap = defaultdict(self.fakeit.mac_address) # Courtesy of Stackoverflow # (https://stackoverflow.com/questions/1418423/the-hostname-regex) self.hname_re = r'([a-zA-Z0-9](?:(?:[_a-zA-Z0-9-]*|(?<!-)\.(?![-.]))*[_a-zA-Z0-9]+)?)' # This regex will not catch the macaddr in this format: # "This is a macaddr:00:01:02:FF:cf:9b" # because the macaddr is preceded by :. It will also not match: # "00:01:29:AB:cf:47:01" because there's one more group (:01) after # the mac address self.macaddr_re = r'(?<!:)([0-9a-f]{2}(?::[0-9a-f]{2}){5})(?!:)' # NXOS and EOS use this format self.nxos_macaddr_re = r'([0-9A-F]{4}\.[0-9A-F]{4}\.[0-9A-F]{4})' self.preset_hostname_jpaths = [ ['TABLE_nbor.ROW_nbor[*].chassis_id'], # NXOS LLDP # NXOS CDP [ 'TABLE_cdp_neighbor_brief_info.ROW_cdp_neighbor_brief_info[*].device_id' ], ['lldpNeighbors.*.lldpNeighborInfo[*].systemName'], # EOS ['lldp[*].interface[*].chassis[*].name[*].value'], # lldpd [ 'lldp-neighbors-information.[0].lldp-neighbor-information[*].lldp-remote-system-name.[0].data' ], # JunOS ['*.*.neighborCapabilities[*].hostName.advHostName'], # FRR BGP ['*.*.neighborCapabilities[*].hostName.rcvHostName'], # FRR BGP ['*.*.neighborCapabilities[*].hostName.advDomainName'], # FRR BGP ['*.*.neighborCapabilities[*].hostName.rcvDomainName'], # FRR BGP ['host_name'] # NXOS show version output ] self.preset_hostname_pfxlst = [ ['SysName:'], ['Static hostname:'], ] # Needed for NXOS and EOS which use the non-standard MACADDR format self.preset_macaddr_jpaths = [ ['configMacAddress'], # EOS show version output ['systemMacAddress'], # EOS show version output ['hwMacAddress'], # EOS show version output ]
def anonymizer(request): """Create a generic fixture for different types of anonymizers.""" if request.param == "v4": return IpAnonymizer(SALT) elif request.param == "v6": return IpV6Anonymizer(SALT) elif request.param == "flipv4": return IpAnonymizer(SALT, salter=lambda a, b: 1) else: raise ValueError("Invalid anonymizer type {}".format(request.param))
def anonymizer_v6(): """All tests in this module use a single IPv6 anonymizer.""" return IpV6Anonymizer(SALT)