diff --git a/monkey/monkey_island/cc/agent_event_handlers/__init__.py b/monkey/monkey_island/cc/agent_event_handlers/__init__.py new file mode 100644 index 000000000..755f52bd2 --- /dev/null +++ b/monkey/monkey_island/cc/agent_event_handlers/__init__.py @@ -0,0 +1,2 @@ +from .save_event_to_event_repository import save_event_to_event_repository +from .save_stolen_credentials_to_repository import save_stolen_credentials_to_repository diff --git a/monkey/monkey_island/cc/agent_event_subscribers/save_event_to_event_repository.py b/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py similarity index 85% rename from monkey/monkey_island/cc/agent_event_subscribers/save_event_to_event_repository.py rename to monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py index 0249369b3..4d69b6283 100644 --- a/monkey/monkey_island/cc/agent_event_subscribers/save_event_to_event_repository.py +++ b/monkey/monkey_island/cc/agent_event_handlers/save_event_to_event_repository.py @@ -14,4 +14,4 @@ class save_event_to_event_repository: try: self._event_repository.save_event(event) except StorageError as err: - logger.error(f"Error occured storing event {event}: {err}") + logger.error(f"Error occurred while storing event {event}: {err}") diff --git a/monkey/monkey_island/cc/agent_event_handlers/save_stolen_credentials_to_repository.py b/monkey/monkey_island/cc/agent_event_handlers/save_stolen_credentials_to_repository.py new file mode 100644 index 000000000..3ac80bb10 --- /dev/null +++ b/monkey/monkey_island/cc/agent_event_handlers/save_stolen_credentials_to_repository.py @@ -0,0 +1,23 @@ +import logging + +from common.events import CredentialsStolenEvent +from monkey_island.cc.repository import ICredentialsRepository, StorageError + +logger = logging.getLogger(__name__) + + +class save_stolen_credentials_to_repository: + """ + Accepts CredentialsStolenEvent and pushes the stolen credentials into ICredentialsRepository + """ + + def __init__(self, credentials_repository: ICredentialsRepository): + self._credentials_repository = credentials_repository + + def __call__(self, credentials_stolen_event: CredentialsStolenEvent): + try: + self._credentials_repository.save_stolen_credentials( + credentials_stolen_event.stolen_credentials + ) + except StorageError as err: + logger.error(f"Error occurred while storing stolen credentials: {err}") diff --git a/monkey/monkey_island/cc/agent_event_subscribers/__init__.py b/monkey/monkey_island/cc/agent_event_subscribers/__init__.py deleted file mode 100644 index f206087d7..000000000 --- a/monkey/monkey_island/cc/agent_event_subscribers/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .save_event_to_event_repository import save_event_to_event_repository diff --git a/monkey/monkey_island/cc/setup/agent_event_handlers.py b/monkey/monkey_island/cc/setup/agent_event_handlers.py index 9fd0a2ea7..d3df0ce4f 100644 --- a/monkey/monkey_island/cc/setup/agent_event_handlers.py +++ b/monkey/monkey_island/cc/setup/agent_event_handlers.py @@ -1,7 +1,11 @@ from common import DIContainer from common.event_queue import IAgentEventQueue -from monkey_island.cc.agent_event_subscribers import save_event_to_event_repository -from monkey_island.cc.repository import IEventRepository +from common.events import CredentialsStolenEvent +from monkey_island.cc.agent_event_handlers import ( + save_event_to_event_repository, + save_stolen_credentials_to_repository, +) +from monkey_island.cc.repository import ICredentialsRepository, IEventRepository def setup_agent_event_handlers(container: DIContainer): @@ -11,6 +15,10 @@ def setup_agent_event_handlers(container: DIContainer): def _subscribe_and_store_to_event_repository(container: DIContainer): agent_event_queue = container.resolve(IAgentEventQueue) - event_repository = container.resolve(IEventRepository) - save_event_subscriber = save_event_to_event_repository(event_repository) + save_event_subscriber = save_event_to_event_repository(container.resolve(IEventRepository)) agent_event_queue.subscribe_all_events(save_event_subscriber) + + save_stolen_credentials_subscriber = save_stolen_credentials_to_repository( + container.resolve(ICredentialsRepository) + ) + agent_event_queue.subscribe_type(CredentialsStolenEvent, save_stolen_credentials_subscriber)