From 5ecc02d553cf8291ed33d6fcf4a16aaeed95eaa3 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 3 Jun 2021 19:28:45 -0400 Subject: [PATCH 1/4] island: Use the data_dir specified in the default server config --- .../cc/environment/server_config_handler.py | 16 ++++++++-------- monkey/monkey_island/cc/server_utils/consts.py | 2 +- monkey/monkey_island/cc/setup/config_setup.py | 9 ++++----- .../unit_tests/monkey_island/cc/test_consts.py | 4 ++-- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/monkey/monkey_island/cc/environment/server_config_handler.py b/monkey/monkey_island/cc/environment/server_config_handler.py index f1e2358c4..b2f4883c0 100644 --- a/monkey/monkey_island/cc/environment/server_config_handler.py +++ b/monkey/monkey_island/cc/environment/server_config_handler.py @@ -2,20 +2,20 @@ import json import os from pathlib import Path -from monkey_island.cc.server_utils.consts import ( - DEFAULT_DEVELOP_SERVER_CONFIG_PATH, - DEFAULT_SERVER_CONFIG_PATH, -) +from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH, SERVER_CONFIG_FILENAME from monkey_island.cc.setup.island_config_options import IslandConfigOptions -def create_default_server_config_file() -> None: - if not os.path.isfile(DEFAULT_SERVER_CONFIG_PATH): - write_default_server_config_to_file(DEFAULT_SERVER_CONFIG_PATH) +def create_default_server_config_file(data_dir) -> None: + config_file_path = os.path.join(data_dir, SERVER_CONFIG_FILENAME) + if not os.path.isfile(config_file_path): + write_default_server_config_to_file(config_file_path) + + return config_file_path def write_default_server_config_to_file(path: str) -> None: - default_config = Path(DEFAULT_DEVELOP_SERVER_CONFIG_PATH).read_text() + default_config = Path(DEFAULT_SERVER_CONFIG_PATH).read_text() Path(path).write_text(default_config) diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index a14c69d0b..3de221647 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -37,7 +37,7 @@ MONGO_EXECUTABLE_PATH = ( ) DEFAULT_SERVER_CONFIG_PATH = os.path.expandvars( - os.path.join(DEFAULT_DATA_DIR, SERVER_CONFIG_FILENAME) + os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME) ) DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( diff --git a/monkey/monkey_island/cc/setup/config_setup.py b/monkey/monkey_island/cc/setup/config_setup.py index 6cc036760..601c67efc 100644 --- a/monkey/monkey_island/cc/setup/config_setup.py +++ b/monkey/monkey_island/cc/setup/config_setup.py @@ -4,7 +4,7 @@ from typing import Tuple from monkey_island.cc.arg_parser import IslandCmdArgs from monkey_island.cc.environment import server_config_handler from monkey_island.cc.environment.utils import create_secure_directory -from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_SERVER_CONFIG_PATH +from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH from monkey_island.cc.setup.island_config_options import IslandConfigOptions @@ -23,8 +23,7 @@ def _setup_config_by_cmd_arg(server_config_path) -> Tuple[IslandConfigOptions, s def _setup_default_config() -> Tuple[IslandConfigOptions, str]: - server_config_path = DEFAULT_SERVER_CONFIG_PATH - create_secure_directory(DEFAULT_DATA_DIR, create_parent_dirs=False) - server_config_handler.create_default_server_config_file() - config = server_config_handler.load_server_config_from_file(server_config_path) + config = server_config_handler.load_server_config_from_file(DEFAULT_SERVER_CONFIG_PATH) + create_secure_directory(config.data_dir, create_parent_dirs=False) + server_config_path = server_config_handler.create_default_server_config_file(config.data_dir) return config, server_config_path diff --git a/monkey/tests/unit_tests/monkey_island/cc/test_consts.py b/monkey/tests/unit_tests/monkey_island/cc/test_consts.py index 993ddaa64..600766aeb 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/test_consts.py +++ b/monkey/tests/unit_tests/monkey_island/cc/test_consts.py @@ -5,8 +5,8 @@ import monkey_island.cc.server_utils.consts as consts def test_default_server_config_file_path(): if platform.system() == "Windows": - server_file_path = f"{consts.DEFAULT_DATA_DIR}\\{consts.SERVER_CONFIG_FILENAME}" + server_file_path = f"{consts.MONKEY_ISLAND_ABS_PATH}\\cc\\{consts.SERVER_CONFIG_FILENAME}" else: - server_file_path = f"{consts.DEFAULT_DATA_DIR}/{consts.SERVER_CONFIG_FILENAME}" + server_file_path = f"{consts.MONKEY_ISLAND_ABS_PATH}/cc/{consts.SERVER_CONFIG_FILENAME}" assert consts.DEFAULT_SERVER_CONFIG_PATH == server_file_path From e57e28a97f592327d7109480275eb1e71f5da994 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 3 Jun 2021 19:39:43 -0400 Subject: [PATCH 2/4] island: Consolidate tests for consts.py --- .../monkey_island/cc/server_utils/test_consts.py | 11 +++++++++++ .../tests/unit_tests/monkey_island/cc/test_consts.py | 12 ------------ 2 files changed, 11 insertions(+), 12 deletions(-) delete mode 100644 monkey/tests/unit_tests/monkey_island/cc/test_consts.py diff --git a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_consts.py b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_consts.py index eeb600b9b..a88f0c64b 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_consts.py +++ b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_consts.py @@ -1,5 +1,16 @@ +import platform + from monkey_island.cc.server_utils import consts def test_monkey_island_abs_path(): assert consts.MONKEY_ISLAND_ABS_PATH.endswith("monkey_island") + + +def test_default_server_config_file_path(): + if platform.system() == "Windows": + server_file_path = f"{consts.MONKEY_ISLAND_ABS_PATH}\\cc\\{consts.SERVER_CONFIG_FILENAME}" + else: + server_file_path = f"{consts.MONKEY_ISLAND_ABS_PATH}/cc/{consts.SERVER_CONFIG_FILENAME}" + + assert consts.DEFAULT_SERVER_CONFIG_PATH == server_file_path diff --git a/monkey/tests/unit_tests/monkey_island/cc/test_consts.py b/monkey/tests/unit_tests/monkey_island/cc/test_consts.py deleted file mode 100644 index 600766aeb..000000000 --- a/monkey/tests/unit_tests/monkey_island/cc/test_consts.py +++ /dev/null @@ -1,12 +0,0 @@ -import platform - -import monkey_island.cc.server_utils.consts as consts - - -def test_default_server_config_file_path(): - if platform.system() == "Windows": - server_file_path = f"{consts.MONKEY_ISLAND_ABS_PATH}\\cc\\{consts.SERVER_CONFIG_FILENAME}" - else: - server_file_path = f"{consts.MONKEY_ISLAND_ABS_PATH}/cc/{consts.SERVER_CONFIG_FILENAME}" - - assert consts.DEFAULT_SERVER_CONFIG_PATH == server_file_path From 0cd9709b826e3b212bd31c8af54583a8dbadb19b Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 3 Jun 2021 20:12:27 -0400 Subject: [PATCH 3/4] island: Remove server_config.json.develop Since Monkey Island no longer writes to server_config.json in the source code directory, and each package will overwrite server_config.json with its own server config (until we separate the deployment from the config in issue #1205), we no longer need server_config.json.develop and we don't need to worry about accidentally committing credentials to git. --- .travis.yml | 3 --- .../cc/{server_config.json.develop => server_config.json} | 0 monkey/monkey_island/cc/server_utils/consts.py | 4 ---- 3 files changed, 7 deletions(-) rename monkey/monkey_island/cc/{server_config.json.develop => server_config.json} (100%) diff --git a/.travis.yml b/.travis.yml index 6654cc3af..b54e7d2cf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,9 +21,6 @@ python: os: linux -before_install: -# Init server_config.json to default -- cp monkey/monkey_island/cc/server_config.json.develop monkey/monkey_island/cc/server_config.json install: # Python diff --git a/monkey/monkey_island/cc/server_config.json.develop b/monkey/monkey_island/cc/server_config.json similarity index 100% rename from monkey/monkey_island/cc/server_config.json.develop rename to monkey/monkey_island/cc/server_config.json diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index 3de221647..357e2bc8e 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -40,9 +40,5 @@ DEFAULT_SERVER_CONFIG_PATH = os.path.expandvars( os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME) ) -DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( - MONKEY_ISLAND_ABS_PATH, "cc", f"{SERVER_CONFIG_FILENAME}.develop" -) - DEFAULT_LOG_LEVEL = "INFO" DEFAULT_START_MONGO_DB = True From 2b1af17433b5422bdbd975288ffbdb4ec02fbc15 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 7 Jun 2021 06:39:05 -0400 Subject: [PATCH 4/4] island: Add typehint to create_default_server_config_file() Co-authored-by: Shreya Malviya --- monkey/monkey_island/cc/environment/server_config_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/environment/server_config_handler.py b/monkey/monkey_island/cc/environment/server_config_handler.py index b2f4883c0..363b7c2e6 100644 --- a/monkey/monkey_island/cc/environment/server_config_handler.py +++ b/monkey/monkey_island/cc/environment/server_config_handler.py @@ -6,7 +6,7 @@ from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH, SER from monkey_island.cc.setup.island_config_options import IslandConfigOptions -def create_default_server_config_file(data_dir) -> None: +def create_default_server_config_file(data_dir: str) -> str: config_file_path = os.path.join(data_dir, SERVER_CONFIG_FILENAME) if not os.path.isfile(config_file_path): write_default_server_config_to_file(config_file_path)