diff options
author | Jake Hunsaker <jhunsake@redhat.com> | 2020-11-17 13:43:02 -0500 |
---|---|---|
committer | Jake Hunsaker <jhunsake@redhat.com> | 2020-11-18 10:06:22 -0500 |
commit | 7314865bab8569e2d6f64e0e30f4118c18bdc2f6 (patch) | |
tree | 2cc57ce06134a20ad6a660bc96fbe4de385ec5f3 | |
parent | 7d74a4aa1e80f109083cebc312a95a6d0dab819e (diff) | |
download | sos-7314865bab8569e2d6f64e0e30f4118c18bdc2f6.tar.gz |
[cleaner] Match MAC addrs in lines that end in punctuation
It was found that sometimes MAC addresses would not be obfuscated from
certain files when the MAC address was followed by punctuation, such as
periods. This was because the regex used to match IPv4 MAC addresses
needs to avoid matching a substring within IPv6 mac addresses.
Update the regex to compensate, and then override the `parse_line()`
method to pull the matched punctuation out of the matched MAC substring
so that obfuscation remains consistent.
Resolves: #2311
Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
-rw-r--r-- | sos/cleaner/parsers/mac_parser.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/sos/cleaner/parsers/mac_parser.py b/sos/cleaner/parsers/mac_parser.py index 6b4be905..5bb5a7b4 100644 --- a/sos/cleaner/parsers/mac_parser.py +++ b/sos/cleaner/parsers/mac_parser.py @@ -11,6 +11,8 @@ from sos.cleaner.parsers import SoSCleanerParser from sos.cleaner.mappings.mac_map import SoSMacMap +import re + class SoSMacParser(SoSCleanerParser): """Handles parsing for MAC addresses""" @@ -21,7 +23,7 @@ class SoSMacParser(SoSCleanerParser): r'(([^:|-])([0-9a-fA-F]{2}(:|-)){7}[0-9a-fA-F]{2}(\s|$))', r'(([^:|-])([0-9a-fA-F]{4}(:|-)){3}[0-9a-fA-F]{4}(\s|$))', # IPv4, avoiding matching a substring within IPv6 addresses - r'(([^:|-])([0-9a-fA-F]{2}([:-])){5}([0-9a-fA-F]){2}(\s|$))' + r'(([^:|-])([0-9a-fA-F]{2}([:-])){5}([0-9a-fA-F]){2}(\.|,|!)?(\s|$))' ] map_file_key = 'mac_map' prep_map_file = 'sos_commands/networking/ip_-d_address' @@ -29,3 +31,22 @@ class SoSMacParser(SoSCleanerParser): def __init__(self, conf_file=None): self.mapping = SoSMacMap() super(SoSMacParser, self).__init__(conf_file) + + def parse_line(self, line): + """Override the base parse_line to account for MAC matches that end + a line with punctuation, which may or may not be beneficial to have in + other parsers + """ + count = 0 + for skip_pattern in self.skip_line_patterns: + if re.match(skip_pattern, line, re.I): + return line, count + for pattern in self.regex_patterns: + matches = [m[0] for m in re.findall(pattern, line, re.I)] + if matches: + count += len(matches) + for match in matches: + stripped_match = match.rstrip('.,!').strip() + new_match = self.mapping.get(stripped_match) + line = line.replace(stripped_match, new_match) + return line, count |