Merge pull request #787 from VakarisZ/server_config_auto_generation

server_config.json no longer tracked in git, generated on island launch
This commit is contained in:
VakarisZ 2020-08-27 15:49:45 +03:00 committed by GitHub
commit ee429b00c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 11 deletions

2
.gitignore vendored
View File

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

View File

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

View File

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

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,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