exchange

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

taler-unified-setup.sh (31151B)


      1 #!/usr/bin/env bash
      2 #
      3 # This file is part of TALER
      4 # Copyright (C) 2023, 2024 Taler Systems SA
      5 #
      6 # TALER is free software; you can redistribute it and/or modify
      7 # it under the terms of the GNU General Public License as
      8 # published by the Free Software Foundation; either version 3, or
      9 # (at your option) any later version.
     10 #
     11 # TALER is distributed in the hope that it will be useful, but
     12 # WITHOUT ANY WARRANTY; without even the implied warranty of
     13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 # GNU General Public License for more details.
     15 #
     16 # You should have received a copy of the GNU General Public
     17 # License along with TALER; see the file COPYING.  If not, see
     18 # <http://www.gnu.org/licenses/>
     19 #
     20 # Author: Christian Grothoff
     21 #
     22 # This script configures and launches various GNU Taler services. Which ones
     23 # depend on command-line options. Use "-h" to find out. Prints
     24 # "READY:$TEST_ROOT" on a separate line once all requested services are
     25 # running. Close STDIN (or input 'NEWLINE') to stop all started services again.
     26 #
     27 # This script must *only* write output to stderr, as stdout is used to for
     28 # structured communication with the parent process.
     29 #
     30 # shellcheck disable=SC2317
     31 
     32 set -eu
     33 
     34 if [[ ${TALER_TEST_VERBOSE:-0} -ge 1 ]]; then
     35   set -x
     36 fi
     37 
     38 # These break TALER_HOME control via TALER_TEST_HOME...
     39 unset XDG_DATA_HOME
     40 unset XDG_CONFIG_HOME
     41 unset XDG_CACHE_HOME
     42 
     43 EXIT_STATUS=2
     44 
     45 # Exit, with status code "skip" (no 'real' failure)
     46 function exit_skip() {
     47     echo " SKIP: " "$@" >&2
     48     EXIT_STATUS=77
     49     exit "$EXIT_STATUS"
     50 }
     51 
     52 # Exit, with error message (hard failure)
     53 function exit_fail() {
     54     echo " FAIL: " "$@" >&2
     55     EXIT_STATUS=1
     56     exit "$EXIT_STATUS"
     57 }
     58 
     59 # Cleanup to run whenever we exit
     60 function cleanup()
     61 {
     62     echo "Taler unified setup terminating at $STAGE!" >&2
     63 
     64     for n in $(jobs -p)
     65     do
     66         kill "$n" 2> /dev/null || true
     67     done
     68     wait
     69     rm -f libeufin-nexus.pid libeufin-sandbox.pid
     70     exit "$EXIT_STATUS"
     71 }
     72 
     73 STAGE="boot"
     74 
     75 # Install cleanup handler (except for kill -9)
     76 trap cleanup EXIT
     77 
     78 WAIT_FOR_SIGNAL=0
     79 START_AUDITOR=0
     80 START_BACKUP=0
     81 START_EXCHANGE=0
     82 START_FAKEBANK=0
     83 START_DONAU=0
     84 START_CHALLENGER=0
     85 START_AGGREGATOR=0
     86 START_MERCHANT=0
     87 START_NEXUS=0
     88 START_BANK=0
     89 START_TRANSFER=0
     90 START_WIREWATCH=0
     91 START_DEPOSITCHECK=0
     92 START_MERCHANT_EXCHANGE=0
     93 START_MERCHANT_WIREWATCH=0
     94 START_MERCHANT_DONAUKEYUPDATE=0
     95 USE_ACCOUNT="exchange-account-1"
     96 USE_VALGRIND=""
     97 WIRE_DOMAIN="x-taler-bank"
     98 CONF_ORIG="$HOME/.config/taler.conf"
     99 LOGLEVEL="DEBUG"
    100 DEFAULT_SLEEP="0.5"
    101 
    102 # Parse command-line options
    103 while getopts ':abc:d:DeEfghkL:mMnr:stu:vwWzZ' OPTION; do
    104     case "$OPTION" in
    105         a)
    106             START_AUDITOR="1"
    107             ;;
    108         b)
    109             START_BANK="1"
    110             ;;
    111         c)
    112             CONF_ORIG="$OPTARG"
    113             ;;
    114         d)
    115             WIRE_DOMAIN="$OPTARG"
    116             ;;
    117         D)
    118             START_DONAU="1"
    119             ;;
    120         e)
    121             START_EXCHANGE="1"
    122             ;;
    123         E)
    124             START_MERCHANT_EXCHANGE="1"
    125             ;;
    126         f)
    127             START_FAKEBANK="1"
    128             ;;
    129         h)
    130             echo 'Supported options:'
    131             echo '  -a           -- start auditor'
    132             echo '  -b           -- start bank'
    133             # shellcheck disable=SC2016
    134             echo '  -c $CONF     -- set configuration'
    135             # shellcheck disable=SC2016
    136             echo '  -d $METHOD   -- use wire method (default: x-taler-bank)'
    137             echo '  -D           -- start donau'
    138             echo '  -e           -- start exchange'
    139             echo '  -E           -- start taler-merchant-exchange'
    140             echo '  -f           -- start fakebank'
    141             echo '  -g           -- start taler-exchange-aggregator'
    142             echo '  -h           -- print this help'
    143             echo '  -k           -- start challenger (KYC service)'
    144             # shellcheck disable=SC2016
    145             echo '  -L $LOGLEVEL -- set log level'
    146             echo '  -m           -- start taler-merchant'
    147             echo '  -M           -- start taler-merchant-depositcheck'
    148             echo '  -n           -- start nexus'
    149             # shellcheck disable=SC2016
    150             echo '  -r $MEX      -- which exchange to use at the merchant (optional)'
    151             echo '  -s           -- start backup/sync'
    152             echo '  -S $SLEEP    -- set default sleep time between retries'
    153             echo '  -t           -- start taler-exchange-transfer'
    154             # shellcheck disable=SC2016
    155             echo '  -u $SECTION  -- exchange account to use'
    156             echo '  -v           -- use valgrind'
    157             echo '  -w           -- start taler-exchange-wirewatch'
    158             echo '  -W           -- wait for signal'
    159             echo '  -z           -- start taler-merchant-wirewatch'
    160             echo '  -Z           -- start taler-merchant-donaukeyupdate'
    161             exit 0
    162             ;;
    163         g)
    164             START_AGGREGATOR="1"
    165             ;;
    166         k)
    167             START_CHALLENGER="1"
    168             ;;
    169         L)
    170             LOGLEVEL="$OPTARG"
    171             ;;
    172         m)
    173             START_MERCHANT="1"
    174             ;;
    175         M)
    176             START_DEPOSITCHECK="1"
    177             ;;
    178         n)
    179             START_NEXUS="1"
    180             ;;
    181         r)
    182             USE_MERCHANT_EXCHANGE="$OPTARG"
    183             ;;
    184         s)
    185             START_BACKUP="1"
    186             ;;
    187         S)
    188             DEFAULT_SLEEP="$OPTARG"
    189             ;;
    190         t)
    191             START_TRANSFER="1"
    192             ;;
    193         u)
    194             USE_ACCOUNT="$OPTARG"
    195             ;;
    196         v)
    197             USE_VALGRIND="valgrind --leak-check=yes"
    198             DEFAULT_SLEEP="2"
    199             ;;
    200         w)
    201             START_WIREWATCH="1"
    202             ;;
    203         W)
    204             WAIT_FOR_SIGNAL="1"
    205             ;;
    206         z)
    207             START_MERCHANT_WIREWATCH="1"
    208             ;;
    209         Z)
    210             START_MERCHANT_DONAUKEYUPDATE="1"
    211             ;;
    212         ?)
    213         exit_fail "Unrecognized command line option"
    214         ;;
    215     esac
    216 done
    217 
    218 STAGE="init"
    219 
    220 
    221 TESTROOT=$(mktemp --tmpdir -d taler-testing-XXXXXX)
    222 
    223 echo "Starting with configuration file at: $CONF_ORIG" >&2
    224 CONF="$CONF_ORIG.edited"
    225 cp "${CONF_ORIG}" "${CONF}"
    226 
    227 STAGE="checks"
    228 
    229 echo -n "Testing for jq" >&2
    230 jq -h > /dev/null || exit_skip " jq required"
    231 echo " FOUND" >&2
    232 
    233 echo -n "Testing for wget" >&2
    234 wget --help > /dev/null || exit_skip " wget required" >&2
    235 echo " FOUND" >&2
    236 
    237 if [ "1" = "$START_EXCHANGE" ]
    238 then
    239     echo -n "Testing for Taler exchange" >&2
    240     taler-exchange-httpd -h > /dev/null || exit_skip " taler-exchange-httpd required"
    241     echo " FOUND" >&2
    242 fi
    243 
    244 if [ "1" = "$START_DONAU" ]
    245 then
    246     echo -n "Testing for Donau" >&2
    247     donau-httpd -h > /dev/null || exit_skip " donau-httpd required"
    248     echo " FOUND" >&2
    249 fi
    250 
    251 if [ "1" = "$START_MERCHANT" ]
    252 then
    253     echo -n "Testing for Taler merchant" >&2
    254     taler-merchant-httpd -h > /dev/null || exit_skip " taler-merchant-httpd required"
    255     echo " FOUND" >&2
    256 fi
    257 
    258 if [ "1" = "$START_CHALLENGER" ]
    259 then
    260     echo -n "Testing for Taler challenger" >&2
    261     challenger-httpd -h > /dev/null || exit_skip " challenger-httpd required"
    262     echo " FOUND" >&2
    263 fi
    264 
    265 if [ "1" = "$START_BACKUP" ]
    266 then
    267     echo -n "Testing for sync-httpd" >&2
    268     sync-httpd -h > /dev/null || exit_skip " sync-httpd required"
    269     echo " FOUND" >&2
    270 fi
    271 
    272 if [ "1" = "$START_NEXUS" ]
    273 then
    274     echo -n "Testing for libeufin-nexus" >&2
    275     libeufin-nexus --help >/dev/null </dev/null || exit_skip " MISSING"
    276     echo " FOUND" >&2
    277 fi
    278 
    279 if [ "1" = "$START_BANK" ]
    280 then
    281     echo -n "Testing for libeufin-bank" >&2
    282     libeufin-bank --help >/dev/null </dev/null || exit_skip " MISSING"
    283     echo " FOUND" >&2
    284 fi
    285 
    286 STAGE="config"
    287 
    288 if [ "1" = "$START_EXCHANGE" ]
    289 then
    290     CURRENCY=$(taler-exchange-config -c "$CONF" -s "EXCHANGE" -o "CURRENCY")
    291 else
    292     if [ "1" = "$START_DONAU" ]
    293     then
    294         CURRENCY=$(donau-config -c "$CONF" -s "DONAU" -o "CURRENCY")
    295     else
    296         if [ "1" = "$START_BANK" ]
    297         then
    298             # Note: would be nice to have libeufin-config in the future...
    299             CURRENCY=$(taler-exchange-config -c "$CONF" -s "libeufin-bank" -o "CURRENCY")
    300         else
    301             CURRENCY="UNKNOWN"
    302         fi
    303     fi
    304 fi
    305 
    306 echo "Setting up for $CURRENCY" >&2
    307 
    308 register_bank_account() {
    309     wget \
    310         --http-user="$AUSER" \
    311         --http-password="$APASS" \
    312         --method=DELETE \
    313         -o /dev/null \
    314         -O /dev/null \
    315         -a wget-delete-account.log \
    316         "http://localhost:${BANK_PORT}/accounts/$1" \
    317         || true # deletion may fail, that's OK!
    318     if [ "$1" = "exchange" ] || [ "$1" = "Exchange" ]
    319     then
    320         IS_EXCHANGE="true"
    321     else
    322         IS_EXCHANGE="false"
    323     fi
    324     MAYBE_IBAN="${4:-}"
    325     if [ -n "$MAYBE_IBAN" ]
    326     then
    327         # shellcheck disable=SC2001
    328         ENAME=$(echo "$3" | sed -e "s/ /+/g")
    329         if [ "$WIRE_DOMAIN" = "x-taler-bank" ]
    330         then
    331             # hostname
    332             OPERATOR="localhost"
    333             MAYBE_IBAN="$1"
    334         else
    335             # BIC
    336             OPERATOR="SANDBOXX"
    337         fi
    338         PAYTO="payto://${WIRE_DOMAIN}/${OPERATOR}/${MAYBE_IBAN}?receiver-name=$ENAME"
    339         BODY='{"username":"'"$1"'","password":"'"$2"'","is_taler_exchange":'"$IS_EXCHANGE"',"name":"'"$3"'","payto_uri":"'"$PAYTO"'"}'
    340     else
    341         BODY='{"username":"'"$1"'","password":"'"$2"'","is_taler_exchange":'"$IS_EXCHANGE"',"name":"'"$3"'"}'
    342     fi
    343     wget \
    344         --http-user="$AUSER" \
    345         --http-password="$APASS" \
    346         --method=POST \
    347         --header='Content-type: application/json' \
    348         --body-data="${BODY}" \
    349         -o /dev/null \
    350         -O /dev/null \
    351         -a wget-register-account.log \
    352         "http://localhost:${BANK_PORT}/accounts"
    353 }
    354 
    355 register_fakebank_account() {
    356     if [ "$1" = "exchange" ] || [ "$1" = "Exchange" ]
    357     then
    358         IS_EXCHANGE="true"
    359     else
    360         IS_EXCHANGE="false"
    361     fi
    362     BODY='{"username":"'"$1"'","password":"'"$2"'","name":"'"$1"'","is_taler_exchange":'"$IS_EXCHANGE"'}'
    363     wget \
    364         --post-data="$BODY" \
    365         --header='Content-type: application/json' \
    366         --tries=3 \
    367         --waitretry=1 \
    368         --timeout=30 \
    369         "http://localhost:$BANK_PORT/accounts" \
    370         -a wget-register-account.log \
    371         -o /dev/null \
    372         -O /dev/null \
    373         >/dev/null
    374 }
    375 
    376 
    377 if [[ "1" = "$START_BANK" ]]
    378 then
    379     BANK_PORT=$(taler-exchange-config -c "$CONF" -s "libeufin-bank" -o "PORT")
    380     BANK_URL="http://localhost:${BANK_PORT}/"
    381 fi
    382 
    383 if [[ "1" = "$START_FAKEBANK" ]]
    384 then
    385     BANK_PORT=$(taler-exchange-config -c "$CONF" -s "BANK" -o "HTTP_PORT")
    386     BANK_URL="http://localhost:${BANK_PORT}/"
    387 fi
    388 
    389 STAGE="bank"
    390 
    391 if [ "1" = "$START_BANK" ]
    392 then
    393     echo -n "Setting up bank database ... " >&2
    394     libeufin-bank dbinit \
    395         -r \
    396         -c "$CONF" \
    397         -L "$LOGLEVEL" \
    398         &> libeufin-bank-reset.log
    399     echo "DONE" >&2
    400     echo -n "Launching bank ... " >&2
    401     libeufin-bank serve \
    402       -c "$CONF" \
    403       -L "$LOGLEVEL" \
    404       > libeufin-bank-stdout.log \
    405       2> libeufin-bank-stderr.log &
    406     echo $! > libeufin-bank.pid
    407     echo "DONE" >&2
    408     echo -n "Waiting for Bank ..." >&2
    409     OK="0"
    410     for n in $(seq 1 100); do
    411         echo -n "." >&2
    412         sleep "$DEFAULT_SLEEP"
    413         wget --timeout=1 \
    414              --tries=3 \
    415              --waitretry=0 \
    416              -a wget-bank-check.log \
    417              -o /dev/null \
    418              -O /dev/null \
    419              "${BANK_URL}config" || continue
    420         OK="1"
    421         break
    422     done
    423     if [ "1" != "$OK" ]
    424     then
    425         exit_skip "Failed to launch services (bank)" >&2
    426     fi
    427     echo "OK" >&2
    428     echo -n "Set admin password..." >&2
    429     AUSER="admin"
    430     APASS="secret-password"
    431     libeufin-bank \
    432       passwd \
    433       -c "$CONF" \
    434       -L "$LOGLEVEL" \
    435       "$AUSER" "$APASS" \
    436       &> libeufin-bank-passwd.log
    437     libeufin-bank \
    438       edit-account \
    439       -c "$CONF" \
    440       -L "$LOGLEVEL" \
    441       --debit_threshold="$CURRENCY:1000000" \
    442       "$AUSER" \
    443       &> libeufin-bank-debit-threshold.log
    444     echo " OK" >&2
    445 fi
    446 
    447 if [ "1" = "$START_NEXUS" ]
    448 then
    449     echo "Nexus currently not supported ..." >&2
    450 fi
    451 
    452 if [ "1" = "$START_FAKEBANK" ]
    453 then
    454     echo -n "Setting up fakebank ..." >&2
    455     $USE_VALGRIND taler-fakebank-run \
    456                   -c "$CONF" \
    457                   -L "$LOGLEVEL" \
    458                   -n 4 \
    459                   2> taler-fakebank-run.log &
    460     echo " OK" >&2
    461 fi
    462 
    463 if [[ "1" = "$START_BANK" || "1" = "$START_FAKEBANK" ]]
    464 then
    465     echo -n "Waiting for the bank" >&2
    466     # Wait for bank to be available (usually the slowest)
    467     OK="0"
    468     for n in $(seq 1 300)
    469     do
    470         echo -n "." >&2
    471         sleep "$DEFAULT_SLEEP"
    472         # bank
    473         wget --tries=1 \
    474              --waitretry=0 \
    475              --timeout=1 \
    476              --user admin \
    477              --password secret \
    478              -a wget-bank-check.log \
    479              -o /dev/null \
    480              -O /dev/null \
    481              "http://localhost:${BANK_PORT}/" || continue
    482         OK="1"
    483         break
    484     done
    485     if [ "1" != "$OK" ]
    486     then
    487         exit_skip "Failed to launch services (bank)"
    488     fi
    489     echo " OK" >&2
    490 fi
    491 
    492 STAGE="accounts"
    493 
    494 if [ "1" = "$START_FAKEBANK" ]
    495 then
    496     echo -n "Register Fakebank users ..." >&2
    497     register_fakebank_account fortytwo password
    498     register_fakebank_account fortythree password
    499     register_fakebank_account exchange password
    500     register_fakebank_account tor password
    501     register_fakebank_account gnunet password
    502     register_fakebank_account tutorial password
    503     register_fakebank_account survey password
    504     echo " DONE" >&2
    505 fi
    506 
    507 if [ "1" = "$START_BANK" ]
    508 then
    509     echo -n "Register bank users ..." >&2
    510     # The specified IBAN and name must match the ones hard-coded into
    511     # the C helper for the add-incoming call.  Without this value,
    512     # libeufin-bank  won't find the target account to debit along a /add-incoming
    513     # call.
    514     register_bank_account fortytwo password "User42" FR7630006000011234567890189
    515     register_bank_account fortythree password "Forty Three"
    516     register_bank_account exchange password "Exchange Company" DE989651
    517     register_bank_account tor password "Tor Project"
    518     register_bank_account gnunet password "GNUnet"
    519     register_bank_account tutorial password "Tutorial"
    520     register_bank_account survey password "Survey"
    521     echo " DONE" >&2
    522 fi
    523 
    524 STAGE="exchange"
    525 
    526 if [ "1" = "$START_EXCHANGE" ]
    527 then
    528     echo -n "Starting exchange ..." >&2
    529     EXCHANGE_PORT=$(taler-exchange-config -c "$CONF" -s EXCHANGE -o PORT)
    530     SERVE=$(taler-exchange-config -c "$CONF" -s EXCHANGE -o SERVE)
    531     if [ "${SERVE}" = "unix" ]
    532     then
    533         EXCHANGE_URL=$(taler-exchange-config -c "$CONF" -s EXCHANGE -o BASE_URL)
    534     else
    535         EXCHANGE_URL="http://localhost:${EXCHANGE_PORT}/"
    536     fi
    537     MASTER_PRIV_FILE=$(taler-exchange-config -f -c "${CONF}" -s "EXCHANGE-OFFLINE" -o "MASTER_PRIV_FILE")
    538     MASTER_PRIV_DIR=$(dirname "$MASTER_PRIV_FILE")
    539     mkdir -p "${MASTER_PRIV_DIR}"
    540     if [ ! -e "$MASTER_PRIV_FILE" ]
    541     then
    542         gnunet-ecc -g1 "$MASTER_PRIV_FILE" > /dev/null 2> /dev/null
    543         echo -n "." >&2
    544     fi
    545     MASTER_PUB=$(gnunet-ecc -p "${MASTER_PRIV_FILE}")
    546     MPUB=$(taler-exchange-config -c "$CONF" -s exchange -o MASTER_PUBLIC_KEY)
    547     if [ "$MPUB" != "$MASTER_PUB" ]
    548     then
    549         echo -n " patching master_pub ($MASTER_PUB from ${MASTER_PRIV_FILE})..." >&2
    550         taler-exchange-config -c "$CONF" -s exchange -o MASTER_PUBLIC_KEY -V "$MASTER_PUB"
    551     fi
    552     taler-exchange-dbinit \
    553         -c "$CONF" \
    554         --reset
    555     $USE_VALGRIND taler-exchange-secmod-eddsa \
    556                   -c "$CONF" \
    557                   -L "$LOGLEVEL" \
    558                   2> taler-exchange-secmod-eddsa.log &
    559     $USE_VALGRIND taler-exchange-secmod-rsa \
    560                   -c "$CONF" \
    561                   -L "$LOGLEVEL" \
    562                   2> taler-exchange-secmod-rsa.log &
    563     $USE_VALGRIND taler-exchange-secmod-cs \
    564                   -c "$CONF" \
    565                   -L "$LOGLEVEL" \
    566                   2> taler-exchange-secmod-cs.log &
    567     $USE_VALGRIND taler-exchange-httpd \
    568                   -c "$CONF" \
    569                   -L "$LOGLEVEL" 2> taler-exchange-httpd.log &
    570     echo " DONE" >&2
    571 fi
    572 
    573 STAGE="donau"
    574 
    575 if [ "1" = "$START_DONAU" ]
    576 then
    577     echo -n "Starting Donau ..." >&2
    578     DONAU_PORT=$(donau-config -c "$CONF" -s DONAU -o PORT)
    579     SERVE=$(donau-config -c "$CONF" -s DONAU -o SERVE)
    580     if [ "${SERVE}" = "unix" ]
    581     then
    582         DONAU_URL=$(donau-config -c "$CONF" -s DONAU -o BASE_URL)
    583     else
    584         DONAU_URL="http://localhost:${DONAU_PORT}/"
    585     fi
    586     donau-dbinit -c "$CONF" --reset
    587     $USE_VALGRIND donau-secmod-eddsa -c "$CONF" -L "$LOGLEVEL" 2> donau-secmod-eddsa.log &
    588     $USE_VALGRIND donau-secmod-rsa -c "$CONF" -L "$LOGLEVEL" 2> donau-secmod-rsa.log &
    589     $USE_VALGRIND donau-secmod-cs -c "$CONF" -L "$LOGLEVEL" 2> donau-secmod-cs.log &
    590     $USE_VALGRIND donau-httpd -c "$CONF" -L "$LOGLEVEL" 2> donau-httpd.log &
    591     echo " DONE" >&2
    592 fi
    593 
    594 STAGE="wirewatch"
    595 
    596 if [ "1" = "$START_WIREWATCH" ]
    597 then
    598     echo -n "Starting wirewatch ..." >&2
    599     $USE_VALGRIND taler-exchange-wirewatch \
    600                   --account="$USE_ACCOUNT" \
    601                   -c "$CONF" \
    602                   -L "$LOGLEVEL" \
    603                   --longpoll-timeout="60 s" \
    604                   2> taler-exchange-wirewatch.log &
    605     echo " DONE" >&2
    606 fi
    607 
    608 STAGE="aggregator"
    609 
    610 if [ "1" = "$START_AGGREGATOR" ]
    611 then
    612     echo -n "Starting aggregator ..." >&2
    613     $USE_VALGRIND taler-exchange-aggregator \
    614                   -c "$CONF" \
    615                   -L "$LOGLEVEL" \
    616                   2> taler-exchange-aggregator.log &
    617     echo " DONE" >&2
    618 fi
    619 
    620 STAGE="transfer"
    621 
    622 if [ "1" = "$START_TRANSFER" ]
    623 then
    624     echo -n "Starting transfer ..." >&2
    625     $USE_VALGRIND taler-exchange-transfer \
    626                   -c "$CONF" \
    627                   -L "$LOGLEVEL" \
    628                   2> taler-exchange-transfer.log &
    629     echo " DONE" >&2
    630 fi
    631 
    632 STAGE="merchant"
    633 
    634 if [ -n "${USE_MERCHANT_EXCHANGE+x}" ]
    635 then
    636     MEPUB=$(taler-merchant-config -c "$CONF" -s "${USE_MERCHANT_EXCHANGE}" -o MASTER_KEY)
    637     MXPUB=${MASTER_PUB:-$(taler-exchange-config -c "$CONF" -s exchange -o MASTER_PUBLIC_KEY)}
    638     if [ "$MEPUB" != "$MXPUB" ]
    639     then
    640         echo -n " patching master_pub ($MXPUB)..." >&2
    641         taler-merchant-config -c "$CONF" -s "${USE_MERCHANT_EXCHANGE}" -o MASTER_KEY -V "$MXPUB"
    642     else
    643         echo -n " with exchange $MXPUB ..." >&2
    644     fi
    645 fi
    646 
    647 if [ "1" = "$START_MERCHANT" ]
    648 then
    649     echo -n "Starting merchant ..." >&2
    650     MERCHANT_TYPE=$(taler-merchant-config -c "$CONF" -s MERCHANT -o SERVE)
    651     if [ "unix" = "$MERCHANT_TYPE" ]
    652     then
    653         MERCHANT_URL="$(taler-merchant-config -c "$CONF" -s MERCHANT -o BASE_URL)"
    654     else
    655         MERCHANT_PORT="$(taler-merchant-config -c "$CONF" -s MERCHANT -o PORT)"
    656         MERCHANT_URL="http://localhost:${MERCHANT_PORT}/"
    657     fi
    658     taler-merchant-dbinit \
    659         -c "$CONF" \
    660         --reset &> taler-merchant-dbinit.log
    661     $USE_VALGRIND taler-merchant-exchangekeyupdate \
    662                   -c "$CONF" \
    663                   -L "$LOGLEVEL" 2> taler-merchant-exchangekeyupdate.log &
    664     $USE_VALGRIND taler-merchant-kyccheck \
    665                   -c "$CONF" \
    666                   -L "$LOGLEVEL" 2> taler-merchant-kyccheck.log &
    667     $USE_VALGRIND taler-merchant-httpd \
    668                   -c "$CONF" \
    669                   -L "$LOGLEVEL" 2> taler-merchant-httpd.log &
    670     $USE_VALGRIND taler-merchant-webhook \
    671                   -c "$CONF" \
    672                   -L "$LOGLEVEL" 2> taler-merchant-webhook.log &
    673     echo " DONE" >&2
    674     if [ "1" = "$START_MERCHANT_WIREWATCH" ]
    675     then
    676        echo -n "Starting taler-merchant-wirewatch ..." >&2
    677        $USE_VALGRIND taler-merchant-wirewatch \
    678                      -c "$CONF" \
    679                      -L "$LOGLEVEL" \
    680                      --persist \
    681                      2> taler-merchant-wirewatch.log &
    682        echo " DONE" >&2
    683     fi
    684     if [ "1" = "$START_MERCHANT_EXCHANGE" ]
    685     then
    686         echo -n "Starting taler-merchant-exchange ..." >&2
    687         $USE_VALGRIND taler-merchant-exchange \
    688                   -c "$CONF" \
    689                   -L "$LOGLEVEL" 2> taler-merchant-exchange.log &
    690         echo " DONE" >&2
    691     fi
    692     if [ "1" = "$START_DEPOSITCHECK" ]
    693     then
    694         echo -n "Starting taler-merchant-depositcheck ..." >&2
    695         $USE_VALGRIND taler-merchant-depositcheck \
    696                       -c "$CONF" \
    697                       -L "$LOGLEVEL" 2> taler-merchant-depositcheck.log &
    698         echo " DONE" >&2
    699     fi
    700     if [ "1" = "$START_MERCHANT_DONAUKEYUPDATE" ]
    701     then
    702         echo -n "Starting taler-merchant-donaukeyupdate..." >&2
    703         $USE_VALGRIND taler-merchant-donaukeyupdate \
    704                       -c "$CONF" \
    705                       -L "$LOGLEVEL" 2> taler-merchant-donaukeyupdate.log &
    706         echo " DONE" >&2
    707     fi
    708 fi
    709 
    710 STAGE="sync"
    711 
    712 if [ "1" = "$START_BACKUP" ]
    713 then
    714     echo -n "Starting sync ..." >&2
    715     SYNC_PORT=$(sync-config -c "$CONF" -s SYNC -o PORT)
    716     SERVE=$(sync-config -c "$CONF" -s SYNC -o SERVE)
    717     if [ "${SERVE}" = "unix" ]
    718     then
    719         SYNC_URL=$(sync-config -c "$CONF" -s SYNC -o BASE_URL)
    720     else
    721         SYNC_URL="http://localhost:${SYNC_PORT}/"
    722     fi
    723     sync-dbinit -c "$CONF" --reset
    724     $USE_VALGRIND sync-httpd \
    725                   -c "$CONF" \
    726                   -L "$LOGLEVEL" \
    727                   2> sync-httpd.log &
    728     echo " DONE" >&2
    729 fi
    730 
    731 STAGE="challenger"
    732 
    733 if [ "1" = "$START_CHALLENGER" ]
    734 then
    735     echo -n "Starting challenger ..." >&2
    736     CHALLENGER_PORT=$(challenger-config -c "$CONF" -s CHALLENGER -o PORT)
    737     SERVE=$(challenger-config -c "$CONF" -s CHALLENGER -o SERVE)
    738     if [ "${SERVE}" = "unix" ]
    739     then
    740         CHALLENGER_URL=$(challenger-config -c "$CONF" -s CHALLENGER -o BASE_URL)
    741     else
    742         CHALLENGER_URL="http://localhost:${CHALLENGER_PORT}/"
    743     fi
    744     challenger-dbinit \
    745         -c "$CONF" \
    746         --reset
    747     $USE_VALGRIND challenger-httpd \
    748                   -c "$CONF" \
    749                   -L "$LOGLEVEL" \
    750                   2> challenger-httpd.log &
    751     echo " DONE" >&2
    752     for SECTION in $(taler-exchange-config -c "$CONF" -S | grep kyc-provider)
    753     do
    754         LOGIC=$(taler-exchange-config -c "$CONF" -s "$SECTION" -o "LOGIC")
    755         if [ "${LOGIC}" = "oauth2" ]
    756         then
    757             INFO=$(taler-exchange-config -c "$CONF" -s "$SECTION" -o "KYC_OAUTH2_INFO_URL")
    758             if [ "${CHALLENGER_URL}info" = "$INFO" ]
    759             then
    760                 echo -n "Enabling Challenger client for $SECTION" >&2
    761                 CLIENT_SECRET=$(taler-exchange-config -c "$CONF" -s "$SECTION" -o "KYC_OAUTH2_CLIENT_SECRET")
    762                 RFC_8959_PREFIX="secret-token:"
    763                 if ! echo "${CLIENT_SECRET}" | grep ^${RFC_8959_PREFIX} > /dev/null
    764                 then
    765                     exit_fail "Client secret does not begin with '${RFC_8959_PREFIX}'"
    766                 fi
    767                 REDIRECT_URI="${EXCHANGE_URL}kyc-proof/kyc-provider-example-challeger"
    768                 CLIENT_ID=$(challenger-admin --add="${CLIENT_SECRET}" --quiet "${REDIRECT_URI}")
    769                 taler-exchange-config -c "$CONF" -s "$SECTION" -o KYC_OAUTH2_CLIENT_ID -V "$CLIENT_ID"
    770                 echo " DONE" >&2
    771             fi
    772         fi
    773     done
    774 fi
    775 
    776 STAGE="auditor"
    777 
    778 if [ "1" = "$START_AUDITOR" ]
    779 then
    780     echo -n "Starting auditor ..." >&2
    781 
    782     export TALER_AUDITOR_SALT=$(taler-auditor-config -c "$CONF" -s AUDITOR -o TALER_AUDITOR_SALT)
    783 
    784     AUDITOR_URL=$(taler-auditor-config -c "$CONF" -s AUDITOR -o BASE_URL)
    785     AUDITOR_PRIV_FILE=$(taler-auditor-config -f -c "$CONF" -s AUDITOR -o AUDITOR_PRIV_FILE)
    786     AUDITOR_PRIV_DIR=$(dirname "$AUDITOR_PRIV_FILE")
    787     mkdir -p "$AUDITOR_PRIV_DIR"
    788     if [ ! -e "$AUDITOR_PRIV_FILE" ]
    789     then
    790         gnunet-ecc -g1 "$AUDITOR_PRIV_FILE" > /dev/null 2> /dev/null
    791         echo -n "." >&2
    792     fi
    793     AUDITOR_PUB=$(gnunet-ecc -p "${AUDITOR_PRIV_FILE}")
    794     APUB=$(taler-exchange-config -c "$CONF" -s auditor -o PUBLIC_KEY)
    795     if [ "$APUB" != "$AUDITOR_PUB" ]
    796     then
    797         echo -n " patching auditor public key ..." >&2
    798         # Using taler-exchange-config is correct here, we don't want to
    799         # suddenly use the auditor-defaults while editing...
    800         taler-exchange-config -c "$CONF" -s auditor -o PUBLIC_KEY -V "$AUDITOR_PUB"
    801     fi
    802 
    803     taler-auditor-dbinit \
    804         -c "$CONF" \
    805         --reset
    806     echo "Launching auditor using $CONF" > taler-auditor-httpd.log >&2
    807     echo "Launching auditor using $AUDITOR_PUB from $AUDITOR_PRIV_FILE" \
    808          >> taler-auditor-httpd.log
    809     $USE_VALGRIND taler-auditor-httpd \
    810                   -L "$LOGLEVEL" \
    811                   -c "$CONF" 2>> taler-auditor-httpd.log &
    812     echo " DONE" >&2
    813 fi
    814 
    815 STAGE="wait"
    816 
    817 echo -n "Waiting for Taler services ..." >&2
    818 # Wait for all other taler services to be available
    819 E_DONE=0
    820 D_DONE=0
    821 M_DONE=0
    822 S_DONE=0
    823 K_DONE=0
    824 A_DONE=0
    825 for n in $(seq 1 30)
    826 do
    827     sleep "$DEFAULT_SLEEP"
    828     OK="0"
    829     if [ "0" = "$E_DONE" ] && [ "1" = "$START_EXCHANGE" ]
    830     then
    831         echo -n "E" >&2
    832         wget \
    833             --tries=1 \
    834             --timeout=1 \
    835             "${EXCHANGE_URL}config" \
    836             -o /dev/null \
    837             -O /dev/null >/dev/null || continue
    838         E_DONE=1
    839     fi
    840    if [ "0" = "$D_DONE" ] && [ "1" = "$START_DONAU" ]
    841     then
    842         echo -n "D" >&2
    843         wget \
    844             --tries=1 \
    845             --timeout=1 \
    846             "${DONAU_URL}config" \
    847             -o /dev/null \
    848             -O /dev/null >/dev/null || continue
    849         D_DONE=1
    850     fi
    851     if [ "0" = "$M_DONE" ] && [ "1" = "$START_MERCHANT" ]
    852     then
    853         echo -n "M" >&2
    854         wget \
    855             --tries=1 \
    856             --timeout=1 \
    857             "${MERCHANT_URL}config" \
    858             -o /dev/null \
    859             -O /dev/null >/dev/null || continue
    860         M_DONE=1
    861     fi
    862     if [ "0" = "$S_DONE" ] && [ "1" = "$START_BACKUP" ]
    863     then
    864         echo -n "S" >&2
    865         wget \
    866             --tries=1 \
    867             --timeout=1 \
    868             "${SYNC_URL}config" \
    869             -o /dev/null \
    870             -O /dev/null >/dev/null || continue
    871         S_DONE=1
    872     fi
    873     if [ "0" = "$K_DONE" ] && [ "1" = "$START_CHALLENGER" ]
    874     then
    875         echo -n "K" >&2
    876         wget \
    877             --tries=1 \
    878             --timeout=1 \
    879             "${CHALLENGER_URL}config" \
    880             -o /dev/null \
    881             -O /dev/null >/dev/null || continue
    882         K_DONE=1
    883     fi
    884     if [ "0" = "$A_DONE" ] && [ "1" = "$START_AUDITOR" ]
    885     then
    886         echo -n "A" >&2
    887         wget \
    888             --tries=1 \
    889             --timeout=1 \
    890             "${AUDITOR_URL}config" \
    891             -o /dev/null \
    892             -O /dev/null >/dev/null || continue
    893         A_DONE=1
    894     fi
    895     OK="1"
    896     break
    897 done
    898 if [ 1 != "$OK" ]
    899 then
    900     exit_skip "Failed to launch (some) Taler services (E: $E_DONE, M: $M_DONE, S: $S_DONE, K: $K_DONE, A: $A_DONE, D: $D_DONE)"
    901 fi
    902 echo " OK" >&2
    903 
    904 if [ "1" = "$START_EXCHANGE" ]
    905 then
    906     echo -n "Wait for exchange /management/keys to be ready " >&2
    907     OK="0"
    908     LAST_RESPONSE=$(mktemp tmp-last-response.XXXXXXXX)
    909     for n in $(seq 1 10)
    910     do
    911         echo -n "." >&2
    912         sleep "$DEFAULT_SLEEP"
    913         # exchange
    914         wget \
    915             --tries=3 \
    916             --waitretry=0 \
    917             --timeout=30 \
    918             "${EXCHANGE_URL}management/keys"\
    919             -o /dev/null \
    920             -O "$LAST_RESPONSE" \
    921             >/dev/null || continue
    922         OK="1"
    923         break;
    924     done
    925     if [ "1" != "$OK" ]
    926     then
    927         cat "$LAST_RESPONSE"
    928         exit_fail "Failed to setup exchange keys, check secmod logs"
    929     fi
    930     rm "$LAST_RESPONSE"
    931     echo " OK" >&2
    932 
    933     echo -n "Setting up exchange keys ..." >&2
    934     rm -f test_exchange_api_home/.local/share/taler-exchange/offline/secm_tofus.pub
    935     NEXT_YEAR=$(expr 1 + $(date +%Y))
    936     taler-exchange-offline -c "$CONF" \
    937       download \
    938       sign \
    939       wire-fee now "$WIRE_DOMAIN" "$CURRENCY:0.01" "$CURRENCY:0.01" \
    940       wire-fee "$NEXT_YEAR" "$WIRE_DOMAIN" "$CURRENCY:0.01" "$CURRENCY:0.01" \
    941       global-fee now "$CURRENCY:0.01" "$CURRENCY:0.01" "$CURRENCY:0.0" 1h 1year 5 \
    942       global-fee "$NEXT_YEAR" "$CURRENCY:0.01" "$CURRENCY:0.01" "$CURRENCY:0.0" 1h 1year 5 \
    943       upload &> taler-exchange-offline.log
    944     echo "OK" >&2
    945     ENABLED=$(taler-exchange-config -c "$CONF" -s "$USE_ACCOUNT" -o "ENABLE_CREDIT")
    946     if [ "YES" = "$ENABLED" ]
    947     then
    948         echo -n "Configuring bank account $USE_ACCOUNT ..." >&2
    949         EXCHANGE_PAYTO_URI=$(taler-exchange-config -c "$CONF" -s "$USE_ACCOUNT" -o "PAYTO_URI")
    950         taler-exchange-offline -c "$CONF" \
    951           enable-account "$EXCHANGE_PAYTO_URI" \
    952           upload &> "taler-exchange-offline-account.log"
    953         echo " OK" >&2
    954     else
    955         echo "WARNING: Account ${USE_ACCOUNT} not enabled (set to: '$ENABLED')" >&2
    956     fi
    957     if [ "1" = "$START_AUDITOR" ]
    958     then
    959         echo -n "Enabling auditor ..." >&2
    960         taler-exchange-offline -c "$CONF" \
    961           enable-auditor "$AUDITOR_PUB" "$AUDITOR_URL" "$CURRENCY Auditor" \
    962           upload &> taler-exchange-offline-auditor.log
    963         echo "OK" >&2
    964     fi
    965 
    966     echo -n "Checking /keys " >&2
    967     OK="0"
    968     LAST_RESPONSE=$(mktemp tmp-last-response.XXXXXXXX)
    969     for n in $(seq 1 10)
    970     do
    971         echo -n "." >&2
    972         sleep "$DEFAULT_SLEEP"
    973         wget \
    974             --tries=1 \
    975             --timeout=5 \
    976             "${EXCHANGE_URL}keys" \
    977             -a wget-keys-check.log \
    978             -o /dev/null \
    979             -O "$LAST_RESPONSE" \
    980             >/dev/null || continue
    981         OK="1"
    982         break
    983     done
    984     if [ "1" != "$OK" ]
    985     then
    986         cat "$LAST_RESPONSE"
    987         exit_fail " Failed to fetch ${EXCHANGE_URL}keys"
    988     fi
    989     rm "$LAST_RESPONSE"
    990     echo " OK" >&2
    991 fi
    992 
    993 if [ "1" = "$START_AUDITOR" ]
    994 then
    995     echo -n "Setting up auditor signatures ..." >&2
    996     timeout 15 taler-auditor-offline -c "$CONF" \
    997       download \
    998       sign \
    999       upload &> taler-auditor-offline.log
   1000     echo " OK" >&2
   1001 
   1002     echo -n "Starting helpers " >&2
   1003 
   1004     $USE_VALGRIND taler-helper-auditor-coins \
   1005                         -L "$LOGLEVEL" \
   1006                         -c "$CONF" 2> taler-helper-auditor.log &
   1007     echo -n "." >&2
   1008 
   1009     $USE_VALGRIND taler-helper-auditor-reserves \
   1010                         -L "$LOGLEVEL" \
   1011                         -c "$CONF" 2> taler-helper-auditor.log &
   1012     echo -n "." >&2
   1013 
   1014     $USE_VALGRIND taler-helper-auditor-purses \
   1015                         -L "$LOGLEVEL" \
   1016                         -c "$CONF" 2> taler-helper-auditor.log &
   1017     echo -n "." >&2
   1018 
   1019     $USE_VALGRIND taler-helper-auditor-aggregation \
   1020                         -L "$LOGLEVEL" \
   1021                         -c "$CONF" 2> taler-helper-auditor.log &
   1022     echo -n "." >&2
   1023 
   1024     $USE_VALGRIND taler-helper-auditor-deposits \
   1025                             -L "$LOGLEVEL" \
   1026                             -c "$CONF" 2> taler-helper-auditor.log &
   1027     echo -n "." >&2
   1028 
   1029     echo " OK" >&2
   1030 
   1031 fi
   1032 
   1033 STAGE="ready"
   1034 
   1035 # Signal caller that we are ready.
   1036 echo "READY:$TESTROOT"
   1037 
   1038 if [ "1" = "$WAIT_FOR_SIGNAL" ]
   1039 then
   1040     while true
   1041     do
   1042         sleep 0.1
   1043     done
   1044 else
   1045     # Wait until caller stops us.
   1046     # shellcheck disable=SC2162
   1047     read
   1048 fi
   1049 
   1050 STAGE="exiting"
   1051 
   1052 echo "Taler unified setup terminating!" >&2
   1053 EXIT_STATUS=0
   1054 exit "$EXIT_STATUS"