#!/bin/sh # Creates the local credentials of the compose stand in ./secrets, which git # ignores. Existing secrets are kept: PostgreSQL applies its password only # when the volume is first initialised, so a new password would lock the API # out of an existing database. To start over: make destroy, then delete # ./secrets and run this again. set -eu cd "$(dirname "$0")/.." files="postgres_password database_url valkey_auth.conf session_store_url" present=0 for f in $files; do if [ -e "secrets/$f" ]; then present=$((present + 1)) fi done if [ "$present" -ne 0 ] && [ "$present" -ne 4 ]; then echo "./secrets is incomplete: delete it (and run make destroy if the stand holds data), then run again" >&2 exit 1 fi # 24 random bytes as hex: URL-safe without escaping. random() { od -An -tx1 -N24 /dev/urandom | tr -d ' \n' } # The directory is private; the files stay readable for the unprivileged # users inside the containers, which see them through bind mounts. umask 022 mkdir -p secrets chmod 700 secrets if [ "$present" -eq 0 ]; then pg=$(random) kv=$(random) printf '%s\n' "$pg" > secrets/postgres_password printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$pg" > secrets/database_url printf 'requirepass %s\n' "$kv" > secrets/valkey_auth.conf printf 'redis://:%s@valkey:6379/0\n' "$kv" > secrets/session_store_url fi # Read just the bucket setting, never execute the local .env file as a script. bucket=${S3_BUCKET:-} if [ -z "$bucket" ] && [ -f .env ]; then bucket=$(sed -n 's/^S3_BUCKET=//p' .env | tail -n 1 | tr -d '\r') fi bucket=${bucket:-messenger-attachments} case "$bucket" in *[!a-z0-9.-]*|.*|*.|-*|*-|*..*|*.-*|*-.*) echo "S3_BUCKET must be a lowercase S3 bucket name" >&2; exit 1 ;; esac if [ "${#bucket}" -lt 3 ] || [ "${#bucket}" -gt 63 ]; then echo "S3_BUCKET must contain 3 to 63 characters" >&2; exit 1 fi s3files="s3_admin_access_key_id s3_admin_secret_access_key s3_access_key_id s3_secret_access_key s3_reconcile_access_key_id s3_reconcile_secret_access_key s3_identities.json" s3present=0 for f in $s3files; do if [ -s "secrets/$f" ]; then s3present=$((s3present + 1)); fi done if [ "$s3present" -eq 7 ]; then if ! grep -Fq "\"Read:$bucket\"" secrets/s3_identities.json; then echo "existing S3 identities use another bucket; keep S3_BUCKET consistent with the retained volume" >&2 exit 1 fi echo "existing PostgreSQL, Valkey and S3 secrets kept" exit 0 fi if [ "$s3present" -ne 0 ]; then echo "S3 secrets are incomplete; restore the s3_* secret files together; PostgreSQL and Valkey secrets were kept" >&2 exit 1 fi admin_key=$(random) admin_secret=$(random) api_key=$(random) api_secret=$(random) recon_key=$(random) recon_secret=$(random) printf '%s\n' "$admin_key" > secrets/s3_admin_access_key_id printf '%s\n' "$admin_secret" > secrets/s3_admin_secret_access_key printf '%s\n' "$api_key" > secrets/s3_access_key_id printf '%s\n' "$api_secret" > secrets/s3_secret_access_key printf '%s\n' "$recon_key" > secrets/s3_reconcile_access_key_id printf '%s\n' "$recon_secret" > secrets/s3_reconcile_secret_access_key # The server loads all identities before listening. There is no anonymous # identity, and no post-start allow-all provisioning phase. cat > secrets/s3_identities.json <