]>
Commit | Line | Data |
---|---|---|
a4e7c43a LS |
1 | #!/bin/bash |
2 | ||
3 | # Fail early | |
4 | set -eu -o pipefail | |
5 | ||
6 | # Check if running in CI environment | |
7 | if [[ ! "${CI:-false}" == "true" ]]; then | |
8 | echo "ERROR: Not running in CI environment!" | |
9 | exit 1 | |
10 | fi | |
11 | ||
12 | _TEST() { | |
d3bc67eb | 13 | echo |
40556950 LS |
14 | echo "${1} " |
15 | } | |
16 | _SUBTEST() { | |
17 | echo -n " + ${1} " | |
a4e7c43a LS |
18 | } |
19 | _PASS() { | |
98fe9b34 LS |
20 | if [[ ! -z "$(cat errorlog)" ]]; then |
21 | _FAIL | |
22 | fi | |
40556950 | 23 | echo -e "[\u001B[32mPASS\u001B[0m]" |
a4e7c43a LS |
24 | } |
25 | _FAIL() { | |
26 | echo -e "[\u001B[31mFAIL\u001B[0m]" | |
27 | echo | |
28 | echo "Problem: ${@}" | |
29 | echo | |
30 | echo "STDOUT:" | |
31 | cat tmplog | |
32 | echo | |
33 | echo "STDERR:" | |
34 | cat errorlog | |
35 | exit 1 | |
36 | } | |
37 | _CHECK_FILE() { | |
d3bc67eb | 38 | _SUBTEST "Checking if file '${1}' exists..." |
40556950 LS |
39 | if [[ -e "${1}" ]]; then |
40 | _PASS | |
41 | else | |
42 | _FAIL "Missing file: ${1}" | |
43 | fi | |
a4e7c43a LS |
44 | } |
45 | _CHECK_LOG() { | |
d3bc67eb | 46 | _SUBTEST "Checking if log contains '${1}'..." |
40556950 LS |
47 | if grep -- "${1}" tmplog > /dev/null; then |
48 | _PASS | |
49 | else | |
50 | _FAIL "Missing in log: ${1}" | |
51 | fi | |
52 | } | |
341f5252 | 53 | _CHECK_NOT_LOG() { |
d3bc67eb | 54 | _SUBTEST "Checking if log doesn't contain '${1}'..." |
341f5252 LS |
55 | if grep -- "${1}" tmplog > /dev/null; then |
56 | _FAIL "Found in log: ${1}" | |
57 | else | |
58 | _PASS | |
59 | fi | |
60 | } | |
40556950 LS |
61 | _CHECK_ERRORLOG() { |
62 | _SUBTEST "Checking if errorlog is empty..." | |
63 | if [[ -z "$(cat errorlog)" ]]; then | |
64 | _PASS | |
65 | else | |
66 | _FAIL "Non-empty errorlog" | |
67 | fi | |
a4e7c43a LS |
68 | } |
69 | ||
70 | # If not found (should be cached in travis) download ngrok | |
71 | if [[ ! -e "ngrok/ngrok" ]]; then | |
72 | ( | |
73 | mkdir -p ngrok | |
ebe9ea3d | 74 | cd ngrok |
a4e7c43a LS |
75 | wget https://dl.ngrok.com/ngrok_2.0.19_linux_amd64.zip -O ngrok.zip |
76 | unzip ngrok.zip ngrok | |
77 | chmod +x ngrok | |
78 | ) | |
79 | fi | |
80 | ||
81 | # Run ngrok and grab temporary url from logfile | |
82 | ngrok/ngrok http 8080 --log stdout --log-format logfmt --log-level debug > tmp.log & | |
338ec308 | 83 | ngrok/ngrok http 8080 --log stdout --log-format logfmt --log-level debug > tmp2.log & |
c9823c25 | 84 | ngrok/ngrok http 8080 --log stdout --log-format logfmt --log-level debug > tmp3.log & |
a4e7c43a LS |
85 | sleep 2 |
86 | TMP_URL="$(grep -Eo "Hostname:[a-z0-9]+.ngrok.io" tmp.log | head -1 | cut -d':' -f2)" | |
338ec308 | 87 | TMP2_URL="$(grep -Eo "Hostname:[a-z0-9]+.ngrok.io" tmp2.log | head -1 | cut -d':' -f2)" |
c9823c25 LS |
88 | TMP3_URL="$(grep -Eo "Hostname:[a-z0-9]+.ngrok.io" tmp3.log | head -1 | cut -d':' -f2)" |
89 | if [[ -z "${TMP_URL}" ]] || [[ -z "${TMP2_URL}" ]] || [[ -z "${TMP3_URL}" ]]; then | |
a4e7c43a LS |
90 | echo "Couldn't get an url from ngrok, not a letsencrypt.sh bug, tests can't continue." |
91 | exit 1 | |
92 | fi | |
93 | ||
94 | # Run python webserver in .acme-challenges directory to serve challenge responses | |
95 | mkdir -p .acme-challenges/.well-known/acme-challenge | |
96 | ( | |
97 | cd .acme-challenges | |
98 | python -m SimpleHTTPServer 8080 > /dev/null 2> /dev/null | |
99 | ) & | |
100 | ||
101 | # Generate config and create empty domains.txt | |
f6f77139 LS |
102 | echo 'CA="https://testca.kurz.pw/directory"' > config.sh |
103 | echo 'LICENSE="https://testca.kurz.pw/terms/v1"' >> config.sh | |
a4e7c43a | 104 | echo 'WELLKNOWN=".acme-challenges/.well-known/acme-challenge"' >> config.sh |
da2795d3 | 105 | echo 'RENEW_DAYS="14"' >> config.sh |
a4e7c43a LS |
106 | touch domains.txt |
107 | ||
108 | # Check if help command is working | |
109 | _TEST "Checking if help command is working..." | |
6a8f4482 | 110 | ./letsencrypt.sh --help > tmplog 2> errorlog || _FAIL "Script execution failed" |
a4e7c43a | 111 | _CHECK_LOG "Default command: help" |
40556950 LS |
112 | _CHECK_LOG "--help (-h)" |
113 | _CHECK_LOG "--domain (-d) domain.tld" | |
114 | _CHECK_ERRORLOG | |
a4e7c43a LS |
115 | |
116 | # Run in cron mode with empty domains.txt (should only generate private key and exit) | |
117 | _TEST "First run in cron mode, checking if private key is generated and registered" | |
6a8f4482 | 118 | ./letsencrypt.sh --cron > tmplog 2> errorlog || _FAIL "Script execution failed" |
a4e7c43a LS |
119 | _CHECK_LOG "Registering account key" |
120 | _CHECK_FILE "private_key.pem" | |
40556950 | 121 | _CHECK_ERRORLOG |
a4e7c43a LS |
122 | |
123 | # Temporarily move config out of the way and try signing certificate by using temporary config location | |
124 | _TEST "Try signing using temporary config location and with domain as command line parameter" | |
125 | mv config.sh tmp_config.sh | |
b57fd221 | 126 | ./letsencrypt.sh --cron --domain "${TMP_URL}" --domain "${TMP2_URL}" -f tmp_config.sh > tmplog 2> errorlog || _FAIL "Script execution failed" |
6a8f4482 | 127 | _CHECK_NOT_LOG "Checking domain name(s) of existing cert" |
a4e7c43a LS |
128 | _CHECK_LOG "Generating private key" |
129 | _CHECK_LOG "Requesting challenge for ${TMP_URL}" | |
338ec308 | 130 | _CHECK_LOG "Requesting challenge for ${TMP2_URL}" |
a4e7c43a LS |
131 | _CHECK_LOG "Challenge is valid!" |
132 | _CHECK_LOG "Creating fullchain.pem" | |
133 | _CHECK_LOG "Done!" | |
40556950 | 134 | _CHECK_ERRORLOG |
a4e7c43a LS |
135 | mv tmp_config.sh config.sh |
136 | ||
137 | # Move private key and add new location to config | |
138 | mv private_key.pem account_key.pem | |
139 | echo 'PRIVATE_KEY="./account_key.pem"' >> config.sh | |
140 | ||
c9823c25 LS |
141 | # Add third domain to command-lime, should force renewal. |
142 | _TEST "Run in cron mode again, this time adding third domain, should force renewal." | |
143 | ./letsencrypt.sh --cron --domain "${TMP_URL}" --domain "${TMP2_URL}" --domain "${TMP3_URL}" -f tmp_config.sh > tmplog 2> errorlog || _FAIL "Script execution failed" | |
144 | _CHECK_LOG "Domain name(s) are not matching!" | |
145 | _CHECK_LOG "Forcing renew." | |
146 | _CHECK_LOG "Requesting challenge for ${TMP_URL}" | |
147 | _CHECK_LOG "Requesting challenge for ${TMP2_URL}" | |
148 | _CHECK_LOG "Requesting challenge for ${TMP3_URL}" | |
149 | _CHECK_LOG "Challenge is valid!" | |
150 | _CHECK_LOG "Creating fullchain.pem" | |
151 | _CHECK_LOG "Done!" | |
152 | _CHECK_ERRORLOG | |
153 | ||
a4e7c43a LS |
154 | # Add domain to domains.txt and run in cron mode again (should find a non-expiring certificate and do nothing) |
155 | _TEST "Run in cron mode again, this time with domain in domains.txt, should find non-expiring certificate" | |
c9823c25 | 156 | echo "${TMP_URL} ${TMP2_URL} ${TMP3_URL}" >> domains.txt |
6a8f4482 LS |
157 | ./letsencrypt.sh --cron > tmplog 2> errorlog || _FAIL "Script execution failed" |
158 | _CHECK_LOG "Checking domain name(s) of existing cert... unchanged." | |
a4e7c43a | 159 | _CHECK_LOG "Skipping!" |
40556950 | 160 | _CHECK_ERRORLOG |
a4e7c43a | 161 | |
341f5252 LS |
162 | # Run in cron mode one last time, with domain in domains.txt and force-resign (should find certificate, resign anyway, and not generate private key) |
163 | _TEST "Run in cron mode one last time, with domain in domains.txt and force-resign" | |
6a8f4482 LS |
164 | ./letsencrypt.sh --cron --force > tmplog 2> errorlog || _FAIL "Script execution failed" |
165 | _CHECK_LOG "Checking domain name(s) of existing cert... unchanged." | |
2d097c92 | 166 | _CHECK_LOG "Ignoring because renew was forced!" |
341f5252 LS |
167 | _CHECK_NOT_LOG "Generating private key" |
168 | _CHECK_LOG "Requesting challenge for ${TMP_URL}" | |
338ec308 | 169 | _CHECK_LOG "Requesting challenge for ${TMP2_URL}" |
c9823c25 | 170 | _CHECK_LOG "Requesting challenge for ${TMP3_URL}" |
341f5252 LS |
171 | _CHECK_LOG "Challenge is valid!" |
172 | _CHECK_LOG "Creating fullchain.pem" | |
173 | _CHECK_LOG "Done!" | |
174 | _CHECK_ERRORLOG | |
175 | ||
c9823c25 LS |
176 | # Check if signcsr command is working |
177 | _TEST "Running signcsr command" | |
178 | ./letsencrypt.sh --signcsr certs/${TMP_URL}/cert.csr > tmplog 2> errorlog || _FAIL "Script execution failed" | |
179 | _CHECK_ERRORLOG | |
180 | ||
a4e7c43a LS |
181 | # Delete account key (not needed anymore) |
182 | rm account_key.pem | |
183 | ||
85b3f191 LS |
184 | # Check if renewal works |
185 | _TEST "Run in cron mode again, to check if renewal works" | |
186 | echo 'RENEW_DAYS="300"' >> config.sh | |
187 | ./letsencrypt.sh --cron > tmplog 2> errorlog || _FAIL "Script execution failed" | |
188 | _CHECK_LOG "Checking domain name(s) of existing cert... unchanged." | |
189 | _CHECK_LOG "Renewing!" | |
190 | _CHECK_ERRORLOG | |
191 | ||
a4e7c43a LS |
192 | # Check if certificate is valid in various ways |
193 | _TEST "Verifying certificate..." | |
d3bc67eb LS |
194 | _SUBTEST "Verifying certificate on its own..." |
195 | openssl x509 -in "certs/${TMP_URL}/cert.pem" -noout -text > tmplog 2> errorlog && _PASS || _FAIL | |
a4e7c43a | 196 | _CHECK_LOG "CN=${TMP_URL}" |
338ec308 | 197 | _CHECK_LOG "${TMP2_URL}" |
d3bc67eb LS |
198 | _SUBTEST "Verifying file with full chain..." |
199 | openssl x509 -in "certs/${TMP_URL}/fullchain.pem" -noout -text > /dev/null 2>> errorlog && _PASS || _FAIL | |
200 | _SUBTEST "Verifying certificate against CA certificate..." | |
201 | (openssl verify -verbose -CAfile "certs/${TMP_URL}/fullchain.pem" -purpose sslserver "certs/${TMP_URL}/fullchain.pem" 2>&1 || true) | (grep -v ': OK$' || true) >> errorlog 2>> errorlog && _PASS || _FAIL | |
40556950 | 202 | _CHECK_ERRORLOG |
a4e7c43a LS |
203 | |
204 | # Revoke certificate using certificate key | |
205 | _TEST "Revoking certificate..." | |
6a8f4482 | 206 | ./letsencrypt.sh --revoke "certs/${TMP_URL}/cert.pem" --privkey "certs/${TMP_URL}/privkey.pem" > tmplog 2> errorlog || _FAIL "Script execution failed" |
c7018036 MG |
207 | REAL_CERT="$(readlink -n "certs/${TMP_URL}/cert.pem")" |
208 | _CHECK_LOG "Revoking certs/${TMP_URL}/${REAL_CERT}" | |
3dcfa8b4 | 209 | _CHECK_LOG "Done." |
c7018036 | 210 | _CHECK_FILE "certs/${TMP_URL}/${REAL_CERT}-revoked" |
40556950 | 211 | _CHECK_ERRORLOG |
a4e7c43a LS |
212 | |
213 | # All done | |
214 | exit 0 |