diff --git a/monkey/monkey_island/cc/services/config.py b/monkey/monkey_island/cc/services/config.py index 416d85e5d..2dfcd175b 100644 --- a/monkey/monkey_island/cc/services/config.py +++ b/monkey/monkey_island/cc/services/config.py @@ -2,18 +2,15 @@ import collections import copy import functools import logging -from typing import Dict -import dpath.util from jsonschema import Draft4Validator, validators import monkey_island.cc.environment.environment_singleton as env_singleton from monkey_island.cc.database import mongo from monkey_island.cc.server_utils.encryptor import get_encryptor -from monkey_island.cc.services.config_filters import FILTER_PER_MODE +from monkey_island.cc.services.config_manipulator import update_config_per_mode from monkey_island.cc.services.config_schema.config_schema import SCHEMA from monkey_island.cc.services.mode.get_island_mode_service import ModeNotSetError, get_mode -from monkey_island.cc.services.mode.mode_enum import IslandModeEnum from monkey_island.cc.services.post_breach_files import PostBreachFilesService from monkey_island.cc.services.utils.network_utils import local_ip_addresses @@ -248,28 +245,6 @@ class ConfigService: return config - @staticmethod - def update_config_on_mode_set(mode: IslandModeEnum) -> bool: - config = ConfigService.get_config() - return ConfigService.update_config_per_mode(mode, config, True) - - @staticmethod - def update_config_per_mode(mode: IslandModeEnum, config: Dict, should_encrypt: bool) -> bool: - config = ConfigService._set_default_config_values_per_mode(mode, config) - return ConfigService.update_config(config_json=config, should_encrypt=True) - - @staticmethod - def _set_default_config_values_per_mode(mode: IslandModeEnum, config: Dict) -> Dict: - config_filter = FILTER_PER_MODE[mode.value] - config = ConfigService._apply_config_filter(config, config_filter) - return config - - @staticmethod - def _apply_config_filter(config: Dict, config_filter: Dict): - for path, value in config_filter.items(): - dpath.util.set(config, path, value, ".") - return config - @staticmethod def init_config(): if ConfigService.get_config(should_decrypt=False) != {}: @@ -283,7 +258,7 @@ class ConfigService: ConfigService.set_server_ips_in_config(config) try: mode = get_mode() - ConfigService.update_config_per_mode(mode, config, should_encrypt=False) + update_config_per_mode(mode, config, should_encrypt=False) except ModeNotSetError: ConfigService.update_config(config, should_encrypt=False) logger.info("Monkey config reset was called") diff --git a/monkey/monkey_island/cc/services/config_manipulator.py b/monkey/monkey_island/cc/services/config_manipulator.py new file mode 100644 index 000000000..8b92e1767 --- /dev/null +++ b/monkey/monkey_island/cc/services/config_manipulator.py @@ -0,0 +1,29 @@ +from typing import Dict + +import dpath.util + +from monkey_island.cc.services.config import ConfigService +from monkey_island.cc.services.config_filters import FILTER_PER_MODE +from monkey_island.cc.services.mode.mode_enum import IslandModeEnum + + +def update_config_on_mode_set(mode: IslandModeEnum) -> bool: + config = ConfigService.get_config() + return update_config_per_mode(mode, config, True) + + +def update_config_per_mode(mode: IslandModeEnum, config: Dict, should_encrypt: bool) -> bool: + config = _set_default_config_values_per_mode(mode, config) + return ConfigService.update_config(config_json=config, should_encrypt=True) + + +def _set_default_config_values_per_mode(mode: IslandModeEnum, config: Dict) -> Dict: + config_filter = FILTER_PER_MODE[mode.value] + config = _apply_config_filter(config, config_filter) + return config + + +def _apply_config_filter(config: Dict, config_filter: Dict): + for path, value in config_filter.items(): + dpath.util.set(config, path, value, ".") + return config diff --git a/monkey/monkey_island/cc/services/mode/set_island_mode_service.py b/monkey/monkey_island/cc/services/mode/set_island_mode_service.py index 2e9e71691..ef6f5e5b4 100644 --- a/monkey/monkey_island/cc/services/mode/set_island_mode_service.py +++ b/monkey/monkey_island/cc/services/mode/set_island_mode_service.py @@ -1,7 +1,7 @@ import logging from monkey_island.cc.models.island_mode_model import IslandMode -from monkey_island.cc.services.config import ConfigService +from monkey_island.cc.services.config_manipulator import update_config_on_mode_set from monkey_island.cc.services.mode.mode_enum import IslandModeEnum LOG = logging.getLogger(__name__) @@ -11,7 +11,7 @@ def set_mode(mode: IslandModeEnum): island_mode_model = IslandMode() island_mode_model.mode = mode.value island_mode_model.save() - if not ConfigService.update_config_on_mode_set(mode): + if not update_config_on_mode_set(mode): LOG.error( "Could not apply configuration changes per mode. Using default advanced configuration." )