From c46c02507f6f03b3c3340fb45ec9cbb7c83a048f Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 10 Sep 2021 15:21:34 +0530 Subject: [PATCH 01/14] build_scripts: Extract deployment field from server configs to separate files for appimage and docker --- build_scripts/appimage/deployment.json | 3 +++ build_scripts/appimage/server_config.json.standard | 3 +-- build_scripts/docker/deployment.json | 3 +++ build_scripts/docker/server_config.json | 3 +-- 4 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 build_scripts/appimage/deployment.json create mode 100644 build_scripts/docker/deployment.json diff --git a/build_scripts/appimage/deployment.json b/build_scripts/appimage/deployment.json new file mode 100644 index 000000000..914c44d4f --- /dev/null +++ b/build_scripts/appimage/deployment.json @@ -0,0 +1,3 @@ +{ + "deployment": "standard" +} 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/deployment.json b/build_scripts/docker/deployment.json new file mode 100644 index 000000000..66125c06f --- /dev/null +++ b/build_scripts/docker/deployment.json @@ -0,0 +1,3 @@ +{ + "deployment": "docker" +} 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 From 2b9b755177ad51704fd8260dba7344b4991fc664 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 10 Sep 2021 16:29:31 +0530 Subject: [PATCH 02/14] island: Extract deployment type and version number into deployment.json --- monkey/monkey_island/cc/deployment.json | 8 ++++++++ monkey/monkey_island/cc/server_config.json | 3 +-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 monkey/monkey_island/cc/deployment.json diff --git a/monkey/monkey_island/cc/deployment.json b/monkey/monkey_island/cc/deployment.json new file mode 100644 index 000000000..474c6e6c0 --- /dev/null +++ b/monkey/monkey_island/cc/deployment.json @@ -0,0 +1,8 @@ +{ + "version": { + "major": 1, + "minor": 11, + "patch": 0 + }, + "deployment": "develop" +} 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 From 2af3878e8193870fa89aba64abd3beb996486485 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 10 Sep 2021 16:36:26 +0530 Subject: [PATCH 03/14] common: Pick up version details from deployment.json in common/version.py --- monkey/common/version.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/monkey/common/version.py b/monkey/common/version.py index 85a263be0..999f173fa 100644 --- a/monkey/common/version.py +++ b/monkey/common/version.py @@ -1,11 +1,19 @@ # To get the version from shell, run `python ./version.py` (see `python ./version.py -h` for # details). import argparse +import json from pathlib import Path -MAJOR = "1" -MINOR = "11" -PATCH = "0" +deployment_info_file_path = Path(__file__).parent.parent.joinpath( + "monkey_island", "cc", "deployment.json" +) +with open(deployment_info_file_path, "r") as deployment_info_file: + deployment_info = json.load(deployment_info_file) + MAJOR = deployment_info["version"]["major"] + MINOR = deployment_info["version"]["minor"] + PATCH = deployment_info["version"]["patch"] + + build_file_path = Path(__file__).parent.joinpath("BUILD") with open(build_file_path, "r") as build_file: BUILD = build_file.read() From a62328dcf6a1cc29598e76fc20cdc3c71529ec15 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 10 Sep 2021 17:31:33 +0530 Subject: [PATCH 04/14] island: Get deployment type from file in env config --- .../cc/environment/environment_config.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/monkey/monkey_island/cc/environment/environment_config.py b/monkey/monkey_island/cc/environment/environment_config.py index b013bdcf3..29bda44d8 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -2,23 +2,34 @@ from __future__ import annotations import json import os -from typing import Dict, List +from typing import Dict, List, Optional 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 +from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH class EnvironmentConfig: def __init__(self, file_path): self._server_config_path = os.path.expanduser(file_path) self.server_config = None - self.deployment = None + self.deployment = self._get_deployment_from_file() self.user_creds = None self.aws = None self._load_from_file(self._server_config_path) + def _get_deployment_from_file(self) -> Optional[str]: + deployment = None + + deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json") + with open(deployment_info_file_path, "r") as deployment_info_file: + deployment_info = json.load(deployment_info_file) + deployment = deployment_info["deployment"] + + return deployment + def _load_from_file(self, file_path): file_path = os.path.expanduser(file_path) From 2b4beb22007da6b1e830bf7cadb1bd60818568ac Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 10 Sep 2021 17:36:57 +0530 Subject: [PATCH 05/14] island: Don't set deployment type from server config in env config --- monkey/monkey_island/cc/environment/environment_config.py | 1 - 1 file changed, 1 deletion(-) diff --git a/monkey/monkey_island/cc/environment/environment_config.py b/monkey/monkey_island/cc/environment/environment_config.py index 29bda44d8..bd1429938 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -46,7 +46,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 From 78ab3f176c9e632ca8c552d8c998d89e417650ba Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 10 Sep 2021 17:41:43 +0530 Subject: [PATCH 06/14] tests: Remove deployment field from unit tests' server configs --- .../server_configs/server_config_no_credentials.json | 3 +-- .../server_configs/server_config_partial_credentials.json | 1 - .../server_configs/server_config_with_credentials.json | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) 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" }, From c1fc56d4ce339db98d2a44860fd1ec786f3e9fd5 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Mon, 13 Sep 2021 18:47:28 +0200 Subject: [PATCH 07/14] Island: Change monkey code to use deployment.json Add UTs for get_deployment. Fix Enviroment UTs. --- monkey/common/version.py | 13 +++--------- monkey/monkey_island/cc/deployment.json | 5 ----- .../monkey_island/cc/environment/__init__.py | 6 ------ .../cc/environment/environment_config.py | 15 +------------- .../cc/services/version_update.py | 20 ++++++++++++++++--- monkey/tests/data_for_tests/deployment.json | 3 +++ .../cc/environment/test_environment_config.py | 11 ++++------ .../cc/services/test_version_update.py | 16 +++++++++++++++ 8 files changed, 44 insertions(+), 45 deletions(-) create mode 100644 monkey/tests/data_for_tests/deployment.json create mode 100644 monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py diff --git a/monkey/common/version.py b/monkey/common/version.py index 999f173fa..3582caa72 100644 --- a/monkey/common/version.py +++ b/monkey/common/version.py @@ -1,18 +1,11 @@ # To get the version from shell, run `python ./version.py` (see `python ./version.py -h` for # details). import argparse -import json from pathlib import Path -deployment_info_file_path = Path(__file__).parent.parent.joinpath( - "monkey_island", "cc", "deployment.json" -) -with open(deployment_info_file_path, "r") as deployment_info_file: - deployment_info = json.load(deployment_info_file) - MAJOR = deployment_info["version"]["major"] - MINOR = deployment_info["version"]["minor"] - PATCH = deployment_info["version"]["patch"] - +MAJOR = "1" +MINOR = "11" +PATCH = "0" build_file_path = Path(__file__).parent.joinpath("BUILD") with open(build_file_path, "r") as build_file: diff --git a/monkey/monkey_island/cc/deployment.json b/monkey/monkey_island/cc/deployment.json index 474c6e6c0..9a9f1c78c 100644 --- a/monkey/monkey_island/cc/deployment.json +++ b/monkey/monkey_island/cc/deployment.json @@ -1,8 +1,3 @@ { - "version": { - "major": 1, - "minor": 11, - "patch": 0 - }, "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 bd1429938..804b7896f 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -2,34 +2,22 @@ from __future__ import annotations import json import os -from typing import Dict, List, Optional +from typing import Dict, List 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 -from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH class EnvironmentConfig: def __init__(self, file_path): self._server_config_path = os.path.expanduser(file_path) self.server_config = None - self.deployment = self._get_deployment_from_file() self.user_creds = None self.aws = None self._load_from_file(self._server_config_path) - def _get_deployment_from_file(self) -> Optional[str]: - deployment = None - - deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json") - with open(deployment_info_file_path, "r") as deployment_info_file: - deployment_info = json.load(deployment_info_file) - deployment = deployment_info["deployment"] - - return deployment - def _load_from_file(self, file_path): file_path = os.path.expanduser(file_path) @@ -61,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/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index c42f2d694..473c96ff1 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -1,10 +1,13 @@ +import json import logging +import os +from typing import Optional 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 logger = logging.getLogger(__name__) @@ -40,7 +43,7 @@ class VersionUpdateService: :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_file(), get_version(), ) @@ -61,6 +64,17 @@ class VersionUpdateService: @staticmethod def get_download_link(): return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % ( - env_singleton.env.get_deployment(), + VersionUpdateService.get_deployment_file(), get_version(), ) + + @staticmethod + def get_deployment_file() -> Optional[str]: + deployment = "unknown" + + deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json") + with open(deployment_info_file_path, "r") as deployment_info_file: + deployment_info = json.load(deployment_info_file) + deployment = deployment_info["deployment"] + + 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/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..01ebb81d5 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py @@ -0,0 +1,16 @@ +import os + +import pytest + +from monkey_island.cc.services.version_update import VersionUpdateService + + +@pytest.fixture +def deployment_file(data_for_tests_dir): + return os.path.join(data_for_tests_dir, "deployment.json") + + +def test_get_deployment_field(deployment_file, monkeypatch): + monkeypatch.setattr(os.path, "join", lambda *args: deployment_file) + deployment = VersionUpdateService().get_deployment_file() + assert deployment == "develop" From 9fd6ea9598306ec9e3a3cef71dc2858fee299130 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 14 Sep 2021 14:02:24 +0530 Subject: [PATCH 08/14] island, tests: Modify function to get deployment type with file path as input and modify related tests --- monkey/monkey_island/cc/services/version_update.py | 14 ++++++++------ .../cc/services/test_version_update.py | 8 ++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index 473c96ff1..5bf773c6b 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -1,7 +1,6 @@ import json import logging import os -from typing import Optional import requests @@ -42,8 +41,10 @@ class VersionUpdateService: Checks if newer monkey version is available :return: False if not, version in string format ('1.6.2') otherwise """ + deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json") + url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % ( - VersionUpdateService.get_deployment_file(), + VersionUpdateService.get_deployment_from_file(deployment_info_file_path), get_version(), ) @@ -63,17 +64,18 @@ class VersionUpdateService: @staticmethod def get_download_link(): + deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json") + return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % ( - VersionUpdateService.get_deployment_file(), + VersionUpdateService.get_deployment_from_file(deployment_info_file_path), get_version(), ) @staticmethod - def get_deployment_file() -> Optional[str]: + def get_deployment_from_file(file_path: str) -> str: deployment = "unknown" - deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json") - with open(deployment_info_file_path, "r") as deployment_info_file: + with open(file_path, "r") as deployment_info_file: deployment_info = json.load(deployment_info_file) deployment = deployment_info["deployment"] 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 index 01ebb81d5..84cb45535 100644 --- 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 @@ -6,11 +6,11 @@ from monkey_island.cc.services.version_update import VersionUpdateService @pytest.fixture -def deployment_file(data_for_tests_dir): +def deployment_info_file_path(data_for_tests_dir): return os.path.join(data_for_tests_dir, "deployment.json") -def test_get_deployment_field(deployment_file, monkeypatch): - monkeypatch.setattr(os.path, "join", lambda *args: deployment_file) - deployment = VersionUpdateService().get_deployment_file() +def test_get_deployment_field(deployment_info_file_path, monkeypatch): + monkeypatch.setattr(os.path, "join", lambda *args: deployment_info_file_path) + deployment = VersionUpdateService().get_deployment_from_file(deployment_info_file_path) assert deployment == "develop" From 90c6392e1636093afbbc4fc4a70308e9b66406c6 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 14 Sep 2021 14:22:45 +0530 Subject: [PATCH 09/14] island, tests: Handle exceptions when getting deployment type from file and add related tests --- .../cc/services/version_update.py | 11 ++++-- monkey/tests/data_for_tests/deployment_flawed | 1 + .../data_for_tests/deployment_key_error.json | 3 ++ .../cc/services/test_version_update.py | 37 ++++++++++++++++++- 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 monkey/tests/data_for_tests/deployment_flawed create mode 100644 monkey/tests/data_for_tests/deployment_key_error.json diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index 5bf773c6b..52dd70c9b 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -75,8 +75,13 @@ class VersionUpdateService: def get_deployment_from_file(file_path: str) -> str: deployment = "unknown" - with open(file_path, "r") as deployment_info_file: - deployment_info = json.load(deployment_info_file) - deployment = deployment_info["deployment"] + try: + with open(file_path, "r") as deployment_info_file: + deployment_info = json.load(deployment_info_file) + deployment = deployment_info["deployment"] + except Exception as ex: + logger.debug( + f"Couldn't get deployment info from {str(file_path)}. Exception: {str(ex)}." + ) return deployment 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/unit_tests/monkey_island/cc/services/test_version_update.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py index 84cb45535..7b0067c0b 100644 --- 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 @@ -10,7 +10,42 @@ def deployment_info_file_path(data_for_tests_dir): return os.path.join(data_for_tests_dir, "deployment.json") -def test_get_deployment_field(deployment_info_file_path, monkeypatch): +@pytest.fixture +def ghost_file(): + return "ghost_file" + + +@pytest.fixture +def key_error_deployment_info_file_path(data_for_tests_dir): + return os.path.join(data_for_tests_dir, "deployment_key_error.json") + + +@pytest.fixture +def flawed_deployment_info_file_path(data_for_tests_dir): + return os.path.join(data_for_tests_dir, "deployment_flawed") + + +def test_get_deployment_field_from_file(deployment_info_file_path, monkeypatch): monkeypatch.setattr(os.path, "join", lambda *args: 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, monkeypatch): + monkeypatch.setattr(os.path, "join", lambda *args: 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, monkeypatch): + monkeypatch.setattr(os.path, "join", lambda *args: 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, monkeypatch): + monkeypatch.setattr(os.path, "join", lambda *args: flawed_deployment_info_file_path) + deployment = VersionUpdateService().get_deployment_from_file(flawed_deployment_info_file_path) + assert deployment == "unknown" From 686f65e4f4f325fc028d54c564bf7d3c5caf683c Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 14 Sep 2021 16:04:06 +0530 Subject: [PATCH 10/14] tests: Move monkeypatch statements to fixtures in test_version_update.py --- .../cc/services/test_version_update.py | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) 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 index 7b0067c0b..4824ddfed 100644 --- 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 @@ -6,46 +6,50 @@ from monkey_island.cc.services.version_update import VersionUpdateService @pytest.fixture -def deployment_info_file_path(data_for_tests_dir): - return os.path.join(data_for_tests_dir, "deployment.json") +def deployment_info_file_path(monkeypatch, data_for_tests_dir): + path = os.path.join(data_for_tests_dir, "deployment.json") + monkeypatch.setattr(os.path, "join", lambda *args: path) + return path @pytest.fixture -def ghost_file(): - return "ghost_file" +def ghost_file(monkeypatch): + path = "ghost_file" + monkeypatch.setattr(os.path, "join", lambda *args: path) + return path @pytest.fixture -def key_error_deployment_info_file_path(data_for_tests_dir): - return os.path.join(data_for_tests_dir, "deployment_key_error.json") +def key_error_deployment_info_file_path(monkeypatch, data_for_tests_dir): + path = os.path.join(data_for_tests_dir, "deployment_key_error.json") + monkeypatch.setattr(os.path, "join", lambda *args: path) + return path @pytest.fixture -def flawed_deployment_info_file_path(data_for_tests_dir): - return os.path.join(data_for_tests_dir, "deployment_flawed") +def flawed_deployment_info_file_path(monkeypatch, data_for_tests_dir): + path = os.path.join(data_for_tests_dir, "deployment_flawed") + monkeypatch.setattr(os.path, "join", lambda *args: path) + return path -def test_get_deployment_field_from_file(deployment_info_file_path, monkeypatch): - monkeypatch.setattr(os.path, "join", lambda *args: deployment_info_file_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, monkeypatch): - monkeypatch.setattr(os.path, "join", lambda *args: ghost_file) +def test_get_deployment_field_from_nonexistent_file(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, monkeypatch): - monkeypatch.setattr(os.path, "join", lambda *args: key_error_deployment_info_file_path) +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, monkeypatch): - monkeypatch.setattr(os.path, "join", lambda *args: flawed_deployment_info_file_path) +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" From 38011f20b5a3eaf5a10ae0083fbf664b1930804f Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 14 Sep 2021 16:05:19 +0530 Subject: [PATCH 11/14] island: Remove unnecessary type conversion in log statement --- monkey/monkey_island/cc/services/version_update.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index 52dd70c9b..25872a45a 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -80,8 +80,6 @@ class VersionUpdateService: deployment_info = json.load(deployment_info_file) deployment = deployment_info["deployment"] except Exception as ex: - logger.debug( - f"Couldn't get deployment info from {str(file_path)}. Exception: {str(ex)}." - ) + logger.debug(f"Couldn't get deployment info from {file_path}. Exception: {str(ex)}.") return deployment From 412aefab3eb2494817dff0425c0730b554a77d85 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 14 Sep 2021 10:19:14 -0400 Subject: [PATCH 12/14] Island: Switch get_deployment_from_file() to use Paths --- .../cc/services/version_update.py | 13 +++++---- .../cc/services/test_version_update.py | 27 +++++++------------ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index 25872a45a..0430ba5be 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -1,6 +1,6 @@ import json import logging -import os +from pathlib import Path import requests @@ -8,6 +8,8 @@ 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__) @@ -41,10 +43,9 @@ class VersionUpdateService: Checks if newer monkey version is available :return: False if not, version in string format ('1.6.2') otherwise """ - deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json") url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % ( - VersionUpdateService.get_deployment_from_file(deployment_info_file_path), + VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH), get_version(), ) @@ -64,15 +65,13 @@ class VersionUpdateService: @staticmethod def get_download_link(): - deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json") - return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % ( - VersionUpdateService.get_deployment_from_file(deployment_info_file_path), + VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH), get_version(), ) @staticmethod - def get_deployment_from_file(file_path: str) -> str: + def get_deployment_from_file(file_path: Path) -> str: deployment = "unknown" try: 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 index 4824ddfed..e7fe7bdf5 100644 --- 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 @@ -1,4 +1,4 @@ -import os +from pathlib import Path import pytest @@ -6,30 +6,20 @@ from monkey_island.cc.services.version_update import VersionUpdateService @pytest.fixture -def deployment_info_file_path(monkeypatch, data_for_tests_dir): - path = os.path.join(data_for_tests_dir, "deployment.json") - monkeypatch.setattr(os.path, "join", lambda *args: path) +def deployment_info_file_path(data_for_tests_dir): + path = data_for_tests_dir / "deployment.json" return path @pytest.fixture -def ghost_file(monkeypatch): - path = "ghost_file" - monkeypatch.setattr(os.path, "join", lambda *args: path) +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 key_error_deployment_info_file_path(monkeypatch, data_for_tests_dir): - path = os.path.join(data_for_tests_dir, "deployment_key_error.json") - monkeypatch.setattr(os.path, "join", lambda *args: path) - return path - - -@pytest.fixture -def flawed_deployment_info_file_path(monkeypatch, data_for_tests_dir): - path = os.path.join(data_for_tests_dir, "deployment_flawed") - monkeypatch.setattr(os.path, "join", lambda *args: path) +def flawed_deployment_info_file_path(data_for_tests_dir): + path = data_for_tests_dir / "deployment_flawed" return path @@ -38,7 +28,8 @@ def test_get_deployment_field_from_file(deployment_info_file_path): assert deployment == "develop" -def test_get_deployment_field_from_nonexistent_file(ghost_file): +def test_get_deployment_field_from_nonexistent_file(): + ghost_file = Path("ghost_file") deployment = VersionUpdateService().get_deployment_from_file(ghost_file) assert deployment == "unknown" From 6ebe2e391b5644490baf317ee442b301ddffafa2 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 14 Sep 2021 16:54:06 +0200 Subject: [PATCH 13/14] Island: Add more exceptions to get_deployment_from_file --- monkey/monkey_island/cc/services/version_update.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index 0430ba5be..b9255232e 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -78,7 +78,13 @@ class VersionUpdateService: 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: {str(ex)}.") + logger.debug(f"Couldn't get deployment info from {file_path}. Exception: {ex}.") return deployment From 3287f4831e86a034480733649503489efdf1ce49 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 14 Sep 2021 12:22:03 -0400 Subject: [PATCH 14/14] Build: Remove deployment.json files --- build_scripts/appimage/deployment.json | 3 --- build_scripts/docker/deployment.json | 3 --- 2 files changed, 6 deletions(-) delete mode 100644 build_scripts/appimage/deployment.json delete mode 100644 build_scripts/docker/deployment.json diff --git a/build_scripts/appimage/deployment.json b/build_scripts/appimage/deployment.json deleted file mode 100644 index 914c44d4f..000000000 --- a/build_scripts/appimage/deployment.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "deployment": "standard" -} diff --git a/build_scripts/docker/deployment.json b/build_scripts/docker/deployment.json deleted file mode 100644 index 66125c06f..000000000 --- a/build_scripts/docker/deployment.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "deployment": "docker" -}