408 lines
15 KiB
Bash
Executable File
408 lines
15 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Lab 7 Zero-Rebuild Leaf Certificate Rotation & CRL Revocation.
|
|
# Modes:
|
|
# 1. scripts/rotate-cert.sh <target> [--revoke] [--cn <new-cn>]
|
|
# Rotates leaf cert atomically into ./secrets/pki/<svc>/, optionally revokes
|
|
# old serial into OpenSSL CRL + distributes ca.crl + uploads CRL to OpenBao,
|
|
# and triggers live reload (SIGHUP / SIGUSR2 / pg_reload_conf) with ZERO
|
|
# image rebuild.
|
|
# 2. scripts/rotate-cert.sh --revoke-cert <pem-path>
|
|
# Revokes the given certificate PEM in operator CA DB, regenerates and
|
|
# distributes ca.crl everywhere, uploads CRL to OpenBao, and reloads
|
|
# CRL-enforcing services (api, haproxy, otel-gateway, postgres).
|
|
# 3. scripts/rotate-cert.sh --issue-test-client <name> <cn> <san> <out-dir>
|
|
# Issues a temporary CA-signed EKU=clientAuth leaf cert into <out-dir> for
|
|
# safe revocation testing without disturbing production service certs.
|
|
# Never prints private keys or tokens.
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")/.."
|
|
|
|
if [ -f .env ]; then
|
|
# shellcheck disable=SC1091
|
|
set -a; . ./.env; set +a
|
|
fi
|
|
|
|
OPERATOR_DIR="${LAB7_OPERATOR_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/mtusi-lab7-operator}"
|
|
PKI_OP_DIR="${OPERATOR_DIR}/pki"
|
|
CA_CNF="${PKI_OP_DIR}/ca.cnf"
|
|
OP_CERT="${OPERATOR_DIR}/operator/operator.crt"
|
|
OP_KEY="${OPERATOR_DIR}/operator/operator.key"
|
|
RUNTIME_PKI_DIR="$(pwd)/secrets/pki"
|
|
CA_CERT="${RUNTIME_PKI_DIR}/ca.crt"
|
|
BAO_ADDR="${BAO_ADDR_HOST:-https://localhost:${OPENBAO_PORT:-8200}}"
|
|
COMPOSE=(docker compose --env-file "${COMPOSE_ENV_FILE:-.env}")
|
|
|
|
umask 077
|
|
TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/lab7-rot-cert.XXXXXX")"
|
|
OP_CFG="${TMP_DIR}/op-auth.cfg"
|
|
cleanup() {
|
|
if [ -s "$OP_CFG" ]; then
|
|
curl -fsS --cacert "$CA_CERT" -K "$OP_CFG" -X POST \
|
|
"${BAO_ADDR}/v1/auth/token/revoke-self" >/dev/null 2>&1 || true
|
|
fi
|
|
rm -rf "$TMP_DIR"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
atomic_install() {
|
|
local src="$1"
|
|
local dst="$2"
|
|
local mode="$3"
|
|
mkdir -p "$(dirname "$dst")"
|
|
local tmp="${dst}.tmp.$$"
|
|
cp "$src" "$tmp"
|
|
chmod "$mode" "$tmp"
|
|
mv -f "$tmp" "$dst"
|
|
}
|
|
|
|
cert_serial() {
|
|
openssl x509 -in "$1" -noout -serial | cut -d= -f2
|
|
}
|
|
|
|
cert_fp256() {
|
|
openssl x509 -in "$1" -noout -fingerprint -sha256 | cut -d= -f2
|
|
}
|
|
|
|
cert_cn() {
|
|
openssl x509 -in "$1" -noout -subject -nameopt RFC2253 | sed -n 's/.*CN=\([^,]*\).*/\1/p'
|
|
}
|
|
|
|
is_running() {
|
|
[ -n "$("${COMPOSE[@]}" ps -q --status running "$1" 2>/dev/null || true)" ]
|
|
}
|
|
|
|
reload_service() {
|
|
local svc="$1"
|
|
local sig="$2"
|
|
if is_running "$svc"; then
|
|
"${COMPOSE[@]}" kill -s "$sig" "$svc" >/dev/null 2>&1 || true
|
|
fi
|
|
}
|
|
|
|
refresh_postgres_tls() {
|
|
if is_running postgres; then
|
|
"${COMPOSE[@]}" exec -T postgres sh -c '
|
|
set -eu
|
|
d=/var/run/postgresql/certs
|
|
mkdir -p "$d" && chmod 0700 "$d"
|
|
for f in ca.crt ca.crl server.crt; do
|
|
cp "/run/certs/$f" "$d/$f.tmp" && chmod 0644 "$d/$f.tmp" && mv -f "$d/$f.tmp" "$d/$f"
|
|
done
|
|
cp /run/certs/server.key "$d/server.key.tmp" && chmod 0600 "$d/server.key.tmp" && mv -f "$d/server.key.tmp" "$d/server.key"
|
|
psql -h /var/run/postgresql -U postgres -d "${POSTGRES_DB:-messenger}" -c "SELECT pg_reload_conf();" >/dev/null
|
|
' >/dev/null
|
|
fi
|
|
}
|
|
|
|
upload_crl_to_openbao() {
|
|
[ -s "$CA_CERT" ] && [ -s "$OP_CERT" ] && [ -s "$OP_KEY" ] || return 0
|
|
local seal_json sealed
|
|
seal_json="$(curl -fsS --cacert "$CA_CERT" "${BAO_ADDR}/v1/sys/seal-status" 2>/dev/null || true)"
|
|
sealed="$(printf '%s' "$seal_json" | jq -r 'if .sealed == false then "false" else "true" end' 2>/dev/null || echo true)"
|
|
if [ "$sealed" != "false" ]; then
|
|
return 0
|
|
fi
|
|
local login_json op_token
|
|
login_json="$(curl -fsS --cacert "$CA_CERT" --cert "$OP_CERT" --key "$OP_KEY" \
|
|
-X POST -H 'Content-Type: application/json' \
|
|
--data '{"name":"messenger-operator"}' \
|
|
"${BAO_ADDR}/v1/auth/cert/login" 2>/dev/null || true)"
|
|
op_token="$(printf '%s' "$login_json" | jq -r '.auth.client_token // empty' 2>/dev/null || true)"
|
|
[ -n "$op_token" ] || return 0
|
|
printf 'header = "X-Vault-Token: %s"\n' "$op_token" > "$OP_CFG"
|
|
chmod 0600 "$OP_CFG"
|
|
jq -Rs '{crl: .}' < "${PKI_OP_DIR}/ca.crl" \
|
|
| curl -fsS --cacert "$CA_CERT" -K "$OP_CFG" -X POST \
|
|
-H 'Content-Type: application/json' --data-binary @- \
|
|
"${BAO_ADDR}/v1/auth/cert/crls/lab7-ca" >/dev/null
|
|
}
|
|
|
|
regenerate_and_distribute_crl() {
|
|
openssl ca -batch -config "$CA_CNF" -gencrl -out "${TMP_DIR}/ca.crl" >/dev/null 2>&1
|
|
atomic_install "${TMP_DIR}/ca.crl" "${PKI_OP_DIR}/ca.crl" 0644
|
|
atomic_install "${TMP_DIR}/ca.crl" "${RUNTIME_PKI_DIR}/ca.crl" 0644
|
|
for svc in haproxy api worker migrate postgres redis broker otel-gateway prometheus openbao; do
|
|
if [ -d "${RUNTIME_PKI_DIR}/${svc}" ]; then
|
|
atomic_install "${TMP_DIR}/ca.crl" "${RUNTIME_PKI_DIR}/${svc}/ca.crl" 0644
|
|
fi
|
|
done
|
|
upload_crl_to_openbao
|
|
refresh_postgres_tls
|
|
reload_service api HUP
|
|
reload_service haproxy USR2
|
|
reload_service otel-gateway USR2
|
|
}
|
|
|
|
revoke_pem() {
|
|
local pem="$1"
|
|
if [ ! -s "$pem" ]; then
|
|
echo "error: certificate not found for revocation: ${pem}" >&2
|
|
exit 1
|
|
fi
|
|
# Revoke (ignore "Already revoked" error cleanly for idempotency)
|
|
local err_file="${TMP_DIR}/revoke.err"
|
|
if ! openssl ca -batch -config "$CA_CNF" -revoke "$pem" 2>"$err_file"; then
|
|
if ! grep -qi "Already revoked" "$err_file"; then
|
|
cat "$err_file" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
}
|
|
|
|
issue_new_leaf() {
|
|
local kind="$1"
|
|
local cn="$2"
|
|
local san="$3"
|
|
local out_crt="$4"
|
|
local out_key="$5"
|
|
local key_mode="$6"
|
|
|
|
local ext_section="v3_server"
|
|
[ "$kind" = "client" ] && ext_section="v3_client"
|
|
[[ "$san" == *:* ]] || san="DNS:${san}"
|
|
|
|
local req_cnf="${TMP_DIR}/leaf.req.cnf"
|
|
local key_tmp="${TMP_DIR}/leaf.key"
|
|
local csr_tmp="${TMP_DIR}/leaf.csr"
|
|
local crt_tmp="${TMP_DIR}/leaf.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"
|
|
}
|
|
|
|
update_otel_cn_allowlist() {
|
|
local old_cn="$1"
|
|
local new_cn="$2"
|
|
local list_file="${RUNTIME_PKI_DIR}/otel-gateway/allowed-otlp-cn.lst"
|
|
[ -f "$list_file" ] || return 0
|
|
local tmp="${TMP_DIR}/allowed-otlp-cn.lst"
|
|
grep -Fxv "$old_cn" "$list_file" > "$tmp" || true
|
|
if ! grep -Fxq "$new_cn" "$tmp"; then
|
|
printf '%s\n' "$new_cn" >> "$tmp"
|
|
fi
|
|
atomic_install "$tmp" "$list_file" 0644
|
|
reload_service otel-gateway USR2
|
|
}
|
|
|
|
if [ $# -lt 1 ]; then
|
|
echo "Usage: $0 <target> [--revoke] [--cn <new-cn>] | --revoke-cert <pem> | --issue-test-client <name> <cn> <san> <out-dir>" >&2
|
|
exit 2
|
|
fi
|
|
|
|
# Mode 2: standalone PEM revocation
|
|
if [ "$1" = "--revoke-cert" ]; then
|
|
pem="${2:?missing pem path}"
|
|
ser="$(cert_serial "$pem")"
|
|
revoke_pem "$pem"
|
|
regenerate_and_distribute_crl
|
|
echo "revoke-cert ok: serial=${ser} crl_updated=true"
|
|
exit 0
|
|
fi
|
|
|
|
# Mode 3: issue temporary test client cert
|
|
if [ "$1" = "--issue-test-client" ]; then
|
|
t_name="${2:?missing name}"
|
|
t_cn="${3:?missing cn}"
|
|
t_san="${4:?missing san}"
|
|
[[ "$t_san" == *:* ]] || t_san="DNS:${t_san}"
|
|
t_dir="${5:?missing out-dir}"
|
|
mkdir -p "$t_dir"
|
|
issue_new_leaf client "$t_cn" "$t_san" "${t_dir}/${t_name}.crt" "${t_dir}/${t_name}.key" 0644
|
|
cat "${t_dir}/${t_name}.crt" "${t_dir}/${t_name}.key" > "${TMP_DIR}/test.pem"
|
|
atomic_install "${TMP_DIR}/test.pem" "${t_dir}/${t_name}.pem" 0644
|
|
echo "issue-test-client ok: name=${t_name} cn=${t_cn} serial=$(cert_serial "${t_dir}/${t_name}.crt") fingerprint=$(cert_fp256 "${t_dir}/${t_name}.crt")"
|
|
exit 0
|
|
fi
|
|
|
|
# Mode 1: rotate named target leaf cert
|
|
TARGET="$1"
|
|
shift
|
|
REVOKE=0
|
|
NEW_CN=""
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--revoke) REVOKE=1 ;;
|
|
--cn)
|
|
shift
|
|
NEW_CN="${1:?missing value for --cn}"
|
|
;;
|
|
-h|--help)
|
|
echo "Usage: $0 <target> [--revoke] [--cn <new-cn>]"
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Unknown argument: $1" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
kind="" cn="" san="" crt_path="" key_path="" bundle_path="" key_mode="0644" reload_svc="" reload_sig=""
|
|
case "$TARGET" in
|
|
api|api-server)
|
|
kind=server; cn="api"; san="DNS:api"
|
|
crt_path="${RUNTIME_PKI_DIR}/api/server.crt"; key_path="${RUNTIME_PKI_DIR}/api/server.key"
|
|
reload_svc="api"; reload_sig="HUP"
|
|
;;
|
|
api-client)
|
|
kind=client; cn="messenger-api-g1"; san="DNS:messenger-api"
|
|
crt_path="${RUNTIME_PKI_DIR}/api/client.crt"; key_path="${RUNTIME_PKI_DIR}/api/client.key"
|
|
reload_svc="api"; reload_sig="HUP"
|
|
;;
|
|
worker|worker-client)
|
|
kind=client; cn="messenger-worker-g1"; san="DNS:messenger-worker"
|
|
crt_path="${RUNTIME_PKI_DIR}/worker/client.crt"; key_path="${RUNTIME_PKI_DIR}/worker/client.key"
|
|
reload_svc="worker"; reload_sig="HUP"
|
|
;;
|
|
migrate|migrator-client)
|
|
kind=client; cn="messenger-migrator-g1"; san="DNS:messenger-migrator"
|
|
crt_path="${RUNTIME_PKI_DIR}/migrate/client.crt"; key_path="${RUNTIME_PKI_DIR}/migrate/client.key"
|
|
;;
|
|
haproxy|haproxy-ingress|haproxy-server)
|
|
kind=server; cn="localhost"; san="DNS:localhost,DNS:haproxy"
|
|
crt_path="${RUNTIME_PKI_DIR}/haproxy/server.crt"; key_path="${RUNTIME_PKI_DIR}/haproxy/server.key"
|
|
bundle_path="${RUNTIME_PKI_DIR}/haproxy/server.pem"
|
|
reload_svc="haproxy"; reload_sig="USR2"
|
|
;;
|
|
haproxy-client)
|
|
kind=client; cn="haproxy-lb-g1"; san="DNS:haproxy-lb"
|
|
crt_path="${RUNTIME_PKI_DIR}/haproxy/client.crt"; key_path="${RUNTIME_PKI_DIR}/haproxy/client.key"
|
|
bundle_path="${RUNTIME_PKI_DIR}/haproxy/client.pem"
|
|
reload_svc="haproxy"; reload_sig="USR2"
|
|
;;
|
|
postgres)
|
|
kind=server; cn="postgres"; san="DNS:postgres,DNS:localhost"
|
|
crt_path="${RUNTIME_PKI_DIR}/postgres/server.crt"; key_path="${RUNTIME_PKI_DIR}/postgres/server.key"
|
|
reload_svc="postgres"; reload_sig="PG"
|
|
;;
|
|
redis)
|
|
kind=server; cn="redis"; san="DNS:redis,DNS:localhost"
|
|
crt_path="${RUNTIME_PKI_DIR}/redis/server.crt"; key_path="${RUNTIME_PKI_DIR}/redis/server.key"
|
|
;;
|
|
s3)
|
|
kind=server; cn="s3"; san="DNS:s3,DNS:localhost"
|
|
crt_path="${RUNTIME_PKI_DIR}/s3/public.crt"; key_path="${RUNTIME_PKI_DIR}/s3/private.key"
|
|
;;
|
|
broker)
|
|
kind=server; cn="broker"; san="DNS:broker"
|
|
crt_path="${RUNTIME_PKI_DIR}/broker/server.crt"; key_path="${RUNTIME_PKI_DIR}/broker/server.key"
|
|
;;
|
|
otel-gateway)
|
|
kind=server; cn="otel-collector"; san="DNS:otel-collector,DNS:otel-gateway"
|
|
crt_path="${RUNTIME_PKI_DIR}/otel-gateway/server.crt"; key_path="${RUNTIME_PKI_DIR}/otel-gateway/server.key"
|
|
bundle_path="${RUNTIME_PKI_DIR}/otel-gateway/server.pem"
|
|
reload_svc="otel-gateway"; reload_sig="USR2"
|
|
;;
|
|
otel-collector)
|
|
kind=client; cn="otel-collector"; san="DNS:otel-collector"
|
|
crt_path="${RUNTIME_PKI_DIR}/otel-collector/client.crt"; key_path="${RUNTIME_PKI_DIR}/otel-collector/client.key"
|
|
;;
|
|
prometheus)
|
|
kind=server; cn="prometheus"; san="DNS:prometheus,DNS:localhost"
|
|
crt_path="${RUNTIME_PKI_DIR}/prometheus/server.crt"; key_path="${RUNTIME_PKI_DIR}/prometheus/server.key"
|
|
reload_svc="prometheus"; reload_sig="HUP"
|
|
;;
|
|
prometheus-client)
|
|
kind=client; cn="prometheus"; san="DNS:prometheus"
|
|
crt_path="${RUNTIME_PKI_DIR}/prometheus/client.crt"; key_path="${RUNTIME_PKI_DIR}/prometheus/client.key"
|
|
reload_svc="prometheus"; reload_sig="HUP"
|
|
;;
|
|
loki)
|
|
kind=server; cn="loki"; san="DNS:loki"
|
|
crt_path="${RUNTIME_PKI_DIR}/loki/server.crt"; key_path="${RUNTIME_PKI_DIR}/loki/server.key"
|
|
;;
|
|
tempo)
|
|
kind=server; cn="tempo"; san="DNS:tempo"
|
|
crt_path="${RUNTIME_PKI_DIR}/tempo/server.crt"; key_path="${RUNTIME_PKI_DIR}/tempo/server.key"
|
|
;;
|
|
grafana)
|
|
kind=server; cn="grafana"; san="DNS:grafana,DNS:localhost"
|
|
crt_path="${RUNTIME_PKI_DIR}/grafana/server.crt"; key_path="${RUNTIME_PKI_DIR}/grafana/server.key"
|
|
;;
|
|
grafana-client)
|
|
kind=client; cn="grafana"; san="DNS:grafana"
|
|
crt_path="${RUNTIME_PKI_DIR}/grafana/client.crt"; key_path="${RUNTIME_PKI_DIR}/grafana/client.key"
|
|
;;
|
|
openbao)
|
|
kind=server; cn="openbao"; san="DNS:openbao,DNS:localhost,IP:127.0.0.1"
|
|
crt_path="${RUNTIME_PKI_DIR}/openbao/server.crt"; key_path="${RUNTIME_PKI_DIR}/openbao/server.key"
|
|
reload_svc="openbao"; reload_sig="HUP"
|
|
;;
|
|
operator)
|
|
kind=client; cn="messenger-operator-g1"; san="DNS:messenger-operator"
|
|
crt_path="${OP_CERT}"; key_path="${OP_KEY}"; key_mode="0600"
|
|
;;
|
|
rogue-client)
|
|
kind=client; cn="rogue-client"; san="DNS:rogue-client"
|
|
crt_path="${PKI_OP_DIR}/rogue-client.crt"; key_path="${PKI_OP_DIR}/rogue-client.key"; key_mode="0600"
|
|
;;
|
|
*)
|
|
echo "error: unknown cert target '${TARGET}'" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
|
|
old_serial="none"
|
|
old_cn=""
|
|
if [ -s "$crt_path" ]; then
|
|
old_serial="$(cert_serial "$crt_path")"
|
|
old_cn="$(cert_cn "$crt_path")"
|
|
cp "$crt_path" "${TMP_DIR}/old.crt"
|
|
fi
|
|
|
|
if [ -n "$NEW_CN" ]; then
|
|
cn="$NEW_CN"
|
|
elif [ -n "$old_cn" ]; then
|
|
cn="$old_cn"
|
|
fi
|
|
|
|
issue_new_leaf "$kind" "$cn" "$san" "$crt_path" "$key_path" "$key_mode"
|
|
if [ -n "$bundle_path" ]; then
|
|
cat "$crt_path" "$key_path" > "${TMP_DIR}/bundle.pem"
|
|
atomic_install "${TMP_DIR}/bundle.pem" "$bundle_path" 0644
|
|
fi
|
|
|
|
if [ -n "$old_cn" ] && [ "$old_cn" != "$cn" ]; then
|
|
case "$TARGET" in
|
|
api-client|worker|worker-client)
|
|
update_otel_cn_allowlist "$old_cn" "$cn"
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
if [ "$REVOKE" -eq 1 ] && [ -s "${TMP_DIR}/old.crt" ]; then
|
|
revoke_pem "${TMP_DIR}/old.crt"
|
|
regenerate_and_distribute_crl
|
|
fi
|
|
|
|
if [ "$reload_svc" = "postgres" ]; then
|
|
refresh_postgres_tls
|
|
elif [ -n "$reload_svc" ] && [ -n "$reload_sig" ]; then
|
|
reload_service "$reload_svc" "$reload_sig"
|
|
fi
|
|
|
|
new_serial="$(cert_serial "$crt_path")"
|
|
new_fp="$(cert_fp256 "$crt_path")"
|
|
echo "rotate-cert ok: target=${TARGET} cn=${cn} old_serial=${old_serial} new_serial=${new_serial} fingerprint=${new_fp} revoked=$([ "$REVOKE" -eq 1 ] && echo true || echo false)"
|