Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/scripts/lib/hesabix_letsencrypt.sh
Babak Alizadeh 1363e21c43 Fix update branch fallback, HTTPS redirects, and production MCP URL.
hesabix -update no longer fails when BRANCH=main but the remote only has master. Nginx redirects HTTP to HTTPS when Let's Encrypt certs exist, and production installs get MCP_PUBLIC_BASE_URL instead of a pip retry on security validation.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-28 02:08:40 +03:30

74 lines
2.2 KiB
Shell

#!/usr/bin/env bash
# Shared Let's Encrypt helpers for Hesabix deploy, update, nginx, and SSL management.
# Returns live cert directory path on stdout, or non-zero if none found.
# Optional env: SSL_LETSENCRYPT_LIVE=/etc/letsencrypt/live/arc.example.com (SAN must include domain)
hesabix_pick_letsencrypt_live_for_domain() {
local dom="$1"
[[ -z "${dom}" ]] && return 1
if [[ -n "${SSL_LETSENCRYPT_LIVE:-}" ]] && [[ -f "${SSL_LETSENCRYPT_LIVE}/fullchain.pem" ]]; then
if openssl x509 -in "${SSL_LETSENCRYPT_LIVE}/fullchain.pem" -noout -text 2>/dev/null | grep -q "DNS:${dom}"; then
echo "${SSL_LETSENCRYPT_LIVE}"
return 0
fi
fi
if [[ -f "/etc/letsencrypt/live/${dom}/fullchain.pem" ]]; then
echo "/etc/letsencrypt/live/${dom}"
return 0
fi
local d
for d in /etc/letsencrypt/live/*; do
[[ -f "${d}/fullchain.pem" ]] || continue
if openssl x509 -in "${d}/fullchain.pem" -noout -text 2>/dev/null | grep -q "DNS:${dom}"; then
echo "${d}"
return 0
fi
done
return 1
}
hesabix_nginx_ssl_block_for_live_dir() {
local live="$1"
[[ -z "${live}" ]] || [[ ! -f "${live}/fullchain.pem" ]] && return 0
echo " listen 443 ssl;"
echo " ssl_certificate ${live}/fullchain.pem;"
echo " ssl_certificate_key ${live}/privkey.pem;"
if [[ -f /etc/letsencrypt/options-ssl-nginx.conf ]]; then
echo " include /etc/letsencrypt/options-ssl-nginx.conf;"
fi
if [[ -f /etc/letsencrypt/ssl-dhparams.pem ]]; then
echo " ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;"
fi
}
# HTTP vhost that keeps ACME challenges on :80 and redirects everything else to HTTPS.
hesabix_nginx_http_to_https_redirect_server() {
local domain="$1"
[[ -z "${domain}" ]] && return 0
cat <<EOF
server {
listen 80;
server_name ${domain};
location /.well-known/acme-challenge/ {
root /var/www/html;
}
location / {
return 301 https://\$host\$request_uri;
}
}
EOF
}
hesabix_domain_has_ssl() {
local dom="$1"
hesabix_pick_letsencrypt_live_for_domain "${dom}" >/dev/null 2>&1
}
hesabix_resolve_ui_public_scheme() {
if hesabix_pick_letsencrypt_live_for_domain "${UI_DOMAIN:-}" >/dev/null 2>&1; then
printf '%s' "https"
return 0
fi
printf '%s' "http"
}