From 438a2701d4768333e22c6772375bc44ac0aa4546 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 11 Feb 2021 13:48:22 -0500 Subject: [PATCH] cc: add `data_dir` property to EnvironmentConfig --- .../cc/environment/environment_config.py | 25 +++++++++++++------ .../cc/environment/environment_singleton.py | 3 +++ .../cc/environment/test_environment_config.py | 23 ++++++++++++----- .../monkey_island/cc/server_utils/consts.py | 2 ++ .../cc/server_utils/island_logger.py | 2 +- .../{ => server_utils}/test_island_logger.py | 4 +-- .../environment/server_config_data_dir.json | 7 ++++++ 7 files changed, 49 insertions(+), 17 deletions(-) rename monkey/monkey_island/cc/{ => server_utils}/test_island_logger.py (83%) create mode 100644 monkey/monkey_island/cc/testing/environment/server_config_data_dir.json diff --git a/monkey/monkey_island/cc/environment/environment_config.py b/monkey/monkey_island/cc/environment/environment_config.py index aec1a777f..3bc9327b4 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -6,6 +6,7 @@ from pathlib import Path from typing import Dict, List import monkey_island.cc.environment.server_config_generator as server_config_generator +from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR from monkey_island.cc.environment.user_creds import UserCreds from monkey_island.cc.resources.auth.auth_user import User from monkey_island.cc.resources.auth.user_store import UserStore @@ -18,6 +19,7 @@ class EnvironmentConfig: self.deployment = None self.user_creds = None self.aws = None + self.data_dir = None self._load_from_file(self._server_config_path) @@ -26,7 +28,7 @@ class EnvironmentConfig: if not Path(file_path).is_file(): server_config_generator.create_default_config_file(file_path) - with open(file_path, 'r') as f: + with open(file_path, "r") as f: config_content = f.read() self._load_from_json(config_content) @@ -37,22 +39,29 @@ class EnvironmentConfig: def _load_from_dict(self, dict_data: Dict): user_creds = UserCreds.get_from_dict(dict_data) - aws = dict_data['aws'] if 'aws' in dict_data else None + aws = dict_data["aws"] if "aws" in dict_data else None + data_dir = ( + dict_data["data_dir"] if "data_dir" in dict_data else DEFAULT_DATA_DIR + ) - self.server_config = dict_data['server_config'] - self.deployment = dict_data['deployment'] + self.server_config = dict_data["server_config"] + self.deployment = dict_data["deployment"] self.user_creds = user_creds self.aws = aws + self.data_dir = data_dir def save_to_file(self): - with open(self._server_config_path, 'w') as f: + with open(self._server_config_path, "w") as f: f.write(json.dumps(self.to_dict(), indent=2)) def to_dict(self) -> Dict: - config_dict = {'server_config': self.server_config, - 'deployment': self.deployment} + config_dict = { + "server_config": self.server_config, + "deployment": self.deployment, + "data_dir": self.data_dir, + } if self.aws: - config_dict.update({'aws': self.aws}) + config_dict.update({"aws": self.aws}) config_dict.update(self.user_creds.to_dict()) return config_dict diff --git a/monkey/monkey_island/cc/environment/environment_singleton.py b/monkey/monkey_island/cc/environment/environment_singleton.py index 01e83096d..aa2dc32d6 100644 --- a/monkey/monkey_island/cc/environment/environment_singleton.py +++ b/monkey/monkey_island/cc/environment/environment_singleton.py @@ -20,6 +20,7 @@ ENV_DICT = { } env = None +config = None def set_env(env_type: str, env_config: EnvironmentConfig): @@ -39,6 +40,8 @@ def set_to_standard(): def initialize_from_file(file_path): + global config + try: config = EnvironmentConfig(file_path) diff --git a/monkey/monkey_island/cc/environment/test_environment_config.py b/monkey/monkey_island/cc/environment/test_environment_config.py index b06e26301..a8e8a1beb 100644 --- a/monkey/monkey_island/cc/environment/test_environment_config.py +++ b/monkey/monkey_island/cc/environment/test_environment_config.py @@ -4,7 +4,7 @@ import shutil import pytest -from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH +from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, MONKEY_ISLAND_ABS_PATH from monkey_island.cc.environment.environment_config import EnvironmentConfig from monkey_island.cc.environment.user_creds import UserCreds @@ -23,6 +23,7 @@ PARTIAL_CREDENTIALS = os.path.join( STANDARD_WITH_CREDENTIALS = os.path.join( TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json" ) +DATA_DIR = os.path.join(TEST_RESOURCES_DIR, "server_config_data_dir.json") @pytest.fixture @@ -33,28 +34,31 @@ def config_file(tmpdir): def test_get_with_credentials(): config_dict = EnvironmentConfig(WITH_CREDENTIALS).to_dict() - assert len(config_dict.keys()) == 4 + assert len(config_dict.keys()) == 5 assert config_dict["server_config"] == "password" assert config_dict["deployment"] == "develop" assert config_dict["user"] == "test" assert config_dict["password_hash"] == "abcdef" + assert config_dict["data_dir"] == DEFAULT_DATA_DIR def test_get_with_no_credentials(): config_dict = EnvironmentConfig(NO_CREDENTIALS).to_dict() - assert len(config_dict.keys()) == 2 + assert len(config_dict.keys()) == 3 assert config_dict["server_config"] == "password" assert config_dict["deployment"] == "develop" + assert config_dict["data_dir"] == DEFAULT_DATA_DIR def test_get_with_partial_credentials(): config_dict = EnvironmentConfig(PARTIAL_CREDENTIALS).to_dict() - assert len(config_dict.keys()) == 3 + assert len(config_dict.keys()) == 4 assert config_dict["server_config"] == "password" assert config_dict["deployment"] == "develop" assert config_dict["user"] == "test" + assert config_dict["data_dir"] == DEFAULT_DATA_DIR def test_save_to_file(config_file): @@ -67,12 +71,13 @@ def test_save_to_file(config_file): with open(config_file, "r") as f: from_file = json.load(f) - assert len(from_file.keys()) == 5 + assert len(from_file.keys()) == 6 assert from_file["server_config"] == "standard" assert from_file["deployment"] == "develop" assert from_file["user"] == "test" assert from_file["password_hash"] == "abcdef" assert from_file["aws"] == "test_aws" + assert from_file["data_dir"] == DEFAULT_DATA_DIR def test_add_user(config_file): @@ -88,7 +93,7 @@ def test_add_user(config_file): with open(config_file, "r") as f: from_file = json.load(f) - assert len(from_file.keys()) == 4 + assert len(from_file.keys()) == 5 assert from_file["user"] == new_user assert from_file["password_hash"] == new_password_hash @@ -113,3 +118,9 @@ def test_generate_default_file(config_file): assert environment_config.user_creds.username == "" assert environment_config.user_creds.password_hash == "" assert environment_config.aws is None + assert environment_config.data_dir == DEFAULT_DATA_DIR + + +def test_data_dir(): + environment_config = EnvironmentConfig(DATA_DIR) + assert environment_config.data_dir == "/test/data/dir" diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index c12a42731..5a0e69581 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -16,3 +16,5 @@ DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( DEFAULT_LOGGER_CONFIG_PATH = os.path.join( MONKEY_ISLAND_ABS_PATH, "cc", "island_logger_default_config.json" ) + +DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc") diff --git a/monkey/monkey_island/cc/server_utils/island_logger.py b/monkey/monkey_island/cc/server_utils/island_logger.py index 224e85364..1efbb7734 100644 --- a/monkey/monkey_island/cc/server_utils/island_logger.py +++ b/monkey/monkey_island/cc/server_utils/island_logger.py @@ -3,7 +3,7 @@ import logging.config import os from typing import Dict -from monkey_island.cc.consts import DEFAULT_LOGGER_CONFIG_PATH +from monkey_island.cc.server_utils.consts import DEFAULT_LOGGER_CONFIG_PATH __author__ = "Maor.Rayzin" diff --git a/monkey/monkey_island/cc/test_island_logger.py b/monkey/monkey_island/cc/server_utils/test_island_logger.py similarity index 83% rename from monkey/monkey_island/cc/test_island_logger.py rename to monkey/monkey_island/cc/server_utils/test_island_logger.py index a1cc27aa7..af58f4b75 100644 --- a/monkey/monkey_island/cc/test_island_logger.py +++ b/monkey/monkey_island/cc/server_utils/test_island_logger.py @@ -3,8 +3,8 @@ import os import pytest -from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH -from monkey_island.cc.island_logger import json_setup_logging +from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH +from monkey_island.cc.server_utils.island_logger import json_setup_logging TEST_LOGGER_CONFIG_PATH = os.path.join( MONKEY_ISLAND_ABS_PATH, "cc", "testing", "logger_config.json" diff --git a/monkey/monkey_island/cc/testing/environment/server_config_data_dir.json b/monkey/monkey_island/cc/testing/environment/server_config_data_dir.json new file mode 100644 index 000000000..b9d6845f3 --- /dev/null +++ b/monkey/monkey_island/cc/testing/environment/server_config_data_dir.json @@ -0,0 +1,7 @@ +{ + "server_config": "password", + "deployment": "develop", + "user": "test", + "password_hash": "abcdef", + "data_dir": "/test/data/dir" +}