From 21a63304bd1e051cbc848765eec53633d28b6b65 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Sun, 22 Nov 2020 07:27:28 -0500 Subject: [PATCH] validate user@host for bpatch and bpatchfull commands. clean up shell scripts. --- bin/bpatch | 11 ++++------- bin/bpatchfull | 11 ++++------- bin/bubble_common | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/bin/bpatch b/bin/bpatch index e8d00b58..035e57f5 100755 --- a/bin/bpatch +++ b/bin/bpatch @@ -21,21 +21,18 @@ # BUBBLE_SSH_PORT : SSH port, default is 1202 # SCRIPT="${0}" -SCRIPT_DIR=$(cd $(dirname ${SCRIPT}) && pwd) -. ${SCRIPT_DIR}/bubble_common +SCRIPT_DIR="$(cd "$(dirname "${SCRIPT}")" && pwd)" +. "${SCRIPT_DIR}"/bubble_common HOST=${1:?no host provided} NO_RESTART=${2} - -if [[ -z "$(echo "${HOST}" | tr -cd '[:alnum:].-')" ]] ; then - die "invalid host: ${HOST}" -fi +validate_user_at_host "${HOST}" BUBBLE_SERVER_DIR="${SCRIPT_DIR}/../bubble-server" if [[ ! -d "${BUBBLE_SERVER_DIR}" ]] ; then die "bubble-server dir not found: ${BUBBLE_SERVER_DIR}" fi -cd ${BUBBLE_SERVER_DIR} +cd "${BUBBLE_SERVER_DIR}" || die "Error changing to ${BUBBLE_SERVER_DIR} directory" mvn -DskipTests=true -Dcheckstyle.skip=true compile && rsync -avzc ./target/classes ${HOST}:/tmp/ | egrep -v '*/$' || die "Error recompiling classes" diff --git a/bin/bpatchfull b/bin/bpatchfull index 55235807..887e5934 100755 --- a/bin/bpatchfull +++ b/bin/bpatchfull @@ -18,15 +18,12 @@ # BUBBLE_SSH_PORT : SSH port, default is 1202 # SCRIPT="${0}" -SCRIPT_DIR=$(cd $(dirname ${SCRIPT}) && pwd) -. ${SCRIPT_DIR}/bubble_common +SCRIPT_DIR="$(cd "$(dirname "${SCRIPT}")" && pwd)" +. "${SCRIPT_DIR}"/bubble_common HOST="${1:?no host provided}" NO_RESTART=${2} - -if [[ -z "$(echo "${HOST}" | tr -cd '[:alnum:].-')" ]] ; then - die "invalid host: ${HOST}" -fi +validate_user_at_host "${HOST}" if [[ -z "${BUBBLE_SSH_PORT}" ]] ; then BUBBLE_SSH_PORT="1202" @@ -36,7 +33,7 @@ BUBBLE_SERVER_DIR="${SCRIPT_DIR}/../bubble-server" if [[ ! -d "${BUBBLE_SERVER_DIR}" ]] ; then die "bubble-server dir not found: ${BUBBLE_SERVER_DIR}" fi -cd ${BUBBLE_SERVER_DIR} +cd "${BUBBLE_SERVER_DIR}" || die "Error changing to ${BUBBLE_SERVER_DIR} directory" ANY_JAR=$(find "./target" -type f -name "bubble*.jar" | head -1 | wc -l | tr -d ' ') if [[ ${ANY_JAR} -eq 0 ]] ; then diff --git a/bin/bubble_common b/bin/bubble_common index 5eeb17d9..a2ffa70f 100755 --- a/bin/bubble_common +++ b/bin/bubble_common @@ -14,7 +14,6 @@ function die() { } function handle_help_request() { - if [[ -z "${2}" ]]; then return fi @@ -31,7 +30,23 @@ function handle_help_request() { done <"${1}" exit 1 fi +} +# Validate that argument is "hostname" or "user@hostname" +# Validate that user (if present) is valid username and hostname is valid hostname +function validate_user_at_host() { + HOST="${1}" + if [[ -z "$(echo "${HOST}" | grep '@')" ]] ; then + if [[ $(echo "${HOST}" | awk -F '@' '{print $2}' | tr -cd '[:alnum:].-' | wc -c) -ne ${#HOST} ]] ; then + die "Invalid host: ${HOST}" + elif [[ $(echo "${HOST}" | awk -F '@' '{print $1}' | tr -cd '[:alnum:].-' | wc -c) -ne ${#HOST} ]] ; then + die "Invalid user: ${HOST}" + fi + else + if [[ $(echo "${HOST}" | tr -cd '[:alnum:].-' | wc -c) -ne ${#HOST} ]] ; then + die "Invalid host: ${HOST}" + fi + fi } function make_temp() {