From d5979fae16da7f2f4fa72604d55a1af874e812a4 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 14 Sep 2022 13:48:55 -0400 Subject: [PATCH 1/5] Island: Fix typo and grammar in save_event_to_event_repository() --- .../agent_event_subscribers/save_event_to_event_repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/agent_event_subscribers/save_event_to_event_repository.py b/monkey/monkey_island/cc/agent_event_subscribers/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_subscribers/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}") From 4db251dd7d933d1f3381a77d94a4bb342bbaf3d9 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 14 Sep 2022 13:49:23 -0400 Subject: [PATCH 2/5] Island: Add save_stolen_credentials_to_repository --- .../cc/agent_event_subscribers/__init__.py | 1 + .../save_stolen_credentials_to_repository.py | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 monkey/monkey_island/cc/agent_event_subscribers/save_stolen_credentials_to_repository.py diff --git a/monkey/monkey_island/cc/agent_event_subscribers/__init__.py b/monkey/monkey_island/cc/agent_event_subscribers/__init__.py index f206087d7..755f52bd2 100644 --- a/monkey/monkey_island/cc/agent_event_subscribers/__init__.py +++ b/monkey/monkey_island/cc/agent_event_subscribers/__init__.py @@ -1 +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_stolen_credentials_to_repository.py b/monkey/monkey_island/cc/agent_event_subscribers/save_stolen_credentials_to_repository.py new file mode 100644 index 000000000..3ac80bb10 --- /dev/null +++ b/monkey/monkey_island/cc/agent_event_subscribers/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}") From b3e9bbe05f26aa6898c4ea3a3e5859d6e6535b18 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 14 Sep 2022 13:54:38 -0400 Subject: [PATCH 3/5] Island: Subscribe save_stolen_credentials_to_repository() --- .../monkey_island/cc/setup/agent_event_handlers.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/monkey/monkey_island/cc/setup/agent_event_handlers.py b/monkey/monkey_island/cc/setup/agent_event_handlers.py index 9fd0a2ea7..af37e6740 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_subscribers 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): @@ -14,3 +18,8 @@ def _subscribe_and_store_to_event_repository(container: DIContainer): event_repository = container.resolve(IEventRepository) save_event_subscriber = save_event_to_event_repository(event_repository) 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) From 03c49fe4da6de582a75a604713afc63a22928954 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 14 Sep 2022 13:55:13 -0400 Subject: [PATCH 4/5] Island: Shorten subscription code --- monkey/monkey_island/cc/setup/agent_event_handlers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/monkey/monkey_island/cc/setup/agent_event_handlers.py b/monkey/monkey_island/cc/setup/agent_event_handlers.py index af37e6740..c859be28e 100644 --- a/monkey/monkey_island/cc/setup/agent_event_handlers.py +++ b/monkey/monkey_island/cc/setup/agent_event_handlers.py @@ -15,8 +15,7 @@ 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( From 5742681b1399ab3b290293bea702339cad65a295 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 14 Sep 2022 13:58:12 -0400 Subject: [PATCH 5/5] Island: Rename agent_event_subscribers -> agent_event_handlers --- .../__init__.py | 0 .../save_event_to_event_repository.py | 0 .../save_stolen_credentials_to_repository.py | 0 monkey/monkey_island/cc/setup/agent_event_handlers.py | 2 +- 4 files changed, 1 insertion(+), 1 deletion(-) rename monkey/monkey_island/cc/{agent_event_subscribers => agent_event_handlers}/__init__.py (100%) rename monkey/monkey_island/cc/{agent_event_subscribers => agent_event_handlers}/save_event_to_event_repository.py (100%) rename monkey/monkey_island/cc/{agent_event_subscribers => agent_event_handlers}/save_stolen_credentials_to_repository.py (100%) diff --git a/monkey/monkey_island/cc/agent_event_subscribers/__init__.py b/monkey/monkey_island/cc/agent_event_handlers/__init__.py similarity index 100% rename from monkey/monkey_island/cc/agent_event_subscribers/__init__.py rename to monkey/monkey_island/cc/agent_event_handlers/__init__.py 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 100% 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 diff --git a/monkey/monkey_island/cc/agent_event_subscribers/save_stolen_credentials_to_repository.py b/monkey/monkey_island/cc/agent_event_handlers/save_stolen_credentials_to_repository.py similarity index 100% rename from monkey/monkey_island/cc/agent_event_subscribers/save_stolen_credentials_to_repository.py rename to monkey/monkey_island/cc/agent_event_handlers/save_stolen_credentials_to_repository.py diff --git a/monkey/monkey_island/cc/setup/agent_event_handlers.py b/monkey/monkey_island/cc/setup/agent_event_handlers.py index c859be28e..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,7 @@ from common import DIContainer from common.event_queue import IAgentEventQueue from common.events import CredentialsStolenEvent -from monkey_island.cc.agent_event_subscribers import ( +from monkey_island.cc.agent_event_handlers import ( save_event_to_event_repository, save_stolen_credentials_to_repository, )