diff options
-rw-r--r-- | sos/cleaner/mappings/hostname_map.py | 6 | ||||
-rw-r--r-- | sos/cleaner/parsers/hostname_parser.py | 8 | ||||
-rw-r--r-- | tests/cleaner_tests/full_report_run.py | 21 |
3 files changed, 28 insertions, 7 deletions
diff --git a/sos/cleaner/mappings/hostname_map.py b/sos/cleaner/mappings/hostname_map.py index e70a5530..0fe78fb1 100644 --- a/sos/cleaner/mappings/hostname_map.py +++ b/sos/cleaner/mappings/hostname_map.py @@ -169,13 +169,13 @@ class SoSHostnameMap(SoSMap): def sanitize_item(self, item): host = item.split('.') - if all([h.isupper() for h in host]): + if len(host) > 1 and all([h.isupper() for h in host]): # by convention we have just a domain _host = [h.lower() for h in host] return self.sanitize_domain(_host).upper() if len(host) == 1: # we have a shortname for a host - return self.sanitize_short_name(host[0]) + return self.sanitize_short_name(host[0].lower()) if len(host) == 2: # we have just a domain name, e.g. example.com return self.sanitize_domain(host) @@ -185,7 +185,7 @@ class SoSHostnameMap(SoSMap): domain = host[1:] # obfuscate the short name if len(hostname) > 2: - ob_hostname = self.sanitize_short_name(hostname) + ob_hostname = self.sanitize_short_name(hostname.lower()) else: # by best practice it appears the host part of the fqdn was cut # off due to some form of truncating, as such don't obfuscate diff --git a/sos/cleaner/parsers/hostname_parser.py b/sos/cleaner/parsers/hostname_parser.py index 0a733bee..7fd0e698 100644 --- a/sos/cleaner/parsers/hostname_parser.py +++ b/sos/cleaner/parsers/hostname_parser.py @@ -8,6 +8,8 @@ # # See the LICENSE file in the source distribution for further information. +import re + from sos.cleaner.parsers import SoSCleanerParser from sos.cleaner.mappings.hostname_map import SoSHostnameMap @@ -91,9 +93,9 @@ class SoSHostnameParser(SoSCleanerParser): """ if search in self.mapping.skip_keys: return ln, count - if search in ln: - count += ln.count(search) - ln = ln.replace(search, self.mapping.get(repl or search)) + _reg = re.compile(search, re.I) + if _reg.search(ln): + return _reg.subn(self.mapping.get(repl or search), ln) return ln, count count = 0 diff --git a/tests/cleaner_tests/full_report_run.py b/tests/cleaner_tests/full_report_run.py index 2de54946..0b23acaf 100644 --- a/tests/cleaner_tests/full_report_run.py +++ b/tests/cleaner_tests/full_report_run.py @@ -26,6 +26,24 @@ class FullCleanTest(StageTwoReportTest): # replace with an empty placeholder, make sure that this test case is not # influenced by previous clean runs files = ['/etc/sos/cleaner/default_mapping'] + packages = { + 'rhel': ['python3-systemd'], + 'ubuntu': ['python3-systemd'] + } + + def pre_sos_setup(self): + # ensure that case-insensitive matching of FQDNs and shortnames work + from systemd import journal + from socket import gethostname + host = gethostname() + short = host.split('.')[0] + sosfd = journal.stream('sos-testing') + sosfd.write( + "This is a test line from sos clean testing. The hostname %s " + "should not appear, nor should %s in an obfuscated archive. The " + "shortnames of %s and %s should also not appear." + % (host.lower(), host.upper(), short.lower(), short.upper()) + ) def test_private_map_was_generated(self): self.assertOutputContains('A mapping of obfuscated elements is available at') @@ -40,8 +58,9 @@ class FullCleanTest(StageTwoReportTest): def test_hostname_not_in_any_file(self): host = self.sysinfo['pre']['networking']['hostname'] + short = host.split('.')[0] # much faster to just use grep here - content = self.grep_for_content(host) + content = self.grep_for_content(host) + self.grep_for_content(short) if not content: assert True else: |