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