From a31067a7529b44d13e5b9fc223b4d81d6e4fd088 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Fri, 21 May 2021 15:25:37 +0300 Subject: [PATCH 01/18] Added a common method to determine the runtime OS --- .../monkey_island/cc/server_utils/common_methods.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 monkey/monkey_island/cc/server_utils/common_methods.py diff --git a/monkey/monkey_island/cc/server_utils/common_methods.py b/monkey/monkey_island/cc/server_utils/common_methods.py new file mode 100644 index 000000000..218f146ed --- /dev/null +++ b/monkey/monkey_island/cc/server_utils/common_methods.py @@ -0,0 +1,11 @@ +import platform + +WINDOWS = "Windows" +LINUX = "Linux" + + +def get_runtime_os() -> str: + if platform.system() == "Windows": + return WINDOWS + else: + return LINUX From 2483691b8b721ba69c893a089d2a3f9ec89f5349 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 24 May 2021 08:45:00 +0300 Subject: [PATCH 02/18] Implemented mongodb process launch from the island --- monkey/monkey_island/cc/main.py | 9 ++- .../monkey_island/cc/server_utils/consts.py | 4 ++ monkey/monkey_island/cc/setup/__init__.py | 0 .../database_initializer.py} | 13 ++-- .../cc/setup/mongo_process_runner.py | 63 +++++++++++++++++++ .../cc/setup/test_mongo_setup.py | 24 +++++++ 6 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 monkey/monkey_island/cc/setup/__init__.py rename monkey/monkey_island/cc/{mongo_setup.py => setup/database_initializer.py} (88%) create mode 100644 monkey/monkey_island/cc/setup/mongo_process_runner.py create mode 100644 monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_setup.py diff --git a/monkey/monkey_island/cc/main.py b/monkey/monkey_island/cc/main.py index df015863b..820630210 100644 --- a/monkey/monkey_island/cc/main.py +++ b/monkey/monkey_island/cc/main.py @@ -24,13 +24,16 @@ from common.version import get_version # noqa: E402 from monkey_island.cc.app import init_app # noqa: E402 from monkey_island.cc.database import get_db_version # noqa: E402 from monkey_island.cc.database import is_db_server_up # noqa: E402 -from monkey_island.cc.mongo_setup import init_collections, launch_mongodb # noqa: E402 from monkey_island.cc.resources.monkey_download import MonkeyDownload # noqa: E402 from monkey_island.cc.server_utils.bootloader_server import BootloaderHttpServer # noqa: E402 from monkey_island.cc.server_utils.encryptor import initialize_encryptor # noqa: E402 from monkey_island.cc.services.initialize import initialize_services # noqa: E402 from monkey_island.cc.services.reporting.exporter_init import populate_exporter_list # noqa: E402 from monkey_island.cc.services.utils.network_utils import local_ip_addresses # noqa: E402 +from monkey_island.cc.setup.mongo_process_runner import ( # noqa: E402 + MongoDbRunner, + init_collections, +) MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0" @@ -51,7 +54,9 @@ def main(setup_only: bool, config_options: IslandConfigOptions): def start_island_server(should_setup_only, config_options: IslandConfigOptions): if config_options.start_mongodb: - launch_mongodb() + MongoDbRunner( + db_dir_parent_path=config_options.data_dir, logging_dir_path=config_options.data_dir + ).launch_mongodb() mongo_url = os.environ.get("MONGO_URL", env_singleton.env.get_mongo_url()) wait_for_mongo_db_server(mongo_url) assert_mongo_db_version(mongo_url) diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index bc99b4394..9bc1e7059 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -23,6 +23,10 @@ DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5 DEFAULT_SERVER_CONFIG_PATH = os.path.expandvars( os.path.join(DEFAULT_DATA_DIR, SERVER_CONFIG_FILENAME) ) +_MONGO_EXECUTABLE_PATH = os.path.join(MONKEY_ISLAND_ABS_PATH, "bin", "mongodb") +MONGO_EXECUTABLE_PATH_WIN = os.path.join(_MONGO_EXECUTABLE_PATH, "mongod.exe") +MONGO_EXECUTABLE_PATH_LINUX = os.path.join(_MONGO_EXECUTABLE_PATH, "bin", "mongod") + DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( MONKEY_ISLAND_ABS_PATH, "cc", f"{SERVER_CONFIG_FILENAME}.develop" diff --git a/monkey/monkey_island/cc/setup/__init__.py b/monkey/monkey_island/cc/setup/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/monkey/monkey_island/cc/mongo_setup.py b/monkey/monkey_island/cc/setup/database_initializer.py similarity index 88% rename from monkey/monkey_island/cc/mongo_setup.py rename to monkey/monkey_island/cc/setup/database_initializer.py index 74cb29fc2..34914c7ce 100644 --- a/monkey/monkey_island/cc/mongo_setup.py +++ b/monkey/monkey_island/cc/setup/database_initializer.py @@ -9,17 +9,12 @@ from monkey_island.cc.services.attack.mitre_api_interface import MitreApiInterfa logger = logging.getLogger(__name__) -def launch_mongodb(): - # TODO: Implement the launch of mongodb process - pass - - def init_collections(): logger.info("Setting up the Monkey Island, this might take a while...") - try_store_mitigations_on_mongo() + _try_store_mitigations_on_mongo() -def try_store_mitigations_on_mongo(): +def _try_store_mitigations_on_mongo(): mitigation_collection_name = AttackMitigations.COLLECTION_NAME try: mongo.db.validate_collection(mitigation_collection_name) @@ -33,10 +28,10 @@ def try_store_mitigations_on_mongo(): except errors.CollectionInvalid: pass finally: - store_mitigations_on_mongo() + _store_mitigations_on_mongo() -def store_mitigations_on_mongo(): +def _store_mitigations_on_mongo(): stix2_mitigations = MitreApiInterface.get_all_mitigations() mongo_mitigations = AttackMitigations.dict_from_stix2_attack_patterns( MitreApiInterface.get_all_attack_techniques() diff --git a/monkey/monkey_island/cc/setup/mongo_process_runner.py b/monkey/monkey_island/cc/setup/mongo_process_runner.py new file mode 100644 index 000000000..bc429479e --- /dev/null +++ b/monkey/monkey_island/cc/setup/mongo_process_runner.py @@ -0,0 +1,63 @@ +import logging +import os +import subprocess +from typing import List + +from monkey_island.cc.server_utils.common_methods import WINDOWS, get_runtime_os +from monkey_island.cc.server_utils.consts import ( + MONGO_EXECUTABLE_PATH_LINUX, + MONGO_EXECUTABLE_PATH_WIN, +) + +logger = logging.getLogger(__name__) + +DB_DIR_NAME = "db" +DB_DIR_PARAM = "--dbpath" +MONGO_LOG_FILENAME = "mongo_log.txt" + + +class MongoDbRunner: + def __init__(self, db_dir_parent_path: str, logging_dir_path: str): + """ + @param db_dir_parent_path: Path where a folder for database contents will be created + @param logging_dir_path: Path to a folder where mongodb logs will be created + """ + self.db_dir_parent_path = db_dir_parent_path + self.logging_dir_path = logging_dir_path + + def launch_mongodb(self): + db_path = self._create_db_dir() + self._start_mongodb_process(db_path) + + def _create_db_dir(self) -> str: + db_path = os.path.join(self.db_dir_parent_path, DB_DIR_NAME) + logger.info(f"Database content directory: {db_path}.") + if not os.path.isdir(db_path): + logger.info("Database content directory not found, creating one.") + os.mkdir(os.path.join(self.db_dir_parent_path, DB_DIR_NAME)) + return db_path + + def _start_mongodb_process(self, db_dir_path: str): + logger.info("Starting MongoDb process.") + mongo_exec = MongoDbRunner._get_path_of_mongo_exec() + + mongo_run_cmd = MongoDbRunner._build_mongo_launch_cmd(mongo_exec, db_dir_path) + logger.info(f"Mongodb will be launched with command: f{' '.join(mongo_run_cmd)}.") + + mongo_log_path = os.path.join(self.logging_dir_path, MONGO_LOG_FILENAME) + logger.info(f"Mongodb log will be available at f{mongo_log_path}.") + + with open(mongo_log_path, "w") as log: + subprocess.Popen(mongo_run_cmd, stderr=subprocess.STDOUT, stdout=log) + logger.info("MongoDb launched successfully!") + + @staticmethod + def _get_path_of_mongo_exec(): + if get_runtime_os() == WINDOWS: + return MONGO_EXECUTABLE_PATH_WIN + else: + return MONGO_EXECUTABLE_PATH_LINUX + + @staticmethod + def _build_mongo_launch_cmd(exec_path: str, db_path: str) -> List[str]: + return [exec_path, DB_DIR_PARAM, db_path] diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_setup.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_setup.py new file mode 100644 index 000000000..388e1a6c0 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_setup.py @@ -0,0 +1,24 @@ +import os + +from monkey_island.cc.setup.mongo_setup import _create_db_dir + + +def test_create_db_dir(monkeypatch, tmpdir): + test_dir_name = "test_dir" + monkeypatch.setattr("monkey_island.cc.setup.mongo_setup.DB_DIR_NAME", test_dir_name) + expected_path = os.path.join(tmpdir, test_dir_name) + + db_path = _create_db_dir(tmpdir) + assert os.path.isdir(expected_path) + assert db_path == expected_path + + +def test_create_db_dir_already_created(monkeypatch, tmpdir): + test_dir_name = "test_dir" + monkeypatch.setattr("monkey_island.cc.setup.mongo_setup.DB_DIR_NAME", test_dir_name) + expected_path = os.path.join(tmpdir, test_dir_name) + os.mkdir(expected_path) + + db_path = _create_db_dir(tmpdir) + assert os.path.isdir(expected_path) + assert db_path == expected_path From 5ec64ef1896e9ae4bb089f404f72cd637d89f767 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 25 May 2021 13:53:22 +0300 Subject: [PATCH 03/18] Added unit test for db dir creation. --- .../cc/setup/test_mongo_setup.py | 24 ------------------- .../cc/setup/test_process_runner.py | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+), 24 deletions(-) delete mode 100644 monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_setup.py create mode 100644 monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_setup.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_setup.py deleted file mode 100644 index 388e1a6c0..000000000 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_setup.py +++ /dev/null @@ -1,24 +0,0 @@ -import os - -from monkey_island.cc.setup.mongo_setup import _create_db_dir - - -def test_create_db_dir(monkeypatch, tmpdir): - test_dir_name = "test_dir" - monkeypatch.setattr("monkey_island.cc.setup.mongo_setup.DB_DIR_NAME", test_dir_name) - expected_path = os.path.join(tmpdir, test_dir_name) - - db_path = _create_db_dir(tmpdir) - assert os.path.isdir(expected_path) - assert db_path == expected_path - - -def test_create_db_dir_already_created(monkeypatch, tmpdir): - test_dir_name = "test_dir" - monkeypatch.setattr("monkey_island.cc.setup.mongo_setup.DB_DIR_NAME", test_dir_name) - expected_path = os.path.join(tmpdir, test_dir_name) - os.mkdir(expected_path) - - db_path = _create_db_dir(tmpdir) - assert os.path.isdir(expected_path) - assert db_path == expected_path diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py new file mode 100644 index 000000000..f4aec0ba8 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py @@ -0,0 +1,24 @@ +import os + +from monkey_island.cc.setup.mongo_process_runner import MongoDbRunner + + +def test_create_db_dir(monkeypatch, tmpdir): + test_dir_name = "test_dir" + monkeypatch.setattr("monkey_island.cc.setup.mongo_process_runner.DB_DIR_NAME", test_dir_name) + expected_path = os.path.join(tmpdir, test_dir_name) + + db_path = MongoDbRunner(tmpdir, tmpdir)._create_db_dir() + assert os.path.isdir(expected_path) + assert db_path == expected_path + + +def test_create_db_dir__already_created(monkeypatch, tmpdir): + test_dir_name = "test_dir" + monkeypatch.setattr("monkey_island.cc.setup.mongo_process_runner.DB_DIR_NAME", test_dir_name) + expected_path = os.path.join(tmpdir, test_dir_name) + os.mkdir(expected_path) + + db_path = MongoDbRunner(tmpdir, tmpdir)._create_db_dir() + assert os.path.isdir(expected_path) + assert db_path == expected_path From 73f23ad3839d3a26223288c3ee734fa78506e326 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 25 May 2021 14:26:02 +0300 Subject: [PATCH 04/18] Removed run.sh and updated mongodb related documentation: removed db folder creation and run.sh execution on linux --- monkey/monkey_island/linux/run.sh | 21 --------------------- monkey/monkey_island/readme.md | 7 +------ 2 files changed, 1 insertion(+), 27 deletions(-) delete mode 100755 monkey/monkey_island/linux/run.sh diff --git a/monkey/monkey_island/linux/run.sh b/monkey/monkey_island/linux/run.sh deleted file mode 100755 index a284ffa83..000000000 --- a/monkey/monkey_island/linux/run.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -start_mongo() { - # TODO: Handle starting and cleaning up mongo inside monkey_island.py or - # monkey_island/main.py. - ./bin/mongodb/bin/mongod --dbpath ./bin/mongodb/db & -} - -cd_to_monkey() { - # Pipenv must be run from monkey/monkey/monkey_island, but monkey_island.py - # must be executed from monkey/monkey. - cd .. -} - -start_monkey_island() { - cd_to_monkey - python ./monkey_island.py -} - -start_mongo -start_monkey_island diff --git a/monkey/monkey_island/readme.md b/monkey/monkey_island/readme.md index 0882aecfe..4351dacff 100644 --- a/monkey/monkey_island/readme.md +++ b/monkey/monkey_island/readme.md @@ -28,7 +28,6 @@ - Place portable version of mongodb 1. Download from: 2. Extract contents of bin folder to \monkey\monkey_island\bin\mongodb. - 3. Create monkey_island\db folder. OR - Use already running instance of mongodb @@ -88,12 +87,8 @@ - `pipenv sync --dev` - `cd ..` -1. Set the linux `run.sh` to be executible: - - `chmod u+x monkey_island/linux/run.sh` - 1. Create the following directories in monkey island folder (execute from ./monkey): - `mkdir -p ./monkey_island/bin/mongodb` - - `mkdir -p ./monkey_island/db` - `mkdir -p ./monkey_island/cc/binaries` 1. Put monkey binaries in /monkey_island/cc/binaries (binaries can be found in releases on github). @@ -136,4 +131,4 @@ #### How to run -1. From the `monkey/monkey_island` directory, run `pipenv run ./linux/run.sh` +1. From the `monkey` directory, run `python3.7 ./monkey_island.py` From 559b61b581634ff775addc593f245b2d98101f06 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 25 May 2021 14:36:29 +0300 Subject: [PATCH 05/18] Removed code related to running mongodb and db folder creation --- deployment_scripts/deploy_windows.ps1 | 1 - monkey/monkey_island/linux/install_mongo.sh | 1 - monkey/monkey_island/windows/clear_db.bat | 4 ---- monkey/monkey_island/windows/run_mongodb.bat | 3 --- monkey/monkey_island/windows/run_server.bat | 4 +--- monkey/monkey_island/windows/run_server_py.bat | 2 -- 6 files changed, 1 insertion(+), 14 deletions(-) delete mode 100644 monkey/monkey_island/windows/clear_db.bat delete mode 100644 monkey/monkey_island/windows/run_mongodb.bat diff --git a/deployment_scripts/deploy_windows.ps1 b/deployment_scripts/deploy_windows.ps1 index 46f2fb0f4..9ce8480cb 100644 --- a/deployment_scripts/deploy_windows.ps1 +++ b/deployment_scripts/deploy_windows.ps1 @@ -176,7 +176,6 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName, } | Select-Object -ExpandProperty Name # Move all files from extracted folder to mongodb folder New-Item -ItemType directory -Path (Join-Path -Path $binDir -ChildPath "mongodb") - New-Item -ItemType directory -Path (Join-Path -Path $monkey_home -ChildPath $MONKEY_ISLAND_DIR | Join-Path -ChildPath "db") "Moving extracted files" Move-Item -Path (Join-Path -Path $binDir -ChildPath $mongodb_folder | Join-Path -ChildPath "\bin\*") -Destination (Join-Path -Path $binDir -ChildPath "mongodb\") "Removing zip file" diff --git a/monkey/monkey_island/linux/install_mongo.sh b/monkey/monkey_island/linux/install_mongo.sh index 2bf2d43d4..825daaf5a 100755 --- a/monkey/monkey_island/linux/install_mongo.sh +++ b/monkey/monkey_island/linux/install_mongo.sh @@ -58,7 +58,6 @@ popd || { } mkdir -p "${MONGODB_DIR}"/bin -mkdir -p "${MONGODB_DIR}"/db cp "${TEMP_MONGO}"/mongodb-*/bin/mongod "${MONGODB_DIR}"/bin/mongod cp "${TEMP_MONGO}"/mongodb-*/LICENSE-Community.txt "${MONGODB_DIR}"/ chmod a+x "${MONGODB_DIR}"/bin/mongod diff --git a/monkey/monkey_island/windows/clear_db.bat b/monkey/monkey_island/windows/clear_db.bat deleted file mode 100644 index 8597f3d32..000000000 --- a/monkey/monkey_island/windows/clear_db.bat +++ /dev/null @@ -1,4 +0,0 @@ -@echo Are you sure? (Press Any Key) -@pause -@rmdir /s /q db -@mkdir db \ No newline at end of file diff --git a/monkey/monkey_island/windows/run_mongodb.bat b/monkey/monkey_island/windows/run_mongodb.bat deleted file mode 100644 index 106b5f00a..000000000 --- a/monkey/monkey_island/windows/run_mongodb.bat +++ /dev/null @@ -1,3 +0,0 @@ -REM - Runs MongoDB Server - -@title MongoDB -@bin\mongodb\mongod.exe --dbpath db --bind_ip 127.0.0.1 \ No newline at end of file diff --git a/monkey/monkey_island/windows/run_server.bat b/monkey/monkey_island/windows/run_server.bat index ab2ad274c..5e5331a2e 100644 --- a/monkey/monkey_island/windows/run_server.bat +++ b/monkey/monkey_island/windows/run_server.bat @@ -1,5 +1,3 @@ REM - Runs MongoDB Server & Monkey Island Server using built pyinstaller EXE - -if not exist db mkdir db -start windows\run_mongodb.bat start windows\run_cc_exe.bat -start https://localhost:5000 \ No newline at end of file +start https://localhost:5000 diff --git a/monkey/monkey_island/windows/run_server_py.bat b/monkey/monkey_island/windows/run_server_py.bat index 90d81c9b7..a727211ea 100644 --- a/monkey/monkey_island/windows/run_server_py.bat +++ b/monkey/monkey_island/windows/run_server_py.bat @@ -1,5 +1,3 @@ REM - Runs MongoDB Server & Monkey Island Server using python - -if not exist db mkdir db -start windows\run_mongodb.bat pipenv run windows\run_cc.bat start https://localhost:5000 From 36b3e987da6474334c90faea058a86299e379f48 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Wed, 26 May 2021 09:14:49 +0300 Subject: [PATCH 06/18] Added expand user call to transform data dir input into a proper path --- monkey/monkey_island/setup/island_config_options.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/monkey/monkey_island/setup/island_config_options.py b/monkey/monkey_island/setup/island_config_options.py index bf1c06e1b..938c840a0 100644 --- a/monkey/monkey_island/setup/island_config_options.py +++ b/monkey/monkey_island/setup/island_config_options.py @@ -1,5 +1,7 @@ from __future__ import annotations +import os + from monkey_island.cc.server_utils.consts import ( DEFAULT_DATA_DIR, DEFAULT_LOG_LEVEL, @@ -9,7 +11,7 @@ from monkey_island.cc.server_utils.consts import ( class IslandConfigOptions: def __init__(self, config_contents: dict): - self.data_dir = config_contents.get("data_dir", DEFAULT_DATA_DIR) + self.data_dir = os.path.expanduser(config_contents.get("data_dir", DEFAULT_DATA_DIR)) self.log_level = config_contents.get("log_level", DEFAULT_LOG_LEVEL) From a5d72c8b94be6db4772bef1b2bf00899c379b948 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Wed, 26 May 2021 10:51:33 +0300 Subject: [PATCH 07/18] Fixed an import statement in monkey_island/main.py --- monkey/monkey_island/cc/main.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/monkey/monkey_island/cc/main.py b/monkey/monkey_island/cc/main.py index 820630210..af84666e8 100644 --- a/monkey/monkey_island/cc/main.py +++ b/monkey/monkey_island/cc/main.py @@ -9,6 +9,7 @@ from threading import Thread # "monkey_island." work. from gevent.pywsgi import WSGIServer +from monkey_island.cc.setup.database_initializer import init_collections from monkey_island.setup.island_config_options import IslandConfigOptions MONKEY_ISLAND_DIR_BASE_PATH = str(Path(__file__).parent.parent) @@ -30,10 +31,7 @@ from monkey_island.cc.server_utils.encryptor import initialize_encryptor # noqa from monkey_island.cc.services.initialize import initialize_services # noqa: E402 from monkey_island.cc.services.reporting.exporter_init import populate_exporter_list # noqa: E402 from monkey_island.cc.services.utils.network_utils import local_ip_addresses # noqa: E402 -from monkey_island.cc.setup.mongo_process_runner import ( # noqa: E402 - MongoDbRunner, - init_collections, -) +from monkey_island.cc.setup.mongo_process_runner import MongoDbRunner # noqa: E402 MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0" From 5f7e886310c2c61dbb8f1dfc82b04df254ff9ee2 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Wed, 26 May 2021 12:36:52 +0300 Subject: [PATCH 08/18] Updated CHANGELOG.md with mongodb launch from island changes. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6e3bfc45..6ab9cfb1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Improved the structure of unit tests by scoping fixtures only to relevant modules instead of having a one huge fixture file, improved and renamed the directory structure of unit tests and unit test infrastructure. #1178 +- MongoDb now gets launched by the Island via python. #1148 - Create/check data directory on Island init. #1170 ### Removed From 58745a0eb4c35213caa5bd68c43c9ecb9df2a353 Mon Sep 17 00:00:00 2001 From: Shreya Date: Wed, 26 May 2021 18:53:47 +0530 Subject: [PATCH 09/18] Use fixtures in test_process_runner.py --- .../cc/setup/test_process_runner.py | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py index f4aec0ba8..5aa4e697b 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py @@ -1,22 +1,26 @@ import os +import pytest + from monkey_island.cc.setup.mongo_process_runner import MongoDbRunner +TEST_DIR_NAME = "test_dir" -def test_create_db_dir(monkeypatch, tmpdir): - test_dir_name = "test_dir" - monkeypatch.setattr("monkey_island.cc.setup.mongo_process_runner.DB_DIR_NAME", test_dir_name) - expected_path = os.path.join(tmpdir, test_dir_name) +@pytest.fixture +def expected_path(monkeypatch, tmpdir): + monkeypatch.setattr("monkey_island.cc.setup.mongo_process_runner.DB_DIR_NAME", TEST_DIR_NAME) + expected_path = os.path.join(tmpdir, TEST_DIR_NAME) + return expected_path + + +def test_create_db_dir(monkeypatch, tmpdir, expected_path): db_path = MongoDbRunner(tmpdir, tmpdir)._create_db_dir() assert os.path.isdir(expected_path) assert db_path == expected_path -def test_create_db_dir__already_created(monkeypatch, tmpdir): - test_dir_name = "test_dir" - monkeypatch.setattr("monkey_island.cc.setup.mongo_process_runner.DB_DIR_NAME", test_dir_name) - expected_path = os.path.join(tmpdir, test_dir_name) +def test_create_db_dir__already_created(monkeypatch, tmpdir, expected_path): os.mkdir(expected_path) db_path = MongoDbRunner(tmpdir, tmpdir)._create_db_dir() From 0be8e5685805a3e8fe39a4098519d7b7bf818694 Mon Sep 17 00:00:00 2001 From: Shreya Date: Wed, 26 May 2021 19:02:11 +0530 Subject: [PATCH 10/18] Add fixture for fake db dir in test_process_runner.py --- .../monkey_island/cc/setup/test_process_runner.py | 6 +++++- whitelist.py | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py index 5aa4e697b..5a6870dc2 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py @@ -7,9 +7,13 @@ from monkey_island.cc.setup.mongo_process_runner import MongoDbRunner TEST_DIR_NAME = "test_dir" +@pytest.fixture(autouse=True) +def fake_db_dir(monkeypatch, tmpdir): + monkeypatch.setattr("monkey_island.cc.setup.mongo_process_runner.DB_DIR_NAME", TEST_DIR_NAME) + + @pytest.fixture def expected_path(monkeypatch, tmpdir): - monkeypatch.setattr("monkey_island.cc.setup.mongo_process_runner.DB_DIR_NAME", TEST_DIR_NAME) expected_path = os.path.join(tmpdir, TEST_DIR_NAME) return expected_path diff --git a/whitelist.py b/whitelist.py index 51d4c22b8..6739d4791 100644 --- a/whitelist.py +++ b/whitelist.py @@ -166,6 +166,8 @@ IBM # unused variable (monkey/common/cloud/environment_names.py:11) DigitalOcean # unused variable (monkey/common/cloud/environment_names.py:12) _.aws_info # unused attribute (monkey/monkey_island/cc/environment/aws.py:13) build_from_config_file_contents # unused method 'build_from_config_file_contents' (\monkey_island\setup\island_config_options.py:18) +fake_db_dir # unused function 'fake_db_dir' (monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py:10) + # these are not needed for it to work, but may be useful extra information to understand what's going on WINDOWS_PBA_TYPE # unused variable (monkey/monkey_island/cc/resources/pba_file_upload.py:23) From 1610860bd0009354353c3be6855873a58f2fb0a2 Mon Sep 17 00:00:00 2001 From: Shreya Date: Wed, 26 May 2021 19:03:30 +0530 Subject: [PATCH 11/18] Rename test_process_runner.py to test_mongo_process_runner.py to better reflect the file it's testing --- .../{test_process_runner.py => test_mongo_process_runner.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename monkey/tests/unit_tests/monkey_island/cc/setup/{test_process_runner.py => test_mongo_process_runner.py} (100%) diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py similarity index 100% rename from monkey/tests/unit_tests/monkey_island/cc/setup/test_process_runner.py rename to monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py From f5f8f572f6efd49b292a9fee540372117f9f9a46 Mon Sep 17 00:00:00 2001 From: Shreya Date: Wed, 26 May 2021 19:05:39 +0530 Subject: [PATCH 12/18] Remove unneeded function arguments in test_mongo_process_runner.py --- .../monkey_island/cc/setup/test_mongo_process_runner.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py index 5a6870dc2..5a8ac75f8 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py @@ -8,23 +8,23 @@ TEST_DIR_NAME = "test_dir" @pytest.fixture(autouse=True) -def fake_db_dir(monkeypatch, tmpdir): +def fake_db_dir(monkeypatch): monkeypatch.setattr("monkey_island.cc.setup.mongo_process_runner.DB_DIR_NAME", TEST_DIR_NAME) @pytest.fixture -def expected_path(monkeypatch, tmpdir): +def expected_path(tmpdir): expected_path = os.path.join(tmpdir, TEST_DIR_NAME) return expected_path -def test_create_db_dir(monkeypatch, tmpdir, expected_path): +def test_create_db_dir(tmpdir, expected_path): db_path = MongoDbRunner(tmpdir, tmpdir)._create_db_dir() assert os.path.isdir(expected_path) assert db_path == expected_path -def test_create_db_dir__already_created(monkeypatch, tmpdir, expected_path): +def test_create_db_dir__already_created(tmpdir, expected_path): os.mkdir(expected_path) db_path = MongoDbRunner(tmpdir, tmpdir)._create_db_dir() From f7674b0635c04d727adf4f124124f938bcacccb4 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Wed, 26 May 2021 16:45:51 +0300 Subject: [PATCH 13/18] Aggregated duplicate runtime os checking functions into one. --- monkey/monkey_island/cc/environment/utils.py | 4 ++-- .../monkey_island/cc/server_utils/common_methods.py | 11 ----------- monkey/monkey_island/cc/setup/mongo_process_runner.py | 4 ++-- 3 files changed, 4 insertions(+), 15 deletions(-) delete mode 100644 monkey/monkey_island/cc/server_utils/common_methods.py diff --git a/monkey/monkey_island/cc/environment/utils.py b/monkey/monkey_island/cc/environment/utils.py index cbb8a1d6f..585b2cc79 100644 --- a/monkey/monkey_island/cc/environment/utils.py +++ b/monkey/monkey_island/cc/environment/utils.py @@ -1,5 +1,5 @@ -import sys +import platform def is_windows_os() -> bool: - return sys.platform.startswith("win") + return platform.system() == "Windows" diff --git a/monkey/monkey_island/cc/server_utils/common_methods.py b/monkey/monkey_island/cc/server_utils/common_methods.py deleted file mode 100644 index 218f146ed..000000000 --- a/monkey/monkey_island/cc/server_utils/common_methods.py +++ /dev/null @@ -1,11 +0,0 @@ -import platform - -WINDOWS = "Windows" -LINUX = "Linux" - - -def get_runtime_os() -> str: - if platform.system() == "Windows": - return WINDOWS - else: - return LINUX diff --git a/monkey/monkey_island/cc/setup/mongo_process_runner.py b/monkey/monkey_island/cc/setup/mongo_process_runner.py index bc429479e..71d3e6c81 100644 --- a/monkey/monkey_island/cc/setup/mongo_process_runner.py +++ b/monkey/monkey_island/cc/setup/mongo_process_runner.py @@ -3,7 +3,7 @@ import os import subprocess from typing import List -from monkey_island.cc.server_utils.common_methods import WINDOWS, get_runtime_os +from monkey_island.cc.environment.utils import is_windows_os from monkey_island.cc.server_utils.consts import ( MONGO_EXECUTABLE_PATH_LINUX, MONGO_EXECUTABLE_PATH_WIN, @@ -53,7 +53,7 @@ class MongoDbRunner: @staticmethod def _get_path_of_mongo_exec(): - if get_runtime_os() == WINDOWS: + if is_windows_os(): return MONGO_EXECUTABLE_PATH_WIN else: return MONGO_EXECUTABLE_PATH_LINUX From 5aeab3a56c901951349aa482bd32cec3db8017f4 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Wed, 26 May 2021 16:56:08 +0300 Subject: [PATCH 14/18] Refactored mongo executable path to be calculated in consts, since this is a trivial calculation. --- monkey/monkey_island/cc/server_utils/consts.py | 11 +++++++---- .../cc/setup/mongo_process_runner.py | 16 ++-------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index 9bc1e7059..5cc9a0dd1 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -20,13 +20,16 @@ DEFAULT_DATA_DIR = os.path.expandvars(get_default_data_dir()) DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5 +_MONGO_BINARY_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "bin", "mongodb") +_MONGO_EXECUTABLE_PATH_WIN = os.path.join(_MONGO_BINARY_DIR, "mongod.exe") +_MONGO_EXECUTABLE_PATH_LINUX = os.path.join(_MONGO_BINARY_DIR, "bin", "mongod") +MONGO_EXECUTABLE_PATH = ( + _MONGO_EXECUTABLE_PATH_WIN if is_windows_os() else _MONGO_EXECUTABLE_PATH_LINUX +) + DEFAULT_SERVER_CONFIG_PATH = os.path.expandvars( os.path.join(DEFAULT_DATA_DIR, SERVER_CONFIG_FILENAME) ) -_MONGO_EXECUTABLE_PATH = os.path.join(MONKEY_ISLAND_ABS_PATH, "bin", "mongodb") -MONGO_EXECUTABLE_PATH_WIN = os.path.join(_MONGO_EXECUTABLE_PATH, "mongod.exe") -MONGO_EXECUTABLE_PATH_LINUX = os.path.join(_MONGO_EXECUTABLE_PATH, "bin", "mongod") - DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( MONKEY_ISLAND_ABS_PATH, "cc", f"{SERVER_CONFIG_FILENAME}.develop" diff --git a/monkey/monkey_island/cc/setup/mongo_process_runner.py b/monkey/monkey_island/cc/setup/mongo_process_runner.py index 71d3e6c81..ab26be8de 100644 --- a/monkey/monkey_island/cc/setup/mongo_process_runner.py +++ b/monkey/monkey_island/cc/setup/mongo_process_runner.py @@ -3,11 +3,7 @@ import os import subprocess from typing import List -from monkey_island.cc.environment.utils import is_windows_os -from monkey_island.cc.server_utils.consts import ( - MONGO_EXECUTABLE_PATH_LINUX, - MONGO_EXECUTABLE_PATH_WIN, -) +from monkey_island.cc.server_utils.consts import MONGO_EXECUTABLE_PATH logger = logging.getLogger(__name__) @@ -39,9 +35,8 @@ class MongoDbRunner: def _start_mongodb_process(self, db_dir_path: str): logger.info("Starting MongoDb process.") - mongo_exec = MongoDbRunner._get_path_of_mongo_exec() - mongo_run_cmd = MongoDbRunner._build_mongo_launch_cmd(mongo_exec, db_dir_path) + mongo_run_cmd = MongoDbRunner._build_mongo_launch_cmd(MONGO_EXECUTABLE_PATH, db_dir_path) logger.info(f"Mongodb will be launched with command: f{' '.join(mongo_run_cmd)}.") mongo_log_path = os.path.join(self.logging_dir_path, MONGO_LOG_FILENAME) @@ -51,13 +46,6 @@ class MongoDbRunner: subprocess.Popen(mongo_run_cmd, stderr=subprocess.STDOUT, stdout=log) logger.info("MongoDb launched successfully!") - @staticmethod - def _get_path_of_mongo_exec(): - if is_windows_os(): - return MONGO_EXECUTABLE_PATH_WIN - else: - return MONGO_EXECUTABLE_PATH_LINUX - @staticmethod def _build_mongo_launch_cmd(exec_path: str, db_path: str) -> List[str]: return [exec_path, DB_DIR_PARAM, db_path] From cb14a4ea9b8d3f658df610936c2c830e81d5207f Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 27 May 2021 10:36:37 +0300 Subject: [PATCH 15/18] Refactored secure directory creation into a separate method. Data dir creation and db dir creation now use that method. Added unit tests for secure directory creation. --- .../cc/environment/linux_permissions.py | 7 +++ monkey/monkey_island/cc/environment/utils.py | 43 +++++++++++++++ .../cc/environment/windows_permissions.py | 11 ++-- .../cc/setup/mongo_process_runner.py | 5 +- .../cc/environment/test_utils.py | 52 +++++++++++++++++++ .../cc/setup/test_mongo_process_runner.py | 32 ------------ 6 files changed, 108 insertions(+), 42 deletions(-) create mode 100644 monkey/monkey_island/cc/environment/linux_permissions.py create mode 100644 monkey/tests/unit_tests/monkey_island/cc/environment/test_utils.py delete mode 100644 monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py diff --git a/monkey/monkey_island/cc/environment/linux_permissions.py b/monkey/monkey_island/cc/environment/linux_permissions.py new file mode 100644 index 000000000..2280c7637 --- /dev/null +++ b/monkey/monkey_island/cc/environment/linux_permissions.py @@ -0,0 +1,7 @@ +import os +import stat + + +def set_perms_to_owner_only(path: str): + # Read, write, and execute by owner + os.chmod(path, stat.S_IRWXU) diff --git a/monkey/monkey_island/cc/environment/utils.py b/monkey/monkey_island/cc/environment/utils.py index 585b2cc79..7d74e4f2b 100644 --- a/monkey/monkey_island/cc/environment/utils.py +++ b/monkey/monkey_island/cc/environment/utils.py @@ -1,5 +1,48 @@ +import logging +import os import platform def is_windows_os() -> bool: return platform.system() == "Windows" + + +if is_windows_os(): + from monkey_island.cc.environment.windows_permissions import ( # noqa: E402 + set_full_folder_access, + ) +else: + from monkey_island.cc.environment.linux_permissions import set_perms_to_owner_only # noqa: E402 + +LOG = logging.getLogger(__name__) + + +def create_secure_directory(path: str, create_parent_dirs: bool): + if not os.path.isdir(path): + create_directory(path, create_parent_dirs) + set_secure_permissions(path) + + +def create_directory(path: str, create_parent_dirs: bool): + try: + if create_parent_dirs: + os.makedirs(path) + else: + os.mkdir(path) + except Exception as ex: + LOG.error( + f'Could not create a directory at "{path}" (maybe `$HOME` could not be ' + f"resolved?): {str(ex)}" + ) + raise ex + + +def set_secure_permissions(dir_path: str): + try: + if is_windows_os(): + set_full_folder_access(folder_path=dir_path) + else: + set_perms_to_owner_only(path=dir_path) + except Exception as ex: + LOG.error(f"Permissions could not be " f"set successfully for {dir_path}: {str(ex)}") + raise ex diff --git a/monkey/monkey_island/cc/environment/windows_permissions.py b/monkey/monkey_island/cc/environment/windows_permissions.py index d17947a2e..5d4913151 100644 --- a/monkey/monkey_island/cc/environment/windows_permissions.py +++ b/monkey/monkey_island/cc/environment/windows_permissions.py @@ -1,10 +1,7 @@ -from monkey_island.cc.environment.utils import is_windows_os - -if is_windows_os(): - import ntsecuritycon - import win32api - import win32con - import win32security +import ntsecuritycon +import win32api +import win32con +import win32security def set_full_folder_access(folder_path: str) -> None: diff --git a/monkey/monkey_island/cc/setup/mongo_process_runner.py b/monkey/monkey_island/cc/setup/mongo_process_runner.py index ab26be8de..d725bef75 100644 --- a/monkey/monkey_island/cc/setup/mongo_process_runner.py +++ b/monkey/monkey_island/cc/setup/mongo_process_runner.py @@ -3,6 +3,7 @@ import os import subprocess from typing import List +from monkey_island.cc.environment.utils import create_secure_directory from monkey_island.cc.server_utils.consts import MONGO_EXECUTABLE_PATH logger = logging.getLogger(__name__) @@ -28,9 +29,7 @@ class MongoDbRunner: def _create_db_dir(self) -> str: db_path = os.path.join(self.db_dir_parent_path, DB_DIR_NAME) logger.info(f"Database content directory: {db_path}.") - if not os.path.isdir(db_path): - logger.info("Database content directory not found, creating one.") - os.mkdir(os.path.join(self.db_dir_parent_path, DB_DIR_NAME)) + create_secure_directory(db_path) return db_path def _start_mongodb_process(self, db_dir_path: str): diff --git a/monkey/tests/unit_tests/monkey_island/cc/environment/test_utils.py b/monkey/tests/unit_tests/monkey_island/cc/environment/test_utils.py new file mode 100644 index 000000000..c2d7baeef --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/environment/test_utils.py @@ -0,0 +1,52 @@ +import os +import shutil +import stat + +import pytest + +from monkey_island.cc.environment.utils import create_secure_directory, is_windows_os + + +@pytest.fixture +def test_path_nested(tmpdir): + nested_path = "/test1/test2/test3" + path = os.path.join(tmpdir, nested_path) + yield path + try: + shutil.rmtree(os.path.join(tmpdir, "/test1")) + except Exception: + pass + + +@pytest.fixture +def test_path(tmpdir): + test_path = "/test1" + path = os.path.join(tmpdir, test_path) + yield path + try: + shutil.rmtree(path) + except Exception: + pass + + +def test_create_secure_directory__parent_dirs(test_path_nested): + create_secure_directory(test_path_nested, create_parent_dirs=True) + assert os.path.isdir(test_path_nested) + + +def test_create_secure_directory__already_created(test_path): + os.mkdir(test_path) + assert os.path.isdir(test_path) + create_secure_directory(test_path, create_parent_dirs=False) + + +def test_create_secure_directory__no_parent_dir(test_path_nested): + with pytest.raises(Exception): + create_secure_directory(test_path_nested, create_parent_dirs=False) + + +@pytest.mark.skipif(is_windows_os(), reason="Tests Posix (not Windows) permissions.") +def test_create_secure_directory__perm_linux(test_path_nested): + create_secure_directory(test_path_nested, create_parent_dirs=True) + st = os.stat(test_path_nested) + return bool(st.st_mode & stat.S_IRWXU) diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py deleted file mode 100644 index 5a8ac75f8..000000000 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_mongo_process_runner.py +++ /dev/null @@ -1,32 +0,0 @@ -import os - -import pytest - -from monkey_island.cc.setup.mongo_process_runner import MongoDbRunner - -TEST_DIR_NAME = "test_dir" - - -@pytest.fixture(autouse=True) -def fake_db_dir(monkeypatch): - monkeypatch.setattr("monkey_island.cc.setup.mongo_process_runner.DB_DIR_NAME", TEST_DIR_NAME) - - -@pytest.fixture -def expected_path(tmpdir): - expected_path = os.path.join(tmpdir, TEST_DIR_NAME) - return expected_path - - -def test_create_db_dir(tmpdir, expected_path): - db_path = MongoDbRunner(tmpdir, tmpdir)._create_db_dir() - assert os.path.isdir(expected_path) - assert db_path == expected_path - - -def test_create_db_dir__already_created(tmpdir, expected_path): - os.mkdir(expected_path) - - db_path = MongoDbRunner(tmpdir, tmpdir)._create_db_dir() - assert os.path.isdir(expected_path) - assert db_path == expected_path From 7240d60342589057d8d6e8ed24a45b80bfba3ae3 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 27 May 2021 10:40:11 +0300 Subject: [PATCH 16/18] Typos and small bugfixes in mongo_process_runner.py --- monkey/monkey_island/cc/setup/mongo_process_runner.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/monkey_island/cc/setup/mongo_process_runner.py b/monkey/monkey_island/cc/setup/mongo_process_runner.py index d725bef75..d03b62913 100644 --- a/monkey/monkey_island/cc/setup/mongo_process_runner.py +++ b/monkey/monkey_island/cc/setup/mongo_process_runner.py @@ -29,17 +29,17 @@ class MongoDbRunner: def _create_db_dir(self) -> str: db_path = os.path.join(self.db_dir_parent_path, DB_DIR_NAME) logger.info(f"Database content directory: {db_path}.") - create_secure_directory(db_path) + create_secure_directory(db_path, create_parent_dirs=False) return db_path def _start_mongodb_process(self, db_dir_path: str): logger.info("Starting MongoDb process.") mongo_run_cmd = MongoDbRunner._build_mongo_launch_cmd(MONGO_EXECUTABLE_PATH, db_dir_path) - logger.info(f"Mongodb will be launched with command: f{' '.join(mongo_run_cmd)}.") + logger.info(f"Mongodb will be launched with command: {' '.join(mongo_run_cmd)}.") mongo_log_path = os.path.join(self.logging_dir_path, MONGO_LOG_FILENAME) - logger.info(f"Mongodb log will be available at f{mongo_log_path}.") + logger.info(f"Mongodb log will be available at {mongo_log_path}.") with open(mongo_log_path, "w") as log: subprocess.Popen(mongo_run_cmd, stderr=subprocess.STDOUT, stdout=log) From 4b733ba383cabfd8138568c66c1e21c6cae41a81 Mon Sep 17 00:00:00 2001 From: Shreya Date: Thu, 27 May 2021 15:40:47 +0530 Subject: [PATCH 17/18] Fix unit tests (test_utils.py) --- .../unit_tests/monkey_island/cc/environment/test_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/environment/test_utils.py b/monkey/tests/unit_tests/monkey_island/cc/environment/test_utils.py index c2d7baeef..fa2c4202b 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/environment/test_utils.py +++ b/monkey/tests/unit_tests/monkey_island/cc/environment/test_utils.py @@ -9,18 +9,18 @@ from monkey_island.cc.environment.utils import create_secure_directory, is_windo @pytest.fixture def test_path_nested(tmpdir): - nested_path = "/test1/test2/test3" + nested_path = "test1/test2/test3" path = os.path.join(tmpdir, nested_path) yield path try: - shutil.rmtree(os.path.join(tmpdir, "/test1")) + shutil.rmtree(os.path.join(tmpdir, "test1")) except Exception: pass @pytest.fixture def test_path(tmpdir): - test_path = "/test1" + test_path = "test1" path = os.path.join(tmpdir, test_path) yield path try: From 26e57153dab5a09cbe344b4d7ed1fa08bcc19411 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 27 May 2021 14:08:51 +0300 Subject: [PATCH 18/18] Fixed typos and renamed windows permission setting function in windows_permissions.py to be more specific. --- .../cc/environment/data_dir_generator.py | 30 ------------------- monkey/monkey_island/cc/environment/utils.py | 14 ++++----- .../cc/environment/windows_permissions.py | 2 +- monkey/monkey_island/setup/config_setup.py | 7 ++--- 4 files changed, 10 insertions(+), 43 deletions(-) delete mode 100644 monkey/monkey_island/cc/environment/data_dir_generator.py diff --git a/monkey/monkey_island/cc/environment/data_dir_generator.py b/monkey/monkey_island/cc/environment/data_dir_generator.py deleted file mode 100644 index 58e16d4b7..000000000 --- a/monkey/monkey_island/cc/environment/data_dir_generator.py +++ /dev/null @@ -1,30 +0,0 @@ -import logging -import os - -from monkey_island.cc.environment.utils import is_windows_os -from monkey_island.cc.environment.windows_permissions import set_full_folder_access - -LOG = logging.getLogger(__name__) - - -def create_data_dir(data_dir: str, create_parent_dirs: bool) -> None: - if not os.path.isdir(data_dir): - try: - if create_parent_dirs: - os.makedirs(data_dir, mode=0o700) - else: - os.mkdir(data_dir, mode=0o700) - except Exception as ex: - LOG.error( - f'Could not create data directory at "{data_dir}" (maybe `$HOME` could not be ' - f"resolved?): {str(ex)}" - ) - - if is_windows_os(): # `mode=0o700` doesn't work on Windows - try: - set_full_folder_access(folder_path=data_dir) - except Exception as ex: - LOG.error( - f'Data directory was created at "{data_dir}" but permissions could not be ' - f"set successfully: {str(ex)}" - ) diff --git a/monkey/monkey_island/cc/environment/utils.py b/monkey/monkey_island/cc/environment/utils.py index 7d74e4f2b..907e30d47 100644 --- a/monkey/monkey_island/cc/environment/utils.py +++ b/monkey/monkey_island/cc/environment/utils.py @@ -8,11 +8,9 @@ def is_windows_os() -> bool: if is_windows_os(): - from monkey_island.cc.environment.windows_permissions import ( # noqa: E402 - set_full_folder_access, - ) + import monkey_island.cc.environment.windows_permissions as windows_permissions else: - from monkey_island.cc.environment.linux_permissions import set_perms_to_owner_only # noqa: E402 + import monkey_island.cc.environment.linux_permissions as linux_permissions # noqa: E402 LOG = logging.getLogger(__name__) @@ -31,7 +29,7 @@ def create_directory(path: str, create_parent_dirs: bool): os.mkdir(path) except Exception as ex: LOG.error( - f'Could not create a directory at "{path}" (maybe `$HOME` could not be ' + f'Could not create a directory at "{path}" (maybe environmental variables could not be ' f"resolved?): {str(ex)}" ) raise ex @@ -40,9 +38,9 @@ def create_directory(path: str, create_parent_dirs: bool): def set_secure_permissions(dir_path: str): try: if is_windows_os(): - set_full_folder_access(folder_path=dir_path) + windows_permissions.set_perms_to_owner_only(folder_path=dir_path) else: - set_perms_to_owner_only(path=dir_path) + linux_permissions.set_perms_to_owner_only(path=dir_path) except Exception as ex: - LOG.error(f"Permissions could not be " f"set successfully for {dir_path}: {str(ex)}") + LOG.error(f"Permissions could not be set successfully for {dir_path}: {str(ex)}") raise ex diff --git a/monkey/monkey_island/cc/environment/windows_permissions.py b/monkey/monkey_island/cc/environment/windows_permissions.py index 5d4913151..225e52370 100644 --- a/monkey/monkey_island/cc/environment/windows_permissions.py +++ b/monkey/monkey_island/cc/environment/windows_permissions.py @@ -4,7 +4,7 @@ import win32con import win32security -def set_full_folder_access(folder_path: str) -> None: +def set_perms_to_owner_only(folder_path: str) -> None: user = get_user_pySID_object() security_descriptor = win32security.GetFileSecurity( diff --git a/monkey/monkey_island/setup/config_setup.py b/monkey/monkey_island/setup/config_setup.py index 5c9625ac4..50330aea3 100644 --- a/monkey/monkey_island/setup/config_setup.py +++ b/monkey/monkey_island/setup/config_setup.py @@ -2,7 +2,7 @@ import os from typing import Tuple from monkey_island.cc.environment import server_config_handler -from monkey_island.cc.environment.data_dir_generator import create_data_dir # noqa: E402 +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.setup.island_config_options import IslandConfigOptions @@ -10,14 +10,13 @@ from monkey_island.setup.island_config_options import IslandConfigOptions def setup_config_by_cmd_arg(server_config_path) -> Tuple[IslandConfigOptions, str]: server_config_path = os.path.expandvars(os.path.expanduser(server_config_path)) config = server_config_handler.load_server_config_from_file(server_config_path) - - create_data_dir(config.data_dir, create_parent_dirs=True) + create_secure_directory(config.data_dir, create_parent_dirs=True) return config, server_config_path def setup_default_config() -> Tuple[IslandConfigOptions, str]: server_config_path = DEFAULT_SERVER_CONFIG_PATH - create_data_dir(DEFAULT_DATA_DIR, create_parent_dirs=False) + 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) return config, server_config_path