From e1209dcb4c7c54fcb864f2652c5fadf63bdf00ab Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 16 Feb 2021 11:59:27 -0500 Subject: [PATCH] cc: add data_dir_abs_path property to EnvironmentConfig EnvironmentConfig needs to handle environment variables and '~' in its `data_dir` property. Other components that consume `data_dir` need environment variables and '~' resolved to an absolute path. Add a property called `data_dir_abs_path` that calculates the absolute path from `data_dir`. Since `data_dir` remains unchanged, the EnvironmentConfig can be saved to file without modifying the `data_dir` option in the file. --- .../cc/environment/environment_config.py | 4 ++++ .../cc/environment/test_environment_config.py | 13 ++++++++++++- monkey/monkey_island/cc/main.py | 2 +- monkey/monkey_island/cc/server_utils/encryptor.py | 4 +--- monkey/monkey_island/cc/test_encryptor.py | 8 -------- .../server_config_with_data_dir_home.json | 7 +++++++ 6 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 monkey/monkey_island/cc/testing/environment/server_config_with_data_dir_home.json diff --git a/monkey/monkey_island/cc/environment/environment_config.py b/monkey/monkey_island/cc/environment/environment_config.py index 3bc9327b4..f390d8186 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -50,6 +50,10 @@ class EnvironmentConfig: self.aws = aws self.data_dir = data_dir + @property + def data_dir_abs_path(self): + return os.path.abspath(os.path.expanduser(os.path.expandvars(self.data_dir))) + def save_to_file(self): with open(self._server_config_path, "w") as f: f.write(json.dumps(self.to_dict(), indent=2)) diff --git a/monkey/monkey_island/cc/environment/test_environment_config.py b/monkey/monkey_island/cc/environment/test_environment_config.py index b7712d480..cee36645b 100644 --- a/monkey/monkey_island/cc/environment/test_environment_config.py +++ b/monkey/monkey_island/cc/environment/test_environment_config.py @@ -24,7 +24,7 @@ STANDARD_WITH_CREDENTIALS = os.path.join( TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json" ) WITH_DATA_DIR = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir.json") - +WITH_DATA_DIR_HOME = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir_home.json") @pytest.fixture def config_file(tmpdir): @@ -124,3 +124,14 @@ def test_generate_default_file(config_file): def test_data_dir(): environment_config = EnvironmentConfig(WITH_DATA_DIR) assert environment_config.data_dir == "/test/data/dir" + + +def set_home_env(monkeypatch, tmpdir): + monkeypatch.setenv("HOME", str(tmpdir)) + + +def test_data_dir_abs_path_from_file(monkeypatch, tmpdir): + set_home_env(monkeypatch, tmpdir) + + config = EnvironmentConfig(WITH_DATA_DIR_HOME) + assert config.data_dir_abs_path == os.path.join(tmpdir, "data_dir") diff --git a/monkey/monkey_island/cc/main.py b/monkey/monkey_island/cc/main.py index 3e6f43d8c..303f5997d 100644 --- a/monkey/monkey_island/cc/main.py +++ b/monkey/monkey_island/cc/main.py @@ -35,7 +35,7 @@ MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0" def main(should_setup_only=False, server_config_filename=DEFAULT_SERVER_CONFIG_PATH): logger.info("Starting bootloader server") env_singleton.initialize_from_file(server_config_filename) - initialize_encryptor(env_singleton.config.data_dir) + initialize_encryptor(env_singleton.config.data_dir_abs_path) mongo_url = os.environ.get('MONGO_URL', env_singleton.env.get_mongo_url()) bootloader_server_thread = Thread(target=BootloaderHttpServer(mongo_url).serve_forever, daemon=True) diff --git a/monkey/monkey_island/cc/server_utils/encryptor.py b/monkey/monkey_island/cc/server_utils/encryptor.py index 5b1328551..162efd15e 100644 --- a/monkey/monkey_island/cc/server_utils/encryptor.py +++ b/monkey/monkey_island/cc/server_utils/encryptor.py @@ -16,9 +16,7 @@ class Encryptor: _PASSWORD_FILENAME = "mongo_key.bin" def __init__(self, password_file_dir): - password_file = os.path.expanduser( - os.path.join(password_file_dir, self._PASSWORD_FILENAME) - ) + password_file = os.path.join(password_file_dir, self._PASSWORD_FILENAME) if os.path.exists(password_file): self._load_existing_key(password_file) diff --git a/monkey/monkey_island/cc/test_encryptor.py b/monkey/monkey_island/cc/test_encryptor.py index d147ed3f5..b2564b16c 100644 --- a/monkey/monkey_island/cc/test_encryptor.py +++ b/monkey/monkey_island/cc/test_encryptor.py @@ -33,11 +33,3 @@ def test_create_new_password_file(tmpdir): initialize_encryptor(tmpdir) assert os.path.isfile(os.path.join(tmpdir, PASSWORD_FILENAME)) - - -def test_expand_home(monkeypatch, tmpdir): - monkeypatch.setenv("HOME", str(tmpdir)) - - initialize_encryptor("~/") - - assert os.path.isfile(os.path.join(tmpdir, "mongo_key.bin")) diff --git a/monkey/monkey_island/cc/testing/environment/server_config_with_data_dir_home.json b/monkey/monkey_island/cc/testing/environment/server_config_with_data_dir_home.json new file mode 100644 index 000000000..e6e4a0a1f --- /dev/null +++ b/monkey/monkey_island/cc/testing/environment/server_config_with_data_dir_home.json @@ -0,0 +1,7 @@ +{ + "server_config": "password", + "deployment": "develop", + "user": "test", + "password_hash": "abcdef", + "data_dir": "~/data_dir" +}