def _setup_config_by_cmd_arg( server_config_path) -> Tuple[IslandConfigOptions, str]: server_config_path = expand_path(server_config_path) config = server_config_handler.load_server_config_from_file( server_config_path) create_secure_directory(str(config.data_dir)) return config, server_config_path
def _set_target_directory(self, os_target_directories: dict): if is_windows_os(): target_directory = os_target_directories["windows_target_dir"] else: target_directory = os_target_directories["linux_target_dir"] try: self.target_directory = expand_path(target_directory) except InvalidPath as e: logger.debug(f"Target ransomware directory set to None: {e}") self.target_directory = None
def __init__(self, config_contents: dict): self.data_dir = expand_path( config_contents.get("data_dir", DEFAULT_DATA_DIR)) self.log_level = config_contents.get("log_level", DEFAULT_LOG_LEVEL) self.start_mongodb = config_contents.get( "mongodb", { "start_mongodb": DEFAULT_START_MONGO_DB }).get("start_mongodb", DEFAULT_START_MONGO_DB) self.crt_path = expand_path( config_contents.get("ssl_certificate", DEFAULT_CERTIFICATE_PATHS).get( "ssl_certificate_file", DEFAULT_CRT_PATH)) self.key_path = expand_path( config_contents.get("ssl_certificate", DEFAULT_CERTIFICATE_PATHS).get( "ssl_certificate_key_file", DEFAULT_KEY_PATH))
def _setup_config_by_cmd_arg( server_config_path) -> Tuple[IslandConfigOptions, str]: server_config_path = expand_path(server_config_path) config = server_config_handler.load_server_config_from_file( server_config_path) # TODO refactor like in https://github.com/guardicore/monkey/pull/1528 because # there's absolutely no reason to be exposed to IslandConfigOptions extraction logic # if you want to modify data directory related code. setup_data_dir(config.data_dir) return config, server_config_path
return r"$HOME/.monkey_island" # TODO: Figure out why windows requires the use of `os.getcwd()`. See issue #1207. def _get_monkey_island_abs_path() -> str: if is_windows_os(): return os.path.join(os.getcwd(), "monkey_island") else: return str(Path(__file__).resolve().parent.parent.parent) SERVER_CONFIG_FILENAME = "server_config.json" MONKEY_ISLAND_ABS_PATH = _get_monkey_island_abs_path() DEFAULT_DATA_DIR = expand_path(get_default_data_dir()) DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5 _MONGO_BINARY_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "bin", "mongodb") _MONGO_EXECUTABLE_PATH_WIN = os.path.join(_MONGO_BINARY_DIR, "mongod.exe") _MONGO_EXECUTABLE_PATH_LINUX = os.path.join(_MONGO_BINARY_DIR, "bin", "mongod") MONGO_EXECUTABLE_PATH = (_MONGO_EXECUTABLE_PATH_WIN if is_windows_os() else _MONGO_EXECUTABLE_PATH_LINUX) MONGO_CONNECTION_TIMEOUT = 15 DEFAULT_SERVER_CONFIG_PATH = str( Path(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME)) DEFAULT_LOG_LEVEL = "INFO"
def test_expand_user(patched_home_env): input_path = os.path.join("~", "test") expected_path = patched_home_env / "test" assert expand_path(input_path) == expected_path
def test_expand_path__empty_path_provided(): with pytest.raises(InvalidPath): expand_path("")