2021-07-27 00:04:40 +08:00
|
|
|
WORKSPACE=${WORKSPACE:-$HOME}
|
2021-07-28 01:08:55 +08:00
|
|
|
DEFAULT_REPO_MONKEY_HOME=$WORKSPACE/git/monkey
|
2021-07-27 00:04:40 +08:00
|
|
|
MONKEY_ORIGIN_URL="https://github.com/guardicore/monkey.git"
|
2022-04-04 16:36:55 +08:00
|
|
|
NODE_SRC=https://deb.nodesource.com/setup_16.x
|
2021-07-28 19:53:33 +08:00
|
|
|
BUILD_SCRIPTS_DIR="$(realpath $(dirname $BASH_SOURCE[0]))"
|
|
|
|
DIST_DIR="$BUILD_SCRIPTS_DIR/dist"
|
2021-07-27 00:04:40 +08:00
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
log_message() {
|
|
|
|
echo -e "\n\n"
|
|
|
|
echo -e "MONKEY ISLAND BUILDER: $1"
|
|
|
|
}
|
2021-07-27 00:04:40 +08:00
|
|
|
|
|
|
|
exit_if_missing_argument() {
|
|
|
|
if [ -z "$2" ] || [ "${2:0:1}" == "-" ]; then
|
|
|
|
echo "Error: Argument for $1 is missing" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
echo_help() {
|
2021-07-28 01:08:55 +08:00
|
|
|
echo "usage: build_package.sh [--help] [--agent-binary-dir <PATH>] [--branch <BRANCH>]"
|
2021-07-27 00:04:40 +08:00
|
|
|
echo " [--monkey-repo <PATH>] [--version <MONKEY_VERSION>]"
|
2021-09-15 20:13:26 +08:00
|
|
|
echo " [--deployment <DEPLOYMENT_TYPE>]"
|
2021-07-27 00:04:40 +08:00
|
|
|
echo ""
|
2021-07-28 01:08:55 +08:00
|
|
|
echo "Creates a package for Infection Monkey."
|
2021-07-27 00:04:40 +08:00
|
|
|
echo ""
|
|
|
|
echo "--agent-binary-dir A directory containing the agent binaries that"
|
2021-07-28 01:08:55 +08:00
|
|
|
echo " you'd like to include with the package. If this"
|
2021-07-27 00:04:40 +08:00
|
|
|
echo " parameter is unspecified, the latest release"
|
|
|
|
echo " binaries will be downloaded from GitHub."
|
|
|
|
echo ""
|
|
|
|
echo "--as-root Throw caution to the wind and allow this script"
|
|
|
|
echo " to be run as root."
|
|
|
|
echo ""
|
2021-07-28 01:08:55 +08:00
|
|
|
echo "--branch The git branch you'd like the package to be"
|
2021-07-27 00:04:40 +08:00
|
|
|
echo " built from. (Default: develop)"
|
|
|
|
echo ""
|
|
|
|
echo "--monkey-repo A directory containing the Infection Monkey git"
|
|
|
|
echo " repository. If the directory is empty or does"
|
|
|
|
echo " not exist, a new repo will be cloned from GitHub."
|
|
|
|
echo " If the directory is already a valid GitHub repo,"
|
|
|
|
echo " it will be used as-is and the --branch parameter"
|
|
|
|
echo " will have no effect."
|
|
|
|
echo " (Default: $DEFAULT_REPO_MONKEY_HOME)"
|
|
|
|
echo ""
|
2021-07-28 01:08:55 +08:00
|
|
|
echo "--version A version number for the package."
|
|
|
|
echo ""
|
2021-09-15 20:13:26 +08:00
|
|
|
echo "--deployment A deployment type for the package."
|
|
|
|
echo " (Default: develop)"
|
|
|
|
echo ""
|
2021-07-29 01:47:11 +08:00
|
|
|
echo "--package Which package to build (\"appimage\" or \"docker.\")"
|
2021-07-27 00:04:40 +08:00
|
|
|
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
is_root() {
|
|
|
|
return "$(id -u)"
|
|
|
|
}
|
|
|
|
|
|
|
|
has_sudo() {
|
|
|
|
# 0 true, 1 false
|
|
|
|
sudo -nv > /dev/null 2>&1
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_error() {
|
|
|
|
echo "Fix the errors above and rerun the script"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
install_nodejs() {
|
|
|
|
log_message "Installing nodejs"
|
|
|
|
|
|
|
|
curl -sL $NODE_SRC | sudo -E bash -
|
|
|
|
sudo apt-get install -y nodejs
|
|
|
|
}
|
|
|
|
|
|
|
|
is_valid_git_repo() {
|
|
|
|
pushd "$1" 2>/dev/null || return 1
|
|
|
|
git status >/dev/null 2>&1
|
|
|
|
success="$?"
|
|
|
|
popd || exit 1
|
|
|
|
|
|
|
|
return $success
|
|
|
|
}
|
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
clone_monkey_repo() {
|
|
|
|
local repo_dir=$1
|
|
|
|
local branch=$2
|
2021-07-27 00:04:40 +08:00
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
if [[ ! -d "$repo_dir" ]]; then
|
|
|
|
mkdir -p "$repo_dir"
|
2021-07-27 00:04:40 +08:00
|
|
|
fi
|
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
log_message "Cloning files from git"
|
|
|
|
git clone -c core.autocrlf=false --single-branch --recurse-submodules -b "$branch" "$MONKEY_ORIGIN_URL" "$repo_dir" 2>&1 || handle_error
|
2021-07-27 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
install_build_prereqs() {
|
|
|
|
sudo apt-get update
|
|
|
|
sudo apt-get upgrade -y
|
2021-07-27 00:04:40 +08:00
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
# monkey island prereqs
|
|
|
|
sudo apt-get install -y curl libcurl4 openssl git build-essential moreutils
|
|
|
|
install_nodejs
|
2021-07-27 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
agent_binary_dir=""
|
|
|
|
as_root=false
|
|
|
|
branch="develop"
|
|
|
|
monkey_repo="$DEFAULT_REPO_MONKEY_HOME"
|
2021-10-18 22:46:41 +08:00
|
|
|
monkey_version=""
|
2021-07-28 01:08:55 +08:00
|
|
|
package=""
|
2021-09-15 20:49:17 +08:00
|
|
|
deployment_type=""
|
2021-07-27 00:04:40 +08:00
|
|
|
|
|
|
|
while (( "$#" )); do
|
|
|
|
case "$1" in
|
|
|
|
--agent-binary-dir)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
agent_binary_dir=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--as-root)
|
|
|
|
as_root=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--branch)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
branch=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
-h|--help)
|
|
|
|
echo_help
|
|
|
|
;;
|
|
|
|
--monkey-repo)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
monkey_repo=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--version)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
monkey_version=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
2021-09-15 20:13:26 +08:00
|
|
|
--deployment)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
deployment_type=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
2021-07-28 01:08:55 +08:00
|
|
|
--package)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
package=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
2021-07-27 00:04:40 +08:00
|
|
|
*)
|
|
|
|
echo "Error: Unsupported parameter $1" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
if ! [[ $package =~ ^(appimage|docker)$ ]]; then
|
|
|
|
log_message "Invalid package: $package."
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-07-27 00:04:40 +08:00
|
|
|
|
|
|
|
if ! $as_root && is_root; then
|
|
|
|
log_message "Please don't run this script as root"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! has_sudo; then
|
|
|
|
log_message "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
|
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
log_message "Building Monkey Island: $package"
|
|
|
|
|
|
|
|
source "./$package/$package.sh"
|
2021-07-27 00:04:40 +08:00
|
|
|
|
|
|
|
if ! is_valid_git_repo "$monkey_repo"; then
|
|
|
|
clone_monkey_repo "$monkey_repo" "$branch"
|
|
|
|
fi
|
|
|
|
|
2021-07-28 19:53:33 +08:00
|
|
|
if [ ! -d "$DIST_DIR" ]; then
|
|
|
|
mkdir "$DIST_DIR"
|
|
|
|
fi
|
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
install_build_prereqs
|
|
|
|
install_package_specific_build_prereqs "$WORKSPACE"
|
|
|
|
|
2022-04-27 23:18:26 +08:00
|
|
|
commit_id=$(get_commit_id "$monkey_repo")
|
|
|
|
|
2022-04-12 20:43:15 +08:00
|
|
|
is_release_build=false
|
|
|
|
# Monkey version is empty on release build
|
|
|
|
if [ ! -z "$monkey_version" ]; then
|
|
|
|
is_release_build=true
|
2022-04-27 23:18:26 +08:00
|
|
|
echo -n "" > "$monkey_repo/monkey/common/BUILD"
|
|
|
|
else
|
|
|
|
echo $commit_id > "$monkey_repo/monkey/common/BUILD"
|
2022-04-12 20:43:15 +08:00
|
|
|
fi
|
2021-07-28 01:08:55 +08:00
|
|
|
|
2022-04-12 20:43:15 +08:00
|
|
|
setup_build_dir "$agent_binary_dir" "$monkey_repo" "$deployment_type" "$is_release_build"
|
2022-04-27 23:18:26 +08:00
|
|
|
|
2021-10-21 23:02:37 +08:00
|
|
|
build_package "$monkey_version" "$commit_id" "$DIST_DIR"
|
2021-07-27 00:04:40 +08:00
|
|
|
|
2021-07-28 01:08:55 +08:00
|
|
|
log_message "Finished building package: $package"
|
2021-07-27 00:04:40 +08:00
|
|
|
exit 0
|