Debian Codegraph @

src/TestDB/1-install-database

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 -eu
33
34
35 if [ $# -ne 2 ] ; then
36 echo >&2 "Usage: $0 mariadb|mongodb|mysql|postgresql users.conf"
37 exit 1
38 fi
39 if [ ! -e "$2" ] ; then
40 echo >&2 "ERROR: Use configuration $2 does not exist!"
41 fi
42
43
44 # ====== Check/set environment variables ====================================
45 if [ ! -e /etc/os-release ] ; then
46 echo >&2 "ERROR: /etc/os-release does not exist!"
47 exit 1
48 fi
49 . /etc/os-release
50
51 DOMAIN="test.local"
52 ROOT_PASSWORD="!root!"
53 DATABASE="just_a_test_database"
54 MAINTAINER_PASSWORD="!maintainer!"
55 IMPORTER_PASSWORD="!importer!"
56 RESEARCHER_PASSWORD="!researcher!"
57 # shellcheck disable=SC1090
58 . "$2"
59
60 if [ "${ROOT_PASSWORD}" == "!root!" ] || \
61 [ "${MAINTAINER_PASSWORD}" == "!maintainer!" ] || \
62 [ "${IMPORTER_PASSWORD}" == "!importer!" ] || \
63 [ "${RESEARCHER_PASSWORD}" == "!researcher!" ] ; then
64 echo >&2 "DO NOT USE THE EXAMPLE PASSWORDS!"
65 exit 1
66 fi
67
68
69 # ###### MariaDB ############################################################
70 if [ "$1" == "mariadb" ] ; then
71 # Documentation: https://www.digitalocean.com/community/tutorials/how-to-install-mariadb-on-ubuntu-22-04
72 server="mariadb"
73
74 # ====== Install MariaDB =================================================
75 echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Installing MariaDB ...\x1b[0m"
76
77 if [ "${ID}" == "ubuntu" ] || [ "${ID}" == "debian" ] ; then
78 sudo DEBIAN_FRONTEND=noninteractive apt install -y -o APT::Keep-Downloaded-Packages=true mariadb-server mariadb-backup mariadb-client libmariadb-dev openssl
79 elif [ "${ID}" == "fedora" ] ; then
80 sudo dnf install -y mariadb-server mariadb-backup mariadb-connector-c-devel openssl
81 sudo systemctl enable mariadb.service
82 sudo systemctl start mariadb.service
83 elif [ "${ID}" == "freebsd" ] ; then
84 sudo pkg install -y mariadb118-server mariadb118-client openssl
85 sudo sysrc mysql_enable=YES
86 sudo service mysql-server start
87 else
88 echo >&2 "ERROR: Unknown ID ${ID} in /etc/os-release. The installation script may need an update for supporting this system!"
89 exit 1
90 fi
91
92 # ====== Basic configuration =============================================
93 sudo ./name-in-etc-hosts "${server}.${DOMAIN}"
94 sudo ./generate-test-certificates /etc/ssl "${server}.${DOMAIN}"
95
96 # MariaDB needs separate certificate and key files.
97 # The chain is in the CA file, it must not be in the certificate file!
98 # Details: https://mariadb.com/kb/en/securing-connections-for-client-and-server/
99 MARIADB_CA_FILE="/etc/ssl/TestLevel1/certs/TestLevel1.crt"
100 MARIADB_CRL_FILE="/etc/ssl/TestGlobal.crl"
101 MARIADB_CRT_FILE="/etc/ssl/${server}.${DOMAIN}/${server}.${DOMAIN}.crt"
102 MARIADB_KEY_FILE="/etc/ssl/${server}.${DOMAIN}/${server}.${DOMAIN}.key"
103 sudo chown mysql:mysql ${MARIADB_KEY_FILE}
104
105 # Set root password, and remove test user and database.
106 (
107 cat <<EOF
108 ALTER USER 'root'@'localhost' IDENTIFIED BY '${ROOT_PASSWORD}';
109 DELETE FROM mysql.user WHERE User='';
110 DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
111 DROP DATABASE IF EXISTS ${DATABASE};
112 DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
113 FLUSH PRIVILEGES;
114 EOF
115 ) | sudo mysql --user=root
116
117 # Enable network access:
118 if [ "${ID}" == "ubuntu" ] || [ "${ID}" == "debian" ] ; then
119 sudo sed \
120 -e '/^event_scheduler[ \t]*=.*$/d' -e 's/\(^\[mysqld\].*\)$/\1\nevent_scheduler = ON/' \
121 -e 's/^\([#]\|\)\(max_allowed_packet[ \t]*\)=.*$/\2= 1G/g' \
122 -e 's/^\(bind-address[ \t]*\)=.*$/\1= */g' \
123 -e "s%\([#][ \t]*\|\)ssl-ca[ ]*=[ ]*.*%ssl-ca = ${MARIADB_CA_FILE}\nssl-crl = ${MARIADB_CRL_FILE}%" \
124 -e "s%\([#][ \t]*\|\)ssl-cert[ ]*=[ ]*.*%ssl-cert = ${MARIADB_CRT_FILE}%" \
125 -e "s%\([#][ \t]*\|\)ssl-key[ ]*=[ ]*.*%ssl-key = ${MARIADB_KEY_FILE}%" \
126 -e "s%\([#][ \t]*\|\)require-secure-transport[ ]*=[ ]*.*%require-secure-transport = on\ntls_version = TLSv1.3%" \
127 /etc/mysql/mariadb.conf.d/50-server.cnf | sudo tee /etc/mysql/mariadb.conf.d/50-server.cnf.new >/dev/null && \
128 sudo diff --color /etc/mysql/mariadb.conf.d/50-server.cnf /etc/mysql/mariadb.conf.d/50-server.cnf.new || true && \
129 sudo mv /etc/mysql/mariadb.conf.d/50-server.cnf.new /etc/mysql/mariadb.conf.d/50-server.cnf
130 sudo service mariadb restart
131 elif [ "${ID}" == "fedora" ] ; then
132 sudo sed \
133 -e '/^event_scheduler[ \t]*=.*$/d' -e 's/\(^\[mysqld\].*\)$/\1\nevent_scheduler = ON/' \
134 -e "s%^#bind-address[ \t]*=.*%max_allowed_packet = 1G\nbind-address = *\nssl-ca = ${MARIADB_CA_FILE}\nssl-crl = ${MARIADB_CRL_FILE}\nssl-cert = ${MARIADB_CRT_FILE}\nssl-key = ${MARIADB_KEY_FILE}\nrequire-secure-transport = on\ntls_version = TLSv1.3%g" \
135 /etc/my.cnf.d/mariadb-server.cnf | sudo tee /etc/my.cnf.d/mariadb-server.cnf.new && \
136 sudo diff --color /etc/my.cnf.d/mariadb-server.cnf /etc/my.cnf.d/mariadb-server.cnf.new || true && \
137 sudo mv /etc/my.cnf.d/mariadb-server.cnf.new /etc/my.cnf.d/mariadb-server.cnf
138 sudo service mariadb restart
139 elif [ "${ID}" == "freebsd" ] ; then
140 sudo sed \
141 -e '/^event_scheduler[ \t]*=.*$/d' -e 's/\(^\[mysqld\].*\)$/\1\nevent_scheduler = ON/' \
142 -e "s%^bind-address[ \t]*=.*%max_allowed_packet = 1G\nbind-address = *\nssl-ca = ${MARIADB_CA_FILE}\nssl-crl = ${MARIADB_CRL_FILE}\nssl-cert = ${MARIADB_CRT_FILE}\nssl-key = ${MARIADB_KEY_FILE}\nrequire-secure-transport = on\ntls_version = TLSv1.3%g" \
143 /usr/local/etc/mysql/conf.d/server.cnf | sudo tee /usr/local/etc/mysql/conf.d/server.cnf.new && \
144 sudo diff --color /usr/local/etc/mysql/conf.d/server.cnf /usr/local/etc/mysql/conf.d/server.cnf.new || true && \
145 sudo mv /usr/local/etc/mysql/conf.d/server.cnf.new /usr/local/etc/mysql/conf.d/server.cnf
146 sudo service mysql-server restart
147 fi
148
149 # ====== Test configuration ==============================================
150 ./test-tls-connection "${server}.${DOMAIN}:3306" /etc/ssl/TestLevel1/certs/TestLevel1.crt -- -starttls mysql
151
152
153 # ###### PostgreSQL #########################################################
154 elif [ "$1" == "postgresql" ] ; then
155 # Documentation: https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-22-04-quickstart
156 server="postgresql"
157
158 # ====== Install PostgreSQL ==============================================
159 echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Installing PostgreSQL ...\x1b[0m"
160
161 if [ "${ID}" == "ubuntu" ] || [ "${ID}" == "debian" ] ; then
162 sudo DEBIAN_FRONTEND=noninteractive apt install -y -o APT::Keep-Downloaded-Packages=true openssl postgresql-all postgresql-contrib
163 elif [ "${ID}" == "fedora" ] ; then
164 sudo dnf install -y openssl postgresql-server libpqxx-devel
165 sudo -u postgres /usr/bin/postgresql-setup --initdb || true
166 sudo systemctl enable postgresql.service
167 sudo systemctl start postgresql.service
168 elif [ "${ID}" == "freebsd" ] ; then
169 sudo pkg install -y postgresql18-server postgresql18-client postgresql-libpqxx openssl
170 sudo sysrc postgresql_enable="YES"
171 sudo /usr/local/etc/rc.d/postgresql initdb
172 sudo service postgresql start
173 else
174 echo >&2 "ERROR: Unknown ID ${ID} in /etc/os-release. The installation script may need an update for supporting this system!"
175 exit 1
176 fi
177
178 # ====== Basic configuration =============================================
179 sudo ./name-in-etc-hosts "${server}.${DOMAIN}"
180 sudo ./generate-test-certificates /etc/ssl "${server}.${DOMAIN}"
181
182 # PostgreSQL needs separate certificate and key files.
183 # The chain is in the CA file, it must not be in the certificate file!
184 POSTGRESQL_CA_FILE="/etc/ssl/TestLevel1/certs/TestLevel1.crt"
185 POSTGRESQL_CRL_FILE="/etc/ssl/TestGlobal.crl"
186 POSTGRESQL_CRT_FILE="/etc/ssl/${server}.${DOMAIN}/${server}.${DOMAIN}.crt"
187 POSTGRESQL_KEY_FILE="/etc/ssl/${server}.${DOMAIN}/${server}.${DOMAIN}.key"
188 sudo chown postgres:postgres ${POSTGRESQL_KEY_FILE}
189
190 POSTGRESDIR=""
191 if [ -d /etc/postgresql ] ; then
192 POSTGRESDIR="$(find /etc/postgresql/ -mindepth 2 -maxdepth 2 -type d -name main | sort -r | head -n1)"
193 elif [ -d /var/db/postgres ] ; then
194 POSTGRESDIR="$(find /var/db/postgres -mindepth 1 -maxdepth 1 -type d -name "data*" | sort -r | head -n1)"
195 elif sudo -u postgres test -d /var/lib/pgsql/data ; then
196 POSTGRESDIR="/var/lib/pgsql/data"
197 fi
198 if [ "${POSTGRESDIR}" == "" ] ; then
199 echo >&2 "ERROR: Cannot find the PostgreSQL configuration directory!"
200 exit 1
201 fi
202 echo "Using PostgreSQL settings in ${POSTGRESDIR}!"
203
204 # Enable network access:
205 sudo -u postgres sed \
206 -e 's%^\(host[ \t]*all[ \t]*all[ \t]*\)127.0.0.1/32\([ \t]*\).*$%\10.0.0.0/0 \2scram-sha-256%g' \
207 -e 's%^\(host[ \t]*all[ \t]*all[ \t]*\)::1/128\([ \t]*\).*$%\1::/0 \2scram-sha-256%g' \
208 "${POSTGRESDIR}"/pg_hba.conf | sudo -u postgres tee "${POSTGRESDIR}"/pg_hba.conf.new >/dev/null && \
209 sudo -u postgres diff --color "${POSTGRESDIR}"/pg_hba.conf.new "${POSTGRESDIR}"/pg_hba.conf || true && \
210 sudo -u postgres mv -f "${POSTGRESDIR}"/pg_hba.conf.new "${POSTGRESDIR}"/pg_hba.conf
211
212 sudo -u postgres sed -r \
213 -e "s%#listen_addresses = 'localhost'%listen_addresses = '*' %" \
214 -e "s%^(#ssl|ssl) =.*%ssl = on%" \
215 -e "s%^(#ssl|ssl)_ca_file = '.*'%ssl_ca_file = '${POSTGRESQL_CA_FILE}'%" \
216 -e "s%^(#ssl|ssl)_crl_file = '.*'%ssl_crl_file = '${POSTGRESQL_CRL_FILE}'%" \
217 -e "s%^(#ssl|ssl)_cert_file = '.*'%ssl_cert_file = '${POSTGRESQL_CRT_FILE}'%" \
218 -e "s%^(#ssl|ssl)_key_file = '.*'%ssl_key_file = '${POSTGRESQL_KEY_FILE}'%" \
219 -e "s%#ssl_ciphers = '.*'%ssl_ciphers = 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256'%" \
220 -e "s%#ssl_prefer_server_ciphers = on%ssl_prefer_server_ciphers = on%" \
221 -e "s%#ssl_min_protocol_version = 'TLSv1.2'%ssl_min_protocol_version = 'TLSv1.3'%" \
222 "${POSTGRESDIR}"/postgresql.conf | sudo -u postgres tee "${POSTGRESDIR}"/postgresql.conf.new >/dev/null && \
223 sudo -u postgres diff --color "${POSTGRESDIR}"/postgresql.conf.new "${POSTGRESDIR}"/postgresql.conf || true && \
224 sudo -u postgres mv -f "${POSTGRESDIR}"/postgresql.conf.new "${POSTGRESDIR}"/postgresql.conf
225
226 sudo service postgresql restart
227 sleep 5
228
229 # ====== Test configuration ==============================================
230 ./test-tls-connection "${server}.${DOMAIN}:5432" /etc/ssl/TestLevel1/certs/TestLevel1.crt -- -starttls postgres
231
232
233 # ###### MongoDB ############################################################
234 elif [ "$1" == "mongodb" ] ; then
235 # Documentation: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
236 server="mongodb"
237
238 # ====== Install MongoDB =================================================
239 echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Installing MongoDB ...\x1b[0m"
240
241 MONGODB_VERSION="8.0"
242 MONGODB_KEY="${MONGODB_VERSION}"
243
244 if [ "${ID}" == "ubuntu" ] || [ "${ID}" == "debian" ] ; then
245 for key in ${MONGODB_VERSION} dev ; do
246 wget -qO - "https://www.mongodb.org/static/pgp/server-${key}.asc" | gpg --dearmor | sudo tee "/usr/share/keyrings/mongodb-${key}.gpg" >/dev/null
247 done
248 . /etc/os-release
249 (
250 if [ "${ID}" == "ubuntu" ] ; then
251 codename="${UBUNTU_CODENAME}"
252 if [[ ! "${codename}" =~ ^(precise|trusty|xenial|bionic|focal|jammy|noble)$ ]] ; then
253 # MongoDB only provides repositories for LTS versions.
254 # Assuming the latest LTS version here, for testing on non-LTS Ubuntu:
255 codename="noble"
256 fi
257 cat <<EOF
258 deb [ signed-by=/usr/share/keyrings/mongodb-${MONGODB_KEY}.gpg ] https://repo.mongodb.org/apt/ubuntu ${codename}/mongodb-org/${MONGODB_VERSION} multiverse
259 deb [ signed-by=/usr/share/keyrings/mongodb-dev.gpg ] https://repo.mongodb.org/apt/ubuntu ${codename}/mongodb-org/development multiverse
260 EOF
261 else
262 cat <<EOF
263 deb [ signed-by=/usr/share/keyrings/mongodb-${MONGODB_KEY}.gpg ] https://repo.mongodb.org/apt/debian ${VERSION_CODENAME}/mongodb-org/${MONGODB_VERSION} main
264 deb [ signed-by=/usr/share/keyrings/mongodb-dev.gpg ] https://repo.mongodb.org/apt/debian ${VERSION_CODENAME}/mongodb-org/development main
265 EOF
266 fi
267 ) | sudo tee /etc/apt/sources.list.d/mongodb-org.list >/dev/null
268 sudo apt update -qq
269 sudo DEBIAN_FRONTEND=noninteractive apt install -y -o APT::Keep-Downloaded-Packages=true mongodb-org mongodb-mongosh openssl
270 sudo systemctl enable mongod.service
271
272 elif [ "${ID}" == "fedora" ] ; then
273 (
274 cat <<EOF
275 [mongodb-org]
276 name=MongoDB ${MONGODB_VERSION} Repository
277 baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/${MONGODB_VERSION}/x86_64/
278 gpgcheck=1
279 enabled=1
280 gpgkey=https://www.mongodb.org/static/pgp/server-${MONGODB_KEY}.asc
281 EOF
282 ) | sudo tee /etc/yum.repos.d/mongodb-org.repo
283 # (
284 # cat <<EOF
285 # [mongodb-org-dev]
286 # name=MongoDB Development Repository
287 # baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/development/x86_64/
288 # gpgcheck=1
289 # enabled=1
290 # gpgkey=https://www.mongodb.org/static/pgp/server-dev.asc
291 # EOF
292 # ) | sudo tee /etc/yum.repos.d/mongodb-org-dev.repo
293 sudo dnf install -y mongodb-org mongodb-mongosh-shared-openssl3 openssl
294 sudo systemctl enable mongod.service
295 sudo systemctl start mongod.service
296
297 elif [ "${ID}" == "freebsd" ] ; then
298 sudo pkg install -y mongodb80 mongodb-tools mongosh mongo-c-driver
299 sudo sysrc mongod_enable="YES"
300 sudo service mongod start
301
302 else
303 echo >&2 "ERROR: Unknown ID ${ID} in /etc/os-release. The installation script may need an update for supporting this system!"
304 exit 1
305 fi
306
307 # ====== Basic configuration =============================================
308 sudo ./name-in-etc-hosts "${server}.${DOMAIN}"
309 sudo ./generate-test-certificates /etc/ssl "${server}.${DOMAIN}"
310
311 # MongoDB wants key + certificate + chain (in this order!) in the PEM file:
312 MONGODB_CA_FILE="/etc/ssl/TestLevel1/certs/TestLevel1.crt"
313 MONGODB_CRL_FILE="/etc/ssl/TestGlobal.crl"
314 MONGODB_CRT_KEY_FILE="/etc/ssl/${server}.${DOMAIN}/${server}.${DOMAIN}.key+crt"
315 sudo touch ${MONGODB_CRT_KEY_FILE}
316 sudo chmod 600 ${MONGODB_CRT_KEY_FILE}
317 sudo cat /etc/ssl/${server}.${DOMAIN}/${server}.${DOMAIN}.key \
318 /etc/ssl/${server}.${DOMAIN}/${server}.${DOMAIN}.crt | \
319 sudo tee ${MONGODB_CRT_KEY_FILE} >/dev/null
320 if [ "${ID}" == "fedora" ] ; then
321 sudo chown mongod:mongod ${MONGODB_CRT_KEY_FILE}
322 else
323 sudo chown mongodb:mongodb ${MONGODB_CRT_KEY_FILE}
324 fi
325
326
327 # WARNING: After installation, MongoDB gives everybody on 127.0.0.1
328 # full access!
329 # The following settings enable authorization check, restricting access
330 # to the configured user (root).
331 if [ "${ID}" == "freebsd" ] ; then
332 mongodConf=/usr/local/etc/mongodb.conf
333 else
334 mongodConf=/etc/mongod.conf
335 fi
336
337 cp ${mongodConf} /tmp/
338 sudo sed \
339 -e "s!# engine:! directoryPerDB: true!" \
340 -e "s!# wiredTiger:! wiredTiger:\n engineConfig:\n directoryForIndexes: true!" \
341 ${mongodConf} | sudo tee ${mongodConf}.new
342 sudo service mongod stop
343 sudo mv ${mongodConf}.new ${mongodConf}
344 sudo service mongod start
345 sleep 5
346
347 # ====== Make sure there is a root user ==================================
348 mongosh --quiet <<EOF
349 use admin
350 db.dropUser("root")
351 db.createUser({ user: "root",
352 pwd: "${ROOT_PASSWORD}",
353 roles: [
354 "userAdminAnyDatabase",
355 "dbAdminAnyDatabase",
356 "readWriteAnyDatabase",
357 "clusterAdmin"
358 ] })
359 quit()
360 EOF
361
362 # ====== Configure MongoDB ===============================================
363 # The following settings enable authorization check, restricting access
364 # to the configured user (root):
365 cp ${mongodConf} /tmp/
366 sudo sed \
367 -e "s!^ bindIp: 127.0.0.1.*! ipv6: true\n bindIpAll: true\n tls:\n mode: requireTLS\n CAFile: ${MONGODB_CA_FILE}\n CRLFile: ${MONGODB_CRL_FILE}\n certificateKeyFile: ${MONGODB_CRT_KEY_FILE}\n disabledProtocols: TLS1_0,TLS1_1,TLS1_2\n allowConnectionsWithoutCertificates: true!" \
368 -e "s!#security:!security:\n authorization: enabled!" \
369 -e "s!# engine:! directoryPerDB: true!" \
370 -e "s!# wiredTiger:! wiredTiger:\n engineConfig:\n directoryForIndexes: true!" \
371 ${mongodConf} | sudo tee ${mongodConf}.new
372 sudo service mongod stop
373 sudo mv ${mongodConf}.new ${mongodConf}
374 sudo service mongod start
375 sleep 5
376
377 # ====== Test configuration ==============================================
378 ./test-tls-connection "${server}.${DOMAIN}:27017" /etc/ssl/TestLevel1/certs/TestLevel1.crt
379
380
381 # ###### Error ##############################################################
382 else
383 echo >&2 "ERROR: Unsupported database: $1"
384 exit 1
385 fi