Agent: improve the readability of island_config_options.py

This commit is contained in:
VakarisZ 2021-12-01 14:35:37 +02:00
parent 8304a4ea19
commit 6e7ddbc6c7
1 changed files with 13 additions and 25 deletions

View File

@ -1,7 +1,5 @@
from __future__ import annotations
from dpath import util
from common.utils.file_utils import expand_path
from monkey_island.cc.server_utils.consts import (
DEFAULT_CERTIFICATE_PATHS,
@ -25,7 +23,7 @@ class IslandConfigOptions:
def __init__(self, config_contents: dict = None):
if not config_contents:
config_contents = {}
self.data_dir = expand_path(config_contents.get(_DATA_DIR, DEFAULT_DATA_DIR))
self.data_dir = config_contents.get(_DATA_DIR, DEFAULT_DATA_DIR)
self.log_level = config_contents.get(_LOG_LEVEL, DEFAULT_LOG_LEVEL)
@ -33,30 +31,20 @@ class IslandConfigOptions:
_MONGODB, {_START_MONGODB: DEFAULT_START_MONGO_DB}
).get(_START_MONGODB, DEFAULT_START_MONGO_DB)
self.crt_path = expand_path(
config_contents.get(_SSL_CERT, DEFAULT_CERTIFICATE_PATHS).get(
_SSL_CERT_FILE, DEFAULT_CRT_PATH
)
self.crt_path = config_contents.get(_SSL_CERT, DEFAULT_CERTIFICATE_PATHS).get(
_SSL_CERT_FILE, DEFAULT_CRT_PATH
)
self.key_path = expand_path(
config_contents.get(_SSL_CERT, DEFAULT_CERTIFICATE_PATHS).get(
_SSL_CERT_KEY, DEFAULT_KEY_PATH
)
self.key_path = config_contents.get(_SSL_CERT, DEFAULT_CERTIFICATE_PATHS).get(
_SSL_CERT_KEY, DEFAULT_KEY_PATH
)
self._expand_paths()
def _expand_paths(self):
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):
target = self._expand_config_paths(target)
self.__dict__.update(target)
@staticmethod
def _expand_config_paths(config: dict) -> dict:
config_paths = [_DATA_DIR, f"{_SSL_CERT}.{_SSL_CERT_FILE}", f"{_SSL_CERT}.{_SSL_CERT_KEY}"]
for config_path in config_paths:
try:
expanded_val = expand_path(util.get(config, config_path, "."))
util.set(config, config_path, expanded_val, ".")
except KeyError:
pass
return config
self._expand_paths()