Files
mtusi2026lab6/scripts/init-secrets.sh
T

52 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
# Creates local Docker secrets for a synthetic lab stand.
# Existing files are never overwritten; values are never printed.
set -eu
dir="$(cd "$(dirname "$0")/.." && pwd)/infra/secrets"
mkdir -p "$dir"
# Host access is limited by the directory mode; the files themselves must stay
# readable by the non-root users inside containers (bind-mounted secrets).
chmod 700 "$dir" 2>/dev/null || true
random() {
od -An -tx1 -N24 /dev/urandom | tr -d ' \n'
}
write_new() {
if [ -f "$dir/$1" ]; then
echo "keep infra/secrets/$1"
else
printf '%s' "$2" > "$dir/$1"
chmod 644 "$dir/$1" 2>/dev/null || true
echo "create infra/secrets/$1"
fi
}
if [ ! -f "$dir/postgres_password.txt" ]; then
write_new postgres_password.txt "$(random)"
fi
pg_password="$(cat "$dir/postgres_password.txt")"
write_new database_url.txt "postgresql://messenger:${pg_password}@db:5432/messenger"
if [ ! -f "$dir/valkey_password.txt" ]; then
write_new valkey_password.txt "$(random)"
fi
valkey_password="$(cat "$dir/valkey_password.txt")"
write_new cache_url.txt "redis://:${valkey_password}@valkey:6379/0"
# Lab4: S3 (SeaweedFS). Admin credentials are used only by the s3-init
# bootstrap; the API gets its own key pair restricted to one bucket.
write_new s3_admin_access_key_id.txt "admin$(random | cut -c1-15)"
write_new s3_admin_secret_access_key.txt "$(random)$(random)"
write_new s3_access_key_id.txt "api$(random | cut -c1-17)"
write_new s3_secret_access_key.txt "$(random)$(random)"
# Lab5: RabbitMQ uses a separate local password. The URL is a Docker secret;
# no credentials appear in compose.yaml or the report.
if [ ! -f "$dir/broker_password.txt" ]; then
write_new broker_password.txt "$(random)"
fi
broker_password="$(cat "$dir/broker_password.txt")"
write_new broker_url.txt "amqp://messenger:${broker_password}@broker:5672/"