umorpha-boxes/modules/base-acme.sh

256 lines
8.5 KiB
Bash

#!/hint/bash -euE
# Copyright (C) 2023-2024 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
load_module "$(dirname -- "${BASH_SOURCE[0]}")/base-nginx.sh"
packages+=(
certbot
sudo
)
post_install+=(30:acme:post_install) # must be after '20:nginx:post_install' and '10:osi-mk:hostname'
acme:post_install() {
local arg_mountpoint=$1
########################################################################
# First up, we're going to run a separate nginx instance from #
# the main one to handle ACME HTTP-01 challenges. #
########################################################################
# Inspired by https://xyrillian.de/thoughts/posts/how-i-run-certbot.html
cat >"$arg_mountpoint/etc/nginx/nginx-cleartext.conf" <<-'EOF'
# -*- mode: nginx; nginx-indent-level: 4; intent-tabs-mode: nil -*-
pid /run/nginx-cleartext.pid;
worker_processes 1;
error_log /var/log/nginx/cleartext-error.log error;
events {
worker_connections 1024;
}
http {
error_log /var/log/nginx/cleartext-error.http.log error;
access_log /var/log/nginx/cleartext-access.http.log combined;
types_hash_max_size 4096;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
return 301 https://$host$request_uri;
}
location /.well-known/acme-challenge {
root /var/lib/letsencrypt;
default_type "text/plain";
}
}
}
EOF
mkdir -p "$arg_mountpoint/etc/systemd/system/nginx-cleartext.service.d"
echo '[Unit]' >"$arg_mountpoint/etc/systemd/system/nginx-cleartext.service"
ln -sT /usr/lib/systemd/system/nginx.service "$arg_mountpoint/etc/systemd/system/nginx-cleartext.service.d/00-base.conf"
cat >"$arg_mountpoint/etc/systemd/system/nginx-cleartext.service.d/01-base.conf" <<-EOF
[Service]
PIDFile=/run/nginx-cleartext.pid
ExecStart=
ExecStart=/usr/bin/nginx -c /etc/nginx/nginx-cleartext.conf
ExecReload=
ExecReload=/usr/bin/nginx -c /etc/nginx/nginx-cleartext.conf -s reload
EOF
########################################################################
# Set up Certbot+SSL for the system #
########################################################################
# General SSL ##########################################################
curl https://ssl-config.mozilla.org/ffdhe2048.txt > "$arg_mountpoint/etc/ssl/private/dhparam.pem"
ln -sT "../../letsencrypt/live/$(cat "$arg_mountpoint/etc/hostname")" "$arg_mountpoint/etc/ssl/private/myhostname"
# Have Certbot run as non-root #########################################
install -Dm644 /dev/stdin "$arg_mountpoint"/etc/sysusers.d/certbot.conf <<-EOF
u keys - "Certbot" /etc/ssl
EOF
install -Dm644 /dev/stdin "$arg_mountpoint"/etc/tmpfiles.d/certbot.conf <<-EOF
d /etc/letsencrypt 0755 keys keys
d /etc/letsencrypt/archive 0750 keys keys
d /etc/letsencrypt/live 0750 keys keys
d /var/lib/letsencrypt 0755 keys keys
d /var/log/letsencrypt 0700 keys keys
EOF
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-renew.service.d/10-user.conf" <<-EOF
[Service]
User=keys
UMask=0027
EOF
install -Dm644 /dev/stdin "$arg_mountpoint/etc/sudoers.d/10-certbot" <<-EOF
keys ALL=(ALL) NOPASSWD: /etc/ssl/misc/certbot-hook
EOF
# Have certbot-renew.service Depend on the above nginx setup ###########
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-renew.service.d/10-deps.conf" <<-EOF
[Unit]
After=network-online.target nginx-cleartext.service nss-lookup.target
Wants=network-online.target nginx-cleartext.service
EOF
# Add certbot-init.service for at boot #################################
install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-init" <<-'EOF'
#!/usr/bin/env bash
set -eu
if [[ "$(whoami)" != keys ]]; then
echo "$0: this script must be run as user 'keys'" >&2
exit 1
fi
cd /
my_addrs=($(ip addr | awk '$1 == "inet" || $1 == "inet6" { print $2 }' | cut -d/ -f1 | sort -u))
echo 'Local addresses:'
printf ' - %s\n' "${my_addrs[@]}"
# The first name listed should be the canonical host name
domains=(
$(cat /etc/hostname)
)
auto_domains=(
$(find -L /etc/ssl/misc/certbot-domains.d -type f -executable -exec {} \;)
)
for domain in "${auto_domains[@]}"; do
domain_addrs=($(getent ahosts "$domain" | awk '{print $1}' | sort -u))
if [[ -n "$(comm -12 <(printf '%s\n' "${my_addrs[@]}") <(printf '%s\n' "${domain_addrs[@]}"))" ]]; then
domains+=("$domain")
else
echo >&2 "WARNING: skipping ${domain} (${domain_addrs[*]})"
fi
done
cmd=(
certbot certonly
--keep-until-expiring # only renew if nescessary
--non-interactive
--agree-tos
--cert-name "${domains[0]}"
--email "$(whoami)@${domains[0]}"
--webroot -w /var/lib/letsencrypt
)
for domain in "${domains[@]}"; do
cmd+=(-d "$domain")
done
umask 0027
"${cmd[@]}"
EOF
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-init.service" <<-EOF
[Unit]
[Install]
WantedBy=multi-user.target
EOF
install -d "$arg_mountpoint/etc/systemd/system/certbot-init.service.d"
ln -s /usr/lib/systemd/system/certbot-renew.service "$arg_mountpoint"/etc/systemd/system/certbot-init.service.d/00-base.conf
ln -sr "$arg_mountpoint"/etc/systemd/system/certbot-renew.service.d/* "$arg_mountpoint"/etc/systemd/system/certbot-init.service.d/
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-init.service.d/20-command.conf" <<-EOF
[Service]
ExecStart=
ExecStart=/etc/ssl/misc/certbot-init
EOF
# Have certbot-renew.service also use that script ######################
install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-renew" <<-'EOF'
#!/usr/bin/env bash
set -eu
. /etc/ssl/misc/certbot-init
sudo /etc/ssl/misc/certbot-hook
EOF
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-renew.service.d/20-command.conf" <<-EOF
[Service]
ExecStart=
ExecStart=/etc/ssl/misc/certbot-renew
EOF
# Enable ###############################################################
systemctl --root="$arg_mountpoint" enable certbot-renew.timer
systemctl --root="$arg_mountpoint" enable certbot-init.service
########################################################################
# Configure the main nginx to play nice #
########################################################################
install -Dm644 /dev/stdin "$arg_mountpoint"/etc/sysusers.d/nginx.conf <<-EOF
m nginx keys
EOF
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/nginx.service.d/10-certbot.conf" <<-EOF
[Unit]
After=certbot-init.service
EOF
cat >"$arg_mountpoint/etc/nginx/snippets/listen.conf" <<-'EOF'
# -*- mode: nginx -*-
listen 443 ssl http2;
listen [::]:443 ssl http2;
# generated 2023-10-28, Mozilla Guideline v5.7, nginx 1.24.0, OpenSSL 3.1.4, intermediate configuration
# https://ssl-config.mozilla.org/#server=nginx&version=1.24.0&config=intermediate&openssl=3.1.4&guideline=5.7
ssl_certificate /etc/ssl/private/myhostname/fullchain.pem;
ssl_certificate_key /etc/ssl/private/myhostname/privkey.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m; # about 40000 sessions
ssl_session_tickets off;
ssl_dhparam /etc/ssl/private/dhparam.pem;
# intermediate configuration
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;
# HSTS (ngx_http_headers_module is required) (63072000 seconds)
add_header Strict-Transport-Security "max-age=63072000" always;
# OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
# verify chain of trust of OCSP response using Root CA and Intermediate certs
ssl_trusted_certificate /etc/ssl/private/myhostname/chain.pem;
EOF
install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-domains.d/10-nginx-auto" <<-'EOF'
#!/bin/sh
sed -En 's/^\s*server_name\s+([^;]+);\s*$/\1/p' /etc/nginx/sites/*.conf
EOF
install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-hook" <<-'EOF'
#!/bin/sh
if systemctl is-active nginx.service; then
systemctl reload nginx.service
fi
EOF
}