From 6dfaf5ec74a80b33d21e93b607df1efe13cc4cbb Mon Sep 17 00:00:00 2001 From: EliaOnceAgain Date: Mon, 2 May 2022 02:29:02 +0300 Subject: [PATCH 01/18] Deploy: Move deployment_scripts/install-infection-monkey-service.sh to build_script/appimage/ --- .../appimage}/install-infection-monkey-service.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {deployment_scripts => build_scripts/appimage}/install-infection-monkey-service.sh (100%) diff --git a/deployment_scripts/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh similarity index 100% rename from deployment_scripts/install-infection-monkey-service.sh rename to build_scripts/appimage/install-infection-monkey-service.sh From 1c702b28ba62d3cc75c27b9b202f47346590e862 Mon Sep 17 00:00:00 2001 From: EliaOnceAgain Date: Mon, 2 May 2022 02:35:06 +0300 Subject: [PATCH 02/18] Build: Include install-infection-monkey-service.sh in the AppImage --- build_scripts/appimage/appimage.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/build_scripts/appimage/appimage.sh b/build_scripts/appimage/appimage.sh index e5c6b97bf..16652c081 100755 --- a/build_scripts/appimage/appimage.sh +++ b/build_scripts/appimage/appimage.sh @@ -38,6 +38,7 @@ setup_build_dir() { copy_monkey_island_to_build_dir "$monkey_repo/monkey" "$BUILD_DIR" copy_server_config_to_build_dir + copy_infection_monkey_service_to_build_dir modify_deployment "$deployment_type" "$BUILD_DIR" add_agent_binaries_to_build_dir "$agent_binary_dir" "$BUILD_DIR" @@ -64,8 +65,12 @@ setup_python_37_appdir() { rm "$PYTHON_APPIMAGE" } +copy_infection_monkey_service_to_build_dir() { + cp "$APPIMAGE_DIR"/install-infection-monkey-service.sh "$APPDIR" +} + copy_server_config_to_build_dir() { - cp "$APPIMAGE_DIR"/server_config.json.standard "$BUILD_DIR"/monkey_island/cc/server_config.json + cp "$APPIMAGE_DIR"/server_config.json.standard "$BUILD_DIR"/monkey_island/cc/server_config.json } install_monkey_island_python_dependencies() { From 02916a25f32b6c3b0a7c6762a170dba4635d80ad Mon Sep 17 00:00:00 2001 From: EliaOnceAgain Date: Mon, 2 May 2022 02:50:12 +0300 Subject: [PATCH 03/18] Build: Check for the 'service' argument in AppRun --- build_scripts/appimage/AppRun | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build_scripts/appimage/AppRun b/build_scripts/appimage/AppRun index 53ef0fa5e..1ed075693 100755 --- a/build_scripts/appimage/AppRun +++ b/build_scripts/appimage/AppRun @@ -13,6 +13,10 @@ export TKPATH="${TK_LIBRARY}" # Export SSL certificate export SSL_CERT_FILE="${APPDIR}/opt/_internal/certs.pem" +if [ "$1" == "service" ] ; then + exec /bin/bash "${APPDIR}/install-infection-monkey-service.sh" ${@:2} +fi + # Call the entry point for opt in "$@" do From 0287f58d8d5cce3874a796ed3eb341757426cce2 Mon Sep 17 00:00:00 2001 From: EliaOnceAgain Date: Mon, 2 May 2022 22:09:41 +0300 Subject: [PATCH 04/18] Build: Use $APPIMAGE instead of --path during service installation --- .../install-infection-monkey-service.sh | 43 ++++++++----------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index e2c9a926f..58f832a2d 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -2,6 +2,7 @@ set -e +SCRIPT_NAME="$(basename "${APPIMAGE}")" SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")" SYSTEMD_UNIT_FILENAME="infection-monkey.service" SYSTEMD_DIR="/lib/systemd/system" @@ -12,18 +13,17 @@ echo_help() { echo "Installs the Infection Monkey service to run on boot." echo "" echo "Usage:" - echo " install-infection-monkey-service.sh --user --appimage " - echo " install-infection-monkey-service.sh --uninstall" - echo " install-infection-monkey-service.sh -h|--help" + echo " ${SCRIPT_NAME} service --user " + echo " ${SCRIPT_NAME} service --uninstall" + echo " ${SCRIPT_NAME} service -h|--help" echo "" echo "Options:" - echo " --user User to run the service as" - echo " --appimage Path to AppImage" - echo " --uninstall Uninstall the Infection Monkey service" + echo " --user Install Infection Monkey service and run as User" + echo " --uninstall Uninstall Infection Monkey service" } install_service() { - move_appimage "$2" + move_appimage cat > "${SCRIPT_DIR}/${SYSTEMD_UNIT_FILENAME}" << EOF [Unit] @@ -65,9 +65,9 @@ uninstall_service() { move_appimage() { sudo mkdir --mode=0755 -p "${MONKEY_BIN}" - if [ "$1" != "${MONKEY_BIN}/${APPIMAGE_NAME}" ] ; then + if [ "${APPIMAGE}" != "${MONKEY_BIN}/${APPIMAGE_NAME}" ] ; then umask 022 - sudo cp "$appimage_path" "${MONKEY_BIN}/${APPIMAGE_NAME}" + sudo cp "${APPIMAGE}" "${MONKEY_BIN}/${APPIMAGE_NAME}" sudo chmod 755 "${MONKEY_BIN}/${APPIMAGE_NAME}" fi } @@ -100,21 +100,16 @@ exit_if_missing_argument() { do_uninstall=false uname="" -appimage_path="" while (( "$#" )); do case "$1" in --user) exit_if_missing_argument "$1" "$2" - uname=$2 shift 2 ;; - --appimage) - exit_if_missing_argument "$1" "$2" - - appimage_path=$2 - shift 2 + --install) + shift ;; --uninstall) do_uninstall=true @@ -137,25 +132,21 @@ Run \`sudo -v\`, enter your password, and then re-run this script." exit 1 fi +if [ -z "${APPIMAGE}" ] ; then + echo "Error: Missing 'APPIMAGE' environment variable. Try installing the Infection Monkey service through the AppImage" + exit 1 +fi + if $do_uninstall ; then uninstall_service exit 0 fi assert_parameter_supplied "--user" "$uname" -assert_parameter_supplied "--appimage" "$appimage_path" if ! user_exists "$uname" ; then echo "Error: User '$uname' does not exist" exit 1 fi -if [ ! -f "$appimage_path" ] ; then - if [ ! -f "${SCRIPT_DIR}/$appimage_path" ] ; then - echo "Error: AppImage '$appimage_path' does not exist" - exit 1 - fi - appimage_path="${SCRIPT_DIR}/$appimage_path" -fi - -install_service "$uname" "$appimage_path" +install_service "$uname" From ed58ca6950abc6b15e5725ca561ea6799a8a6b0e Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 09:21:19 -0400 Subject: [PATCH 05/18] Build: Rename $uname -> $username in AppImage installation script "uname" could refer to the `uname` command (short for unix name). "username" more clearly communicates the variable's purpose. --- .../appimage/install-infection-monkey-service.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 58f832a2d..2ecf6729d 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -99,13 +99,13 @@ exit_if_missing_argument() { } do_uninstall=false -uname="" +username="" while (( "$#" )); do case "$1" in --user) exit_if_missing_argument "$1" "$2" - uname=$2 + username=$2 shift 2 ;; --install) @@ -142,11 +142,11 @@ if $do_uninstall ; then exit 0 fi -assert_parameter_supplied "--user" "$uname" +assert_parameter_supplied "--user" "$username" -if ! user_exists "$uname" ; then - echo "Error: User '$uname' does not exist" +if ! user_exists "$username" ; then + echo "Error: User '$username' does not exist" exit 1 fi -install_service "$uname" +install_service "$username" From 6d50814c1971c9cfaf3c487124797676e138d4ba Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 09:29:46 -0400 Subject: [PATCH 06/18] Build: Make --install and --uninstall mutually exclusive --- build_scripts/appimage/install-infection-monkey-service.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 2ecf6729d..450fe0a60 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -99,6 +99,7 @@ exit_if_missing_argument() { } do_uninstall=false +do_install=false username="" while (( "$#" )); do @@ -109,6 +110,7 @@ while (( "$#" )); do shift 2 ;; --install) + do_install=true shift ;; --uninstall) @@ -137,6 +139,11 @@ if [ -z "${APPIMAGE}" ] ; then exit 1 fi +if $do_install && $do_uninstall ; then + echo "The --install and --uninstall flags are mutually exclusive." + exit 1 +fi + if $do_uninstall ; then uninstall_service exit 0 From 7e039f73ffaa388995cdb306a0f40f64f49b6b65 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 09:31:29 -0400 Subject: [PATCH 07/18] Build: Ensure either --install or --uninstall are specified --- .../install-infection-monkey-service.sh | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 450fe0a60..1b222781a 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -13,12 +13,13 @@ echo_help() { echo "Installs the Infection Monkey service to run on boot." echo "" echo "Usage:" - echo " ${SCRIPT_NAME} service --user " + echo " ${SCRIPT_NAME} service --install --user " echo " ${SCRIPT_NAME} service --uninstall" echo " ${SCRIPT_NAME} service -h|--help" echo "" echo "Options:" - echo " --user Install Infection Monkey service and run as User" + echo " --install Install the Infection Monkey service" + echo " --user Configure the Infection Monkey service to run as a specific user" echo " --uninstall Uninstall Infection Monkey service" } @@ -76,6 +77,7 @@ user_exists() { id -u "$1" &>/dev/null } +# s/parameter/argument assert_parameter_supplied() { if [ -z "$2" ] ; then echo "Error: missing required parameter '$1'" @@ -149,11 +151,17 @@ if $do_uninstall ; then exit 0 fi -assert_parameter_supplied "--user" "$username" +if $do_install ; then + assert_parameter_supplied "--user" "$username" -if ! user_exists "$username" ; then - echo "Error: User '$username' does not exist" - exit 1 + if ! user_exists "$username" ; then + echo "Error: User '$username' does not exist" + exit 1 + fi + + install_service "$username" + exit 0 fi -install_service "$username" +echo "You must specify either the --install or --uninstall flag" +exit 1 From 2a18e3e1bfab7688e5c791bd4f1136b0fc55c4e4 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 09:39:16 -0400 Subject: [PATCH 08/18] Build: Remove unnecessary assert_parameter_supplied() --- .../appimage/install-infection-monkey-service.sh | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 1b222781a..114ca5651 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -77,15 +77,6 @@ user_exists() { id -u "$1" &>/dev/null } -# s/parameter/argument -assert_parameter_supplied() { - if [ -z "$2" ] ; then - echo "Error: missing required parameter '$1'" - echo_help - exit 1 - fi -} - has_sudo() { # 0 true, 1 false sudo -nv > /dev/null 2>&1 @@ -152,8 +143,6 @@ if $do_uninstall ; then fi if $do_install ; then - assert_parameter_supplied "--user" "$username" - if ! user_exists "$username" ; then echo "Error: User '$username' does not exist" exit 1 From 02ba8361a97287747812658a61d3685ced10cb7f Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 09:42:16 -0400 Subject: [PATCH 09/18] Build: Extract function exit_if_user_doesnt_exist() --- .../appimage/install-infection-monkey-service.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 114ca5651..8baffc735 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -77,6 +77,13 @@ user_exists() { id -u "$1" &>/dev/null } +exit_if_user_doesnt_exist() { + if ! user_exists "$1" ; then + echo "Error: User '$1' does not exist" + exit 1 + fi +} + has_sudo() { # 0 true, 1 false sudo -nv > /dev/null 2>&1 @@ -99,6 +106,7 @@ while (( "$#" )); do case "$1" in --user) exit_if_missing_argument "$1" "$2" + exit_if_user_doesnt_exist "$2" username=$2 shift 2 ;; @@ -143,11 +151,6 @@ if $do_uninstall ; then fi if $do_install ; then - if ! user_exists "$username" ; then - echo "Error: User '$username' does not exist" - exit 1 - fi - install_service "$username" exit 0 fi From 27ac518f2e5e6adce1b7cdfe8593458bcf1e47d9 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 09:43:43 -0400 Subject: [PATCH 10/18] Build: Set umask before writing temporary systemd unit file --- build_scripts/appimage/install-infection-monkey-service.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 8baffc735..aa434318b 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -25,6 +25,7 @@ echo_help() { install_service() { move_appimage + umask 077 cat > "${SCRIPT_DIR}/${SYSTEMD_UNIT_FILENAME}" << EOF [Unit] @@ -40,7 +41,6 @@ ExecStart="${MONKEY_BIN}/${APPIMAGE_NAME}" WantedBy=multi-user.target EOF - umask 077 sudo mv "${SCRIPT_DIR}/${SYSTEMD_UNIT_FILENAME}" "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}" sudo systemctl enable "${SYSTEMD_UNIT_FILENAME}" &>/dev/null From a81462e6176c92332990f449188b2e39f9b41440 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 09:53:31 -0400 Subject: [PATCH 11/18] Build: Use writable location for temporary systemd unit file As set, the $SCRIPT_DIR was read-only (since the AppImage's squashfs is readonly). Use $PWD instead. --- .../appimage/install-infection-monkey-service.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index aa434318b..b09bf2001 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -3,8 +3,8 @@ set -e SCRIPT_NAME="$(basename "${APPIMAGE}")" -SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")" SYSTEMD_UNIT_FILENAME="infection-monkey.service" +TMP_SYSTEMD_UNIT="${PWD}/${SYSTEMD_UNIT_FILENAME}" SYSTEMD_DIR="/lib/systemd/system" MONKEY_BIN="/opt/infection-monkey/bin" APPIMAGE_NAME="InfectionMonkey.AppImage" @@ -25,9 +25,9 @@ echo_help() { install_service() { move_appimage - umask 077 - cat > "${SCRIPT_DIR}/${SYSTEMD_UNIT_FILENAME}" << EOF + umask 077 + cat > "${TMP_SYSTEMD_UNIT}" << EOF [Unit] Description=Infection Monkey Runner After=network.target @@ -41,7 +41,7 @@ ExecStart="${MONKEY_BIN}/${APPIMAGE_NAME}" WantedBy=multi-user.target EOF - sudo mv "${SCRIPT_DIR}/${SYSTEMD_UNIT_FILENAME}" "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}" + sudo mv "${TMP_SYSTEMD_UNIT}" "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}" sudo systemctl enable "${SYSTEMD_UNIT_FILENAME}" &>/dev/null echo -e "The Infection Monkey service has been installed and will start on boot.\n\ From 88eb9dbe817d9fb3f1c3bfa07c74c1d61b53b85e Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 09:59:45 -0400 Subject: [PATCH 12/18] Build: Extract function install_systemd_unit() --- .../appimage/install-infection-monkey-service.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index b09bf2001..7f3baa69f 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -25,7 +25,14 @@ echo_help() { install_service() { move_appimage + install_systemd_unit "$1" + + echo -e "The Infection Monkey service has been installed and will start on boot.\n\ +Run 'systemctl start infection-monkey' to start the service now." +} + +install_systemd_unit() { umask 077 cat > "${TMP_SYSTEMD_UNIT}" << EOF [Unit] @@ -43,9 +50,6 @@ EOF sudo mv "${TMP_SYSTEMD_UNIT}" "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}" sudo systemctl enable "${SYSTEMD_UNIT_FILENAME}" &>/dev/null - - echo -e "The Infection Monkey service has been installed and will start on boot.\n\ -Run 'systemctl start infection-monkey' to start the service now." } uninstall_service() { From e1b053f000bdf8e17fb785c17fc6c2adb5a0d448 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 10:01:53 -0400 Subject: [PATCH 13/18] Build: Move move_appimage() for improved readability --- .../install-infection-monkey-service.sh | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 7f3baa69f..58934bc74 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -32,6 +32,16 @@ install_service() { Run 'systemctl start infection-monkey' to start the service now." } +move_appimage() { + sudo mkdir --mode=0755 -p "${MONKEY_BIN}" + + if [ "${APPIMAGE}" != "${MONKEY_BIN}/${APPIMAGE_NAME}" ] ; then + umask 022 + sudo cp "${APPIMAGE}" "${MONKEY_BIN}/${APPIMAGE_NAME}" + sudo chmod 755 "${MONKEY_BIN}/${APPIMAGE_NAME}" + fi +} + install_systemd_unit() { umask 077 cat > "${TMP_SYSTEMD_UNIT}" << EOF @@ -67,16 +77,6 @@ uninstall_service() { echo "The Infection Monkey service has been uninstalled" } -move_appimage() { - sudo mkdir --mode=0755 -p "${MONKEY_BIN}" - - if [ "${APPIMAGE}" != "${MONKEY_BIN}/${APPIMAGE_NAME}" ] ; then - umask 022 - sudo cp "${APPIMAGE}" "${MONKEY_BIN}/${APPIMAGE_NAME}" - sudo chmod 755 "${MONKEY_BIN}/${APPIMAGE_NAME}" - fi -} - user_exists() { id -u "$1" &>/dev/null } From 56bdbe5ac692fc08dd041434e4c1a1a7cc680262 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 10:02:22 -0400 Subject: [PATCH 14/18] Build: Rename move_appimage() -> copy_appimage() --- build_scripts/appimage/install-infection-monkey-service.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 58934bc74..bb2f02903 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -24,7 +24,7 @@ echo_help() { } install_service() { - move_appimage + copy_appimage install_systemd_unit "$1" @@ -32,7 +32,7 @@ install_service() { Run 'systemctl start infection-monkey' to start the service now." } -move_appimage() { +copy_appimage() { sudo mkdir --mode=0755 -p "${MONKEY_BIN}" if [ "${APPIMAGE}" != "${MONKEY_BIN}/${APPIMAGE_NAME}" ] ; then From 0f133d0b358d599b0199fdd6728aac090af26bda Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 10:05:45 -0400 Subject: [PATCH 15/18] Build: Improve echo in install-infection-monkey-service.sh --- build_scripts/appimage/install-infection-monkey-service.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index bb2f02903..bdc16cdf3 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -27,9 +27,8 @@ install_service() { copy_appimage install_systemd_unit "$1" - - echo -e "The Infection Monkey service has been installed and will start on boot.\n\ -Run 'systemctl start infection-monkey' to start the service now." + echo "The Infection Monkey service has been installed and will start on boot." + echo "Run 'systemctl start infection-monkey' to start the service now." } copy_appimage() { From 019bc56a7391d61e72e408fc12e8fa2b8024acd8 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 10:14:04 -0400 Subject: [PATCH 16/18] Build: Handle empty usernames --- build_scripts/appimage/install-infection-monkey-service.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index bdc16cdf3..0f437e53b 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -154,6 +154,13 @@ if $do_uninstall ; then fi if $do_install ; then + if [ -z "$username" ] ; then + echo "You must supply a username." + echo "" + echo_help + exit 1 + fi + install_service "$username" exit 0 fi From d5bb94f1a861fe648f425cb3b86762bf6ef895c8 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 10:43:22 -0400 Subject: [PATCH 17/18] Build: Make error message formatting consistent --- .../install-infection-monkey-service.sh | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 0f437e53b..272d6eb44 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -9,6 +9,13 @@ SYSTEMD_DIR="/lib/systemd/system" MONKEY_BIN="/opt/infection-monkey/bin" APPIMAGE_NAME="InfectionMonkey.AppImage" +die() { + echo "$1" >&2 + echo "" + echo_help + exit 1 +} + echo_help() { echo "Installs the Infection Monkey service to run on boot." echo "" @@ -82,8 +89,7 @@ user_exists() { exit_if_user_doesnt_exist() { if ! user_exists "$1" ; then - echo "Error: User '$1' does not exist" - exit 1 + die "Error: User '$1' does not exist." fi } @@ -95,9 +101,7 @@ has_sudo() { exit_if_missing_argument() { if [ -z "$2" ] || [ "${2:0:1}" == "-" ]; then - echo "Error: Argument for parameter '$1' is missing" >&2 - echo_help - exit 1 + die "Error: Argument for parameter '$1' is missing." fi } @@ -126,26 +130,22 @@ while (( "$#" )); do exit 0 ;; *) - echo "Error: Unsupported parameter $1" >&2 - exit 1 + die "Error: Unsupported parameter $1." ;; esac done if ! has_sudo; then - echo "Error: You need root permissions for some of this script operations. \ + die "Error: You need root permissions for some of this script operations. \ Run \`sudo -v\`, enter your password, and then re-run this script." - exit 1 fi if [ -z "${APPIMAGE}" ] ; then - echo "Error: Missing 'APPIMAGE' environment variable. Try installing the Infection Monkey service through the AppImage" - exit 1 + die "Error: Missing 'APPIMAGE' environment variable. Try installing the Infection Monkey service through the AppImage" fi if $do_install && $do_uninstall ; then - echo "The --install and --uninstall flags are mutually exclusive." - exit 1 + die "Error: The --install and --uninstall flags are mutually exclusive." fi if $do_uninstall ; then @@ -155,15 +155,11 @@ fi if $do_install ; then if [ -z "$username" ] ; then - echo "You must supply a username." - echo "" - echo_help - exit 1 + die "Error: You must supply a username." fi install_service "$username" exit 0 fi -echo "You must specify either the --install or --uninstall flag" -exit 1 +die "Error:You must specify either the --install or --uninstall flag." From fc96e3aced4508d033082fe7f17efcaa52009367 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 5 May 2022 10:44:07 -0400 Subject: [PATCH 18/18] Build: Switch order of user_exists() and exit_if_user_doesnt_exist() --- .../appimage/install-infection-monkey-service.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build_scripts/appimage/install-infection-monkey-service.sh b/build_scripts/appimage/install-infection-monkey-service.sh index 272d6eb44..596dad010 100755 --- a/build_scripts/appimage/install-infection-monkey-service.sh +++ b/build_scripts/appimage/install-infection-monkey-service.sh @@ -83,16 +83,16 @@ uninstall_service() { echo "The Infection Monkey service has been uninstalled" } -user_exists() { - id -u "$1" &>/dev/null -} - exit_if_user_doesnt_exist() { if ! user_exists "$1" ; then die "Error: User '$1' does not exist." fi } +user_exists() { + id -u "$1" &>/dev/null +} + has_sudo() { # 0 true, 1 false sudo -nv > /dev/null 2>&1