forked from p15670423/monkey
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:
parent
39f187b936
commit
05395fee6d
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
|
@ -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
|
||||
|
Loading…
Reference in New Issue