211 lines
7.0 KiB
Bash
211 lines
7.0 KiB
Bash
#!/hint/bash -euE
|
|
# Copyright (C) 2023 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/error-cleartext.log error;
|
|
|
|
events {
|
|
worker_connections 1024;
|
|
}
|
|
|
|
http {
|
|
error_log /var/log/nginx/http.error.log error;
|
|
access_log /var/log/nginx/http.access.log combined;
|
|
|
|
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
|
|
|
|
systemctl --root="$arg_mountpoint" enable nginx-cleartext.service
|
|
|
|
########################################################################
|
|
# 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
|
|
useradd --root="$arg_mountpoint" --system --user-group --no-create-home --home-dir /etc/ssl --shell /usr/bin/nologin keys
|
|
mkdir "$arg_mountpoint"/etc/letsencrypt/{archive,live}
|
|
chmod 0750 "$arg_mountpoint"/etc/letsencrypt/{archive,live}
|
|
arch-chroot -- "$arg_mountpoint" chown -R keys:keys /etc/letsencrypt /var/log/letsencrypt /var/lib/letsencrypt
|
|
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-renew.service.d/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 the normal certbot-renew.service handle changing lists of domains.
|
|
install -Dm755 /dev/stdin "$arg_mountpoint/etc/ssl/misc/certbot-renew" <<-'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 /
|
|
|
|
# The first name listed should be the canonical host name
|
|
domains=(
|
|
$(cat /etc/hostname)
|
|
$(find -L /etc/ssl/misc/certbot-domains.d -type f -executable -exec {} \;)
|
|
)
|
|
|
|
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[@]}"
|
|
sudo /etc/ssl/misc/certbot-hook
|
|
EOF
|
|
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-renew.service.d/domains.conf" <<-EOF
|
|
[Service]
|
|
ExecStart=
|
|
ExecStart=/etc/ssl/misc/certbot-renew
|
|
EOF
|
|
|
|
# Run Certbot
|
|
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/certbot-renew.service.d/deps.conf" <<-EOF
|
|
[Unit]
|
|
After=network-online.target nginx-cleartext.service
|
|
Wants=network-online.target nginx-cleartext.service
|
|
EOF
|
|
systemctl --root="$arg_mountpoint" enable certbot-renew.timer
|
|
|
|
########################################################################
|
|
# Configure the main nginx to play nice #
|
|
########################################################################
|
|
|
|
gpasswd --root="$arg_mountpoint" --add=http keys
|
|
sed -i 's|/var/log/nginx/http\.|/var/log/nginx/https.|' "$arg_mountpoint/etc/nginx/nginx.conf"
|
|
|
|
install -Dm644 /dev/stdin "$arg_mountpoint/etc/systemd/system/nginx.service.d/10-certbot.conf" <<-EOF
|
|
[Unit]
|
|
After=certbot-renew.service
|
|
Wants=certbot-renew.service
|
|
EOF
|
|
|
|
cat >"$arg_mountpoint/etc/nginx/snippets/listen.conf" <<-'EOF'
|
|
# -*- mode: nginx -*-
|
|
listen 443 ssl http2;
|
|
listen [::]:443 ssl http2;
|
|
|
|
error_log /var/log/nginx/https.$server_name.error.log error;
|
|
access_log /var/log/nginx/https.$server_name.access.log combined;
|
|
|
|
# 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
|
|
grep -oE 'server_name\s+([-.a-z0-9]+)\s*;' /etc/nginx/sites/*.conf | sed -E -e 's/server_name\s+//' -e 's/\s*;//'
|
|
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
|
|
}
|