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