From 34ff8dabf7614b1d0cf3de38eb4dc77b2f73f623 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Sun, 9 Jan 2022 10:32:47 -0500 Subject: [PATCH] Island: Fix updating logic for IslandConfigOptions --- .../monkey_island/cc/server_utils/consts.py | 5 --- .../cc/setup/island_config_options.py | 35 +++++++++++-------- .../cc/setup/test_island_config_options.py | 26 ++++++++++++++ 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index 29fe78933..5da759efe 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -43,11 +43,6 @@ DEFAULT_START_MONGO_DB = True DEFAULT_CRT_PATH = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", "server.crt")) DEFAULT_KEY_PATH = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", "server.key")) -DEFAULT_CERTIFICATE_PATHS = { - "ssl_certificate_file": DEFAULT_CRT_PATH, - "ssl_certificate_key_file": DEFAULT_KEY_PATH, -} - GEVENT_EXCEPTION_LOG = "gevent_exceptions.log" ISLAND_PORT = 5000 diff --git a/monkey/monkey_island/cc/setup/island_config_options.py b/monkey/monkey_island/cc/setup/island_config_options.py index 27df897e8..763474c83 100644 --- a/monkey/monkey_island/cc/setup/island_config_options.py +++ b/monkey/monkey_island/cc/setup/island_config_options.py @@ -2,7 +2,6 @@ from __future__ import annotations from common.utils.file_utils import expand_path from monkey_island.cc.server_utils.consts import ( - DEFAULT_CERTIFICATE_PATHS, DEFAULT_CRT_PATH, DEFAULT_DATA_DIR, DEFAULT_KEY_PATH, @@ -21,21 +20,33 @@ _LOG_LEVEL = "log_level" class IslandConfigOptions: def __init__(self, config_contents: dict = None): - if not config_contents: + if config_contents is None: config_contents = {} - self.data_dir = config_contents.get(_DATA_DIR, DEFAULT_DATA_DIR) - self.log_level = config_contents.get(_LOG_LEVEL, DEFAULT_LOG_LEVEL) + self.data_dir = DEFAULT_DATA_DIR + self.log_level = DEFAULT_LOG_LEVEL + self.start_mongodb = DEFAULT_START_MONGO_DB + self.crt_path = DEFAULT_CRT_PATH + self.key_path = DEFAULT_KEY_PATH + + self._expand_paths() + + self.update(config_contents) + + def update(self, config_contents: dict): + self.data_dir = config_contents.get(_DATA_DIR, self.data_dir) + + self.log_level = config_contents.get(_LOG_LEVEL, self.log_level) self.start_mongodb = config_contents.get( - _MONGODB, {_START_MONGODB: DEFAULT_START_MONGO_DB} - ).get(_START_MONGODB, DEFAULT_START_MONGO_DB) + _MONGODB, {_START_MONGODB: self.start_mongodb} + ).get(_START_MONGODB, self.start_mongodb) - self.crt_path = config_contents.get(_SSL_CERT, DEFAULT_CERTIFICATE_PATHS).get( - _SSL_CERT_FILE, DEFAULT_CRT_PATH + self.crt_path = config_contents.get(_SSL_CERT, {_SSL_CERT_FILE: self.crt_path}).get( + _SSL_CERT_FILE, self.crt_path ) - self.key_path = config_contents.get(_SSL_CERT, DEFAULT_CERTIFICATE_PATHS).get( - _SSL_CERT_KEY, DEFAULT_KEY_PATH + self.key_path = config_contents.get(_SSL_CERT, {_SSL_CERT_KEY: self.key_path}).get( + _SSL_CERT_KEY, self.key_path ) self._expand_paths() @@ -44,7 +55,3 @@ class IslandConfigOptions: self.data_dir = expand_path(str(self.data_dir)) self.crt_path = expand_path(str(self.crt_path)) self.key_path = expand_path(str(self.key_path)) - - def update(self, target: dict): - self.__dict__.update(target) - self._expand_paths() diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py index c9964af7e..2e8609fd1 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py @@ -144,3 +144,29 @@ def assert_ssl_certificate_key_file_equals(config_file_contents, expected_ssl_ce def assert_island_config_option_equals(config_file_contents, option_name, expected_value): options = IslandConfigOptions(config_file_contents) assert getattr(options, option_name) == expected_value + + +def test_start_mongo_overridden(patched_home_env): + config = IslandConfigOptions() + assert config.start_mongodb + + config.update({"mongodb": {"start_mongodb": False}}) + assert not config.start_mongodb + + +def test_crt_path_overridden(patched_home_env): + expected_path = Path("/fake/file.crt") + config = IslandConfigOptions() + assert config.crt_path != expected_path + + config.update({"ssl_certificate": {"ssl_certificate_file": str(expected_path)}}) + assert config.crt_path == expected_path + + +def test_key_path_overridden(patched_home_env): + expected_path = Path("/fake/file.key") + config = IslandConfigOptions() + assert config.key_path != expected_path + + config.update({"ssl_certificate": {"ssl_certificate_key_file": str(expected_path)}}) + assert config.key_path == expected_path