From 9dad253fccee8374d63804e2c45b2dd7847f81cb Mon Sep 17 00:00:00 2001
From: VakarisZ <vakarisz@yahoo.com>
Date: Thu, 14 Oct 2021 09:53:50 +0300
Subject: [PATCH] Island: create data_dir.py where data dir setup logic will be
 held

---
 monkey/monkey_island/cc/server_setup.py       |  2 +-
 monkey/monkey_island/cc/setup/config_setup.py | 16 ++++++++++++----
 monkey/monkey_island/cc/setup/data_dir.py     |  5 +++++
 3 files changed, 18 insertions(+), 5 deletions(-)
 create mode 100644 monkey/monkey_island/cc/setup/data_dir.py

diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py
index fdb94b67f..e3e8462a8 100644
--- a/monkey/monkey_island/cc/server_setup.py
+++ b/monkey/monkey_island/cc/server_setup.py
@@ -61,7 +61,7 @@ def run_monkey_island():
 
 def _setup_data_dir(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, str]:
     try:
-        return config_setup.setup_data_dir(island_args)
+        return config_setup.setup_server_config(island_args)
     except OSError as ex:
         print(f"Error opening server config file: {ex}")
         exit(1)
diff --git a/monkey/monkey_island/cc/setup/config_setup.py b/monkey/monkey_island/cc/setup/config_setup.py
index 3d241b748..ec9a1637d 100644
--- a/monkey/monkey_island/cc/setup/config_setup.py
+++ b/monkey/monkey_island/cc/setup/config_setup.py
@@ -4,11 +4,11 @@ from common.utils.file_utils import expand_path
 from monkey_island.cc.arg_parser import IslandCmdArgs
 from monkey_island.cc.environment import server_config_handler
 from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH
-from monkey_island.cc.server_utils.file_utils import create_secure_directory
+from monkey_island.cc.setup.data_dir import setup_data_dir
 from monkey_island.cc.setup.island_config_options import IslandConfigOptions
 
 
-def setup_data_dir(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, str]:
+def setup_server_config(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, str]:
     if island_args.server_config_path:
         return _setup_config_by_cmd_arg(island_args.server_config_path)
 
@@ -18,7 +18,12 @@ def setup_data_dir(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, str
 def _setup_config_by_cmd_arg(server_config_path) -> Tuple[IslandConfigOptions, str]:
     server_config_path = expand_path(server_config_path)
     config = server_config_handler.load_server_config_from_file(server_config_path)
-    create_secure_directory(str(config.data_dir))
+
+    # TODO refactor like in https://github.com/guardicore/monkey/pull/1528 because
+    # there's absolutely no reason to be exposed to IslandConfigOptions extraction logic
+    # if you want to modify data directory related code.
+    setup_data_dir(str(config.data_dir))
+
     return config, server_config_path
 
 
@@ -26,7 +31,10 @@ def _setup_default_config() -> Tuple[IslandConfigOptions, str]:
     default_config = server_config_handler.load_server_config_from_file(DEFAULT_SERVER_CONFIG_PATH)
     default_data_dir = default_config.data_dir
 
-    create_secure_directory(str(default_data_dir))
+    # TODO refactor like in https://github.com/guardicore/monkey/pull/1528 because
+    # there's absolutely no reason to be exposed to IslandConfigOptions extraction logic
+    # if you want to modify data directory related code.
+    setup_data_dir(str(default_data_dir))
 
     server_config_path = server_config_handler.create_default_server_config_file(default_data_dir)
     config = server_config_handler.load_server_config_from_file(server_config_path)
diff --git a/monkey/monkey_island/cc/setup/data_dir.py b/monkey/monkey_island/cc/setup/data_dir.py
new file mode 100644
index 000000000..c95bc897d
--- /dev/null
+++ b/monkey/monkey_island/cc/setup/data_dir.py
@@ -0,0 +1,5 @@
+from monkey_island.cc.server_utils.file_utils import create_secure_directory
+
+
+def setup_data_dir(path: str):
+    create_secure_directory(path)