]> git.street.me.uk Git - andy/dehydrated.git/blame - test.sh
curl: use custom user agent
[andy/dehydrated.git] / test.sh
CommitLineData
1561e9fc 1#!/usr/bin/env bash
a4e7c43a
LS
2
3# Fail early
4set -eu -o pipefail
5
6# Check if running in CI environment
7if [[ ! "${CI:-false}" == "true" ]]; then
8 echo "ERROR: Not running in CI environment!"
9 exit 1
10fi
11
12_TEST() {
d3bc67eb 13 echo
40556950
LS
14 echo "${1} "
15}
16_SUBTEST() {
17 echo -n " + ${1} "
a4e7c43a
LS
18}
19_PASS() {
40556950 20 echo -e "[\u001B[32mPASS\u001B[0m]"
a4e7c43a
LS
21}
22_FAIL() {
23 echo -e "[\u001B[31mFAIL\u001B[0m]"
24 echo
25 echo "Problem: ${@}"
26 echo
27 echo "STDOUT:"
28 cat tmplog
29 echo
30 echo "STDERR:"
31 cat errorlog
32 exit 1
33}
34_CHECK_FILE() {
d3bc67eb 35 _SUBTEST "Checking if file '${1}' exists..."
40556950
LS
36 if [[ -e "${1}" ]]; then
37 _PASS
38 else
39 _FAIL "Missing file: ${1}"
40 fi
a4e7c43a
LS
41}
42_CHECK_LOG() {
d3bc67eb 43 _SUBTEST "Checking if log contains '${1}'..."
40556950
LS
44 if grep -- "${1}" tmplog > /dev/null; then
45 _PASS
46 else
47 _FAIL "Missing in log: ${1}"
48 fi
49}
341f5252 50_CHECK_NOT_LOG() {
d3bc67eb 51 _SUBTEST "Checking if log doesn't contain '${1}'..."
341f5252
LS
52 if grep -- "${1}" tmplog > /dev/null; then
53 _FAIL "Found in log: ${1}"
54 else
55 _PASS
56 fi
57}
40556950
LS
58_CHECK_ERRORLOG() {
59 _SUBTEST "Checking if errorlog is empty..."
60 if [[ -z "$(cat errorlog)" ]]; then
61 _PASS
62 else
63 _FAIL "Non-empty errorlog"
64 fi
a4e7c43a
LS
65}
66
67# If not found (should be cached in travis) download ngrok
68if [[ ! -e "ngrok/ngrok" ]]; then
69 (
70 mkdir -p ngrok
ebe9ea3d 71 cd ngrok
607c89ca 72 wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip -O ngrok.zip
a4e7c43a
LS
73 unzip ngrok.zip ngrok
74 chmod +x ngrok
75 )
76fi
77
78# Run ngrok and grab temporary url from logfile
79ngrok/ngrok http 8080 --log stdout --log-format logfmt --log-level debug > tmp.log &
338ec308 80ngrok/ngrok http 8080 --log stdout --log-format logfmt --log-level debug > tmp2.log &
c9823c25 81ngrok/ngrok http 8080 --log stdout --log-format logfmt --log-level debug > tmp3.log &
a4e7c43a
LS
82sleep 2
83TMP_URL="$(grep -Eo "Hostname:[a-z0-9]+.ngrok.io" tmp.log | head -1 | cut -d':' -f2)"
338ec308 84TMP2_URL="$(grep -Eo "Hostname:[a-z0-9]+.ngrok.io" tmp2.log | head -1 | cut -d':' -f2)"
c9823c25
LS
85TMP3_URL="$(grep -Eo "Hostname:[a-z0-9]+.ngrok.io" tmp3.log | head -1 | cut -d':' -f2)"
86if [[ -z "${TMP_URL}" ]] || [[ -z "${TMP2_URL}" ]] || [[ -z "${TMP3_URL}" ]]; then
ec49a443 87 echo "Couldn't get an url from ngrok, not a dehydrated bug, tests can't continue."
a4e7c43a
LS
88 exit 1
89fi
90
91# Run python webserver in .acme-challenges directory to serve challenge responses
92mkdir -p .acme-challenges/.well-known/acme-challenge
93(
94 cd .acme-challenges
95 python -m SimpleHTTPServer 8080 > /dev/null 2> /dev/null
96) &
97
98# Generate config and create empty domains.txt
d5b28586 99echo 'CA="https://testca.kurz.pw/directory"' > config
6a32f20e 100echo 'CA_TERMS="https://testca.kurz.pw/terms"' >> config
d5b28586
LS
101echo 'WELLKNOWN=".acme-challenges/.well-known/acme-challenge"' >> config
102echo 'RENEW_DAYS="14"' >> config
a4e7c43a
LS
103touch domains.txt
104
105# Check if help command is working
106_TEST "Checking if help command is working..."
ec49a443 107./dehydrated --help > tmplog 2> errorlog || _FAIL "Script execution failed"
a4e7c43a 108_CHECK_LOG "Default command: help"
40556950
LS
109_CHECK_LOG "--help (-h)"
110_CHECK_LOG "--domain (-d) domain.tld"
111_CHECK_ERRORLOG
a4e7c43a 112
6a32f20e
LS
113# Register account key without LICENSE set
114_TEST "Register account key without LICENSE set"
115./dehydrated --register > tmplog 2> errorlog && _FAIL "Script execution failed"
116_CHECK_LOG "To accept these terms"
117_CHECK_ERRORLOG
118
119# Register account key and agreeing to terms
120_TEST "Register account key without LICENSE set"
121./dehydrated --register --accept-terms > tmplog 2> errorlog || _FAIL "Script execution failed"
122_CHECK_LOG "Registering account key"
123_CHECK_FILE accounts/*/account_key.pem
124_CHECK_ERRORLOG
125
126# Delete accounts and add LICENSE to config for normal operation
127rm -rf accounts
128echo 'LICENSE="https://testca.kurz.pw/terms/v1"' >> config
129
a4e7c43a
LS
130# Run in cron mode with empty domains.txt (should only generate private key and exit)
131_TEST "First run in cron mode, checking if private key is generated and registered"
ec49a443 132./dehydrated --cron > tmplog 2> errorlog || _FAIL "Script execution failed"
a4e7c43a 133_CHECK_LOG "Registering account key"
034ec30c 134_CHECK_FILE accounts/*/account_key.pem
40556950 135_CHECK_ERRORLOG
a4e7c43a
LS
136
137# Temporarily move config out of the way and try signing certificate by using temporary config location
138_TEST "Try signing using temporary config location and with domain as command line parameter"
d5b28586 139mv config tmp_config
6a32f20e 140./dehydrated --cron --domain "${TMP_URL}" --domain "${TMP2_URL}" --accept-terms -f tmp_config > tmplog 2> errorlog || _FAIL "Script execution failed"
6a8f4482 141_CHECK_NOT_LOG "Checking domain name(s) of existing cert"
a4e7c43a
LS
142_CHECK_LOG "Generating private key"
143_CHECK_LOG "Requesting challenge for ${TMP_URL}"
338ec308 144_CHECK_LOG "Requesting challenge for ${TMP2_URL}"
a4e7c43a
LS
145_CHECK_LOG "Challenge is valid!"
146_CHECK_LOG "Creating fullchain.pem"
147_CHECK_LOG "Done!"
40556950 148_CHECK_ERRORLOG
d5b28586 149mv tmp_config config
a4e7c43a 150
c9823c25
LS
151# Add third domain to command-lime, should force renewal.
152_TEST "Run in cron mode again, this time adding third domain, should force renewal."
ec49a443 153./dehydrated --cron --domain "${TMP_URL}" --domain "${TMP2_URL}" --domain "${TMP3_URL}" > tmplog 2> errorlog || _FAIL "Script execution failed"
c9823c25
LS
154_CHECK_LOG "Domain name(s) are not matching!"
155_CHECK_LOG "Forcing renew."
a2867410 156_CHECK_LOG "Generating private key"
c9823c25
LS
157_CHECK_LOG "Requesting challenge for ${TMP_URL}"
158_CHECK_LOG "Requesting challenge for ${TMP2_URL}"
159_CHECK_LOG "Requesting challenge for ${TMP3_URL}"
160_CHECK_LOG "Challenge is valid!"
161_CHECK_LOG "Creating fullchain.pem"
162_CHECK_LOG "Done!"
163_CHECK_ERRORLOG
164
33f07fcc
LS
165# Prepare domains.txt
166# Modify TMP3_URL to be uppercase to check for upper-lower-case mismatch bugs
167echo "${TMP_URL} ${TMP2_URL} $(tr 'a-z' 'A-Z' <<<"${TMP3_URL}")" >> domains.txt
168
169# Run in cron mode again (should find a non-expiring certificate and do nothing)
a4e7c43a 170_TEST "Run in cron mode again, this time with domain in domains.txt, should find non-expiring certificate"
ec49a443 171./dehydrated --cron > tmplog 2> errorlog || _FAIL "Script execution failed"
6a8f4482 172_CHECK_LOG "Checking domain name(s) of existing cert... unchanged."
64b23e7a 173_CHECK_LOG "Skipping renew"
40556950 174_CHECK_ERRORLOG
a4e7c43a 175
a2867410 176# Disable private key renew
d5b28586 177echo 'PRIVATE_KEY_RENEW="no"' >> config
a2867410 178
341f5252
LS
179# 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)
180_TEST "Run in cron mode one last time, with domain in domains.txt and force-resign"
ec49a443 181./dehydrated --cron --force > tmplog 2> errorlog || _FAIL "Script execution failed"
6a8f4482 182_CHECK_LOG "Checking domain name(s) of existing cert... unchanged."
2d097c92 183_CHECK_LOG "Ignoring because renew was forced!"
af2bc7a9 184_CHECK_NOT_LOG "Generating private key"
341f5252 185_CHECK_LOG "Requesting challenge for ${TMP_URL}"
338ec308 186_CHECK_LOG "Requesting challenge for ${TMP2_URL}"
c9823c25 187_CHECK_LOG "Requesting challenge for ${TMP3_URL}"
6a32f20e 188_CHECK_LOG "Already validated!"
341f5252
LS
189_CHECK_LOG "Creating fullchain.pem"
190_CHECK_LOG "Done!"
191_CHECK_ERRORLOG
192
c9823c25
LS
193# Check if signcsr command is working
194_TEST "Running signcsr command"
ec49a443 195./dehydrated --signcsr certs/${TMP_URL}/cert.csr > tmplog 2> errorlog || _FAIL "Script execution failed"
bfb45d8b
LS
196_CHECK_LOG "BEGIN CERTIFICATE"
197_CHECK_LOG "END CERTIFICATE"
198_CHECK_NOT_LOG "ERROR"
c9823c25 199
85b3f191
LS
200# Check if renewal works
201_TEST "Run in cron mode again, to check if renewal works"
d5b28586 202echo 'RENEW_DAYS="300"' >> config
ec49a443 203./dehydrated --cron > tmplog 2> errorlog || _FAIL "Script execution failed"
85b3f191
LS
204_CHECK_LOG "Checking domain name(s) of existing cert... unchanged."
205_CHECK_LOG "Renewing!"
206_CHECK_ERRORLOG
207
a4e7c43a
LS
208# Check if certificate is valid in various ways
209_TEST "Verifying certificate..."
d3bc67eb
LS
210_SUBTEST "Verifying certificate on its own..."
211openssl x509 -in "certs/${TMP_URL}/cert.pem" -noout -text > tmplog 2> errorlog && _PASS || _FAIL
a4e7c43a 212_CHECK_LOG "CN=${TMP_URL}"
338ec308 213_CHECK_LOG "${TMP2_URL}"
d3bc67eb
LS
214_SUBTEST "Verifying file with full chain..."
215openssl x509 -in "certs/${TMP_URL}/fullchain.pem" -noout -text > /dev/null 2>> errorlog && _PASS || _FAIL
216_SUBTEST "Verifying certificate against CA certificate..."
217(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 218_CHECK_ERRORLOG
a4e7c43a
LS
219
220# Revoke certificate using certificate key
221_TEST "Revoking certificate..."
ec49a443 222./dehydrated --revoke "certs/${TMP_URL}/cert.pem" --privkey "certs/${TMP_URL}/privkey.pem" > tmplog 2> errorlog || _FAIL "Script execution failed"
c7018036
MG
223REAL_CERT="$(readlink -n "certs/${TMP_URL}/cert.pem")"
224_CHECK_LOG "Revoking certs/${TMP_URL}/${REAL_CERT}"
3dcfa8b4 225_CHECK_LOG "Done."
c7018036 226_CHECK_FILE "certs/${TMP_URL}/${REAL_CERT}-revoked"
40556950 227_CHECK_ERRORLOG
a4e7c43a 228
a13e4103 229# Enable private key renew
230echo 'PRIVATE_KEY_RENEW="yes"' >> config
231echo 'PRIVATE_KEY_ROLLOVER="yes"' >> config
232
233# Check if Rolloverkey creation works
234_TEST "Testing Rolloverkeys..."
235_SUBTEST "First Run: Creating rolloverkey"
236./dehydrated --cron --domain "${TMP2_URL}" > tmplog 2> errorlog || _FAIL "Script execution failed"
237CERT_ROLL_HASH=$(openssl rsa -in certs/${TMP2_URL}/privkey.roll.pem -outform DER -pubout 2>/dev/null | openssl sha256)
238_CHECK_LOG "Generating private key"
239_CHECK_LOG "Generating private rollover key"
240_SUBTEST "Second Run: Force Renew, Use rolloverkey"
241./dehydrated --cron --force --domain "${TMP2_URL}" > tmplog 2> errorlog || _FAIL "Script execution failed"
242CERT_NEW_HASH=$(openssl rsa -in certs/${TMP2_URL}/privkey.pem -outform DER -pubout 2>/dev/null | openssl sha256)
243_CHECK_LOG "Generating private key"
244_CHECK_LOG "Moving Rolloverkey into position"
245_SUBTEST "Verifying Hash Rolloverkey and private key second run"
246[[ "${CERT_ROLL_HASH}" = "${CERT_NEW_HASH}" ]] && _PASS || _FAIL
247_CHECK_ERRORLOG
248
a35b89e9
LS
249# Test cleanup command
250_TEST "Cleaning up certificates"
ec49a443 251./dehydrated --cleanup > tmplog 2> errorlog || _FAIL "Script execution failed"
a35b89e9
LS
252_CHECK_LOG "Moving unused file to archive directory: ${TMP_URL}/cert-"
253_CHECK_LOG "Moving unused file to archive directory: ${TMP_URL}/chain-"
254_CHECK_LOG "Moving unused file to archive directory: ${TMP_URL}/fullchain-"
255_CHECK_ERRORLOG
256
a4e7c43a
LS
257# All done
258exit 0