Skip to content

Instantly share code, notes, and snippets.

@selivan
Last active December 1, 2020 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selivan/a65b2c8dfe1a2563b50d822727fa7d0f to your computer and use it in GitHub Desktop.
Save selivan/a65b2c8dfe1a2563b50d822727fa7d0f to your computer and use it in GitHub Desktop.
Letsencrypt hook to generate combined fullchain+privkey certificates for software like Haproxy
#!/bin/bash
# SAVE TO /etc/letsencrypt/renewal-hooks/post/generate-bundle-certs.sh
# chmod a+x /etc/letsencrypt/renewal-hooks/post/generate-bundle-certs.sh
find /etc/letsencrypt/live -mindepth 1 -maxdepth 1 -type d -print0 | while IFS='' read -d $'\0' dir; do
# Update only if necessary
test -e "$dir/fullchain.pem" -a \
-e "$dir/privkey.pem" -a \
-e "$dir/fullchain_and_privkey.pem" && \
cmp <(cat "$dir/fullchain.pem" "$dir/privkey.pem") <(cat "$dir/fullchain_and_privkey.pem")
if [ $? -eq 0 ]; then
echo "already updated: $dir/fullchain_and_privkey.pem"
continue
else
echo "updating: $dir/fullchain_and_privkey.pem"
# Atomic update
cat "$dir/fullchain.pem" "$dir/privkey.pem" > "$dir/fullchain_and_privkey.pem.new"
mv -f "$dir/fullchain_and_privkey.pem.new" "$dir/fullchain_and_privkey.pem"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment