50 lines
1.5 KiB
Bash
50 lines
1.5 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)"
|
|
|
|
printf 'done: %s\n' "$dir"
|
|
printf 'next: sh scripts/deploy.sh\n'
|