Island: Return Path object from expand_path()

This commit is contained in:
Mike Salvatore 2021-07-06 12:21:44 -04:00
parent 8508a9f98f
commit ae7687243f
4 changed files with 17 additions and 14 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

@ -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)
) )