#!/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