Merge pull request #1300 from guardicore/expand-path-returns-path

Expand path returns path
This commit is contained in:
Mike Salvatore 2021-07-07 19:11:10 -04:00 committed by GitHub
commit 0db85ae407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 17 deletions

View File

@ -1,11 +1,13 @@
import os import os
from pathlib import Path
class InvalidPath(Exception): class InvalidPath(Exception):
pass pass
def expand_path(path: str) -> str: def expand_path(path: str) -> Path:
if not path: if not path:
raise InvalidPath("Empty path provided") raise InvalidPath("Empty path provided")
return os.path.expandvars(os.path.expanduser(path))
return Path(os.path.expandvars(os.path.expanduser(path)))

View File

@ -45,7 +45,7 @@ class RansomwarePayload:
target_dir_field = target_directories["linux_target_dir"] target_dir_field = target_directories["linux_target_dir"]
try: try:
return Path(expand_path(target_dir_field)) return expand_path(target_dir_field)
except InvalidPath as e: except InvalidPath as e:
LOG.debug(f"Target ransomware dir set to None: {e}") LOG.debug(f"Target ransomware dir set to None: {e}")
return None return None

View File

@ -37,9 +37,7 @@ MONGO_EXECUTABLE_PATH = (
_MONGO_EXECUTABLE_PATH_WIN if is_windows_os() else _MONGO_EXECUTABLE_PATH_LINUX _MONGO_EXECUTABLE_PATH_WIN if is_windows_os() else _MONGO_EXECUTABLE_PATH_LINUX
) )
DEFAULT_SERVER_CONFIG_PATH = expand_path( DEFAULT_SERVER_CONFIG_PATH = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME))
os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME)
)
DEFAULT_LOG_LEVEL = "INFO" DEFAULT_LOG_LEVEL = "INFO"

View File

@ -7,14 +7,14 @@ from common.utils.file_utils import InvalidPath, expand_path
def test_expand_user(patched_home_env): def test_expand_user(patched_home_env):
input_path = os.path.join("~", "test") 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 assert expand_path(input_path) == expected_path
def test_expand_vars(patched_home_env): def test_expand_vars(patched_home_env):
input_path = os.path.join("$HOME", "test") 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 assert expand_path(input_path) == expected_path

View File

@ -1,4 +1,5 @@
import os import os
from pathlib import Path
from monkey_island.cc.server_utils.consts import ( from monkey_island.cc.server_utils.consts import (
DEFAULT_CRT_PATH, DEFAULT_CRT_PATH,
@ -37,7 +38,7 @@ def test_data_dir_expanduser(patched_home_env):
assert_data_dir_equals( assert_data_dir_equals(
{"data_dir": os.path.join("~", DATA_DIR_NAME)}, {"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( assert_data_dir_equals(
{"data_dir": os.path.join("$HOME", DATA_DIR_NAME)}, {"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): 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(): def test_log_level():
@ -86,7 +87,7 @@ def test_crt_path_expanduser(patched_home_env):
assert_ssl_certificate_file_equals( assert_ssl_certificate_file_equals(
{"ssl_certificate": {"ssl_certificate_file": os.path.join("~", FILE_NAME)}}, {"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( assert_ssl_certificate_file_equals(
{"ssl_certificate": {"ssl_certificate_file": os.path.join("$HOME", FILE_NAME)}}, {"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): def assert_ssl_certificate_file_equals(config_file_contents, expected_ssl_certificate_file):
assert_island_config_option_equals( 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( assert_ssl_certificate_key_file_equals(
{"ssl_certificate": {"ssl_certificate_key_file": os.path.join("~", FILE_NAME)}}, {"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( assert_ssl_certificate_key_file_equals(
{"ssl_certificate": {"ssl_certificate_key_file": os.path.join("$HOME", FILE_NAME)}}, {"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): def assert_ssl_certificate_key_file_equals(config_file_contents, expected_ssl_certificate_file):
assert_island_config_option_equals( assert_island_config_option_equals(
config_file_contents, "key_path", expected_ssl_certificate_file config_file_contents, "key_path", Path(expected_ssl_certificate_file)
) )