sandcastle-ng

Scripts for the deployment of Sandcastle (GNU Taler)
Log | Files | Refs | README

setup-sandcastle.sh (53530B)


      1 #!/usr/bin/env bash
      2 
      3 # This scripts provisions all configuration and
      4 # services for the Taler sandcastle container.
      5 #
      6 # Important: This script needs to be completely
      7 # idempotent, nothing must break if it is executed
      8 # multiple times.
      9 
     10 set -eu
     11 set -x
     12 export LC_ALL="C.UTF-8"
     13 
     14 if [[ -n ${SANDCASTLE_SKIP_SETUP:-} ]]; then
     15   echo "skipping sandcastle setup, requested by environment var SANDCASTLE_SKIP_SETUP"
     16   exit 1
     17 fi
     18 
     19 
     20 # Helper to replace a comment-delimited block of lines in a config file with
     21 # the desired content. If the block doesn't exist yet, append it.
     22 update_config_block() {
     23   local config_file="$1"
     24   local marker_tag="$2"
     25   local new_content="$3"
     26   local begin_marker="# begin ${marker_tag}"
     27   local end_marker="# end ${marker_tag}"
     28   if [[ ! -f "$config_file" ]]; then
     29     echo "Error: Config file '$config_file' not found." >&2
     30     return 1
     31   fi
     32   if grep -qF "$begin_marker" "$config_file"; then
     33     # Markers exist. Replace the block.
     34     # Escape newlines in the content so sed processes it as a single block
     35     local escaped_content="${new_content//$'\n'/\\n}"
     36     sed -i "/$begin_marker/,/$end_marker/c\\$begin_marker\n$escaped_content\n$end_marker" "$config_file"
     37   else
     38     # Markers do not exist. Append to the end.
     39     printf "\n%s\n%s\n%s\n" "$begin_marker" "$new_content" "$end_marker" >> "$config_file"
     40   fi
     41 }
     42 
     43 SANDCASTLE_STATUS_DIR=/var/www/sandcastle/metrics
     44 SANDCASTLE_STATUS_FILE=$SANDCASTLE_STATUS_DIR/sandcastle-status
     45 
     46 # Atomically publish the version information for the status endpoint after a
     47 # successful deployment.
     48 write_sandcastle_status() {
     49   local temporary_file
     50   local sandcastle_version=unknown
     51   local architecture
     52   local component_dir
     53   local component
     54   local package
     55   local versions
     56   local turnstile_version
     57 
     58   if [[ -s /usr/share/sandcastle/version ]]; then
     59     sandcastle_version=$(</usr/share/sandcastle/version)
     60   fi
     61   architecture=$(dpkg --print-architecture)
     62   mkdir -p "$SANDCASTLE_STATUS_DIR"
     63   temporary_file=$(mktemp "$SANDCASTLE_STATUS_FILE.XXXXXX")
     64 
     65   {
     66     printf 'sandcastle: %s\n' "$sandcastle_version"
     67     printf 'deployed-at: %s\n' "$(date --utc +%Y-%m-%dT%H:%M:%SZ)"
     68     printf 'components:\n'
     69     for component_dir in "/packages/$architecture"/*; do
     70       [[ -d $component_dir ]] || continue
     71       component=${component_dir##*/}
     72       versions=$(
     73         for package in "$component_dir"/*.deb; do
     74           [[ -f $package ]] || continue
     75           dpkg-deb --field "$package" Version
     76         done | sort --unique | paste --serial --delimiters=,
     77       )
     78       [[ -n $versions ]] || continue
     79       printf '  %s: %s\n' "$component" "$versions"
     80     done
     81     if turnstile_version=$(
     82       git -C /opt/turnstile describe --tags --always --dirty 2>/dev/null
     83     ); then
     84       printf '  turnstile: %s\n' "$turnstile_version"
     85     fi
     86   } >"$temporary_file"
     87 
     88   chmod 644 "$temporary_file"
     89   mv "$temporary_file" "$SANDCASTLE_STATUS_FILE"
     90 }
     91 
     92 echo "Provisioning sandcastle"
     93 rm -f "$SANDCASTLE_STATUS_FILE" "$SANDCASTLE_STATUS_FILE".*
     94 
     95 # General configuration.
     96 # Might eventually be moved to an external file.
     97 
     98 # Source any overrides from external file
     99 if [[ -e /overrides ]]; then
    100   source /overrides
    101 fi
    102 
    103 # When serving on an external port (for localhost deployments),
    104 # we use http.
    105 if [[ ${EXTERNAL_PORT:-} =~ ^[0-9]+$ ]]; then
    106   PROTO=http
    107   PORT_SUFFIX=:$EXTERNAL_PORT
    108 else
    109   PROTO=https
    110   PORT_SUFFIX=
    111 fi
    112 
    113 : ${CURRENCY:="KUDOS"}
    114 : ${WIRE_METHOD:=x-taler-bank}
    115 
    116 
    117 if [[ $WIRE_METHOD = iban ]]; then
    118   EXCHANGE_IBAN=DE159593
    119   EXCHANGE_PAYTO="payto://iban/$EXCHANGE_IBAN?receiver-name=Sandcastle+Echange+Inc"
    120 
    121   # Randomly generated IBANs for the merchants
    122   MERCHANT_IBAN_ADMIN=DE85500105175178585583
    123   MERCHANT_IBAN_POS=DE4218710
    124   MERCHANT_IBAN_LANDING_SAMPLES=DE07500105170000163902
    125   MERCHANT_IBAN_BLOG=DE8292195
    126   MERCHANT_IBAN_GNUNET=DE9709960
    127   MERCHANT_IBAN_TALER=DE1740597
    128   MERCHANT_IBAN_TOR=DE2648777
    129   MERCHANT_IBAN_SANDBOX=DE949115029592
    130   MERCHANT_IBAN_UMAMI=DE358841382499
    131   MERCHANT_IBAN_PAIVANA=DE5183926
    132   MERCHANT_IBAN_TESTING=DE38936850270207298566
    133 
    134   MERCHANT_PAYTO_ADMIN="payto://iban/$MERCHANT_IBAN_ADMIN?receiver-name=Default+Merchant"
    135   MERCHANT_PAYTO_TESTING="payto://iban/$MERCHANT_IBAN_TESTING?receiver-name=Testing+Merchant"
    136   MERCHANT_PAYTO_POS="payto://iban/$MERCHANT_IBAN_POS?receiver-name=PoS+Merchant"
    137   MERCHANT_PAYTO_LANDING_SAMPLES="payto://iban/$MERCHANT_IBAN_LANDING_SAMPLES?receiver-name=Landing+Samples+Merchant"
    138   MERCHANT_PAYTO_BLOG="payto://iban/$MERCHANT_IBAN_BLOG?receiver-name=Blog+Merchant"
    139   MERCHANT_PAYTO_GNUNET="payto://iban/$MERCHANT_IBAN_GNUNET?receiver-name=GNUnet+Merchant"
    140   MERCHANT_PAYTO_TALER="payto://iban/$MERCHANT_IBAN_TALER?receiver-name=Taler+Merchant"
    141   MERCHANT_PAYTO_TOR="payto://iban/$MERCHANT_IBAN_TOR?receiver-name=Tor+Merchant"
    142   MERCHANT_PAYTO_UMAMI="payto://iban/$MERCHANT_IBAN_UMAMI?receiver-name=Umami"
    143   MERCHANT_PAYTO_PAIVANA="payto://iban/$MERCHANT_IBAN_PAIVANA?receiver-name=Paivana+Merchant"
    144   MERCHANT_PAYTO_SANDBOX="payto://iban/$MERCHANT_IBAN_SANDBOX?receiver-name=Sandbox+Merchant"
    145 elif [[ $WIRE_METHOD = x-taler-bank ]]; then
    146   XTBHOST=sandcastle
    147   EXCHANGE_PAYTO="payto://x-taler-bank/$XTBHOST/exchange?receiver-name=Sandcastle+Exchange+Inc"
    148   MERCHANT_PAYTO_ADMIN="payto://x-taler-bank/$XTBHOST/merchant-admin?receiver-name=Admin+Merchant"
    149   MERCHANT_PAYTO_TESTING="payto://x-taler-bank/$XTBHOST/merchant-testing?receiver-name=Default+Merchant"
    150   MERCHANT_PAYTO_POS="payto://x-taler-bank/$XTBHOST/merchant-pos?receiver-name=PoS+Merchant"
    151   MERCHANT_PAYTO_LANDING_SAMPLES="payto://x-taler-bank/$XTBHOST/merchant-landing-samples?receiver-name=Landing+Samples+Merchant"
    152   MERCHANT_PAYTO_BLOG="payto://x-taler-bank/$XTBHOST/merchant-blog?receiver-name=Blog+Merchant"
    153   MERCHANT_PAYTO_GNUNET="payto://x-taler-bank/$XTBHOST/merchant-gnunet?receiver-name=GNUnet+Merchant"
    154   MERCHANT_PAYTO_TALER="payto://x-taler-bank/$XTBHOST/merchant-taler?receiver-name=Taler+Merchant"
    155   MERCHANT_PAYTO_TOR="payto://x-taler-bank/$XTBHOST/merchant-tor?receiver-name=Tor+Merchant"
    156   MERCHANT_PAYTO_UMAMI="payto://x-taler-bank/$XTBHOST/merchant-umami?receiver-name=Umami"
    157   MERCHANT_PAYTO_PAIVANA="payto://x-taler-bank/$XTBHOST/merchant-paivana?receiver-name=Paivana+Merchant"
    158   MERCHANT_PAYTO_SANDBOX="payto://x-taler-bank/$XTBHOST/merchant-sandbox?receiver-name=Sandbox+Merchant"
    159 else
    160   echo "wire method $WIRE_METHOD not supported"
    161   exit 1
    162 fi
    163 
    164 MYDOMAIN=${MYDOMAIN:="demo.taler.net"}
    165 LANDING_DOMAIN=$MYDOMAIN
    166 BANK_DOMAIN=bank.$MYDOMAIN
    167 EXCHANGE_DOMAIN=exchange.$MYDOMAIN
    168 MERCHANT_DOMAIN=backend.$MYDOMAIN
    169 DONAU_DOMAIN=donau.$MYDOMAIN
    170 BLOG_DOMAIN=shop.$MYDOMAIN
    171 DONATIONS_DOMAIN=donations.$MYDOMAIN
    172 DRUPAL_DOMAIN=drupal.$MYDOMAIN
    173 CHALLENGER_DOMAIN=challenger.$MYDOMAIN
    174 AUDITOR_DOMAIN=auditor.$MYDOMAIN
    175 PAIVANA_DOMAIN=paivana.$MYDOMAIN
    176 
    177 # Website that is put behind the Paivana paywall.
    178 # Note that this is an *upstream* server, it is not
    179 # served by the sandcastle itself.
    180 PAIVANA_DESTINATION=${PAIVANA_DESTINATION:-https://docs.taler.net/}
    181 
    182 # Port of libeufin-bank running inside the container.
    183 # Should be synchronized with the sandcastle-run script.
    184 PORT_INTERNAL_LIBEUFIN_BANK=8080
    185 
    186 
    187 ENABLE_AUDITOR=0
    188 
    189 # Just make sure the services are stopped
    190 systemctl stop postgresql.service
    191 systemctl stop taler-auditor.target
    192 systemctl stop taler-auditor-httpd.socket
    193 systemctl stop taler-exchange.target
    194 systemctl stop taler-exchange-httpd.socket
    195 systemctl stop taler-exchange-offline.timer
    196 systemctl stop taler-merchant-httpd.service
    197 systemctl stop taler-merchant-httpd.socket
    198 systemctl stop taler-merchant.target
    199 systemctl stop taler-demo-landing.service
    200 systemctl stop taler-demo-blog.service
    201 systemctl stop taler-demo-donations.service
    202 systemctl stop libeufin-bank.service
    203 systemctl stop donau-httpd.service
    204 systemctl stop donau-httpd.socket
    205 systemctl stop paivana-httpd.service
    206 systemctl stop paivana-httpd.socket
    207 
    208 # libeufin-nexus is not used
    209 systemctl stop libeufin-nexus-ebics-fetch.service
    210 systemctl disable libeufin-nexus-ebics-fetch.service
    211 systemctl stop libeufin-nexus-ebics-submit.service
    212 systemctl disable libeufin-nexus-ebics-submit.service
    213 
    214 systemctl reset-failed
    215 
    216 # We now make sure that some important locations are symlinked to
    217 # the persistent storage volume.
    218 # Files that already exist in this location are moved to the storage volume
    219 # and then symlinked.
    220 # These locations are:
    221 # /etc/taler
    222 # /etc/libeufin
    223 # /var/lib/taler
    224 # postgres DB directory
    225 
    226 function lift_dir() {
    227   where=$1
    228   src=$2
    229   target=$3
    230   if [[ -L $src ]]; then
    231     # be idempotent
    232     echo "$src is already a symlink"
    233   elif [[ -d /$where/$target ]]; then
    234     echo "symlinking existing /$where/$target"
    235     rm -rf "$src"
    236     ln -s "/$where/$target" "$src"
    237   else
    238     echo "symlinking new /$where/$target"
    239     mv "$src" "/$where/$target"
    240     ln -s "/$where/$target" "$src"
    241   fi
    242 }
    243 
    244 lift_dir talerdata /var/lib/taler-exchange var-lib-taler-exchange
    245 lift_dir talerdata /etc/taler-merchant etc-taler-merchant
    246 lift_dir talerdata /etc/taler-exchange etc-taler-exchange
    247 lift_dir talerdata /etc/taler-exchange etc-taler-auditor
    248 lift_dir talerdata /etc/donau etc-donau
    249 lift_dir talerdata /etc/libeufin etc-libeufin
    250 # lift both config and data
    251 lift_dir talerdata /etc/postgresql etc-postgresql
    252 lift_dir talerdata /var/lib/postgresql var-lib-postgresql
    253 # offline keys are in a separate volume
    254 lift_dir talerdata_persistent /var/lib/taler-exchange/offline exchange-offline
    255 
    256 # Usage: get_credential_pw COMPONENT/ACCOUNT
    257 function get_credential_pw() {
    258   if [[ ${USE_INSECURE_SANDBOX_PASSWORDS:-0} = 1 ]]; then
    259     echo "sandbox"
    260     return
    261   fi
    262   p=/credentials/$1
    263   if [[ ! -f $p ]]; then
    264     mkdir -p $(dirname "$p")
    265     uuidgen -r >$p
    266   fi
    267   cat "$p"
    268 }
    269 
    270 import_instr=none
    271 if [[ -d /exported && -e /exported/import-request ]]; then
    272   import_instr=$(cat /exported/import-request)
    273 fi
    274 
    275 # If necessary, import the offline key.
    276 # Done before everything else, as we need the key
    277 # to generate the config.
    278 
    279 if [[ $import_instr = all ]]; then
    280   echo "Importing exchange offline key"
    281   rm -rf /var/lib/taler-exchange/offline/*
    282   cp -r /exported/taler-exchange/offline/* /var/lib/taler-exchange/offline/
    283 fi
    284 
    285 # Adjust ownership.
    286 # Necessary when the container is rebuilt with different user IDs.
    287 chown --recursive taler-exchange-offline:taler-exchange-offline /var/lib/taler-exchange/offline/. || true
    288 chown root:taler-exchange-db /etc/taler-exchange/secrets/exchange-db.secret.conf
    289 chown taler-exchange-wire:root /etc/taler-exchange/secrets/exchange-accountcredentials-*.conf
    290 chown taler-merchant-httpd:root /etc/taler-merchant/secrets/merchant-db.secret.conf
    291 chown root:donau-db /etc/donau/secrets/donau-db.secret.conf
    292 
    293 
    294 MASTER_PUBLIC_KEY=$(sudo -u taler-exchange-offline taler-exchange-offline -LDEBUG setup)
    295 
    296 
    297 #
    298 # Create the basic configuration files
    299 #
    300 
    301 mkdir -p /etc/challenger/conf.d
    302 cat <<EOF >/etc/challenger/conf.d/setup-sandcastle.conf
    303 [challenger]
    304 BASE_URL = $PROTO://$CHALLENGER_DOMAIN$PORT_SUFFIX/
    305 ADDRESS_TYPE = email
    306 AUTH_COMMAND = /data/sandcastle-challenger-auth
    307 ADDRESS_RESTRICTIONS = {"email":{"hint":"not an e-mail address","regex":"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$"}}
    308 EOF
    309 
    310 # Move systemd-activated HTTP listeners into the bind-mounted directory so the
    311 # host can use them directly without an extra reverse-proxy hop.
    312 configure_public_socket() {
    313   local unit=$1
    314   local socket_path=$2
    315   local drop_in_dir=/etc/systemd/system/$unit.d
    316 
    317   mkdir -p "$drop_in_dir"
    318   cat <<EOF >"$drop_in_dir/sandcastle.conf"
    319 [Socket]
    320 ListenStream=
    321 ListenStream=$socket_path
    322 SocketMode=0666
    323 RemoveOnStop=yes
    324 EOF
    325 }
    326 
    327 configure_public_socket taler-exchange-httpd.socket /sockets/exchange.sock
    328 configure_public_socket taler-merchant-httpd.socket /sockets/merchant-backend.sock
    329 configure_public_socket taler-auditor-httpd.socket /sockets/auditor.sock
    330 configure_public_socket donau-httpd.socket /sockets/donau.sock
    331 configure_public_socket paivana-httpd.socket /sockets/paivana.sock
    332 
    333 mkdir -p /etc/paivana
    334 cat <<EOF >/etc/paivana/paivana.conf
    335 [paivana]
    336 # Serve on the socket provided by paivana-httpd.socket.
    337 SERVE = systemd
    338 
    339 # Public base URL of the paywall.
    340 BASE_URL = $PROTO://$PAIVANA_DOMAIN$PORT_SUFFIX/
    341 
    342 # Upstream site that is protected by the paywall.
    343 DESTINATION_BASE_URL = $PAIVANA_DESTINATION
    344 
    345 # Merchant instance that sells access to the site.
    346 MERCHANT_BACKEND_URL = $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/paivana/
    347 MERCHANT_ACCESS_TOKEN = secret-token:$(get_credential_pw merchant/paivana)
    348 
    349 # Stable key for the access cookie MAC, so that paid clients
    350 # survive a restart of paivana-httpd.
    351 SECRET = $(get_credential_pw paivana/secret)
    352 
    353 # Static resources are served without payment, as the
    354 # protected site is otherwise unusable after paying.
    355 WHITELIST = ^/(favicon\.ico|robots\.txt|.*\.(css|js|png|jpe?g|gif|svg|ico|webp|woff2?|ttf|eot))\$
    356 EOF
    357 
    358 # The config contains the merchant access token and the cookie secret.
    359 chown paivana-httpd:root /etc/paivana/paivana.conf
    360 chmod 640 /etc/paivana/paivana.conf
    361 
    362 # The paywall runs behind a reverse proxy, so we need
    363 # '-f' to get the client address from X-Forwarded-For.  With '-g'
    364 # a single payment unlocks the entire site instead of just the
    365 # URL that was paid for.
    366 mkdir -p /etc/systemd/system/paivana-httpd.service.d
    367 cat <<EOF >/etc/systemd/system/paivana-httpd.service.d/sandcastle.conf
    368 [Unit]
    369 # Paywall templates are fetched from the merchant backend on
    370 # startup, so keep retrying until the merchant is up.
    371 After=taler-merchant-httpd.service
    372 StartLimitIntervalSec=0
    373 
    374 [Service]
    375 ExecStart=
    376 ExecStart=/usr/bin/paivana-httpd -c /etc/paivana/paivana.conf -f -g -L INFO
    377 EOF
    378 
    379 systemctl daemon-reload
    380 
    381 cat <<EOF >/etc/libeufin/libeufin-bank.conf
    382 [libeufin-bank]
    383 BASE_URL = $PROTO://$BANK_DOMAIN$PORT_SUFFIX/
    384 CURRENCY = $CURRENCY
    385 DEFAULT_DEBT_LIMIT = $CURRENCY:500
    386 REGISTRATION_BONUS = $CURRENCY:100
    387 SPA_CAPTCHA_URL = $PROTO://$BANK_DOMAIN$PORT_SUFFIX/webui/#/operation/{woid}
    388 SUGGESTED_WITHDRAWAL_EXCHANGE = $PROTO://$EXCHANGE_DOMAIN$PORT_SUFFIX/
    389 ALLOW_REGISTRATION = yes
    390 SERVE = tcp
    391 PORT = $PORT_INTERNAL_LIBEUFIN_BANK
    392 # Bind address.
    393 # Option soon to be deprecated!
    394 ADDRESS = 127.0.0.1
    395 WIRE_TYPE = $WIRE_METHOD
    396 
    397 # Compat mode for now
    398 PWD_CHECK = no
    399 PWD_AUTH_COMPAT = yes
    400 
    401 [currency-$CURRENCY]
    402 ENABLED = YES
    403 name = "${NAME:=Kudos}"
    404 code = "$CURRENCY"
    405 decimal_separator = "."
    406 fractional_input_digits = ${FRACTIONALS:=2}
    407 fractional_normal_digits = ${FRACTIONALS:=2}
    408 fractional_trailing_zero_digits = ${FRACTIONALS:=2}
    409 is_currency_name_leading = NO
    410 alt_unit_names = {"0":"${ALT_UNIT_NAME:=ク}"}
    411 EOF
    412 
    413 cat <<EOF >/usr/share/libeufin-bank-webui/settings.json
    414 {
    415   "backendBaseURL": "$PROTO://$BANK_DOMAIN$PORT_SUFFIX/",
    416   "iconLinkURL": "$PROTO://$LANDING_DOMAIN$PORT_SUFFIX/",
    417   "enableDemoHeader": true,
    418   "showPublicAccounts": true,
    419   "defaultSuggestedAmount": 10,
    420   "topNavSites": {
    421     "Introduction": "$PROTO://$LANDING_DOMAIN$PORT_SUFFIX/",
    422     "Bank": "$PROTO://$BANK_DOMAIN$PORT_SUFFIX/",
    423     "Essay Shop": "$PROTO://$BLOG_DOMAIN$PORT_SUFFIX/",
    424     "Donations": "$PROTO://$DONATIONS_DOMAIN$PORT_SUFFIX/"
    425   }
    426 }
    427 EOF
    428 
    429 # Generate /tmp/sandcastle-setup.conf
    430 cat <<EOF >/tmp/sandcastle-setup.conf
    431 [currency-$CURRENCY]
    432 ENABLED = YES
    433 name = "${NAME:=Kudos}"
    434 code = "$CURRENCY"
    435 decimal_separator = "."
    436 fractional_input_digits = ${FRACTIONALS:=2}
    437 fractional_normal_digits = ${FRACTIONALS:=2}
    438 fractional_trailing_zero_digits = ${FRACTIONALS:=2}
    439 is_currency_name_leading = NO
    440 alt_unit_names = {"0":"${ALT_UNIT_NAME:=ク}"}
    441 EOF
    442 
    443 cp /tmp/sandcastle-setup.conf /etc/taler-exchange/conf.d/sandcastle-setup.conf
    444 cp /tmp/sandcastle-setup.conf /etc/taler-merchant/conf.d/sandcastle-setup.conf
    445 
    446 
    447 cat <<EOF >/etc/taler-exchange/conf.d/sandcastle-exchange.conf
    448 [exchange]
    449 CURRENCY = $CURRENCY
    450 CURRENCY_ROUND_UNIT = $CURRENCY:0.01
    451 TINY_AMOUNT = $CURRENCY:0.01
    452 AML_THRESHOLD = $CURRENCY:1000000
    453 MASTER_PUBLIC_KEY = $MASTER_PUBLIC_KEY
    454 BASE_URL = $PROTO://$EXCHANGE_DOMAIN$PORT_SUFFIX/
    455 
    456 [taler-exchange-secmod-rsa]
    457 LOOKAHEAD_SIGN = 4 weeks
    458 
    459 [taler-exchange-secmod-eddsa]
    460 LOOKAHEAD_SIGN = 4 weeks
    461 
    462 [taler-exchange-secmod-cs]
    463 LOOKAHEAD_SIGN = 4 weeks
    464 
    465 [exchange-account-default]
    466 PAYTO_URI = $EXCHANGE_PAYTO
    467 ENABLE_DEBIT = YES
    468 ENABLE_CREDIT = YES
    469 @inline-secret@ exchange-accountcredentials-default ../secrets/exchange-accountcredentials-default.secret.conf
    470 EOF
    471 
    472 
    473 cat <<EOF >/etc/taler-exchange/secrets/exchange-accountcredentials-default.secret.conf
    474 [exchange-accountcredentials-default]
    475 WIRE_GATEWAY_URL = $PROTO://$BANK_DOMAIN$PORT_SUFFIX/accounts/exchange/taler-wire-gateway/
    476 WIRE_GATEWAY_AUTH_METHOD = basic
    477 USERNAME = exchange
    478 PASSWORD = $(get_credential_pw bank/exchange)
    479 EOF
    480 
    481 if [[ $ENABLE_AUDITOR = 1 ]]; then
    482   # Make sandcastle exchange config available to auditor
    483   cp /etc/taler-exchange/conf.d/sandcastle-exchange.conf /etc/taler-auditor/conf.d/sandcastle-exchange.conf
    484 
    485   # We run the offline tooling as root, maybe in the future there should be
    486   # a separate user created by the Debian package for that.
    487   AUDITOR_PUB=$(taler-auditor-offline setup)
    488 
    489   cat <<EOF >/etc/taler-auditor/conf.d/sandcastle-auditor.conf
    490 [auditor]
    491 PUBLIC_KEY = $AUDITOR_PUB
    492 
    493 [exchangedb]
    494 
    495 $(dup_exchange_opt exchangedb IDLE_RESERVE_EXPIRATION_TIME)
    496 $(dup_exchange_opt exchangedb LEGAL_RESERVE_EXPIRATION_TIME)
    497 $(dup_exchange_opt exchangedb AGGREGATOR_SHIFT)
    498 $(dup_exchange_opt exchangedb DEFAULT_PURSE_LIMIT)
    499 
    500 [exchangedb-postgres]
    501 $(dup_exchange_opt exchangedb-postgres CONFIG)
    502 
    503 [exchange]
    504 $(dup_exchange_opt exchange CURRENCY)
    505 $(dup_exchange_opt exchange CURRENCY_ROUND_UNIT)
    506 $(dup_exchange_opt exchange DB)
    507 
    508 
    509 EOF
    510 fi
    511 
    512 # The config shipped with the package can conflict with the
    513 # trusted sandcastle exchange if the currency is KUDOS.
    514 rm -f /usr/share/taler-exchange/config.d/kudos.conf
    515 rm -f /usr/share/taler-merchant/config.d/kudos.conf
    516 
    517 MY_HELPER_EMAIL=${OVERRIDE_MERCHANT_HELPER_EMAIL:-/data/sandcastle-merchant-email-helper}
    518 
    519 # We need to define the default currency for the UI.
    520 cat <<EOF >/etc/taler-merchant/conf.d/sandcastle-merchant.conf
    521 [merchant]
    522 # Default currency
    523 CURRENCY = $CURRENCY
    524 ENABLE_SELF_PROVISIONING = YES
    525 MANDATORY_TAN_CHANNELS = email
    526 HELPER_EMAIL = $MY_HELPER_EMAIL
    527 
    528 BASE_URL = $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/
    529 
    530 # Will be the default in future merchant versions
    531 # => Can be removed after 1.6 release
    532 BACKOFFICE_SPA_DIR = /usr/share/taler-merchant-webui/
    533 
    534 EOF
    535 
    536 cat <<EOF >/etc/taler-merchant/conf.d/sandcastle-merchant-exchanges.conf
    537 [merchant-exchange-sandcastle]
    538 EXCHANGE_BASE_URL = $PROTO://$EXCHANGE_DOMAIN$PORT_SUFFIX/
    539 MASTER_KEY = $MASTER_PUBLIC_KEY
    540 CURRENCY = $CURRENCY
    541 EOF
    542 
    543 # Allow overrides to modify merchant config
    544 [[ $(type -t hook_merchant_config) == function ]] && hook_merchant_config
    545 
    546 mkdir -p /etc/taler-merchant-demos
    547 install -d -o taler-merchant-demos -g taler-merchant-demos /run/taler-merchant-demos
    548 
    549 cat <<EOF >/etc/taler-merchant-demos/taler-merchant-demos.conf
    550 [taler]
    551 CURRENCY = $CURRENCY
    552 
    553 [frontend-demo]
    554 LANDING_URL = $PROTO://$LANDING_DOMAIN$PORT_SUFFIX/
    555 BANK_URL = $PROTO://$BANK_DOMAIN$PORT_SUFFIX/
    556 BLOG_URL = $PROTO://$BLOG_DOMAIN$PORT_SUFFIX/
    557 DONATIONS_URL = $PROTO://$DONATIONS_DOMAIN$PORT_SUFFIX/
    558 
    559 [frontend-demo-landing]
    560 HTTP_SERVE = unix
    561 HTTP_UNIXPATH = /run/taler-merchant-demos/landing.sock
    562 HTTP_UNIXPATH_MODE = 666
    563 ENABLE_MERCHANT_SAMPLES = yes
    564 BACKEND_URL = $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/landing-samples/
    565 BACKEND_APIKEY = secret-token:$(get_credential_pw merchant/landing-samples)
    566 
    567 [frontend-demo-blog]
    568 HTTP_SERVE = unix
    569 HTTP_UNIXPATH = /sockets/blog.sock
    570 HTTP_UNIXPATH_MODE = 666
    571 BACKEND_URL = $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/blog/
    572 BACKEND_APIKEY = secret-token:$(get_credential_pw merchant/blog)
    573 ENABLE_TOKENS = ${ENABLE_SUBSCRIPTIONS:-no}
    574 
    575 [frontend-demo-donations]
    576 DONAU_URL = $PROTO://$DONAU_DOMAIN$PORT_SUFFIX/
    577 HTTP_SERVE = unix
    578 HTTP_UNIXPATH = /sockets/donations.sock
    579 HTTP_UNIXPATH_MODE = 666
    580 BACKEND_URL_TOR = $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/tor/
    581 BACKEND_APIKEY_TOR = secret-token:$(get_credential_pw merchant/tor)
    582 BACKEND_URL_TALER = $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/taler/
    583 BACKEND_APIKEY_TALER = secret-token:$(get_credential_pw merchant/taler)
    584 BACKEND_URL_GNUNET = $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/gnunet/
    585 BACKEND_APIKEY_GNUNET = secret-token:$(get_credential_pw merchant/gnunet)
    586 EOF
    587 
    588 #
    589 # Create databases
    590 #
    591 
    592 function wait_pg_ready() {
    593   while true; do
    594     ret=0
    595     pg_isready || ret=$?
    596     case "$ret" in
    597       0)
    598         echo "Postgres is ready" >&2
    599         break
    600         ;;
    601       1|2)
    602         echo "pg_isready returned status $ret, waiting" >&2
    603         sleep 1
    604         ;;
    605       3)
    606         echo "pg_isready returned status $ret, giving up" >&2
    607         exit 3
    608         ;;
    609     esac
    610   done
    611 }
    612 
    613 PG_VERSION="17"
    614 
    615 # Since the sandcastle is a test system, we turn fsync off for performance
    616 # reasons (especially with the drupal setup).
    617 # CAUTION: You do not want to set this in production,
    618 # especially not for the taler-exchange.
    619 pg_conftool $PG_VERSION main set fsync off
    620 # Higher limits needed for migration
    621 pg_conftool $PG_VERSION main set max_locks_per_transaction 512
    622 pg_conftool $PG_VERSION main set max_pred_locks_per_transaction 512
    623 
    624 backup_file=/exported/postgres-backup.sql
    625 if [[ $import_instr = singledump ]]; then
    626   echo "Importing database dump"
    627   if [[ ! -e "$backup_file" ]]; then
    628     echo "Requested import, but backup file does not exist" >&2
    629     exit 1
    630   fi
    631   pg_dropcluster --stop 17 main || true
    632   pg_createcluster 17 main
    633   systemctl start postgresql.service
    634   wait_pg_ready
    635   sudo -u postgres psql postgres -f "$backup_file"
    636 else
    637   systemctl start postgresql.service
    638   wait_pg_ready
    639 fi
    640 
    641 # Set up databases.
    642 # Do that *before* we potentially do a per-service restore-from-backup.
    643 
    644 challenger-dbconfig
    645 
    646 # Sets up the database for both libeufin-bank and libeufin-nexus.  We only need
    647 # the libeufin-bank DB though.
    648 libeufin-dbconfig
    649 
    650 if [[ $ENABLE_AUDITOR = 1 ]]; then
    651   # Add auditor user to DB group *before* running taler-exchange-dbconfig,
    652   # so that DB permissions are adjusted accordingly.
    653   usermod taler-auditor-httpd -aG taler-exchange-db
    654   taler-auditor-dbconfig
    655 fi
    656 
    657 taler-exchange-dbconfig
    658 
    659 taler-merchant-dbconfig
    660 
    661 
    662 #
    663 # Import backup if necessary.
    664 #
    665 
    666 if [[ $import_instr = all ]]; then
    667   echo "Importing databases"
    668 
    669   # FIXME: Consider backing up old DB before importing new one
    670   # FIXME: This is rather hacky, it would be better to use "pg_dump -Fc" and "pg_restore"
    671   sudo -u postgres dropdb taler-exchange
    672   sudo -u postgres dropdb taler-merchant
    673   sudo -u postgres dropdb libeufin
    674 
    675   sudo -u postgres createdb taler-exchange
    676   sudo -u postgres createdb taler-merchant
    677   sudo -u postgres createdb libeufin
    678 
    679   sudo -u postgres psql taler-exchange -f /exported/taler-exchange/taler-exchange.sql
    680   sudo -u postgres psql taler-merchant -f /exported/taler-merchant/taler-merchant.sql
    681   sudo -u postgres psql libeufin -f /exported/libeufin/libeufin.sql
    682 
    683   libeufin-dbconfig
    684   taler-exchange-dbconfig
    685   taler-merchant-dbconfig
    686 
    687   rm -rf /var/lib/taler-exchange/secmod-eddsa/*
    688   cp -r /exported/taler-exchange/secmod-eddsa/* /var/lib/taler-exchange/secmod-eddsa/
    689 
    690   rm -rf /var/lib/taler-exchange/secmod-rsa/*
    691   cp -r /exported/taler-exchange/secmod-rsa/* /var/lib/taler-exchange/secmod-rsa/
    692 
    693   rm -rf /var/lib/taler-exchange/secmod-cs/*
    694   cp -r /exported/taler-exchange/secmod-cs/* /var/lib/taler-exchange/secmod-cs/
    695 fi
    696 
    697 if [[ $import_instr != none ]]; then
    698   echo "Marking import as done"
    699   rm /exported/import-request
    700 fi
    701 
    702 # We need to adjust file ownership, as the container might have different user and group
    703 # IDs than the volume. That can happen when the packages in the container are installed
    704 # in a different order.
    705 # This is only relevant for non-root ownership.
    706 chown --recursive taler-exchange-offline:taler-exchange-offline /var/lib/taler-exchange/offline/* || true
    707 chown --recursive taler-exchange-secmod-cs:taler-exchange-secmod /var/lib/taler-exchange/secmod-cs
    708 chown --recursive taler-exchange-secmod-rsa:taler-exchange-secmod /var/lib/taler-exchange/secmod-rsa
    709 chown --recursive taler-exchange-secmod-eddsa:taler-exchange-secmod /var/lib/taler-exchange/secmod-eddsa
    710 chown root:taler-exchange-db /etc/taler-exchange/secrets/exchange-db.secret.conf
    711 chown root:taler-auditor-httpd /etc/taler-auditor/secrets/auditor-db.secret.conf
    712 chmod 440 /etc/taler-merchant/secrets/merchant-db.secret.conf
    713 chown taler-merchant-httpd:root /etc/taler-merchant/secrets/merchant-db.secret.conf
    714 chown root:taler-exchange-db /etc/taler-exchange/secrets/exchange-db.secret.conf
    715 chown taler-exchange-wire:taler-exchange-db /etc/taler-exchange/secrets/exchange-accountcredentials-default.secret.conf
    716 
    717 
    718 # Caddy configuration.
    719 # We use the caddy reverse proxy with automatic
    720 # internal TLS setup to ensure that the services are
    721 # reachable inside the container without any external
    722 # DNS setup under the same domain name and with TLS
    723 # from inside the container.
    724 
    725 systemctl stop caddy.service
    726 
    727 cat <<EOF >/etc/caddy/Caddyfile
    728 {
    729   servers {
    730       trusted_proxies static private_ranges
    731   }
    732 }
    733 
    734 (sandcastle_bank) {
    735   # Keep the incoming language and other query parameters when entering the
    736   # SPA. The bank's own root redirect currently discards them.
    737   @bank_root {
    738     method GET HEAD
    739     path /
    740   }
    741   route @bank_root {
    742     rewrite * /webui/
    743     redir {uri} 302
    744   }
    745   @bank_locale {
    746     method GET HEAD
    747     path_regexp bank_locale ^/([A-Za-z]{2}([-_][A-Za-z0-9]{2,8})*)/?$
    748   }
    749   route @bank_locale {
    750     uri query lang {re.bank_locale.1}
    751     rewrite * /webui/
    752     redir {uri} 302
    753   }
    754   reverse_proxy 127.0.0.1:$PORT_INTERNAL_LIBEUFIN_BANK {
    755     # libeufin-bank should eventually not require this anymore,
    756     # but currently doesn't work without this header.
    757     header_up X-Forwarded-Prefix ""
    758   }
    759 }
    760 
    761 (sandcastle_landing) {
    762   handle /metrics/sandcastle-status {
    763     root * /var/www/sandcastle
    764     header Content-Type "text/plain; charset=utf-8"
    765     file_server
    766   }
    767   handle {
    768     reverse_proxy unix//run/taler-merchant-demos/landing.sock
    769   }
    770 }
    771 
    772 (sandcastle_drupal) {
    773   root * /talerdata/sandcastle-drupal/web/
    774   php_fastcgi unix/var/run/php/php8.4-fpm.sock
    775   file_server
    776 }
    777 
    778 (sandcastle_challenger) {
    779   handle {
    780     reverse_proxy unix//run/challenger/httpd/challenger-http.sock {
    781       # Set this, or otherwise wrong taler://pay URIs will be generated.
    782       header_up X-Forwarded-Proto "https"
    783     }
    784   }
    785 
    786   # Serve challenges via HTTP.
    787   # This is obviously completely insecure, but fine
    788   # for the demo sandcastle.
    789   handle_path /challenges/* {
    790     root * /tmp/challenges/
    791     file_server {
    792       browse
    793     }
    794   }
    795 }
    796 
    797 # Caddy owns sockets only where it bridges a TCP-only service, composes
    798 # additional routing, or serves the application itself.  Native services or
    799 # their socket units own the others.
    800 
    801 http:// {
    802   bind unix//sockets/bank.sock|0666
    803   import sandcastle_bank
    804 }
    805 
    806 http:// {
    807   bind unix//sockets/landing.sock|0666
    808   import sandcastle_landing
    809 }
    810 
    811 http:// {
    812   bind unix//sockets/challenger.sock|0666
    813   import sandcastle_challenger
    814 }
    815 
    816 http:// {
    817   bind unix//sockets/drupal.sock|0666
    818   import sandcastle_drupal
    819 }
    820 EOF
    821 
    822 if [[ $PROTO = https ]]; then
    823   cat <<EOF >>/etc/caddy/Caddyfile
    824 
    825 # Internally reverse-proxy https://,
    826 # so that service can talk to each other via
    827 # https:// inside the container.
    828 
    829 https://$BANK_DOMAIN {
    830   tls internal
    831   import sandcastle_bank
    832 }
    833 
    834 https://$LANDING_DOMAIN {
    835   tls internal
    836   import sandcastle_landing
    837 }
    838 
    839 https://$EXCHANGE_DOMAIN {
    840   tls internal
    841   reverse_proxy unix//sockets/exchange.sock
    842 }
    843 
    844 https://$MERCHANT_DOMAIN {
    845   tls internal
    846   reverse_proxy unix//sockets/merchant-backend.sock {
    847     # Set this, or otherwise wrong taler://pay URIs will be generated.
    848     header_up X-Forwarded-Proto "https"
    849   }
    850 }
    851 
    852 https://$DONAU_DOMAIN {
    853   tls internal
    854   reverse_proxy unix//sockets/donau.sock {
    855     header_up X-Forwarded-Proto "https"
    856   }
    857 }
    858 
    859 https://$AUDITOR_DOMAIN {
    860   tls internal
    861   reverse_proxy unix//sockets/auditor.sock
    862 }
    863 
    864 https://$CHALLENGER_DOMAIN {
    865   tls internal
    866   reverse_proxy unix//run/challenger/httpd/challenger-http.sock
    867 }
    868 
    869 https://$PAIVANA_DOMAIN {
    870   tls internal
    871   reverse_proxy unix//sockets/paivana.sock
    872 }
    873 
    874 EOF
    875 
    876 else
    877   # Config for HTTP without TLS.
    878 
    879   cat <<EOF >>/etc/caddy/Caddyfile
    880 
    881 http://$BANK_DOMAIN$PORT_SUFFIX {
    882   import sandcastle_bank
    883 }
    884 
    885 http://$EXCHANGE_DOMAIN$PORT_SUFFIX {
    886   reverse_proxy unix//sockets/exchange.sock
    887 }
    888 
    889 http://$MERCHANT_DOMAIN$PORT_SUFFIX {
    890   reverse_proxy unix//sockets/merchant-backend.sock
    891 }
    892 
    893 http://$DONAU_DOMAIN$PORT_SUFFIX {
    894   reverse_proxy unix//sockets/donau.sock
    895 }
    896 
    897 http://$AUDITOR_DOMAIN$PORT_SUFFIX {
    898   reverse_proxy unix//sockets/auditor.sock
    899 }
    900 
    901 http://$CHALLENGER_DOMAIN$PORT_SUFFIX {
    902   reverse_proxy unix//run/challenger/httpd/challenger-http.sock
    903 }
    904 
    905 http://$LANDING_DOMAIN$PORT_SUFFIX {
    906   import sandcastle_landing
    907 }
    908 
    909 http://$BLOG_DOMAIN$PORT_SUFFIX {
    910   reverse_proxy unix//sockets/blog.sock
    911 }
    912 
    913 http://$DONATIONS_DOMAIN$PORT_SUFFIX {
    914   reverse_proxy unix//sockets/donations.sock
    915 }
    916 
    917 http://$DRUPAL_DOMAIN$PORT_SUFFIX {
    918   import sandcastle_drupal
    919 }
    920 
    921 http://$PAIVANA_DOMAIN$PORT_SUFFIX {
    922   reverse_proxy unix//sockets/paivana.sock
    923 }
    924 
    925 EOF
    926 
    927 fi
    928 
    929 cat <<EOF >>/etc/hosts
    930 # Start of Taler Sandcastle Domains
    931 127.0.0.1 $LANDING_DOMAIN
    932 127.0.0.1 $BANK_DOMAIN
    933 127.0.0.1 $EXCHANGE_DOMAIN
    934 127.0.0.1 $MERCHANT_DOMAIN
    935 127.0.0.1 $BLOG_DOMAIN
    936 127.0.0.1 $DONATIONS_DOMAIN
    937 127.0.0.1 $DONAU_DOMAIN
    938 127.0.0.1 $CHALLENGER_DOMAIN
    939 127.0.0.1 $DRUPAL_DOMAIN
    940 127.0.0.1 $PAIVANA_DOMAIN
    941 # End of Taler Sandcastle Domains
    942 EOF
    943 
    944 systemctl start caddy.service
    945 
    946 # Install local, internal CA certs for caddy
    947 caddy trust
    948 
    949 # Set up challenger
    950 
    951 CHALLENGER_CLIENT_SECRET=secret-token:sandbox
    952 CHALLENGER_CLIENT_ID=$(sudo -u challenger-httpd challenger-admin -q --add="$CHALLENGER_CLIENT_SECRET" https://$EXCHANGE_DOMAIN/kyc-proof/mychallenger)
    953 echo Challenger client ID: $CHALLENGER_CLIENT_ID
    954 
    955 systemctl enable --now challenger-httpd.service
    956 
    957 # Set up bank
    958 
    959 sudo -u libeufin-bank libeufin-bank edit-account admin --debit_threshold=$CURRENCY:10000000
    960 sudo -u libeufin-bank libeufin-bank passwd admin $(get_credential_pw bank/admin)
    961 
    962 systemctl enable --now libeufin-bank.service
    963 
    964 BANK_BASEURL=$PROTO://$BANK_DOMAIN$PORT_SUFFIX/
    965 
    966 taler-harness deployment wait-taler-service taler-corebank ${BANK_BASEURL}config
    967 
    968 sudo -u libeufin-bank libeufin-bank passwd exchange $(get_credential_pw bank/exchange) || true
    969 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
    970   --login exchange --exchange --public \
    971   --payto $EXCHANGE_PAYTO \
    972   --name Exchange \
    973   --password $(get_credential_pw bank/exchange)
    974 
    975 sudo -u libeufin-bank libeufin-bank passwd merchant-admin $(get_credential_pw bank/merchant-admin) || true
    976 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
    977   --login merchant-admin --public \
    978   --payto $MERCHANT_PAYTO_ADMIN \
    979   --name "Default Demo Merchant" \
    980   --password $(get_credential_pw bank/merchant-admin)
    981 
    982 sudo -u libeufin-bank libeufin-bank passwd merchant-testing $(get_credential_pw bank/merchant-testing) || true
    983 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
    984   --login merchant-testing --public \
    985   --payto $MERCHANT_PAYTO_TESTING \
    986   --name "Testing Merchant" \
    987   --password $(get_credential_pw bank/merchant-testing)
    988 
    989 sudo -u libeufin-bank libeufin-bank passwd merchant-pos $(get_credential_pw bank/merchant-pos) || true
    990 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
    991   --login merchant-pos --public \
    992   --payto $MERCHANT_PAYTO_POS \
    993   --name "PoS Merchant" \
    994   --password $(get_credential_pw bank/merchant-pos)
    995 
    996 sudo -u libeufin-bank libeufin-bank passwd merchant-landing-samples "$(get_credential_pw bank/merchant-landing-samples)" || true
    997 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
    998   --login merchant-landing-samples --public \
    999   --payto "$MERCHANT_PAYTO_LANDING_SAMPLES" \
   1000   --name "Landing Samples Merchant" \
   1001   --password "$(get_credential_pw bank/merchant-landing-samples)"
   1002 
   1003 sudo -u libeufin-bank libeufin-bank passwd merchant-blog $(get_credential_pw bank/merchant-blog) || true
   1004 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
   1005   --login merchant-blog --public \
   1006   --payto $MERCHANT_PAYTO_BLOG \
   1007   --name "Blog Merchant" \
   1008   --password $(get_credential_pw bank/merchant-blog)
   1009 
   1010 sudo -u libeufin-bank libeufin-bank passwd merchant-gnunet $(get_credential_pw bank/merchant-gnunet) || true
   1011 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
   1012   --login merchant-gnunet --public \
   1013   --payto "$MERCHANT_PAYTO_GNUNET" \
   1014   --name "GNUnet Donations Merchant" \
   1015   --password $(get_credential_pw bank/merchant-gnunet)
   1016 
   1017 sudo -u libeufin-bank libeufin-bank passwd merchant-taler $(get_credential_pw bank/merchant-taler) || true
   1018 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
   1019   --login merchant-taler --public \
   1020   --payto "$MERCHANT_PAYTO_TALER" \
   1021   --name "Taler Donations Merchant" \
   1022   --password $(get_credential_pw bank/merchant-taler)
   1023 
   1024 sudo -u libeufin-bank libeufin-bank passwd merchant-tor $(get_credential_pw bank/merchant-tor) || true
   1025 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
   1026   --login merchant-tor --public \
   1027   --payto "$MERCHANT_PAYTO_TOR" \
   1028   --name "Tor Donations Merchant" \
   1029   --password $(get_credential_pw bank/merchant-tor)
   1030 
   1031 sudo -u libeufin-bank libeufin-bank passwd merchant-umami $(get_credential_pw bank/merchant-umami) || true
   1032 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
   1033   --login merchant-umami --public \
   1034   --payto "$MERCHANT_PAYTO_UMAMI" \
   1035   --name "Umami Merchant" \
   1036   --password $(get_credential_pw bank/merchant-umami)
   1037 
   1038 sudo -u libeufin-bank libeufin-bank passwd merchant-paivana $(get_credential_pw bank/merchant-paivana) || true
   1039 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
   1040   --login merchant-paivana --public \
   1041   --payto "$MERCHANT_PAYTO_PAIVANA" \
   1042   --name "Paivana Merchant" \
   1043   --password $(get_credential_pw bank/merchant-paivana)
   1044 
   1045 # Special bank account without a secure password
   1046 sudo -u libeufin-bank libeufin-bank passwd merchant-sandbox sandbox || true
   1047 taler-harness deployment provision-bank-account "${BANK_BASEURL}" \
   1048   --login merchant-sandbox --public \
   1049   --payto "$MERCHANT_PAYTO_SANDBOX" \
   1050   --name "Sandbox Merchant" \
   1051   --password sandbox
   1052 
   1053 # Set up exchange
   1054 
   1055 ##
   1056 ## Configure KYC if enabled
   1057 ##
   1058 
   1059 if [[ ${ENABLE_KYC:-0} = 1 ]]; then
   1060   # KYC config
   1061   if [[ ${KYC_DIALECT:-simple} = simple ]]; then
   1062     source /data/setup-kyc-simple.sh
   1063   elif [[ ${KYC_DIALECT:-simple} = tops ]]; then
   1064     source /data/setup-kyc-tops.sh
   1065   fi
   1066 else
   1067   rm -f /etc/taler-exchange/conf.d/sandcastle-kyc.conf
   1068 fi
   1069 
   1070 
   1071 if [[ ! -e /etc/taler-exchange/conf.d/sandcastle-$CURRENCY-coins.conf ]]; then
   1072   # Only create if necessary, as each [COIN-...] section
   1073   # has a unique name with a timestamp.
   1074   taler-harness deployment gen-coin-config \
   1075     --min-amount "${CURRENCY}:0.01" \
   1076     --max-amount "${CURRENCY}:100" \
   1077     --no-fees \
   1078     >"/etc/taler-exchange/conf.d/sandcastle-$CURRENCY-coins.conf"
   1079 else
   1080   # Exchange broke backwards compatibility, fix up existing config file.
   1081   sed -i 's/COIN-/COIN_/gI' "/etc/taler-exchange/conf.d/sandcastle-$CURRENCY-coins.conf"
   1082 fi
   1083 
   1084 taler-terms-generator -i /usr/share/taler-exchange/terms/exchange-tos-v0
   1085 taler-terms-generator -i /usr/share/taler-exchange/terms/exchange-pp-v0
   1086 
   1087 systemctl enable --now taler-exchange.target
   1088 
   1089 taler-harness deployment wait-taler-service taler-exchange $PROTO://$EXCHANGE_DOMAIN$PORT_SUFFIX/config
   1090 taler-harness deployment wait-endpoint $PROTO://$EXCHANGE_DOMAIN$PORT_SUFFIX/management/keys
   1091 
   1092 sudo -u taler-exchange-offline \
   1093   taler-exchange-offline \
   1094   -c /etc/taler-exchange/taler-exchange.conf \
   1095   download \
   1096   sign \
   1097   upload
   1098 
   1099 sudo -u taler-exchange-offline \
   1100   taler-exchange-offline \
   1101   enable-account "${EXCHANGE_PAYTO}" \
   1102   wire-fee now "$WIRE_METHOD" "${CURRENCY}":0 "${CURRENCY}":0 \
   1103   global-fee now "${CURRENCY}":0 "${CURRENCY}":0 "${CURRENCY}":0 1h 6a 0 \
   1104   upload
   1105 
   1106 systemctl enable --now taler-exchange-offline.timer
   1107 
   1108 function dup_exchange_opt() {
   1109   echo "$2 = $(taler-exchange-config -c /etc/taler-exchange/taler-exchange.conf -s $1 -o $2)"
   1110 }
   1111 
   1112 #
   1113 # Set up exchange auditor
   1114 #
   1115 
   1116 if [[ $ENABLE_AUDITOR = 1 ]]; then
   1117   systemctl enable --now taler-auditor.target
   1118 fi
   1119 
   1120 # Set up merchant backend
   1121 
   1122 MERCHANT_BASEURL=$PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/
   1123 
   1124 cat <<EOF >/etc/taler-merchant/conf.d/sandcastle-merchant-terms.conf
   1125 [merchant]
   1126 TERMS_ETAG = merchant-tos-demo-v0
   1127 TERMS_DIR = \${TALER_DATA_HOME}terms/
   1128 EOF
   1129 
   1130 taler-terms-generator -i /usr/share/taler-merchant/terms/merchant-tos-demo-v0.en.rst -o "$(taler-merchant-config -f -s merchant -o terms_dir)"
   1131 
   1132 systemctl enable --now taler-merchant.target
   1133 taler-harness deployment wait-taler-service taler-merchant ${MERCHANT_BASEURL}config
   1134 
   1135 function reset_merchant_pw() {
   1136   local pw status=0
   1137   pw=$(get_credential_pw merchant/$1)
   1138   sudo -u taler-merchant-httpd taler-merchant-passwd --instance "$1" "$pw" || status=$?
   1139   if [[ $status -eq 2 ]]; then
   1140     echo "Instance $1 does not exist" >&2
   1141     return 2
   1142   fi
   1143   if [[ $status -ne 0 ]]; then
   1144     echo "Failed to reset password for merchant instance $1" >&2
   1145     exit 1
   1146   fi
   1147 }
   1148 
   1149 # FIXME: Move this into a harness tool (that just reads a config file)?
   1150 
   1151 instance_missing=no
   1152 reset_merchant_pw admin || instance_missing=yes
   1153 if [[ $instance_missing = yes ]]; then
   1154   taler-harness deployment provision-merchant-instance \
   1155     ${MERCHANT_BASEURL} \
   1156     --management-token "secret-token:none" \
   1157     --instance-password $(get_credential_pw merchant/admin) \
   1158     --name Merchant \
   1159     --id admin \
   1160     --payto "$MERCHANT_PAYTO_ADMIN"
   1161 fi
   1162 
   1163 ADMIN_TOK=$(taler-harness merchant token ${MERCHANT_BASEURL} admin --password $(get_credential_pw merchant/admin))
   1164 
   1165 instance_missing=no
   1166 reset_merchant_pw pos || instance_missing=yes
   1167 if [[ $instance_missing = yes ]]; then
   1168   taler-harness deployment provision-merchant-instance \
   1169     ${MERCHANT_BASEURL} \
   1170     --management-token $ADMIN_TOK \
   1171     --instance-password $(get_credential_pw merchant/pos) \
   1172     --name "POS Merchant" \
   1173     --id pos \
   1174     --payto "$MERCHANT_PAYTO_POS"
   1175 fi
   1176 
   1177 instance_missing=no
   1178 reset_merchant_pw testing || instance_missing=yes
   1179 if [[ $instance_missing = yes ]]; then
   1180   taler-harness deployment provision-merchant-instance \
   1181     ${MERCHANT_BASEURL} \
   1182     --management-token $ADMIN_TOK \
   1183     --instance-password $(get_credential_pw merchant/testing) \
   1184     --name "Testing Merchant" \
   1185     --id testing \
   1186     --payto "$MERCHANT_PAYTO_TESTING"
   1187 fi
   1188 
   1189 # Reconcile the payout account even if an earlier run only created the instance.
   1190 reset_merchant_pw landing-samples || [[ $? -eq 2 ]]
   1191 taler-harness deployment provision-merchant-instance \
   1192   "${MERCHANT_BASEURL}" \
   1193   --management-token "$ADMIN_TOK" \
   1194   --instance-password "$(get_credential_pw merchant/landing-samples)" \
   1195   --name "Landing Samples Merchant" \
   1196   --id landing-samples \
   1197   --payto "$MERCHANT_PAYTO_LANDING_SAMPLES"
   1198 
   1199 instance_missing=no
   1200 reset_merchant_pw blog || instance_missing=yes
   1201 if [[ $instance_missing = yes ]]; then
   1202   taler-harness deployment provision-merchant-instance \
   1203     ${MERCHANT_BASEURL} \
   1204     --management-token $ADMIN_TOK \
   1205     --instance-password $(get_credential_pw merchant/blog) \
   1206     --name "Blog Merchant" \
   1207     --id blog \
   1208     --payto "$MERCHANT_PAYTO_BLOG"
   1209 fi
   1210 
   1211 instance_missing=no
   1212 reset_merchant_pw gnunet || instance_missing=yes
   1213 if [[ $instance_missing = yes ]]; then
   1214   taler-harness deployment provision-merchant-instance \
   1215     ${MERCHANT_BASEURL} \
   1216     --management-token $ADMIN_TOK \
   1217     --instance-password $(get_credential_pw merchant/gnunet) \
   1218     --name "GNUnet Merchant" \
   1219     --id gnunet \
   1220     --payto "$MERCHANT_PAYTO_GNUNET"
   1221 fi
   1222 
   1223 instance_missing=no
   1224 reset_merchant_pw taler || instance_missing=yes
   1225 if [[ $instance_missing = yes ]]; then
   1226   taler-harness deployment provision-merchant-instance \
   1227     ${MERCHANT_BASEURL} \
   1228     --management-token $ADMIN_TOK \
   1229     --instance-password $(get_credential_pw merchant/taler) \
   1230     --name "Taler Merchant" \
   1231     --id taler \
   1232     --payto "$MERCHANT_PAYTO_TALER"
   1233 fi
   1234 
   1235 instance_missing=no
   1236 reset_merchant_pw tor || instance_missing=yes
   1237 if [[ $instance_missing = yes ]]; then
   1238   taler-harness deployment provision-merchant-instance \
   1239     ${MERCHANT_BASEURL} \
   1240     --management-token $ADMIN_TOK \
   1241     --instance-password $(get_credential_pw merchant/tor) \
   1242     --name "Tor Merchant" \
   1243     --id tor \
   1244     --payto "$MERCHANT_PAYTO_TOR"
   1245 fi
   1246 
   1247 instance_missing=no
   1248 reset_merchant_pw umami || instance_missing=yes
   1249 if [[ $instance_missing = yes ]]; then
   1250   taler-harness deployment provision-merchant-instance \
   1251     ${MERCHANT_BASEURL} \
   1252     --management-token $ADMIN_TOK \
   1253     --instance-password $(get_credential_pw merchant/umami) \
   1254     --name "Umami Merchant" \
   1255     --id umami \
   1256     --payto "$MERCHANT_PAYTO_UMAMI"
   1257 fi
   1258 
   1259 instance_missing=no
   1260 reset_merchant_pw paivana || instance_missing=yes
   1261 if [[ $instance_missing = yes ]]; then
   1262   taler-harness deployment provision-merchant-instance \
   1263     ${MERCHANT_BASEURL} \
   1264     --management-token $ADMIN_TOK \
   1265     --instance-password $(get_credential_pw merchant/paivana) \
   1266     --name "Paivana Merchant" \
   1267     --id paivana \
   1268     --payto "$MERCHANT_PAYTO_PAIVANA"
   1269 fi
   1270 
   1271 # Special instance with fixed "sandbox" password
   1272 sudo -u taler-merchant-httpd taler-merchant-passwd --instance sandbox sandbox || true
   1273 taler-harness deployment provision-merchant-instance \
   1274   ${MERCHANT_BASEURL} \
   1275   --management-token $ADMIN_TOK \
   1276   --instance-password sandbox \
   1277   --name "sandbox merchant" \
   1278   --id sandbox \
   1279   --payto "$MERCHANT_PAYTO_SANDBOX"
   1280 
   1281 # token families needed by demo blog
   1282 
   1283 langs=(de en ar zh fr hi it ja ko pt pt_BR ru es sv tr uk)
   1284 duration_us=$((30 * 24 * 60 * 60 * 1000000)) # 30 days
   1285 validity_granularity_us=$((24 * 60 * 60 * 1000000)) # 1 day
   1286 # valid_before is intentionally omitted so token families remain valid forever.
   1287 
   1288 provision_token_family() {
   1289   local instance_url=$1
   1290   local access_token=$2
   1291   local slug=$3
   1292   local request_body=$4
   1293   local response_file
   1294   local response_status
   1295   local update_body
   1296 
   1297   response_file=$(mktemp)
   1298   response_status=$(curl -X POST "${instance_url}private/tokenfamilies" \
   1299     -H "Authorization: Bearer $access_token" \
   1300     -H "Content-Type: application/json" \
   1301     --data-raw "$request_body" \
   1302     --silent --show-error \
   1303     --output "$response_file" \
   1304     --write-out '%{http_code}')
   1305 
   1306   case "$response_status" in
   1307     204)
   1308       ;;
   1309     409)
   1310       response_status=$(curl "${instance_url}private/tokenfamilies/${slug}" \
   1311         -H "Authorization: Bearer $access_token" \
   1312         --silent --show-error \
   1313         --output "$response_file" \
   1314         --write-out '%{http_code}')
   1315       if [[ $response_status != 200 ]]; then
   1316         echo "Failed to retrieve existing token family ${slug} (HTTP $response_status)" >&2
   1317         cat "$response_file" >&2
   1318         rm -f "$response_file"
   1319         return 1
   1320       fi
   1321 
   1322       # PATCH only the mutable fields, preserving the original validity start.
   1323       # Omitting valid_before changes the validity end to "forever".
   1324       update_body=$(jq '
   1325         {name, description, description_i18n, valid_after}
   1326         + (if .extra_data == null then {} else {extra_data} end)
   1327       ' "$response_file")
   1328       response_status=$(curl -X PATCH "${instance_url}private/tokenfamilies/${slug}" \
   1329         -H "Authorization: Bearer $access_token" \
   1330         -H "Content-Type: application/json" \
   1331         --data-raw "$update_body" \
   1332         --silent --show-error \
   1333         --output "$response_file" \
   1334         --write-out '%{http_code}')
   1335       if [[ $response_status != 204 ]]; then
   1336         echo "Failed to update token family ${slug} (HTTP $response_status)" >&2
   1337         cat "$response_file" >&2
   1338         rm -f "$response_file"
   1339         return 1
   1340       fi
   1341       ;;
   1342     *)
   1343       echo "Failed to create token family ${slug} (HTTP $response_status)" >&2
   1344       cat "$response_file" >&2
   1345       rm -f "$response_file"
   1346       return 1
   1347       ;;
   1348   esac
   1349   rm -f "$response_file"
   1350 }
   1351 
   1352 # FIXME: Move this into a harness tool?
   1353 for lang in "${langs[@]}"; do
   1354   blog_token_family="{
   1355   \"kind\": \"subscription\",
   1356   \"slug\": \"blog_abo_${lang}\",
   1357   \"name\": \"One month of access (${lang})\",
   1358   \"description\": \"One month of access (${lang})\",
   1359   \"description_i18n\": {
   1360     \"de\": \"Ein monat lang Zugang zu den Artikeln\",
   1361     \"en\": \"One month of access to articles\",
   1362     \"fr\": \"Un mois d'accès aux articles\",
   1363     \"es\": \"Un mes de acceso a los artículos\"
   1364   },
   1365   \"duration\": { \"d_us\": ${duration_us} },
   1366   \"validity_granularity\": { \"d_us\": ${validity_granularity_us} }
   1367 }"
   1368   provision_token_family \
   1369     "${MERCHANT_BASEURL}instances/blog/" \
   1370     "secret-token:$(get_credential_pw merchant/blog)" \
   1371     "blog_abo_${lang}" \
   1372     "$blog_token_family"
   1373 done
   1374 
   1375 
   1376 # Set up the Paivana paywall.
   1377 # The paywall itself is configured entirely via merchant templates,
   1378 # see /etc/paivana/paivana.conf for the daemon configuration.
   1379 
   1380 PAIVANA_INSTANCE_URL=${MERCHANT_BASEURL}instances/paivana/
   1381 PAIVANA_TOKEN=secret-token:$(get_credential_pw merchant/paivana)
   1382 
   1383 # Subscription offered as an alternative to paying per visit.
   1384 # Reuses the validity settings of the blog subscriptions above.
   1385 paivana_token_family="{
   1386   \"kind\": \"subscription\",
   1387   \"slug\": \"paivana_abo\",
   1388   \"name\": \"One month of access\",
   1389   \"description\": \"One month of access to $PAIVANA_DESTINATION\",
   1390   \"duration\": { \"d_us\": ${duration_us} },
   1391   \"validity_granularity\": { \"d_us\": ${validity_granularity_us} }
   1392 }"
   1393 provision_token_family \
   1394   "$PAIVANA_INSTANCE_URL" \
   1395   "$PAIVANA_TOKEN" \
   1396   paivana_abo \
   1397   "$paivana_token_family"
   1398 
   1399 # paivana-httpd learns which URLs cost how much from the templates
   1400 # of its merchant instance.  A single template covering the whole
   1401 # site is enough here, as paivana-httpd runs with '-g'.
   1402 paivana_template=$(cat <<EOF
   1403 {
   1404   "template_id": "paivana",
   1405   "template_description": "Paywall for $PAIVANA_DESTINATION",
   1406   "template_contract": {
   1407     "template_type": "paivana",
   1408     "summary": "Access to $PAIVANA_DESTINATION",
   1409     "website_regex": ".*",
   1410     "max_pickup_duration": { "d_us": $((24 * 60 * 60 * 1000000)) },
   1411     "choices": [
   1412       {
   1413         "amount": "$CURRENCY:1",
   1414         "description": "Pay for one day of access"
   1415       },
   1416       {
   1417         "amount": "$CURRENCY:10",
   1418         "description": "Buy a one month subscription",
   1419         "outputs": [ { "type": "token", "token_family_slug": "paivana_abo" } ]
   1420       },
   1421       {
   1422         "amount": "$CURRENCY:0",
   1423         "description": "Use my subscription",
   1424         "inputs": [ { "type": "token", "token_family_slug": "paivana_abo" } ],
   1425         "outputs": [ { "type": "token", "token_family_slug": "paivana_abo" } ]
   1426       }
   1427     ]
   1428   }
   1429 }
   1430 EOF
   1431 )
   1432 
   1433 paivana_response=$(mktemp)
   1434 paivana_status=$(curl -X POST "${PAIVANA_INSTANCE_URL}private/templates" \
   1435   -H "Authorization: Bearer $PAIVANA_TOKEN" \
   1436   -H "Content-Type: application/json" \
   1437   --data-raw "$paivana_template" \
   1438   -s -o "$paivana_response" -w '%{http_code}')
   1439 
   1440 case "$paivana_status" in
   1441   204)
   1442     ;;
   1443   409)
   1444     # Template exists already, update it so that changes take effect.
   1445     paivana_status=$(curl -X PATCH "${PAIVANA_INSTANCE_URL}private/templates/paivana" \
   1446       -H "Authorization: Bearer $PAIVANA_TOKEN" \
   1447       -H "Content-Type: application/json" \
   1448       --data-raw "$(jq 'del(.template_id)' <<<"$paivana_template")" \
   1449       -s -o "$paivana_response" -w '%{http_code}')
   1450     if [[ $paivana_status != 204 ]]; then
   1451       echo "Failed to update Paivana template (HTTP $paivana_status)" >&2
   1452       cat "$paivana_response" >&2
   1453       exit 1
   1454     fi
   1455     ;;
   1456   *)
   1457     echo "Failed to create Paivana template (HTTP $paivana_status)" >&2
   1458     cat "$paivana_response" >&2
   1459     exit 1
   1460     ;;
   1461 esac
   1462 rm -f "$paivana_response"
   1463 
   1464 systemctl enable --now paivana-httpd.socket
   1465 systemctl enable --now paivana-httpd.service
   1466 
   1467 
   1468 # Set up Donau
   1469 
   1470 cat <<EOF >/etc/donau/conf.d/sandcastle.conf
   1471 [donau]
   1472 CURRENCY = $CURRENCY
   1473 LEGAL_DOMAIN = Gnuland
   1474 EXPIRE_LEGAL_YEARS = 3
   1475 # We don't do the token yet, as the merchant doesn't support
   1476 # authenticating with donau.
   1477 # ADMIN_BEARER_TOKEN = secret-token:secret
   1478 EOF
   1479 
   1480 donau-dbconfig
   1481 
   1482 if [[ ! -e /etc/donau/conf.d/sandcastle-$CURRENCY-units.conf ]]; then
   1483   # Only create if necessary
   1484   taler-harness deployment gen-doco-config \
   1485     --min-amount "${CURRENCY}:0.01" \
   1486     --max-amount "${CURRENCY}:100" \
   1487     >"/etc/donau/conf.d/sandcastle-$CURRENCY-units.conf"
   1488 fi
   1489 
   1490 systemctl enable --now donau.target
   1491 
   1492 DONAU_BASE_URL=$PROTO://$DONAU_DOMAIN$PORT_SUFFIX/
   1493 
   1494 taler-harness deployment wait-taler-service donau ${DONAU_BASE_URL}config
   1495 
   1496 if [[ ${ENABLE_TALDIR:-0} == 1 ]]; then
   1497   # Mailbox and Directory
   1498   mkdir -p /etc/taler-directory
   1499   cp /usr/share/taler-directory/taldir.conf.example /etc/taler-directory/taler-directory.conf
   1500   taler-directory-dbconfig
   1501   systemctl enable --now taler-directory.service
   1502 fi
   1503 
   1504 if [[ ${ENABLE_MAILBOX:-0} == 1 ]]; then
   1505   mkdir -p /etc/taler-mailbox
   1506   cp /usr/share/taler-mailbox/mailbox.conf.example /etc/taler-mailbox/taler-mailbox.conf
   1507   sed -i 's/localhost:11000/localhost:12000/' /etc/taler-mailbox/taler-mailbox.conf
   1508   taler-mailbox-dbconfig
   1509   systemctl enable --now taler-mailbox.service
   1510 fi
   1511 
   1512 GNUNET_TOK=$(taler-harness merchant token ${MERCHANT_BASEURL}instances/gnunet/ gnunet --password $(get_credential_pw merchant/gnunet))
   1513 taler-harness deployment provision-merchant-donau \
   1514   --merchant-auth-token $GNUNET_TOK \
   1515   --merchant-base-url $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/gnunet/ \
   1516   --donau-base-url $DONAU_BASE_URL \
   1517   --donau-auth-token secret-token:secret \
   1518   --currency $CURRENCY
   1519 
   1520 TALER_TOK=$(taler-harness merchant token ${MERCHANT_BASEURL}instances/taler/ taler --password $(get_credential_pw merchant/taler))
   1521 taler-harness deployment provision-merchant-donau \
   1522   --merchant-auth-token $TALER_TOK \
   1523   --merchant-base-url $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/taler/ \
   1524   --donau-base-url $DONAU_BASE_URL \
   1525   --donau-auth-token secret-token:secret \
   1526   --currency $CURRENCY
   1527 
   1528 TOR_TOK=$(taler-harness merchant token ${MERCHANT_BASEURL}instances/tor/ tor --password $(get_credential_pw merchant/tor))
   1529 taler-harness deployment provision-merchant-donau \
   1530   --merchant-auth-token $TOR_TOK \
   1531   --merchant-base-url $PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/tor/ \
   1532   --donau-base-url $DONAU_BASE_URL \
   1533   --donau-auth-token secret-token:secret \
   1534   --currency $CURRENCY
   1535 
   1536 UMAMI_TOK=$(taler-harness merchant token ${MERCHANT_BASEURL}instances/umami/ umami --password $(get_credential_pw merchant/umami))
   1537 
   1538 # Now we set up the taler-merchant-demos
   1539 
   1540 systemctl enable --now taler-demo-landing
   1541 systemctl enable --now taler-demo-blog
   1542 systemctl enable --now taler-demo-donations
   1543 
   1544 # Turnstile (drupal/php)
   1545 if [[ ${ENABLE_TURNSTILE:-1} = 1 ]]; then
   1546 
   1547 systemctl enable --now php8.4-fpm
   1548 
   1549 DRUPAL_DB_PW=$(get_credential_pw db/drupal)
   1550 DRUPAL_ADMIN_PW=$(get_credential_pw drupal/admin)
   1551 
   1552 sudo -i -u postgres psql postgres -c "CREATE ROLE drupal WITH login;" || true
   1553 sudo -i -u postgres psql postgres -c "ALTER ROLE drupal password '$DRUPAL_DB_PW';"
   1554 sudo -u postgres createdb drupal --owner=drupal || true
   1555 
   1556 # Needed by PHP's composer
   1557 export HOME=/root
   1558 
   1559 # FIXME: Would probably be better to checkout output of
   1560 # drush status --fields=bootstrap --format=string
   1561 
   1562 cd /talerdata/
   1563 if [[ ! -e /talerdata/sandcastle-drupal ]]; then
   1564   composer create-project drupal/recommended-project:^10 sandcastle-drupal
   1565   cd /talerdata/sandcastle-drupal
   1566   composer require drush/drush
   1567   # This can take a ridiculous amount of time!
   1568   COMPOSER_PROCESS_TIMEOUT=0 composer exec -- drush site-install demo_umami --account-name=admin --account-pass=$DRUPAL_ADMIN_PW --account-mail=admin@localhost --db-url=pgsql://drupal:$DRUPAL_DB_PW@localhost/drupal --site-name=SandcastleUmami --yes
   1569 fi
   1570 
   1571 chown -R www-data:www-data /talerdata/sandcastle-drupal/
   1572 
   1573 ln -sf /opt/turnstile /talerdata/sandcastle-drupal/web/modules/taler_turnstile
   1574 
   1575 snip=$(cat <<'EOF'
   1576 $settings['reverse_proxy'] = TRUE;
   1577 $settings['reverse_proxy_addresses'] = ['127.0.0.1', '10.0.0.0/8'];
   1578 $settings['trusted_host_patterns'] = ['.*'];
   1579 EOF
   1580 )
   1581 
   1582 update_config_block /talerdata/sandcastle-drupal/web/sites/default/settings.php SANDCASTLE "$snip"
   1583 
   1584 cd /talerdata/sandcastle-drupal
   1585 composer exec -- drush upwd admin $DRUPAL_ADMIN_PW
   1586 composer exec -- drush en taler_turnstile
   1587 composer exec -- drush config:set taler_turnstile.settings access_token "$UMAMI_TOK" --yes
   1588 composer exec -- drush config:set taler_turnstile.settings payment_backend_url "$PROTO://$MERCHANT_DOMAIN$PORT_SUFFIX/instances/umami/" --yes
   1589 composer exec -- drush config:set --input-format=yaml taler_turnstile.settings enabled_content_types '["article", "recipe"]' --yes
   1590 
   1591 snip=$(cat <<'EOF' | sed "s/@CURRENCY@/$CURRENCY/"
   1592 $storage = \Drupal::entityTypeManager()->getStorage('taler_turnstile_price_category');
   1593 $e = $storage->create(['id'=>"normal", 'label'=>'Normal', 'description' => "Normal Article Price", 'prices' => ['%none%' => ['@CURRENCY@' => '0.3']]]);
   1594 try {
   1595   $e->save();
   1596 } catch (Exception $ex) {
   1597   echo $ex->getMessage();
   1598   echo "\n";
   1599 }
   1600 EOF
   1601 )
   1602 composer exec -- drush php:eval "$snip"
   1603 
   1604 snip=$(cat <<'EOF'
   1605 $prcat_storage = \Drupal::entityTypeManager()->getStorage('taler_turnstile_price_category');
   1606 $node_storage = \Drupal::entityTypeManager()->getStorage('node');
   1607 $prcat = $prcat_storage->load('normal');
   1608 $nodes = $node_storage->loadByProperties(['type'=> ['article', 'recipe']]);
   1609 foreach ($nodes as $k => $node) {
   1610   echo 'updating node ' . $k . "\n";
   1611   if (!$node->hasField('field_taler_turnstile_prcat')) {
   1612     echo 'prcat missing' . "\n";
   1613     continue;
   1614   }
   1615   $node->set('field_taler_turnstile_prcat', $prcat);
   1616   $node->save();
   1617 }
   1618 EOF
   1619 )
   1620 composer exec -- drush php:eval "$snip"
   1621 
   1622 composer exec -- drush cr
   1623 
   1624 fi
   1625 
   1626 cd /
   1627 
   1628 write_sandcastle_status
   1629 
   1630 # FIXME: Maybe do some taler-wallet-cli test?
   1631 # FIXME: How do we report errors occurring during the setup script?