forked from p15670423/monkey
cc: add `data_dir` property to EnvironmentConfig
This commit is contained in:
parent
2d971d95fc
commit
438a2701d4
|
@ -6,6 +6,7 @@ from pathlib import Path
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
|
||||||
import monkey_island.cc.environment.server_config_generator as server_config_generator
|
import monkey_island.cc.environment.server_config_generator as server_config_generator
|
||||||
|
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR
|
||||||
from monkey_island.cc.environment.user_creds import UserCreds
|
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.auth_user import User
|
||||||
from monkey_island.cc.resources.auth.user_store import UserStore
|
from monkey_island.cc.resources.auth.user_store import UserStore
|
||||||
|
@ -18,6 +19,7 @@ class EnvironmentConfig:
|
||||||
self.deployment = None
|
self.deployment = None
|
||||||
self.user_creds = None
|
self.user_creds = None
|
||||||
self.aws = None
|
self.aws = None
|
||||||
|
self.data_dir = None
|
||||||
|
|
||||||
self._load_from_file(self._server_config_path)
|
self._load_from_file(self._server_config_path)
|
||||||
|
|
||||||
|
@ -26,7 +28,7 @@ class EnvironmentConfig:
|
||||||
|
|
||||||
if not Path(file_path).is_file():
|
if not Path(file_path).is_file():
|
||||||
server_config_generator.create_default_config_file(file_path)
|
server_config_generator.create_default_config_file(file_path)
|
||||||
with open(file_path, 'r') as f:
|
with open(file_path, "r") as f:
|
||||||
config_content = f.read()
|
config_content = f.read()
|
||||||
|
|
||||||
self._load_from_json(config_content)
|
self._load_from_json(config_content)
|
||||||
|
@ -37,22 +39,29 @@ class EnvironmentConfig:
|
||||||
|
|
||||||
def _load_from_dict(self, dict_data: Dict):
|
def _load_from_dict(self, dict_data: Dict):
|
||||||
user_creds = UserCreds.get_from_dict(dict_data)
|
user_creds = UserCreds.get_from_dict(dict_data)
|
||||||
aws = dict_data['aws'] if 'aws' in dict_data else None
|
aws = dict_data["aws"] if "aws" in dict_data else None
|
||||||
|
data_dir = (
|
||||||
|
dict_data["data_dir"] if "data_dir" in dict_data else DEFAULT_DATA_DIR
|
||||||
|
)
|
||||||
|
|
||||||
self.server_config = dict_data['server_config']
|
self.server_config = dict_data["server_config"]
|
||||||
self.deployment = dict_data['deployment']
|
self.deployment = dict_data["deployment"]
|
||||||
self.user_creds = user_creds
|
self.user_creds = user_creds
|
||||||
self.aws = aws
|
self.aws = aws
|
||||||
|
self.data_dir = data_dir
|
||||||
|
|
||||||
def save_to_file(self):
|
def save_to_file(self):
|
||||||
with open(self._server_config_path, 'w') as f:
|
with open(self._server_config_path, "w") as f:
|
||||||
f.write(json.dumps(self.to_dict(), indent=2))
|
f.write(json.dumps(self.to_dict(), indent=2))
|
||||||
|
|
||||||
def to_dict(self) -> Dict:
|
def to_dict(self) -> Dict:
|
||||||
config_dict = {'server_config': self.server_config,
|
config_dict = {
|
||||||
'deployment': self.deployment}
|
"server_config": self.server_config,
|
||||||
|
"deployment": self.deployment,
|
||||||
|
"data_dir": self.data_dir,
|
||||||
|
}
|
||||||
if self.aws:
|
if self.aws:
|
||||||
config_dict.update({'aws': self.aws})
|
config_dict.update({"aws": self.aws})
|
||||||
config_dict.update(self.user_creds.to_dict())
|
config_dict.update(self.user_creds.to_dict())
|
||||||
return config_dict
|
return config_dict
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ ENV_DICT = {
|
||||||
}
|
}
|
||||||
|
|
||||||
env = None
|
env = None
|
||||||
|
config = None
|
||||||
|
|
||||||
|
|
||||||
def set_env(env_type: str, env_config: EnvironmentConfig):
|
def set_env(env_type: str, env_config: EnvironmentConfig):
|
||||||
|
@ -39,6 +40,8 @@ def set_to_standard():
|
||||||
|
|
||||||
|
|
||||||
def initialize_from_file(file_path):
|
def initialize_from_file(file_path):
|
||||||
|
global config
|
||||||
|
|
||||||
try:
|
try:
|
||||||
config = EnvironmentConfig(file_path)
|
config = EnvironmentConfig(file_path)
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import shutil
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
|
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, MONKEY_ISLAND_ABS_PATH
|
||||||
from monkey_island.cc.environment.environment_config import EnvironmentConfig
|
from monkey_island.cc.environment.environment_config import EnvironmentConfig
|
||||||
from monkey_island.cc.environment.user_creds import UserCreds
|
from monkey_island.cc.environment.user_creds import UserCreds
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ PARTIAL_CREDENTIALS = os.path.join(
|
||||||
STANDARD_WITH_CREDENTIALS = os.path.join(
|
STANDARD_WITH_CREDENTIALS = os.path.join(
|
||||||
TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json"
|
TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json"
|
||||||
)
|
)
|
||||||
|
DATA_DIR = os.path.join(TEST_RESOURCES_DIR, "server_config_data_dir.json")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -33,28 +34,31 @@ def config_file(tmpdir):
|
||||||
def test_get_with_credentials():
|
def test_get_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()) == 5
|
||||||
assert config_dict["server_config"] == "password"
|
assert config_dict["server_config"] == "password"
|
||||||
assert config_dict["deployment"] == "develop"
|
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"
|
||||||
|
assert config_dict["data_dir"] == DEFAULT_DATA_DIR
|
||||||
|
|
||||||
|
|
||||||
def test_get_with_no_credentials():
|
def test_get_with_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()) == 3
|
||||||
assert config_dict["server_config"] == "password"
|
assert config_dict["server_config"] == "password"
|
||||||
assert config_dict["deployment"] == "develop"
|
assert config_dict["deployment"] == "develop"
|
||||||
|
assert config_dict["data_dir"] == DEFAULT_DATA_DIR
|
||||||
|
|
||||||
|
|
||||||
def test_get_with_partial_credentials():
|
def test_get_with_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()) == 4
|
||||||
assert config_dict["server_config"] == "password"
|
assert config_dict["server_config"] == "password"
|
||||||
assert config_dict["deployment"] == "develop"
|
assert config_dict["deployment"] == "develop"
|
||||||
assert config_dict["user"] == "test"
|
assert config_dict["user"] == "test"
|
||||||
|
assert config_dict["data_dir"] == DEFAULT_DATA_DIR
|
||||||
|
|
||||||
|
|
||||||
def test_save_to_file(config_file):
|
def test_save_to_file(config_file):
|
||||||
|
@ -67,12 +71,13 @@ def test_save_to_file(config_file):
|
||||||
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.keys()) == 5
|
assert len(from_file.keys()) == 6
|
||||||
assert from_file["server_config"] == "standard"
|
assert from_file["server_config"] == "standard"
|
||||||
assert from_file["deployment"] == "develop"
|
assert from_file["deployment"] == "develop"
|
||||||
assert from_file["user"] == "test"
|
assert from_file["user"] == "test"
|
||||||
assert from_file["password_hash"] == "abcdef"
|
assert from_file["password_hash"] == "abcdef"
|
||||||
assert from_file["aws"] == "test_aws"
|
assert from_file["aws"] == "test_aws"
|
||||||
|
assert from_file["data_dir"] == DEFAULT_DATA_DIR
|
||||||
|
|
||||||
|
|
||||||
def test_add_user(config_file):
|
def test_add_user(config_file):
|
||||||
|
@ -88,7 +93,7 @@ def test_add_user(config_file):
|
||||||
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.keys()) == 4
|
assert len(from_file.keys()) == 5
|
||||||
assert from_file["user"] == new_user
|
assert from_file["user"] == new_user
|
||||||
assert from_file["password_hash"] == new_password_hash
|
assert from_file["password_hash"] == new_password_hash
|
||||||
|
|
||||||
|
@ -113,3 +118,9 @@ def test_generate_default_file(config_file):
|
||||||
assert environment_config.user_creds.username == ""
|
assert environment_config.user_creds.username == ""
|
||||||
assert environment_config.user_creds.password_hash == ""
|
assert environment_config.user_creds.password_hash == ""
|
||||||
assert environment_config.aws is None
|
assert environment_config.aws is None
|
||||||
|
assert environment_config.data_dir == DEFAULT_DATA_DIR
|
||||||
|
|
||||||
|
|
||||||
|
def test_data_dir():
|
||||||
|
environment_config = EnvironmentConfig(DATA_DIR)
|
||||||
|
assert environment_config.data_dir == "/test/data/dir"
|
||||||
|
|
|
@ -16,3 +16,5 @@ DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join(
|
||||||
DEFAULT_LOGGER_CONFIG_PATH = os.path.join(
|
DEFAULT_LOGGER_CONFIG_PATH = os.path.join(
|
||||||
MONKEY_ISLAND_ABS_PATH, "cc", "island_logger_default_config.json"
|
MONKEY_ISLAND_ABS_PATH, "cc", "island_logger_default_config.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc")
|
||||||
|
|
|
@ -3,7 +3,7 @@ import logging.config
|
||||||
import os
|
import os
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
from monkey_island.cc.consts import DEFAULT_LOGGER_CONFIG_PATH
|
from monkey_island.cc.server_utils.consts import DEFAULT_LOGGER_CONFIG_PATH
|
||||||
|
|
||||||
__author__ = "Maor.Rayzin"
|
__author__ = "Maor.Rayzin"
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH
|
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
|
||||||
from monkey_island.cc.island_logger import json_setup_logging
|
from monkey_island.cc.server_utils.island_logger import json_setup_logging
|
||||||
|
|
||||||
TEST_LOGGER_CONFIG_PATH = os.path.join(
|
TEST_LOGGER_CONFIG_PATH = os.path.join(
|
||||||
MONKEY_ISLAND_ABS_PATH, "cc", "testing", "logger_config.json"
|
MONKEY_ISLAND_ABS_PATH, "cc", "testing", "logger_config.json"
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"server_config": "password",
|
||||||
|
"deployment": "develop",
|
||||||
|
"user": "test",
|
||||||
|
"password_hash": "abcdef",
|
||||||
|
"data_dir": "/test/data/dir"
|
||||||
|
}
|
Loading…
Reference in New Issue