From fe9cc86d8bd9ad3069098c896308511ce88662f5 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 13 Jul 2022 09:32:59 -0400 Subject: [PATCH] Island: Remove disused PostBreachFilesService --- .../monkey_island/cc/services/initialize.py | 2 - .../cc/services/post_breach_files.py | 24 ----------- .../cc/services/test_post_breach_files.py | 43 ------------------- 3 files changed, 69 deletions(-) delete mode 100644 monkey/monkey_island/cc/services/post_breach_files.py delete mode 100644 monkey/tests/unit_tests/monkey_island/cc/services/test_post_breach_files.py diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index f76d0ddef..9cf019421 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -33,7 +33,6 @@ 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.post_breach_files import PostBreachFilesService from monkey_island.cc.services.run_local_monkey import LocalMonkeyRunService from monkey_island.cc.services.telemetry.processing.credentials.credentials_parser import ( CredentialsParser, @@ -70,7 +69,6 @@ def initialize_services(data_dir: Path) -> DIContainer: _patch_credentials_parser(container) # This is temporary until we get DI all worked out. - PostBreachFilesService.initialize(container.resolve(IFileRepository)) ReportService.initialize(container.resolve(AWSService)) return container diff --git a/monkey/monkey_island/cc/services/post_breach_files.py b/monkey/monkey_island/cc/services/post_breach_files.py deleted file mode 100644 index efb370b31..000000000 --- a/monkey/monkey_island/cc/services/post_breach_files.py +++ /dev/null @@ -1,24 +0,0 @@ -import logging - -from monkey_island.cc.repository import IFileRepository - -logger = logging.getLogger(__name__) - - -# TODO: This service wraps an IFileStorageService for the sole purpose of making the -# `remove_PBA_files()` method available to the ConfigService. This whole service can be -# removed once ConfigService is refactored to be stateful (it already is but everything is -# still statically/globally scoped) and use dependency injection. -class PostBreachFilesService: - _file_storage_service = None - - # TODO: A number of these services should be instance objects instead of - # static/singleton hybrids. At the moment, this requires invasive refactoring that's - # not a priority. - @classmethod - def initialize(cls, file_storage_service: IFileRepository): - cls._file_storage_service = file_storage_service - - @classmethod - def remove_PBA_files(cls): - cls._file_storage_service.delete_all_files() diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_post_breach_files.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_post_breach_files.py deleted file mode 100644 index 708fdc034..000000000 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_post_breach_files.py +++ /dev/null @@ -1,43 +0,0 @@ -import io -import os - -import pytest -from tests.utils import raise_ - -from monkey_island.cc.repository import LocalStorageFileRepository -from monkey_island.cc.services.post_breach_files import PostBreachFilesService - - -@pytest.fixture -def local_storage_file_repository(tmp_path): - return LocalStorageFileRepository(tmp_path) - - -@pytest.fixture(autouse=True) -def post_breach_files_service(local_storage_file_repository): - PostBreachFilesService.initialize(local_storage_file_repository) - - -def test_remove_pba_files(local_storage_file_repository, tmp_path): - local_storage_file_repository.save_file("linux_file", io.BytesIO(b"")) - local_storage_file_repository.save_file("windows_file", io.BytesIO(b"")) - assert not dir_is_empty(tmp_path) - - PostBreachFilesService.remove_PBA_files() - - assert dir_is_empty(tmp_path) - - -def dir_is_empty(dir_path): - dir_contents = os.listdir(dir_path) - return len(dir_contents) == 0 - - -def test_remove_failure(local_storage_file_repository, monkeypatch): - monkeypatch.setattr(os, "remove", lambda x: raise_(OSError("Permission denied"))) - - try: - local_storage_file_repository.save_file("windows_file", io.BytesIO(b"")) - PostBreachFilesService.remove_PBA_files() - except Exception as ex: - pytest.fail(f"Unxepected exception: {ex}")