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..59a97f60c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,10 @@ python: os: linux +before_install: +# Init server_config.json to default +- cp monkey/monkey_island/cc/server_config.json.default monkey/monkey_island/cc/server_config.json + install: # Python - pip freeze @@ -50,7 +54,9 @@ 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 +- pushd /home/travis/build/guardicore/monkey/monkey +- python monkey_island/cc/environment/set_server_config.py testing +- popd 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..79182d56b 100644 --- a/monkey/monkey_island/cc/environment/environment_config.py +++ b/monkey/monkey_island/cc/environment/environment_config.py @@ -2,13 +2,17 @@ from __future__ import annotations import json import os +from pathlib import Path from typing import Dict, List +import monkey_island.cc.environment.server_config_generator as server_config_generator 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 +SERVER_CONFIG_FILENAME = "server_config.json" + class EnvironmentConfig: def __init__(self, @@ -43,13 +47,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 73% rename from monkey/monkey_island/cc/set_server_config.py rename to monkey/monkey_island/cc/environment/set_server_config.py index fc20747c9..d31dd6916 100644 --- a/monkey/monkey_island/cc/set_server_config.py +++ b/monkey/monkey_island/cc/environment/set_server_config.py @@ -1,8 +1,20 @@ import argparse import json import logging +import sys from pathlib import Path + +def add_monkey_dir_to_sys_path(): + monkey_path = Path(sys.path[0]) + monkey_path = monkey_path.parents[2] + sys.path.insert(0, monkey_path.__str__()) + + +add_monkey_dir_to_sys_path() + +from monkey_island.cc.environment.environment_config import EnvironmentConfig # noqa: E402 isort:skip + SERVER_CONFIG = "server_config" logger = logging.getLogger(__name__) @@ -12,7 +24,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 +40,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