reconcile-certs.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/bash
  2. # Issue a certificate for every registered hostname that lacks one.
  3. #
  4. # WHY THIS EXISTS: adding a hostname in the web UI used to be half a job - the
  5. # name resolved, the registry served it, and TLS refused the handshake because
  6. # no certificate existed, which an operator experiences as "I added a host and
  7. # it does not work" with nothing saying why.
  8. #
  9. # WHY NOT AN ACME CLIENT INSIDE VECTORAPI, which was asked and is the obvious
  10. # idea: an ACME bug in our own process is a live outage on EVERY hostname at
  11. # once, renewal scheduling is somebody else's well-tested code, and an account
  12. # key is a credential we would rather not hold. Certbot already runs here and
  13. # already renews on a timer. What was missing was never the protocol - it was
  14. # nobody connecting "a hostname was registered" to "ask for a certificate".
  15. # This is that connection, and it is 60 lines rather than a protocol.
  16. #
  17. # Idempotent: a hostname that already has a live certificate is skipped, so it
  18. # is safe to run on a timer and safe to run by hand twice.
  19. set -u
  20. BASE="${VECTORAPI_BASE:-https://127.0.0.1}"
  21. HOSTHDR="${VECTORAPI_ADMIN_HOST:-sb-prod-01.smartbotics.ai}"
  22. CERTDIR="${VECTORAPI_CERT_DIR:-/etc/smartbotic-vectorapi/tls}"
  23. ENVFILE="${VECTORAPI_ENV:-/etc/smartbotic-vectorapi/vectorapi.env}"
  24. KEY=$(grep -oE '^SMARTBOTIC_VECTORAPI_KEY=.*' "$ENVFILE" 2>/dev/null | cut -d= -f2)
  25. if [ -z "$KEY" ]; then echo "no admin key in $ENVFILE" >&2; exit 1; fi
  26. api() { curl -s -k --resolve "$HOSTHDR:443:127.0.0.1" -H "Authorization: Bearer $KEY" "$@"; }
  27. projects=$(api "https://$HOSTHDR/api/v1/projects" | python3 -c 'import json,sys; print(" ".join(json.load(sys.stdin).get("projects",[])))' 2>/dev/null)
  28. if [ -z "$projects" ]; then echo "could not list projects" >&2; exit 1; fi
  29. # The deployment's own names count too - they need certificates like any other.
  30. wanted=$(api "https://$HOSTHDR/api/v1/settings" | python3 -c '
  31. import json,sys
  32. d=json.load(sys.stdin)
  33. out=[d.get("admin_hostname","")]+list(d.get("global_hostnames",[]) or [])
  34. print(" ".join(h for h in out if h))' 2>/dev/null)
  35. for p in $projects; do
  36. hs=$(api "https://$HOSTHDR/api/v1/projects/$p/settings/hostnames" | python3 -c '
  37. import json,sys
  38. try: print(" ".join(h["host"] for h in json.load(sys.stdin).get("hostnames",[])))
  39. except Exception: pass' 2>/dev/null)
  40. wanted="$wanted $hs"
  41. done
  42. issued=0; skipped=0; failed=0
  43. for h in $wanted; do
  44. [ -z "$h" ] && continue
  45. if [ -s "$CERTDIR/$h.crt" ] && [ -s "$CERTDIR/$h.key" ]; then
  46. skipped=$((skipped+1)); continue
  47. fi
  48. echo " issuing for $h"
  49. # --standalone needs port 80 free. vectorapi deliberately does NOT listen
  50. # there, precisely so renewals can bind it.
  51. if certbot certonly --standalone --non-interactive --agree-tos \
  52. --register-unsafely-without-email --quiet -d "$h" 2>/tmp/rc.err; then
  53. install -m640 -o root -g smartbotic-vectorapi \
  54. "/etc/letsencrypt/live/$h/fullchain.pem" "$CERTDIR/$h.crt"
  55. install -m640 -o root -g smartbotic-vectorapi \
  56. "/etc/letsencrypt/live/$h/privkey.pem" "$CERTDIR/$h.key"
  57. issued=$((issued+1))
  58. else
  59. # NAMED, not counted silently. A hostname whose certificate could not be
  60. # issued refuses every handshake, and the operator needs to know WHICH.
  61. echo " FAILED for $h: $(head -2 /tmp/rc.err | tr '\n' ' ')" >&2
  62. failed=$((failed+1))
  63. fi
  64. done
  65. rm -f /tmp/rc.err
  66. echo " certificates: $issued issued, $skipped already present, $failed failed"
  67. [ "$failed" -gt 0 ] && exit 1
  68. exit 0