diff options
-rw-r--r-- | sos/cleaner/__init__.py | 18 | ||||
-rw-r--r-- | sos/cleaner/parsers/__init__.py | 16 | ||||
-rw-r--r-- | sos/cleaner/parsers/hostname_parser.py | 4 | ||||
-rw-r--r-- | sos/cleaner/parsers/ip_parser.py | 4 | ||||
-rw-r--r-- | sos/cleaner/parsers/keyword_parser.py | 4 | ||||
-rw-r--r-- | sos/cleaner/parsers/mac_parser.py | 4 | ||||
-rw-r--r-- | sos/cleaner/parsers/username_parser.py | 4 | ||||
-rw-r--r-- | tests/unittests/cleaner_tests.py | 10 |
8 files changed, 28 insertions, 36 deletions
diff --git a/sos/cleaner/__init__.py b/sos/cleaner/__init__.py index 4c983782..ca5f93e5 100644 --- a/sos/cleaner/__init__.py +++ b/sos/cleaner/__init__.py @@ -76,7 +76,7 @@ class SoSCleaner(SoSComponent): # when obfuscating a SoSCollector run during archive extraction os.makedirs(os.path.join(self.tmpdir, 'cleaner'), exist_ok=True) - self.validate_map_file() + self.cleaner_mapping = self.load_map_file() os.umask(0o77) self.in_place = in_place self.hash_name = self.policy.get_preferred_hash_name() @@ -84,12 +84,12 @@ class SoSCleaner(SoSComponent): self.cleaner_md = self.manifest.components.add_section('cleaner') self.parsers = [ - SoSHostnameParser(self.opts.map_file, self.opts.domains), - SoSIPParser(self.opts.map_file), - SoSMacParser(self.opts.map_file), - SoSKeywordParser(self.opts.map_file, self.opts.keywords, + SoSHostnameParser(self.cleaner_mapping, self.opts.domains), + SoSIPParser(self.cleaner_mapping), + SoSMacParser(self.cleaner_mapping), + SoSKeywordParser(self.cleaner_mapping, self.opts.keywords, self.opts.keyword_file), - SoSUsernameParser(self.opts.map_file, self.opts.usernames) + SoSUsernameParser(self.cleaner_mapping, self.opts.usernames) ] self.log_info("Cleaner initialized. From cmdline: %s" @@ -114,12 +114,13 @@ class SoSCleaner(SoSComponent): _fmt = _fmt + fill(line, width, replace_whitespace=False) + '\n' return _fmt - def validate_map_file(self): + def load_map_file(self): """Verifies that the map file exists and has usable content. If the provided map file does not exist, or it is empty, we will print a warning and continue on with cleaning building a fresh map """ + _conf = {} default_map = '/etc/sos/cleaner/default_mapping' if os.path.isdir(self.opts.map_file): raise Exception("Requested map file %s is a directory" @@ -132,13 +133,14 @@ class SoSCleaner(SoSComponent): else: with open(self.opts.map_file, 'r') as mf: try: - json.load(mf) + _conf = json.load(mf) except json.JSONDecodeError: self.log_error("ERROR: Unable to parse map file, json is " "malformed. Will not load any mappings.") except Exception as err: self.log_error("ERROR: Could not load '%s': %s" % (self.opts.map_file, err)) + return _conf def print_disclaimer(self): """When we are directly running `sos clean`, rather than hooking into diff --git a/sos/cleaner/parsers/__init__.py b/sos/cleaner/parsers/__init__.py index 852e20ef..3076db39 100644 --- a/sos/cleaner/parsers/__init__.py +++ b/sos/cleaner/parsers/__init__.py @@ -8,7 +8,6 @@ # # See the LICENSE file in the source distribution for further information. -import json import re @@ -52,18 +51,9 @@ class SoSCleanerParser(): map_file_key = 'unset' prep_map_file = 'unset' - def __init__(self, conf_file=None): - # attempt to load previous run data into the mapping for the parser - if conf_file: - try: - with open(conf_file, 'r') as map_file: - _default_mappings = json.load(map_file) - if self.map_file_key in _default_mappings: - self.mapping.conf_update( - _default_mappings[self.map_file_key] - ) - except (IOError, json.decoder.JSONDecodeError): - pass + def __init__(self, config={}): + if self.map_file_key in config: + self.mapping.conf_update(config[self.map_file_key]) def parse_line(self, line): """This will be called for every line in every file we process, so that diff --git a/sos/cleaner/parsers/hostname_parser.py b/sos/cleaner/parsers/hostname_parser.py index 3de6bb08..71e13d3f 100644 --- a/sos/cleaner/parsers/hostname_parser.py +++ b/sos/cleaner/parsers/hostname_parser.py @@ -21,9 +21,9 @@ class SoSHostnameParser(SoSCleanerParser): r'(((\b|_)[a-zA-Z0-9-\.]{1,200}\.[a-zA-Z]{1,63}(\b|_)))' ] - def __init__(self, conf_file=None, opt_domains=None): + def __init__(self, config, opt_domains=None): self.mapping = SoSHostnameMap() - super(SoSHostnameParser, self).__init__(conf_file) + super(SoSHostnameParser, self).__init__(config) self.mapping.load_domains_from_map() self.mapping.load_domains_from_options(opt_domains) self.short_names = [] diff --git a/sos/cleaner/parsers/ip_parser.py b/sos/cleaner/parsers/ip_parser.py index 08d1cd05..525139e8 100644 --- a/sos/cleaner/parsers/ip_parser.py +++ b/sos/cleaner/parsers/ip_parser.py @@ -43,6 +43,6 @@ class SoSIPParser(SoSCleanerParser): map_file_key = 'ip_map' prep_map_file = 'sos_commands/networking/ip_-o_addr' - def __init__(self, conf_file=None): + def __init__(self, config): self.mapping = SoSIPMap() - super(SoSIPParser, self).__init__(conf_file) + super(SoSIPParser, self).__init__(config) diff --git a/sos/cleaner/parsers/keyword_parser.py b/sos/cleaner/parsers/keyword_parser.py index 9134f82d..68de3727 100644 --- a/sos/cleaner/parsers/keyword_parser.py +++ b/sos/cleaner/parsers/keyword_parser.py @@ -22,10 +22,10 @@ class SoSKeywordParser(SoSCleanerParser): map_file_key = 'keyword_map' prep_map_file = '' - def __init__(self, conf_file=None, keywords=None, keyword_file=None): + def __init__(self, config, keywords=None, keyword_file=None): self.mapping = SoSKeywordMap() self.user_keywords = [] - super(SoSKeywordParser, self).__init__(conf_file) + super(SoSKeywordParser, self).__init__(config) for _keyword in self.mapping.dataset.keys(): self.user_keywords.append(_keyword) if keywords: diff --git a/sos/cleaner/parsers/mac_parser.py b/sos/cleaner/parsers/mac_parser.py index 618c43f8..7ca80b8d 100644 --- a/sos/cleaner/parsers/mac_parser.py +++ b/sos/cleaner/parsers/mac_parser.py @@ -32,9 +32,9 @@ class SoSMacParser(SoSCleanerParser): map_file_key = 'mac_map' prep_map_file = 'sos_commands/networking/ip_-d_address' - def __init__(self, conf_file=None): + def __init__(self, config): self.mapping = SoSMacMap() - super(SoSMacParser, self).__init__(conf_file) + super(SoSMacParser, self).__init__(config) def reduce_mac_match(self, match): """Strips away leading and trailing non-alphanum characters from any diff --git a/sos/cleaner/parsers/username_parser.py b/sos/cleaner/parsers/username_parser.py index 64843205..96ce5f0c 100644 --- a/sos/cleaner/parsers/username_parser.py +++ b/sos/cleaner/parsers/username_parser.py @@ -35,9 +35,9 @@ class SoSUsernameParser(SoSCleanerParser): 'ubuntu' ] - def __init__(self, conf_file=None, opt_names=None): + def __init__(self, config, opt_names=None): self.mapping = SoSUsernameMap() - super(SoSUsernameParser, self).__init__(conf_file) + super(SoSUsernameParser, self).__init__(config) self.mapping.load_names_from_options(opt_names) def load_usernames_into_map(self, content): diff --git a/tests/unittests/cleaner_tests.py b/tests/unittests/cleaner_tests.py index 5510dd80..cb20772f 100644 --- a/tests/unittests/cleaner_tests.py +++ b/tests/unittests/cleaner_tests.py @@ -100,11 +100,11 @@ class CleanerMapTests(unittest.TestCase): class CleanerParserTests(unittest.TestCase): def setUp(self): - self.ip_parser = SoSIPParser() - self.mac_parser = SoSMacParser() - self.host_parser = SoSHostnameParser(opt_domains='foobar.com') - self.kw_parser = SoSKeywordParser(keywords=['foobar']) - self.kw_parser_none = SoSKeywordParser() + self.ip_parser = SoSIPParser(config={}) + self.mac_parser = SoSMacParser(config={}) + self.host_parser = SoSHostnameParser(config={}, opt_domains='foobar.com') + self.kw_parser = SoSKeywordParser(config={}, keywords=['foobar']) + self.kw_parser_none = SoSKeywordParser(config={}) def test_ip_parser_valid_ipv4_line(self): line = 'foobar foo 10.0.0.1/24 barfoo bar' |