From 2e824f92b34bba6e749fd9e38b881edd8e918b66 Mon Sep 17 00:00:00 2001 From: Kevin Baensch Date: Fri, 23 Dec 2022 11:14:55 +0100 Subject: [PATCH] Add fixes to sql proxy --- script/myssh | 51 +++++++++++++++++++++++++------------------ templates/sqlproxy.sh | 2 +- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/script/myssh b/script/myssh index 9582dcc..fe52a16 100755 --- a/script/myssh +++ b/script/myssh @@ -88,8 +88,6 @@ get_host() { then printf 'No such host: "%s"\n' $1 exit 1 - else - printf "%s" "${TARGET_HOST_DATA}" fi fi } @@ -102,33 +100,34 @@ get_host() { # $4 - username (optional) # $5 - password (optional) set_host_env() { - if [ $1 = 'mysql' ] || [ $1 = 'psql' ] + if [ "$1" = 'mysql' ] || [ "$1" = 'psql' ] then - TARGET_HOST_TYPE=$1 + TARGET_HOST_TYPE="$1" else - printf 'Invalid Database type: "%s"\n' $1 + printf 'Invalid Database type: "%s"\n' "$1" exit 1 fi - if [ $2 != '' ] + if [[ "$2" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] then - TARGET_HOST_IP=$2 + TARGET_HOST_IP="$2" else - printf 'No Host IP given.\n' + printf 'Invalid Host IP "%s" given.\n' "$2" exit 1 fi - if echo $3 | grep -q '[0-9]\+' + if [[ "$3" =~ ^[0-9]+$ ]] then - TARGET_HOST_PORT=$3 + TARGET_HOST_PORT="$3" else printf 'Invalid Host Port "%s" given.\n' "$3" + exit 1 fi - if [ -z "${TARGET_HOST_USERNAME}" ] && [ -n $4 ] + if [ -z "${TARGET_HOST_USERNAME}" ] && [ -n "$4" ] then - TARGET_HOST_USERNAME=$4 + TARGET_HOST_USERNAME="$4" fi - if [ -z "${TARGET_HOST_PASSWORD}" ] && [ -n $5 ] + if [ -z "${TARGET_HOST_PASSWORD}" ] && [ -n "$5" ] then - TARGET_HOST_PASSWORD=$5 + TARGET_HOST_PASSWORD="$5" fi } @@ -161,18 +160,27 @@ run_client() { fi } -MAIN_OPTION=$1 -shift +MAIN_OPTION="$1" +if [ -n "$1" ] +then + shift +fi # ensure connection connect -case $MAIN_OPTION in +case "${MAIN_OPTION}" in ls) ls_hosts;; connect) # check if host is valid - TARGET_HOST=$1 - shift + TARGET_HOST="$1" + if [ -n "$1" ] + then + shift + else + printf 'No host specified.\n' + exit 1 + fi while getopts "u:p:" o do @@ -181,7 +189,8 @@ case $MAIN_OPTION in p) TARGET_HOST_PASSWORD="$OPTARG" ;; esac done - set_host_env $(get_host "${TARGET_HOST}") + get_host "${TARGET_HOST}" + set_host_env ${TARGET_HOST_DATA} port_forward "${TARGET_HOST_IP}" "$TARGET_HOST_PORT" if [ -n "${TARGET_HOST_USERNAME}" ] && [ -n "${TARGET_HOST_PASSWORD}" ] then @@ -191,5 +200,5 @@ case $MAIN_OPTION in disconnect) disconnect;; *) - echo -e $HELP;; + echo -e "${HELP}";; esac diff --git a/templates/sqlproxy.sh b/templates/sqlproxy.sh index f8613f9..384da0b 100644 --- a/templates/sqlproxy.sh +++ b/templates/sqlproxy.sh @@ -13,7 +13,7 @@ WRAP_END='\n]' OUT="/config/sqlproxy.json" label_hook() { - if [ -z "{LOCAL_DB_PORT}" ] + if [ -z "${LOCAL_DB_PORT}" ] then if [ "${LOCAL_DB_TYPE}" = "mysql" ] then