Skip to content

Commit

Permalink
[keywords] Add new Prepper
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
TurboTurtle committed Jun 22, 2023
1 parent 09c36b6 commit ce5d4f5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
3 changes: 1 addition & 2 deletions sos/cleaner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]

Expand Down
17 changes: 1 addition & 16 deletions sos/cleaner/parsers/keyword_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
37 changes: 37 additions & 0 deletions sos/cleaner/preppers/keywords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2023 Red Hat, Inc. Jake Hunsaker <[email protected]>

# 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 :

0 comments on commit ce5d4f5

Please sign in to comment.