Skip to content

Commit 35f932f

Browse files
Move letsencrypt scripts to work on boot
Allows ssl and letsencrypt templates to run on boot via initscripts This change adds the decision to configure https or letsencrypt to be at runtime rather than at build time via env vars. Under the hood, these are the commands, just migrated to shellscripts that run when a container boots. Runs on existence of ENABLE_SSL (base ssl template) or LETSENCRYPT_ACCOUNT_EMAIL (ssl template+letsencrypt template) Both cases checks and errors on blank hostname.
1 parent e189968 commit 35f932f

File tree

2 files changed

+89
-77
lines changed

2 files changed

+89
-77
lines changed
Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
env:
2-
LETSENCRYPT_DIR: "/shared/letsencrypt"
32
DISCOURSE_FORCE_HTTPS: true
43

5-
hooks:
6-
after_ssl:
7-
- exec:
8-
cmd:
9-
- if [ -z "$LETSENCRYPT_ACCOUNT_EMAIL" ]; then echo "LETSENCRYPT_ACCOUNT_EMAIL ENV variable is required and has not been set."; exit 1; fi
10-
- /bin/bash -c "if [[ ! \"$LETSENCRYPT_ACCOUNT_EMAIL\" =~ ([^@]+)@([^\.]+) ]]; then echo \"LETSENCRYPT_ACCOUNT_EMAIL is not a valid email address\"; exit 1; fi"
11-
12-
- exec:
13-
cmd:
14-
- cd /root && git clone --branch 3.0.6 --depth 1 https://github.com/acmesh-official/acme.sh.git && cd /root/acme.sh
15-
- touch /var/spool/cron/crontabs/root
16-
- install -d -m 0755 -g root -o root $LETSENCRYPT_DIR
17-
- cd /root/acme.sh && LE_WORKING_DIR="${LETSENCRYPT_DIR}" ./acme.sh --install --log "${LETSENCRYPT_DIR}/acme.sh.log"
18-
- cd /root/acme.sh && LE_WORKING_DIR="${LETSENCRYPT_DIR}" ./acme.sh --upgrade --auto-upgrade
19-
- cd /root/acme.sh && LE_WORKING_DIR="${LETSENCRYPT_DIR}" ./acme.sh --set-default-ca --server letsencrypt
20-
21-
- file:
22-
path: "/etc/nginx/letsencrypt.conf"
23-
contents: |
4+
run:
5+
- exec:
6+
cmd:
7+
- cd /opt && git clone --branch 3.0.6 --depth 1 https://github.com/acmesh-official/acme.sh.git
8+
- file:
9+
path: "/usr/local/bin/configure-letsencrypt"
10+
chmod: "+x"
11+
contents: |
12+
#!/bin/bash
13+
touch /var/spool/cron/crontabs/root
14+
LETSENCRYPT_DIR="/shared/letsencrypt"
15+
install -d -m 0755 -g root -o root $LETSENCRYPT_DIR
16+
cd /opt/acme.sh && LE_WORKING_DIR="${LETSENCRYPT_DIR}" ./acme.sh --install --log "${LETSENCRYPT_DIR}/acme.sh.log"
17+
cd /opt/acme.sh && LE_WORKING_DIR="${LETSENCRYPT_DIR}" ./acme.sh --upgrade --auto-upgrade
18+
cd /opt/acme.sh && LE_WORKING_DIR="${LETSENCRYPT_DIR}" ./acme.sh --set-default-ca --server letsencrypt
19+
20+
cat << EOF > /etc/nginx/letsencrypt.conf
2421
user www-data;
2522
worker_processes auto;
2623
daemon on;
@@ -50,20 +47,34 @@ hooks:
5047
}
5148
}
5249
}
50+
EOF
5351
54-
- file:
55-
path: /etc/runit/1.d/letsencrypt
56-
chmod: "+x"
57-
contents: |
52+
sed -Ei "s/^#?ACCOUNT_EMAIL=.+/ACCOUNT_EMAIL=${LETSENCRYPT_ACCOUNT_EMAIL}/" \
53+
/shared/letsencrypt/account.conf
54+
55+
sed -Ei "s/ssl_certificate .+/ssl_certificate \/shared\/ssl\/${DISCOURSE_HOSTNAME}.cer;\
56+
ssl_certificate \/shared\/ssl\/${DISCOURSE_HOSTNAME}_ecc.cer;/" \
57+
/etc/nginx/conf.d/outlets/server/20-https.conf
58+
sed -Ei "s/ssl_certificate_key .+/ssl_certificate_key \/shared\/ssl\/${DISCOURSE_HOSTNAME}.key; \
59+
ssl_certificate_key \/shared\/ssl\/${DISCOURSE_HOSTNAME}_ecc.key;/" \
60+
/etc/nginx/conf.d/outlets/server/20-https.conf
61+
62+
exec /usr/local/bin/letsencrypt
63+
64+
- file:
65+
path: /usr/local/bin/letsencrypt
66+
chmod: "+x"
67+
contents: |
5868
#!/bin/bash
69+
LETSENCRYPT_DIR="/shared/letsencrypt"
5970
/usr/sbin/nginx -c /etc/nginx/letsencrypt.conf
6071
6172
issue_cert() {
62-
LE_WORKING_DIR="${LETSENCRYPT_DIR}" $$ENV_LETSENCRYPT_DIR/acme.sh --issue $2 -d $$ENV_DISCOURSE_HOSTNAME --keylength $1 -w /var/www/discourse/public
73+
LE_WORKING_DIR="${LETSENCRYPT_DIR}" ${LETSENCRYPT_DIR}/acme.sh --issue $2 -d ${DISCOURSE_HOSTNAME} --keylength $1 -w /var/www/discourse/public
6374
}
6475
6576
cert_exists() {
66-
[[ "$(cd $$ENV_LETSENCRYPT_DIR/$$ENV_DISCOURSE_HOSTNAME$1 && openssl verify -CAfile <(openssl x509 -in ca.cer) fullchain.cer | grep "OK")" ]]
77+
[[ "$(cd ${LETSENCRYPT_DIR}/${DISCOURSE_HOSTNAME}$1 && openssl verify -CAfile <(openssl x509 -in ca.cer) fullchain.cer | grep "OK")" ]]
6778
}
6879
6980
########################################################
@@ -76,11 +87,11 @@ hooks:
7687
issue_cert "4096" "--force"
7788
fi
7889
79-
LE_WORKING_DIR="${LETSENCRYPT_DIR}" $$ENV_LETSENCRYPT_DIR/acme.sh \
90+
LE_WORKING_DIR="${LETSENCRYPT_DIR}" ${LETSENCRYPT_DIR}/acme.sh \
8091
--installcert \
81-
-d $$ENV_DISCOURSE_HOSTNAME \
82-
--fullchainpath /shared/ssl/$$ENV_DISCOURSE_HOSTNAME.cer \
83-
--keypath /shared/ssl/$$ENV_DISCOURSE_HOSTNAME.key \
92+
-d ${DISCOURSE_HOSTNAME} \
93+
--fullchainpath /shared/ssl/${DISCOURSE_HOSTNAME}.cer \
94+
--keypath /shared/ssl/${DISCOURSE_HOSTNAME}.key \
8495
--reloadcmd "sv reload nginx"
8596
8697
########################################################
@@ -93,11 +104,11 @@ hooks:
93104
issue_cert "ec-256" "--force"
94105
fi
95106
96-
LE_WORKING_DIR="${LETSENCRYPT_DIR}" $$ENV_LETSENCRYPT_DIR/acme.sh \
107+
LE_WORKING_DIR="${LETSENCRYPT_DIR}" ${LETSENCRYPT_DIR}/acme.sh \
97108
--installcert --ecc \
98-
-d $$ENV_DISCOURSE_HOSTNAME \
99-
--fullchainpath /shared/ssl/$$ENV_DISCOURSE_HOSTNAME_ecc.cer \
100-
--keypath /shared/ssl/$$ENV_DISCOURSE_HOSTNAME_ecc.key \
109+
-d ${DISCOURSE_HOSTNAME} \
110+
--fullchainpath /shared/ssl/${DISCOURSE_HOSTNAME}_ecc.cer \
111+
--keypath /shared/ssl/${DISCOURSE_HOSTNAME}_ecc.key \
101112
--reloadcmd "sv reload nginx"
102113
103114
if cert_exists "" || cert_exists "_ecc"; then
@@ -106,22 +117,15 @@ hooks:
106117
107118
/usr/sbin/nginx -c /etc/nginx/letsencrypt.conf -s stop
108119
109-
- replace:
110-
filename: /shared/letsencrypt/account.conf
111-
from: /#?ACCOUNT_EMAIL=.+/
112-
to: |
113-
ACCOUNT_EMAIL=$$ENV_LETSENCRYPT_ACCOUNT_EMAIL
114-
115-
- replace:
116-
filename: "/etc/nginx/conf.d/outlets/server/20-https.conf"
117-
from: /ssl_certificate.+/
118-
to: |
119-
ssl_certificate /shared/ssl/$$ENV_DISCOURSE_HOSTNAME.cer;
120-
ssl_certificate /shared/ssl/$$ENV_DISCOURSE_HOSTNAME_ecc.cer;
121-
122-
- replace:
123-
filename: "/etc/nginx/conf.d/outlets/server/20-https.conf"
124-
from: /ssl_certificate_key.+/
125-
to: |
126-
ssl_certificate_key /shared/ssl/$$ENV_DISCOURSE_HOSTNAME.key;
127-
ssl_certificate_key /shared/ssl/$$ENV_DISCOURSE_HOSTNAME_ecc.key;
120+
hooks:
121+
after_ssl:
122+
- file:
123+
path: /etc/runit/1.d/install-ssl
124+
chmod: "+x"
125+
contents: |
126+
#!/bin/bash
127+
if [ -z "$DISCOURSE_HOSTNAME" ]; then echo "DISCOURSE_HOSTNAME expected"; exit 1; fi
128+
if [ -z "$LETSENCRYPT_ACCOUNT_EMAIL" ]; then echo "LETSENCRYPT_ACCOUNT_EMAIL ENV not set. Skipping Let's Encrypt setup."; exit 0; fi
129+
if [[ ! "$LETSENCRYPT_ACCOUNT_EMAIL" =~ ([^@]+)@([^\.]+) ]]; then echo "LETSENCRYPT_ACCOUNT_EMAIL is not a valid email address"; exit 1; fi
130+
/usr/local/bin/configure-ssl
131+
exec /usr/local/bin/configure-letsencrypt

templates/web.ssl.template.yml

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
run:
2-
- exec:
3-
cmd:
4-
- "mkdir -p /shared/ssl/"
52
- file:
6-
path: "/etc/nginx/conf.d/outlets/before-server/20-redirect-http-to-https.conf"
3+
path: /etc/runit/1.d/install-ssl
4+
hook: ssl
5+
chmod: "+x"
6+
contents: |
7+
#!/bin/bash
8+
if [ -z "$DISCOURSE_HOSTNAME" ]; then echo "DISCOURSE_HOSTNAME expected"; exit 1; fi
9+
if [ ! -z "$ENABLE_SSL" ]; then
10+
exec /usr/local/bin/configure-ssl
11+
fi
12+
13+
- file:
14+
path: "/usr/local/bin/configure-ssl"
15+
chmod: "+x"
716
contents: |
17+
#!/bin/bash
18+
mkdir -p /shared/ssl/
19+
cat << EOF > /etc/nginx/conf.d/outlets/before-server/20-redirect-http-to-https.conf
820
server {
921
listen 80;
10-
return 301 https://$$ENV_DISCOURSE_HOSTNAME$request_uri;
22+
return 301 https://${DISCOURSE_HOSTNAME}$request_uri;
1123
}
12-
- file:
13-
path: "/etc/nginx/conf.d/outlets/server/10-http.conf"
14-
contents: ""
15-
- file:
16-
hook: ssl
17-
path: "/etc/nginx/conf.d/outlets/server/20-https.conf"
18-
contents: |
24+
EOF
25+
26+
> /etc/nginx/conf.d/outlets/server/10-http.conf
27+
28+
cat << EOF > /etc/nginx/conf.d/outlets/server/20-https.conf
1929
listen 443 ssl;
2030
http2 on;
2131
@@ -32,17 +42,15 @@ run:
3242
3343
add_header Strict-Transport-Security 'max-age=31536000';
3444
35-
if ($http_host != $$ENV_DISCOURSE_HOSTNAME) {
36-
rewrite (.*) https://$$ENV_DISCOURSE_HOSTNAME$1 permanent;
45+
if (\$http_host != ${DISCOURSE_HOSTNAME}) {
46+
rewrite (.*) https://${DISCOURSE_HOSTNAME}\$1 permanent;
3747
}
38-
- file:
39-
path: "/etc/nginx/conf.d/outlets/discourse/20-https.conf"
40-
contents: |
48+
EOF
49+
50+
cat << EOF > /etc/nginx/conf.d/outlets/discourse/20-https.conf
4151
add_header Strict-Transport-Security 'max-age=31536000';
42-
- exec:
43-
cmd:
44-
- |-
45-
if [ -f "/proc/net/if_inet6" ] ; then
46-
sed -i 's/listen 80;/listen 80;\nlisten [::]:80;/g' /etc/nginx/conf.d/outlets/before-server/20-redirect-http-to-https.conf
47-
sed -i 's/listen 443 ssl;/listen 443 ssl;\nlisten [::]:443 ssl;/g' /etc/nginx/conf.d/outlets/server/20-https.conf
48-
fi
52+
EOF
53+
if [ -f "/proc/net/if_inet6" ] ; then
54+
sed -i 's/listen 80;/listen 80;\nlisten [::]:80;/g' /etc/nginx/conf.d/outlets/before-server/20-redirect-http-to-https.conf
55+
sed -i 's/listen 443 ssl;/listen 443 ssl;\nlisten [::]:443 ssl;/g' /etc/nginx/conf.d/outlets/server/20-https.conf
56+
fi

0 commit comments

Comments
 (0)