Files
mtusi2026lab7/scripts/init-env.sh
T

107 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Idempotently creates non-secret .env and stateful infra-only bootstrap secrets
# under ./secrets/infra/. Never prints secret values and NEVER creates
# application credentials (DB/Redis/S3/Broker/Metrics app creds live ONLY in
# OpenBao Raft KV v2 and backend auth stores).
set -euo pipefail
cd "$(dirname "$0")/.."
if [ ! -f .env ]; then
cp .env.example .env
chmod 0644 .env
echo "created .env (non-secret topology config only)"
else
echo "kept .env"
fi
mkdir -p secrets secrets/infra
chmod 0700 secrets secrets/infra
# Host isolation is enforced by 0700 on ./secrets and ./secrets/infra; individual
# secret files are 0644 so heterogeneous non-root container UIDs (70, 99, 472,
# 999, 1000, 10001, 65534) can read only the specific secret mounted to them.
write_infra_secret() {
local name="$1"
local value="$2"
local path="secrets/infra/${name}"
if [ -s "$path" ]; then
echo "kept ${path}"
return 0
fi
local tmp="${path}.tmp.$$"
(umask 133; printf '%s' "$value" > "$tmp")
chmod 0644 "$tmp"
mv -f "$tmp" "$path"
echo "created ${path}"
}
bcrypt_hash() {
local password="$1"
printf '%s' "$password" | python3 -c '
import sys, bcrypt
pw = sys.stdin.read().encode("utf-8")
print(bcrypt.hashpw(pw, bcrypt.gensalt(rounds=10)).decode("ascii"), end="")
'
}
write_infra_secret postgres_superuser_password "$(openssl rand -hex 24)"
write_infra_secret minio_root_user "root-$(openssl rand -hex 6)"
write_infra_secret minio_root_password "$(openssl rand -hex 24)"
write_infra_secret redis_admin_password "$(openssl rand -hex 24)"
write_infra_secret redis_health_password "$(openssl rand -hex 24)"
write_infra_secret grafana_admin_password "$(openssl rand -hex 24)"
write_infra_secret grafana_prometheus_password "$(openssl rand -hex 24)"
write_infra_secret otel_prometheus_password "$(openssl rand -hex 24)"
write_infra_secret prometheus_self_password "$(openssl rand -hex 24)"
write_infra_secret prometheus_operator_password "$(openssl rand -hex 24)"
write_infra_secret haproxy_stats_password "$(openssl rand -hex 24)"
# Render Prometheus web.yml (HTTPS + mTLS + Basic Auth bcrypt hashes) from template.
if [ -s secrets/infra/prometheus_web.yml ]; then
echo "kept secrets/infra/prometheus_web.yml"
else
op_hash="$(bcrypt_hash "$(cat secrets/infra/prometheus_operator_password)")"
gr_hash="$(bcrypt_hash "$(cat secrets/infra/grafana_prometheus_password)")"
ot_hash="$(bcrypt_hash "$(cat secrets/infra/otel_prometheus_password)")"
sf_hash="$(bcrypt_hash "$(cat secrets/infra/prometheus_self_password)")"
tmp="secrets/infra/prometheus_web.yml.tmp.$$"
(
umask 133
python3 - "$op_hash" "$gr_hash" "$ot_hash" "$sf_hash" <<'PY' > "$tmp"
import sys
from pathlib import Path
op_h, gr_h, ot_h, sf_h = sys.argv[1:5]
tpl = Path("infra/prometheus/web.yml").read_text(encoding="utf-8")
out = (
tpl.replace("__OPERATOR_BCRYPT__", op_h)
.replace("__GRAFANA_BCRYPT__", gr_h)
.replace("__OTEL_BCRYPT__", ot_h)
.replace("__SELF_BCRYPT__", sf_h)
)
sys.stdout.write(out)
PY
)
chmod 0644 "$tmp"
mv -f "$tmp" secrets/infra/prometheus_web.yml
echo "created secrets/infra/prometheus_web.yml"
fi
# Render HAProxy stats Basic Auth userlist fragment (SHA-512 crypt hash).
if [ -s secrets/infra/haproxy_stats_auth.cfg ]; then
echo "kept secrets/infra/haproxy_stats_auth.cfg"
else
stats_hash="$(openssl passwd -6 -stdin < secrets/infra/haproxy_stats_password)"
tmp="secrets/infra/haproxy_stats_auth.cfg.tmp.$$"
(
umask 133
{
printf 'userlist ops_stats\n'
printf ' user operator password %s\n' "$stats_hash"
} > "$tmp"
)
chmod 0644 "$tmp"
mv -f "$tmp" secrets/infra/haproxy_stats_auth.cfg
echo "created secrets/infra/haproxy_stats_auth.cfg"
fi