Skip to content

Commit ce95d15

Browse files
benniekissBen Milburn-Town
authored andcommitted
refactor init_cert.sh to enable custom certs
1 parent 58cec8f commit ce95d15

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

docker/init_cert.sh

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
11
#!/bin/bash
22
set -ex
3-
LETSENCRYPT_DOMAIN=${LETSENCRYPT_DOMAIN:-"none"}
3+
4+
LETSENCRYPT_DOMAIN=${LETSENCRYPT_DOMAIN:-}
45
LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL:-"example@local"}
6+
NGINX_SSL_CERT=${NGINX_SSL_CERT:-}
7+
NGINX_SSL_KEY=${NGINX_SSL_KEY:-}
58
NGINX_SSL_PORT=${NGINX_SSL_PORT:-443}
9+
NGINX_CONF="/etc/nginx/http.d/default.conf"
610

7-
# If no domain is provided, skip certbot execution and configuration
8-
if [ "${LETSENCRYPT_DOMAIN}-x" == "none-x" ]; then
9-
exit 0
10-
fi
11+
remove_ssl_config() {
12+
sed -i -E "/ssl_certificate /,/^$/d" "${NGINX_CONF}"
13+
sed -i -E "/ssl_certificate_key /,/^$/d" "${NGINX_CONF}"
14+
}
1115

1216
# Request a certificate
1317
# this also updates the nginx config file with new SSL entries
14-
certbot -n --nginx --agree-tos --email ${LETSENCRYPT_EMAIL} -d ${LETSENCRYPT_DOMAIN} --https-port ${NGINX_SSL_PORT}
15-
# Add cron job file
16-
cat <<EOF >/etc/crontabs/root
18+
if [[ -n "${LETSENCRYPT_DOMAIN}" ]]; then
19+
echo "Generating SSL certificate using certbot for ${LETSENCRYPT_DOMAIN} with automatic renewal."
20+
certbot -n --nginx --agree-tos --email "${LETSENCRYPT_EMAIL}" -d "${LETSENCRYPT_DOMAIN}" --https-port "${NGINX_SSL_PORT}"
21+
# Add cron job file
22+
cat <<EOF >/etc/crontabs/root
1723
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
1824
19-
0 */12 * * * certbot -q renew --nginx --https-port ${NGINX_SSL_PORT}
25+
0 */12 * * * certbot -q renew --nginx --https-port "${NGINX_SSL_PORT}"
2026
EOF
21-
# start cron daemon
22-
supervisorctl start cron
27+
# start cron daemon
28+
supervisorctl start cron
29+
# Update the nginx config file with the provided SSL entries
30+
elif [[ -n "${NGINX_SSL_CERT}" && -n "${NGINX_SSL_KEY}" ]]; then
31+
echo "Configuring the provided SSL certificate at ${NGINX_SSL_CERT}"
32+
remove_ssl_config
33+
sed -i -E "s|listen [0-9]{1,5} (default_server\|ssl http2);|listen ${NGINX_SSL_PORT} ssl http2;|" "${NGINX_CONF}"
34+
sed -i -E "s|listen \[::\]:[0-9]{1,5} (default_server\|ssl http2);|listen \[::\]:${NGINX_SSL_PORT} ssl http2;\n\n ssl_certificate ${NGINX_SSL_CERT};\n ssl_certificate_key ${NGINX_SSL_KEY};|" "${NGINX_CONF}"
35+
# If the nginx port is provided but no other settings are, update the port
36+
elif [[ "${NGINX_SSL_PORT}" != 443 ]]; then
37+
echo "Setting nginx listen port."
38+
remove_ssl_config
39+
sed -i -E "s|listen [0-9]{1,5} (default_server\|ssl http2);|listen ${NGINX_SSL_PORT} default_server;|" "${NGINX_CONF}"
40+
sed -i -E "s|listen \[::\]:[0-9]{1,5} (default_server\|ssl http2);|listen \[::\]:${NGINX_SSL_PORT} default_server;|" "${NGINX_CONF}"
41+
else
42+
echo "No certificates or Letsencrypt domain was provided. Exiting."
43+
exit 0
44+
fi

0 commit comments

Comments
 (0)