iac/nixos/hosts/asxpio-services/xmpp-cert-install.sh
Sergei Poljanski 422eb3bc6e
nixos: bucket replica and XMPP certificate sync
s3-replicate is the same rclone sync with the same delete limit, run by
the rclone package instead of its image.

The certificate push keeps its shape: ops reads Traefik's acme.json and
sends each certificate over SSH with a key that may run one command on
services, from ops only. The key is now a sops secret with its public
half in the config, instead of being generated on the host, so a
rebuilt pair works without a manual step. Tried on the rehearsal pair
with a throwaway certificate; any other command is refused.

Assisted-by: Claude:fable-5-2
2026-11-16 23:27:47 +04:00

31 lines
1.3 KiB
Shell

# Forced command of the xmpp-cert-sync key from asxpio-ops: read a
# certificate chain + key on stdin, check it, install it as
# certs/<name>.pem for ejabberd (which picks the matching file per
# domain) and reload.
set -eu
umask 022
name=${SSH_ORIGINAL_COMMAND:-}
case "$name" in
asxp.io|*.asxp.io) ;;
*) echo "refusing certificate name '$name'" >&2; exit 1 ;;
esac
case "$name" in *[!a-z0-9.-]*) echo "bad name" >&2; exit 1 ;; esac
dir=/opt/xmpp/state/certs
tmp=$(mktemp "$dir/.incoming.XXXXXX")
trap 'rm -f "$tmp"' EXIT
head -c 65536 > "$tmp"
# Valid for at least another day, for this name, with its matching key
openssl x509 -in "$tmp" -noout -checkend 86400 >/dev/null
openssl x509 -in "$tmp" -noout -checkhost "$name" | grep -q "does match"
cert_pub=$(openssl x509 -in "$tmp" -noout -pubkey | sha256sum)
key_pub=$(openssl pkey -in "$tmp" -pubout | sha256sum)
[ "$cert_pub" = "$key_pub" ] || { echo "key does not match certificate" >&2; exit 1; }
chmod 0644 "$tmp"
mv "$tmp" "$dir/$name.pem"
trap - EXIT
# Not running during a restore or on a rehearsal host: ejabberd reads the
# file at its next start
if podman container exists xmpp-ejabberd; then
podman exec xmpp-ejabberd ejabberdctl reload_config
fi
logger -t xmpp-cert-install "installed $name.pem, $(openssl x509 -in "$dir/$name.pem" -noout -enddate)"