From e50b034324dccd970ece7824dae0c7429440d645 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Mon, 12 Sep 2022 13:17:07 +0200 Subject: [PATCH 01/15] Island: Create callable reset_agent_configuration class --- .../cc/services/reset_agent_configuration.py | 25 +++++++++ .../test_reset_agent_configuration.py | 56 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 monkey/monkey_island/cc/services/reset_agent_configuration.py create mode 100644 monkey/tests/unit_tests/monkey_island/cc/services/test_reset_agent_configuration.py diff --git a/monkey/monkey_island/cc/services/reset_agent_configuration.py b/monkey/monkey_island/cc/services/reset_agent_configuration.py new file mode 100644 index 000000000..081922e7a --- /dev/null +++ b/monkey/monkey_island/cc/services/reset_agent_configuration.py @@ -0,0 +1,25 @@ +from monkey_island.cc.repository import IAgentConfigurationRepository, IFileRepository + + +class reset_agent_configuration: + def __init__( + self, + agent_configuration_repository: IAgentConfigurationRepository, + file_repository: IFileRepository, + ): + self._agent_configuration_repository = agent_configuration_repository + self._file_repository = file_repository + + def __call__(self): + self._remove_pba_files() + self._agent_configuration_repository.reset_to_default() + + def _remove_pba_files(self): + agent_configuration = self._agent_configuration_repository.get_configuration() + custom_pbas = agent_configuration.custom_pbas + + if custom_pbas.linux_filename: + self._file_repository.delete_file(custom_pbas.linux_filename) + + if custom_pbas.windows_filename: + self._file_repository.delete_file(custom_pbas.windows_filename) diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_reset_agent_configuration.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_reset_agent_configuration.py new file mode 100644 index 000000000..41d78fb13 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_reset_agent_configuration.py @@ -0,0 +1,56 @@ +from unittest.mock import MagicMock + +import pytest +from tests.monkey_island import InMemoryAgentConfigurationRepository + +from common.agent_configuration import AgentConfiguration +from monkey_island.cc.repository import IAgentConfigurationRepository, IFileRepository +from monkey_island.cc.services import reset_agent_configuration + +LINUX_FILENAME = "linux_pba_file.sh" +WINDOWS_FILENAME = "windows_pba_file.ps1" + + +@pytest.fixture +def agent_configuration(default_agent_configuration: AgentConfiguration) -> AgentConfiguration: + custom_pbas = default_agent_configuration.custom_pbas.copy( + update={"linux_filename": LINUX_FILENAME, "windows_filename": WINDOWS_FILENAME}, + ) + return default_agent_configuration.copy(update={"custom_pbas": custom_pbas}) + + +@pytest.fixture +def agent_configuration_repository( + agent_configuration: AgentConfiguration, +) -> IAgentConfigurationRepository: + agent_configuration_repository = InMemoryAgentConfigurationRepository() + agent_configuration_repository.store_configuration(agent_configuration) + + return agent_configuration_repository + + +@pytest.fixture +def mock_file_repository() -> IFileRepository: + return MagicMock(spec=IFileRepository) + + +@pytest.fixture +def callable_reset_agent_configuration(agent_configuration_repository, mock_file_repository): + return reset_agent_configuration(agent_configuration_repository, mock_file_repository) + + +def test_reset_configuration__remove_pba_files( + callable_reset_agent_configuration, mock_file_repository +): + callable_reset_agent_configuration() + + assert mock_file_repository.delete_file.called_with(LINUX_FILENAME) + assert mock_file_repository.delete_file.called_with(WINDOWS_FILENAME) + + +def test_reset_configuration__agent_configuration_changed( + callable_reset_agent_configuration, agent_configuration_repository, agent_configuration +): + callable_reset_agent_configuration() + + assert agent_configuration_repository.get_configuration() != agent_configuration From f9b1c5ebd2572c55c6f5236b86f23c0ca6027548 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Mon, 12 Sep 2022 13:18:06 +0200 Subject: [PATCH 02/15] Island: Export reset_agent_configuration from __init__ --- monkey/monkey_island/cc/services/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/monkey/monkey_island/cc/services/__init__.py b/monkey/monkey_island/cc/services/__init__.py index 9be3ac250..a35bee933 100644 --- a/monkey/monkey_island/cc/services/__init__.py +++ b/monkey/monkey_island/cc/services/__init__.py @@ -3,3 +3,4 @@ from .authentication_service import AuthenticationService from .aws import AWSService from .island_mode_service import IslandModeService from .repository_service import RepositoryService +from .reset_agent_configuration import reset_agent_configuration From b834bfe7f7a6beb9d501f85b1005b341b61852e6 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Mon, 12 Sep 2022 13:19:59 +0200 Subject: [PATCH 03/15] Island: Publish RESET_AGENT_CONFIGURATION from endpoint --- .../cc/resources/reset_agent_configuration.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monkey/monkey_island/cc/resources/reset_agent_configuration.py b/monkey/monkey_island/cc/resources/reset_agent_configuration.py index 36b49820e..04de3bedc 100644 --- a/monkey/monkey_island/cc/resources/reset_agent_configuration.py +++ b/monkey/monkey_island/cc/resources/reset_agent_configuration.py @@ -2,22 +2,22 @@ from http import HTTPStatus from flask import make_response +from monkey_island.cc.event_queue import IIslandEventQueue, IslandEventTopic from monkey_island.cc.resources.AbstractResource import AbstractResource from monkey_island.cc.resources.request_authentication import jwt_required -from monkey_island.cc.services import RepositoryService class ResetAgentConfiguration(AbstractResource): urls = ["/api/reset-agent-configuration"] - def __init__(self, repository_service: RepositoryService): - self._repository_service = repository_service + def __init__(self, event_queue: IIslandEventQueue): + self._event_queue = event_queue @jwt_required def post(self): """ Reset the agent configuration to its default values """ - self._repository_service.reset_agent_configuration() + self._event_queue.publish(IslandEventTopic.RESET_AGENT_CONFIGURATION) return make_response({}, HTTPStatus.OK) From 0ca13eb93dcee570a1d6e753a82fd212319d364f Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Mon, 12 Sep 2022 15:59:58 +0200 Subject: [PATCH 04/15] Island: Add subscribe_to_topics on server setup --- monkey/monkey_island/cc/__init__.py | 1 + monkey/monkey_island/cc/server_setup.py | 4 +++- monkey/monkey_island/cc/subscribe_to_topics.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 monkey/monkey_island/cc/subscribe_to_topics.py diff --git a/monkey/monkey_island/cc/__init__.py b/monkey/monkey_island/cc/__init__.py index 1a7e6c0ea..89172f65f 100644 --- a/monkey/monkey_island/cc/__init__.py +++ b/monkey/monkey_island/cc/__init__.py @@ -1 +1,2 @@ from .version import Version +from .subscribe_to_topics import subscribe_to_topics diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index e98c4f652..b297cae42 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -10,7 +10,7 @@ import gevent.hub import requests from gevent.pywsgi import WSGIServer -from monkey_island.cc import Version +from monkey_island.cc import Version, subscribe_to_topics from monkey_island.cc.deployment import Deployment from monkey_island.cc.server_utils.consts import ISLAND_PORT from monkey_island.cc.server_utils.island_logger import get_log_file_path @@ -62,6 +62,8 @@ def run_monkey_island(): _initialize_mongodb_connection(config_options.start_mongodb, config_options.data_dir) _start_island_server(ip_addresses, island_args.setup_only, config_options, container) + subscribe_to_topics(container) + def _extract_config(island_args: IslandCmdArgs) -> IslandConfigOptions: try: diff --git a/monkey/monkey_island/cc/subscribe_to_topics.py b/monkey/monkey_island/cc/subscribe_to_topics.py new file mode 100644 index 000000000..ef5154084 --- /dev/null +++ b/monkey/monkey_island/cc/subscribe_to_topics.py @@ -0,0 +1,11 @@ +from common import DIContainer +from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue +from monkey_island.cc.services.reset_agent_configuration import reset_agent_configuration + + +def subscribe_to_topics(container: DIContainer): + event_queue = container.resolve(PyPubSubIslandEventQueue) + + event_queue.subscribe( + IslandEventTopic.RESET_AGENT_CONFIGURATION, container.resolve(reset_agent_configuration) + ) From fd7a00a2259906e95d706ed8b05dfac672568900 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 11:20:17 -0400 Subject: [PATCH 05/15] Island: Rename subscribe_to_topics() -> setup_island_event_subscribers() --- monkey/monkey_island/cc/__init__.py | 2 +- monkey/monkey_island/cc/server_setup.py | 4 ++-- monkey/monkey_island/cc/subscribe_to_topics.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/monkey/monkey_island/cc/__init__.py b/monkey/monkey_island/cc/__init__.py index 89172f65f..7ab52f48a 100644 --- a/monkey/monkey_island/cc/__init__.py +++ b/monkey/monkey_island/cc/__init__.py @@ -1,2 +1,2 @@ from .version import Version -from .subscribe_to_topics import subscribe_to_topics +from .subscribe_to_topics import setup_island_event_subscribers diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index b297cae42..ba36ec562 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -10,7 +10,7 @@ import gevent.hub import requests from gevent.pywsgi import WSGIServer -from monkey_island.cc import Version, subscribe_to_topics +from monkey_island.cc import Version, setup_island_event_subscribers from monkey_island.cc.deployment import Deployment from monkey_island.cc.server_utils.consts import ISLAND_PORT from monkey_island.cc.server_utils.island_logger import get_log_file_path @@ -62,7 +62,7 @@ def run_monkey_island(): _initialize_mongodb_connection(config_options.start_mongodb, config_options.data_dir) _start_island_server(ip_addresses, island_args.setup_only, config_options, container) - subscribe_to_topics(container) + setup_island_event_subscribers(container) def _extract_config(island_args: IslandCmdArgs) -> IslandConfigOptions: diff --git a/monkey/monkey_island/cc/subscribe_to_topics.py b/monkey/monkey_island/cc/subscribe_to_topics.py index ef5154084..7a7afc9cc 100644 --- a/monkey/monkey_island/cc/subscribe_to_topics.py +++ b/monkey/monkey_island/cc/subscribe_to_topics.py @@ -3,7 +3,7 @@ from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQu from monkey_island.cc.services.reset_agent_configuration import reset_agent_configuration -def subscribe_to_topics(container: DIContainer): +def setup_island_event_subscribers(container: DIContainer): event_queue = container.resolve(PyPubSubIslandEventQueue) event_queue.subscribe( From 4bc9f735bec07b02fee48892faa9e95b52ade5da Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 11:25:58 -0400 Subject: [PATCH 06/15] Island: Rename setup_island_event_{subscribers,handlers} --- monkey/monkey_island/cc/__init__.py | 2 +- monkey/monkey_island/cc/server_setup.py | 4 ++-- ...{subscribe_to_topics.py => setup_island_event_handlers.py} | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename monkey/monkey_island/cc/{subscribe_to_topics.py => setup_island_event_handlers.py} (86%) diff --git a/monkey/monkey_island/cc/__init__.py b/monkey/monkey_island/cc/__init__.py index 7ab52f48a..d3308c2c6 100644 --- a/monkey/monkey_island/cc/__init__.py +++ b/monkey/monkey_island/cc/__init__.py @@ -1,2 +1,2 @@ from .version import Version -from .subscribe_to_topics import setup_island_event_subscribers +from .setup_island_event_handlers import setup_island_event_handlers diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index ba36ec562..5dd55e22e 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -10,7 +10,7 @@ import gevent.hub import requests from gevent.pywsgi import WSGIServer -from monkey_island.cc import Version, setup_island_event_subscribers +from monkey_island.cc import Version, setup_island_event_handlers from monkey_island.cc.deployment import Deployment from monkey_island.cc.server_utils.consts import ISLAND_PORT from monkey_island.cc.server_utils.island_logger import get_log_file_path @@ -62,7 +62,7 @@ def run_monkey_island(): _initialize_mongodb_connection(config_options.start_mongodb, config_options.data_dir) _start_island_server(ip_addresses, island_args.setup_only, config_options, container) - setup_island_event_subscribers(container) + setup_island_event_handlers(container) def _extract_config(island_args: IslandCmdArgs) -> IslandConfigOptions: diff --git a/monkey/monkey_island/cc/subscribe_to_topics.py b/monkey/monkey_island/cc/setup_island_event_handlers.py similarity index 86% rename from monkey/monkey_island/cc/subscribe_to_topics.py rename to monkey/monkey_island/cc/setup_island_event_handlers.py index 7a7afc9cc..03e97db36 100644 --- a/monkey/monkey_island/cc/subscribe_to_topics.py +++ b/monkey/monkey_island/cc/setup_island_event_handlers.py @@ -3,7 +3,7 @@ from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQu from monkey_island.cc.services.reset_agent_configuration import reset_agent_configuration -def setup_island_event_subscribers(container: DIContainer): +def setup_island_event_handlers(container: DIContainer): event_queue = container.resolve(PyPubSubIslandEventQueue) event_queue.subscribe( From 285e6aaa35d47f87c6764a2284ee592906ae673b Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 11:48:36 -0400 Subject: [PATCH 07/15] Island: Remove disused local_ips() functions --- .../cc/services/utils/network_utils.py | 48 ------------------- 1 file changed, 48 deletions(-) diff --git a/monkey/monkey_island/cc/services/utils/network_utils.py b/monkey/monkey_island/cc/services/utils/network_utils.py index 8af255ee0..663a8c948 100644 --- a/monkey/monkey_island/cc/services/utils/network_utils.py +++ b/monkey/monkey_island/cc/services/utils/network_utils.py @@ -1,57 +1,9 @@ -import array import ipaddress -import socket -import struct -import sys from typing import Sequence from netifaces import AF_INET, ifaddresses, interfaces from ring import lru -# Local ips function -# TODO: I can't find anywhere these are actually used. Confirm this is the case, remove these -# functions, and test. -if sys.platform == "win32": - - def local_ips(): - local_hostname = socket.gethostname() - return socket.gethostbyname_ex(local_hostname)[2] - -else: - import fcntl - - def local_ips(): - result = [] - try: - is_64bits = sys.maxsize > 2**32 - struct_size = 40 if is_64bits else 32 - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - max_possible = 8 # initial value - while True: - struct_bytes = max_possible * struct_size - names = array.array("B", "\0" * struct_bytes) - outbytes = struct.unpack( - "iL", - fcntl.ioctl( - s.fileno(), - 0x8912, # SIOCGIFCONF - struct.pack("iL", struct_bytes, names.buffer_info()[0]), - ), - )[0] - if outbytes == struct_bytes: - max_possible *= 2 - else: - break - namestr = names.tostring() - - for i in range(0, outbytes, struct_size): - addr = socket.inet_ntoa(namestr[i + 20 : i + 24]) - if not addr.startswith("127"): - result.append(addr) - # name of interface is (namestr[i:i+16].split('\0', 1)[0] - finally: - return result - # The local IP addresses list should not change often. Therefore, we can cache the result and never # call this function more than once. This stopgap measure is here since this function is called a From ad3dd6ba36bdbf51061ad6ba0ec267169d1b66cf Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 11:52:53 -0400 Subject: [PATCH 08/15] Island: Move network_utils from services/ -> server_utils/ --- monkey/monkey_island/cc/models/monkey.py | 2 +- monkey/monkey_island/cc/server_setup.py | 2 +- .../cc/{services/utils => server_utils}/network_utils.py | 2 ++ monkey/monkey_island/cc/services/node.py | 2 +- monkey/monkey_island/cc/services/reporting/report.py | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-) rename monkey/monkey_island/cc/{services/utils => server_utils}/network_utils.py (93%) diff --git a/monkey/monkey_island/cc/models/monkey.py b/monkey/monkey_island/cc/models/monkey.py index 003e14509..cf15c3cd4 100644 --- a/monkey/monkey_island/cc/models/monkey.py +++ b/monkey/monkey_island/cc/models/monkey.py @@ -19,7 +19,7 @@ from mongoengine import ( from monkey_island.cc.models.command_control_channel import CommandControlChannel from monkey_island.cc.models.monkey_ttl import MonkeyTtl, create_monkey_ttl_document from monkey_island.cc.server_utils.consts import DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS -from monkey_island.cc.services.utils.network_utils import get_ip_addresses +from monkey_island.cc.server_utils.network_utils import get_ip_addresses class ParentNotFoundError(Exception): diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 5dd55e22e..8682d345a 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -33,8 +33,8 @@ from monkey_island.cc.server_utils.consts import ( # noqa: E402 MONKEY_ISLAND_ABS_PATH, ) from monkey_island.cc.server_utils.island_logger import reset_logger, setup_logging # noqa: E402 +from monkey_island.cc.server_utils.network_utils import get_ip_addresses # noqa: E402 from monkey_island.cc.services.initialize import initialize_services # noqa: E402 -from monkey_island.cc.services.utils.network_utils import get_ip_addresses # noqa: E402 from monkey_island.cc.setup import PyWSGILoggingFilter # noqa: E402 from monkey_island.cc.setup import island_config_options_validator # noqa: E402 from monkey_island.cc.setup.data_dir import IncompatibleDataDirectory, setup_data_dir # noqa: E402 diff --git a/monkey/monkey_island/cc/services/utils/network_utils.py b/monkey/monkey_island/cc/server_utils/network_utils.py similarity index 93% rename from monkey/monkey_island/cc/services/utils/network_utils.py rename to monkey/monkey_island/cc/server_utils/network_utils.py index 663a8c948..60c397c77 100644 --- a/monkey/monkey_island/cc/services/utils/network_utils.py +++ b/monkey/monkey_island/cc/server_utils/network_utils.py @@ -4,6 +4,8 @@ from typing import Sequence from netifaces import AF_INET, ifaddresses, interfaces from ring import lru +# TODO: This functionality is duplicated in the agent. Unify them after 2216-tcp-relay is merged + # The local IP addresses list should not change often. Therefore, we can cache the result and never # call this function more than once. This stopgap measure is here since this function is called a diff --git a/monkey/monkey_island/cc/services/node.py b/monkey/monkey_island/cc/services/node.py index faac42539..2a7f0f661 100644 --- a/monkey/monkey_island/cc/services/node.py +++ b/monkey/monkey_island/cc/services/node.py @@ -7,9 +7,9 @@ import monkey_island.cc.services.log from monkey_island.cc import models from monkey_island.cc.database import mongo from monkey_island.cc.models import Monkey +from monkey_island.cc.server_utils.network_utils import get_ip_addresses from monkey_island.cc.services.edge.displayed_edge import DisplayedEdgeService from monkey_island.cc.services.edge.edge import EdgeService -from monkey_island.cc.services.utils.network_utils import get_ip_addresses from monkey_island.cc.services.utils.node_states import NodeStates diff --git a/monkey/monkey_island/cc/services/reporting/report.py b/monkey/monkey_island/cc/services/reporting/report.py index 32761c7a6..01ce711d1 100644 --- a/monkey/monkey_island/cc/services/reporting/report.py +++ b/monkey/monkey_island/cc/services/reporting/report.py @@ -10,6 +10,7 @@ from monkey_island.cc.database import mongo from monkey_island.cc.models import Monkey from monkey_island.cc.models.report import get_report, save_report from monkey_island.cc.repository import IAgentConfigurationRepository, ICredentialsRepository +from monkey_island.cc.server_utils.network_utils import get_ip_addresses, get_subnets from monkey_island.cc.services.node import NodeService from monkey_island.cc.services.reporting.exploitations.manual_exploitation import get_manual_monkeys from monkey_island.cc.services.reporting.exploitations.monkey_exploitation import ( @@ -19,7 +20,6 @@ from monkey_island.cc.services.reporting.pth_report import PTHReportService from monkey_island.cc.services.reporting.report_generation_synchronisation import ( safe_generate_regular_report, ) -from monkey_island.cc.services.utils.network_utils import get_ip_addresses, get_subnets from .. import AWSService from . import aws_exporter From 3e67f5696c6d1e2f6f7d867a3de2b1cccd548cc1 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 11:56:34 -0400 Subject: [PATCH 09/15] Island: Move setup_island_event_handlers to monkey_island.cc.setup --- monkey/monkey_island/cc/__init__.py | 1 - monkey/monkey_island/cc/server_setup.py | 4 ++-- monkey/monkey_island/cc/setup/__init__.py | 1 + .../island_event_handlers.py} | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename monkey/monkey_island/cc/{setup_island_event_handlers.py => setup/island_event_handlers.py} (80%) diff --git a/monkey/monkey_island/cc/__init__.py b/monkey/monkey_island/cc/__init__.py index d3308c2c6..1a7e6c0ea 100644 --- a/monkey/monkey_island/cc/__init__.py +++ b/monkey/monkey_island/cc/__init__.py @@ -1,2 +1 @@ from .version import Version -from .setup_island_event_handlers import setup_island_event_handlers diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 8682d345a..3a35538b9 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -10,7 +10,7 @@ import gevent.hub import requests from gevent.pywsgi import WSGIServer -from monkey_island.cc import Version, setup_island_event_handlers +from monkey_island.cc import Version from monkey_island.cc.deployment import Deployment from monkey_island.cc.server_utils.consts import ISLAND_PORT from monkey_island.cc.server_utils.island_logger import get_log_file_path @@ -35,8 +35,8 @@ from monkey_island.cc.server_utils.consts import ( # noqa: E402 from monkey_island.cc.server_utils.island_logger import reset_logger, setup_logging # noqa: E402 from monkey_island.cc.server_utils.network_utils import get_ip_addresses # noqa: E402 from monkey_island.cc.services.initialize import initialize_services # noqa: E402 -from monkey_island.cc.setup import PyWSGILoggingFilter # noqa: E402 from monkey_island.cc.setup import island_config_options_validator # noqa: E402 +from monkey_island.cc.setup import PyWSGILoggingFilter, setup_island_event_handlers # noqa: E402 from monkey_island.cc.setup.data_dir import IncompatibleDataDirectory, setup_data_dir # noqa: E402 from monkey_island.cc.setup.gevent_hub_error_handler import GeventHubErrorHandler # noqa: E402 from monkey_island.cc.setup.island_config_options import IslandConfigOptions # noqa: E402 diff --git a/monkey/monkey_island/cc/setup/__init__.py b/monkey/monkey_island/cc/setup/__init__.py index a28c5dd05..3642bf9b2 100644 --- a/monkey/monkey_island/cc/setup/__init__.py +++ b/monkey/monkey_island/cc/setup/__init__.py @@ -1 +1,2 @@ from .pywsgi_logging_filter import PyWSGILoggingFilter +from .island_event_handlers import setup_island_event_handlers diff --git a/monkey/monkey_island/cc/setup_island_event_handlers.py b/monkey/monkey_island/cc/setup/island_event_handlers.py similarity index 80% rename from monkey/monkey_island/cc/setup_island_event_handlers.py rename to monkey/monkey_island/cc/setup/island_event_handlers.py index 03e97db36..0fcf2febc 100644 --- a/monkey/monkey_island/cc/setup_island_event_handlers.py +++ b/monkey/monkey_island/cc/setup/island_event_handlers.py @@ -1,6 +1,6 @@ from common import DIContainer from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue -from monkey_island.cc.services.reset_agent_configuration import reset_agent_configuration +from monkey_island.cc.island_event_handlers import reset_agent_configuration def setup_island_event_handlers(container: DIContainer): From 8b49554cb5d0bd1fb7a9f7533dcaeb4fa536df72 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 11:57:03 -0400 Subject: [PATCH 10/15] Island: Add island_event_handlers package --- monkey/monkey_island/cc/island_event_handlers/__init__.py | 1 + .../reset_agent_configuration.py | 0 monkey/monkey_island/cc/services/__init__.py | 1 - .../monkey_island/cc/island_event_handlers/__init__.py | 0 .../test_reset_agent_configuration.py | 2 +- 5 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 monkey/monkey_island/cc/island_event_handlers/__init__.py rename monkey/monkey_island/cc/{services => island_event_handlers}/reset_agent_configuration.py (100%) create mode 100644 monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/__init__.py rename monkey/tests/unit_tests/monkey_island/cc/{services => island_event_handlers}/test_reset_agent_configuration.py (96%) diff --git a/monkey/monkey_island/cc/island_event_handlers/__init__.py b/monkey/monkey_island/cc/island_event_handlers/__init__.py new file mode 100644 index 000000000..7a20d4c44 --- /dev/null +++ b/monkey/monkey_island/cc/island_event_handlers/__init__.py @@ -0,0 +1 @@ +from .reset_agent_configuration import reset_agent_configuration diff --git a/monkey/monkey_island/cc/services/reset_agent_configuration.py b/monkey/monkey_island/cc/island_event_handlers/reset_agent_configuration.py similarity index 100% rename from monkey/monkey_island/cc/services/reset_agent_configuration.py rename to monkey/monkey_island/cc/island_event_handlers/reset_agent_configuration.py diff --git a/monkey/monkey_island/cc/services/__init__.py b/monkey/monkey_island/cc/services/__init__.py index a35bee933..9be3ac250 100644 --- a/monkey/monkey_island/cc/services/__init__.py +++ b/monkey/monkey_island/cc/services/__init__.py @@ -3,4 +3,3 @@ from .authentication_service import AuthenticationService from .aws import AWSService from .island_mode_service import IslandModeService from .repository_service import RepositoryService -from .reset_agent_configuration import reset_agent_configuration diff --git a/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/__init__.py b/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_reset_agent_configuration.py b/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_reset_agent_configuration.py similarity index 96% rename from monkey/tests/unit_tests/monkey_island/cc/services/test_reset_agent_configuration.py rename to monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_reset_agent_configuration.py index 41d78fb13..d82c0bec2 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_reset_agent_configuration.py +++ b/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_reset_agent_configuration.py @@ -4,8 +4,8 @@ import pytest from tests.monkey_island import InMemoryAgentConfigurationRepository from common.agent_configuration import AgentConfiguration +from monkey_island.cc.island_event_handlers import reset_agent_configuration from monkey_island.cc.repository import IAgentConfigurationRepository, IFileRepository -from monkey_island.cc.services import reset_agent_configuration LINUX_FILENAME = "linux_pba_file.sh" WINDOWS_FILENAME = "windows_pba_file.ps1" From 0e112cc31ac696c1be0e0e8479801e28447691b7 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 14:10:22 -0400 Subject: [PATCH 11/15] Island: Wire up event handlers before starting the island server _start_island_server() blocks, so event handlers need to be wired/subscribed before the server starts. --- monkey/monkey_island/cc/server_setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 3a35538b9..2a946d326 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -58,11 +58,11 @@ def run_monkey_island(): _send_analytics(deployment, version) container = _initialize_di_container(ip_addresses, version, config_options.data_dir) + setup_island_event_handlers(container) _initialize_mongodb_connection(config_options.start_mongodb, config_options.data_dir) _start_island_server(ip_addresses, island_args.setup_only, config_options, container) - setup_island_event_handlers(container) def _extract_config(island_args: IslandCmdArgs) -> IslandConfigOptions: From ea47fc326aaf9ddb65d6cf3f157de8057320b95f Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 14:14:10 -0400 Subject: [PATCH 12/15] Island: Remove extra newline from server_setup.py --- monkey/monkey_island/cc/server_setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 2a946d326..9fc5fc030 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -64,7 +64,6 @@ def run_monkey_island(): _start_island_server(ip_addresses, island_args.setup_only, config_options, container) - def _extract_config(island_args: IslandCmdArgs) -> IslandConfigOptions: try: return get_server_config(island_args) From 599c753cf7a80534c09af87670150f03f1c857da Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 13 Sep 2022 09:31:38 +0200 Subject: [PATCH 13/15] Island: Add docstrings to reset_agent_configuration event handler --- .../cc/island_event_handlers/reset_agent_configuration.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/monkey/monkey_island/cc/island_event_handlers/reset_agent_configuration.py b/monkey/monkey_island/cc/island_event_handlers/reset_agent_configuration.py index 081922e7a..d6c6f8786 100644 --- a/monkey/monkey_island/cc/island_event_handlers/reset_agent_configuration.py +++ b/monkey/monkey_island/cc/island_event_handlers/reset_agent_configuration.py @@ -2,6 +2,11 @@ from monkey_island.cc.repository import IAgentConfigurationRepository, IFileRepo class reset_agent_configuration: + """ + Callable class that handles removal of PBA files and reset of agent configuration + on the Island. + """ + def __init__( self, agent_configuration_repository: IAgentConfigurationRepository, @@ -15,6 +20,9 @@ class reset_agent_configuration: self._agent_configuration_repository.reset_to_default() def _remove_pba_files(self): + """ + Removes PBA files from the file repository + """ agent_configuration = self._agent_configuration_repository.get_configuration() custom_pbas = agent_configuration.custom_pbas From 2bfb85a652494288c2bb4987f869e870f978a35f Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 13 Sep 2022 09:33:48 +0200 Subject: [PATCH 14/15] UT: Add typehints to test_reset_agent_configuration --- .../island_event_handlers/test_reset_agent_configuration.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_reset_agent_configuration.py b/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_reset_agent_configuration.py index d82c0bec2..2fb7d3230 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_reset_agent_configuration.py +++ b/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_reset_agent_configuration.py @@ -35,7 +35,10 @@ def mock_file_repository() -> IFileRepository: @pytest.fixture -def callable_reset_agent_configuration(agent_configuration_repository, mock_file_repository): +def callable_reset_agent_configuration( + agent_configuration_repository: IAgentConfigurationRepository, + mock_file_repository: IFileRepository, +) -> reset_agent_configuration: return reset_agent_configuration(agent_configuration_repository, mock_file_repository) From afa6c6b159da6f7be5ab7d9a60e806d0eebfe269 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 13 Sep 2022 10:44:52 +0200 Subject: [PATCH 15/15] Island: Rename event_queue to island_event_queue --- .../monkey_island/cc/resources/reset_agent_configuration.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/monkey_island/cc/resources/reset_agent_configuration.py b/monkey/monkey_island/cc/resources/reset_agent_configuration.py index 04de3bedc..68028d048 100644 --- a/monkey/monkey_island/cc/resources/reset_agent_configuration.py +++ b/monkey/monkey_island/cc/resources/reset_agent_configuration.py @@ -10,14 +10,14 @@ from monkey_island.cc.resources.request_authentication import jwt_required class ResetAgentConfiguration(AbstractResource): urls = ["/api/reset-agent-configuration"] - def __init__(self, event_queue: IIslandEventQueue): - self._event_queue = event_queue + def __init__(self, island_event_queue: IIslandEventQueue): + self._island_event_queue = island_event_queue @jwt_required def post(self): """ Reset the agent configuration to its default values """ - self._event_queue.publish(IslandEventTopic.RESET_AGENT_CONFIGURATION) + self._island_event_queue.publish(IslandEventTopic.RESET_AGENT_CONFIGURATION) return make_response({}, HTTPStatus.OK)