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
+373
View File
@@ -0,0 +1,373 @@
#!/usr/bin/env bash
# Lab 7 Idempotent Local PKI Bootstrap.
# - Operator CA private key, OpenSSL CA DB, foreign-ca, rogue-client, and
# messenger-operator-g1 live strictly outside Git in $LAB7_OPERATOR_DIR (0700)
# and are NEVER mounted into any container.
# - Runtime per-service leaf certs, ca.crt, and ca.crl are installed atomically
# into gitignored ./secrets/pki/<service>/ with strict EKU separation
# (serverAuth ONLY vs clientAuth ONLY) and single-DNS-SAN client identities.
# - Never prints private keys. Re-running is idempotent unless --force is set.
set -euo pipefail
cd "$(dirname "$0")/.."
FORCE=0
while [ $# -gt 0 ]; do
case "$1" in
--force) FORCE=1 ;;
-h|--help)
echo "Usage: $0 [--force]"
exit 0
;;
*)
echo "Unknown argument: $1" >&2
exit 2
;;
esac
shift
done
OPERATOR_DIR="${LAB7_OPERATOR_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/mtusi-lab7-operator}"
PKI_OP_DIR="${OPERATOR_DIR}/pki"
BAO_OP_DIR="${OPERATOR_DIR}/openbao"
OP_CERT_DIR="${OPERATOR_DIR}/operator"
RUNTIME_PKI_DIR="$(pwd)/secrets/pki"
umask 077
mkdir -p "$OPERATOR_DIR" "$PKI_OP_DIR/newcerts" "$PKI_OP_DIR/issued" "$BAO_OP_DIR" "$OP_CERT_DIR"
chmod 0700 "$OPERATOR_DIR" "$PKI_OP_DIR" "$PKI_OP_DIR/newcerts" "$PKI_OP_DIR/issued" "$BAO_OP_DIR" "$OP_CERT_DIR"
mkdir -p secrets "$RUNTIME_PKI_DIR"
chmod 0700 secrets "$RUNTIME_PKI_DIR"
TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/lab7-pki.XXXXXX")"
trap 'rm -rf "$TMP_DIR"' EXIT
CA_CNF="${PKI_OP_DIR}/ca.cnf"
write_ca_cnf() {
cat > "${CA_CNF}" <<EOF
[ ca ]
default_ca = CA_default
[ CA_default ]
dir = ${PKI_OP_DIR}
certs = \$dir
crl_dir = \$dir
new_certs_dir = \$dir/newcerts
database = \$dir/index.txt
serial = \$dir/serial
crlnumber = \$dir/crlnumber
certificate = \$dir/ca.crt
private_key = \$dir/ca.key
default_md = sha256
default_days = 30
default_crl_days = 30
preserve = no
unique_subject = no
policy = policy_loose
copy_extensions = copy
crl_extensions = crl_ext
[ policy_loose ]
organizationName = optional
commonName = supplied
[ req ]
default_md = sha256
distinguished_name = req_dn
prompt = no
[ req_dn ]
O = mtusi-lab7
CN = mtusi-lab7-local-ca
[ v3_ca ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true, pathlen:0
keyUsage = critical, keyCertSign, cRLSign
[ v3_server ]
basicConstraints = critical, CA:false
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
keyUsage = critical, digitalSignature
extendedKeyUsage = serverAuth
[ v3_client ]
basicConstraints = critical, CA:false
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
keyUsage = critical, digitalSignature
extendedKeyUsage = clientAuth
[ crl_ext ]
authorityKeyIdentifier = keyid:always
EOF
chmod 0600 "${CA_CNF}"
}
atomic_install() {
local src="$1"
local dst="$2"
local mode="$3"
local dir
dir="$(dirname "$dst")"
mkdir -p "$dir"
local tmp="${dst}.tmp.$$"
cp "$src" "$tmp"
chmod "$mode" "$tmp"
mv -f "$tmp" "$dst"
}
if [ "$FORCE" -eq 1 ]; then
rm -f "${PKI_OP_DIR}/ca.key" "${PKI_OP_DIR}/ca.crt" "${PKI_OP_DIR}/ca.crl" \
"${PKI_OP_DIR}/index.txt" "${PKI_OP_DIR}/index.txt.old" "${PKI_OP_DIR}/index.txt.attr" "${PKI_OP_DIR}/index.txt.attr.old" \
"${PKI_OP_DIR}/serial" "${PKI_OP_DIR}/serial.old" "${PKI_OP_DIR}/crlnumber" "${PKI_OP_DIR}/crlnumber.old" \
"${PKI_OP_DIR}/foreign-ca.key" "${PKI_OP_DIR}/foreign-ca.crt" \
"${PKI_OP_DIR}/foreign-client.key" "${PKI_OP_DIR}/foreign-client.crt" \
"${PKI_OP_DIR}/rogue-client.key" "${PKI_OP_DIR}/rogue-client.crt" \
"${OP_CERT_DIR}/operator.key" "${OP_CERT_DIR}/operator.crt"
rm -rf "${PKI_OP_DIR}/newcerts" "${PKI_OP_DIR}/issued"
mkdir -p "${PKI_OP_DIR}/newcerts" "${PKI_OP_DIR}/issued"
chmod 0700 "${PKI_OP_DIR}/newcerts" "${PKI_OP_DIR}/issued"
fi
write_ca_cnf
# 1. Local Root CA (ECDSA P-256, pathlen:0, 365d)
if [ ! -s "${PKI_OP_DIR}/ca.key" ] || [ ! -s "${PKI_OP_DIR}/ca.crt" ]; then
: > "${PKI_OP_DIR}/index.txt"
printf 'unique_subject = no\n' > "${PKI_OP_DIR}/index.txt.attr"
printf '1000\n' > "${PKI_OP_DIR}/serial"
printf '1000\n' > "${PKI_OP_DIR}/crlnumber"
openssl ecparam -name prime256v1 -genkey -noout -out "${PKI_OP_DIR}/ca.key"
chmod 0600 "${PKI_OP_DIR}/ca.key"
openssl req -new -x509 -sha256 -days 365 \
-key "${PKI_OP_DIR}/ca.key" \
-config "${CA_CNF}" -extensions v3_ca \
-subj "/O=mtusi-lab7/CN=mtusi-lab7-local-ca" \
-out "${PKI_OP_DIR}/ca.crt"
chmod 0644 "${PKI_OP_DIR}/ca.crt"
echo "created Root CA in ${PKI_OP_DIR}"
else
[ -f "${PKI_OP_DIR}/index.txt" ] || : > "${PKI_OP_DIR}/index.txt"
[ -f "${PKI_OP_DIR}/index.txt.attr" ] || printf 'unique_subject = no\n' > "${PKI_OP_DIR}/index.txt.attr"
[ -f "${PKI_OP_DIR}/serial" ] || printf '1000\n' > "${PKI_OP_DIR}/serial"
[ -f "${PKI_OP_DIR}/crlnumber" ] || printf '1000\n' > "${PKI_OP_DIR}/crlnumber"
echo "kept Root CA in ${PKI_OP_DIR}"
fi
# Generate or refresh CRL v2
if [ ! -s "${PKI_OP_DIR}/ca.crl" ] || [ "$FORCE" -eq 1 ]; then
openssl ca -batch -config "${CA_CNF}" -gencrl -out "${PKI_OP_DIR}/ca.crl" >/dev/null 2>&1
chmod 0644 "${PKI_OP_DIR}/ca.crl"
fi
# 2. Foreign CA + Foreign Client (strictly in operator dir for negative tests)
if [ ! -s "${PKI_OP_DIR}/foreign-ca.key" ] || [ ! -s "${PKI_OP_DIR}/foreign-ca.crt" ]; then
openssl ecparam -name prime256v1 -genkey -noout -out "${PKI_OP_DIR}/foreign-ca.key"
chmod 0600 "${PKI_OP_DIR}/foreign-ca.key"
openssl req -new -x509 -sha256 -days 365 \
-key "${PKI_OP_DIR}/foreign-ca.key" \
-subj "/O=mtusi-lab7-foreign/CN=mtusi-lab7-foreign-ca" \
-addext "basicConstraints=critical,CA:true,pathlen:0" \
-addext "keyUsage=critical,keyCertSign,cRLSign" \
-out "${PKI_OP_DIR}/foreign-ca.crt"
chmod 0644 "${PKI_OP_DIR}/foreign-ca.crt"
fi
if [ ! -s "${PKI_OP_DIR}/foreign-client.key" ] || [ ! -s "${PKI_OP_DIR}/foreign-client.crt" ]; then
openssl ecparam -name prime256v1 -genkey -noout -out "${PKI_OP_DIR}/foreign-client.key"
chmod 0600 "${PKI_OP_DIR}/foreign-client.key"
openssl req -new -sha256 \
-key "${PKI_OP_DIR}/foreign-client.key" \
-subj "/O=mtusi-lab7-foreign/CN=foreign-client" \
-out "${TMP_DIR}/foreign-client.csr"
cat > "${TMP_DIR}/foreign-client.ext" <<EOF
basicConstraints = critical, CA:false
keyUsage = critical, digitalSignature
extendedKeyUsage = clientAuth
subjectAltName = DNS:foreign-client
EOF
openssl x509 -req -sha256 -days 30 \
-in "${TMP_DIR}/foreign-client.csr" \
-CA "${PKI_OP_DIR}/foreign-ca.crt" -CAkey "${PKI_OP_DIR}/foreign-ca.key" -CAcreateserial \
-extfile "${TMP_DIR}/foreign-client.ext" \
-out "${PKI_OP_DIR}/foreign-client.crt" >/dev/null 2>&1
chmod 0644 "${PKI_OP_DIR}/foreign-client.crt"
rm -f "${PKI_OP_DIR}/foreign-ca.srl"
fi
# Helper: issue leaf cert signed by our Local Root CA via `openssl ca` (tracks serial in index.txt)
issue_leaf() {
local cert_id="$1"
local kind="$2" # server | client
local cn="$3"
local san="$4"
local out_crt="$5"
local out_key="$6"
local key_mode="${7:-0644}"
if [ "$FORCE" -eq 0 ] && [ -s "$out_crt" ] && [ -s "$out_key" ]; then
return 0
fi
local ext_section="v3_server"
[ "$kind" = "client" ] && ext_section="v3_client"
local req_cnf="${TMP_DIR}/${cert_id}.req.cnf"
local key_tmp="${TMP_DIR}/${cert_id}.key"
local csr_tmp="${TMP_DIR}/${cert_id}.csr"
local crt_tmp="${TMP_DIR}/${cert_id}.crt"
cat > "$req_cnf" <<EOF
[ req ]
default_md = sha256
distinguished_name = dn
req_extensions = req_ext
prompt = no
[ dn ]
O = mtusi-lab7
CN = ${cn}
[ req_ext ]
subjectAltName = ${san}
EOF
openssl ecparam -name prime256v1 -genkey -noout -out "$key_tmp"
openssl req -new -sha256 -key "$key_tmp" -config "$req_cnf" -out "$csr_tmp"
openssl ca -batch -notext -config "${CA_CNF}" -extensions "$ext_section" \
-in "$csr_tmp" -out "$crt_tmp" >/dev/null 2>&1
atomic_install "$crt_tmp" "$out_crt" 0644
atomic_install "$key_tmp" "$out_key" "$key_mode"
# Keep public cert reference in operator DB for deterministic revocation later.
atomic_install "$crt_tmp" "${PKI_OP_DIR}/issued/${cert_id}.crt" 0644
echo "issued ${cert_id} (kind=${kind} cn=${cn})"
}
make_bundle() {
local crt="$1"
local key="$2"
local bundle="$3"
local tmp="${TMP_DIR}/bundle.pem"
cat "$crt" "$key" > "$tmp"
atomic_install "$tmp" "$bundle" 0644
}
# 3. Operator-only certs signed by our Root CA (NEVER mounted into containers)
issue_leaf rogue-client client rogue-client "DNS:rogue-client" \
"${PKI_OP_DIR}/rogue-client.crt" "${PKI_OP_DIR}/rogue-client.key" 0600
issue_leaf messenger-operator client messenger-operator-g1 "DNS:messenger-operator" \
"${OP_CERT_DIR}/operator.crt" "${OP_CERT_DIR}/operator.key" 0600
# 4. Prepare isolated per-service runtime cert dirs (0755 inside 0700 host parent)
SERVICES=(
haproxy api worker migrate postgres redis s3 broker
otel-gateway otel-collector prometheus loki tempo grafana openbao
)
for svc in "${SERVICES[@]}"; do
mkdir -p "${RUNTIME_PKI_DIR}/${svc}"
chmod 0755 "${RUNTIME_PKI_DIR}/${svc}"
done
mkdir -p "${RUNTIME_PKI_DIR}/s3/CAs"
chmod 0755 "${RUNTIME_PKI_DIR}/s3/CAs"
# 5. Server certificates (EKU=serverAuth ONLY, per unified-contract.md §1.2)
issue_leaf haproxy-ingress server localhost "DNS:localhost,DNS:haproxy" \
"${RUNTIME_PKI_DIR}/haproxy/server.crt" "${RUNTIME_PKI_DIR}/haproxy/server.key" 0644
make_bundle "${RUNTIME_PKI_DIR}/haproxy/server.crt" "${RUNTIME_PKI_DIR}/haproxy/server.key" \
"${RUNTIME_PKI_DIR}/haproxy/server.pem"
issue_leaf api-server server api "DNS:api" \
"${RUNTIME_PKI_DIR}/api/server.crt" "${RUNTIME_PKI_DIR}/api/server.key" 0644
issue_leaf postgres-server server postgres "DNS:postgres,DNS:localhost" \
"${RUNTIME_PKI_DIR}/postgres/server.crt" "${RUNTIME_PKI_DIR}/postgres/server.key" 0644
issue_leaf redis-server server redis "DNS:redis,DNS:localhost" \
"${RUNTIME_PKI_DIR}/redis/server.crt" "${RUNTIME_PKI_DIR}/redis/server.key" 0644
issue_leaf s3-server server s3 "DNS:s3,DNS:localhost" \
"${RUNTIME_PKI_DIR}/s3/public.crt" "${RUNTIME_PKI_DIR}/s3/private.key" 0644
issue_leaf broker-server server broker "DNS:broker" \
"${RUNTIME_PKI_DIR}/broker/server.crt" "${RUNTIME_PKI_DIR}/broker/server.key" 0644
issue_leaf otel-server server otel-collector "DNS:otel-collector,DNS:otel-gateway" \
"${RUNTIME_PKI_DIR}/otel-gateway/server.crt" "${RUNTIME_PKI_DIR}/otel-gateway/server.key" 0644
make_bundle "${RUNTIME_PKI_DIR}/otel-gateway/server.crt" "${RUNTIME_PKI_DIR}/otel-gateway/server.key" \
"${RUNTIME_PKI_DIR}/otel-gateway/server.pem"
issue_leaf prometheus-server server prometheus "DNS:prometheus,DNS:localhost" \
"${RUNTIME_PKI_DIR}/prometheus/server.crt" "${RUNTIME_PKI_DIR}/prometheus/server.key" 0644
issue_leaf loki-server server loki "DNS:loki" \
"${RUNTIME_PKI_DIR}/loki/server.crt" "${RUNTIME_PKI_DIR}/loki/server.key" 0644
issue_leaf tempo-server server tempo "DNS:tempo" \
"${RUNTIME_PKI_DIR}/tempo/server.crt" "${RUNTIME_PKI_DIR}/tempo/server.key" 0644
issue_leaf grafana-server server grafana "DNS:grafana,DNS:localhost" \
"${RUNTIME_PKI_DIR}/grafana/server.crt" "${RUNTIME_PKI_DIR}/grafana/server.key" 0644
issue_leaf openbao-server server openbao "DNS:openbao,DNS:localhost,IP:127.0.0.1" \
"${RUNTIME_PKI_DIR}/openbao/server.crt" "${RUNTIME_PKI_DIR}/openbao/server.key" 0644
# 6. Client identity certificates (EKU=clientAuth ONLY, single DNS SAN, per unified-contract.md §1.3)
issue_leaf haproxy-client client haproxy-lb-g1 "DNS:haproxy-lb" \
"${RUNTIME_PKI_DIR}/haproxy/client.crt" "${RUNTIME_PKI_DIR}/haproxy/client.key" 0644
make_bundle "${RUNTIME_PKI_DIR}/haproxy/client.crt" "${RUNTIME_PKI_DIR}/haproxy/client.key" \
"${RUNTIME_PKI_DIR}/haproxy/client.pem"
issue_leaf api-client client messenger-api-g1 "DNS:messenger-api" \
"${RUNTIME_PKI_DIR}/api/client.crt" "${RUNTIME_PKI_DIR}/api/client.key" 0644
issue_leaf worker-client client messenger-worker-g1 "DNS:messenger-worker" \
"${RUNTIME_PKI_DIR}/worker/client.crt" "${RUNTIME_PKI_DIR}/worker/client.key" 0644
issue_leaf migrator-client client messenger-migrator-g1 "DNS:messenger-migrator" \
"${RUNTIME_PKI_DIR}/migrate/client.crt" "${RUNTIME_PKI_DIR}/migrate/client.key" 0644
issue_leaf otel-collector-client client otel-collector "DNS:otel-collector" \
"${RUNTIME_PKI_DIR}/otel-collector/client.crt" "${RUNTIME_PKI_DIR}/otel-collector/client.key" 0644
issue_leaf grafana-client client grafana "DNS:grafana" \
"${RUNTIME_PKI_DIR}/grafana/client.crt" "${RUNTIME_PKI_DIR}/grafana/client.key" 0644
issue_leaf prometheus-client client prometheus "DNS:prometheus" \
"${RUNTIME_PKI_DIR}/prometheus/client.crt" "${RUNTIME_PKI_DIR}/prometheus/client.key" 0644
issue_leaf redis-health client redis-health "DNS:redis-health" \
"${RUNTIME_PKI_DIR}/redis/health.crt" "${RUNTIME_PKI_DIR}/redis/health.key" 0644
# 7. Distribute public CA cert and CRL atomically into runtime PKI dirs
atomic_install "${PKI_OP_DIR}/ca.crt" "${RUNTIME_PKI_DIR}/ca.crt" 0644
atomic_install "${PKI_OP_DIR}/ca.crl" "${RUNTIME_PKI_DIR}/ca.crl" 0644
atomic_install "${PKI_OP_DIR}/ca.crt" "${RUNTIME_PKI_DIR}/s3/CAs/ca.crt" 0644
for svc in haproxy api worker migrate postgres redis broker otel-gateway otel-collector prometheus loki tempo grafana openbao; do
atomic_install "${PKI_OP_DIR}/ca.crt" "${RUNTIME_PKI_DIR}/${svc}/ca.crt" 0644
done
for svc in haproxy api worker migrate postgres redis broker otel-gateway prometheus openbao; do
atomic_install "${PKI_OP_DIR}/ca.crl" "${RUNTIME_PKI_DIR}/${svc}/ca.crl" 0644
done
# 8. OTel Gateway mTLS CN & SAN allowlists (mounted via /run/certs:ro; never dirty Git)
if [ "$FORCE" -eq 1 ] || [ ! -s "${RUNTIME_PKI_DIR}/otel-gateway/allowed-otlp-cn.lst" ]; then
api_cn="$(openssl x509 -in "${RUNTIME_PKI_DIR}/api/client.crt" -noout -subject -nameopt RFC2253 | sed -n 's/.*CN=\([^,]*\).*/\1/p')"
worker_cn="$(openssl x509 -in "${RUNTIME_PKI_DIR}/worker/client.crt" -noout -subject -nameopt RFC2253 | sed -n 's/.*CN=\([^,]*\).*/\1/p')"
printf '%s\n%s\n' "${api_cn:-messenger-api-g1}" "${worker_cn:-messenger-worker-g1}" > "${TMP_DIR}/allowed-otlp-cn.lst"
atomic_install "${TMP_DIR}/allowed-otlp-cn.lst" "${RUNTIME_PKI_DIR}/otel-gateway/allowed-otlp-cn.lst" 0644
fi
if [ "$FORCE" -eq 1 ] || [ ! -s "${RUNTIME_PKI_DIR}/otel-gateway/allowed-otlp-san.lst" ]; then
printf 'DNS:messenger-api\nDNS:messenger-worker\n' > "${TMP_DIR}/allowed-otlp-san.lst"
atomic_install "${TMP_DIR}/allowed-otlp-san.lst" "${RUNTIME_PKI_DIR}/otel-gateway/allowed-otlp-san.lst" 0644
fi
echo "pki-bootstrap ok: operator_dir=${OPERATOR_DIR} runtime_dir=${RUNTIME_PKI_DIR}"