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
+59 -7
View File
@@ -23,6 +23,12 @@ set -eu
# (откат без checkout);
# ROLLOUT_MIGRATE=0 — без миграций: откат на прежний образ, его миграции
# не знают новой ревизии, а схема только расширялась;
# ROLLOUT_BUILD=0 — без сборки (change rotation-revocation, design, решение
# 6): новые реплики на образе работающих, чтобы прочитать текущие файлы
# TLS, CRL и секреты и закрыть соединения прежних. Тег сервиса собран
# заново, и образ реплик другой — код 2;
# ROLLOUT_CHECK=1 — только предусловия (код 0 / 2): ни контейнеры, ни слоты
# не меняются;
# ROLLOUT_READY_TIMEOUT (60) — срок готовности новой реплики, с;
# ROLLOUT_DRAIN_TIMEOUT (90) — срок дренажа старой, с: больше timeout
# server пути загрузки и наибольшей длительности загрузки (LB 30 с,
@@ -46,6 +52,8 @@ ready_timeout="${ROLLOUT_READY_TIMEOUT:-60}"
drain_timeout="${ROLLOUT_DRAIN_TIMEOUT:-90}"
commit="${ROLLOUT_COMMIT:-}"
migrate="${ROLLOUT_MIGRATE:-1}"
build="${ROLLOUT_BUILD:-1}"
check="${ROLLOUT_CHECK:-0}"
# состояние текущей замены для trap (решение 5): "" — реплики не тронуты,
# starting — новая запущена и ещё не UP, replacing — новая UP, старая
@@ -263,6 +271,13 @@ case "$migrate" in
0 | 1) ;;
*) fail 2 "ROLLOUT_MIGRATE must be 0 or 1" ;;
esac
case "$build$check" in
00 | 01 | 10 | 11) ;;
*) fail 2 "ROLLOUT_BUILD and ROLLOUT_CHECK must be 0 or 1" ;;
esac
if [ "$build" = 0 ] && [ -n "$commit" ]; then
fail 2 "ROLLOUT_BUILD=0 and ROLLOUT_COMMIT exclude each other"
fi
if [ -n "$commit" ] && ! git rev-parse --verify --quiet "$commit^{commit}" >/dev/null; then
fail 2 "ROLLOUT_COMMIT $commit is not a commit of this repository"
fi
@@ -296,25 +311,62 @@ for id in $old; do
fi
done
image=$(docker inspect -f '{{.Config.Image}}' "$(printf '%s\n' "$old" | head -n 1)")
# без сборки образ новых реплик — тег сервиса: он обязан быть образом
# работающих, иначе замена молча выкатила бы другой код. Сначала манифест
# платформы против метки compose (docker.md); другой манифест — ещё не другой
# код: сборка другим проектом compose (make failure_checks) меняет только его
# метки в образе, поэтому тогда сравниваются файлы (пути, размеры, время)
if [ "$build" = 0 ]; then
platform=$(docker version -f '{{.Server.Os}}/{{.Server.Arch}}')
tag_image=$(docker image inspect --platform "$platform" -f '{{.Id}}' "$image" 2>/dev/null) ||
fail 2 "no image $image for $platform"
files='find /app /usr/local -type f ! -path /app/config.toml -printf "%p %s %T@\n" | sort | sha256sum'
tag_files=""
for id in $old; do
running=$(docker inspect -f '{{index .Config.Labels "com.docker.compose.image"}}' "$id")
if [ "$running" = "$tag_image" ]; then
continue
fi
if [ -z "$tag_files" ]; then
tag_files=$(docker run --rm --network none --no-healthcheck --entrypoint sh "$image" -c "$files")
fi
if [ "$(docker exec "$id" sh -c "$files")" != "$tag_files" ]; then
fail 2 "replica $(short "$id") runs another build of $image: run make run_docker or make rolling_update first"
fi
done
fi
if [ "$check" = 1 ]; then
log "preconditions hold: $replicas replicas UP, nothing changed"
exit 0
fi
ids=""
for id in $old; do
ids="$ids $(short "$id")"
done
log "start: $replicas replicas${ids}, image $image from ${commit:-working tree}"
if [ "$build" = 1 ]; then
source="from ${commit:-working tree}"
else
source="unchanged (ROLLOUT_BUILD=0)"
fi
log "start: $replicas replicas${ids}, image $image $source"
began=$(now)
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
# 1. сборка: тот же тег сервиса, работающие реплики остаются на прежнем
started=$(now)
if [ -n "$commit" ]; then
# только файлы commit, рабочее дерево и HEAD не меняются (решение 6)
git archive --format=tar "$commit" | docker build -q -t "$image" - >/dev/null
if [ "$build" = 1 ]; then
started=$(now)
if [ -n "$commit" ]; then
# только файлы commit, рабочее дерево и HEAD не меняются (решение 6)
git archive --format=tar "$commit" | docker build -q -t "$image" - >/dev/null
else
docker compose build api
fi
log "build done in $(($(now) - started))s"
else
docker compose build api
log "build skipped (ROLLOUT_BUILD=0)"
fi
log "build done in $(($(now) - started))s"
# 2. миграции один раз, до первой новой реплики (спек db-migrations).
# Контейнер сервиса migrate пересоздаётся на новом образе, а не одноразовый