From 7caba8e3996a35a06adc14c7921043a86866392f Mon Sep 17 00:00:00 2001 From: vakarisz Date: Tue, 31 May 2022 15:57:38 +0300 Subject: [PATCH] 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)