Created code that generates default server_config.json, so we don't need to track this file on git and remember not to commit credentials.

This commit is contained in:
VakarisZ 2020-08-14 15:43:06 +03:00
parent 39f187b936
commit 05395fee6d
6 changed files with 20 additions and 12 deletions

2
.gitignore vendored
View File

@ -91,7 +91,7 @@ profiler_logs/
# vim swap files # vim swap files
*.swp *.swp
# Server config might contain credentials. Don't commit by default. # Server config might contain credentials
/monkey/monkey_island/cc/server_config.json /monkey/monkey_island/cc/server_config.json
# Virtualenv # Virtualenv

View File

@ -50,7 +50,7 @@ install:
before_script: before_script:
# Set the server config to `testing`. This is required for for the UTs to pass. # 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: script:
# Check Python code # Check Python code

View File

@ -2,12 +2,17 @@ from __future__ import annotations
import json import json
import os import os
from pathlib import Path
from typing import Dict, List from typing import Dict, List
from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.environment.user_creds import UserCreds 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.auth_user import User
from monkey_island.cc.resources.auth.user_store import UserStore 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: class EnvironmentConfig:
@ -43,13 +48,15 @@ class EnvironmentConfig:
@staticmethod @staticmethod
def get_from_file() -> EnvironmentConfig: def get_from_file() -> EnvironmentConfig:
file_path = EnvironmentConfig.get_config_file_path() 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: with open(file_path, 'r') as f:
config_content = f.read() config_content = f.read()
return EnvironmentConfig.get_from_json(config_content) return EnvironmentConfig.get_from_json(config_content)
@staticmethod @staticmethod
def get_config_file_path() -> str: 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: def to_dict(self) -> Dict:
config_dict = {'server_config': self.server_config, config_dict = {'server_config': self.server_config,

View File

@ -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)

View File

@ -1,7 +1,8 @@
import argparse import argparse
import json import json
import logging import logging
from pathlib import Path
from monkey_island.cc.environment.environment_config import EnvironmentConfig
SERVER_CONFIG = "server_config" SERVER_CONFIG = "server_config"
@ -12,7 +13,7 @@ logger.setLevel(logging.DEBUG)
def main(): def main():
args = parse_args() args = parse_args()
file_path = get_config_file_path(args) file_path = EnvironmentConfig.get_config_file_path()
# Read config # Read config
with open(file_path) as config_file: 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. 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(): def parse_args():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("server_config", choices=["standard", "testing", "password"]) 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() args = parser.parse_args()
return args return args