Merge pull request #1459 from guardicore/extract-deployment-field

Extract deployment field from server config
This commit is contained in:
Mike Salvatore 2021-09-14 12:24:46 -04:00 committed by GitHub
commit f54b759d92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 92 additions and 29 deletions

View File

@ -2,8 +2,7 @@
"data_dir": "~/.monkey_island", "data_dir": "~/.monkey_island",
"log_level": "DEBUG", "log_level": "DEBUG",
"environment": { "environment": {
"server_config": "password", "server_config": "password"
"deployment": "standard"
}, },
"mongodb": { "mongodb": {
"start_mongodb": true "start_mongodb": true

View File

@ -2,8 +2,7 @@
"data_dir": "/monkey_island_data", "data_dir": "/monkey_island_data",
"log_level": "DEBUG", "log_level": "DEBUG",
"environment": { "environment": {
"server_config": "password", "server_config": "password"
"deployment": "docker"
}, },
"mongodb": { "mongodb": {
"start_mongodb": false "start_mongodb": false

View File

@ -6,6 +6,7 @@ from pathlib import Path
MAJOR = "1" MAJOR = "1"
MINOR = "11" MINOR = "11"
PATCH = "0" PATCH = "0"
build_file_path = Path(__file__).parent.joinpath("BUILD") build_file_path = Path(__file__).parent.joinpath("BUILD")
with open(build_file_path, "r") as build_file: with open(build_file_path, "r") as build_file:
BUILD = build_file.read() BUILD = build_file.read()

View File

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

View File

@ -88,9 +88,3 @@ class Environment(object, metaclass=ABCMeta):
def get_auth_expiration_time(self): def get_auth_expiration_time(self):
return self._AUTH_EXPIRATION_TIME 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

@ -13,7 +13,6 @@ class EnvironmentConfig:
def __init__(self, file_path): def __init__(self, file_path):
self._server_config_path = os.path.expanduser(file_path) self._server_config_path = os.path.expanduser(file_path)
self.server_config = None self.server_config = None
self.deployment = None
self.user_creds = None self.user_creds = None
self.aws = None self.aws = None
@ -35,7 +34,6 @@ class EnvironmentConfig:
aws = dict_data["aws"] if "aws" in dict_data else None aws = dict_data["aws"] if "aws" in dict_data else None
self.server_config = dict_data["server_config"] self.server_config = dict_data["server_config"]
self.deployment = dict_data["deployment"]
self.user_creds = _get_user_credentials_from_config(dict_data) self.user_creds = _get_user_credentials_from_config(dict_data)
self.aws = aws self.aws = aws
@ -51,7 +49,6 @@ class EnvironmentConfig:
def to_dict(self) -> Dict: def to_dict(self) -> Dict:
config_dict = { config_dict = {
"server_config": self.server_config, "server_config": self.server_config,
"deployment": self.deployment,
} }
if self.aws: if self.aws:
config_dict.update({"aws": self.aws}) config_dict.update({"aws": self.aws})

View File

@ -1,8 +1,7 @@
{ {
"log_level": "DEBUG", "log_level": "DEBUG",
"environment": { "environment": {
"server_config": "password", "server_config": "password"
"deployment": "develop"
}, },
"mongodb": { "mongodb": {
"start_mongodb": true "start_mongodb": true

View File

@ -1,10 +1,14 @@
import json
import logging import logging
from pathlib import Path
import requests import requests
import monkey_island.cc.environment.environment_singleton as env_singleton
from common.utils.exceptions import VersionServerConnectionError from common.utils.exceptions import VersionServerConnectionError
from common.version import get_version 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__) logger = logging.getLogger(__name__)
@ -39,8 +43,9 @@ class VersionUpdateService:
Checks if newer monkey version is available Checks if newer monkey version is available
:return: False if not, version in string format ('1.6.2') otherwise :return: False if not, version in string format ('1.6.2') otherwise
""" """
url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % ( url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % (
env_singleton.env.get_deployment(), VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH),
get_version(), get_version(),
) )
@ -61,6 +66,25 @@ class VersionUpdateService:
@staticmethod @staticmethod
def get_download_link(): def get_download_link():
return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % ( return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % (
env_singleton.env.get_deployment(), VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH),
get_version(), 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

View File

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

View File

@ -0,0 +1 @@
develop

View File

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

View File

@ -1,7 +1,6 @@
{ {
"environment" : { "environment" : {
"server_config": "password", "server_config": "password"
"deployment": "develop"
}, },
"mongodb": { "mongodb": {
"start_mongodb": true "start_mongodb": true

View File

@ -1,7 +1,6 @@
{ {
"environment" : { "environment" : {
"server_config": "password", "server_config": "password",
"deployment": "develop",
"user": "test" "user": "test"
}, },
"mongodb": { "mongodb": {

View File

@ -2,7 +2,6 @@
"log_level": "NOTICE", "log_level": "NOTICE",
"environment" : { "environment" : {
"server_config": "password", "server_config": "password",
"deployment": "develop",
"user": "test", "user": "test",
"password_hash": "abcdef" "password_hash": "abcdef"
}, },

View File

@ -16,9 +16,8 @@ def config_file(tmpdir):
def test_get_with_credentials(with_credentials): def test_get_with_credentials(with_credentials):
config_dict = EnvironmentConfig(with_credentials).to_dict() 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["server_config"] == "password"
assert config_dict["deployment"] == "develop"
assert config_dict["user"] == "test" assert config_dict["user"] == "test"
assert config_dict["password_hash"] == "abcdef" 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): def test_get_with_no_credentials(no_credentials):
config_dict = EnvironmentConfig(no_credentials).to_dict() 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["server_config"] == "password"
assert config_dict["deployment"] == "develop"
def test_get_with_partial_credentials(partial_credentials): def test_get_with_partial_credentials(partial_credentials):
config_dict = EnvironmentConfig(partial_credentials).to_dict() 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["server_config"] == "password"
assert config_dict["deployment"] == "develop"
assert config_dict["user"] == "test" assert config_dict["user"] == "test"
@ -80,7 +77,7 @@ def test_add_user(config_file, with_credentials):
with open(config_file, "r") as f: with open(config_file, "r") as f:
from_file = json.load(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"]["user"] == new_user
assert from_file["environment"]["password_hash"] == new_password_hash assert from_file["environment"]["password_hash"] == new_password_hash

View File

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