From ba3a2b5cfc4974a3d821585bbb90c8218f5d4cf3 Mon Sep 17 00:00:00 2001 From: Patrick Double Date: Tue, 21 May 2024 07:08:44 -0500 Subject: [PATCH] remove file_path from fact attribute (#52) Signed-off-by: Patrick Double --- shadycompass/__init__.py | 19 ++++++++++++++----- shadycompass/facts/__init__.py | 1 + tests/test_shadycompassengine.py | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/shadycompass/__init__.py b/shadycompass/__init__.py index d17528d..f4b3750 100644 --- a/shadycompass/__init__.py +++ b/shadycompass/__init__.py @@ -19,7 +19,7 @@ from shadycompass.facts.filemetadata import FileMetadataCache from shadycompass.rules.all import AllRules -BANNER=""" +BANNER = """ N .--^-. / . . . \\ @@ -33,6 +33,9 @@ shadycompass - https://github.com/double16/shadycompass """ +FILE_PATH_ATTR = 'file_path' + + class ShadyCompassEngine( KnowledgeEngine, AllRules, @@ -61,7 +64,7 @@ def update_facts(self): f'[!] returned fact is None from {file_path}, {type(fact_reader)}, implementation error, file report at https://github.com/double16/shadycompass/issues', file=sys.stderr) else: - the_fact.update({'file_path': file_path}) + setattr(the_fact, FILE_PATH_ATTR, file_path) self.declare(the_fact) except BaseException: print( @@ -70,7 +73,7 @@ def update_facts(self): else: # retract facts for files that have been removed for fact in self.facts.values(): - if fact.get('file_path') == file_path: + if hasattr(fact, FILE_PATH_ATTR) and getattr(fact, FILE_PATH_ATTR) == file_path: retract_queue.append(fact) for fact in retract_queue: self.retract(fact) @@ -204,7 +207,7 @@ def handle_tool_choices(self) -> bool: while True: print(f"\nChoose your preferred tool for {category}:", file=self.fd_out) for idx, name in enumerate(names): - print(f"{idx+1}. {name}") + print(f"{idx + 1}. {name}") print("0. no preference, consider all", file=self.fd_out) try: choice = int(input("? ").strip()) - 1 @@ -365,7 +368,7 @@ def show_config(self): with io.StringIO() as buffer: config.write(buffer) - config_string = '\n'+buffer.getvalue() + config_string = '\n' + buffer.getvalue() print(config_string, file=self.fd_out) @@ -394,6 +397,12 @@ def set_config_value(self, command: list[str]): value = arg else: raise ValueError(arg) + if option is None: + print('[!] option and value are required', file=self.fd_err) + return + if value is None: + print('[!] value is required', file=self.fd_err) + return self.engine.declare(ConfigFact(section=section, option=option, value=value, global0=global0)) self.print_save_config_warning() diff --git a/shadycompass/facts/__init__.py b/shadycompass/facts/__init__.py index 8664011..c26b5bc 100644 --- a/shadycompass/facts/__init__.py +++ b/shadycompass/facts/__init__.py @@ -825,6 +825,7 @@ def __init__(self, *args, **kwargs): class HttpBustingNeeded(Fact): + # TODO: consider replacing with ScanNeeded/ScanPresent secure = Field(bool, mandatory=True) addr = Field(str, mandatory=True) port = Field(int, mandatory=True) diff --git a/tests/test_shadycompassengine.py b/tests/test_shadycompassengine.py index 7b71563..3ed0109 100644 --- a/tests/test_shadycompassengine.py +++ b/tests/test_shadycompassengine.py @@ -60,7 +60,7 @@ def tearDown(self): def test_retract_facts(self): file_path = os.path.join(self.tempdir, 'hosts') - fact = HostnameIPv4Resolution(hostname='localhost', addr='127.0.0.1', file_path=file_path) + fact = HostnameIPv4Resolution(hostname='localhost', addr='127.0.0.1') assertFactIn(fact, self.engine) os.remove(file_path) self.engine.update_facts()