feat: rotate and revoke credentials on the running stand

Add operator commands for leaf and secret rotation, identity revocation
and CRL renewal without rebuilding images, and a drill under load.
A rejected PostgreSQL role password is now 503 instead of 500.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 06:46:55 +03:00
co-authored by Claude Opus 5.5
parent 21b8a08e46
commit c40fbef36a
40 changed files with 5804 additions and 127 deletions
+101 -13
View File
@@ -18,13 +18,24 @@
# не текущим CA, отозванные или те, чьи DNS SAN
# либо назначение разошлись со строкой списка
# (change https-tooling); действующие не меняются
# rotate <identity> — новый ключ и leaf строки списка в каталог стенда;
# прежний НЕ отзывается, CRL не меняется (change
# rotation-revocation, design, решение 4)
# issue <имя> --profile client|server --san <dns> [--san <dns>…]
# --days N --out <каталог> [--bundle]
# — разовый leaf (например, проба mtls-probe на 1 день)
# revoke <identity> — отозвать действующие leaf identity и выпустить CRL;
# нет действующего — код 1 без изменений
# --days N | --not-after YYYYMMDDHHMMSSZ --out <каталог> [--bundle]
# — разовый leaf (например, проба mtls-probe на 1 день);
# --not-after — точный конец срока (UTC)
# revoke <identity> [--superseded]
# — отозвать действующие leaf identity и выпустить CRL;
# нет действующего — код 1 без изменений. Только
# файлы: работающий стенд принимает leaf, пока отзыв
# не применён (make revoke_identity). --superseded —
# все действующие и не истёкшие, кроме leaf каталога
# стенда; нечего — код 0, база и CRL прежние
# crl — выпустить CRL (30 дней) в каталог стенда
# status — сроки CA, leaf стенда и CRL с отметками
# status — сроки CA, leaf стенда и CRL с отметками; identity,
# у которых кроме leaf стенда есть ещё действующие
# (не отозванные и не истёкшие), и их число
#
# Нужен OpenSSL ≥ 3 (LibreSSL не годится): бинарь — OPENSSL, проверка до
# любых файлов. Вывод — только identity, серийные номера, сроки, отпечатки и
@@ -59,7 +70,7 @@ die() {
}
usage() {
sed -n '13,27p' "$0" >&2
sed -n '13,39p' "$0" >&2
exit 2
}
@@ -201,7 +212,8 @@ cmd_init() {
describe_cert "CA" "$PKI_OPERATOR_DIR/ca.crt"
}
# leaf: issue_leaf <имя> <профиль> <SAN через запятую> <дней> <каталог> <форма>
# leaf: issue_leaf <имя> <профиль> <SAN через запятую> <срок> <каталог> <форма>;
# срок — число дней или момент конца YYYYMMDDHHMMSSZ
issue_leaf() {
name=$1
profile=$2
@@ -230,8 +242,12 @@ issue_leaf() {
ossl req -config "$PKI_CONFIG" -new -key "$key_tmp" -subj "/CN=$name" \
-out "$tmp_dir/$name.csr"
PKI_SAN=$san_ext
case $days in
*Z) period=-enddate ;;
*) period=-days ;;
esac
ossl ca -config "$PKI_CONFIG" -batch -notext \
-extensions "${profile}_ext" -days "$days" \
-extensions "${profile}_ext" "$period" "$days" \
-in "$tmp_dir/$name.csr" -out "$crt_tmp"
PKI_SAN=DNS:unused
chmod 600 "$key_tmp"
@@ -352,6 +368,42 @@ cmd_stand() {
done
}
# серийные номера действующих (не отозванных и не истёкших) leaf identity по
# базе openssl ca, кроме $2; пусто — нет. Срок в базе — YYMMDDHHMMSSZ (UTC),
# с 2050 года — YYYYMMDDHHMMSSZ
older_serials() {
awk -F '\t' -v subj="/CN=$1" -v keep="$2" -v now="$(date -u +%Y%m%d%H%M%S)" '
$1 == "V" && $6 == subj && $4 != keep {
end = $2
sub(/Z$/, "", end)
if (length(end) == 12) end = "20" end
if (end > now) print $4
}' "$PKI_OPERATOR_DIR/index.txt"
}
# серийный номер leaf каталога стенда или пусто
stand_serial() {
if [ -f "$PKI_STAND_DIR/$1.crt" ]; then
cert_serial "$PKI_STAND_DIR/$1.crt"
fi
}
# новый leaf строки списка рядом с действующим прежним
cmd_rotate() {
[ $# -eq 1 ] || usage
name=$1
check_name "$name"
require_ca
row=$(read_identities | awk -v name="$name" '$1 == name')
[ -n "$row" ] || die "identity '$name' is not in $PKI_IDENTITIES"
read -r name profile sans form _ <<EOF
$row
EOF
before=$(stand_serial "$name")
issue_leaf "$name" "$profile" "$sans" "$LEAF_DAYS" "$PKI_STAND_DIR" "$form"
echo "rotated $name: serial ${before:-none} -> $(stand_serial "$name"); the previous leaf is not revoked yet"
}
cmd_issue() {
[ $# -ge 1 ] || usage
name=$1
@@ -360,6 +412,7 @@ cmd_issue() {
profile=
sans=
days=
not_after=
out=
form=split
while [ $# -gt 0 ]; do
@@ -367,6 +420,7 @@ cmd_issue() {
--profile) [ $# -ge 2 ] || usage; profile=$2; shift 2 ;;
--san) [ $# -ge 2 ] || usage; sans="${sans:+$sans,}$2"; shift 2 ;;
--days) [ $# -ge 2 ] || usage; days=$2; shift 2 ;;
--not-after) [ $# -ge 2 ] || usage; not_after=$2; shift 2 ;;
--out) [ $# -ge 2 ] || usage; out=$2; shift 2 ;;
--bundle) form=bundle; shift ;;
*) usage ;;
@@ -374,26 +428,51 @@ cmd_issue() {
done
case $profile in client | server) ;; *) die "--profile client|server is required" ;; esac
[ -n "$sans" ] || die "--san is required"
case $days in '' | *[!0-9]* | 0) die "--days N (N > 0) is required" ;; esac
if [ -n "$not_after" ]; then
[ -z "$days" ] || die "--days and --not-after exclude each other"
case $not_after in
*[!0-9Z]* | *Z?*) days=bad ;;
??????????????Z) days=$not_after ;;
*) days=bad ;;
esac
[ "$days" != bad ] || die "--not-after YYYYMMDDHHMMSSZ is required"
else
case $days in '' | *[!0-9]* | 0) die "--days N (N > 0) or --not-after is required" ;; esac
fi
[ -n "$out" ] || die "--out is required"
require_ca
issue_leaf "$name" "$profile" "$sans" "$days" "$out" "$form"
}
cmd_revoke() {
[ $# -eq 1 ] || usage
[ $# -ge 1 ] && [ $# -le 2 ] || usage
name=$1
check_name "$name"
superseded=${2:-}
[ -z "$superseded" ] || [ "$superseded" = --superseded ] || usage
require_ca
serials=$(awk -F '\t' -v subj="/CN=$name" '$1 == "V" && $6 == subj { print $4 }' \
"$PKI_OPERATOR_DIR/index.txt")
[ -n "$serials" ] || die "no valid certificate of identity '$name'; nothing changed"
if [ -n "$superseded" ]; then
keep=$(stand_serial "$name")
[ -n "$keep" ] || die "no leaf of identity '$name' in $PKI_STAND_DIR; nothing changed"
serials=$(older_serials "$name" "$keep")
if [ -z "$serials" ]; then
echo "$name: only the stand leaf is valid; nothing to revoke"
return
fi
else
serials=$(awk -F '\t' -v subj="/CN=$name" '$1 == "V" && $6 == subj { print $4 }' \
"$PKI_OPERATOR_DIR/index.txt")
[ -n "$serials" ] || die "no valid certificate of identity '$name'; nothing changed"
fi
for serial in $serials; do
ossl ca -config "$PKI_CONFIG" -revoke "$PKI_OPERATOR_DIR/certs/$serial.pem" \
-crl_reason superseded
echo "revoked $name: serial $serial"
done
gen_crl "$PKI_STAND_DIR"
if [ -z "$superseded" ]; then
echo "the running stand still accepts this leaf: make revoke_identity IDENTITY=$name"
fi
}
mark() {
@@ -423,6 +502,14 @@ cmd_status() {
left=$(days_left "$end")
echo "leaf $name: serial $(cert_serial "$crt"), not after $end ($left days, $(mark "$left" "$RENEW_DAYS")), sha256 $(cert_fingerprint "$crt"), $crt"
done
# прежние leaf: перевыпуск без отзыва, прерванная ротация, разовая проба
awk -F '\t' '$1 == "V" && $6 ~ /^\/CN=[a-z0-9-]+$/ { print substr($6, 5) }' \
"$PKI_OPERATOR_DIR/index.txt" | sort -u | while read -r name; do
count=$(older_serials "$name" "$(stand_serial "$name")" | grep -c . || true)
if [ "$count" -gt 0 ]; then
echo "older leaf $name: $count still valid and not revoked besides the stand leaf"
fi
done
crl="$PKI_STAND_DIR/crl.pem"
if [ ! -f "$crl" ]; then
echo "CRL: MISSING ($crl)"
@@ -441,6 +528,7 @@ check_openssl
case $command in
init) [ $# -eq 0 ] || usage; cmd_init ;;
stand) [ $# -eq 0 ] || usage; cmd_stand ;;
rotate) cmd_rotate "$@" ;;
issue) cmd_issue "$@" ;;
revoke) cmd_revoke "$@" ;;
crl) [ $# -eq 0 ] || usage; require_ca; gen_crl "$PKI_STAND_DIR" ;;