forked from p15670423/monkey
Island: try reading server_config.json from install directory
On windows it's not easy to pass server_config as a commandline parameter. It's easier to just create a file in install directory.
This commit is contained in:
parent
00665cbae0
commit
dcc71faaa9
|
@ -36,7 +36,8 @@ MONGO_EXECUTABLE_PATH = (
|
|||
)
|
||||
MONGO_CONNECTION_TIMEOUT = 15
|
||||
|
||||
DEFAULT_SERVER_CONFIG_PATH = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME))
|
||||
PACKAGE_CONFIG_PATH = Path(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME)
|
||||
USER_CONFIG_PATH = Path(os.getcwd(), SERVER_CONFIG_FILENAME)
|
||||
|
||||
DEFAULT_LOG_LEVEL = "INFO"
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ from pathlib import Path
|
|||
|
||||
from common.utils.file_utils import expand_path
|
||||
from monkey_island.cc.arg_parser import IslandCmdArgs
|
||||
from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH
|
||||
from monkey_island.cc.server_utils.consts import PACKAGE_CONFIG_PATH, USER_CONFIG_PATH
|
||||
from monkey_island.cc.setup.island_config_options import IslandConfigOptions
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
@ -13,7 +13,9 @@ logger = getLogger(__name__)
|
|||
def get_server_config(island_args: IslandCmdArgs) -> IslandConfigOptions:
|
||||
config = IslandConfigOptions({})
|
||||
|
||||
update_config_from_file(config, DEFAULT_SERVER_CONFIG_PATH)
|
||||
update_config_from_file(config, PACKAGE_CONFIG_PATH)
|
||||
|
||||
update_config_from_file(config, USER_CONFIG_PATH)
|
||||
|
||||
if island_args.server_config_path:
|
||||
path_to_config = expand_path(island_args.server_config_path)
|
||||
|
@ -26,6 +28,7 @@ def update_config_from_file(config: IslandConfigOptions, config_path: Path):
|
|||
try:
|
||||
config_from_file = load_server_config_from_file(config_path)
|
||||
config.update(config_from_file)
|
||||
logger.info(f"Server config updated from {config_path}")
|
||||
except OSError:
|
||||
logger.info(f"Server config not found in path {config_path}")
|
||||
|
||||
|
|
|
@ -15,4 +15,4 @@ def test_default_server_config_file_path():
|
|||
else:
|
||||
server_file_path = f"{consts.MONKEY_ISLAND_ABS_PATH}/cc/{consts.SERVER_CONFIG_FILENAME}"
|
||||
|
||||
assert consts.DEFAULT_SERVER_CONFIG_PATH == server_file_path
|
||||
assert consts.PACKAGE_CONFIG_PATH == server_file_path
|
||||
|
|
|
@ -12,15 +12,23 @@ BAD_JSON = '{"data_dir": "C:\\test\\test"'
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def user_server_config_path(tmpdir) -> Path:
|
||||
def cmd_server_config_path(tmpdir) -> Path:
|
||||
# Represents the config that user can provide via cmd arguments
|
||||
return tmpdir / "fake_server_config.json"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def deployment_server_config_path(tmpdir) -> Path:
|
||||
def user_default_server_config_path(tmpdir) -> Path:
|
||||
# Represents the config that can be put into the install dir
|
||||
return tmpdir / "fake_server_config2.json"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def deployment_server_config_path(tmpdir) -> Path:
|
||||
# Represents the config that is built in, deployment specific
|
||||
return tmpdir / "fake_server_config3.json"
|
||||
|
||||
|
||||
def create_server_config(config_contents: str, server_config_path: Path):
|
||||
with open(server_config_path, "w") as file:
|
||||
file.write(config_contents)
|
||||
|
@ -34,6 +42,14 @@ def mock_deployment_config_path(monkeypatch, deployment_server_config_path):
|
|||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_user_config_path(monkeypatch, deployment_server_config_path):
|
||||
monkeypatch.setattr(
|
||||
"monkey_island.cc.setup.config_setup.USER_CONFIG_PATH",
|
||||
deployment_server_config_path,
|
||||
)
|
||||
|
||||
|
||||
def test_extract_config_defaults():
|
||||
expected = IslandConfigOptions({})
|
||||
assert (
|
||||
|
@ -42,7 +58,7 @@ def test_extract_config_defaults():
|
|||
)
|
||||
|
||||
|
||||
def test_package_config_overrides_defaults(deployment_server_config_path):
|
||||
def test_deployment_config_overrides_defaults(deployment_server_config_path):
|
||||
expected = IslandConfigOptions({"key_path": "/key_path_2"})
|
||||
create_server_config(dumps({"key_path": "/key_path_2"}), deployment_server_config_path)
|
||||
assert (
|
||||
|
@ -51,19 +67,32 @@ def test_package_config_overrides_defaults(deployment_server_config_path):
|
|||
)
|
||||
|
||||
|
||||
def test_user_config_overrides_package_config(
|
||||
deployment_server_config_path, user_server_config_path
|
||||
def test_user_config_overrides_deployment(
|
||||
deployment_server_config_path, cmd_server_config_path, user_default_server_config_path
|
||||
):
|
||||
expected = IslandConfigOptions({"key_path": "/key_path_3"})
|
||||
create_server_config(dumps({"key_path": "/key_path_2"}), deployment_server_config_path)
|
||||
create_server_config(dumps({"key_path": "/key_path_3"}), user_server_config_path)
|
||||
create_server_config(dumps({"key_path": "/key_path_3"}), user_default_server_config_path)
|
||||
extracted_config = _extract_config(
|
||||
IslandCmdArgs(setup_only=False, server_config_path=user_server_config_path)
|
||||
IslandCmdArgs(setup_only=False, server_config_path=cmd_server_config_path)
|
||||
)
|
||||
assert expected.__dict__ == extracted_config.__dict__
|
||||
|
||||
|
||||
def test_malformed_json(user_server_config_path):
|
||||
create_server_config(BAD_JSON, user_server_config_path)
|
||||
def test_cmd_config_overrides_everything(
|
||||
deployment_server_config_path, cmd_server_config_path, user_default_server_config_path
|
||||
):
|
||||
expected = IslandConfigOptions({"key_path": "/key_path_4"})
|
||||
create_server_config(dumps({"key_path": "/key_path_2"}), deployment_server_config_path)
|
||||
create_server_config(dumps({"key_path": "/key_path_3"}), user_default_server_config_path)
|
||||
create_server_config(dumps({"key_path": "/key_path_4"}), cmd_server_config_path)
|
||||
extracted_config = _extract_config(
|
||||
IslandCmdArgs(setup_only=False, server_config_path=cmd_server_config_path)
|
||||
)
|
||||
assert expected.__dict__ == extracted_config.__dict__
|
||||
|
||||
|
||||
def test_malformed_json(cmd_server_config_path):
|
||||
create_server_config(BAD_JSON, cmd_server_config_path)
|
||||
with pytest.raises(SystemExit):
|
||||
_extract_config(IslandCmdArgs(setup_only=False, server_config_path=user_server_config_path))
|
||||
_extract_config(IslandCmdArgs(setup_only=False, server_config_path=cmd_server_config_path))
|
||||
|
|
Loading…
Reference in New Issue