diff --git a/build_scripts/appimage/server_config.json.standard b/build_scripts/appimage/server_config.json.standard index af975a9e0..889654ea2 100644 --- a/build_scripts/appimage/server_config.json.standard +++ b/build_scripts/appimage/server_config.json.standard @@ -2,8 +2,7 @@ "data_dir": "~/.monkey_island", "log_level": "DEBUG", "environment": { - "server_config": "password", - "deployment": "standard" + "server_config": "password" }, "mongodb": { "start_mongodb": true diff --git a/build_scripts/docker/server_config.json b/build_scripts/docker/server_config.json index 77e5d9855..ea464f181 100644 --- a/build_scripts/docker/server_config.json +++ b/build_scripts/docker/server_config.json @@ -2,8 +2,7 @@ "data_dir": "/monkey_island_data", "log_level": "DEBUG", "environment": { - "server_config": "password", - "deployment": "docker" + "server_config": "password" }, "mongodb": { "start_mongodb": false diff --git a/monkey/common/version.py b/monkey/common/version.py index 85a263be0..3582caa72 100644 --- a/monkey/common/version.py +++ b/monkey/common/version.py @@ -6,6 +6,7 @@ from pathlib import Path MAJOR = "1" MINOR = "11" PATCH = "0" + build_file_path = Path(__file__).parent.joinpath("BUILD") with open(build_file_path, "r") as build_file: BUILD = build_file.read() diff --git a/monkey/monkey_island/cc/deployment.json b/monkey/monkey_island/cc/deployment.json new file mode 100644 index 000000000..9a9f1c78c --- /dev/null +++ b/monkey/monkey_island/cc/deployment.json @@ -0,0 +1,3 @@ +{ + "deployment": "develop" +} diff --git a/monkey/monkey_island/cc/environment/__init__.py b/monkey/monkey_island/cc/environment/__init__.py index 2c43eb9be..281b08a3a 100644 --- a/monkey/monkey_island/cc/environment/__init__.py +++ b/monkey/monkey_island/cc/environment/__init__.py @@ -88,9 +88,3 @@ class Environment(object, metaclass=ABCMeta): def get_auth_expiration_time(self): return self._AUTH_EXPIRATION_TIME - - def get_deployment(self) -> str: - deployment = "unknown" - if self._config and self._config.deployment: - deployment = self._config.deployment - return deployment diff --git a/monkey/monkey_island/cc/environment/environment_config.py b/monkey/monkey_island/cc/environment/environment_config.py index b013bdcf3..804b7896f 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -13,7 +13,6 @@ class EnvironmentConfig: def __init__(self, file_path): self._server_config_path = os.path.expanduser(file_path) self.server_config = None - self.deployment = None self.user_creds = None self.aws = None @@ -35,7 +34,6 @@ class EnvironmentConfig: aws = dict_data["aws"] if "aws" in dict_data else None self.server_config = dict_data["server_config"] - self.deployment = dict_data["deployment"] self.user_creds = _get_user_credentials_from_config(dict_data) self.aws = aws @@ -51,7 +49,6 @@ class EnvironmentConfig: def to_dict(self) -> Dict: config_dict = { "server_config": self.server_config, - "deployment": self.deployment, } if self.aws: config_dict.update({"aws": self.aws}) diff --git a/monkey/monkey_island/cc/server_config.json b/monkey/monkey_island/cc/server_config.json index 5d8dc85aa..7ae515179 100644 --- a/monkey/monkey_island/cc/server_config.json +++ b/monkey/monkey_island/cc/server_config.json @@ -1,8 +1,7 @@ { "log_level": "DEBUG", "environment": { - "server_config": "password", - "deployment": "develop" + "server_config": "password" }, "mongodb": { "start_mongodb": true diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index c42f2d694..b9255232e 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -1,10 +1,14 @@ +import json import logging +from pathlib import Path import requests -import monkey_island.cc.environment.environment_singleton as env_singleton from common.utils.exceptions import VersionServerConnectionError from common.version import get_version +from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH + +DEPLOYMENT_FILE_PATH = Path(MONKEY_ISLAND_ABS_PATH) / "cc" / "deployment.json" logger = logging.getLogger(__name__) @@ -39,8 +43,9 @@ class VersionUpdateService: Checks if newer monkey version is available :return: False if not, version in string format ('1.6.2') otherwise """ + url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % ( - env_singleton.env.get_deployment(), + VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH), get_version(), ) @@ -61,6 +66,25 @@ class VersionUpdateService: @staticmethod def get_download_link(): return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % ( - env_singleton.env.get_deployment(), + VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH), get_version(), ) + + @staticmethod + def get_deployment_from_file(file_path: Path) -> str: + deployment = "unknown" + + try: + with open(file_path, "r") as deployment_info_file: + deployment_info = json.load(deployment_info_file) + deployment = deployment_info["deployment"] + except FileNotFoundError as ex: + logger.debug(f"Deployment file {file_path} is not found. Exception: {ex}") + except KeyError as ex: + logger.debug(f"Invalid key in the deployment file. Exception: {ex}") + except json.JSONDecodeError as ex: + logger.debug(f"Invalid deployment info file. Exception: {ex}") + except Exception as ex: + logger.debug(f"Couldn't get deployment info from {file_path}. Exception: {ex}.") + + return deployment diff --git a/monkey/tests/data_for_tests/deployment.json b/monkey/tests/data_for_tests/deployment.json new file mode 100644 index 000000000..9a9f1c78c --- /dev/null +++ b/monkey/tests/data_for_tests/deployment.json @@ -0,0 +1,3 @@ +{ + "deployment": "develop" +} diff --git a/monkey/tests/data_for_tests/deployment_flawed b/monkey/tests/data_for_tests/deployment_flawed new file mode 100644 index 000000000..6563189c5 --- /dev/null +++ b/monkey/tests/data_for_tests/deployment_flawed @@ -0,0 +1 @@ +develop diff --git a/monkey/tests/data_for_tests/deployment_key_error.json b/monkey/tests/data_for_tests/deployment_key_error.json new file mode 100644 index 000000000..acba72b2d --- /dev/null +++ b/monkey/tests/data_for_tests/deployment_key_error.json @@ -0,0 +1,3 @@ +{ + "tnemyolped": "develop" +} diff --git a/monkey/tests/data_for_tests/server_configs/server_config_no_credentials.json b/monkey/tests/data_for_tests/server_configs/server_config_no_credentials.json index 31da48aa4..eadc78473 100644 --- a/monkey/tests/data_for_tests/server_configs/server_config_no_credentials.json +++ b/monkey/tests/data_for_tests/server_configs/server_config_no_credentials.json @@ -1,7 +1,6 @@ { "environment" : { - "server_config": "password", - "deployment": "develop" + "server_config": "password" }, "mongodb": { "start_mongodb": true diff --git a/monkey/tests/data_for_tests/server_configs/server_config_partial_credentials.json b/monkey/tests/data_for_tests/server_configs/server_config_partial_credentials.json index e29d514cd..34a7f857c 100644 --- a/monkey/tests/data_for_tests/server_configs/server_config_partial_credentials.json +++ b/monkey/tests/data_for_tests/server_configs/server_config_partial_credentials.json @@ -1,7 +1,6 @@ { "environment" : { "server_config": "password", - "deployment": "develop", "user": "test" }, "mongodb": { diff --git a/monkey/tests/data_for_tests/server_configs/server_config_with_credentials.json b/monkey/tests/data_for_tests/server_configs/server_config_with_credentials.json index 8690ef1c7..7732c240a 100644 --- a/monkey/tests/data_for_tests/server_configs/server_config_with_credentials.json +++ b/monkey/tests/data_for_tests/server_configs/server_config_with_credentials.json @@ -2,7 +2,6 @@ "log_level": "NOTICE", "environment" : { "server_config": "password", - "deployment": "develop", "user": "test", "password_hash": "abcdef" }, diff --git a/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment_config.py b/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment_config.py index 52f0d96ca..63ae123bf 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment_config.py +++ b/monkey/tests/unit_tests/monkey_island/cc/environment/test_environment_config.py @@ -16,9 +16,8 @@ def config_file(tmpdir): def test_get_with_credentials(with_credentials): config_dict = EnvironmentConfig(with_credentials).to_dict() - assert len(config_dict.keys()) == 4 + assert len(config_dict.keys()) == 3 assert config_dict["server_config"] == "password" - assert config_dict["deployment"] == "develop" assert config_dict["user"] == "test" assert config_dict["password_hash"] == "abcdef" @@ -26,17 +25,15 @@ def test_get_with_credentials(with_credentials): def test_get_with_no_credentials(no_credentials): config_dict = EnvironmentConfig(no_credentials).to_dict() - assert len(config_dict.keys()) == 2 + assert len(config_dict.keys()) == 1 assert config_dict["server_config"] == "password" - assert config_dict["deployment"] == "develop" def test_get_with_partial_credentials(partial_credentials): config_dict = EnvironmentConfig(partial_credentials).to_dict() - assert len(config_dict.keys()) == 3 + assert len(config_dict.keys()) == 2 assert config_dict["server_config"] == "password" - assert config_dict["deployment"] == "develop" assert config_dict["user"] == "test" @@ -80,7 +77,7 @@ def test_add_user(config_file, with_credentials): with open(config_file, "r") as f: from_file = json.load(f) - assert len(from_file["environment"].keys()) == 4 + assert len(from_file["environment"].keys()) == 3 assert from_file["environment"]["user"] == new_user assert from_file["environment"]["password_hash"] == new_password_hash diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py new file mode 100644 index 000000000..e7fe7bdf5 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py @@ -0,0 +1,46 @@ +from pathlib import Path + +import pytest + +from monkey_island.cc.services.version_update import VersionUpdateService + + +@pytest.fixture +def deployment_info_file_path(data_for_tests_dir): + path = data_for_tests_dir / "deployment.json" + return path + + +@pytest.fixture +def key_error_deployment_info_file_path(data_for_tests_dir): + path = data_for_tests_dir / "deployment_key_error.json" + return path + + +@pytest.fixture +def flawed_deployment_info_file_path(data_for_tests_dir): + path = data_for_tests_dir / "deployment_flawed" + return path + + +def test_get_deployment_field_from_file(deployment_info_file_path): + deployment = VersionUpdateService().get_deployment_from_file(deployment_info_file_path) + assert deployment == "develop" + + +def test_get_deployment_field_from_nonexistent_file(): + ghost_file = Path("ghost_file") + deployment = VersionUpdateService().get_deployment_from_file(ghost_file) + assert deployment == "unknown" + + +def test_get_deployment_field_key_error(key_error_deployment_info_file_path): + deployment = VersionUpdateService().get_deployment_from_file( + key_error_deployment_info_file_path + ) + assert deployment == "unknown" + + +def test_get_deployment_field_from_flawed_json_file(flawed_deployment_info_file_path): + deployment = VersionUpdateService().get_deployment_from_file(flawed_deployment_info_file_path) + assert deployment == "unknown"