who_need_help/scripts/ensure-local-load-env.sh

83 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
set -eu
ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
TEMPLATE="$ROOT/.env.load.example"
ENV_FILE="$ROOT/.env.load"
for command in openssl perl; do
if ! command -v "$command" >/dev/null 2>&1; then
echo "Required command is unavailable: $command" >&2
exit 1
fi
done
if [ -f "$ENV_FILE" ]; then
chmod 600 "$ENV_FILE"
if grep -q '^LOAD_FIXTURE_PASSWORD=' "$ENV_FILE"; then
echo ".env.load already exists; no secret or experiment input was changed."
exit 0
fi
umask 077
load_fixture_password=$(openssl rand -hex 24)
{
printf '\n# Added by the authenticated-load profile upgrade.\n'
printf 'LOAD_AUTH_VUS=8\n'
printf 'LOAD_AUTH_WS_TIMEOUT_MS=5000\n'
printf 'LOAD_AUTH_THINK_SECONDS=0.1\n'
printf 'LOAD_FIXTURE_PASSWORD=%s\n' "$load_fixture_password"
} >>"$ENV_FILE"
chmod 600 "$ENV_FILE"
unset load_fixture_password
echo "Added authenticated-load inputs and a random fixture password to ignored .env.load."
exit 0
fi
if [ ! -f "$TEMPLATE" ]; then
echo "Missing tracked template: $TEMPLATE" >&2
exit 1
fi
umask 077
postgres_password=$(openssl rand -hex 32)
secret_key_base=$(openssl rand -hex 64)
handover_secret=$(openssl rand -hex 64)
release_cookie=$(openssl rand -hex 64)
metrics_token=$(openssl rand -hex 32)
load_fixture_password=$(openssl rand -hex 24)
database_url="ecto://wnh_load:${postgres_password}@db/who_need_help_load"
temporary=$(mktemp "${ENV_FILE}.XXXXXX")
trap 'rm -f "$temporary"' EXIT HUP INT TERM
POSTGRES_PASSWORD_VALUE=$postgres_password \
DATABASE_URL_VALUE=$database_url \
SECRET_KEY_BASE_VALUE=$secret_key_base \
HANDOVER_SECRET_VALUE=$handover_secret \
RELEASE_COOKIE_VALUE=$release_cookie \
METRICS_TOKEN_VALUE=$metrics_token \
LOAD_FIXTURE_PASSWORD_VALUE=$load_fixture_password \
perl -0pe '
s/GENERATE_POSTGRES_PASSWORD/$ENV{POSTGRES_PASSWORD_VALUE}/g;
s/GENERATE_DATABASE_URL/$ENV{DATABASE_URL_VALUE}/g;
s/GENERATE_SECRET_KEY_BASE/$ENV{SECRET_KEY_BASE_VALUE}/g;
s/GENERATE_HANDOVER_SECRET/$ENV{HANDOVER_SECRET_VALUE}/g;
s/GENERATE_RELEASE_COOKIE/$ENV{RELEASE_COOKIE_VALUE}/g;
s/GENERATE_METRICS_TOKEN/$ENV{METRICS_TOKEN_VALUE}/g;
s/GENERATE_LOAD_FIXTURE_PASSWORD/$ENV{LOAD_FIXTURE_PASSWORD_VALUE}/g;
' "$TEMPLATE" >"$temporary"
if grep -Eq '^[A-Z0-9_]+=GENERATE_' "$temporary"; then
echo "A secret marker was not replaced; refusing to publish .env.load." >&2
exit 1
fi
chmod 600 "$temporary"
mv "$temporary" "$ENV_FILE"
trap - EXIT HUP INT TERM
unset postgres_password secret_key_base handover_secret release_cookie metrics_token \
load_fixture_password database_url
echo "Generated independent load-profile secrets in ignored .env.load."