Island: Fix updating logic for IslandConfigOptions

This commit is contained in:
Mike Salvatore 2022-01-09 10:32:47 -05:00
parent a48c1afefd
commit d467f30d16
3 changed files with 47 additions and 19 deletions

View File

@ -43,11 +43,6 @@ DEFAULT_START_MONGO_DB = True
DEFAULT_CRT_PATH = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", "server.crt")) 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_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" GEVENT_EXCEPTION_LOG = "gevent_exceptions.log"
ISLAND_PORT = 5000 ISLAND_PORT = 5000

View File

@ -2,7 +2,6 @@ from __future__ import annotations
from common.utils.file_utils import expand_path from common.utils.file_utils import expand_path
from monkey_island.cc.server_utils.consts import ( from monkey_island.cc.server_utils.consts import (
DEFAULT_CERTIFICATE_PATHS,
DEFAULT_CRT_PATH, DEFAULT_CRT_PATH,
DEFAULT_DATA_DIR, DEFAULT_DATA_DIR,
DEFAULT_KEY_PATH, DEFAULT_KEY_PATH,
@ -21,21 +20,33 @@ _LOG_LEVEL = "log_level"
class IslandConfigOptions: class IslandConfigOptions:
def __init__(self, config_contents: dict = None): def __init__(self, config_contents: dict = None):
if not config_contents: if config_contents is None:
config_contents = {} 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( self.start_mongodb = config_contents.get(
_MONGODB, {_START_MONGODB: DEFAULT_START_MONGO_DB} _MONGODB, {_START_MONGODB: self.start_mongodb}
).get(_START_MONGODB, DEFAULT_START_MONGO_DB) ).get(_START_MONGODB, self.start_mongodb)
self.crt_path = config_contents.get(_SSL_CERT, DEFAULT_CERTIFICATE_PATHS).get( self.crt_path = config_contents.get(_SSL_CERT, {_SSL_CERT_FILE: self.crt_path}).get(
_SSL_CERT_FILE, DEFAULT_CRT_PATH _SSL_CERT_FILE, self.crt_path
) )
self.key_path = config_contents.get(_SSL_CERT, DEFAULT_CERTIFICATE_PATHS).get( self.key_path = config_contents.get(_SSL_CERT, {_SSL_CERT_KEY: self.key_path}).get(
_SSL_CERT_KEY, DEFAULT_KEY_PATH _SSL_CERT_KEY, self.key_path
) )
self._expand_paths() self._expand_paths()
@ -44,7 +55,3 @@ class IslandConfigOptions:
self.data_dir = expand_path(str(self.data_dir)) self.data_dir = expand_path(str(self.data_dir))
self.crt_path = expand_path(str(self.crt_path)) self.crt_path = expand_path(str(self.crt_path))
self.key_path = expand_path(str(self.key_path)) self.key_path = expand_path(str(self.key_path))
def update(self, target: dict):
self.__dict__.update(target)
self._expand_paths()

View File

@ -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): def assert_island_config_option_equals(config_file_contents, option_name, expected_value):
options = IslandConfigOptions(config_file_contents) options = IslandConfigOptions(config_file_contents)
assert getattr(options, option_name) == expected_value 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