island: Trap OSError and JSONDecodeError when reading config

This commit is contained in:
Mike Salvatore 2021-06-02 19:44:31 -04:00
parent 4a1653ed5f
commit edbb2c39f3
2 changed files with 17 additions and 3 deletions

View File

@ -1,8 +1,10 @@
import json
import logging
import os
import sys
from pathlib import Path
from threading import Thread
from typing import Tuple
from gevent.pywsgi import WSGIServer
@ -13,8 +15,10 @@ if str(MONKEY_ISLAND_DIR_BASE_PATH) not in sys.path:
sys.path.insert(0, MONKEY_ISLAND_DIR_BASE_PATH)
import monkey_island.cc.environment.environment_singleton as env_singleton # noqa: E402
import monkey_island.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
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
@ -31,7 +35,6 @@ from monkey_island.cc.setup.mongo.mongo_setup import ( # noqa: E402
register_mongo_shutdown_callback,
start_mongodb,
)
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__)
@ -39,7 +42,7 @@ 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, server_config_path = _setup_data_dir(island_args)
_configure_logging(config_options)
_initialize_global_resources(config_options, server_config_path)
@ -53,6 +56,17 @@ def run_monkey_island():
_start_island_server(island_args.setup_only, config_options)
def _setup_data_dir(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, str]:
try:
return config_setup.setup_data_dir(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 _configure_logging(config_options):
reset_logger()
setup_logging(config_options.data_dir, config_options.log_level)

View File

@ -8,7 +8,7 @@ from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_SERVE
from monkey_island.setup.island_config_options import IslandConfigOptions
def setup_data_dir(island_args: IslandCmdArgs):
def setup_data_dir(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, str]:
if island_args.server_config_path:
return _setup_config_by_cmd_arg(island_args.server_config_path)