From fb3e66f75e069a3e26620ef10b0c71408ee34874 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 11 May 2021 14:28:30 -0400 Subject: [PATCH] island: Add "environment" section to server_config.json Put the environment config inside its own "environment" object in the server_config.json to provide a logical separation between the environment config, logger config, data directory, etc. --- .../cc/environment/environment_config.py | 9 +++-- .../cc/server_config.json.develop | 7 ++-- .../cc/environment/test_environment_config.py | 36 +++++++++++++------ .../server_config_no_credentials.json | 6 ++-- .../server_config_partial_credentials.json | 8 +++-- .../server_config_standard_env.json | 6 ++-- ...rver_config_standard_with_credentials.json | 3 ++ .../server_config_with_credentials.json | 2 ++ .../server_config_with_data_dir.json | 6 ++-- .../server_config_with_data_dir_home.json | 6 ++-- 10 files changed, 64 insertions(+), 25 deletions(-) diff --git a/monkey/monkey_island/cc/environment/environment_config.py b/monkey/monkey_island/cc/environment/environment_config.py index 1f9602d22..d2974e058 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -35,7 +35,7 @@ class EnvironmentConfig: def _load_from_json(self, config_json: str) -> EnvironmentConfig: data = json.loads(config_json) - self._load_from_dict(data) + self._load_from_dict(data["environment"]) def _load_from_dict(self, dict_data: Dict): aws = dict_data["aws"] if "aws" in dict_data else None @@ -52,8 +52,13 @@ class EnvironmentConfig: return os.path.abspath(os.path.expanduser(os.path.expandvars(self.data_dir))) def save_to_file(self): + with open(self._server_config_path, "r") as f: + config = json.load(f) + + config["environment"] = self.to_dict() + with open(self._server_config_path, "w") as f: - f.write(json.dumps(self.to_dict(), indent=2)) + f.write(json.dumps(config, indent=2)) def to_dict(self) -> Dict: config_dict = { diff --git a/monkey/monkey_island/cc/server_config.json.develop b/monkey/monkey_island/cc/server_config.json.develop index 33fb33487..f317c7925 100644 --- a/monkey/monkey_island/cc/server_config.json.develop +++ b/monkey/monkey_island/cc/server_config.json.develop @@ -1,5 +1,8 @@ { - "server_config": "password", "deployment": "develop", - "log_level": "DEBUG" + "log_level": "DEBUG", + "environment": { + "server_config": "password", + "deployment": "develop" + } } diff --git a/monkey/tests/monkey_island/cc/environment/test_environment_config.py b/monkey/tests/monkey_island/cc/environment/test_environment_config.py index 6f9170f2f..a5efe40f9 100644 --- a/monkey/tests/monkey_island/cc/environment/test_environment_config.py +++ b/monkey/tests/monkey_island/cc/environment/test_environment_config.py @@ -54,13 +54,29 @@ def test_save_to_file(config_file, standard_with_credentials): with open(config_file, "r") as f: from_file = json.load(f) - 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 + assert len(from_file.keys()) == 2 + assert len(from_file["environment"].keys()) == 6 + assert from_file["environment"]["server_config"] == "standard" + assert from_file["environment"]["deployment"] == "develop" + assert from_file["environment"]["user"] == "test" + assert from_file["environment"]["password_hash"] == "abcdef" + assert from_file["environment"]["aws"] == "test_aws" + assert from_file["environment"]["data_dir"] == DEFAULT_DATA_DIR + + +def test_save_to_file_preserve_log_level(config_file, standard_with_credentials): + shutil.copyfile(standard_with_credentials, config_file) + + environment_config = EnvironmentConfig(config_file) + environment_config.aws = "test_aws" + environment_config.save_to_file() + + with open(config_file, "r") as f: + from_file = json.load(f) + + assert len(from_file.keys()) == 2 + assert "log_level" in from_file + assert from_file["log_level"] == "NOTICE" def test_add_user(config_file, standard_with_credentials): @@ -76,9 +92,9 @@ def test_add_user(config_file, standard_with_credentials): with open(config_file, "r") as f: from_file = json.load(f) - assert len(from_file.keys()) == 5 - assert from_file["user"] == new_user - assert from_file["password_hash"] == new_password_hash + assert len(from_file["environment"].keys()) == 5 + assert from_file["environment"]["user"] == new_user + assert from_file["environment"]["password_hash"] == new_password_hash def test_get_users(standard_with_credentials): diff --git a/monkey/tests/resources/environment/server_config_no_credentials.json b/monkey/tests/resources/environment/server_config_no_credentials.json index ecc4c1708..0b7de96ef 100644 --- a/monkey/tests/resources/environment/server_config_no_credentials.json +++ b/monkey/tests/resources/environment/server_config_no_credentials.json @@ -1,4 +1,6 @@ { - "server_config": "password", - "deployment": "develop" + "environment" : { + "server_config": "password", + "deployment": "develop" + } } diff --git a/monkey/tests/resources/environment/server_config_partial_credentials.json b/monkey/tests/resources/environment/server_config_partial_credentials.json index a9e283924..6158c4f30 100644 --- a/monkey/tests/resources/environment/server_config_partial_credentials.json +++ b/monkey/tests/resources/environment/server_config_partial_credentials.json @@ -1,5 +1,7 @@ { - "server_config": "password", - "deployment": "develop", - "user": "test" + "environment" : { + "server_config": "password", + "deployment": "develop", + "user": "test" + } } diff --git a/monkey/tests/resources/environment/server_config_standard_env.json b/monkey/tests/resources/environment/server_config_standard_env.json index 420f1b303..3d5e0b8a0 100644 --- a/monkey/tests/resources/environment/server_config_standard_env.json +++ b/monkey/tests/resources/environment/server_config_standard_env.json @@ -1,4 +1,6 @@ { - "server_config": "standard", - "deployment": "develop" + "environment" : { + "server_config": "standard", + "deployment": "develop" + } } diff --git a/monkey/tests/resources/environment/server_config_standard_with_credentials.json b/monkey/tests/resources/environment/server_config_standard_with_credentials.json index 4bff379e8..b8cdf5258 100644 --- a/monkey/tests/resources/environment/server_config_standard_with_credentials.json +++ b/monkey/tests/resources/environment/server_config_standard_with_credentials.json @@ -1,6 +1,9 @@ { + "log_level": "NOTICE", + "environment" : { "server_config": "standard", "deployment": "develop", "user": "test", "password_hash": "abcdef" + } } diff --git a/monkey/tests/resources/environment/server_config_with_credentials.json b/monkey/tests/resources/environment/server_config_with_credentials.json index 54c0fa787..73cd6bbc3 100644 --- a/monkey/tests/resources/environment/server_config_with_credentials.json +++ b/monkey/tests/resources/environment/server_config_with_credentials.json @@ -1,6 +1,8 @@ { + "environment" : { "server_config": "password", "deployment": "develop", "user": "test", "password_hash": "abcdef" + } } diff --git a/monkey/tests/resources/environment/server_config_with_data_dir.json b/monkey/tests/resources/environment/server_config_with_data_dir.json index b9d6845f3..8a9b7d75e 100644 --- a/monkey/tests/resources/environment/server_config_with_data_dir.json +++ b/monkey/tests/resources/environment/server_config_with_data_dir.json @@ -1,7 +1,9 @@ { + "data_dir": "/test/data/dir", + "environment" : { "server_config": "password", "deployment": "develop", "user": "test", - "password_hash": "abcdef", - "data_dir": "/test/data/dir" + "password_hash": "abcdef" + } } diff --git a/monkey/tests/resources/environment/server_config_with_data_dir_home.json b/monkey/tests/resources/environment/server_config_with_data_dir_home.json index e6e4a0a1f..88f44e164 100644 --- a/monkey/tests/resources/environment/server_config_with_data_dir_home.json +++ b/monkey/tests/resources/environment/server_config_with_data_dir_home.json @@ -1,7 +1,9 @@ { + "data_dir": "~/data_dir", + "environment" : { "server_config": "password", "deployment": "develop", "user": "test", - "password_hash": "abcdef", - "data_dir": "~/data_dir" + "password_hash": "abcdef" + } }