Files
mtusi2026lab7/scripts/bootstrap-secrets.sh
T
kokorindenis a884c07867
Skeleton contracts / contracts (pull_request) Canceled after 0s
solution
2026-11-27 16:42:18 +03:00

75 lines
2.5 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# Генерирует локальные пароли/ключи стенда. Ничего не печатает и не
# перезаписывает: файлы лежат в secrets/ (gitignored) с mode 644 (каталог 700).
set -eu
export MSYS_NO_PATHCONV=1
export MSYS2_ARG_CONV_EXCL="*"
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"
DIR="${SECRETS_DIR:-secrets}"
mkdir -p "$DIR"
umask 077
rand_hex() { openssl rand -hex "${1:-24}"; }
rand_user() { printf 'lab7-%s-%s' "$1" "$(openssl rand -hex 4)"; }
put() {
path=$1
value=$2
if [ -f "$path" ]; then
echo "keep $path"
else
printf '%s' "$value" > "$path"
chmod 644 "$path"
echo "create $path"
fi
}
put "$DIR/postgres_password" "$(rand_hex 24)"
PG_PW=$(tr -d '\r\n' < "$DIR/postgres_password")
put "$DIR/migrate_database_url" "postgresql+asyncpg://messenger:$PG_PW@postgres:5432/messenger"
put "$DIR/db_api_password" "$(rand_hex 24)"
put "$DIR/db_worker_password" "$(rand_hex 24)"
put "$DIR/db_relay_password" "$(rand_hex 24)"
put "$DIR/rabbitmq_root_user" "lab7-root"
put "$DIR/rabbitmq_root_password" "$(rand_hex 24)"
put "$DIR/rabbitmq_api_user" "messenger-api"
put "$DIR/rabbitmq_api_password" "$(rand_hex 24)"
put "$DIR/rabbitmq_worker_user" "messenger-worker"
put "$DIR/rabbitmq_worker_password" "$(rand_hex 24)"
put "$DIR/minio_root_user" "lab7-minio-root"
put "$DIR/minio_root_password" "$(rand_hex 24)"
put "$DIR/s3_api_access_key" "$(rand_user s3api)"
put "$DIR/s3_api_secret_key" "$(rand_hex 24)"
put "$DIR/s3_worker_access_key" "$(rand_user s3worker)"
put "$DIR/s3_worker_secret_key" "$(rand_hex 24)"
put "$DIR/cursor_secret" "$(rand_hex 32)"
put "$DIR/grafana_admin_password" "$(rand_hex 16)"
mkdir -p "$DIR/bao_runtime"
for role in api worker relay; do
# Placeholders нужны, чтобы docker compose config не падал до bootstrap;
# openbao_bootstrap.py перезапишет их настоящими role_id/secret_id.
[ -f "$DIR/bao_runtime/${role}_role_id" ] || : > "$DIR/bao_runtime/${role}_role_id"
[ -f "$DIR/bao_runtime/${role}_secret_id" ] || : > "$DIR/bao_runtime/${role}_secret_id"
done
chmod 755 "$DIR/bao_runtime"
chmod 644 "$DIR/bao_runtime"/* 2>/dev/null || true
if [ ! -f .env ]; then
cat > .env <<EOF
COMPOSE_PROJECT_NAME=lab7
PUBLIC_BASE_URL=https://localhost:8443
CA_FILE=$ROOT/certs/ca.crt
SESSION_TTL_SECONDS=3600
BAO_TOKEN_TTL_SECONDS=300
BAO_TOKEN_MAX_TTL_SECONDS=900
EOF
chmod 600 .env
echo "create .env"
else
echo "keep .env"
fi
echo "Local secrets are ready. Values are not printed; never commit $DIR or .env."