island: Move config manipulator functions from `config.py` to `config_manipulator.py`

This commit is contained in:
Shreya 2021-07-20 15:43:03 +05:30
parent 5159a577e4
commit 9b2ec5c316
3 changed files with 33 additions and 29 deletions

View File

@ -2,18 +2,15 @@ import collections
import copy import copy
import functools import functools
import logging import logging
from typing import Dict
import dpath.util
from jsonschema import Draft4Validator, validators from jsonschema import Draft4Validator, validators
import monkey_island.cc.environment.environment_singleton as env_singleton import monkey_island.cc.environment.environment_singleton as env_singleton
from monkey_island.cc.database import mongo from monkey_island.cc.database import mongo
from monkey_island.cc.server_utils.encryptor import get_encryptor 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.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.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.post_breach_files import PostBreachFilesService
from monkey_island.cc.services.utils.network_utils import local_ip_addresses from monkey_island.cc.services.utils.network_utils import local_ip_addresses
@ -248,28 +245,6 @@ class ConfigService:
return config 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 @staticmethod
def init_config(): def init_config():
if ConfigService.get_config(should_decrypt=False) != {}: if ConfigService.get_config(should_decrypt=False) != {}:
@ -283,7 +258,7 @@ class ConfigService:
ConfigService.set_server_ips_in_config(config) ConfigService.set_server_ips_in_config(config)
try: try:
mode = get_mode() mode = get_mode()
ConfigService.update_config_per_mode(mode, config, should_encrypt=False) update_config_per_mode(mode, config, should_encrypt=False)
except ModeNotSetError: except ModeNotSetError:
ConfigService.update_config(config, should_encrypt=False) ConfigService.update_config(config, should_encrypt=False)
logger.info("Monkey config reset was called") logger.info("Monkey config reset was called")

View File

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

View File

@ -1,7 +1,7 @@
import logging import logging
from monkey_island.cc.models.island_mode_model import IslandMode 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 from monkey_island.cc.services.mode.mode_enum import IslandModeEnum
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -11,7 +11,7 @@ def set_mode(mode: IslandModeEnum):
island_mode_model = IslandMode() island_mode_model = IslandMode()
island_mode_model.mode = mode.value island_mode_model.mode = mode.value
island_mode_model.save() island_mode_model.save()
if not ConfigService.update_config_on_mode_set(mode): if not update_config_on_mode_set(mode):
LOG.error( LOG.error(
"Could not apply configuration changes per mode. Using default advanced configuration." "Could not apply configuration changes per mode. Using default advanced configuration."
) )