From ba6d1b18ee326333a0e398c21c14412de1ab8598 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 3 Aug 2022 17:39:30 +0200 Subject: [PATCH] Island: Remove VersionUpdate service Issue #2109 PR #2161 --- .../cc/services/version_update.py | 90 ------------------- .../cc/services/test_version_update.py | 46 ---------- 2 files changed, 136 deletions(-) delete mode 100644 monkey/monkey_island/cc/services/version_update.py delete mode 100644 monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py deleted file mode 100644 index b9255232e..000000000 --- a/monkey/monkey_island/cc/services/version_update.py +++ /dev/null @@ -1,90 +0,0 @@ -import json -import logging -from pathlib import Path - -import requests - -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__) - - -class VersionUpdateService: - VERSION_SERVER_URL_PREF = "https://updates.infectionmonkey.com" - VERSION_SERVER_CHECK_NEW_URL = VERSION_SERVER_URL_PREF + "?deployment=%s&monkey_version=%s" - VERSION_SERVER_DOWNLOAD_URL = VERSION_SERVER_CHECK_NEW_URL + "&is_download=true" - - newer_version = None - - def __init__(self): - pass - - @staticmethod - def get_newer_version(): - """ - Checks for newer version if never checked before. - :return: None if failed checking for newer version, result of '_check_new_version' otherwise - """ - if VersionUpdateService.newer_version is None: - try: - VersionUpdateService.newer_version = VersionUpdateService._check_new_version() - except VersionServerConnectionError: - logger.info("Failed updating version number") - - return VersionUpdateService.newer_version - - @staticmethod - def _check_new_version(): - """ - 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 % ( - VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH), - get_version(), - ) - - try: - reply = requests.get(url, timeout=7) - except requests.exceptions.RequestException: - logger.info("Can't get latest monkey version, probably no connection to the internet.") - raise VersionServerConnectionError - - res = reply.json().get("newer_version", None) - - if res is False: - return res - - [int(x) for x in res.split(".")] # raises value error if version is invalid format - return res - - @staticmethod - def get_download_link(): - return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % ( - 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/unit_tests/monkey_island/cc/services/test_version_update.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py deleted file mode 100644 index e7fe7bdf5..000000000 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py +++ /dev/null @@ -1,46 +0,0 @@ -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"