forked from mtusi2026/mtusi2026lab7
на оценку 4
This commit is contained in:
Executable
+61
@@ -0,0 +1,61 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$project_root"
|
||||
|
||||
state_file="$project_root/.operator/openbao-init.json"
|
||||
password_file="$project_root/secrets/broker_worker_password.txt"
|
||||
[ -s "$state_file" ] || { printf '%s\n' "OpenBao operator state is required" >&2; exit 1; }
|
||||
[ -s "$password_file" ] || { printf '%s\n' "Broker worker password is required" >&2; exit 1; }
|
||||
|
||||
old_password=$(cat "$password_file")
|
||||
new_password=$(openssl rand -hex 24)
|
||||
root_token=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1], encoding="utf-8"))["root_token"])' "$state_file")
|
||||
app_image_before=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-app:latest)
|
||||
completed=false
|
||||
|
||||
restore_old_secret() {
|
||||
printf '%s' "$old_password" > "$password_file"
|
||||
chmod 600 "$password_file"
|
||||
./scripts/init-secrets.sh >/dev/null
|
||||
docker compose exec -T -e BAO_TOKEN="$root_token" openbao \
|
||||
bao kv put secret/worker @/bootstrap/worker.json >/dev/null 2>&1 || true
|
||||
docker compose exec -T broker rabbitmqctl change_password worker "$old_password" >/dev/null 2>&1 || true
|
||||
docker compose up -d --no-deps --no-build --force-recreate worker >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
if [ "$completed" != true ]; then
|
||||
restore_old_secret
|
||||
fi
|
||||
}
|
||||
trap cleanup EXIT HUP INT TERM
|
||||
|
||||
printf '%s' "$new_password" > "$password_file"
|
||||
chmod 600 "$password_file"
|
||||
./scripts/init-secrets.sh >/dev/null
|
||||
docker compose exec -T -e BAO_TOKEN="$root_token" openbao \
|
||||
bao kv put secret/worker @/bootstrap/worker.json >/dev/null
|
||||
docker compose exec -T broker rabbitmqctl change_password worker "$new_password" >/dev/null
|
||||
|
||||
if docker compose exec -T broker rabbitmqctl authenticate_user worker "$old_password" >/dev/null 2>&1; then
|
||||
printf '%s\n' "Old broker secret is still accepted" >&2
|
||||
exit 1
|
||||
fi
|
||||
docker compose exec -T broker rabbitmqctl authenticate_user worker "$new_password" >/dev/null
|
||||
|
||||
docker compose up -d --no-deps --no-build --force-recreate worker >/dev/null
|
||||
sleep 4
|
||||
[ -n "$(docker compose ps --status running -q worker)" ] || {
|
||||
printf '%s\n' "Worker did not restart with the rotated secret" >&2
|
||||
exit 1
|
||||
}
|
||||
app_image_after=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-app:latest)
|
||||
[ "$app_image_before" = "$app_image_after" ] || {
|
||||
printf '%s\n' "Application image changed during secret rotation" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
completed=true
|
||||
printf '%s\n' "PASS: worker broker secret rotated, old credential revoked, image unchanged"
|
||||
Reference in New Issue
Block a user