Files
deploy/remote/db_login.sh
T
Christian Manivong ec7d113cdb
CI / check (pull_request) Successful in 22s
feat: the bundled database's application gets its own restricted login (NetOrk/netork#2937)
remote/db_login.sh runs after .env names the version and before the broker
and the engine: a container of the engine image being deployed
(python -m netork.core.db_role_env) rewrites .env so that the owner's URL
becomes MIGRATION_DATABASE_URL and DATABASE_URL a netork_app login with a
password made on the host; netork-api's db_init creates the role. No bundled
database, an image from before the module, an .env separated already or
RESTRICTED_DB_LOGIN=off: .env stays as it is. Any other failure stops the
deploy before the engine is touched.
2026-12-02 02:35:23 +01:00

55 lines
2.1 KiB
Bash

#!/bin/bash
# db_login.sh: on the bundled database, netOrk's application gets a login of its
# own that cannot change the audit log (NetOrk/netork#2937). deploy.sh runs it on
# the host, in ~/netork, after the database step and before the engine is
# recreated, so the new containers start with the new .env.
#
# The engine image being deployed decides, not this script: `python -m
# netork.core.db_role_env` reads .env and answers with the whole new file (exit 0)
# or nothing (exit 3: separated already, switched off with RESTRICTED_DB_LOGIN=off,
# or not the bundled database's owner login). The password it makes goes from the
# container into .env and nowhere else; nothing of the file is printed.
#
# - No bundled database (external PostgreSQL): nothing to do; there the role is an
# infrastructure change.
# - An image from before the module: .env stays as it is, the deploy goes on.
# - Any other failure: exit 1, .env as it was, the deploy stops before the engine.
#
# Env: ENGINE_IMAGE, the engine image of the version being deployed.
set -euo pipefail
cd ~/netork
if [ -z "$(docker ps -aq --filter label=com.docker.compose.project=netork \
--filter label=com.docker.compose.service=postgres | head -1)" ]; then
echo "no bundled database: the application's database login stays as it is"
exit 0
fi
umask 077
new=.env.db-login.new
err=.env.db-login.err
trap 'rm -f "$new" "$err"' EXIT
status=0
docker run --rm -i --network none --entrypoint python "$ENGINE_IMAGE" \
-m netork.core.db_role_env < .env > "$new" 2> "$err" || status=$?
case $status in
0)
# in place: .env keeps its owner and mode
cat "$new" > .env
echo "the application now has its own restricted database login (netork_app)"
;;
3)
echo "the application's database login stays as it is"
;;
*)
if grep -q "No module named netork.core.db_role_env" "$err"; then
echo "this version cannot restrict the application's database login: it stays as it is"
exit 0
fi
echo "could not set up the application's database login (exit $status):" >&2
tail -c 500 "$err" >&2
exit 1
;;
esac