Debian Codegraph @

src/dbshell

raw symbols SCIP
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 -euo pipefail
33
34
35 # ###### Usage ##############################################################
36 usage () {
37 local exitCode="$1"
38 echo >&2 "Usage: $0 configuration_file [-d|--dry-run] [-q|--quiet] [-w|--verbose] [-B|--write-dbeaver-config file_prefix] [-h|--help] [-v|--version]"
39 exit "${exitCode}"
40 }
41
42
43 # ###### Version ############################################################
44 version () {
45 echo "DBShell @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@"
46 exit 0
47 }
48
49
50
51 # ###### Main program #######################################################
52
53 # ====== Handle arguments ===================================================
54 if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then
55 echo "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!"
56 exit 1
57 fi
58 UNAME="$(uname)"
59 case "${UNAME}" in
60 FreeBSD)
61 GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || true)"
62 ;;
63 NetBSD)
64 GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || true)"
65 ;;
66 OpenBSD)
67 GETOPT="$(command -v gnugetopt || true)"
68 ;;
69 Darwin)
70 GETOPT="$(env PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || true)"
71 ;;
72 *)
73 GETOPT="$(command -v getopt || true)"
74 ;;
75 esac
76 if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then
77 echo >&2 "ERROR: Enhanced/GNU getopt is required!"
78 exit 1
79 fi
80 options="$(${GETOPT} -o dqwB:hv --long dry-run,quiet,verbose,write-dbeaver-config:,help,version -a -- "$@")"
81 # shellcheck disable=SC2181
82 if [[ $? -ne 0 ]]; then
83 usage 1
84 fi
85
86 DRY_RUN=0
87 QUIET=0
88 DBEAVER_CONFIG_PREFIX=""
89 eval set -- "${options}"
90 while [ $# -gt 0 ] ; do
91 case "$1" in
92 -B | --write-dbeaver-config)
93 DBEAVER_CONFIG_PREFIX="$2"
94 shift
95 ;;
96 -d | --dry-run)
97 DRY_RUN=1
98 ;;
99 -q | --quiet)
100 QUIET=1
101 ;;
102 -w | --verbose)
103 QUIET=0
104 ;;
105 -h | --help)
106 usage 0
107 ;;
108 -v | --version)
109 version
110 ;;
111 --)
112 shift
113 break
114 ;;
115 *)
116 # This should not happen: wrong getopt parameters, or missing case?
117 echo >&2 "INTERNAL ERROR: Unhandled option $1!"
118 exit 1
119 ;;
120 esac
121 shift
122 done
123 if [ $# -ne 1 ] ; then
124 usage 1
125 fi
126 CONFIGURATION_FILE="$1"
127 if [ ! -e "${CONFIGURATION_FILE}" ] ; then
128 echo >&2 "ERROR: User configuration ${CONFIGURATION_FILE} does not exist!"
129 exit 1
130 fi
131
132
133 # ====== Read configuration file ============================================
134 dbbackend=""
135 dbserver=""
136 dbport=""
137 dbcafile=""
138 dbcrlfile=""
139 dbkeyfile=""
140 dbcertfile=""
141 dbcertkeyfile=""
142 dbconnectionflags=""
143 dbuser=""
144 dbpassword=""
145 database=""
146 configContent=$(sed -r '/[^=#]+=[^=]+/!d' <"${CONFIGURATION_FILE}" | sed -r "s/[[:space:]]*=[[:space:]]*(.*)[[:space:]]*/='\1'/g")
147 eval "${configContent}"
148 if [ "${dbcafile}" == "NONE" ] ; then
149 dbcafile=""
150 fi
151 if [ "${dbcrlfile}" == "NONE" ] ; then
152 dbcrlfile=""
153 fi
154 if [ "${dbkeyfile}" == "NONE" ] ; then
155 dbkeyfile=""
156 fi
157 if [ "${dbcertfile}" == "NONE" ] ; then
158 dbcertfile=""
159 fi
160 if [ "${dbcertkeyfile}" == "NONE" ] ; then
161 dbcertkeyfile=""
162 fi
163 tls_enabled=1
164 tls_check_certificate=1
165 tls_check_hostname=1
166 for dbconnectionflag in ${dbconnectionflags} ; do
167 if [ "${dbconnectionflag}" == "DisableTLS" ] ; then
168 tls_enabled=0
169 tls_check_certificate=0
170 tls_check_hostname=0
171 elif [ "${dbconnectionflag}" == "AllowInvalidCertificate" ] ; then
172 tls_check_certificate=0
173 tls_check_hostname=0
174 elif [ "${dbconnectionflag}" == "AllowInvalidHostname" ] ; then
175 tls_check_certificate=1
176 tls_check_hostname=0
177 elif [ "${dbconnectionflag}" != "NONE" ] ; then
178 echo >&2 "ERROR: Invalid connection flag ${dbconnectionflag}!"
179 exit 1
180 fi
181 done
182
183
184 # ====== Build the parameters line =============================================
185 oldCommand=0
186 command=""
187 parameters=""
188 if [ "${dbbackend}" == "MariaDB" ] || [ "${dbbackend}" == "MySQL" ] ; then
189
190 if [ "${dbbackend}" == "MariaDB" ] ; then
191 command="$(which mariadb || true)"
192 if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then
193 echo >&2 "ERROR: MariaDB client (mariadb) not found. Is it installed?"
194 exit 1
195 fi
196 else
197 command="$(which mysql || true)"
198 if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then
199 echo >&2 "ERROR: MySQL client (mysql) not found. Is it installed?"
200 exit 1
201 fi
202 fi
203 if [ "${dbport}" == "0" ] ; then
204 dbport="3306"
205 fi
206 parameters="${parameters} --silent --host=\"${dbserver}\" --port=${dbport} --protocol=tcp --user=\"${dbuser}\" --password=\"${dbpassword}\" --database=\"${database}\""
207 if [ ${tls_enabled} -eq 1 ] && [ "${dbcafile}" != "" ] ; then
208 if [ ${tls_check_certificate} -eq 1 ] || [ ${tls_check_hostname} -eq 1 ] ; then
209 parameters="${parameters} --ssl-verify-server-cert"
210 fi
211 parameters="${parameters} --ssl-ca=\"${dbcafile}\""
212 if [ "${dbcrlfile}" != "" ] ; then
213 parameters="${parameters} --ssl-crl \"${dbcrlfile}\""
214 fi
215 fi
216
217 elif [ "${dbbackend}" == "PostgreSQL" ] ; then
218
219 command="$(which psql || true)"
220 if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then
221 echo >&2 "ERROR: PostgreSQL client (psql) not found. Is it installed?"
222 exit 1
223 fi
224 if [ "${dbport}" == "0" ] ; then
225 dbport="5432"
226 fi
227 parameters="--quiet --host=\"${dbserver}\" --port=${dbport} --username=\"${dbuser}\" --dbname=\"${database}\""
228 export PGPASSWORD="${dbpassword}"
229 if [ ${tls_enabled} -eq 1 ] && [ "${dbcafile}" != "" ] ; then
230 if [ ${tls_check_certificate} -eq 1 ] ; then
231 if [ ${tls_check_hostname} -eq 1 ] ; then
232 export PGSSLMODE="verify-full"
233 else
234 export PGSSLMODE="verify-ca"
235 fi
236 else
237 export PGSSLMODE="require"
238 fi
239 export PGSSLROOTCERT="${dbcafile}"
240 if [ "${dbcrlfile}" != "" ] ; then
241 export PGSSLCRL="${dbcrlfile}"
242 fi
243 else
244 export PGSSLMODE="prefer"
245 fi
246
247 elif [ "${dbbackend}" == "MongoDB" ] ; then
248
249 command="$(which mongosh || true)"
250 if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then
251 command="$(which mongo || true)"
252 if [ "${DRY_RUN}" -eq 0 ] && [ "${command}" == "" ] ; then
253 echo >&2 "ERROR: MongoDB client (mongosh or mongo) not found. Is it installed?"
254 exit 1
255 else
256 oldCommand=1
257 fi
258 fi
259 if [ "${dbport}" == "0" ] ; then
260 dbport="27017"
261 fi
262 parameters="\"mongodb://${dbserver}:${dbport}/${database}\" --username \"${dbuser}\" --password \"${dbpassword}\""
263 if [ ${tls_enabled} -eq 1 ] && [ "${dbcafile}" != "" ] ; then
264 if [ ${oldCommand} -eq 0 ] ; then
265 parameters="${parameters} --tls --tlsDisabledProtocols TLS1_0,TLS1_1,TLS1_2 --tlsCAFile \"${dbcafile}\""
266 if [ ${tls_check_certificate} -eq 0 ] ; then
267 parameters="${parameters} --tlsAllowInvalidCertificates"
268 elif [ ${tls_check_hostname} -eq 0 ] ; then
269 parameters="${parameters} --tlsAllowInvalidHostnames"
270 fi
271 if [ "${dbcrlfile}" != "" ] ; then
272 # FIXME: --tlsCRLFile \"${dbcrlfile}\" does not work!
273 # parameters="${parameters} --tlsCRLFile \"${dbcrlfile}\""
274 true
275 fi
276 else
277 parameters="${parameters} --ssl --sslDisabledProtocols TLS1_0,TLS1_1,TLS1_2 --sslCAFile \"${dbcafile}\""
278 if [ "${dbcrlfile}" != "" ] ; then
279 parameters="${parameters} --sslCRLFile \"${dbcrlfile}\""
280 fi
281 fi
282 fi
283 if [ ${oldCommand} -eq 0 ] ; then
284 parameters="${parameters} --quiet"
285 fi
286
287 else
288
289 echo >&2 "ERROR: Invalid dbbackend setting \"${dbbackend}\"!"
290 exit 1
291
292 fi
293
294
295 # ====== Generate DBeaver configuration file ================================
296 if [ "${DBEAVER_CONFIG_PREFIX}" ] ; then
297 driver=""
298 provider=""
299 jdbc=""
300 custom=""
301 handler=""
302 dbusername="${dbuser}"
303
304 # ====== MariaDB/MySQL ===================================================
305 if [ "${dbbackend}" == "MariaDB" ] || [ "${dbbackend}" == "MySQL" ] ; then
306 provider="mysql"
307 handler="mysql_ssl"
308 # FIXME!
309 # Currently, the MariaDB driver is not working. Using MySQL driver instead
310 # => https://github.com/dbeaver/dbeaver/issues/35242
311 # if [ "${dbbackend}" == "MariaDB" ] ; then
312 # driver="mariaDB"
313 # jdbc="mariadb"
314 # else
315 driver="mysql8"
316 jdbc="mysql"
317 # fi
318 if [ ${tls_enabled} -eq 1 ] && [ "${dbcafile}" != "" ] ; then
319 ssl_require="true"
320 if [ ${tls_check_certificate} -eq 1 ] || [ ${tls_check_hostname} -eq 1 ] ; then
321 ssl_verify_server="true"
322 else
323 ssl_verify_server="false"
324 fi
325 else
326 ssl_require="false"
327 ssl_verify_server="false"
328 fi
329 custom=",
330 \"ssl_require\": \"${ssl_require}\",
331 \"ssl_verify.server\": \"${ssl_verify_server}\",
332 \"ssl.public.key.retrieve\": \"false\""
333
334 # ====== PostgreSQL ======================================================
335 elif [ "${dbbackend}" == "PostgreSQL" ] ; then
336 driver="postgres-jdbc"
337 provider="postgresql"
338 jdbc="postgresql"
339 handler="postgre_ssl"
340 if [ ${tls_check_certificate} -eq 1 ] ; then
341 if [ ${tls_check_hostname} -eq 1 ] ; then
342 ssl_mode="verify-full"
343 else
344 ssl_mode="verify-ca"
345 fi
346 else
347 ssl_mode="require"
348 fi
349 custom=",
350 \"sslMode\": \"${ssl_mode}\",
351 \"sslFactory\": \"\""
352 if [ "${dbuser}" == "postgres" ] ; then
353 dbusername="root"
354 fi
355
356 # ====== Unsupported =====================================================
357 else
358 if [ ${QUIET} -eq 0 ] ; then
359 echo >&2 "WARNING: DBeaver does not support ${dbbackend} backend!"
360 fi
361 echo "" >"${DBEAVER_CONFIG_PREFIX}-data-source.json"
362 echo "" >"${DBEAVER_CONFIG_PREFIX}-credentials-config.json"
363 fi
364
365 if [ "${driver}" != "" ] ; then
366 identifier="$(echo "${provider}://${dbusername}@${dbserver}:${dbport}/${database}" | md5sum --tag | cut -d' ' -f4)"
367
368 color="211,215,207"
369 if [ "${dbusername}" == "importer" ] ; then
370 color="237,212,0"
371 elif [ "${dbusername}" == "researcher" ] ; then
372 color="0,127,253"
373 elif [ "${dbusername}" == "maintainer" ] ; then
374 color="114,159,207"
375 elif [ "${dbusername}" == "root" ] ; then
376 color="239,41,41"
377 fi
378 # ansiColor="${color//,/;}"
379 # echo >&2 -e "\x1b[38;2;${ansiColor}mdbusername=${dbusername} database=${database} color=${color}\x1b[0m"
380
381 # ====== Generate DBeaver configuration ==================================
382 if [ ${QUIET} -eq 0 ] ; then
383 echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Writing DBeaver configuration to ${DBEAVER_CONFIG_PREFIX} ...\x1b[0m"
384 fi
385 (
386 cat << EOF
387 {
388 "folders": {},
389 "connection-types": {
390 "${dbusername}": {
391 "name": "${dbusername}",
392 "color": "${color}",
393 "description": "Regular database access for ${dbusername}",
394 "auto-commit": true,
395 "confirm-execute": false,
396 "confirm-data-change": false,
397 "smart-commit": false,
398 "smart-commit-recover": true,
399 "auto-close-transactions": true,
400 "close-transactions-period": 1800,
401 "auto-close-connections": true,
402 "close-connections-period": 14400
403 }
404 },
405 "connections": {
406 "${driver}-${identifier}": {
407 "provider": "${provider}",
408 "driver": "${driver}",
409 "name": "${dbusername}@${dbserver}:${dbport}/${database} ${dbbackend}",
410 "save-password": true,
411 "configuration": {
412 "host": "${dbserver}",
413 "port": "${dbport}",
414 "database": "${database}",
415 "url": "jdbc:${jdbc}://${dbserver}:${dbport}/${database}",
416 "configurationType": "MANUAL",
417 "type": "${dbusername}",
418 "closeIdleConnection": true,
419 "auth-model": "native",
420 "handlers": {
421 "${handler}": {
422 "type": "CONFIG",
423 "enabled": true,
424 "save-password": true,
425 "properties": {
426 "ssl.ca.cert": "${dbcafile}",
427 "ssl.client.cert": "${dbcertfile}",
428 "ssl.client.key": "${dbkeyfile}",
429 "ssl.method": "CERTIFICATES",
430 "ssl.public.key.retrieve": "false",
431 "ssl.cipher.suites": ""${custom}
432 }
433 }
434 }
435 }
436 }
437 }
438 }
439 EOF
440 ) >"${DBEAVER_CONFIG_PREFIX}-data-source.json"
441
442 (
443 cat << EOF
444 {
445 "${driver}-${identifier}": {
446 "#connection": {
447 "user": "${dbuser}",
448 "password": "${dbpassword}"
449 }
450 }
451 }
452 EOF
453 ) >"${DBEAVER_CONFIG_PREFIX}-credentials-config.json"
454 fi
455 fi
456
457
458 # ====== Run the parameters line ============================================
459 if [ ${QUIET} -eq 0 ] ; then
460 if [ ${DRY_RUN} -eq 0 ] ; then
461 echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Connecting to database ...\x1b[0m"
462 echo -en "\x1b[37m"
463 fi
464
465 if [ "${dbbackend}" == "PostgreSQL" ] ; then
466 export | grep "PGPASSWORD=\|PGSSL[A-Z]*=" | sort || true
467 fi
468 echo "${command} ${parameters}"
469 fi
470
471 if [ ${DRY_RUN} -eq 0 ] ; then
472 if [ ${QUIET} -eq 0 ] ; then
473 echo -en "\x1b[0m"
474 fi
475 sh -c "${command} ${parameters}"
476 fi