2020-05-05 02:23:55 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-05-10 22:33:03 +01:00
|
|
|
. bootstrap.env
|
|
|
|
|
|
|
|
sed -ri "s/%hostname%/${domain}/" /etc/nginx/conf.d/challenge.conf
|
2020-05-05 02:23:55 +01:00
|
|
|
|
|
|
|
nginx
|
|
|
|
|
|
|
|
rsa_key_size=4096
|
|
|
|
certbot_path="/var/www/certbot"
|
|
|
|
lets_path="/etc/letsencrypt"
|
|
|
|
|
|
|
|
echo "Starting bootstrap"
|
|
|
|
|
2020-05-10 22:33:03 +01:00
|
|
|
if [ ! -e "$lets_path/live//options-ssl-nginx.conf" ] || [ ! -e "$lets_path/live/ssl-dhparams.pem" ]
|
|
|
|
then
|
|
|
|
|
|
|
|
echo "### Downloading recommended TLS parameters ..."
|
|
|
|
mkdir -p "${lets_path}/live/${domain_root}"
|
2020-05-05 02:23:55 +01:00
|
|
|
|
2020-05-10 22:33:03 +01:00
|
|
|
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf >"$lets_path/options-ssl-nginx.conf"
|
|
|
|
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem >"$lets_path/ssl-dhparams.pem"
|
2020-05-05 02:23:55 +01:00
|
|
|
|
2020-05-10 22:33:03 +01:00
|
|
|
if [ ${signed} -eq 0 ]
|
|
|
|
then
|
|
|
|
echo "### Creating self signed certificate for ${domain_root} ..."
|
|
|
|
openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 365 \
|
|
|
|
-keyout "${lets_path}/live/${domain_root}/privkey.pem" \
|
|
|
|
-out "${lets_path}/live/${domain_root}/fullchain.pem" -subj "/CN=${domain_root}"
|
2020-05-05 02:23:55 +01:00
|
|
|
|
2020-05-10 22:33:03 +01:00
|
|
|
else
|
|
|
|
echo "### Creating dummy certificate for ${domain_root} ..."
|
|
|
|
openssl req -x509 -nodes -newkey rsa:1024 -days 1 \
|
|
|
|
-keyout "${lets_path}/live/${domain_root}/privkey.pem" \
|
|
|
|
-out "${lets_path}/live/${domain_root}/fullchain.pem" -subj '/CN=localhost'
|
2020-05-05 02:23:55 +01:00
|
|
|
|
|
|
|
nginx -s reload
|
|
|
|
|
2020-05-10 22:33:03 +01:00
|
|
|
rm -Rf "${lets_path}/live/${domain_root}"
|
|
|
|
rm -Rf "${lets_path}/archive/${domain_root}"
|
|
|
|
rm -Rf "${lets_path}/renewal/${domain_root}.conf"
|
2020-05-05 02:23:55 +01:00
|
|
|
|
2020-05-10 22:33:03 +01:00
|
|
|
echo "### Requesting Let's Encrypt certificate for ${domain_root} ..."
|
|
|
|
# Format domain_args with the cartesian product of `domain_root` and `subdomains`
|
2020-05-05 02:23:55 +01:00
|
|
|
|
2020-05-10 22:33:03 +01:00
|
|
|
if [ "${domain_root}" = "${domain}" ]; then domain_arg="-d ${domain_root}"; else domain_arg="-d ${domain_root} -d ${domain}"; fi
|
2020-05-05 02:23:55 +01:00
|
|
|
|
|
|
|
# Ask Let's Encrypt to create certificates, if challenge passed
|
2020-05-10 22:33:03 +01:00
|
|
|
certbot certonly --webroot -w "${certbot_path}" \
|
|
|
|
--email "${email}" \
|
2020-05-07 22:56:39 +01:00
|
|
|
${domain_arg} \
|
2020-05-05 02:23:55 +01:00
|
|
|
--non-interactive \
|
2020-05-10 22:33:03 +01:00
|
|
|
--rsa-key-size "${rsa_key_size}" \
|
2020-05-05 02:23:55 +01:00
|
|
|
--agree-tos \
|
|
|
|
--force-renewal
|
2020-05-10 22:33:03 +01:00
|
|
|
fi
|
2020-05-05 02:23:55 +01:00
|
|
|
|
|
|
|
else
|
2020-05-10 22:33:03 +01:00
|
|
|
echo "Certificate related files exists, exiting"
|
2020-05-05 02:23:55 +01:00
|
|
|
fi
|