From 86c6a55097095402c5cc94cac069decc3d01f562 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Sat, 30 Apr 2022 01:39:45 +0200 Subject: [PATCH] Island, UT: Fix file_utils paths --- monkey/monkey_island/cc/server_utils/file_utils.py | 13 +++++++------ monkey/monkey_island/cc/setup/data_dir.py | 2 +- monkey/monkey_island/cc/setup/mongo/mongo_setup.py | 6 +++--- .../cc/server_utils/test_file_utils.py | 12 ++++++------ 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/monkey/monkey_island/cc/server_utils/file_utils.py b/monkey/monkey_island/cc/server_utils/file_utils.py index 3371d5acb..73f13e05e 100644 --- a/monkey/monkey_island/cc/server_utils/file_utils.py +++ b/monkey/monkey_island/cc/server_utils/file_utils.py @@ -3,6 +3,7 @@ import os import platform import stat from contextlib import contextmanager +from pathlib import Path from typing import Generator logger = logging.getLogger(__name__) @@ -20,32 +21,32 @@ if is_windows_os(): import monkey_island.cc.server_utils.windows_permissions as windows_permissions -def create_secure_directory(path: str): - if not os.path.isdir(path): +def create_secure_directory(path: Path): + if not path.is_dir(): if is_windows_os(): _create_secure_directory_windows(path) else: _create_secure_directory_linux(path) -def _create_secure_directory_linux(path: str): +def _create_secure_directory_linux(path: Path): try: # Don't split directory creation and permission setting # because it will temporarily create an accessible directory which anyone can use. - os.mkdir(path, mode=stat.S_IRWXU) + path.mkdir(mode=stat.S_IRWXU) except Exception as ex: logger.error(f'Could not create a directory at "{path}": {str(ex)}') raise ex -def _create_secure_directory_windows(path: str): +def _create_secure_directory_windows(path: Path): try: security_attributes = win32security.SECURITY_ATTRIBUTES() security_attributes.SECURITY_DESCRIPTOR = ( windows_permissions.get_security_descriptor_for_owner_only_perms() ) - win32file.CreateDirectory(path, security_attributes) + win32file.CreateDirectory(str(path), security_attributes) except Exception as ex: logger.error(f'Could not create a directory at "{path}": {str(ex)}') diff --git a/monkey/monkey_island/cc/setup/data_dir.py b/monkey/monkey_island/cc/setup/data_dir.py index 124b2a6f7..85be593ae 100644 --- a/monkey/monkey_island/cc/setup/data_dir.py +++ b/monkey/monkey_island/cc/setup/data_dir.py @@ -20,7 +20,7 @@ def setup_data_dir(data_dir_path: Path): if _is_data_dir_old(data_dir_path): logger.info("Version in data directory does not match the Island's version.") _handle_old_data_directory(data_dir_path) - create_secure_directory(str(data_dir_path)) + create_secure_directory(data_dir_path) write_version(data_dir_path) logger.info(f"Data directory set up in {data_dir_path}.") diff --git a/monkey/monkey_island/cc/setup/mongo/mongo_setup.py b/monkey/monkey_island/cc/setup/mongo/mongo_setup.py index 196ad54bf..bbb5046b8 100644 --- a/monkey/monkey_island/cc/setup/mongo/mongo_setup.py +++ b/monkey/monkey_island/cc/setup/mongo/mongo_setup.py @@ -31,12 +31,12 @@ def start_mongodb(data_dir: Path) -> MongoDbProcess: return mongo_db_process -def _create_db_dir(db_dir_parent_path) -> str: - db_dir = os.path.join(db_dir_parent_path, DB_DIR_NAME) +def _create_db_dir(db_dir_parent_path: Path) -> str: + db_dir = db_dir_parent_path / DB_DIR_NAME logger.info(f"Database content directory: {db_dir}.") create_secure_directory(db_dir) - return db_dir + return str(db_dir) def register_mongo_shutdown_callback(mongo_db_process: MongoDbProcess): diff --git a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py index 7d05e29c2..c31d5778a 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py +++ b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py @@ -12,22 +12,22 @@ from monkey_island.cc.server_utils.file_utils import ( @pytest.fixture -def test_path_nested(tmpdir): - path = os.path.join(tmpdir, "test1", "test2", "test3") +def test_path_nested(tmp_path): + path = tmp_path / "test1" / "test2" / "test3" return path @pytest.fixture -def test_path(tmpdir): +def test_path(tmp_path): test_path = "test1" - path = os.path.join(tmpdir, test_path) + path = tmp_path / test_path return path def test_create_secure_directory__already_exists(test_path): - os.mkdir(test_path) - assert os.path.isdir(test_path) + test_path.mkdir() + assert test_path.is_dir() create_secure_directory(test_path)