From c1fc56d4ce339db98d2a44860fd1ec786f3e9fd5 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Mon, 13 Sep 2021 18:47:28 +0200 Subject: [PATCH] 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"