Fixed a bug where models were getting imported without mongodb connection and failing

This commit is contained in:
VakarisZ 2021-05-24 18:06:53 +03:00
parent d273e85858
commit 3a800d9a44
3 changed files with 8 additions and 10 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)