Add fixes to sql proxy

This commit is contained in:
Kevin Baensch 2022-12-23 11:14:55 +01:00
parent f35b108e17
commit 2e824f92b3
2 changed files with 31 additions and 22 deletions

View file

@ -88,8 +88,6 @@ get_host() {
then then
printf 'No such host: "%s"\n' $1 printf 'No such host: "%s"\n' $1
exit 1 exit 1
else
printf "%s" "${TARGET_HOST_DATA}"
fi fi
fi fi
} }
@ -102,33 +100,34 @@ get_host() {
# $4 - username (optional) # $4 - username (optional)
# $5 - password (optional) # $5 - password (optional)
set_host_env() { set_host_env() {
if [ $1 = 'mysql' ] || [ $1 = 'psql' ] if [ "$1" = 'mysql' ] || [ "$1" = 'psql' ]
then then
TARGET_HOST_TYPE=$1 TARGET_HOST_TYPE="$1"
else else
printf 'Invalid Database type: "%s"\n' $1 printf 'Invalid Database type: "%s"\n' "$1"
exit 1 exit 1
fi fi
if [ $2 != '' ] if [[ "$2" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then then
TARGET_HOST_IP=$2 TARGET_HOST_IP="$2"
else else
printf 'No Host IP given.\n' printf 'Invalid Host IP "%s" given.\n' "$2"
exit 1 exit 1
fi fi
if echo $3 | grep -q '[0-9]\+' if [[ "$3" =~ ^[0-9]+$ ]]
then then
TARGET_HOST_PORT=$3 TARGET_HOST_PORT="$3"
else else
printf 'Invalid Host Port "%s" given.\n' "$3" printf 'Invalid Host Port "%s" given.\n' "$3"
exit 1
fi fi
if [ -z "${TARGET_HOST_USERNAME}" ] && [ -n $4 ] if [ -z "${TARGET_HOST_USERNAME}" ] && [ -n "$4" ]
then then
TARGET_HOST_USERNAME=$4 TARGET_HOST_USERNAME="$4"
fi fi
if [ -z "${TARGET_HOST_PASSWORD}" ] && [ -n $5 ] if [ -z "${TARGET_HOST_PASSWORD}" ] && [ -n "$5" ]
then then
TARGET_HOST_PASSWORD=$5 TARGET_HOST_PASSWORD="$5"
fi fi
} }
@ -161,18 +160,27 @@ run_client() {
fi fi
} }
MAIN_OPTION=$1 MAIN_OPTION="$1"
shift if [ -n "$1" ]
then
shift
fi
# ensure connection # ensure connection
connect connect
case $MAIN_OPTION in case "${MAIN_OPTION}" in
ls) ls)
ls_hosts;; ls_hosts;;
connect) connect)
# check if host is valid # check if host is valid
TARGET_HOST=$1 TARGET_HOST="$1"
if [ -n "$1" ]
then
shift shift
else
printf 'No host specified.\n'
exit 1
fi
while getopts "u:p:" o while getopts "u:p:" o
do do
@ -181,7 +189,8 @@ case $MAIN_OPTION in
p) TARGET_HOST_PASSWORD="$OPTARG" ;; p) TARGET_HOST_PASSWORD="$OPTARG" ;;
esac esac
done 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" port_forward "${TARGET_HOST_IP}" "$TARGET_HOST_PORT"
if [ -n "${TARGET_HOST_USERNAME}" ] && [ -n "${TARGET_HOST_PASSWORD}" ] if [ -n "${TARGET_HOST_USERNAME}" ] && [ -n "${TARGET_HOST_PASSWORD}" ]
then then
@ -191,5 +200,5 @@ case $MAIN_OPTION in
disconnect) disconnect)
disconnect;; disconnect;;
*) *)
echo -e $HELP;; echo -e "${HELP}";;
esac esac

View file

@ -13,7 +13,7 @@ WRAP_END='\n]'
OUT="/config/sqlproxy.json" OUT="/config/sqlproxy.json"
label_hook() { label_hook() {
if [ -z "{LOCAL_DB_PORT}" ] if [ -z "${LOCAL_DB_PORT}" ]
then then
if [ "${LOCAL_DB_TYPE}" = "mysql" ] if [ "${LOCAL_DB_TYPE}" = "mysql" ]
then then