From ce5d4f53003d56dbed714d309323dd675effbd42 Mon Sep 17 00:00:00 2001 From: Jake Hunsaker Date: Wed, 31 May 2023 10:18:19 -0400 Subject: [PATCH] [keywords] Add new Prepper Adds a new Prepper to handle keyword preparation. This is slightly inefficient since we will only realistically need this once, but baking it into the archive loop does not pose any other problems, and it would be more fragile to break out a special flow just for keywords. Signed-off-by: Jake Hunsaker --- sos/cleaner/__init__.py | 3 +-- sos/cleaner/parsers/keyword_parser.py | 17 +----------- sos/cleaner/preppers/keywords.py | 37 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 sos/cleaner/preppers/keywords.py diff --git a/sos/cleaner/__init__.py b/sos/cleaner/__init__.py index 87001aece1..feeedf6680 100644 --- a/sos/cleaner/__init__.py +++ b/sos/cleaner/__init__.py @@ -130,8 +130,7 @@ def __init__(self, parser=None, args=None, cmdline=None, in_place=False, SoSIPParser(self.cleaner_mapping), SoSIPv6Parser(self.cleaner_mapping), SoSMacParser(self.cleaner_mapping), - SoSKeywordParser(self.cleaner_mapping, self.opts.keywords, - self.opts.keyword_file), + SoSKeywordParser(self.cleaner_mapping), SoSUsernameParser(self.cleaner_mapping) ] diff --git a/sos/cleaner/parsers/keyword_parser.py b/sos/cleaner/parsers/keyword_parser.py index 9a0f65ba8a..f611ccd2b1 100644 --- a/sos/cleaner/parsers/keyword_parser.py +++ b/sos/cleaner/parsers/keyword_parser.py @@ -8,7 +8,6 @@ # # See the LICENSE file in the source distribution for further information. -import os from sos.cleaner.parsers import SoSCleanerParser from sos.cleaner.mappings.keyword_map import SoSKeywordMap @@ -21,23 +20,9 @@ class SoSKeywordParser(SoSCleanerParser): name = 'Keyword Parser' map_file_key = 'keyword_map' - def __init__(self, config, keywords=None, keyword_file=None): + def __init__(self, config): self.mapping = SoSKeywordMap() - self.user_keywords = [] super(SoSKeywordParser, self).__init__(config) - for _keyword in self.mapping.dataset.keys(): - self.user_keywords.append(_keyword) - if keywords: - for keyword in keywords: - if keyword not in self.user_keywords: - # pre-generate an obfuscation mapping for each keyword - # this is necessary for cases where filenames are being - # obfuscated before or instead of file content - self.mapping.get(keyword.lower()) - self.user_keywords.append(keyword) - if keyword_file and os.path.exists(keyword_file): - with open(keyword_file, 'r') as kwf: - self.user_keywords.extend(kwf.read().splitlines()) def _parse_line(self, line): return line, 0 diff --git a/sos/cleaner/preppers/keywords.py b/sos/cleaner/preppers/keywords.py new file mode 100644 index 0000000000..9baf86a2a8 --- /dev/null +++ b/sos/cleaner/preppers/keywords.py @@ -0,0 +1,37 @@ +# Copyright 2023 Red Hat, Inc. Jake Hunsaker + +# This file is part of the sos project: https://github.com/sosreport/sos +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions of +# version 2 of the GNU General Public License. +# +# See the LICENSE file in the source distribution for further information. + +import os + +from sos.cleaner.preppers import SoSPrepper + + +class KeywordPrepper(SoSPrepper): + """ + Prepper to handle keywords passed to cleaner via either the `--keywords` + or `--keyword-file` options. + """ + + name = 'keyword' + + def _get_items_for_keyword(self, archive): + items = [] + for kw in self.opts.keywords: + items.append(kw) + if self.opts.keyword_file and os.path.exists(self.opts.keyword_file): + with open(self.opts.keyword_file, 'r') as kwf: + items.extend(kwf.read().splitlines()) + + for item in items: + self.regex_items['keyword'].add(item) + + return items + +# vim: set et ts=4 sw=4 :