challenger

OAuth 2.0-based authentication service that validates user can receive messages at a certain address
Log | Files | Refs | Submodules | README | LICENSE

commit 4c2ebdb4434177e1a35d9dba8f7d82da48e3d047
parent 67d1e14bcf9529b99937f904ecc5b7074345102e
Author: Florian Dold <florian@dold.me>
Date:   Fri, 23 Aug 2024 15:25:04 +0200

new-style dbconfig

Diffstat:
Mcontrib/challenger-dbconfig | 165+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 86 insertions(+), 79 deletions(-)

diff --git a/contrib/challenger-dbconfig b/contrib/challenger-dbconfig @@ -23,108 +23,115 @@ RESET_DB=0 SKIP_DBINIT=0 DBUSER="challenger-httpd" DBNAME="challenger" -CFGFILE="/etc/challenger/secrets/challenger-db.secret.conf" +CFGFILE="/etc/challenger/challenger.conf" # Parse command-line options while getopts ':hn:rsu:' OPTION; do - case "$OPTION" in - h) - echo 'Supported options:' - echo " -c FILENAME -- write configuration to FILENAME (default: $CFGFILE)" - echo " -n NAME -- user NAME for database name (default: $DBNAME)" - echo " -r -- reset database (dangerous)" - echo " -s -- skip database initialization" - echo " -u USER -- challenger-httpd to be run by USER (default: $DBUSER)" - exit 0 - ;; - n) - DBNAME="$OPTARG" - ;; - r) - RESET_DB="1" - ;; - s) - SKIP_DBINIT="1" - ;; - u) - DBUSER="$OPTARG" - ;; - ?) - exit_fail "Unrecognized command line option" - ;; - esac + case "$OPTION" in + h) + echo 'Supported options:' + echo " -c FILENAME -- write configuration to FILENAME (default: $CFGFILE)" + echo " -n NAME -- user NAME for database name (default: $DBNAME)" + echo " -r -- reset database (dangerous)" + echo " -s -- skip database initialization" + echo " -u USER -- challenger-httpd to be run by USER (default: $DBUSER)" + exit 0 + ;; + c) + CFGFILE="$OPTARG" + ;; + n) + DBNAME="$OPTARG" + ;; + r) + RESET_DB="1" + ;; + s) + SKIP_DBINIT="1" + ;; + u) + DBUSER="$OPTARG" + ;; + ?) + exit_fail "Unrecognized command line option" + ;; + esac done -if ! id postgres > /dev/null -then - echo "Could not find 'postgres' user. Please install Postgresql first" - exit 1 +if ! id postgres >/dev/null; then + echo "Could not find 'postgres' user. Please install Postgresql first" + exit 1 fi -if [ "$(id -u)" -ne 0 ] -then - echo "This script must be run as root" - exit 1 +if [ "$(id -u)" -ne 0 ]; then + echo "This script must be run as root" + exit 1 fi -if [ 0 = "$SKIP_DBINIT" ] -then - if ! challenger-dbinit -v 2> /dev/null - then - echo "Required 'challenger-dbinit' not found. Please fix your installation." - fi +if [ 0 = "$SKIP_DBINIT" ]; then + if ! challenger-dbinit -v 2>/dev/null; then + echo "Required 'challenger-dbinit' not found. Please fix your installation." + fi + DBINIT=$(which challenger) fi -if ! id "$DBUSER" > /dev/null -then - echo "Could not find '$DBUSER' user. Please set it up first" - exit 1 -fi - -if sudo -i -u postgres psql "$DBNAME" < /dev/null 2> /dev/null -then - if [ 1 = "$RESET_DB" ] - then - echo "Deleting existing database $DBNAME." 1>&2 - sudo -i -u postgres dropdb "$DBNAME" - else - echo "Database '$DBNAME' already exists, refusing to setup again." - echo "Use -r to delete the existing database first (dangerous!)." - exit 77 - fi +if ! id "$DBUSER" >/dev/null; then + echo "Could not find '$DBUSER' user. Please create it up first" + exit 1 fi echo "Setting up database user $DBUSER." 1>&2 -if ! sudo -i -u postgres createuser "$DBUSER" 2> /dev/null -then - echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 +if ! sudo -i -u postgres createuser "$DBUSER" 2>/dev/null; then + echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 fi -echo "Creating database $DBNAME." 1>&2 +DBPATH=$(challenger-config \ + -c "$CFGFILE" \ + -s challengerdb-postgres \ + -o CONFIG) -if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME" -then - echo "Failed to create database '$DBNAME'" - exit 1 +if ! echo "$DBPATH" | grep "postgres://" >/dev/null; then + echo "Invalid database configuration value '$DBPATH'." 1>&2 + exit 1 fi -if [ -f "$CFGFILE" ] -then - echo "Adding database configuration to $CFGFILE." 1>&2 - echo -e "[challengerdb-postgres]\nCONFIG=postgres:///$DBNAME\n" >> "$CFGFILE" +DBNAME=$(echo "$DBPATH" | + sed \ + -e "s/postgres:\/\/.*\///" \ + -e "s/?.*//") + +if sudo -i -u postgres psql "$DBNAME" </dev/null 2>/dev/null; then + if [ 1 = "$RESET_DB" ]; then + echo "Deleting existing database '$DBNAME'." 1>&2 + if ! sudo -i -u postgres dropdb "$DBNAME"; then + echo "Failed to delete existing database '$DBNAME'" + exit 1 + fi + DO_CREATE=1 + else + echo "Database '$DBNAME' already exists, continuing anyway." + DO_CREATE=0 + fi else - echo "Configuration $CFGFILE does not yet exist, creating it." 1>&2 - mkdir -p "$(dirname "$CFGFILE")" - echo -e "[challengerdb-postgres]\nCONFIG=postgres:///$DBNAME\n" >> "$CFGFILE" - chown "$DBUSER":root "$CFGFILE" - chmod 460 "$CFGFILE" + DO_CREATE=1 +fi + +if [ 1 = "$DO_CREATE" ]; then + echo "Creating database '$DBNAME'." 1>&2 + + if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME"; then + echo "Failed to create database '$DBNAME'" + exit 1 + fi fi -if [ 0 = "$SKIP_DBINIT" ] -then - echo "Initializing database $DBNAME." 1>&2 - sudo -u "$DBUSER" challenger-dbinit +if [ 0 = "$SKIP_DBINIT" ]; then + echo "Initializing database '$DBNAME'." 1>&2 + if ! sudo -u "$DBUSER" "$DBINIT" -c "$CFGFILE"; then + echo "Failed to initialize database schema" + exit 1 + fi fi echo "Database configuration finished." 1>&2