From d58d8bb87b2619932fb7f0de02b69d611427a28c Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 14:24:56 -0400 Subject: [PATCH] Island: Remove RepositoryService Issue #2234 --- monkey/monkey_island/cc/services/__init__.py | 1 - monkey/monkey_island/cc/services/database.py | 2 +- .../monkey_island/cc/services/initialize.py | 3 +- .../cc/services/repository_service.py | 42 ---------- .../cc/services/test_repository_service.py | 79 ------------------- 5 files changed, 2 insertions(+), 125 deletions(-) delete mode 100644 monkey/monkey_island/cc/services/repository_service.py delete mode 100644 monkey/tests/unit_tests/monkey_island/cc/services/test_repository_service.py diff --git a/monkey/monkey_island/cc/services/__init__.py b/monkey/monkey_island/cc/services/__init__.py index 9be3ac250..14c5664e9 100644 --- a/monkey/monkey_island/cc/services/__init__.py +++ b/monkey/monkey_island/cc/services/__init__.py @@ -2,4 +2,3 @@ from .authentication_service import AuthenticationService from .aws import AWSService from .island_mode_service import IslandModeService -from .repository_service import RepositoryService diff --git a/monkey/monkey_island/cc/services/database.py b/monkey/monkey_island/cc/services/database.py index 1d7233df0..fd99f8045 100644 --- a/monkey/monkey_island/cc/services/database.py +++ b/monkey/monkey_island/cc/services/database.py @@ -10,7 +10,7 @@ from monkey_island.cc.models.attack.attack_mitigations import AttackMitigations logger = logging.getLogger(__name__) -# NOTE: This service is being replaced by the RepositoryService +# NOTE: This service is being replaced a little at a time by repositories class Database(object): def __init__(self): pass diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 888bf75c1..1b6a3e4d5 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -35,7 +35,7 @@ from monkey_island.cc.repository import ( ) from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH from monkey_island.cc.server_utils.encryption import ILockableEncryptor, RepositoryEncryptor -from monkey_island.cc.services import AWSService, IslandModeService, RepositoryService +from monkey_island.cc.services import AWSService, IslandModeService from monkey_island.cc.services.attack.technique_reports.T1003 import T1003, T1003GetReportData from monkey_island.cc.services.run_local_monkey import LocalMonkeyRunService from monkey_island.cc.services.telemetry.processing.credentials.credentials_parser import ( @@ -151,7 +151,6 @@ def _register_services(container: DIContainer): container.register_instance(LocalMonkeyRunService, container.resolve(LocalMonkeyRunService)) container.register_instance(IslandModeService, container.resolve(IslandModeService)) container.register_instance(AuthenticationService, container.resolve(AuthenticationService)) - container.register_instance(RepositoryService, container.resolve(RepositoryService)) def _dirty_hacks(container: DIContainer): diff --git a/monkey/monkey_island/cc/services/repository_service.py b/monkey/monkey_island/cc/services/repository_service.py deleted file mode 100644 index 8cc310b62..000000000 --- a/monkey/monkey_island/cc/services/repository_service.py +++ /dev/null @@ -1,42 +0,0 @@ -from monkey_island.cc.repository import ( - IAgentConfigurationRepository, - ICredentialsRepository, - IFileRepository, -) -from monkey_island.cc.services.database import Database - - -class RepositoryService: - def __init__( - self, - agent_configuration_repository: IAgentConfigurationRepository, - file_repository: IFileRepository, - credentials_repository: ICredentialsRepository, - ): - self._agent_configuration_repository = agent_configuration_repository - self._file_repository = file_repository - self._credentials_repository = credentials_repository - - def reset_agent_configuration(self): - # NOTE: This method will be replaced by an event when we implement pub/sub in the island. - # Different plugins and components will be able to register for the event and reset - # their configurations. - 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) - - def clear_simulation_data(self): - # NOTE: This method will be replaced by an event when we implement pub/sub in the island. - # Different plugins and components will be able to register for the event and clear - # any configuration data they've collected. - Database.reset_db(reset_config=False) - self._credentials_repository.remove_stolen_credentials() diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_repository_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_repository_service.py deleted file mode 100644 index 292936635..000000000 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_repository_service.py +++ /dev/null @@ -1,79 +0,0 @@ -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, - ICredentialsRepository, - IFileRepository, -) -from monkey_island.cc.services import RepositoryService - -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 mock_credentials_repository() -> ICredentialsRepository: - return MagicMock(spec=ICredentialsRepository) - - -@pytest.fixture -def repository_service( - agent_configuration_repository, mock_file_repository, mock_credentials_repository -) -> RepositoryService: - return RepositoryService( - agent_configuration_repository, mock_file_repository, mock_credentials_repository - ) - - -def test_reset_configuration__remove_pba_files(repository_service, mock_file_repository): - repository_service.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( - repository_service, agent_configuration_repository, agent_configuration -): - repository_service.reset_agent_configuration() - - assert agent_configuration_repository.get_configuration() != agent_configuration - - -@pytest.mark.usefixtures("uses_database") -def test_clear_simulation_data( - repository_service: RepositoryService, - mock_credentials_repository: ICredentialsRepository, - monkeypatch, -): - monkeypatch.setattr("monkey_island.cc.services.repository_service.Database", MagicMock()) - repository_service.clear_simulation_data() - - mock_credentials_repository.remove_stolen_credentials.assert_called_once()