Files
mtusi2026lab3/scripts/bootstrap-secrets.sh
T

28 lines
829 B
Bash
Executable File

#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
SECRETS_DIR="$ROOT/secrets"
PASSWORD_FILE="$SECRETS_DIR/postgres_password"
DATABASE_URL_FILE="$SECRETS_DIR/database_url"
if [ -e "$PASSWORD_FILE" ] || [ -e "$DATABASE_URL_FILE" ]; then
printf '%s\n' 'Secret files already exist; refusing to overwrite them.' >&2
exit 1
fi
command -v openssl >/dev/null 2>&1 || {
printf '%s\n' 'openssl is required to generate local credentials.' >&2
exit 1
}
mkdir -p "$SECRETS_DIR"
chmod 700 "$SECRETS_DIR"
umask 077
password=$(openssl rand -hex 32)
printf '%s' "$password" > "$PASSWORD_FILE"
printf 'postgresql://messenger:%s@postgres:5432/messenger' "$password" > "$DATABASE_URL_FILE"
chmod 644 "$PASSWORD_FILE" "$DATABASE_URL_FILE"
unset password
printf '%s\n' 'Local PostgreSQL secret files created.'