forked from p15670423/monkey
island: Add "environment" section to server_config.json
Put the environment config inside its own "environment" object in the server_config.json to provide a logical separation between the environment config, logger config, data directory, etc.
This commit is contained in:
parent
9476441526
commit
fb3e66f75e
|
@ -35,7 +35,7 @@ class EnvironmentConfig:
|
|||
|
||||
def _load_from_json(self, config_json: str) -> EnvironmentConfig:
|
||||
data = json.loads(config_json)
|
||||
self._load_from_dict(data)
|
||||
self._load_from_dict(data["environment"])
|
||||
|
||||
def _load_from_dict(self, dict_data: Dict):
|
||||
aws = dict_data["aws"] if "aws" in dict_data else None
|
||||
|
@ -52,8 +52,13 @@ class EnvironmentConfig:
|
|||
return os.path.abspath(os.path.expanduser(os.path.expandvars(self.data_dir)))
|
||||
|
||||
def save_to_file(self):
|
||||
with open(self._server_config_path, "r") as f:
|
||||
config = json.load(f)
|
||||
|
||||
config["environment"] = self.to_dict()
|
||||
|
||||
with open(self._server_config_path, "w") as f:
|
||||
f.write(json.dumps(self.to_dict(), indent=2))
|
||||
f.write(json.dumps(config, indent=2))
|
||||
|
||||
def to_dict(self) -> Dict:
|
||||
config_dict = {
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
{
|
||||
"server_config": "password",
|
||||
"deployment": "develop",
|
||||
"log_level": "DEBUG"
|
||||
"log_level": "DEBUG",
|
||||
"environment": {
|
||||
"server_config": "password",
|
||||
"deployment": "develop"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,13 +54,29 @@ def test_save_to_file(config_file, standard_with_credentials):
|
|||
with open(config_file, "r") as f:
|
||||
from_file = json.load(f)
|
||||
|
||||
assert len(from_file.keys()) == 6
|
||||
assert from_file["server_config"] == "standard"
|
||||
assert from_file["deployment"] == "develop"
|
||||
assert from_file["user"] == "test"
|
||||
assert from_file["password_hash"] == "abcdef"
|
||||
assert from_file["aws"] == "test_aws"
|
||||
assert from_file["data_dir"] == DEFAULT_DATA_DIR
|
||||
assert len(from_file.keys()) == 2
|
||||
assert len(from_file["environment"].keys()) == 6
|
||||
assert from_file["environment"]["server_config"] == "standard"
|
||||
assert from_file["environment"]["deployment"] == "develop"
|
||||
assert from_file["environment"]["user"] == "test"
|
||||
assert from_file["environment"]["password_hash"] == "abcdef"
|
||||
assert from_file["environment"]["aws"] == "test_aws"
|
||||
assert from_file["environment"]["data_dir"] == DEFAULT_DATA_DIR
|
||||
|
||||
|
||||
def test_save_to_file_preserve_log_level(config_file, standard_with_credentials):
|
||||
shutil.copyfile(standard_with_credentials, config_file)
|
||||
|
||||
environment_config = EnvironmentConfig(config_file)
|
||||
environment_config.aws = "test_aws"
|
||||
environment_config.save_to_file()
|
||||
|
||||
with open(config_file, "r") as f:
|
||||
from_file = json.load(f)
|
||||
|
||||
assert len(from_file.keys()) == 2
|
||||
assert "log_level" in from_file
|
||||
assert from_file["log_level"] == "NOTICE"
|
||||
|
||||
|
||||
def test_add_user(config_file, standard_with_credentials):
|
||||
|
@ -76,9 +92,9 @@ def test_add_user(config_file, standard_with_credentials):
|
|||
with open(config_file, "r") as f:
|
||||
from_file = json.load(f)
|
||||
|
||||
assert len(from_file.keys()) == 5
|
||||
assert from_file["user"] == new_user
|
||||
assert from_file["password_hash"] == new_password_hash
|
||||
assert len(from_file["environment"].keys()) == 5
|
||||
assert from_file["environment"]["user"] == new_user
|
||||
assert from_file["environment"]["password_hash"] == new_password_hash
|
||||
|
||||
|
||||
def test_get_users(standard_with_credentials):
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
{
|
||||
"environment" : {
|
||||
"server_config": "password",
|
||||
"deployment": "develop"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
{
|
||||
"environment" : {
|
||||
"server_config": "password",
|
||||
"deployment": "develop",
|
||||
"user": "test"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
{
|
||||
"environment" : {
|
||||
"server_config": "standard",
|
||||
"deployment": "develop"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
{
|
||||
"log_level": "NOTICE",
|
||||
"environment" : {
|
||||
"server_config": "standard",
|
||||
"deployment": "develop",
|
||||
"user": "test",
|
||||
"password_hash": "abcdef"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
{
|
||||
"environment" : {
|
||||
"server_config": "password",
|
||||
"deployment": "develop",
|
||||
"user": "test",
|
||||
"password_hash": "abcdef"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
{
|
||||
"data_dir": "/test/data/dir",
|
||||
"environment" : {
|
||||
"server_config": "password",
|
||||
"deployment": "develop",
|
||||
"user": "test",
|
||||
"password_hash": "abcdef",
|
||||
"data_dir": "/test/data/dir"
|
||||
"password_hash": "abcdef"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
{
|
||||
"data_dir": "~/data_dir",
|
||||
"environment" : {
|
||||
"server_config": "password",
|
||||
"deployment": "develop",
|
||||
"user": "test",
|
||||
"password_hash": "abcdef",
|
||||
"data_dir": "~/data_dir"
|
||||
"password_hash": "abcdef"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue