#!/usr/bin/env bash # RasterizedDB Enterprise: add a customer to the per-user-shard cluster. The # gateway's configuration API creates the customer's database and login role # on the chosen shard and pins the login there. # Needs release 1.1.0 or later. # # RDB_CUSTOMER=acme RDB_CUSTOMER_SHARD=1 RDB_CUSTOMER_PASSWORD='…' ./add-customer.sh set -euo pipefail RDB_WORK="${RDB_WORK:-./per-user-cluster}" # holds api.token RDB_API="${RDB_API:-http://127.0.0.1:6480}" RDB_GATEWAY_PORT="${RDB_GATEWAY_PORT:-6432}" RDB_CUSTOMER="${RDB_CUSTOMER:-}" # required: the customer's database RDB_CUSTOMER_ROLE="${RDB_CUSTOMER_ROLE:-${RDB_CUSTOMER}_app}" # its login role RDB_CUSTOMER_SHARD="${RDB_CUSTOMER_SHARD:-}" # required: the shard id it lives on die() { echo "error: $*" >&2; exit 1; } command -v curl >/dev/null 2>&1 || die "curl is needed" [ -f "$RDB_WORK/api.token" ] || die "no $RDB_WORK/api.token; run setup.sh first, or set RDB_WORK" [ -n "$RDB_CUSTOMER" ] || die "set RDB_CUSTOMER to the customer's database name" [ -n "$RDB_CUSTOMER_SHARD" ] || die "set RDB_CUSTOMER_SHARD to the shard id (0, 1 or 2)" : "${RDB_CUSTOMER_PASSWORD:?set RDB_CUSTOMER_PASSWORD to the password for the login role}" for v in "$RDB_CUSTOMER" "$RDB_CUSTOMER_ROLE"; do [[ "$v" =~ ^[A-Za-z][A-Za-z0-9_]{0,62}$ ]] || die "$v: start with a letter, then letters, digits and _ (63 at most)" done [[ "$RDB_CUSTOMER_SHARD" =~ ^[0-9]+$ ]] || die "RDB_CUSTOMER_SHARD must be a number" case "$RDB_CUSTOMER_PASSWORD" in *'"'* | *'\'*) die "the password may not contain a double quote or a backslash" ;; esac # The token and the password go through files and standard input, never the # command line. header="$(mktemp)" trap 'rm -f "$header"' EXIT chmod 600 "$header" printf 'Authorization: Bearer %s\n' "$(cat "$RDB_WORK/api.token")" > "$header" printf '{"database": "%s", "role": "%s", "password": "%s", "shard": %s}' \ "$RDB_CUSTOMER" "$RDB_CUSTOMER_ROLE" "$RDB_CUSTOMER_PASSWORD" "$RDB_CUSTOMER_SHARD" \ | curl --fail-with-body -sS -H @"$header" -H 'Content-Type: application/json' --data-binary @- "$RDB_API/v1/customers" echo curl --fail-with-body -sS -H @"$header" "$RDB_API/v1/routes" echo echo echo "The customer connects through the gateway, and lands on shard $RDB_CUSTOMER_SHARD:" echo " psql \"postgresql://$RDB_CUSTOMER_ROLE@127.0.0.1:$RDB_GATEWAY_PORT/$RDB_CUSTOMER?sslmode=disable\""