Skip to content

Commit 6968d45

Browse files
authored
Merge pull request #710 from srvrco/tests-for-pr706-and-fix276
Tests for pr706 and fix276
2 parents 59d9797 + f6d48ac commit 6968d45

File tree

4 files changed

+153
-20
lines changed

4 files changed

+153
-20
lines changed

getssl

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
# 2021-07-30 Run tests with -d to catch intermittent failures, Use fork's repo for upgrade tests. (tlhackque) (#692) (2.41)
271271
# 2021-08-26 Improve upgrade check & make upgrade do a full install when possible (tlhackque) (#694) (2.42)
272272
# 2021-09-02 Fix version compare - cURL v8 may have single digit minor numbers. (tlhackque) (2.43)
273+
# 2021-09-26 Delete key file when key algorithm has changed (makuhama)
273274
# ----------------------------------------------------------------------------------------
274275

275276
case :$SHELLOPTS: in
@@ -819,11 +820,13 @@ check_getssl_upgrade() { # check if a more recent release is available
819820
if [ "$TEMP_UPGRADE_FILE" == "" ]; then
820821
error_exit "mktemp failed"
821822
fi
822-
CODE_LOCATION=$(sed -e"s/master/${release_tag}/" <<<"$CODE_LOCATION")
823+
CODE_LOCATION=$(sed -e"s/getssl\/master/${release_tag}/" <<<"$CODE_LOCATION")
824+
# shellcheck disable=SC2086
825+
debug curl ${_NOMETER:---silent} --user-agent "$CURL_USERAGENT" "$CODE_LOCATION" --output "$TEMP_UPGRADE_FILE"
823826
# shellcheck disable=SC2086
824827
curl ${_NOMETER:---silent} --user-agent "$CURL_USERAGENT" "$CODE_LOCATION" --output "$TEMP_UPGRADE_FILE"
825-
826828
errcode=$?
829+
827830
if [[ $errcode -eq 60 ]]; then
828831
error_exit "curl needs updating, your version does not support SNI (multiple SSL domains on a single IP)"
829832
elif [[ $errcode -gt 0 ]]; then
@@ -838,11 +841,11 @@ check_getssl_upgrade() { # check if a more recent release is available
838841
fi
839842

840843
if [[ ${_MUTE} -eq 0 ]]; then
841-
echo "Updated getssl from v${VERSION} to v${release_tag}"
844+
echo "Updated getssl from v${VERSION} to ${release_tag}"
842845
echo "The old version remains as ${0}.v${VERSION} and should be removed"
843846
echo "These update notifications can be turned off using the -Q option"
844847
echo ""
845-
echo "Updates are;"
848+
echo "Updates are:"
846849
awk "/\(${VERSION}\)$/ {s=1} s; /\(${release_tag}\)$/ || /^# ----/ {s=0}" "$TEMP_UPGRADE_FILE" | awk '{if(NR>1)print}'
847850
echo ""
848851
fi
@@ -2117,11 +2120,22 @@ json_get() { # get values from json
21172120

21182121
obtain_ca_resource_locations()
21192122
{
2123+
CURL_RESPONSE_FILE="$(mktemp 2>/dev/null || mktemp -t getssl.XXXXXX)"
2124+
21202125
for suffix in "" "/directory" "/dir";
21212126
do
21222127
# Obtain CA resource locations
21232128
# shellcheck disable=SC2086
2124-
ca_all_loc=$(curl ${_NOMETER} --user-agent "$CURL_USERAGENT" "${CA}${suffix}" 2>/dev/null)
2129+
ca_all_loc=$(curl ${_NOMETER} --user-agent "$CURL_USERAGENT" "${CA}${suffix}" 2> $CURL_RESPONSE_FILE)
2130+
errcode=$?
2131+
if [[ $errcode -ne 0 ]]; then
2132+
response=$(cat "$CURL_RESPONSE_FILE")
2133+
rm "$CURL_RESPONSE_FILE"
2134+
error_exit "ERROR curl \"$CA$suffix\" failed with $errcode and returned:\n$response"
2135+
else
2136+
rm "$CURL_RESPONSE_FILE"
2137+
fi
2138+
21252139
debug "ca_all_loc from ${CA}${suffix} gives $ca_all_loc"
21262140
# APIv1
21272141
URL_new_reg=$(echo "$ca_all_loc" | grep "new-reg" | awk -F'"' '{print $4}')
@@ -3146,6 +3160,22 @@ else
31463160
fi
31473161
debug "created SAN list = $SANLIST"
31483162

3163+
# check if private key alg has changed from RSA to EC (or vice versa)
3164+
if [[ "$DUAL_RSA_ECDSA" == "false" ]] && [[ -s "$DOMAIN_DIR/${DOMAIN}.key" ]]; then
3165+
case "${PRIVATE_KEY_ALG}" in
3166+
rsa)
3167+
if grep -q -- "-----BEGIN EC PRIVATE KEY-----" "$DOMAIN_DIR/${DOMAIN}.key"; then
3168+
rm -f "$DOMAIN_DIR/${DOMAIN}.key"
3169+
_FORCE_RENEW=1
3170+
fi ;;
3171+
prime256v1|secp384r1|secp521r1)
3172+
if grep -q -- "-----BEGIN RSA PRIVATE KEY-----" "$DOMAIN_DIR/${DOMAIN}.key"; then
3173+
rm -f "$DOMAIN_DIR/${DOMAIN}.key"
3174+
_FORCE_RENEW=1
3175+
fi ;;
3176+
esac
3177+
fi
3178+
31493179
# if there is an existing certificate file, check details.
31503180
if [[ -s "$CERT_FILE" ]]; then
31513181
debug "certificate $CERT_FILE exists"
@@ -3199,20 +3229,6 @@ if [[ "$REUSE_PRIVATE_KEY" != "true" ]]; then
31993229
fi
32003230
fi
32013231

3202-
# check if private key alg has changed from RSA to EC (or vice versa)
3203-
if [[ "$DUAL_RSA_ECDSA" == "false" ]] && [[ -s "$DOMAIN_DIR/${DOMAIN}.key" ]]; then
3204-
case "${PRIVATE_KEY_ALG}" in
3205-
rsa)
3206-
if grep --silent -- "-----BEGIN EC PRIVATE KEY-----" "$DOMAIN_DIR/${DOMAIN}.key"; then
3207-
rm -f "$DOMAIN_DIR/${DOMAIN}.key"
3208-
fi ;;
3209-
prime256v1|secp384r1|secp521r1)
3210-
if grep --silent -- "-----BEGIN RSA PRIVATE KEY-----" "$DOMAIN_DIR/${DOMAIN}.key"; then
3211-
rm -f "$DOMAIN_DIR/${DOMAIN}.key"
3212-
fi ;;
3213-
esac
3214-
fi
3215-
32163232
# create new domain keys if they don't already exist
32173233
if [[ "$DUAL_RSA_ECDSA" == "false" ]]; then
32183234
create_key "${PRIVATE_KEY_ALG}" "$DOMAIN_DIR/${DOMAIN}.key" "$DOMAIN_KEY_LENGTH"

test/0-test-curl-error.bats

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#! /usr/bin/env bats
2+
3+
load '/bats-support/load.bash'
4+
load '/bats-assert/load.bash'
5+
load '/getssl/test/test_helper.bash'
6+
7+
8+
# This is run for every test
9+
teardown() {
10+
[ -n "$BATS_TEST_COMPLETED" ] || touch $BATS_RUN_TMPDIR/failed.skip
11+
}
12+
13+
setup() {
14+
[ ! -f $BATS_RUN_TMPDIR/failed.skip ] || skip "skipping tests after first failure"
15+
#export CURL_CA_BUNDLE=/root/pebble-ca-bundle.crt
16+
}
17+
18+
19+
@test "Run getssl without pebble certificates to check the error message" {
20+
if [ -n "$STAGING" ]; then
21+
skip "Using staging server, skipping internal test"
22+
fi
23+
CONFIG_FILE="getssl-http01.cfg"
24+
setup_environment
25+
init_getssl
26+
create_certificate
27+
refute_line "getssl: unknown API version"
28+
assert_failure
29+
}

test/32-test-upgrade.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ teardown() {
161161

162162
# Check for current tag or file version otherwise push to master fails on a new version (or if the tag hasn't been updated)
163163
assert_line --regexp "Installed v(${CURRENT_TAG}|${FILE_VERSION}), restarting"
164-
assert_line "Configuration check successful"
164+
assert_line --partial "Configuration check successful"
165165
}
166166

167167

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#! /usr/bin/env bats
2+
3+
load '/bats-support/load.bash'
4+
load '/bats-assert/load.bash'
5+
load '/getssl/test/test_helper.bash'
6+
7+
8+
# This is run for every test
9+
teardown() {
10+
[ -n "$BATS_TEST_COMPLETED" ] || touch $BATS_RUN_TMPDIR/failed.skip
11+
}
12+
13+
setup() {
14+
[ ! -f $BATS_RUN_TMPDIR/failed.skip ] || skip "skipping tests after first failure"
15+
export CURL_CA_BUNDLE=/root/pebble-ca-bundle.crt
16+
}
17+
18+
teardown_file() {
19+
cleanup_environment
20+
}
21+
22+
@test "Create new certificate to create a private key" {
23+
if [ -n "$STAGING" ]; then
24+
skip "Using staging server, skipping internal test"
25+
fi
26+
CONFIG_FILE="getssl-http01.cfg"
27+
setup_environment
28+
init_getssl
29+
create_certificate
30+
assert_success
31+
check_output_for_errors
32+
# save a coy of the private key
33+
cp "${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/${GETSSL_CMD_HOST}.key" "${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/${GETSSL_CMD_HOST}.key.orig"
34+
}
35+
36+
@test "Renew certificate (not force) and check nothing happens and key doesn't change" {
37+
if [ -n "$STAGING" ]; then
38+
skip "Using staging server, skipping internal test"
39+
fi
40+
41+
ORIG_KEY_HASH="$(cat ${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/${GETSSL_CMD_HOST}.key | sha256sum)"
42+
43+
run ${CODE_DIR}/getssl -U -d $GETSSL_HOST
44+
assert_success
45+
assert_line --partial "certificate is valid for more than 30 days"
46+
check_output_for_errors
47+
48+
NEW_KEY_HASH="$(cat ${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/${GETSSL_CMD_HOST}.key | sha256sum)"
49+
50+
assert [ "$NEW_KEY_HASH" == "$ORIG_KEY_HASH" ]
51+
}
52+
53+
@test "Force renewal and check key hasn't changed" {
54+
if [ -n "$STAGING" ]; then
55+
skip "Using staging server, skipping internal test"
56+
fi
57+
ORIG_KEY_HASH="$(cat ${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/${GETSSL_CMD_HOST}.key | sha256sum)"
58+
59+
run ${CODE_DIR}/getssl -U -d -f $GETSSL_HOST
60+
assert_success
61+
check_output_for_errors
62+
63+
NEW_KEY_HASH="$(cat ${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/${GETSSL_CMD_HOST}.key | sha256sum)"
64+
65+
assert [ "$NEW_KEY_HASH" == "$ORIG_KEY_HASH" ]
66+
}
67+
68+
@test "Change key algorithm, force renewal, and check key has changed" {
69+
if [ -n "$STAGING" ]; then
70+
skip "Using staging server, skipping internal test"
71+
fi
72+
73+
ORIG_KEY_HASH="$(cat ${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/${GETSSL_CMD_HOST}.key | sha256sum)"
74+
75+
cat <<- 'EOF' > ${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/getssl_test_specific.cfg
76+
PRIVATE_KEY_ALG="prime256v1"
77+
EOF
78+
79+
run ${CODE_DIR}/getssl -U -d $GETSSL_HOST
80+
assert_success
81+
refute_line --partial "certificate is valid for more than 30 days"
82+
83+
check_output_for_errors
84+
85+
NEW_KEY_HASH="$(cat ${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/${GETSSL_CMD_HOST}.key | sha256sum)"
86+
87+
assert [ "$NEW_KEY_HASH" != "$ORIG_KEY_HASH" ]
88+
}

0 commit comments

Comments
 (0)