diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index 9367cc04a..3b681bf89 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -1,5 +1,6 @@ from gevent import monkey as gevent_monkey +import monkey_island.cc.environment.environment_singleton as env_singleton # noqa: E402 from monkey_island.cc.arg_parser import parse_cli_args from monkey_island.setup.config_setup import setup_config_by_cmd_arg, setup_default_config @@ -30,6 +31,11 @@ if "__main__" == __name__: print(f"Error loading server config: {ex}") exit(1) + # We need to initialize environment singleton before importing main, + # because main imports modules from monkey_island/cc/models and models need a connection to the + # mongodb. Mongodb connection parameters are initialized in environment singleton. + env_singleton.initialize_from_file(server_config_path) + from monkey_island.cc.main import main # noqa: E402 - main(island_args.setup_only, server_config_path, config) + main(island_args.setup_only, config) diff --git a/monkey/monkey_island/cc/environment/environment_singleton.py b/monkey/monkey_island/cc/environment/environment_singleton.py index e7e316ac5..f1a6a2a39 100644 --- a/monkey/monkey_island/cc/environment/environment_singleton.py +++ b/monkey/monkey_island/cc/environment/environment_singleton.py @@ -2,7 +2,6 @@ import logging import monkey_island.cc.resources.auth.user_store as user_store from monkey_island.cc.environment import EnvironmentConfig, aws, password, standard -from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH __author__ = "itay.mizeretz" @@ -48,8 +47,3 @@ def initialize_from_file(file_path): except Exception: logger.error("Failed initializing environment", exc_info=True) raise - - -# TODO: This is only needed so that unit tests pass. After PR #848 is merged, we may be -# able to remove this line. -initialize_from_file(DEFAULT_SERVER_CONFIG_PATH) diff --git a/monkey/monkey_island/cc/main.py b/monkey/monkey_island/cc/main.py index 21b118447..293dd871b 100644 --- a/monkey/monkey_island/cc/main.py +++ b/monkey/monkey_island/cc/main.py @@ -35,9 +35,7 @@ from monkey_island.cc.services.utils.network_utils import local_ip_addresses # MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0" -def main(setup_only: bool, server_config_path: str, config_options: IslandConfigOptions): - - env_singleton.initialize_from_file(server_config_path) +def main(setup_only: bool, config_options: IslandConfigOptions): initialize_encryptor(config_options.data_dir) initialize_services(config_options.data_dir)