Island: separate the workflow of config extraction from data directory setup

This commit is contained in:
VakarisZ 2021-11-25 12:11:09 +02:00
parent a3563b97a8
commit 7e21635733
4 changed files with 22 additions and 61 deletions

View File

@ -1,24 +1,8 @@
import json
import os
from pathlib import Path
from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH, SERVER_CONFIG_FILENAME
from monkey_island.cc.setup.island_config_options import IslandConfigOptions
def create_default_server_config_file(data_dir: str) -> str:
config_file_path = os.path.join(data_dir, SERVER_CONFIG_FILENAME)
if not os.path.isfile(config_file_path):
write_default_server_config_to_file(config_file_path)
return config_file_path
def write_default_server_config_to_file(path: str) -> None:
default_config = Path(DEFAULT_SERVER_CONFIG_PATH).read_text()
Path(path).write_text(default_config)
def load_server_config_from_file(server_config_path) -> IslandConfigOptions:
with open(server_config_path, "r") as f:
config_content = f.read()

View File

@ -5,12 +5,12 @@ import sys
from pathlib import Path
from sys import exit
from threading import Thread
from typing import Tuple
import gevent.hub
from gevent.pywsgi import WSGIServer
from monkey_island.cc.server_utils.consts import ISLAND_PORT
from monkey_island.cc.setup.config_setup import extract_server_config
# Add the monkey_island directory to the path, to make sure imports that don't start with
# "monkey_island." work.
@ -18,7 +18,6 @@ MONKEY_ISLAND_DIR_BASE_PATH = str(Path(__file__).parent.parent)
if str(MONKEY_ISLAND_DIR_BASE_PATH) not in sys.path:
sys.path.insert(0, MONKEY_ISLAND_DIR_BASE_PATH)
import monkey_island.cc.setup.config_setup as config_setup # noqa: E402
from common.version import get_version # noqa: E402
from monkey_island.cc.app import init_app # noqa: E402
from monkey_island.cc.arg_parser import IslandCmdArgs # noqa: E402
@ -34,7 +33,7 @@ from monkey_island.cc.services.initialize import initialize_services # noqa: E4
from monkey_island.cc.services.reporting.exporter_init import populate_exporter_list # noqa: E402
from monkey_island.cc.services.utils.network_utils import local_ip_addresses # noqa: E402
from monkey_island.cc.setup import island_config_options_validator # noqa: E402
from monkey_island.cc.setup.data_dir import IncompatibleDataDirectory # noqa: E402
from monkey_island.cc.setup.data_dir import IncompatibleDataDirectory, setup_data_dir # noqa: E402
from monkey_island.cc.setup.gevent_hub_error_handler import GeventHubErrorHandler # noqa: E402
from monkey_island.cc.setup.island_config_options import IslandConfigOptions # noqa: E402
from monkey_island.cc.setup.mongo import mongo_setup # noqa: E402
@ -45,12 +44,13 @@ logger = logging.getLogger(__name__)
def run_monkey_island():
island_args = parse_cli_args()
config_options, server_config_path = _setup_data_dir(island_args)
config_options = _extract_config(island_args)
_setup_data_dir(config_options.data_dir)
_exit_on_invalid_config_options(config_options)
_configure_logging(config_options)
_initialize_globals(config_options, server_config_path)
_initialize_globals(config_options.data_dir)
mongo_db_process = None
if config_options.start_mongodb:
@ -58,19 +58,24 @@ def run_monkey_island():
_connect_to_mongodb(mongo_db_process)
_configure_gevent_exception_handling(Path(config_options.data_dir))
_configure_gevent_exception_handling(config_options.data_dir)
_start_island_server(island_args.setup_only, config_options)
def _setup_data_dir(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, str]:
def _extract_config(island_args: IslandCmdArgs) -> IslandConfigOptions:
try:
return config_setup.setup_server_config(island_args)
return extract_server_config(island_args)
except OSError as ex:
print(f"Error opening server config file: {ex}")
exit(1)
except json.JSONDecodeError as ex:
print(f"Error loading server config: {ex}")
exit(1)
def _setup_data_dir(data_dir_path: Path):
try:
setup_data_dir(data_dir_path)
except IncompatibleDataDirectory as ex:
print(f"Incompatible data directory: {ex}")
exit(1)
@ -89,8 +94,8 @@ def _configure_logging(config_options):
setup_logging(config_options.data_dir, config_options.log_level)
def _initialize_globals(config_options: IslandConfigOptions, server_config_path: str):
initialize_services(config_options.data_dir)
def _initialize_globals(data_dir: Path):
initialize_services(data_dir)
def _start_mongodb(data_dir: Path) -> MongoDbProcess:

View File

@ -1,42 +1,14 @@
from typing import Tuple
from common.utils.file_utils import expand_path
from monkey_island.cc.arg_parser import IslandCmdArgs
from monkey_island.cc.environment import server_config_handler
from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH
from monkey_island.cc.setup.data_dir import setup_data_dir
from monkey_island.cc.setup.island_config_options import IslandConfigOptions
def setup_server_config(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, str]:
def extract_server_config(island_args: IslandCmdArgs) -> IslandConfigOptions:
if island_args.server_config_path:
return _setup_config_by_cmd_arg(island_args.server_config_path)
path_to_config = expand_path(island_args.server_config_path)
else:
path_to_config = DEFAULT_SERVER_CONFIG_PATH
return _setup_default_config()
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
def _setup_default_config() -> Tuple[IslandConfigOptions, str]:
default_config = server_config_handler.load_server_config_from_file(DEFAULT_SERVER_CONFIG_PATH)
default_data_dir = default_config.data_dir
# 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(default_data_dir)
server_config_path = server_config_handler.create_default_server_config_file(default_data_dir)
config = server_config_handler.load_server_config_from_file(server_config_path)
return config, server_config_path
return server_config_handler.load_server_config_from_file(path_to_config)

View File

@ -15,14 +15,14 @@ class IncompatibleDataDirectory(Exception):
pass
def setup_data_dir(data_dir_path: Path) -> None:
def setup_data_dir(data_dir_path: Path):
logger.info(f"Setting up data directory at {data_dir_path}.")
if _is_data_dir_old(data_dir_path):
logger.info("Version in data directory does not match the Island's version.")
_handle_old_data_directory(data_dir_path)
create_secure_directory(str(data_dir_path))
write_version(data_dir_path)
logger.info("Data directory set up.")
logger.info(f"Data directory set up in {data_dir_path}.")
def _is_data_dir_old(data_dir_path: Path) -> bool: