Files
mtusi2026lab5/scripts/bootstrap-secrets.sh
T
2026-11-20 20:11:39 +03:00

56 lines
1.9 KiB
Bash

#!/bin/sh
# Create local Swarm secret files under secrets/ (gitignored, never printed).
# Existing files are never overwritten, so rerunning the script is safe.
# Usage: sh scripts/bootstrap-secrets.sh
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
dir="$root/secrets"
# Files are created with mode 600: group and other have no access.
umask 077
mkdir -p "$dir"
random_hex() {
head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n'
}
# 10 bytes = 20 hex chars: MinIO access keys are limited to 20 characters.
random_access_key() {
head -c 10 /dev/urandom | od -An -tx1 | tr -d ' \n'
}
# write <name> <value>: keeps the current file if it already has content.
write() {
name=$1
value=$2
if [ -s "$dir/$name" ]; then
printf 'keep secrets/%s\n' "$name"
return 0
fi
printf '%s\n' "$value" > "$dir/$name"
printf 'create secrets/%s\n' "$name"
}
write postgres_password "$(random_hex)"
write cursor_secret "$(random_hex)"
write cache_url 'redis://valkey:6379/0'
# The password is hex, so the URL needs no percent-encoding.
write database_url "postgresql+asyncpg://messenger:$(cat "$dir/postgres_password")@postgres:5432/messenger"
# MinIO: root credentials only for minio-init; API uses a separate restricted
# pair. The access key is 20 hex chars (MinIO limit), the secret is 64 hex chars.
write s3_root_user 'messenger-root'
write s3_root_password "$(random_hex)"
write s3_access_key_id "$(random_access_key)"
write s3_secret_access_key "$(random_hex)"
# RabbitMQ: default user создаётся при первом старте из этих файлов; URL
# использует URL-safe hex-пароль (percent-encoding не нужен).
write rabbitmq_user 'messenger'
write rabbitmq_password "$(random_hex)"
write broker_url "amqp://messenger:$(cat "$dir/rabbitmq_password")@rabbitmq:5672/%2F"
printf 'done: %s\n' "$dir"
printf 'next: sh scripts/deploy.sh\n'