[WIP] Configure SQL Proxy

This commit is contained in:
Kevin Baensch 2022-12-21 19:30:38 +01:00
parent 566c56c423
commit cfb82fd78a
4 changed files with 74 additions and 37 deletions

View file

@ -10,10 +10,9 @@ services:
- 3022:22 - 3022:22
volumes: volumes:
- ./etc/ssh:/etc/ssh/ - ./etc/ssh:/etc/ssh/
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./script/hostman.sh:/hostman.sh:ro
- ./script/sqlproxy.sh:/sqlproxy.sh:ro - ./script/sqlproxy.sh:/sqlproxy.sh:ro
- ./script/sqlproxy_cli.sh:/sqlproxy_cli.sh:ro - ./script/sqlproxy_cli.sh:/sqlproxy_cli.sh:ro
- ./config:/config
environment: environment:
DISABLE_KEYGEN: true DISABLE_KEYGEN: true
DISABLE_CONFIG_GEN: true DISABLE_CONFIG_GEN: true

View file

@ -4,9 +4,9 @@ if [ -z "$SQL_CLI_TEMPLATE" ]
then then
if [ $(uname -s) = "Linux" ] if [ $(uname -s) = "Linux" ]
then then
SQL_CLI_TEMPLATE='mysql --protocol=TCP -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h localhost -P 3306' SQL_CLI_TEMPLATE='mysql --protocol=TCP -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h localhost -P 6033'
else else
SQL_CLI_TEMPLATE='open \"mysql://$MYSQL_USERNAME:$MYSQL_PASSWORD@localhost:3306\" -a \"Sequel Ace\"' SQL_CLI_TEMPLATE='open \"mysql://$MYSQL_USERNAME:$MYSQL_PASSWORD@localhost:6033\" -a \"Sequel Ace\"'
fi fi
else else
echo -e "Warning, custom client string:\n$SQL_CLI_TEMPLATE" echo -e "Warning, custom client string:\n$SQL_CLI_TEMPLATE"
@ -18,7 +18,6 @@ else
fi fi
[ -z $SQL_PROXY_HOST ] && SQL_PROXY_HOST="localhost" [ -z $SQL_PROXY_HOST ] && SQL_PROXY_HOST="localhost"
[ -z $SQL_PROXY_DB_PORT ] && SQL_PROXY_DB_PORT="3306"
CONNECTION_CACHE="$HOME/.cache/sqlproxy_$SQL_PROXY_HOST" CONNECTION_CACHE="$HOME/.cache/sqlproxy_$SQL_PROXY_HOST"
HELP="Usage: myssh [ls|connect]\n HELP="Usage: myssh [ls|connect]\n
@ -54,20 +53,41 @@ disconnect() {
port_forward() { port_forward() {
ACTIVE_HOST=$(cat $CONNECTION_CACHE) ACTIVE_HOST=$(cat $CONNECTION_CACHE)
if [ -z $ACTIVE_HOST ] || [ $ACTIVE_HOST != $1:$SQL_PROXY_DB_PORT ] if [ -z $ACTIVE_HOST ] || [ $ACTIVE_HOST != $1:$2 ]
then then
echo "Reconnect"
if [ ! -z $ACTIVE_HOST ] if [ ! -z $ACTIVE_HOST ]
then then
ssh -O cancel -L 3306:$ACTIVE_HOST -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST q ssh -O cancel -L 6033:$ACTIVE_HOST -S $HOME/.ssh/controlmasters/%r@%h:%p ${SQL_PROXY_HOST} q
fi fi
ssh -O forward -L 3306:$1:$SQL_PROXY_DB_PORT -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST echo $ACTIVE_HOST
echo $1 $2
ssh -O forward -L 6033:$1:$2 -S ${HOME}/.ssh/controlmasters/%r@%h:%p ${SQL_PROXY_HOST}
fi fi
echo $1:$SQL_PROXY_DB_PORT > $CONNECTION_CACHE echo $2:$3 > "${CONNECTION_CACHE}"
} }
ls_hosts() { ls_hosts() {
ssh -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST ls ssh -S ${HOME}/.ssh/controlmasters/%r@%h:%p "${SQL_PROXY_HOST}" ls
}
get_host() {
if [ $1 == '' ]
then
printf 'Please specify the host to connect to.\nRun "myssh ls" to list all available hosts.\n'
else
ssh -S ${HOME}/.ssh/controlmasters/%r@%h:%p "${SQL_PROXY_HOST}" "get $1"
fi
}
set_host_env() {
if [ $1 == 'mysql' | $1 == 'psql' ]
then
TARGET_HOST_TYPE=$1
fi
if [ $2 != '' ]
then
fi
} }
run_client() { run_client() {
@ -86,25 +106,21 @@ case $MAIN_OPTION in
# check if host is valid # check if host is valid
TARGET_HOST=$1 TARGET_HOST=$1
shift shift
ls_hosts | grep -qe "^$TARGET_HOST$"
GREP_EXIT_CODE=$? while getopts "u:p:" o
if [ $GREP_EXIT_CODE -eq 0 ] do
then case "$o" in
while getopts "u:p:" o u) MYSQL_USERNAME="$OPTARG" ;;
do p) MYSQL_PASSWORD="$OPTARG" ;;
case "$o" in esac
u) MYSQL_USERNAME="$OPTARG" ;; done
p) MYSQL_PASSWORD="$OPTARG" ;; TARGET_HOST_DATA="$(get_host "${TARGET_HOST}")"
esac port_forward $TARGET_HOST_DATA
done # port_forward $TARGET_HOST
port_forward $TARGET_HOST # if [ ! -z $MYSQL_USERNAME ] && [ ! -z $MYSQL_PASSWORD ]
if [ ! -z $MYSQL_USERNAME ] && [ ! -z $MYSQL_PASSWORD ] # then
then # run_client
run_client # fi
fi
else
echo "Invalid Hostname: $TARGET_HOST."
fi
;; ;;
disconnect) disconnect)
disconnect;; disconnect;;

View file

@ -1,18 +1,27 @@
#!/usr/bin/env sh #!/usr/bin/env sh
DB_DATA_FILE="${DB_DATA_FILE:-/config/sqlproxy.json}"
ls_hosts() { ls_hosts() {
# the containers version of grep does not support perl regex so "[^ ]*(?= # Added by hostman)" does not work jq -r '.[].host' < "${DB_DATA_FILE}"
grep -e "# Added by hostman" /etc/hosts | grep -oe "^[^ ]* [^ ]*" | grep -oe "[^ ]*$" }
get_host() {
export HOST=$(echo "${SSH_ORIGINAL_COMMAND}" | cut -d ' ' -f2)
if [ "${HOST}" != 'get' ]
then
jq -r 'first(.[] | select(.host == $ENV.HOST)) | [ .type, .ip, .port, .user, .password ] | join(" ")' < "${DB_DATA_FILE}"
fi
} }
idle() { idle() {
echo "Press CTRL C to quit this connection" printf "Press CTRL C to quit this connection\n"
sleep infinity sleep infinity
} }
case "$SSH_ORIGINAL_COMMAND" in case "${SSH_ORIGINAL_COMMAND}" in
"") idle;; "") idle;;
ls) ls_hosts;; ls) ls_hosts;;
get*) get_host;;
q|quit) exit 0;; q|quit) exit 0;;
*) exit 1;; *) exit 1;;
esac esac

View file

@ -4,16 +4,29 @@ set -e
WRAP_START='[\n' WRAP_START='[\n'
if ${EXCLUDE_USERPASS} if ${EXCLUDE_USERPASS}
then then
TEMPLATE=' { "type": "${LOCAL_DB_TYPE}", "host": "${LOCAL_DB_HOST}", "port": "${LOCAL_DB_PORT}" }' TEMPLATE=' { "ip": "${LOCAL_IP}", "type": "${LOCAL_DB_TYPE}", "host": "${LOCAL_DB_HOST}", "port": "${LOCAL_DB_PORT}" }'
else else
TEMPLATE=' { "type": "${LOCAL_DB_TYPE}", "user": "${LOCAL_DB_USER}", "password": "${LOCAL_DB_PASSWORD}", "host": "${LOCAL_DB_HOST}", "port": "${LOCAL_DB_PORT}" }' TEMPLATE=' { "ip": "${LOCAL_IP}", "type": "${LOCAL_DB_TYPE}", "user": "${LOCAL_DB_USER}", "password": "${LOCAL_DB_PASSWORD}", "host": "${LOCAL_DB_HOST}", "port": "${LOCAL_DB_PORT}" }'
fi fi
SEPARATOR=',\n' SEPARATOR=',\n'
WRAP_END='\n]' WRAP_END='\n]'
OUT="/config/sqlproxy.json" OUT="/config/sqlproxy.json"
label_hook() {
if [ -z "{LOCAL_DB_PORT}" ]
then
if [ "${LOCAL_DB_TYPE}" = "mysql" ]
then
LOCAL_DB_PORT='3306'
elif [ "${LOCAL_DB_TYPE}" = "psql" ]
then
LOCAL_DB_PORT='5432'
fi
fi
}
template_hook() { template_hook() {
if grep -q '"type": "(?mysql|psql)".*"host": ".+", port: "\d+"' <<< "${PARTIAL_RESULT}" if [ "$(jq '((.type == "mysql") or (.type == "psql")) and (.host != "")' <<< "${PARTIAL_RESULT}" 2> /dev/null)" = true ]
then then
return 0 return 0
fi fi