bootstrap.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/sh
  2. # This script is intended to run inside the bootstrap container. It
  3. # should work outside, but that use case is not tested.
  4. . bootstrap.env
  5. sed -ri "s/%hostname%/${MAIL_DOMAIN}/" /etc/nginx/conf.d/challenge.conf
  6. nginx
  7. # TODO Expose these in the configuration utility
  8. RSA_KEY_SIZE=4096
  9. PREFIX="/etc/letsencrypt"
  10. SELF_SIGNED_CERTIFICATE_TTL=365
  11. echo "Starting bootstrap"
  12. obtain_certificates () {
  13. DOMAIN="$1"
  14. if [ ! -e "${PREFIX}/live/${DOMAIN}" ] || [ ! -e "${PREFIX}/live/ssl-dhparams.pem" ];then
  15. echo "### Downloading recommended TLS parameters ..."
  16. mkdir -p "${PREFIX}/live/${DOMAIN}"
  17. curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "${PREFIX}/options-ssl-nginx.conf"
  18. curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem >"${PREFIX}/ssl-dhparams.pem"
  19. if [ ${SIGNED} -eq 0 ]; then
  20. echo "### Creating self signed certificate for ${DOMAIN} ..."
  21. openssl req -x509 -nodes -newkey "rsa:${RSA_KEY_SIZE}" -days "${SELF_SIGNED_CERTIFICATE_TTL}" \
  22. -keyout "${PREFIX}/live/${DOMAIN}/privkey.pem" \
  23. -out "${PREFIX}/live/${DOMAIN}/fullchain.pem" -subj "/CN=${DOMAIN}"
  24. else
  25. echo "### Creating dummy certificate for ${DOMAIN} ..."
  26. openssl req -x509 -nodes -newkey rsa:1024 -days 1 \
  27. -keyout "${PREFIX}/live/${DOMAIN}/privkey.pem" \
  28. -out "${PREFIX}/live/${DOMAIN}/fullchain.pem" -subj '/CN=localhost'
  29. nginx -s reload
  30. rm -Rf "${PREFIX}/live/${DOMAIN}"
  31. rm -Rf "${PREFIX}/archive/${DOMAIN}"
  32. rm -Rf "${PREFIX}/renewal/${DOMAIN}.conf"
  33. echo "### Requesting Let's Encrypt certificate for ${DOMAIN} ..."
  34. # Ask Let's Encrypt to create certificates, if challenge passes
  35. certbot certonly --webroot -w "/var/www/certbot" \
  36. --email "${EMAIL}" \
  37. -d "${DOMAIN}" \
  38. --non-interactive \
  39. --rsa-key-size "${RSA_KEY_SIZE}" \
  40. --agree-tos \
  41. --force-renewal
  42. fi
  43. else
  44. echo "Certificate related files exists, exiting"
  45. fi
  46. }
  47. obtain_certificates "${WEB_DOMAIN}"
  48. obtain_certificates "${MAIL_DOMAIN}"