71 lines
3.0 KiB
Bash
71 lines
3.0 KiB
Bash
#!/usr/bin/env bash
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
# Provision TLS on external port 8850 using an already routed HTTP-01 webroot.
|
|
# Never stops or reconfigures the ROBUST process that owns port 80.
|
|
set -Eeuo pipefail
|
|
if [[ $# -ne 3 ]]; then
|
|
echo "Usage: sudo bash $0 <sysapi-fqdn> <acme-email> <public-http01-webroot>" >&2
|
|
exit 2
|
|
fi
|
|
domain="$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')"
|
|
email="$2"
|
|
webroot="$3"
|
|
[[ "$EUID" -eq 0 ]] || { echo "Root required" >&2; exit 2; }
|
|
[[ "$domain" =~ ^[a-z0-9][a-z0-9.-]{2,245}[a-z0-9]$ ]] &&
|
|
[[ "$domain" == *.* ]] && [[ "$domain" != *..* ]] ||
|
|
{ echo "Invalid FQDN" >&2; exit 2; }
|
|
[[ "$email" =~ ^[^[:space:]@]+@[^[:space:]@]+\.[^[:space:]@]+$ ]] ||
|
|
{ echo "Valid ACME contact email required" >&2; exit 2; }
|
|
[[ -d "$webroot" && ! -L "$webroot" ]] ||
|
|
{ echo "ACME webroot missing or symlink" >&2; exit 2; }
|
|
for binary in certbot nginx curl openssl; do
|
|
command -v "$binary" >/dev/null ||
|
|
{ echo "Missing $binary; install it first" >&2; exit 2; }
|
|
done
|
|
challenge_dir="$webroot/.well-known/acme-challenge"
|
|
install -d -m 0755 "$challenge_dir"
|
|
nonce="$(openssl rand -hex 16)"
|
|
challenge="$challenge_dir/ogl-sysapi-check-$nonce"
|
|
trap 'rm -f "$challenge"' EXIT
|
|
printf '%s' "$nonce" > "$challenge"
|
|
public="$(curl -fsSL --max-time 12 \
|
|
"http://$domain/.well-known/acme-challenge/ogl-sysapi-check-$nonce" || true)"
|
|
[[ "$public" == "$nonce" ]] ||
|
|
{ echo "HTTP-01 cannot reach webroot through public TCP/80 for $domain." >&2
|
|
echo "Route ACME challenges via the public gateway or use DNS-01." >&2
|
|
exit 3; }
|
|
certbot certonly --webroot -w "$webroot" -d "$domain" \
|
|
--agree-tos --non-interactive --email "$email" --keep-until-expiring
|
|
cert="/etc/letsencrypt/live/$domain/fullchain.pem"
|
|
key="/etc/letsencrypt/live/$domain/privkey.pem"
|
|
[[ -s "$cert" && -s "$key" ]] ||
|
|
{ echo "Missing freshly issued certificate" >&2; exit 4; }
|
|
template="$(dirname "$(realpath "$0")")/sys-api-nginx-8850.conf.example"
|
|
[[ -r "$template" ]] || { echo "Nginx template missing" >&2; exit 4; }
|
|
target=/etc/nginx/conf.d/ogl-sys-api-8850.conf
|
|
backup=""
|
|
if [[ -e "$target" ]]; then
|
|
backup="$target.backup-$(date -u +%Y%m%d%H%M%S)"
|
|
cp -p "$target" "$backup"
|
|
fi
|
|
sed "s/YOUR_DOMAIN/$domain/g" "$template" > "$target"
|
|
if ! nginx -t; then
|
|
if [[ -n "$backup" ]]; then cp -p "$backup" "$target"; else rm -f "$target"; fi
|
|
nginx -t || true
|
|
echo "Nginx config invalid; reverted" >&2
|
|
exit 5
|
|
fi
|
|
install -d -m 0755 /etc/letsencrypt/renewal-hooks/deploy
|
|
hook=/etc/letsencrypt/renewal-hooks/deploy/ogl-sysapi-nginx-reload
|
|
printf '#!/bin/sh\nsystemctl reload nginx\n' > "$hook"
|
|
chmod 0755 "$hook"
|
|
if systemctl list-unit-files certbot.timer >/dev/null 2>&1; then
|
|
systemctl enable --now certbot.timer
|
|
else
|
|
echo "Configure an ACME renewal timer for this certificate." >&2
|
|
fi
|
|
systemctl reload nginx || systemctl start nginx
|
|
echo "HTTPS proxy ready at https://$domain:8850/"
|
|
echo "Set [SysApi] PublicOrigin accordingly and verify public NAT/firewall."
|
|
echo "Port 8851 must remain private; renewals reload Nginx."
|