Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove file_path from fact attribute #52

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions shadycompass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from shadycompass.facts.filemetadata import FileMetadataCache
from shadycompass.rules.all import AllRules

BANNER="""
BANNER = """
N
.--^-.
/ . . . \\
Expand All @@ -33,6 +33,9 @@
shadycompass - https://github.com/double16/shadycompass
"""

FILE_PATH_ATTR = 'file_path'


class ShadyCompassEngine(
KnowledgeEngine,
AllRules,
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions shadycompass/facts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_shadycompassengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down