From 154d59b8376d85634bbec02b6e5502c1f74dd57e Mon Sep 17 00:00:00 2001 From: vakarisz Date: Tue, 31 May 2022 12:45:45 +0300 Subject: [PATCH 1/5] Island: Rename and move file storage classes --- .../monkey_island/cc/repository/__init__.py | 2 ++ .../cc/repository/file_storage/__init__.py | 0 .../file_storage/filesystem_storage.py} | 4 ++-- .../file_storage/i_file_repository.py} | 2 +- .../cc/resources/pba_file_download.py | 4 ++-- .../cc/resources/pba_file_upload.py | 6 ++--- monkey/monkey_island/cc/services/__init__.py | 3 --- .../monkey_island/cc/services/initialize.py | 9 ++++--- .../cc/services/post_breach_files.py | 4 ++-- .../cc/resources/test_pba_file_download.py | 6 ++--- .../cc/resources/test_pba_file_upload.py | 6 ++--- .../test_directory_file_storage_service.py | 24 +++++++++---------- .../cc/services/test_post_breach_files.py | 4 ++-- 13 files changed, 36 insertions(+), 38 deletions(-) create mode 100644 monkey/monkey_island/cc/repository/file_storage/__init__.py rename monkey/monkey_island/cc/{services/directory_file_storage_service.py => repository/file_storage/filesystem_storage.py} (96%) rename monkey/monkey_island/cc/{services/i_file_storage_service.py => repository/file_storage/i_file_repository.py} (96%) diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index e69de29bb..020b5a1f2 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -0,0 +1,2 @@ +from .file_storage.filesystem_storage import FilesystemStorage +from .file_storage.i_file_repository import IFileRepository, FileRetrievalError diff --git a/monkey/monkey_island/cc/repository/file_storage/__init__.py b/monkey/monkey_island/cc/repository/file_storage/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/monkey/monkey_island/cc/services/directory_file_storage_service.py b/monkey/monkey_island/cc/repository/file_storage/filesystem_storage.py similarity index 96% rename from monkey/monkey_island/cc/services/directory_file_storage_service.py rename to monkey/monkey_island/cc/repository/file_storage/filesystem_storage.py index 48a037a68..1d4f1fad2 100644 --- a/monkey/monkey_island/cc/services/directory_file_storage_service.py +++ b/monkey/monkey_island/cc/repository/file_storage/filesystem_storage.py @@ -6,12 +6,12 @@ from typing import BinaryIO from common.utils.file_utils import get_all_regular_files_in_directory from monkey_island.cc.server_utils.file_utils import create_secure_directory -from . import FileRetrievalError, IFileStorageService +from .i_file_repository import FileRetrievalError, IFileRepository logger = logging.getLogger(__name__) -class DirectoryFileStorageService(IFileStorageService): +class FilesystemStorage(IFileRepository): """ A implementation of IFileStorageService that reads and writes files from/to the local filesystem. diff --git a/monkey/monkey_island/cc/services/i_file_storage_service.py b/monkey/monkey_island/cc/repository/file_storage/i_file_repository.py similarity index 96% rename from monkey/monkey_island/cc/services/i_file_storage_service.py rename to monkey/monkey_island/cc/repository/file_storage/i_file_repository.py index 4425d1210..462b9718e 100644 --- a/monkey/monkey_island/cc/services/i_file_storage_service.py +++ b/monkey/monkey_island/cc/repository/file_storage/i_file_repository.py @@ -6,7 +6,7 @@ class FileRetrievalError(ValueError): pass -class IFileStorageService(metaclass=abc.ABCMeta): +class IFileRepository(metaclass=abc.ABCMeta): """ A service that allows the storage and retrieval of individual files. """ diff --git a/monkey/monkey_island/cc/resources/pba_file_download.py b/monkey/monkey_island/cc/resources/pba_file_download.py index 73658d0ad..85395d5e3 100644 --- a/monkey/monkey_island/cc/resources/pba_file_download.py +++ b/monkey/monkey_island/cc/resources/pba_file_download.py @@ -2,8 +2,8 @@ import logging from flask import make_response, send_file +from monkey_island.cc.repository import FileRetrievalError, IFileRepository from monkey_island.cc.resources.AbstractResource import AbstractResource -from monkey_island.cc.services import FileRetrievalError, IFileStorageService logger = logging.getLogger(__file__) @@ -14,7 +14,7 @@ class PBAFileDownload(AbstractResource): File download endpoint used by monkey to download user's PBA file """ - def __init__(self, file_storage_service: IFileStorageService): + def __init__(self, file_storage_service: IFileRepository): self._file_storage_service = file_storage_service # Used by monkey. can't secure. diff --git a/monkey/monkey_island/cc/resources/pba_file_upload.py b/monkey/monkey_island/cc/resources/pba_file_upload.py index a4ce2705a..9d5cce8b1 100644 --- a/monkey/monkey_island/cc/resources/pba_file_upload.py +++ b/monkey/monkey_island/cc/resources/pba_file_upload.py @@ -5,9 +5,9 @@ from flask import Response, make_response, request, send_file from werkzeug.utils import secure_filename as sanitize_filename from common.config_value_paths import PBA_LINUX_FILENAME_PATH, PBA_WINDOWS_FILENAME_PATH +from monkey_island.cc.repository import FileRetrievalError, IFileRepository from monkey_island.cc.resources.AbstractResource import AbstractResource from monkey_island.cc.resources.request_authentication import jwt_required -from monkey_island.cc.services import FileRetrievalError, IFileStorageService from monkey_island.cc.services.config import ConfigService logger = logging.getLogger(__file__) @@ -29,8 +29,8 @@ class FileUpload(AbstractResource): "/api/file-upload/?restore=", ] - def __init__(self, file_storage_service: IFileStorageService): - self._file_storage_service = file_storage_service + def __init__(self, file_storage_repository: IFileRepository): + self._file_storage_service = file_storage_repository # This endpoint is basically a duplicate of PBAFileDownload.get(). They serve slightly different # purposes. This endpoint is authenticated, whereas the one in PBAFileDownload can not be (at diff --git a/monkey/monkey_island/cc/services/__init__.py b/monkey/monkey_island/cc/services/__init__.py index f50990ce3..7fc707d2a 100644 --- a/monkey/monkey_island/cc/services/__init__.py +++ b/monkey/monkey_island/cc/services/__init__.py @@ -1,6 +1,3 @@ -from .i_file_storage_service import IFileStorageService, FileRetrievalError -from .directory_file_storage_service import DirectoryFileStorageService - from .authentication.authentication_service import AuthenticationService from .authentication.json_file_user_datastore import JsonFileUserDatastore diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 78846cff5..3d08d5ca1 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -2,7 +2,8 @@ from pathlib import Path from common import DIContainer from common.aws import AWSInstance -from monkey_island.cc.services import AWSService, DirectoryFileStorageService, IFileStorageService +from monkey_island.cc.repository import FilesystemStorage, IFileRepository +from monkey_island.cc.services import AWSService from monkey_island.cc.services.post_breach_files import PostBreachFilesService from monkey_island.cc.services.run_local_monkey import LocalMonkeyRunService @@ -14,13 +15,11 @@ def initialize_services(data_dir: Path) -> DIContainer: container = DIContainer() container.register_instance(AWSInstance, AWSInstance()) - container.register_instance( - IFileStorageService, DirectoryFileStorageService(data_dir / "custom_pbas") - ) + container.register_instance(IFileRepository, FilesystemStorage(data_dir / "custom_pbas")) container.register_instance(AWSService, container.resolve(AWSService)) # This is temporary until we get DI all worked out. - PostBreachFilesService.initialize(container.resolve(IFileStorageService)) + PostBreachFilesService.initialize(container.resolve(IFileRepository)) LocalMonkeyRunService.initialize(data_dir) AuthenticationService.initialize(data_dir, JsonFileUserDatastore(data_dir)) ReportService.initialize(container.resolve(AWSService)) diff --git a/monkey/monkey_island/cc/services/post_breach_files.py b/monkey/monkey_island/cc/services/post_breach_files.py index 9b24ce391..efb370b31 100644 --- a/monkey/monkey_island/cc/services/post_breach_files.py +++ b/monkey/monkey_island/cc/services/post_breach_files.py @@ -1,6 +1,6 @@ import logging -from monkey_island.cc.services import IFileStorageService +from monkey_island.cc.repository import IFileRepository logger = logging.getLogger(__name__) @@ -16,7 +16,7 @@ class PostBreachFilesService: # static/singleton hybrids. At the moment, this requires invasive refactoring that's # not a priority. @classmethod - def initialize(cls, file_storage_service: IFileStorageService): + def initialize(cls, file_storage_service: IFileRepository): cls._file_storage_service = file_storage_service @classmethod diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_download.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_download.py index d54d14f84..709c6c7e2 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_download.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_download.py @@ -5,14 +5,14 @@ import pytest from tests.common import StubDIContainer from tests.unit_tests.monkey_island.conftest import get_url_for_resource +from monkey_island.cc.repository import FileRetrievalError, IFileRepository from monkey_island.cc.resources.pba_file_download import PBAFileDownload -from monkey_island.cc.services import FileRetrievalError, IFileStorageService FILE_NAME = "test_file" FILE_CONTENTS = b"HelloWorld!" -class MockFileStorageService(IFileStorageService): +class MockFileStorageService(IFileRepository): def __init__(self): self._file = io.BytesIO(FILE_CONTENTS) @@ -35,7 +35,7 @@ class MockFileStorageService(IFileStorageService): @pytest.fixture def flask_client(build_flask_client): container = StubDIContainer() - container.register(IFileStorageService, MockFileStorageService) + container.register(IFileRepository, MockFileStorageService) with build_flask_client(container) as flask_client: yield flask_client diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_upload.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_upload.py index 3ec9a631a..9f7796263 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_upload.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_upload.py @@ -6,8 +6,8 @@ from tests.common import StubDIContainer from tests.unit_tests.monkey_island.conftest import get_url_for_resource from tests.utils import raise_ +from monkey_island.cc.repository import FileRetrievalError, IFileRepository from monkey_island.cc.resources.pba_file_upload import LINUX_PBA_TYPE, WINDOWS_PBA_TYPE, FileUpload -from monkey_island.cc.services import FileRetrievalError, IFileStorageService TEST_FILE_CONTENTS = b"m0nk3y" TEST_FILE = ( @@ -40,7 +40,7 @@ def mock_get_config_value(monkeypatch): ) -class MockFileStorageService(IFileStorageService): +class MockFileStorageService(IFileRepository): def __init__(self): self._file = None @@ -67,7 +67,7 @@ def file_storage_service(): @pytest.fixture def flask_client(build_flask_client, file_storage_service): container = StubDIContainer() - container.register_instance(IFileStorageService, file_storage_service) + container.register_instance(IFileRepository, file_storage_service) with build_flask_client(container) as flask_client: yield flask_client diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py index ea40aa49d..7ccb62a20 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py @@ -4,8 +4,8 @@ from pathlib import Path import pytest from tests.monkey_island.utils import assert_linux_permissions, assert_windows_permissions +from monkey_island.cc.repository import FileRetrievalError, FilesystemStorage from monkey_island.cc.server_utils.file_utils import is_windows_os -from monkey_island.cc.services import DirectoryFileStorageService, FileRetrievalError def test_error_if_storage_directory_is_file(tmp_path): @@ -13,13 +13,13 @@ def test_error_if_storage_directory_is_file(tmp_path): new_file.write_text("HelloWorld!") with pytest.raises(ValueError): - DirectoryFileStorageService(new_file) + FilesystemStorage(new_file) def test_directory_created(tmp_path): new_dir = tmp_path / "new_dir" - DirectoryFileStorageService(new_dir) + FilesystemStorage(new_dir) assert new_dir.exists() and new_dir.is_dir() @@ -28,7 +28,7 @@ def test_directory_created(tmp_path): def test_directory_permissions__linux(tmp_path): new_dir = tmp_path / "new_dir" - DirectoryFileStorageService(new_dir) + FilesystemStorage(new_dir) assert_linux_permissions(new_dir) @@ -37,7 +37,7 @@ def test_directory_permissions__linux(tmp_path): def test_directory_permissions__windows(tmp_path): new_dir = tmp_path / "new_dir" - DirectoryFileStorageService(new_dir) + FilesystemStorage(new_dir) assert_windows_permissions(new_dir) @@ -47,7 +47,7 @@ def save_file(tmp_path, file_path_prefix=""): file_contents = "Hello World!" expected_file_path = tmp_path / file_name - fss = DirectoryFileStorageService(tmp_path) + fss = FilesystemStorage(tmp_path) fss.save_file(Path(file_path_prefix) / file_name, io.BytesIO(file_contents.encode())) assert expected_file_path.is_file() @@ -60,7 +60,7 @@ def delete_file(tmp_path, file_path_prefix=""): file.touch() assert file.is_file() - fss = DirectoryFileStorageService(tmp_path) + fss = FilesystemStorage(tmp_path) fss.delete_file(Path(file_path_prefix) / file_name) assert not file.exists() @@ -72,7 +72,7 @@ def open_file(tmp_path, file_path_prefix=""): expected_file_path = tmp_path / file_name expected_file_path.write_text(expected_file_contents) - fss = DirectoryFileStorageService(tmp_path) + fss = FilesystemStorage(tmp_path) with fss.open_file(Path(file_path_prefix) / file_name) as f: actual_file_contents = f.read() @@ -101,7 +101,7 @@ def test_remove_all_files(tmp_path): for filename in ["1.txt", "2.txt", "3.txt"]: (tmp_path / filename).touch() - fss = DirectoryFileStorageService(tmp_path) + fss = FilesystemStorage(tmp_path) fss.delete_all_files() for file in tmp_path.iterdir(): @@ -114,7 +114,7 @@ def test_remove_all_files__skip_directories(tmp_path): for filename in ["1.txt", "2.txt", "3.txt"]: (tmp_path / filename).touch() - fss = DirectoryFileStorageService(tmp_path) + fss = FilesystemStorage(tmp_path) fss.delete_all_files() for file in tmp_path.iterdir(): @@ -122,14 +122,14 @@ def test_remove_all_files__skip_directories(tmp_path): def test_remove_nonexistant_file(tmp_path): - fss = DirectoryFileStorageService(tmp_path) + fss = FilesystemStorage(tmp_path) # This test will fail if this call raises an exception. fss.delete_file("nonexistant_file.txt") def test_open_nonexistant_file(tmp_path): - fss = DirectoryFileStorageService(tmp_path) + fss = FilesystemStorage(tmp_path) with pytest.raises(FileRetrievalError): fss.open_file("nonexistant_file.txt") 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 index 23f42cf79..6d87627b9 100644 --- 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 @@ -4,13 +4,13 @@ import os import pytest from tests.utils import raise_ -from monkey_island.cc.services import DirectoryFileStorageService +from monkey_island.cc.repository import FilesystemStorage from monkey_island.cc.services.post_breach_files import PostBreachFilesService @pytest.fixture def file_storage_service(tmp_path): - return DirectoryFileStorageService(tmp_path) + return FilesystemStorage(tmp_path) @pytest.fixture(autouse=True) From faf2259c59f5d1afce7f33b11d157f7cf396c6ce Mon Sep 17 00:00:00 2001 From: vakarisz Date: Tue, 31 May 2022 12:59:38 +0300 Subject: [PATCH 2/5] Island: Rename repository file names to snake case --- ...gentRepository.py => i_agent_repository.py} | 0 ...ackRepository.py => i_attack_repository.py} | 0 ...figRepository.py => i_config_repository.py} | 0 .../{ILogRepository.py => i_log_repository.py} | 0 ...neRepository.py => i_machine_repository.py} | 0 ...pository.py => i_network_map_repository.py} | 0 ...ortRepository.py => i_report_repository.py} | 0 ...epository.py => i_simulation_repository.py} | 0 ...y.py => i_stolen_credentials_repository.py} | 0 ...Repository.py => i_telemetry_repository.py} | 0 vulture_allowlist.py | 18 +++++++++--------- 11 files changed, 9 insertions(+), 9 deletions(-) rename monkey/monkey_island/cc/repository/{IAgentRepository.py => i_agent_repository.py} (100%) rename monkey/monkey_island/cc/repository/{IAttackRepository.py => i_attack_repository.py} (100%) rename monkey/monkey_island/cc/repository/{IConfigRepository.py => i_config_repository.py} (100%) rename monkey/monkey_island/cc/repository/{ILogRepository.py => i_log_repository.py} (100%) rename monkey/monkey_island/cc/repository/{IMachineRepository.py => i_machine_repository.py} (100%) rename monkey/monkey_island/cc/repository/{INetworkMapRepository.py => i_network_map_repository.py} (100%) rename monkey/monkey_island/cc/repository/{IReportRepository.py => i_report_repository.py} (100%) rename monkey/monkey_island/cc/repository/{ISimulationRepository.py => i_simulation_repository.py} (100%) rename monkey/monkey_island/cc/repository/{IStolenCredentialsRepository.py => i_stolen_credentials_repository.py} (100%) rename monkey/monkey_island/cc/repository/{ITelemetryRepository.py => i_telemetry_repository.py} (100%) diff --git a/monkey/monkey_island/cc/repository/IAgentRepository.py b/monkey/monkey_island/cc/repository/i_agent_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/IAgentRepository.py rename to monkey/monkey_island/cc/repository/i_agent_repository.py diff --git a/monkey/monkey_island/cc/repository/IAttackRepository.py b/monkey/monkey_island/cc/repository/i_attack_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/IAttackRepository.py rename to monkey/monkey_island/cc/repository/i_attack_repository.py diff --git a/monkey/monkey_island/cc/repository/IConfigRepository.py b/monkey/monkey_island/cc/repository/i_config_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/IConfigRepository.py rename to monkey/monkey_island/cc/repository/i_config_repository.py diff --git a/monkey/monkey_island/cc/repository/ILogRepository.py b/monkey/monkey_island/cc/repository/i_log_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/ILogRepository.py rename to monkey/monkey_island/cc/repository/i_log_repository.py diff --git a/monkey/monkey_island/cc/repository/IMachineRepository.py b/monkey/monkey_island/cc/repository/i_machine_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/IMachineRepository.py rename to monkey/monkey_island/cc/repository/i_machine_repository.py diff --git a/monkey/monkey_island/cc/repository/INetworkMapRepository.py b/monkey/monkey_island/cc/repository/i_network_map_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/INetworkMapRepository.py rename to monkey/monkey_island/cc/repository/i_network_map_repository.py diff --git a/monkey/monkey_island/cc/repository/IReportRepository.py b/monkey/monkey_island/cc/repository/i_report_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/IReportRepository.py rename to monkey/monkey_island/cc/repository/i_report_repository.py diff --git a/monkey/monkey_island/cc/repository/ISimulationRepository.py b/monkey/monkey_island/cc/repository/i_simulation_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/ISimulationRepository.py rename to monkey/monkey_island/cc/repository/i_simulation_repository.py diff --git a/monkey/monkey_island/cc/repository/IStolenCredentialsRepository.py b/monkey/monkey_island/cc/repository/i_stolen_credentials_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/IStolenCredentialsRepository.py rename to monkey/monkey_island/cc/repository/i_stolen_credentials_repository.py diff --git a/monkey/monkey_island/cc/repository/ITelemetryRepository.py b/monkey/monkey_island/cc/repository/i_telemetry_repository.py similarity index 100% rename from monkey/monkey_island/cc/repository/ITelemetryRepository.py rename to monkey/monkey_island/cc/repository/i_telemetry_repository.py diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 553c9780e..edfebe4cb 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -8,16 +8,16 @@ from monkey_island.cc import app from monkey_island.cc.models import Report from monkey_island.cc.models.networkmap import Arc, NetworkMap from monkey_island.cc.repository.attack.IMitigationsRepository import IMitigationsRepository -from monkey_island.cc.repository.IAgentRepository import IAgentRepository -from monkey_island.cc.repository.IAttackRepository import IAttackRepository -from monkey_island.cc.repository.IConfigRepository import IConfigRepository -from monkey_island.cc.repository.ILogRepository import ILogRepository -from monkey_island.cc.repository.IMachineRepository import IMachineRepository -from monkey_island.cc.repository.INetworkMapRepository import INetworkMapRepository -from monkey_island.cc.repository.IReportRepository import IReportRepository -from monkey_island.cc.repository.ISimulationRepository import ISimulationRepository +from monkey_island.cc.repository.i_agent_repository import IAgentRepository +from monkey_island.cc.repository.i_attack_repository import IAttackRepository +from monkey_island.cc.repository.i_config_repository import IConfigRepository +from monkey_island.cc.repository.i_log_repository import ILogRepository +from monkey_island.cc.repository.i_machine_repository import IMachineRepository +from monkey_island.cc.repository.i_network_map_repository import INetworkMapRepository +from monkey_island.cc.repository.i_report_repository import IReportRepository +from monkey_island.cc.repository.i_simulation_repository import ISimulationRepository +from monkey_island.cc.repository.i_telemetry_repository import ITelemetryRepository from monkey_island.cc.repository.IStolenCredentials import IStolenCredentialsRepository -from monkey_island.cc.repository.ITelemetryRepository import ITelemetryRepository from monkey_island.cc.repository.zero_trust.IEventRepository import IEventRepository from monkey_island.cc.repository.zero_trust.IFindingRepository import IFindingRepository From 6c1fa80f429fcf9f1edc08390c9b128a0be08002 Mon Sep 17 00:00:00 2001 From: vakarisz Date: Tue, 31 May 2022 13:59:47 +0300 Subject: [PATCH 3/5] Island: Rename Filesystem to FileSystem --- .../monkey_island/cc/repository/__init__.py | 2 +- ...stem_storage.py => file_system_storage.py} | 2 +- .../monkey_island/cc/services/initialize.py | 4 ++-- .../test_directory_file_storage_service.py | 24 +++++++++---------- .../cc/services/test_post_breach_files.py | 4 ++-- 5 files changed, 18 insertions(+), 18 deletions(-) rename monkey/monkey_island/cc/repository/file_storage/{filesystem_storage.py => file_system_storage.py} (98%) diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index 020b5a1f2..249e17fd4 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -1,2 +1,2 @@ -from .file_storage.filesystem_storage import FilesystemStorage +from .file_storage.file_system_storage import FileSystemStorage from .file_storage.i_file_repository import IFileRepository, FileRetrievalError diff --git a/monkey/monkey_island/cc/repository/file_storage/filesystem_storage.py b/monkey/monkey_island/cc/repository/file_storage/file_system_storage.py similarity index 98% rename from monkey/monkey_island/cc/repository/file_storage/filesystem_storage.py rename to monkey/monkey_island/cc/repository/file_storage/file_system_storage.py index 1d4f1fad2..adc6ff700 100644 --- a/monkey/monkey_island/cc/repository/file_storage/filesystem_storage.py +++ b/monkey/monkey_island/cc/repository/file_storage/file_system_storage.py @@ -11,7 +11,7 @@ from .i_file_repository import FileRetrievalError, IFileRepository logger = logging.getLogger(__name__) -class FilesystemStorage(IFileRepository): +class FileSystemStorage(IFileRepository): """ A implementation of IFileStorageService that reads and writes files from/to the local filesystem. diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 3d08d5ca1..08396510b 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -2,7 +2,7 @@ from pathlib import Path from common import DIContainer from common.aws import AWSInstance -from monkey_island.cc.repository import FilesystemStorage, IFileRepository +from monkey_island.cc.repository import FileSystemStorage, IFileRepository from monkey_island.cc.services import AWSService from monkey_island.cc.services.post_breach_files import PostBreachFilesService from monkey_island.cc.services.run_local_monkey import LocalMonkeyRunService @@ -15,7 +15,7 @@ def initialize_services(data_dir: Path) -> DIContainer: container = DIContainer() container.register_instance(AWSInstance, AWSInstance()) - container.register_instance(IFileRepository, FilesystemStorage(data_dir / "custom_pbas")) + container.register_instance(IFileRepository, FileSystemStorage(data_dir / "custom_pbas")) container.register_instance(AWSService, container.resolve(AWSService)) # This is temporary until we get DI all worked out. diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py index 7ccb62a20..184f66edd 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py @@ -4,7 +4,7 @@ from pathlib import Path import pytest from tests.monkey_island.utils import assert_linux_permissions, assert_windows_permissions -from monkey_island.cc.repository import FileRetrievalError, FilesystemStorage +from monkey_island.cc.repository import FileRetrievalError, FileSystemStorage from monkey_island.cc.server_utils.file_utils import is_windows_os @@ -13,13 +13,13 @@ def test_error_if_storage_directory_is_file(tmp_path): new_file.write_text("HelloWorld!") with pytest.raises(ValueError): - FilesystemStorage(new_file) + FileSystemStorage(new_file) def test_directory_created(tmp_path): new_dir = tmp_path / "new_dir" - FilesystemStorage(new_dir) + FileSystemStorage(new_dir) assert new_dir.exists() and new_dir.is_dir() @@ -28,7 +28,7 @@ def test_directory_created(tmp_path): def test_directory_permissions__linux(tmp_path): new_dir = tmp_path / "new_dir" - FilesystemStorage(new_dir) + FileSystemStorage(new_dir) assert_linux_permissions(new_dir) @@ -37,7 +37,7 @@ def test_directory_permissions__linux(tmp_path): def test_directory_permissions__windows(tmp_path): new_dir = tmp_path / "new_dir" - FilesystemStorage(new_dir) + FileSystemStorage(new_dir) assert_windows_permissions(new_dir) @@ -47,7 +47,7 @@ def save_file(tmp_path, file_path_prefix=""): file_contents = "Hello World!" expected_file_path = tmp_path / file_name - fss = FilesystemStorage(tmp_path) + fss = FileSystemStorage(tmp_path) fss.save_file(Path(file_path_prefix) / file_name, io.BytesIO(file_contents.encode())) assert expected_file_path.is_file() @@ -60,7 +60,7 @@ def delete_file(tmp_path, file_path_prefix=""): file.touch() assert file.is_file() - fss = FilesystemStorage(tmp_path) + fss = FileSystemStorage(tmp_path) fss.delete_file(Path(file_path_prefix) / file_name) assert not file.exists() @@ -72,7 +72,7 @@ def open_file(tmp_path, file_path_prefix=""): expected_file_path = tmp_path / file_name expected_file_path.write_text(expected_file_contents) - fss = FilesystemStorage(tmp_path) + fss = FileSystemStorage(tmp_path) with fss.open_file(Path(file_path_prefix) / file_name) as f: actual_file_contents = f.read() @@ -101,7 +101,7 @@ def test_remove_all_files(tmp_path): for filename in ["1.txt", "2.txt", "3.txt"]: (tmp_path / filename).touch() - fss = FilesystemStorage(tmp_path) + fss = FileSystemStorage(tmp_path) fss.delete_all_files() for file in tmp_path.iterdir(): @@ -114,7 +114,7 @@ def test_remove_all_files__skip_directories(tmp_path): for filename in ["1.txt", "2.txt", "3.txt"]: (tmp_path / filename).touch() - fss = FilesystemStorage(tmp_path) + fss = FileSystemStorage(tmp_path) fss.delete_all_files() for file in tmp_path.iterdir(): @@ -122,14 +122,14 @@ def test_remove_all_files__skip_directories(tmp_path): def test_remove_nonexistant_file(tmp_path): - fss = FilesystemStorage(tmp_path) + fss = FileSystemStorage(tmp_path) # This test will fail if this call raises an exception. fss.delete_file("nonexistant_file.txt") def test_open_nonexistant_file(tmp_path): - fss = FilesystemStorage(tmp_path) + fss = FileSystemStorage(tmp_path) with pytest.raises(FileRetrievalError): fss.open_file("nonexistant_file.txt") 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 index 6d87627b9..a6f5b5a24 100644 --- 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 @@ -4,13 +4,13 @@ import os import pytest from tests.utils import raise_ -from monkey_island.cc.repository import FilesystemStorage +from monkey_island.cc.repository import FileSystemStorage from monkey_island.cc.services.post_breach_files import PostBreachFilesService @pytest.fixture def file_storage_service(tmp_path): - return FilesystemStorage(tmp_path) + return FileSystemStorage(tmp_path) @pytest.fixture(autouse=True) From 7caba8e3996a35a06adc14c7921043a86866392f Mon Sep 17 00:00:00 2001 From: vakarisz Date: Tue, 31 May 2022 15:57:38 +0300 Subject: [PATCH 4/5] Island: Rename FileSystemStorage to LocalStorageFileRepository --- .../monkey_island/cc/repository/__init__.py | 2 +- ...ge.py => local_storage_file_repository.py} | 2 +- .../monkey_island/cc/services/initialize.py | 6 +++-- .../test_directory_file_storage_service.py | 24 +++++++++---------- .../cc/services/test_post_breach_files.py | 4 ++-- 5 files changed, 20 insertions(+), 18 deletions(-) rename monkey/monkey_island/cc/repository/file_storage/{file_system_storage.py => local_storage_file_repository.py} (98%) diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index 249e17fd4..d12e4b150 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -1,2 +1,2 @@ -from .file_storage.file_system_storage import FileSystemStorage +from .file_storage.local_storage_file_repository import LocalStorageFileRepository from .file_storage.i_file_repository import IFileRepository, FileRetrievalError diff --git a/monkey/monkey_island/cc/repository/file_storage/file_system_storage.py b/monkey/monkey_island/cc/repository/file_storage/local_storage_file_repository.py similarity index 98% rename from monkey/monkey_island/cc/repository/file_storage/file_system_storage.py rename to monkey/monkey_island/cc/repository/file_storage/local_storage_file_repository.py index adc6ff700..ec765b4e7 100644 --- a/monkey/monkey_island/cc/repository/file_storage/file_system_storage.py +++ b/monkey/monkey_island/cc/repository/file_storage/local_storage_file_repository.py @@ -11,7 +11,7 @@ from .i_file_repository import FileRetrievalError, IFileRepository logger = logging.getLogger(__name__) -class FileSystemStorage(IFileRepository): +class LocalStorageFileRepository(IFileRepository): """ A implementation of IFileStorageService that reads and writes files from/to the local filesystem. diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 08396510b..431eb68d3 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -2,7 +2,7 @@ from pathlib import Path from common import DIContainer from common.aws import AWSInstance -from monkey_island.cc.repository import FileSystemStorage, IFileRepository +from monkey_island.cc.repository import IFileRepository, LocalStorageFileRepository from monkey_island.cc.services import AWSService from monkey_island.cc.services.post_breach_files import PostBreachFilesService from monkey_island.cc.services.run_local_monkey import LocalMonkeyRunService @@ -15,7 +15,9 @@ def initialize_services(data_dir: Path) -> DIContainer: container = DIContainer() container.register_instance(AWSInstance, AWSInstance()) - container.register_instance(IFileRepository, FileSystemStorage(data_dir / "custom_pbas")) + container.register_instance( + IFileRepository, LocalStorageFileRepository(data_dir / "custom_pbas") + ) container.register_instance(AWSService, container.resolve(AWSService)) # This is temporary until we get DI all worked out. diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py index 184f66edd..cf506ffd0 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py @@ -4,7 +4,7 @@ from pathlib import Path import pytest from tests.monkey_island.utils import assert_linux_permissions, assert_windows_permissions -from monkey_island.cc.repository import FileRetrievalError, FileSystemStorage +from monkey_island.cc.repository import FileRetrievalError, LocalStorageFileRepository from monkey_island.cc.server_utils.file_utils import is_windows_os @@ -13,13 +13,13 @@ def test_error_if_storage_directory_is_file(tmp_path): new_file.write_text("HelloWorld!") with pytest.raises(ValueError): - FileSystemStorage(new_file) + LocalStorageFileRepository(new_file) def test_directory_created(tmp_path): new_dir = tmp_path / "new_dir" - FileSystemStorage(new_dir) + LocalStorageFileRepository(new_dir) assert new_dir.exists() and new_dir.is_dir() @@ -28,7 +28,7 @@ def test_directory_created(tmp_path): def test_directory_permissions__linux(tmp_path): new_dir = tmp_path / "new_dir" - FileSystemStorage(new_dir) + LocalStorageFileRepository(new_dir) assert_linux_permissions(new_dir) @@ -37,7 +37,7 @@ def test_directory_permissions__linux(tmp_path): def test_directory_permissions__windows(tmp_path): new_dir = tmp_path / "new_dir" - FileSystemStorage(new_dir) + LocalStorageFileRepository(new_dir) assert_windows_permissions(new_dir) @@ -47,7 +47,7 @@ def save_file(tmp_path, file_path_prefix=""): file_contents = "Hello World!" expected_file_path = tmp_path / file_name - fss = FileSystemStorage(tmp_path) + fss = LocalStorageFileRepository(tmp_path) fss.save_file(Path(file_path_prefix) / file_name, io.BytesIO(file_contents.encode())) assert expected_file_path.is_file() @@ -60,7 +60,7 @@ def delete_file(tmp_path, file_path_prefix=""): file.touch() assert file.is_file() - fss = FileSystemStorage(tmp_path) + fss = LocalStorageFileRepository(tmp_path) fss.delete_file(Path(file_path_prefix) / file_name) assert not file.exists() @@ -72,7 +72,7 @@ def open_file(tmp_path, file_path_prefix=""): expected_file_path = tmp_path / file_name expected_file_path.write_text(expected_file_contents) - fss = FileSystemStorage(tmp_path) + fss = LocalStorageFileRepository(tmp_path) with fss.open_file(Path(file_path_prefix) / file_name) as f: actual_file_contents = f.read() @@ -101,7 +101,7 @@ def test_remove_all_files(tmp_path): for filename in ["1.txt", "2.txt", "3.txt"]: (tmp_path / filename).touch() - fss = FileSystemStorage(tmp_path) + fss = LocalStorageFileRepository(tmp_path) fss.delete_all_files() for file in tmp_path.iterdir(): @@ -114,7 +114,7 @@ def test_remove_all_files__skip_directories(tmp_path): for filename in ["1.txt", "2.txt", "3.txt"]: (tmp_path / filename).touch() - fss = FileSystemStorage(tmp_path) + fss = LocalStorageFileRepository(tmp_path) fss.delete_all_files() for file in tmp_path.iterdir(): @@ -122,14 +122,14 @@ def test_remove_all_files__skip_directories(tmp_path): def test_remove_nonexistant_file(tmp_path): - fss = FileSystemStorage(tmp_path) + fss = LocalStorageFileRepository(tmp_path) # This test will fail if this call raises an exception. fss.delete_file("nonexistant_file.txt") def test_open_nonexistant_file(tmp_path): - fss = FileSystemStorage(tmp_path) + fss = LocalStorageFileRepository(tmp_path) with pytest.raises(FileRetrievalError): fss.open_file("nonexistant_file.txt") 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 index a6f5b5a24..a4439cb97 100644 --- 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 @@ -4,13 +4,13 @@ import os import pytest from tests.utils import raise_ -from monkey_island.cc.repository import FileSystemStorage +from monkey_island.cc.repository import LocalStorageFileRepository from monkey_island.cc.services.post_breach_files import PostBreachFilesService @pytest.fixture def file_storage_service(tmp_path): - return FileSystemStorage(tmp_path) + return LocalStorageFileRepository(tmp_path) @pytest.fixture(autouse=True) From 027c53a09037ff791d6f7cd3f8a607e47f9e157c Mon Sep 17 00:00:00 2001 From: vakarisz Date: Tue, 31 May 2022 16:13:00 +0300 Subject: [PATCH 5/5] UT: Rename variables and fixtures to file repository --- .../cc/resources/test_pba_file_download.py | 4 ++-- .../cc/resources/test_pba_file_upload.py | 14 +++++++------- .../cc/services/test_post_breach_files.py | 16 ++++++++-------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_download.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_download.py index 709c6c7e2..cf2a109b9 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_download.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_download.py @@ -12,7 +12,7 @@ FILE_NAME = "test_file" FILE_CONTENTS = b"HelloWorld!" -class MockFileStorageService(IFileRepository): +class MockFileRepository(IFileRepository): def __init__(self): self._file = io.BytesIO(FILE_CONTENTS) @@ -35,7 +35,7 @@ class MockFileStorageService(IFileRepository): @pytest.fixture def flask_client(build_flask_client): container = StubDIContainer() - container.register(IFileRepository, MockFileStorageService) + container.register(IFileRepository, MockFileRepository) with build_flask_client(container) as flask_client: yield flask_client diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_upload.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_upload.py index 9f7796263..2642c0758 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_upload.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_pba_file_upload.py @@ -40,7 +40,7 @@ def mock_get_config_value(monkeypatch): ) -class MockFileStorageService(IFileRepository): +class MockFileRepository(IFileRepository): def __init__(self): self._file = None @@ -60,14 +60,14 @@ class MockFileStorageService(IFileRepository): @pytest.fixture -def file_storage_service(): - return MockFileStorageService() +def file_repository(): + return MockFileRepository() @pytest.fixture -def flask_client(build_flask_client, file_storage_service): +def flask_client(build_flask_client, file_repository): container = StubDIContainer() - container.register_instance(IFileRepository, file_storage_service) + container.register_instance(IFileRepository, file_repository) with build_flask_client(container) as flask_client: yield flask_client @@ -98,9 +98,9 @@ def test_pba_file_upload_post__invalid(flask_client, mock_set_config_value): @pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE]) def test_pba_file_upload_post__internal_server_error( - flask_client, pba_os, mock_set_config_value, file_storage_service + flask_client, pba_os, mock_set_config_value, file_repository ): - file_storage_service.save_file = lambda x, y: raise_(Exception()) + file_repository.save_file = lambda x, y: raise_(Exception()) url = get_url_for_resource(FileUpload, target_os=pba_os) resp = flask_client.post( 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 index a4439cb97..708fdc034 100644 --- 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 @@ -9,18 +9,18 @@ from monkey_island.cc.services.post_breach_files import PostBreachFilesService @pytest.fixture -def file_storage_service(tmp_path): +def local_storage_file_repository(tmp_path): return LocalStorageFileRepository(tmp_path) @pytest.fixture(autouse=True) -def post_breach_files_service(file_storage_service): - PostBreachFilesService.initialize(file_storage_service) +def post_breach_files_service(local_storage_file_repository): + PostBreachFilesService.initialize(local_storage_file_repository) -def test_remove_pba_files(file_storage_service, tmp_path): - file_storage_service.save_file("linux_file", io.BytesIO(b"")) - file_storage_service.save_file("windows_file", io.BytesIO(b"")) +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() @@ -33,11 +33,11 @@ def dir_is_empty(dir_path): return len(dir_contents) == 0 -def test_remove_failure(file_storage_service, monkeypatch): +def test_remove_failure(local_storage_file_repository, monkeypatch): monkeypatch.setattr(os, "remove", lambda x: raise_(OSError("Permission denied"))) try: - file_storage_service.save_file("windows_file", io.BytesIO(b"")) + 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}")