containers-certbot/certbotd.sh

116 lines
3.4 KiB
Bash
Raw Permalink Normal View History

2019-09-10 23:17:04 +00:00
#!/bin/sh
2022-09-10 21:04:08 +00:00
if test -z "${NODES}" && test -z "${SINGLE_NODE}"; then
2022-03-19 13:02:18 +00:00
echo "The env var NODES is empty, if you don't want to synchronize to other servers, set SINGLE_NODE=true" >&2
exit 1
fi
2022-03-08 17:40:02 +00:00
lock=/tmp/certbot.lck
updated=/tmp/certbot.updated
2022-03-08 17:40:02 +00:00
ensure() {
test -n "$1" && echo "$1 received, exiting gracefully..."
rm -f "${lock}"
test -f "${updated}" || exit 0
rm -f "${updated}"
2022-03-08 17:40:02 +00:00
# Fix permissions, users in group ssl have read access
find /etc/letsencrypt -type d | xargs -r chmod 2750
find /etc/letsencrypt -type f | xargs -r chmod 640
chgrp -R ssl /etc/letsencrypt
2022-03-19 13:02:18 +00:00
${SINGLE_NODE:-false} && exit 0
2022-03-08 17:40:02 +00:00
# Push certificates to nodes, we use SSH as a secure transport
# but this means we're synchronizing from container to host which is
# awkward. A restricted rsync treats / as the remote location for the
# certificates.
for NODE in ${NODES}; do
rsync -avHAXL --delete-after /etc/letsencrypt/live/ ${NODE}/
2022-03-08 17:40:02 +00:00
done
}
2022-03-19 12:56:54 +00:00
for SIG in TERM QUIT INT HUP; do
2022-03-08 17:40:02 +00:00
trap "ensure ${SIG}" ${SIG}
done
set -e
2019-09-10 23:17:04 +00:00
case $1 in
# Renew certificates, trust in certbot's algorithms
2022-03-15 21:55:31 +00:00
renew)
/usr/bin/certbot renew --quiet --agree-tos || true
touch "${updated}"
2022-03-15 21:55:31 +00:00
;;
2019-09-10 23:17:04 +00:00
bootstrap)
test -d "/etc/letsencrypt/live/${SUTTY}" && exit 0
# Get a single certificate for the whole domain
/usr/bin/certbot \
2023-12-20 14:21:18 +00:00
certonly \
--non-interactive \
--authenticator "dns-standalone" \
--email "certbot@${SUTTY}" \
--agree-tos \
-d "${SUTTY}" \
-d "*.${SUTTY}" \
-d "*.testing.${SUTTY}"
touch "${updated}"
2022-09-10 19:53:29 +00:00
;;
prune)
comm -13 <(realpath /etc/letsencrypt/live/*/*.pem | sort) <(find /etc/letsencrypt/archive/ -name "*.pem" | sort) | xargs rm -v
touch "${updated}"
;;
2019-09-10 23:17:04 +00:00
# Generate certificates
*)
2022-03-08 17:40:02 +00:00
# Only one instance can run at a time
2022-03-19 13:00:10 +00:00
if test -f "${lock}" ; then
echo "There's a certbotd instance already running, doing nothing..." >&2
echo "If the problem persists, you may need to remove ${lock} manually." >&2
exit 1
fi
2022-03-08 17:40:02 +00:00
touch "${lock}"
2019-09-10 23:17:04 +00:00
# Save headers here
headers=/tmp/headers
# Gets ETag from previous headers
test -f "${headers}" \
&& etag="$(grep "^ Etag: " "${headers}" | cut -d : -f 2)"
# Get site list from the API and transform to a list. Save headers
# for next run. Use ETag to avoid running when nothing changed
wget --user="${HTTP_BASIC_USER}" --password="${HTTP_BASIC_PASSWORD}" \
--header="If-None-Match:${etag}" -qSO - \
"https://api.${SUTTY}/v1/sites.json" \
2>"${headers}" \
| jq --raw-output .[] \
| while read domain; do
# Skip already existing domains
test -d "/etc/letsencrypt/live/${domain}" && continue
2019-09-10 23:17:04 +00:00
# Ignore non local domains
2023-04-24 16:35:07 +00:00
if ! nslookup "${domain}" 8.8.8.8 | grep -qE "(${SUTTY_ADDRESSES// /|})" ; then
echo "${domain} is not configured to any Sutty node or DNS records are still cached, ignoring for now"
continue
fi
2019-09-10 23:17:04 +00:00
# Get the certificate for the domain, the webserver will need
# access to this directory
/usr/bin/certbot certonly --email "certbot@${SUTTY}" \
2022-03-19 13:03:06 +00:00
-n \
--webroot \
--agree-tos \
--webroot-path /var/lib/letsencrypt \
-d "${domain}"
touch "${updated}"
2019-09-10 23:17:04 +00:00
done
esac
2022-03-08 17:40:02 +00:00
ensure