#!/bin/bash # Входит в OpenBao сертификатом сервиса и кладёт пароли в /tmp/messenger.env. # Токен на диск не пишется. Root token здесь не используется. set -eu role="${SECRET_ROLE:?SECRET_ROLE is required}" attempt=0 while true; do code="$(curl -s -o /dev/null -w '%{http_code}' --cacert /certs/ca.crt https://openbao:8200/v1/sys/health || true)" if [ "$code" = "200" ]; then break fi attempt=$((attempt + 1)) if [ "$attempt" -gt 60 ]; then echo "OpenBao is not unsealed (last health HTTP $code)" >&2 exit 1 fi sleep 2 done login="$(curl -fsS --cert /certs/client.crt --key /certs/client.key --cacert /certs/ca.crt \ --request POST --data "{\"name\":\"$role\"}" \ https://openbao:8200/v1/auth/cert/login)" token="$(printf '%s' "$login" | jq -r '.auth.client_token')" if [ -z "$token" ] || [ "$token" = "null" ]; then echo "OpenBao cert login failed for role $role" >&2 exit 1 fi body="$(curl -fsS --cacert /certs/ca.crt -H "X-Vault-Token: $token" \ "https://openbao:8200/v1/secret/data/messenger/$role")" printf '%s' "$body" | jq -er '.data.data | to_entries[] | "export \(.key)=\(.value)"' > /tmp/messenger.env chmod 600 /tmp/messenger.env unset token login body