From 2d971d95fc3b81ceecd1ca2864c41b8893b756b5 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 11 Feb 2021 07:27:56 -0500 Subject: [PATCH] build: select server and logger config at runtime in appimage --- deployment_scripts/appimage/build_appimage.sh | 11 +++++-- .../appimage/island_logger_config.json | 33 +++++++++++++++++++ .../appimage/monkey_island_builder.yml | 2 +- .../appimage/{run.sh => run_appimage.sh} | 16 +++++++-- .../cc/environment/environment_singleton.py | 2 ++ 5 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 deployment_scripts/appimage/island_logger_config.json rename deployment_scripts/appimage/{run.sh => run_appimage.sh} (51%) diff --git a/deployment_scripts/appimage/build_appimage.sh b/deployment_scripts/appimage/build_appimage.sh index 407df9bb7..d3a0fab47 100755 --- a/deployment_scripts/appimage/build_appimage.sh +++ b/deployment_scripts/appimage/build_appimage.sh @@ -94,7 +94,7 @@ clone_monkey_repo() { fi log_message "Cloning files from git" - branch=${2:-"develop"} + branch=${2:-"select-logger-config-at-runtime"} git clone --single-branch --recurse-submodules -b "$branch" "${MONKEY_GIT_URL}" "${REPO_MONKEY_HOME}" 2>&1 || handle_error chmod 774 -R "${MONKEY_HOME}" @@ -105,7 +105,12 @@ copy_monkey_island_to_appdir() { cp $REPO_MONKEY_SRC/monkey_island.py $INSTALL_DIR cp -r $REPO_MONKEY_SRC/common $INSTALL_DIR cp -r $REPO_MONKEY_SRC/monkey_island $INSTALL_DIR - cp ./run.sh $INSTALL_DIR/monkey_island/linux/ + cp ./run_appimage.sh $INSTALL_DIR/monkey_island/linux/ + cp ./island_logger_config.json $INSTALL_DIR/ + + # TODO: This is a workaround that may be able to be removed after PR #848 is + # merged. See monkey_island/cc/environment_singleton.py for more information. + cp $INSTALL_DIR/monkey_island/cc/server_config.json.standard $INSTALL_DIR/monkey_island/cc/server_config.json } install_monkey_island_python_dependencies() { @@ -231,7 +236,7 @@ cp $REPO_MONKEY_SRC/monkey_island/cc/ui/src/images/monkey-icon.svg $APPDIR/usr/s #cp ./monkey_island.desktop $APPDIR log_message "Building AppImage" -appimage-builder --recipe monkey_island_builder.yml --skip-appimage +appimage-builder --recipe monkey_island_builder.yml log_message "Deployment script finished." diff --git a/deployment_scripts/appimage/island_logger_config.json b/deployment_scripts/appimage/island_logger_config.json new file mode 100644 index 000000000..4acf875e3 --- /dev/null +++ b/deployment_scripts/appimage/island_logger_config.json @@ -0,0 +1,33 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters": { + "simple": { + "format": "%(asctime)s - %(filename)s:%(lineno)s - %(funcName)10s() - %(levelname)s - %(message)s" + } + }, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "level": "DEBUG", + "formatter": "simple", + "stream": "ext://sys.stdout" + }, + "info_file_handler": { + "class": "logging.handlers.RotatingFileHandler", + "level": "INFO", + "formatter": "simple", + "filename": "~/.monkey_island/info.log", + "maxBytes": 10485760, + "backupCount": 20, + "encoding": "utf8" + } + }, + "root": { + "level": "DEBUG", + "handlers": [ + "console", + "info_file_handler" + ] + } +} diff --git a/deployment_scripts/appimage/monkey_island_builder.yml b/deployment_scripts/appimage/monkey_island_builder.yml index f0848211a..6b77c7437 100644 --- a/deployment_scripts/appimage/monkey_island_builder.yml +++ b/deployment_scripts/appimage/monkey_island_builder.yml @@ -9,7 +9,7 @@ AppDir: icon: monkey-icon version: 1.10.0 exec: bin/bash - exec_args: "$APPDIR/usr/src/monkey_island/linux/run.sh $HOME/.monkey_island/db" + exec_args: "$APPDIR/usr/src/monkey_island/linux/run_appimage.sh" apt: diff --git a/deployment_scripts/appimage/run.sh b/deployment_scripts/appimage/run_appimage.sh similarity index 51% rename from deployment_scripts/appimage/run.sh rename to deployment_scripts/appimage/run_appimage.sh index 328535e78..17c8802b2 100644 --- a/deployment_scripts/appimage/run.sh +++ b/deployment_scripts/appimage/run_appimage.sh @@ -1,5 +1,14 @@ #!/bin/bash +DOT_MONKEY=$HOME/.monkey_island/ + +configure_default_logging() { + if [ ! -f $DOT_MONKEY/island_logger_config.json ]; then + cp $APPDIR/usr/src/island_logger_config.json $DOT_MONKEY + fi +} + + # Detecting command that calls python 3.7 python_cmd="" if [[ $(python --version 2>&1) == *"Python 3.7"* ]]; then @@ -12,10 +21,13 @@ if [[ $(python3.7 --version 2>&1) == *"Python 3.7"* ]]; then python_cmd="python3.7" fi -DB_DIR=${1:-"./monkey_island/bin/mongodb/db"} +mkdir -p $DOT_MONKEY +DB_DIR=$DOT_MONKEY/db mkdir -p $DB_DIR +configure_default_logging + cd $APPDIR/usr/src ./monkey_island/bin/mongodb/bin/mongod --dbpath $DB_DIR & -${python_cmd} ./monkey_island.py +${python_cmd} ./monkey_island.py --server-config $DOT_MONKEY/server_config.json --logger-config $DOT_MONKEY/island_logger_config.json diff --git a/monkey/monkey_island/cc/environment/environment_singleton.py b/monkey/monkey_island/cc/environment/environment_singleton.py index accb104ce..01e83096d 100644 --- a/monkey/monkey_island/cc/environment/environment_singleton.py +++ b/monkey/monkey_island/cc/environment/environment_singleton.py @@ -51,4 +51,6 @@ def initialize_from_file(file_path): raise +# TODO: This is only needed so that unit tests pass. After PR #848 is merged, we may be +# able to remove this line. initialize_from_file(DEFAULT_SERVER_CONFIG_PATH)