exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

taler-exchange-dbconfig (4965B)


      1 #!/bin/bash
      2 # This file is part of GNU TALER.
      3 # Copyright (C) 2023 Taler Systems SA
      4 #
      5 # TALER is free software; you can redistribute it and/or modify it under the
      6 # terms of the GNU Lesser General Public License as published by the Free Software
      7 # Foundation; either version 2.1, or (at your option) any later version.
      8 #
      9 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU Lesser General Public License along with
     14 # TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 #
     16 # @author Christian Grothoff
     17 #
     18 #
     19 # Error checking on
     20 set -eu
     21 
     22 RESET_DB=0
     23 SKIP_DBINIT=0
     24 FORCE_PERMS=0
     25 DBUSER="taler-exchange-httpd"
     26 DBGROUP="taler-exchange-db"
     27 CFGFILE="/etc/taler-exchange/taler-exchange.conf"
     28 
     29 # Parse command-line options
     30 while getopts 'c:g:hprsu:' OPTION; do
     31   case "$OPTION" in
     32   c)
     33     CFGFILE="$OPTARG"
     34     ;;
     35   h)
     36     echo 'Supported options:'
     37     echo "  -c FILENAME  -- use configuration FILENAME (default: $CFGFILE)"
     38     echo "  -g GROUP     -- taler-exchange to be run by GROUP (default: $DBGROUP)"
     39     echo "  -h           -- print this help text"
     40     echo "  -r           -- reset database (dangerous)"
     41     echo "  -p           -- force permission setup even without database initialization"
     42     echo "  -s           -- skip database initialization"
     43     echo "  -u USER      -- taler-exchange to be run by USER (default: $DBUSER)"
     44     exit 0
     45     ;;
     46   p)
     47     FORCE_PERMS="1"
     48     ;;
     49   r)
     50     RESET_DB="1"
     51     ;;
     52   s)
     53     SKIP_DBINIT="1"
     54     ;;
     55   u)
     56     DBUSER="$OPTARG"
     57     ;;
     58   ?)
     59     echo "Unrecognized command line option '$OPTION'" 1>&2
     60     exit 1
     61     ;;
     62   esac
     63 done
     64 
     65 if ! id postgres >/dev/null; then
     66   echo "Could not find 'postgres' user. Please install Postgresql first"
     67   exit 1
     68 fi
     69 
     70 if [ "$(id -u)" -ne 0 ]; then
     71   echo "This script must be run as root"
     72   exit 1
     73 fi
     74 
     75 if [ 0 = "$SKIP_DBINIT" ]; then
     76   if ! taler-exchange-dbinit -v 2>/dev/null; then
     77     echo "Required 'taler-exchange-dbinit' not found. Please fix your installation."
     78     exit 1
     79   fi
     80   DBINIT=$(which taler-exchange-dbinit)
     81 fi
     82 
     83 if ! id "$DBUSER" >/dev/null; then
     84   echo "Could not find '$DBUSER' user. Please set it up first"
     85   exit 1
     86 fi
     87 
     88 echo "Setting up database user '$DBUSER'." 1>&2
     89 
     90 if ! sudo -i -u postgres createuser "$DBUSER" 2>/dev/null; then
     91   echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2
     92 fi
     93 
     94 DBPATH=$(taler-exchange-config \
     95   -c "$CFGFILE" \
     96   -s exchangedb-postgres \
     97   -o CONFIG)
     98 
     99 if ! echo "$DBPATH" | grep "postgres://" >/dev/null; then
    100   echo "Invalid database configuration value '$DBPATH'." 1>&2
    101   exit 1
    102 fi
    103 
    104 DBNAME=$(echo "$DBPATH" |
    105   sed \
    106     -e "s/postgres:\/\/.*\///" \
    107     -e "s/?.*//")
    108 
    109 if sudo -i -u postgres psql "$DBNAME" </dev/null 2>/dev/null; then
    110   if [ 1 = "$RESET_DB" ]; then
    111     echo "Deleting existing database '$DBNAME'." 1>&2
    112     if ! sudo -i -u postgres dropdb "$DBNAME"; then
    113       echo "Failed to delete existing database '$DBNAME'"
    114       exit 1
    115     fi
    116     DO_CREATE=1
    117   else
    118     echo "Database '$DBNAME' already exists, continuing anyway."
    119     DO_CREATE=0
    120   fi
    121 else
    122   DO_CREATE=1
    123 fi
    124 
    125 if [ 1 = "$DO_CREATE" ]; then
    126   echo "Creating database '$DBNAME'." 1>&2
    127 
    128   if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME"; then
    129     echo "Failed to create database '$DBNAME'"
    130     exit 1
    131   fi
    132 fi
    133 
    134 if [ 0 = "$SKIP_DBINIT" ]; then
    135   echo "Initializing database '$DBNAME'." 1>&2
    136   if ! sudo -u "$DBUSER" "$DBINIT" -c "$CFGFILE"; then
    137     echo "Failed to initialize database schema"
    138     exit 1
    139   fi
    140 fi
    141 
    142 if [ 0 = "$SKIP_DBINIT" ] || [ 1 = "$FORCE_PERMS" ]; then
    143   DB_GRP="$(getent group "$DBGROUP" | sed -e "s/.*://g" -e "s/,/ /g")"
    144   echo "Initializing permissions for '$DB_GRP'." 1>&2
    145   for GROUPIE in $DB_GRP; do
    146     if [ "$GROUPIE" != "$DBUSER" ]; then
    147       if ! sudo -i -u postgres createuser "$GROUPIE" 2>/dev/null; then
    148         echo "Database user '$GROUPIE' already existed. Continuing anyway." 1>&2
    149       fi
    150       echo -e 'GRANT SELECT,INSERT,UPDATE,DELETE ON ALL TABLES IN SCHEMA exchange TO "'"$GROUPIE"'";\n' \
    151         'GRANT USAGE ON ALL SEQUENCES IN SCHEMA exchange TO "'"$GROUPIE"'";\n' |
    152         sudo -u "$DBUSER" psql "$DBNAME"
    153       echo -e 'GRANT USAGE ON SCHEMA exchange TO "'"$GROUPIE"'"' |
    154         sudo -u "$DBUSER" psql "$DBNAME"
    155       # Auditor needs to create schema in exchange database.
    156       echo -e 'GRANT CREATE ON DATABASE "'"$DBNAME"'" TO "'"$GROUPIE"'"' |
    157         sudo -u "$DBUSER" psql "$DBNAME"
    158       # FIXME: double-check the following GRANTs
    159       echo -e 'GRANT USAGE ON SCHEMA _v TO "'"$GROUPIE"'"' |
    160         sudo -u "$DBUSER" psql "$DBNAME"
    161       echo -e 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA _v TO "'"$GROUPIE"'"' |
    162         sudo -u "$DBUSER" psql "$DBNAME"
    163 
    164     fi
    165   done
    166 fi
    167 
    168 echo "Database configuration finished." 1>&2
    169 
    170 exit 0