| 1 |
#!/usr/bin/env bash |
| 2 |
# ========================================================================== |
| 3 |
# _ _ _ ____ ____ _____ |
| 4 |
# | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __ |
| 5 |
# | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__| |
| 6 |
# | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ | |
| 7 |
# |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_| |
| 8 |
# |
| 9 |
# --- High-Performance Connectivity Tracer (HiPerConTracer) --- |
| 10 |
# https://www.nntb.no/~dreibh/hipercontracer/ |
| 11 |
# ========================================================================== |
| 12 |
# |
| 13 |
# High-Performance Connectivity Tracer (HiPerConTracer) |
| 14 |
# Copyright (C) 2015-2026 by Thomas Dreibholz |
| 15 |
# |
| 16 |
# This program is free software: you can redistribute it and/or modify |
| 17 |
# it under the terms of the GNU General Public License as published by |
| 18 |
# the Free Software Foundation, either version 3 of the License, or |
| 19 |
# (at your option) any later version. |
| 20 |
# |
| 21 |
# This program is distributed in the hope that it will be useful, |
| 22 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
# GNU General Public License for more details. |
| 25 |
# |
| 26 |
# You should have received a copy of the GNU General Public License |
| 27 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 |
# |
| 29 |
# Contact: [email protected] |
| 30 |
|
| 31 |
# Bash options: |
| 32 |
set -eu |
| 33 |
|
| 34 |
|
| 35 |
# ###### Usage ############################################################## |
| 36 |
usage () { |
| 37 |
echo >&2 "Usage: $0 configuration_file [-h|--help] [-d|--dry-run] [-q|--quiet] [-B|--write-dbeaver-config file_prefix]" |
| 38 |
exit 1 |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
# ###### Main program ####################################################### |
| 43 |
|
| 44 |
# ====== Handle arguments =================================================== |
| 45 |
GETOPT="$(PATH=/usr/local/bin:$PATH which getopt)" |
| 46 |
options="$(${GETOPT} -o hdqB: --long help,dry-run,quiet,write-dbeaver-config: -a -- "$@")" |
| 47 |
# shellcheck disable=SC2181 |
| 48 |
if [[ $? -ne 0 ]]; then |
| 49 |
usage |
| 50 |
fi |
| 51 |
|
| 52 |
DRY_RUN=0 |
| 53 |
QUIET=0 |
| 54 |
DBEAVER_CONFIG_PREFIX="" |
| 55 |
eval set -- "${options}" |
| 56 |
while [ $# -gt 0 ] ; do |
| 57 |
case "$1" in |
| 58 |
-B | --write-dbeaver-config) |
| 59 |
DBEAVER_CONFIG_PREFIX="$2" |
| 60 |
shift 2 |
| 61 |
;; |
| 62 |
-d | --dry-run) |
| 63 |
DRY_RUN=1 |
| 64 |
shift |
| 65 |
;; |
| 66 |
-q | --quiet) |
| 67 |
QUIET=1 |
| 68 |
shift |
| 69 |
;; |
| 70 |
--) |
| 71 |
shift |
| 72 |
break |
| 73 |
;; |
| 74 |
esac |
| 75 |
done |
| 76 |
if [ $# -lt 1 ] ; then |
| 77 |
usage |
| 78 |
fi |
| 79 |
CONFIGURATION_FILE="$1" |
| 80 |
if [ ! -e "${CONFIGURATION_FILE}" ] ; then |
| 81 |
echo >&2 "ERROR: User configuration ${CONFIGURATION_FILE} does not exist!" |
| 82 |
exit 1 |
| 83 |
fi |
| 84 |
|
| 85 |
|
| 86 |
# ====== Read configuration file ============================================ |
| 87 |
dbbackend="" |
| 88 |
dbserver="" |
| 89 |
dbport="" |
| 90 |
dbcafile="" |
| 91 |
dbcrlfile="" |
| 92 |
dbkeyfile="" |
| 93 |
dbcertfile="" |
| 94 |
dbcertkeyfile="" |
| 95 |
dbconnectionflags="" |
| 96 |
dbuser="" |
| 97 |
dbpassword="" |
| 98 |
database="" |
| 99 |
configContent=$(sed -r '/[^=#]+=[^=]+/!d' <"${CONFIGURATION_FILE}" | sed -r "s/[[:space:]]*=[[:space:]]*(.*)[[:space:]]*/='\1'/g") |
| 100 |
eval "${configContent}" |
| 101 |
if [ "${dbcafile}" == "NONE" ] ; then |
| 102 |
dbcafile="" |
| 103 |
fi |
| 104 |
if [ "${dbcrlfile}" == "NONE" ] ; then |
| 105 |
dbcrlfile="" |
| 106 |
fi |
| 107 |
if [ "${dbkeyfile}" == "NONE" ] ; then |
| 108 |
dbkeyfile="" |
| 109 |
fi |
| 110 |
if [ "${dbcertfile}" == "NONE" ] ; then |
| 111 |
dbcertfile="" |
| 112 |
fi |
| 113 |
if [ "${dbcertkeyfile}" == "NONE" ] ; then |
| 114 |
dbcertkeyfile="" |
| 115 |
fi |
| 116 |
if [ "${dbcertfile}" == "NONE" ] ; then |
| 117 |
dbcertfile="" |
| 118 |
fi |
| 119 |
tls_enabled=1 |
| 120 |
tls_check_certificate=1 |
| 121 |
tls_check_hostname=1 |
| 122 |
for dbconnectionflag in ${dbconnectionflags} ; do |
| 123 |
if [ "${dbconnectionflag}" == "DisableTLS" ] ; then |
| 124 |
tls_enabled=0 |
| 125 |
tls_check_certificate=0 |
| 126 |
tls_check_hostname=0 |
| 127 |
elif [ "${dbconnectionflag}" == "AllowInvalidCertificate" ] ; then |
| 128 |
tls_check_certificate=0 |
| 129 |
tls_check_hostname=0 |
| 130 |
elif [ "${dbconnectionflag}" == "AllowInvalidHostname" ] ; then |
| 131 |
tls_check_certificate=1 |
| 132 |
tls_check_hostname=0 |
| 133 |
elif [ "${dbconnectionflag}" != "NONE" ] ; then |
| 134 |
echo >&2 "ERROR: Invalid connection flag ${dbconnectionflag}!" |
| 135 |
exit 1 |
| 136 |
fi |
| 137 |
done |
| 138 |
|
| 139 |
|
| 140 |
# ====== Build the parameters line ============================================= |
| 141 |
oldCommand=0 |
| 142 |
command="" |
| 143 |
parameters="" |
| 144 |
if [ "${dbbackend}" == "MariaDB" ] || [ "${dbbackend}" == "MySQL" ] ; then |
| 145 |
|
| 146 |
if [ "${dbbackend}" == "MariaDB" ] ; then |
| 147 |
command="$(which mariadb || true)" |
| 148 |
if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then |
| 149 |
echo >&2 "ERROR: MariaDB client (mariadb) not found. Is it installed?" |
| 150 |
exit 1 |
| 151 |
fi |
| 152 |
else |
| 153 |
command="$(which mysql || true)" |
| 154 |
if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then |
| 155 |
echo >&2 "ERROR: MySQL client (mysql) not found. Is it installed?" |
| 156 |
exit 1 |
| 157 |
fi |
| 158 |
fi |
| 159 |
if [ "${dbport}" == "0" ] ; then |
| 160 |
dbport="3306" |
| 161 |
fi |
| 162 |
parameters="${parameters} --silent --host=\"${dbserver}\" --port=${dbport} --protocol=tcp --user=\"${dbuser}\" --password=\"${dbpassword}\" --database=\"${database}\"" |
| 163 |
if [ ${tls_enabled} -eq 1 ] && [ "${dbcafile}" != "" ] ; then |
| 164 |
if [ ${tls_check_certificate} -eq 1 ] || [ ${tls_check_hostname} -eq 1 ] ; then |
| 165 |
parameters="${parameters} --ssl-verify-server-cert" |
| 166 |
fi |
| 167 |
parameters="${parameters} --ssl-ca=\"${dbcafile}\"" |
| 168 |
if [ "${dbcrlfile}" != "" ] ; then |
| 169 |
parameters="${parameters} --ssl-crl \"${dbcrlfile}\"" |
| 170 |
fi |
| 171 |
fi |
| 172 |
|
| 173 |
elif [ "${dbbackend}" == "PostgreSQL" ] ; then |
| 174 |
|
| 175 |
command="$(which psql || true)" |
| 176 |
if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then |
| 177 |
echo >&2 "ERROR: PostgreSQL client (psql) not found. Is it installed?" |
| 178 |
exit 1 |
| 179 |
fi |
| 180 |
if [ "${dbport}" == "0" ] ; then |
| 181 |
dbport="5432" |
| 182 |
fi |
| 183 |
parameters="--quiet --host=\"${dbserver}\" --port=${dbport} --username=\"${dbuser}\" --dbname=\"${database}\"" |
| 184 |
export PGPASSWORD="${dbpassword}" |
| 185 |
if [ ${tls_enabled} -eq 1 ] && [ "${dbcafile}" != "" ] ; then |
| 186 |
if [ ${tls_check_certificate} -eq 1 ] ; then |
| 187 |
if [ ${tls_check_hostname} -eq 1 ] ; then |
| 188 |
export PGSSLMODE="verify-full" |
| 189 |
else |
| 190 |
export PGSSLMODE="verify-ca" |
| 191 |
fi |
| 192 |
else |
| 193 |
export PGSSLMODE="require" |
| 194 |
fi |
| 195 |
export PGSSLROOTCERT="${dbcafile}" |
| 196 |
if [ "${dbcrlfile}" != "" ] ; then |
| 197 |
export PGSSLCRL="${dbcrlfile}" |
| 198 |
fi |
| 199 |
else |
| 200 |
export PGSSLMODE="prefer" |
| 201 |
fi |
| 202 |
|
| 203 |
elif [ "${dbbackend}" == "MongoDB" ] ; then |
| 204 |
|
| 205 |
command="$(which mongosh || true)" |
| 206 |
if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then |
| 207 |
command="$(which mongo || true)" |
| 208 |
if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then |
| 209 |
echo >&2 "ERROR: MongoDB client (mongosh or mongo) not found. Is it installed?" |
| 210 |
exit 1 |
| 211 |
else |
| 212 |
oldCommand=1 |
| 213 |
fi |
| 214 |
fi |
| 215 |
if [ "${dbport}" == "0" ] ; then |
| 216 |
dbport="27017" |
| 217 |
fi |
| 218 |
parameters="\"mongodb://${dbserver}:${dbport}/${database}\" --username \"${dbuser}\" --password \"${dbpassword}\"" |
| 219 |
if [ ${tls_enabled} -eq 1 ] && [ "${dbcafile}" != "" ] ; then |
| 220 |
if [ ${oldCommand} -eq 0 ] ; then |
| 221 |
parameters="${parameters} --tls --tlsDisabledProtocols TLS1_0,TLS1_1,TLS1_2 --tlsCAFile \"${dbcafile}\"" |
| 222 |
if [ ${tls_check_certificate} -eq 0 ] ; then |
| 223 |
parameters="${parameters} --tlsAllowInvalidCertificates" |
| 224 |
elif [ ${tls_check_hostname} -eq 0 ] ; then |
| 225 |
parameters="${parameters} --tlsAllowInvalidHostnames" |
| 226 |
fi |
| 227 |
if [ "${dbcrlfile}" != "" ] ; then |
| 228 |
# FIXME: --tlsCRLFile \"${dbcrlfile}\" does not work! |
| 229 |
# parameters="${parameters} --tlsCRLFile \"${dbcrlfile}\"" |
| 230 |
true |
| 231 |
fi |
| 232 |
else |
| 233 |
parameters="${parameters} --ssl --sslDisabledProtocols TLS1_0,TLS1_1,TLS1_2 --sslCAFile \"${dbcafile}\"" |
| 234 |
if [ "${dbcrlfile}" != "" ] ; then |
| 235 |
parameters="${parameters} --sslCRLFile \"${dbcrlfile}\"" |
| 236 |
fi |
| 237 |
fi |
| 238 |
fi |
| 239 |
if [ ${oldCommand} -eq 0 ] ; then |
| 240 |
parameters="${parameters} --quiet" |
| 241 |
fi |
| 242 |
|
| 243 |
else |
| 244 |
|
| 245 |
echo >&2 "ERROR: Invalid dbbackend setting \"${dbbackend}!\"" |
| 246 |
exit 1 |
| 247 |
|
| 248 |
fi |
| 249 |
|
| 250 |
|
| 251 |
# ====== Generate DBeaver configuration file ================================ |
| 252 |
if [ "${DBEAVER_CONFIG_PREFIX}" ] ; then |
| 253 |
driver="" |
| 254 |
provider="" |
| 255 |
jdbc="" |
| 256 |
custom="" |
| 257 |
handler="" |
| 258 |
dbusername="${dbuser}" |
| 259 |
|
| 260 |
# ====== MariaDB/MySQL =================================================== |
| 261 |
if [ "${dbbackend}" == "MariaDB" ] || [ "${dbbackend}" == "MySQL" ] ; then |
| 262 |
provider="mysql" |
| 263 |
handler="mysql_ssl" |
| 264 |
# FIXME! |
| 265 |
# Currently, the MariaDB driver is not working. Using MySQL driver instead |
| 266 |
# => https://github.com/dbeaver/dbeaver/issues/35242 |
| 267 |
# if [ "${dbbackend}" == "MariaDB" ] ; then |
| 268 |
# driver="mariaDB" |
| 269 |
# jdbc="mariadb" |
| 270 |
# else |
| 271 |
driver="mysql8" |
| 272 |
jdbc="mysql" |
| 273 |
# fi |
| 274 |
if [ ${tls_enabled} -eq 1 ] && [ "${dbcafile}" != "" ] ; then |
| 275 |
ssl_require="true" |
| 276 |
if [ ${tls_check_certificate} -eq 1 ] || [ ${tls_check_hostname} -eq 1 ] ; then |
| 277 |
ssl_verify_server="true" |
| 278 |
else |
| 279 |
ssl_verify_server="false" |
| 280 |
fi |
| 281 |
else |
| 282 |
ssl_require="false" |
| 283 |
ssl_verify_server="false" |
| 284 |
fi |
| 285 |
custom=", |
| 286 |
\"ssl_require\": \"${ssl_require}\", |
| 287 |
\"ssl_verify.server\": \"${ssl_verify_server}\", |
| 288 |
\"ssl.public.key.retrieve\": \"false\"" |
| 289 |
|
| 290 |
# ====== PostgreSQL ====================================================== |
| 291 |
elif [ "${dbbackend}" == "PostgreSQL" ] ; then |
| 292 |
driver="postgres-jdbc" |
| 293 |
provider="postgresql" |
| 294 |
jdbc="postgresql" |
| 295 |
handler="postgre_ssl" |
| 296 |
if [ ${tls_check_certificate} -eq 1 ] ; then |
| 297 |
if [ ${tls_check_hostname} -eq 1 ] ; then |
| 298 |
ssl_mode="verify-full" |
| 299 |
else |
| 300 |
ssl_mode="verify-ca" |
| 301 |
fi |
| 302 |
else |
| 303 |
ssl_mode="require" |
| 304 |
fi |
| 305 |
custom=", |
| 306 |
\"sslMode\": \"${ssl_mode}\", |
| 307 |
\"sslFactory\": \"\"" |
| 308 |
if [ "${dbuser}" == "postgres" ] ; then |
| 309 |
dbusername="root" |
| 310 |
fi |
| 311 |
|
| 312 |
# ====== Unsupported ===================================================== |
| 313 |
else |
| 314 |
if [ ${QUIET} -eq 0 ] ; then |
| 315 |
echo >&2 "WARNING: DBeaver does not support ${dbbackend} backend!" |
| 316 |
fi |
| 317 |
echo "" >"${DBEAVER_CONFIG_PREFIX}-data-source.json" |
| 318 |
echo "" >"${DBEAVER_CONFIG_PREFIX}-credentials-config.json" |
| 319 |
fi |
| 320 |
|
| 321 |
if [ "${driver}" != "" ] ; then |
| 322 |
identifier="$(echo "${provider}://${dbusername}@${dbserver}:${dbport}/${database}" | md5sum --tag | cut -d' ' -f4)" |
| 323 |
|
| 324 |
color="211,215,207" |
| 325 |
if [ "${dbusername}" == "importer" ] ; then |
| 326 |
color="237,212,0" |
| 327 |
elif [ "${dbusername}" == "researcher" ] ; then |
| 328 |
color="0,127,253" |
| 329 |
elif [ "${dbusername}" == "maintainer" ] ; then |
| 330 |
color="114,159,207" |
| 331 |
elif [ "${dbusername}" == "root" ] ; then |
| 332 |
color="239,41,41" |
| 333 |
fi |
| 334 |
# ansiColor="${color//,/;}" |
| 335 |
# echo >&2 -e "\x1b[38;2;${ansiColor}mdbusername=${dbusername} database=${database} color=${color}\x1b[0m" |
| 336 |
|
| 337 |
# ====== Generate DBeaver configuration ================================== |
| 338 |
if [ ${QUIET} -eq 0 ] ; then |
| 339 |
echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Writing DBeaver configuration to ${DBEAVER_CONFIG_PREFIX} ...\x1b[0m" |
| 340 |
fi |
| 341 |
( |
| 342 |
cat << EOF |
| 343 |
{ |
| 344 |
"folders": {}, |
| 345 |
"connection-types": { |
| 346 |
"${dbusername}": { |
| 347 |
"name": "${dbusername}", |
| 348 |
"color": "${color}", |
| 349 |
"description": "Regular database access for ${dbusername}", |
| 350 |
"auto-commit": true, |
| 351 |
"confirm-execute": false, |
| 352 |
"confirm-data-change": false, |
| 353 |
"smart-commit": false, |
| 354 |
"smart-commit-recover": true, |
| 355 |
"auto-close-transactions": true, |
| 356 |
"close-transactions-period": 1800, |
| 357 |
"auto-close-connections": true, |
| 358 |
"close-connections-period": 14400 |
| 359 |
} |
| 360 |
}, |
| 361 |
"connections": { |
| 362 |
"${driver}-${identifier}": { |
| 363 |
"provider": "${provider}", |
| 364 |
"driver": "${driver}", |
| 365 |
"name": "${dbusername}@${dbserver}:${dbport}/${database} ${dbbackend}", |
| 366 |
"save-password": true, |
| 367 |
"configuration": { |
| 368 |
"host": "${dbserver}", |
| 369 |
"port": "${dbport}", |
| 370 |
"database": "${database}", |
| 371 |
"url": "jdbc:${jdbc}://${dbserver}:${dbport}/${database}", |
| 372 |
"configurationType": "MANUAL", |
| 373 |
"type": "${dbusername}", |
| 374 |
"closeIdleConnection": true, |
| 375 |
"auth-model": "native", |
| 376 |
"handlers": { |
| 377 |
"${handler}": { |
| 378 |
"type": "CONFIG", |
| 379 |
"enabled": true, |
| 380 |
"save-password": true, |
| 381 |
"properties": { |
| 382 |
"ssl.ca.cert": "${dbcafile}", |
| 383 |
"ssl.client.cert": "${dbcertfile}", |
| 384 |
"ssl.client.key": "${dbkeyfile}", |
| 385 |
"ssl.method": "CERTIFICATES", |
| 386 |
"ssl.public.key.retrieve": "false", |
| 387 |
"ssl.cipher.suites": ""${custom} |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
EOF |
| 396 |
) >"${DBEAVER_CONFIG_PREFIX}-data-source.json" |
| 397 |
|
| 398 |
( |
| 399 |
cat << EOF |
| 400 |
{ |
| 401 |
"${driver}-${identifier}": { |
| 402 |
"#connection": { |
| 403 |
"user": "${dbuser}", |
| 404 |
"password": "${dbpassword}" |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
EOF |
| 409 |
) >"${DBEAVER_CONFIG_PREFIX}-credentials-config.json" |
| 410 |
fi |
| 411 |
fi |
| 412 |
|
| 413 |
|
| 414 |
# ====== Run the parameters line ============================================ |
| 415 |
if [ ${QUIET} -eq 0 ] ; then |
| 416 |
if [ ${DRY_RUN} -eq 0 ] ; then |
| 417 |
echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Connecting to database ...\x1b[0m" |
| 418 |
echo -en "\x1b[37m" |
| 419 |
fi |
| 420 |
|
| 421 |
if [ "${dbbackend}" == "PostgreSQL" ] ; then |
| 422 |
export | grep "PGPASSWORD=\|PGSSL[A-Z]*=" | sort || true |
| 423 |
fi |
| 424 |
echo "${command} ${parameters}" |
| 425 |
fi |
| 426 |
|
| 427 |
if [ ${DRY_RUN} -eq 0 ] ; then |
| 428 |
if [ ${QUIET} -eq 0 ] ; then |
| 429 |
echo -en "\x1b[0m" |
| 430 |
fi |
| 431 |
sh -c "${command} ${parameters}" |
| 432 |
fi |