diff --git a/monkey/monkey_island/cc/environment/data_dir_generator.py b/monkey/monkey_island/cc/environment/data_dir_generator.py deleted file mode 100644 index 58e16d4b7..000000000 --- a/monkey/monkey_island/cc/environment/data_dir_generator.py +++ /dev/null @@ -1,30 +0,0 @@ -import logging -import os - -from monkey_island.cc.environment.utils import is_windows_os -from monkey_island.cc.environment.windows_permissions import set_full_folder_access - -LOG = logging.getLogger(__name__) - - -def create_data_dir(data_dir: str, create_parent_dirs: bool) -> None: - if not os.path.isdir(data_dir): - try: - if create_parent_dirs: - os.makedirs(data_dir, mode=0o700) - else: - os.mkdir(data_dir, mode=0o700) - except Exception as ex: - LOG.error( - f'Could not create data directory at "{data_dir}" (maybe `$HOME` could not be ' - f"resolved?): {str(ex)}" - ) - - if is_windows_os(): # `mode=0o700` doesn't work on Windows - try: - set_full_folder_access(folder_path=data_dir) - except Exception as ex: - LOG.error( - f'Data directory was created at "{data_dir}" but permissions could not be ' - f"set successfully: {str(ex)}" - ) diff --git a/monkey/monkey_island/cc/environment/utils.py b/monkey/monkey_island/cc/environment/utils.py index 7d74e4f2b..907e30d47 100644 --- a/monkey/monkey_island/cc/environment/utils.py +++ b/monkey/monkey_island/cc/environment/utils.py @@ -8,11 +8,9 @@ def is_windows_os() -> bool: if is_windows_os(): - from monkey_island.cc.environment.windows_permissions import ( # noqa: E402 - set_full_folder_access, - ) + import monkey_island.cc.environment.windows_permissions as windows_permissions else: - from monkey_island.cc.environment.linux_permissions import set_perms_to_owner_only # noqa: E402 + import monkey_island.cc.environment.linux_permissions as linux_permissions # noqa: E402 LOG = logging.getLogger(__name__) @@ -31,7 +29,7 @@ def create_directory(path: str, create_parent_dirs: bool): os.mkdir(path) except Exception as ex: LOG.error( - f'Could not create a directory at "{path}" (maybe `$HOME` could not be ' + f'Could not create a directory at "{path}" (maybe environmental variables could not be ' f"resolved?): {str(ex)}" ) raise ex @@ -40,9 +38,9 @@ def create_directory(path: str, create_parent_dirs: bool): def set_secure_permissions(dir_path: str): try: if is_windows_os(): - set_full_folder_access(folder_path=dir_path) + windows_permissions.set_perms_to_owner_only(folder_path=dir_path) else: - set_perms_to_owner_only(path=dir_path) + linux_permissions.set_perms_to_owner_only(path=dir_path) except Exception as ex: - LOG.error(f"Permissions could not be " f"set successfully for {dir_path}: {str(ex)}") + LOG.error(f"Permissions could not be set successfully for {dir_path}: {str(ex)}") raise ex diff --git a/monkey/monkey_island/cc/environment/windows_permissions.py b/monkey/monkey_island/cc/environment/windows_permissions.py index 5d4913151..225e52370 100644 --- a/monkey/monkey_island/cc/environment/windows_permissions.py +++ b/monkey/monkey_island/cc/environment/windows_permissions.py @@ -4,7 +4,7 @@ import win32con import win32security -def set_full_folder_access(folder_path: str) -> None: +def set_perms_to_owner_only(folder_path: str) -> None: user = get_user_pySID_object() security_descriptor = win32security.GetFileSecurity( diff --git a/monkey/monkey_island/setup/config_setup.py b/monkey/monkey_island/setup/config_setup.py index 5c9625ac4..50330aea3 100644 --- a/monkey/monkey_island/setup/config_setup.py +++ b/monkey/monkey_island/setup/config_setup.py @@ -2,7 +2,7 @@ import os from typing import Tuple from monkey_island.cc.environment import server_config_handler -from monkey_island.cc.environment.data_dir_generator import create_data_dir # noqa: E402 +from monkey_island.cc.environment.utils import create_secure_directory from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_SERVER_CONFIG_PATH from monkey_island.setup.island_config_options import IslandConfigOptions @@ -10,14 +10,13 @@ from monkey_island.setup.island_config_options import IslandConfigOptions def setup_config_by_cmd_arg(server_config_path) -> Tuple[IslandConfigOptions, str]: server_config_path = os.path.expandvars(os.path.expanduser(server_config_path)) config = server_config_handler.load_server_config_from_file(server_config_path) - - create_data_dir(config.data_dir, create_parent_dirs=True) + create_secure_directory(config.data_dir, create_parent_dirs=True) return config, server_config_path def setup_default_config() -> Tuple[IslandConfigOptions, str]: server_config_path = DEFAULT_SERVER_CONFIG_PATH - create_data_dir(DEFAULT_DATA_DIR, create_parent_dirs=False) + create_secure_directory(DEFAULT_DATA_DIR, create_parent_dirs=False) server_config_handler.create_default_server_config_file() config = server_config_handler.load_server_config_from_file(server_config_path) return config, server_config_path