bootstrap.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. # TODO: Add mail domain when implemented
  6. rm -f /etc/nginx/conf.d/default.conf
  7. sed -ri "s/%hostname%/${WEB_DOMAIN}/" /etc/nginx/conf.d/challenge.conf
  8. nginx
  9. # TODO Expose these in the configuration utility
  10. RSA_KEY_SIZE=4096
  11. PREFIX="/etc/letsencrypt"
  12. SELF_SIGNED_CERTIFICATE_TTL=365
  13. echo "Starting bootstrap"
  14. obtain_certificates () {
  15. DOMAIN="$1"
  16. if [ ! -e "${PREFIX}/live/${DOMAIN}" ] || [ ! -e "${PREFIX}/live/ssl-dhparams.pem" ];then
  17. echo "### Downloading recommended TLS parameters ..."
  18. mkdir -p "${PREFIX}/live/${DOMAIN}"
  19. 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"
  20. curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem >"${PREFIX}/ssl-dhparams.pem"
  21. if [ ${SIGNED} -eq 0 ]; then
  22. echo "### Creating self signed certificate for ${DOMAIN} ..."
  23. openssl req -x509 -nodes -newkey "rsa:${RSA_KEY_SIZE}" -days "${SELF_SIGNED_CERTIFICATE_TTL}" \
  24. -keyout "${PREFIX}/live/${DOMAIN}/privkey.pem" \
  25. -out "${PREFIX}/live/${DOMAIN}/fullchain.pem" -subj "/CN=${DOMAIN}"
  26. else
  27. echo "### Creating dummy certificate for ${DOMAIN} ..."
  28. openssl req -x509 -nodes -newkey rsa:1024 -days 1 \
  29. -keyout "${PREFIX}/live/${DOMAIN}/privkey.pem" \
  30. -out "${PREFIX}/live/${DOMAIN}/fullchain.pem" -subj '/CN=localhost'
  31. nginx -s reload
  32. rm -Rf "${PREFIX}/live/${DOMAIN}"
  33. rm -Rf "${PREFIX}/archive/${DOMAIN}"
  34. rm -Rf "${PREFIX}/renewal/${DOMAIN}.conf"
  35. echo "### Requesting Let's Encrypt certificate for ${DOMAIN} ..."
  36. # Ask Let's Encrypt to create certificates, if challenge passes
  37. certbot certonly --webroot -w "/var/www/certbot" \
  38. --email "${EMAIL}" \
  39. -d "${DOMAIN}" \
  40. --non-interactive \
  41. --rsa-key-size "${RSA_KEY_SIZE}" \
  42. --agree-tos \
  43. --force-renewal
  44. fi
  45. else
  46. echo "Certificate related files exists, exiting"
  47. fi
  48. }
  49. obtain_certificates "${WEB_DOMAIN}"
  50. #TODO: Uncomment when implemented (:
  51. #obtain_certificates "${MAIL_DOMAIN}"