lab7: PKI, mTLS infra, OpenBao non-dev, hardening, lifecycle scripts

This commit is contained in:
2026-11-26 16:47:23 +03:00
parent cda6e27fb8
commit 16accda962
45 changed files with 3593 additions and 660 deletions
+85 -56
View File
@@ -1,77 +1,106 @@
#!/bin/sh
# Идемпотентно создаёт .env и secrets/* для локального стенда. Значения не печатает.
set -eu
#!/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
(
umask 077
pg=$(openssl rand -hex 24)
mt=$(openssl rand -hex 16)
sed \
-e "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=${pg}|" \
-e "s|^DATABASE_URL=.*|DATABASE_URL=postgresql://messenger:${pg}@postgres:5432/messenger|" \
-e "s|^METRICS_TOKEN=.*|METRICS_TOKEN=${mt}|" \
.env.example > .env
)
echo "created .env"
cp .env.example .env
chmod 0644 .env
echo "created .env (non-secret topology config only)"
else
echo "kept .env"
fi
mkdir -p secrets
chmod 700 secrets
mkdir -p secrets secrets/infra
chmod 0700 secrets secrets/infra
write_secret() {
name=$1
value=$2
if [ -s "secrets/$name" ]; then
echo "kept secrets/$name"
return
# 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
(umask 133; printf '%s' "$value" > "secrets/$name")
chmod 0644 "secrets/$name"
echo "created secrets/$name"
local tmp="${path}.tmp.$$"
(umask 133; printf '%s' "$value" > "$tmp")
chmod 0644 "$tmp"
mv -f "$tmp" "$path"
echo "created ${path}"
}
write_secret minio_root_user "root-$(openssl rand -hex 6)"
write_secret minio_root_password "$(openssl rand -hex 24)"
write_secret s3_api_access_key_id "api-$(openssl rand -hex 8)"
write_secret s3_api_secret_access_key "$(openssl rand -hex 24)"
write_secret s3_worker_access_key_id "worker-$(openssl rand -hex 8)"
write_secret s3_worker_secret_access_key "$(openssl rand -hex 24)"
write_secret grafana_admin_password "$(openssl rand -hex 24)"
bcrypt_hash() {
local password="$1"
python3 -c '
import sys, bcrypt
pw = sys.stdin.read().encode("utf-8")
print(bcrypt.hashpw(pw, bcrypt.gensalt(rounds=10)).decode("ascii"), end="")
' <<< "$password"
}
if [ -s secrets/broker_worker_password ] && [ -s secrets/broker_worker_url ]; then
echo "kept secrets/broker_worker_password secrets/broker_worker_url"
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
wp=$(openssl rand -hex 24)
(umask 133; printf '%s' "$wp" > secrets/broker_worker_password)
chmod 0644 secrets/broker_worker_password
(umask 133; printf '%s' "amqp://messenger-worker:${wp}@broker:5672/messenger" > secrets/broker_worker_url)
chmod 0644 secrets/broker_worker_url
echo "created secrets/broker_worker_password secrets/broker_worker_url"
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
# Broker password lives in one place and is rendered into both files, so the
# URL used by the app and the RabbitMQ config cannot drift apart.
if [ -s secrets/broker_url ] && [ -s secrets/rabbitmq.conf ]; then
echo "kept secrets/broker_url secrets/rabbitmq.conf"
# 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
bp=$(openssl rand -hex 24)
(umask 133; printf '%s' "amqp://messenger:${bp}@broker:5672/messenger" > secrets/broker_url)
chmod 0644 secrets/broker_url
stats_hash="$(openssl passwd -6 -stdin < secrets/infra/haproxy_stats_password)"
tmp="secrets/infra/haproxy_stats_auth.cfg.tmp.$$"
(
umask 133
{
printf 'default_vhost = messenger\n'
printf 'default_user = messenger\n'
printf 'default_pass = %s\n' "$bp"
printf 'default_permissions.configure = ^(attachments\\.jobs|attachments\\.process|attachments\\.dlx|attachments\\.process\\.dlq)$\n'
printf 'default_permissions.read = ^attachments\\.(jobs|process|dlx|process\\.dlq)$\n'
printf 'default_permissions.write = ^attachments\\.(jobs|process|dlx|process\\.dlq)$\n'
} > secrets/rabbitmq.conf
printf 'userlist ops_stats\n'
printf ' user operator password %s\n' "$stats_hash"
} > "$tmp"
)
chmod 0644 secrets/rabbitmq.conf
echo "created secrets/broker_url secrets/rabbitmq.conf"
chmod 0644 "$tmp"
mv -f "$tmp" secrets/infra/haproxy_stats_auth.cfg
echo "created secrets/infra/haproxy_stats_auth.cfg"
fi