Skip to content

Commit 80bf661

Browse files
committed
Check nginx + httpd + openssl version and unit tests.
1 parent ea59e14 commit 80bf661

File tree

5 files changed

+119
-24
lines changed

5 files changed

+119
-24
lines changed

config.sh

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ readonly SERVER_INIT_PATH='/etc/init.d/nginx'
6262
# The minimum version the server has to have for session ticket keys via files.
6363
readonly SERVER_MIN_VERSION='1.5.7'
6464

65+
# The minimum version the OpenSSL library requires for session ticket support.
66+
readonly OPENSSL_MIN_VERSION='0.9.8f'
67+
6568
# Absolute path to the cron program.
6669
readonly CRON_PATH='/etc/cron.d/session_ticket_key_rotation'
6770

@@ -202,31 +205,74 @@ system time and ensure all servers are in sync"
202205
fi
203206
}
204207

208+
# Check OpenSSL version which has (of course) an awkward formatting.
209+
#
210+
# ARGS:
211+
# $1 - The minimum required version.
212+
# RETURN:
213+
# 0 - If version is equal or greater.
214+
# 1 - If version is lower.
215+
check_openssl_version()
216+
{
217+
# Example output: `OpenSSL 1.0.1f 6 Jan 2014`
218+
OPENSSL_VERSION=$(openssl version)
219+
220+
OPENSSL_VERSION="${OPENSSL_VERSION#* }" # Remove smallest prefix space.
221+
OPENSSL_VERSION="${OPENSSL_VERSION%% *}" # Remove largest suffix space.
222+
# Now we have only `1.0.1f` left from above example.
223+
224+
# This one's complicated. We need an integer for -ge comparison and therefore
225+
# remove the last character and all dots from the version string. Afterwards
226+
# we get the last character and convert it to its ASCII code point.
227+
#
228+
# Note the leading single quote in front of the second command, that's what
229+
# converts the character to its code point.
230+
V1=$(printf -- '%s%03d' \
231+
"$(printf -- '%s' ${OPENSSL_VERSION} | head -c -1 | tr -d '.')" \
232+
"'$(printf -- '%s' ${OPENSSL_VERSION} | tail -c -1)")
233+
234+
# Now we need to do the same with the minimum version.
235+
V2=$(printf -- '%s%03d' \
236+
"$(printf -- '%s' ${1} | head -c -1 | tr -d '.')" \
237+
"'$(printf -- '%s' ${1} | tail -c -1)")
238+
239+
# Greater or equals is what we are interested in.
240+
if [ "${V1}" -ge "${V2}" ]
241+
then
242+
ok "Installed OpenSSL version is ${YELLOW}${OPENSSL_VERSION}${NORMAL}"
243+
else
244+
fail "Installed OpenSSL version is ${YELLOW}${OPENSSL_VERSION}${NORMAL} \
245+
which does not support session ticket keys. You need to install at least \
246+
version ${YELLOW}${2}${NORMAL}"
247+
fi
248+
}
249+
205250
# Check program version.
206251
#
252+
# NOTE: Works for nginx and Apache http (httpd).
207253
# ARGS:
208254
# $1 - The name of the program to check the version (must support -v option).
209255
# $2 - The minimum version.
210256
# RETURN:
211257
# 0 - If version is equal or greater.
212258
# 1 - If version is lower.
213-
check_version()
259+
check_server_version()
214260
{
215-
# Get version information from program.
216-
SERVER_VERSION=$("${1}" -v 2>&1)
217-
218-
# nginx specific, the format of the output looks like:
219-
# `nginx version: nginx/1.7.6`
220-
# We need to strip the part to the left of the slash.
221-
SERVER_VERSION="${SERVER_VERSION##*/}"
222-
223-
# Remove dots and leading zeros.
224-
V1=$(printf '%s' "${SERVER_VERSION}" | tr -d '.')
225-
V1="${V1##*0}"
226-
227-
# Remove dots and leading zeros.
228-
V2=$(printf '%s' "${2}" | tr -d '.')
229-
V2="${V2##*0}"
261+
# Get version information from program. The head call isn't necessary for
262+
# nginx but it is for httpd because it will output something like:
263+
# Server version: Apache/2.4.10
264+
# Server built: Jul 09 2014 07:22:45
265+
SERVER_VERSION=$("${1}" -v 2>&1 | head -n1)
266+
267+
# nginx: nginx version: nginx/1.7.6
268+
# httpd: Server version: Apache/2.4.10
269+
SERVER_VERSION="${SERVER_VERSION##*/}" # Remove longest match slash.
270+
# nginx: 1.7.6
271+
# httpd: 2.4.10
272+
273+
# Remove dots.
274+
V1=$(printf -- '%s' "${SERVER_VERSION}" | tr -d '.')
275+
V2=$(printf -- '%s' "${2}" | tr -d '.')
230276

231277
# Greater or equals is what we are interested in.
232278
if [ "${V1}" -ge "${V2}" ]

install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fi
6565
super_user
6666
check_ntpd
6767
is_installed "${SERVER}"
68-
check_version "${SERVER}" "${SERVER_MIN_VERSION}"
68+
check_server_version "${SERVER}" "${SERVER_MIN_VERSION}"
6969
check_filesystem "${FILESYSTEMS_PATH}"
7070

7171
# Simple fail only checks, we have to make sure that the currently configured

test/integration_test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ set -e
4141
WD=$(cd -- $(dirname -- "${0}"); pwd)
4242
. "${WD}/test.sh"
4343

44+
check_openssl_version "${OPENSSL_MIN_VERSION}"
45+
4446
# Clean-up everything on exit (any: see trap).
4547
teardown()
4648
{
@@ -63,8 +65,6 @@ teardown()
6365
}
6466
trap -- teardown 0 1 2 3 6 9 14 15
6567

66-
# We need faster rotation, otherwise this test is going to take days.
67-
6868
# Generate private key and certificate for localhost server.
6969
TEST_NAME='integration_test_key_cert'
7070
openssl req -x509 -nodes -days 1 -newkey rsa:2048 \
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
WD=$(cd -- $(dirname -- "${0}"); pwd)
3838
. "${WD}/test.sh"
3939

40-
check_version nginx 0.0.1 && test_ok || test_fail
41-
check_version nginx 99.99.99 && test_fail || test_ok
40+
check_openssl_version '0.0.1a' && test_ok || test_fail
41+
check_openssl_version "${OPENSSL_MIN_VERSION}" && test_ok || test_fail
42+
check_openssl_version '99.99.99z' && test_fail || test_ok
4243

4344
# Equal
44-
V=$(nginx -v 2>&1)
45-
V="${V##*/}"
46-
check_version nginx "${V}" && test_ok || test_fail
45+
V=$(openssl version)
46+
V="${V#* }"
47+
V="${V%% *}"
48+
check_openssl_version "${V}" && test_ok || test_fail

test/test_check_server_version.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
3+
# ------------------------------------------------------------------------------
4+
# This is free and unencumbered software released into the public domain.
5+
#
6+
# Anyone is free to copy, modify, publish, use, compile, sell, or
7+
# distribute this software, either in source code form or as a compiled
8+
# binary, for any purpose, commercial or non-commercial, and by any
9+
# means.
10+
#
11+
# In jurisdictions that recognize copyright laws, the author or authors
12+
# of this software dedicate any and all copyright interest in the
13+
# software to the public domain. We make this dedication for the benefit
14+
# of the public at large and to the detriment of our heirs and
15+
# successors. We intend this dedication to be an overt act of
16+
# relinquishment in perpetuity of all present and future rights to this
17+
# software under copyright law.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+
# OTHER DEALINGS IN THE SOFTWARE.
26+
#
27+
# For more information, please refer to <http://unlicense.org>
28+
# ------------------------------------------------------------------------------
29+
30+
# ------------------------------------------------------------------------------
31+
# AUTHOR: Richard Fussenegger <richard@fussenegger.info>
32+
# COPYRIGHT: Copyright (c) 2013 Richard Fussenegger
33+
# LICENSE: http://unlicense.org/ PD
34+
# LINK: http://richard.fussenegger.info/
35+
# ------------------------------------------------------------------------------
36+
37+
WD=$(cd -- $(dirname -- "${0}"); pwd)
38+
. "${WD}/test.sh"
39+
40+
check_server_version "${SERVER}" '0.0.1' && test_ok || test_fail
41+
check_server_version "${SERVER}" "${SERVER_MIN_VERSION}" && test_ok || test_fail
42+
check_server_version "${SERVER}" '99.99.99' && test_fail || test_ok
43+
44+
# Equal
45+
V=$("${SERVER}" -v 2>&1 | head -n1)
46+
V="${V##*/}"
47+
check_server_version "${SERVER}" "${V}" && test_ok || test_fail

0 commit comments

Comments
 (0)