-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_gitlab_vuln_report.py
executable file
·268 lines (236 loc) · 8.74 KB
/
check_gitlab_vuln_report.py
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
import argparse
import sys
from pprint import pprint
from pathlib import Path
from pathlib import PurePath
import os.path
import csv
import json
from operator import itemgetter
import requests
# https://www.peterbe.com/plog/best-practice-with-retries-with-requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
def requests_retry_session(
retries=8,
backoff_factor=1,
status_forcelist=(500, 502, 504, 404),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def nagios_ok(*args):
print("OK: " + "\n".join(args))
sys.exit(0)
def nagios_warn(*args):
print("WARNING: " + "\n".join(args))
sys.exit(1)
def nagios_crit(*args):
print("CRITICAL: " + "\n".join(args))
sys.exit(2)
def nagios_unknown(*args):
print("UNKNOWN: " + "\n".join(args))
sys.exit(3)
# Debug
# import logging
# logging.basicConfig(level=logging.DEBUG)
try:
parser = argparse.ArgumentParser(
description='''
Monitor the vulnerabilities in Gitlab generated reports.
''')
parser.add_argument('--api',
help='''
The API URL to use. See
https://docs.gitlab.com/ee/api/vulnerability_exports.html for
examples. This can use project, group, or instance level reports.
Example:
"https://gitlab.my.org/api/v4/groups/234/vulnerability_exports".
''',
required=True)
parser.add_argument('--token',
help='''
Access token to use. This can be a personal access token, and
requires the "api" scope. Read
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/security.html#bestpractices
when using Nagios.
''',
required=True)
parser.add_argument('--diff',
help='''
Instead of the absolute number of vulnerabilties in the report, use
the difference compared to the previous report. This effectively
means you just see the changes. A cache file will be used to store
the relevant report details for comparison between checks. See also
the '--cachedir' option.
''',
action="store_true")
parser.add_argument('--warn', '-w',
help='''
Number of found vulnerabilities that should result in a WARNING
(default: 1)).
''',
required=False,
type=int,
default=1)
parser.add_argument('--crit', '-c',
help='''
Number of found vulnerabilities that should result in a CRITICAL
(default: 3)).
''',
required=False,
type=int,
default=3)
parser.add_argument('--severity',
help='''
Comma separated list of vulnerability severities to take into
account. Options: info, unknown, low, medium, high, criticial.
Default: "high,critical".
''',
required=False,
default='high,critical')
parser.add_argument('--status',
help='''
Comma separated list of vulnerability statuses to take into
account. Options: detected, confirmed, dismissed, resolved.
Default: "detected,confirmed".
''',
default='detected,confirmed',
required=False)
parser.add_argument('--cachedir',
help='''
Which directory to use for storing cached content (default:
"/tmp").
''',
default='/tmp',
required=False)
parser.add_argument('--verbose',
help='''
Show verbose output.
''',
action="store_true")
args = parser.parse_args()
# our arguments
api = args.api
diff = args.diff
warn = args.warn
crit = args.crit
cachedir = args.cachedir
verbose = args.verbose
headers = { 'PRIVATE-TOKEN': args.token }
# Split these into lists
severity = args.severity.split(',')
status = args.status.split(',')
cachefile = PurePath(cachedir, PurePath( __file__).name).with_suffix('.cache.json')
# Filter, dedupe, and sort
def filter_vuls(
vuls,
severity=severity,
status=status):
filtered = filter(lambda d:
d['Severity'] in severity and d['Status'] in status,
vuls)
deduped = [dict(t) for t in {tuple(d.items()) for d in filtered}]
# Sort on all keys
for sortkey in deduped[0].keys():
deduped = sorted(deduped, key=itemgetter(sortkey))
return deduped
# write cache to disk (serialised as JSON)
def write_cache(data, file=cachefile):
jsondata = json.dumps(filter_vuls(data), indent=4)
with open(file, "w") as c:
c.write(jsondata)
# Read JSON serialised cache from disk
def read_cache(file=cachefile):
with open(file, "r") as c:
content = json.loads(c.read())
return filter_vuls(content)
# Initial request to the API
r = requests.post(api, headers=headers)
r.raise_for_status()
# Fetch the download URI for the security report
security_report_download_url = r.json()['_links']['download']
# Request the report URL, using the retry logic, as the report will take some time
# to generate.
download = requests_retry_session().get(security_report_download_url, headers=headers)
download.raise_for_status()
# Generate dict structure from CSV data
reader = csv.DictReader(download.text.splitlines())
# Filter the results
vuls = filter_vuls(reader)
# Cosmetics
report_line = "See the entire report at " + security_report_download_url
# We are only interested in the difference with the previous report
if diff:
if os.path.isfile(cachefile):
# Fetch old vulnerabilities
old_vuls = read_cache()
# write new data to disk
write_cache(vuls)
if vuls != old_vuls:
# Calculate the difference between the old and new vulnerability lists
difference = [i for i in vuls if i not in old_vuls] + [j for j in old_vuls if j not in vuls]
plural = '' if len(difference) == 1 else 's'
if len(vuls) > len(old_vuls):
if len(difference) > crit:
nagios_crit(
str(len(difference)) + " new issue" + plural + " found",
json.dumps(difference, indent=4),
report_line
)
else:
nagios_warn(
str(len(difference)) + " new issue" + plural + " found",
json.dumps(difference, indent=4),
report_line
)
elif len(vuls) < len(old_vuls):
nagios_ok(
str(len(difference)) + " issue" + plural + " less found",
json.dumps(difference, indent=4),
report_line
)
else:
nagios_warn(
"Same amount of issues found, but different ones",
json.dumps(difference, indent=4),
report_line
)
else:
write_cache(vuls)
nagios_ok(
"No changes compared to previous check",
report_line
)
else:
# Save the current output to disk for next time
write_cache(vuls)
nagios_unknown(
"No cache file yet, generating it for next use"
)
found_items = len(vuls)
plural = '' if found_items == 1 else 's'
message = (
"Found " + str(found_items) + " issue" + plural + " with " +
"severity " + " or ".join(severity) +
", and " + "status " + " or ".join(status)
)
if found_items > crit:
nagios_crit(message, report_line)
elif found_items > warn:
nagios_warn(message, report_line)
else:
nagios_ok(message, report_line)
except Exception as e:
nagios_unknown(e)