From 184594f5092bda55c4c3b58085c3b1de7de10d92 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 2 Jun 2021 09:16:00 -0400 Subject: [PATCH] island: Refactor setup/start logic separate concerns Most configuration/setup logic is removed from monkey_island.py. The only responsibility of startup.py is now to setup a default failsafe logger and call run_monkey_island(). setup_island() has been renamed to run_monkey_island(), since that's what it does. Some of the logic used to setup/configure/initialize monkey island has been wrapped in different functions with specific responsibilities. setup_mongodb() has been renamed to start_mongodb(), since that's what it does. --- monkey/monkey_island/cc/server_setup.py | 36 ++++++++++++++----- .../cc/setup/mongo/mongo_setup.py | 2 +- monkey/monkey_island/setup/config_setup.py | 12 +++++-- monkey/monkey_island/startup.py | 27 ++++---------- 4 files changed, 45 insertions(+), 32 deletions(-) diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 208055173..a989f6387 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -15,39 +15,59 @@ if str(MONKEY_ISLAND_DIR_BASE_PATH) not in sys.path: import monkey_island.cc.environment.environment_singleton as env_singleton # 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 parse_cli_args # noqa: E402 from monkey_island.cc.resources.monkey_download import MonkeyDownload # noqa: E402 from monkey_island.cc.server_utils.bootloader_server import BootloaderHttpServer # noqa: E402 from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH # noqa: E402 from monkey_island.cc.server_utils.encryptor import initialize_encryptor # noqa: E402 +from monkey_island.cc.server_utils.island_logger import reset_logger, setup_logging # noqa: E402 from monkey_island.cc.services.initialize import initialize_services # noqa: E402 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.mongo.database_initializer import init_collections # noqa: E402 -from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL, setup_mongodb # noqa: E402 +from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL, start_mongodb # noqa: E402 +from monkey_island.setup.config_setup import setup_data_dir # noqa: E402 from monkey_island.setup.island_config_options import IslandConfigOptions # noqa: E402 logger = logging.getLogger(__name__) -def setup_island(setup_only: bool, config_options: IslandConfigOptions, server_config_path: str): +def run_monkey_island(): + island_args = parse_cli_args() + config_options, server_config_path = setup_data_dir(island_args) + + _configure_logging(config_options) + _initialize_global_resources(config_options, server_config_path) + + start_mongodb(config_options) + bootloader_server_thread = _start_bootloader_server(MONGO_URL) + _start_island_server(island_args.setup_only, config_options) + bootloader_server_thread.join() + + +def _configure_logging(config_options): + reset_logger() + setup_logging(config_options.data_dir, config_options.log_level) + + +def _initialize_global_resources(config_options: IslandConfigOptions, server_config_path: str): env_singleton.initialize_from_file(server_config_path) initialize_encryptor(config_options.data_dir) initialize_services(config_options.data_dir) + +def _start_bootloader_server(mongo_url) -> Thread: bootloader_server_thread = Thread( - target=BootloaderHttpServer(MONGO_URL).serve_forever, daemon=True + target=BootloaderHttpServer(mongo_url).serve_forever, daemon=True ) bootloader_server_thread.start() - _start_island_server(setup_only, config_options) - bootloader_server_thread.join() + + return bootloader_server_thread def _start_island_server(should_setup_only, config_options: IslandConfigOptions): - - setup_mongodb(config_options) - populate_exporter_list() app = init_app(MONGO_URL) diff --git a/monkey/monkey_island/cc/setup/mongo/mongo_setup.py b/monkey/monkey_island/cc/setup/mongo/mongo_setup.py index ad523ce98..d5001dd1b 100644 --- a/monkey/monkey_island/cc/setup/mongo/mongo_setup.py +++ b/monkey/monkey_island/cc/setup/mongo/mongo_setup.py @@ -18,7 +18,7 @@ MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0" logger = logging.getLogger(__name__) -def setup_mongodb(config_options: IslandConfigOptions): +def start_mongodb(config_options: IslandConfigOptions): if config_options.start_mongodb: MongoDbRunner( db_dir_parent_path=config_options.data_dir, logging_dir_path=config_options.data_dir diff --git a/monkey/monkey_island/setup/config_setup.py b/monkey/monkey_island/setup/config_setup.py index 50330aea3..95ebbc194 100644 --- a/monkey/monkey_island/setup/config_setup.py +++ b/monkey/monkey_island/setup/config_setup.py @@ -1,20 +1,28 @@ import os from typing import Tuple +from monkey_island.cc.arg_parser import IslandCmdArgs from monkey_island.cc.environment import server_config_handler from monkey_island.cc.environment.utils import create_secure_directory from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_SERVER_CONFIG_PATH from monkey_island.setup.island_config_options import IslandConfigOptions -def setup_config_by_cmd_arg(server_config_path) -> Tuple[IslandConfigOptions, str]: +def setup_data_dir(island_args: IslandCmdArgs): + if island_args.server_config_path: + return _setup_config_by_cmd_arg(island_args.server_config_path) + + return _setup_default_config() + + +def _setup_config_by_cmd_arg(server_config_path) -> Tuple[IslandConfigOptions, str]: server_config_path = os.path.expandvars(os.path.expanduser(server_config_path)) config = server_config_handler.load_server_config_from_file(server_config_path) create_secure_directory(config.data_dir, create_parent_dirs=True) return config, server_config_path -def setup_default_config() -> Tuple[IslandConfigOptions, str]: +def _setup_default_config() -> Tuple[IslandConfigOptions, str]: server_config_path = DEFAULT_SERVER_CONFIG_PATH create_secure_directory(DEFAULT_DATA_DIR, create_parent_dirs=False) server_config_handler.create_default_server_config_file() diff --git a/monkey/monkey_island/startup.py b/monkey/monkey_island/startup.py index f53c679ab..90dcd577e 100644 --- a/monkey/monkey_island/startup.py +++ b/monkey/monkey_island/startup.py @@ -1,36 +1,21 @@ # This import patches other imports and needs to be first import monkey_island.setup.gevent_setup # noqa: F401 isort:skip -import json - -from monkey_island.cc.arg_parser import parse_cli_args -from monkey_island.cc.server_setup import setup_island -from monkey_island.cc.server_utils.island_logger import setup_logging -from monkey_island.setup.config_setup import setup_config_by_cmd_arg, setup_default_config +from monkey_island.cc.server_utils.island_logger import setup_default_failsafe_logging def start_island(): - island_args = parse_cli_args() - # This is here in order to catch EVERYTHING, some functions are being called on # imports, so the log init needs to be first. try: - if island_args.server_config_path: - config, server_config_path = setup_config_by_cmd_arg(island_args.server_config_path) - else: - config, server_config_path = setup_default_config() - - setup_logging(config.data_dir, config.log_level) - - except OSError as ex: - print(f"Error opening server config file: {ex}") + setup_default_failsafe_logging() + except Exception as ex: + print(f"Error configuring logging: {ex}") exit(1) - except json.JSONDecodeError as ex: - print(f"Error loading server config: {ex}") - exit(1) + from monkey_island.cc.server_setup import run_monkey_island # noqa: E402 - setup_island(island_args.setup_only, config, server_config_path) + run_monkey_island() if "__main__" == __name__: