-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_bundle_audit
executable file
·170 lines (138 loc) · 4.47 KB
/
check_bundle_audit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env bash
#
# check_bundle_audit - Nagios plugin for monitoring ruby applications for CVEs
# with bundler-audit, written in shell.
#
# Released under the MIT License.
#
# https://github.com/tommarshall/nagios-plugin-bundle-audit
#
VERSION=0.6.0
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
BUNDLE_AUDIT_PATH='bundle-audit'
CRITICAL_STATES='high,unknown'
WARNING_STATES='high,medium,low,unknown'
OK_STATES='high,medium,low,unknown'
#
# Output version.
#
version() {
echo "check_bundle_audit $VERSION"
}
#
# Output usage information.
#
usage() {
echo 'Usage: ./check_bundle_audit -p <path> [options]'
}
#
# Output help information.
#
help() {
usage
cat <<-EOF
Examples:
./check_bundle_audit -p /var/www/app
./check_bundle_audit -p /var/www/app -c all
./check_bundle_audit -p /var/www/app -c high -w medium,low,unknown
./check_bundle_audit -p /var/www/app -b /usr/local/bin/bundle-audit
./check_bundle_audit -p /var/www/app -i "CVE-2016-4658 CVE-2014-0083"
Options:
-p, --path <path> path to project
-b --bundle-audit-path path to bundle-audit gem
-w, --warning <criticalities> comma seperated CVE criticalities to treat as WARNING
-c, --critical <criticalities> comma seperated CVE criticalities to treat as CRITICAL
-i, --ignore <advisory ID(s)> space seperated advisories to ignore
-V, --version output version
-h, --help output help information
-c/--critical takes priority over -w/--warning.
For more information, see https://github.com/tommarshall/nagios-check-bundle-audit
EOF
}
#
# Parse argv.
#
while test $# -ne 0; do
ARG=$1; shift
case $ARG in
-p|--path) PROJECT_PATH=$1; shift ;;
-b|--bundle-audit-path) BUNDLE_AUDIT_PATH=$1; shift ;;
-w|--warning) WARNING_STATES=$1; shift ;;
-c|--critical) CRITICAL_STATES=$1; shift ;;
-i|--ignore) IGNORE_ARG="--ignore ${1}"; shift ;;
-V|--version) version; exit ;;
-h|--help) help; exit ;;
*)
echo "UNKNOWN: Unrecognised argument: $ARG"
usage >&2
exit $UNKNOWN
;;
esac
done
#
# Showtime.
#
# ensure we have bundle-audit
if ! command -v $BUNDLE_AUDIT_PATH > /dev/null; then
echo 'UNKNOWN: Unable to find bundle-audit'
exit $UNKNOWN
fi
# ensure we have a PROJECT_PATH
if [ -z "$PROJECT_PATH" ]; then
echo 'UNKNOWN: --path/-p not set'
exit $UNKNOWN
fi
# ensure Gemfile.lock exists
if [ ! -f "${PROJECT_PATH}/Gemfile.lock" ]; then
echo 'UNKNOWN: Unable to find Gemfile.lock'
exit $UNKNOWN
fi
# update the ruby advisory db
if ! $BUNDLE_AUDIT_PATH update >/dev/null 2>&1; then
echo "UNKNOWN: Unable to update ruby-advisory-db"
exit $UNKNOWN
fi
# run bundle-audit
REPORT=$(cd $PROJECT_PATH; $BUNDLE_AUDIT_PATH $IGNORE_ARG)
# parse report
LOW_VULN_COUNT=$(echo "$REPORT" | grep -c 'Criticality: Low')
MEDIUM_VULN_COUNT=$(echo "$REPORT" | grep -c 'Criticality: Medium')
HIGH_VULN_COUNT=$(echo "$REPORT" | grep -c 'Criticality: High')
UNKNOWN_VULN_COUNT=$(echo "$REPORT" | grep -c 'Criticality: Unknown')
TOTAL_VULN_COUNT=$((LOW_VULN_COUNT + MEDIUM_VULN_COUNT + HIGH_VULN_COUNT + UNKNOWN_VULN_COUNT))
# resolve state aliases
test "$CRITICAL_STATES" == 'all' && CRITICAL_STATES='unknown,high,medium,low'
test "$WARNING_STATES" == 'all' && WARNING_STATES='unknown,high,medium,low'
# report and exit
IFS=','
for CRITICAL_STATE in $CRITICAL_STATES; do
STATE_COUNT=$(echo "$REPORT" | grep -ic "Criticality: ${CRITICAL_STATE}")
if [ $STATE_COUNT -gt 0 ]; then
echo "CRITICAL: ${TOTAL_VULN_COUNT} vulnerabilities found (${HIGH_VULN_COUNT} high, ${MEDIUM_VULN_COUNT} medium, ${LOW_VULN_COUNT} low, ${UNKNOWN_VULN_COUNT} unknown)"
exit 2
fi
done
for WARNING_STATE in $WARNING_STATES; do
STATE_COUNT=$(echo "$REPORT" | grep -ic "Criticality: ${WARNING_STATE}")
if [ $STATE_COUNT -gt 0 ]; then
echo "WARNING: ${TOTAL_VULN_COUNT} vulnerabilities found (${HIGH_VULN_COUNT} high, ${MEDIUM_VULN_COUNT} medium, ${LOW_VULN_COUNT} low, ${UNKNOWN_VULN_COUNT} unknown)"
exit 1
fi
done
for OK_STATE in $OK_STATES; do
STATE_COUNT=$(echo "$REPORT" | grep -ic "Criticality: ${OK_STATE}")
if [ $STATE_COUNT -gt 0 ]; then
echo "OK: ${TOTAL_VULN_COUNT} vulnerabilities found (${HIGH_VULN_COUNT} high, ${MEDIUM_VULN_COUNT} medium, ${LOW_VULN_COUNT} low, ${UNKNOWN_VULN_COUNT} unknown)"
exit 0
fi
done
if [ "$REPORT" == 'No vulnerabilities found' ]; then
echo 'OK: No vulnerabilities found'
exit 0
fi
# catch all
echo 'UNKNOWN: Unable to parse bundle-audit report'
exit 3