diff --git a/.gitignore b/.gitignore index 2f48a6781..76e08185b 100644 --- a/.gitignore +++ b/.gitignore @@ -91,7 +91,7 @@ profiler_logs/ # vim swap files *.swp -# Server config might contain credentials. Don't commit by default. +# Server config might contain credentials /monkey/monkey_island/cc/server_config.json # Virtualenv diff --git a/.travis.yml b/.travis.yml index d1178458b..0200ca8e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -50,7 +50,7 @@ install: before_script: # Set the server config to `testing`. This is required for for the UTs to pass. -- python monkey/monkey_island/cc/set_server_config.py testing +- python monkey/monkey_island/cc/environment/set_server_config.py testing script: # Check Python code diff --git a/monkey/monkey_island/cc/environment/environment_config.py b/monkey/monkey_island/cc/environment/environment_config.py index 0c66b2fc4..cf54d049e 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -2,12 +2,17 @@ from __future__ import annotations import json import os +from pathlib import Path from typing import Dict, List from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH from monkey_island.cc.environment.user_creds import UserCreds from monkey_island.cc.resources.auth.auth_user import User from monkey_island.cc.resources.auth.user_store import UserStore +import monkey_island.cc.environment.server_config_generator as server_config_generator + + +SERVER_CONFIG_FILENAME = "server_config.json" class EnvironmentConfig: @@ -43,13 +48,15 @@ class EnvironmentConfig: @staticmethod def get_from_file() -> EnvironmentConfig: file_path = EnvironmentConfig.get_config_file_path() + if not Path(file_path).is_file(): + server_config_generator.create_default_config_file(file_path) with open(file_path, 'r') as f: config_content = f.read() return EnvironmentConfig.get_from_json(config_content) @staticmethod def get_config_file_path() -> str: - return os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', 'server_config.json') + return os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', SERVER_CONFIG_FILENAME) def to_dict(self) -> Dict: config_dict = {'server_config': self.server_config, diff --git a/monkey/monkey_island/cc/environment/server_config_generator.py b/monkey/monkey_island/cc/environment/server_config_generator.py new file mode 100644 index 000000000..d5c645564 --- /dev/null +++ b/monkey/monkey_island/cc/environment/server_config_generator.py @@ -0,0 +1,7 @@ +from pathlib import Path + + +def create_default_config_file(path): + default_config_path = f"{path}.default" + default_config = Path(default_config_path).read_text() + Path(path).write_text(default_config) diff --git a/monkey/monkey_island/cc/set_server_config.py b/monkey/monkey_island/cc/environment/set_server_config.py similarity index 74% rename from monkey/monkey_island/cc/set_server_config.py rename to monkey/monkey_island/cc/environment/set_server_config.py index fc20747c9..359110ebc 100644 --- a/monkey/monkey_island/cc/set_server_config.py +++ b/monkey/monkey_island/cc/environment/set_server_config.py @@ -1,7 +1,8 @@ import argparse import json import logging -from pathlib import Path + +from monkey_island.cc.environment.environment_config import EnvironmentConfig SERVER_CONFIG = "server_config" @@ -12,7 +13,7 @@ logger.setLevel(logging.DEBUG) def main(): args = parse_args() - file_path = get_config_file_path(args) + file_path = EnvironmentConfig.get_config_file_path() # Read config with open(file_path) as config_file: @@ -28,16 +29,9 @@ def main(): config_file.write("\n") # Have to add newline at end of file, since json.dump does not. -def get_config_file_path(args): - file_path = Path(__file__).parent.joinpath(args.file_name) - logger.info("Config file path: {}".format(file_path)) - return file_path - - def parse_args(): parser = argparse.ArgumentParser() parser.add_argument("server_config", choices=["standard", "testing", "password"]) - parser.add_argument("-f", "--file_name", required=False, default="server_config.json") args = parser.parse_args() return args diff --git a/monkey/monkey_island/cc/server_config.json b/monkey/monkey_island/cc/server_config.json.default similarity index 100% rename from monkey/monkey_island/cc/server_config.json rename to monkey/monkey_island/cc/server_config.json.default