Island: Change monkey code to use deployment.json

Add UTs for get_deployment. Fix Enviroment UTs.
This commit is contained in:
Ilija Lazoroski 2021-09-13 18:47:28 +02:00
parent 78ab3f176c
commit c1fc56d4ce
8 changed files with 44 additions and 45 deletions

View File

@ -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:

View File

@ -1,8 +1,3 @@
{
"version": {
"major": 1,
"minor": 11,
"patch": 0
},
"deployment": "develop"
}

View File

@ -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

View File

@ -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})

View File

@ -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

View File

@ -0,0 +1,3 @@
{
"deployment": "develop"
}

View File

@ -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

View File

@ -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"