diff --git a/monkey/common/utils/file_utils.py b/monkey/common/utils/file_utils.py index 6110cd020..a4cff2b48 100644 --- a/monkey/common/utils/file_utils.py +++ b/monkey/common/utils/file_utils.py @@ -1,11 +1,13 @@ import os +from pathlib import Path class InvalidPath(Exception): pass -def expand_path(path: str) -> str: +def expand_path(path: str) -> Path: if not path: raise InvalidPath("Empty path provided") - return os.path.expandvars(os.path.expanduser(path)) + + return Path(os.path.expandvars(os.path.expanduser(path))) diff --git a/monkey/infection_monkey/ransomware/ransomware_payload.py b/monkey/infection_monkey/ransomware/ransomware_payload.py index f2e3eb476..29d1fcd65 100644 --- a/monkey/infection_monkey/ransomware/ransomware_payload.py +++ b/monkey/infection_monkey/ransomware/ransomware_payload.py @@ -45,7 +45,7 @@ class RansomwarePayload: target_dir_field = target_directories["linux_target_dir"] try: - return Path(expand_path(target_dir_field)) + return expand_path(target_dir_field) except InvalidPath as e: LOG.debug(f"Target ransomware dir set to None: {e}") return None diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index 7c6cee89d..dbbe67531 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -37,9 +37,7 @@ MONGO_EXECUTABLE_PATH = ( _MONGO_EXECUTABLE_PATH_WIN if is_windows_os() else _MONGO_EXECUTABLE_PATH_LINUX ) -DEFAULT_SERVER_CONFIG_PATH = expand_path( - os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME) -) +DEFAULT_SERVER_CONFIG_PATH = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME)) DEFAULT_LOG_LEVEL = "INFO" diff --git a/monkey/tests/unit_tests/common/utils/test_common_file_utils.py b/monkey/tests/unit_tests/common/utils/test_common_file_utils.py index b67341cfe..3fe981a74 100644 --- a/monkey/tests/unit_tests/common/utils/test_common_file_utils.py +++ b/monkey/tests/unit_tests/common/utils/test_common_file_utils.py @@ -7,14 +7,14 @@ from common.utils.file_utils import InvalidPath, expand_path def test_expand_user(patched_home_env): input_path = os.path.join("~", "test") - expected_path = os.path.join(patched_home_env, "test") + expected_path = patched_home_env / "test" assert expand_path(input_path) == expected_path def test_expand_vars(patched_home_env): input_path = os.path.join("$HOME", "test") - expected_path = os.path.join(patched_home_env, "test") + expected_path = patched_home_env / "test" assert expand_path(input_path) == expected_path diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py index 1554980a2..c9964af7e 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py @@ -1,4 +1,5 @@ import os +from pathlib import Path from monkey_island.cc.server_utils.consts import ( DEFAULT_CRT_PATH, @@ -37,7 +38,7 @@ def test_data_dir_expanduser(patched_home_env): assert_data_dir_equals( {"data_dir": os.path.join("~", DATA_DIR_NAME)}, - os.path.join(patched_home_env, DATA_DIR_NAME), + patched_home_env / DATA_DIR_NAME, ) @@ -46,12 +47,12 @@ def test_data_dir_expandvars(patched_home_env): assert_data_dir_equals( {"data_dir": os.path.join("$HOME", DATA_DIR_NAME)}, - os.path.join(patched_home_env, DATA_DIR_NAME), + patched_home_env / DATA_DIR_NAME, ) def assert_data_dir_equals(config_file_contents, expected_data_dir): - assert_island_config_option_equals(config_file_contents, "data_dir", expected_data_dir) + assert_island_config_option_equals(config_file_contents, "data_dir", Path(expected_data_dir)) def test_log_level(): @@ -86,7 +87,7 @@ def test_crt_path_expanduser(patched_home_env): assert_ssl_certificate_file_equals( {"ssl_certificate": {"ssl_certificate_file": os.path.join("~", FILE_NAME)}}, - os.path.join(patched_home_env, FILE_NAME), + patched_home_env / FILE_NAME, ) @@ -95,13 +96,13 @@ def test_crt_path_expandvars(patched_home_env): assert_ssl_certificate_file_equals( {"ssl_certificate": {"ssl_certificate_file": os.path.join("$HOME", FILE_NAME)}}, - os.path.join(patched_home_env, FILE_NAME), + patched_home_env / FILE_NAME, ) def assert_ssl_certificate_file_equals(config_file_contents, expected_ssl_certificate_file): assert_island_config_option_equals( - config_file_contents, "crt_path", expected_ssl_certificate_file + config_file_contents, "crt_path", Path(expected_ssl_certificate_file) ) @@ -121,7 +122,7 @@ def test_key_path_expanduser(patched_home_env): assert_ssl_certificate_key_file_equals( {"ssl_certificate": {"ssl_certificate_key_file": os.path.join("~", FILE_NAME)}}, - os.path.join(patched_home_env, FILE_NAME), + patched_home_env / FILE_NAME, ) @@ -130,13 +131,13 @@ def test_key_path_expandvars(patched_home_env): assert_ssl_certificate_key_file_equals( {"ssl_certificate": {"ssl_certificate_key_file": os.path.join("$HOME", FILE_NAME)}}, - os.path.join(patched_home_env, FILE_NAME), + patched_home_env / FILE_NAME, ) def assert_ssl_certificate_key_file_equals(config_file_contents, expected_ssl_certificate_file): assert_island_config_option_equals( - config_file_contents, "key_path", expected_ssl_certificate_file + config_file_contents, "key_path", Path(expected_ssl_certificate_file) )