-
Notifications
You must be signed in to change notification settings - Fork 24
/
anonymize_ips.py
81 lines (65 loc) · 2.54 KB
/
anonymize_ips.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Anonymize IPs."""
import os
import sys
import argparse
import fileinput
import random
import string
from netconan.ip_anonymization import IpAnonymizer, IpV6Anonymizer, anonymize_ip_addr # type: ignore
TEST_DATA_PATH = os.path.join("tests", "unit", "data")
_DEFAULT_SALT_LENGTH = 16
_CHAR_CHOICES = string.ascii_letters + string.digits
SALT = "".join(random.choice(_CHAR_CHOICES) for _ in range(_DEFAULT_SALT_LENGTH)) # nosec
# RFC 5737
IPV4_DOCUMENTATION_ADDRESSES = ["192.0.2.0/24", "198.51.100.0/24", "203.0.113.0/24"]
anonymizer4 = IpAnonymizer(
SALT,
None,
preserve_addresses=IPV4_DOCUMENTATION_ADDRESSES,
preserve_suffix=None,
)
anonymizer6 = IpV6Anonymizer(SALT, preserve_suffix=None)
def replace(filename):
"""Replace line when a substitution is needed."""
partial_report = ""
try:
with open(filename, encoding="utf-8") as file:
file.read()
except (UnicodeDecodeError, RuntimeError) as error:
return f"Warning: Not able to process {filename}: {error}\n"
with fileinput.input(files=(filename,), inplace=True) as file:
for line in file:
newline = anonymize_ip_addr(anonymizer6, line, False)
newline = anonymize_ip_addr(anonymizer4, newline, False)
sys.stdout.write(newline)
if line != newline:
if not partial_report:
partial_report = f"IP Address found and replaced in {filename}\n"
partial_report += f" - {line}"
return partial_report
def main():
"""Main function."""
parser = argparse.ArgumentParser(description="Clean IP Addresses.")
parser.add_argument("--provider", help="Provider folder name, under tests/unit/data to anonymize.", required=False)
parser.add_argument("--file", help="Absolute filename name to anonymize.", required=False)
args = parser.parse_args()
if args.provider and args.file:
print("Provider and File are mutually exclusive, choose one.")
sys.exit(0)
elif args.provider:
path = os.path.join(TEST_DATA_PATH, args.provider)
elif args.file:
path = args.file
else:
path = TEST_DATA_PATH
report = ""
for (dirpath, _, files) in os.walk(path):
for file in files:
filename = os.path.join(dirpath, file)
report += replace(filename)
print(
f"{report}" "\nIPv4 and IPv6 addresses have been anonymized.",
" \nPlease, keep in mind that this could uncover some parsing dependencies on white spaces.",
)
if __name__ == "__main__":
main()