-
Notifications
You must be signed in to change notification settings - Fork 3
/
dependa2.py
391 lines (313 loc) · 13.1 KB
/
dependa2.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/bin/python3
#
# Basic Python script to gather dependabot alert information across all repos
# listed.
#
# There are likely more optimal methods to implement this script.
# For the short term this works. To be revisited shortly.
import json
import pprint
import re
import csv
from datetime import datetime
from pathlib import Path
class Repo:
def __init__(self, name, repo_dict):
self.repo_dict = repo_dict
(
state_open,
state_fixed,
state_dismissed,
) = self.get_state_data()
combined_data = {
**state_open,
**state_fixed,
**state_dismissed,
}
# returned the parsed data as a single large dictionary
self.parsed_data = {"Name": name}
self.parsed_data.update(combined_data)
def get_slo(self):
CRIT_MAX_SLO_DAYS = 15
HIGH_MAX_SLO_DAYS = 30
MED_MAX_SLO_DAYS = 90
LOW_MAX_SLO_DAYS = 180
current_time = datetime.now()
slo = {
"Crit Exceeded": 0,
"Crit Total": self.parsed_data["Open Crit"],
"High Exceeded": 0,
"High Total": self.parsed_data["Open High"],
"Med Exceeded": 0,
"Med Total": self.parsed_data["Open Med"],
"Low Exceeded": 0,
"Low Total": self.parsed_data["Open Low"],
}
for item in self.repo_dict:
if item["state"] == "open":
if item["security_advisory"]["severity"] == "critical":
temp_date = item["security_advisory"]["published_at"]
temp_date_obj = datetime.strptime(
temp_date, "%Y-%m-%dT%H:%M:%SZ"
)
crit_age = current_time - temp_date_obj
if crit_age.days >= CRIT_MAX_SLO_DAYS:
slo["Crit Exceeded"] += 1
elif item["security_advisory"]["severity"] == "high":
temp_date = item["security_advisory"]["published_at"]
temp_date_obj = datetime.strptime(
temp_date, "%Y-%m-%dT%H:%M:%SZ"
)
high_age = current_time - temp_date_obj
if high_age.days >= HIGH_MAX_SLO_DAYS:
slo["High Exceeded"] += 1
elif item["security_advisory"]["severity"] == "medium":
temp_date = item["security_advisory"]["published_at"]
temp_date_obj = datetime.strptime(
temp_date, "%Y-%m-%dT%H:%M:%SZ"
)
medium_age = current_time - temp_date_obj
if medium_age.days >= MED_MAX_SLO_DAYS:
slo["Med Exceeded"] += 1
elif item["security_advisory"]["severity"] == "low":
temp_date = item["security_advisory"]["published_at"]
temp_date_obj = datetime.strptime(
temp_date, "%Y-%m-%dT%H:%M:%SZ"
)
low_age = current_time - temp_date_obj
if low_age.days >= LOW_MAX_SLO_DAYS:
slo["Low Exceeded"] += 1
return slo
def get_state_data(self):
# template dictionary keys; allows reuse of nested parse_data function
state_template = {
"Total": 0,
"Crit": 0,
"High": 0,
"Med": 0,
"Low": 0,
"Date": "",
"Npm": 0,
"Pip": 0,
"Rubygems": 0,
"Nuget": 0,
"Maven": 0,
"Composer": 0,
"Rust": 0,
"Unknown": 0,
}
state_open = dict(state_template)
date_list_open = []
state_fixed = dict(state_template)
date_list_fixed = []
state_dismissed = dict(state_template)
date_list_dismissed = []
def parse_data(item_dict, parsed_dict):
parsed_dict["Total"] += 1
if item_dict["security_advisory"]["severity"] == "critical":
parsed_dict["Crit"] += 1
elif item_dict["security_advisory"]["severity"] == "high":
parsed_dict["High"] += 1
elif item_dict["security_advisory"]["severity"] == "medium":
parsed_dict["Med"] += 1
else:
parsed_dict["Low"] += 1
if item_dict["dependency"]["package"]["ecosystem"] == "npm":
parsed_dict["Npm"] += 1
elif item_dict["dependency"]["package"]["ecosystem"] == "pip":
parsed_dict["Pip"] += 1
elif item_dict["dependency"]["package"]["ecosystem"] == "rubygems":
parsed_dict["Rubygems"] += 1
elif item_dict["dependency"]["package"]["ecosystem"] == "nuget":
parsed_dict["Nuget"] += 1
elif item_dict["dependency"]["package"]["ecosystem"] == "maven":
parsed_dict["Maven"] += 1
elif item_dict["dependency"]["package"]["ecosystem"] == "composer":
parsed_dict["Composer"] += 1
elif item_dict["dependency"]["package"]["ecosystem"] == "rust":
parsed_dict["Rust"] += 1
else:
parsed_dict["Unknown"] += 1
return parsed_dict
for item in self.repo_dict:
if item["state"] == "open":
state_open = parse_data(item, state_open)
# keep only first reported open alert date
temp_pub_at_date = item["security_advisory"]["published_at"]
date_list_open.append(
datetime.strptime(temp_pub_at_date, "%Y-%m-%dT%H:%M:%SZ")
)
state_open["Date"] = str(min(date_list_open))
elif item["state"] == "fixed":
state_fixed = parse_data(item, state_fixed)
# keep only most recent fixed alert date
temp_fixed_at_date = item["fixed_at"]
date_list_fixed.append(
datetime.strptime(temp_fixed_at_date, "%Y-%m-%dT%H:%M:%SZ")
)
state_fixed["Date"] = str(max(date_list_fixed))
elif item["state"] == "dismissed":
state_dismissed = parse_data(item, state_dismissed)
# keep only most recent dismissed alert date
temp_dismissed_at_date = item["dismissed_at"]
date_list_dismissed.append(
datetime.strptime(
temp_dismissed_at_date, "%Y-%m-%dT%H:%M:%SZ"
)
)
state_dismissed["Date"] = str(max(date_list_dismissed))
# amend the dictionaries keys to reflect the state data
state_open = {
f"Open {key}": value for key, value in state_open.items()
}
state_fixed = {
f"Fixed {key}": value for key, value in state_fixed.items()
}
state_dismissed = {
f"Dismissed {key}": value for key, value in state_dismissed.items()
}
# set a priority level for remediation for open alerts
priority = state_open["Open Crit"] + state_open["Open High"]
state_open["Priority"] = priority
return state_open, state_fixed, state_dismissed
def get_files(dir):
repos_list = []
repos_no_alerts = []
repos_dependabot_disabled = []
input_list = Path(dir).iterdir()
# this is the wrong way to determine repo status! Todo:
# 1. Detect repos with alerts via empty item return
# 2. Determine disabled dependabot repo via presence of message key
for item in input_list:
if item.is_file():
# remove files (repos) less than 5 bytes; likely repos without
# dependabot alerts
if item.stat().st_size < 5:
repos_no_alerts.append(item)
continue
# remove files (repos) less than 500 bytes; likely repos with
# dependabot disabled
if item.stat().st_size < 500 and item.stat().st_size > 6:
repos_dependabot_disabled.append(item)
continue
repos_list.append(item)
# remove repos with no alerts from the list to check for vulns
repos_list = list(set(repos_list) - set(repos_no_alerts))
# remove repos with dependbot alerts disabled
repos_list = list(set(repos_list) - set(repos_dependabot_disabled))
return repos_list, repos_no_alerts, repos_dependabot_disabled
def validate_json(json_list):
valid_json_files = []
invalid_json_files = []
for json_file in json_list:
with open(json_file) as file:
try:
json.load(file)
valid_json_files.append(json_file.name)
except ValueError as err:
print("invalid json file: " + str(json_file))
invalid_json_files.append(json_file)
# find and replace where paginate occurred during http requests
# this regex is only valid because jq was used to reformat the returned
# json; this is not an ideal process!
for json_file in invalid_json_files:
with open(json_file) as file:
json_read = file.read()
# match = re.findall(r" \}\n\]\n\[", json_read)
match = re.sub(r" \}\n\]\n\[", r"},", json_read, count=25)
with open(json_file, "w") as file:
file.write(match)
return valid_json_files, invalid_json_files
def write_org_data(
repos_no_vulns, repos_with_vulns, repos_disabled, parsed_data
):
num_no_vulns = len(repos_no_vulns)
num_with_vulns = len(repos_with_vulns)
num_disabled = len(repos_disabled)
total_repos = num_no_vulns + num_with_vulns + num_disabled
org_data = {
"Total Number of Repos": total_repos,
"Repos with alerts": num_with_vulns,
"Repos without alerts": num_no_vulns,
"Repos disabled alerts": num_disabled,
"Open Critical": 0,
"Open High": 0,
"Open Medium": 0,
"Open Low": 0,
"Open Npm": 0,
"Open Pip": 0,
"Open Rubygems": 0,
"Open Nuget": 0,
"Open Maven": 0,
"Open Composer": 0,
"Open Rust": 0,
"Open Unknown": 0,
}
for data in range(len(parsed_data)):
org_data["Open Critical"] += parsed_data[data]["Open Crit"]
org_data["Open High"] += parsed_data[data]["Open High"]
org_data["Open Medium"] += parsed_data[data]["Open Med"]
org_data["Open Low"] += parsed_data[data]["Open Low"]
org_data["Open Npm"] += parsed_data[data]["Open Npm"]
org_data["Open Pip"] += parsed_data[data]["Open Pip"]
org_data["Open Rubygems"] += parsed_data[data]["Open Rubygems"]
org_data["Open Nuget"] += parsed_data[data]["Open Nuget"]
org_data["Open Maven"] += parsed_data[data]["Open Maven"]
org_data["Open Composer"] += parsed_data[data]["Open Composer"]
org_data["Open Rust"] += parsed_data[data]["Open Rust"]
org_data["Open Unknown"] += parsed_data[data]["Open Unknown"]
org_data_header = org_data.keys()
org_data_csv = "org_data2.csv"
with open(org_data_csv, "w") as org_data_file:
writer = csv.DictWriter(org_data_file, fieldnames=org_data_header)
writer.writeheader()
writer.writerow(org_data)
print()
print(f"CSV of all organization data written to {org_data_csv}")
print()
def write_csv_data(sorted_data):
repo_header = sorted_data[0].keys()
parsed_data_csv = "parsed_data2.csv"
with open(parsed_data_csv, "w") as parsed_data_file:
writer = csv.DictWriter(parsed_data_file, fieldnames=repo_header)
writer.writeheader()
writer.writerows(sorted_data)
print()
print(f"CSV of all dependabot repos written to {parsed_data_csv}")
def write_txt_data(sorted_data):
parsed_data_txt = "parsed_data2.txt"
with open(parsed_data_txt, "w") as parsed_data_file:
pp = pprint.PrettyPrinter(
depth=4, sort_dicts=False, stream=parsed_data_file
)
pp.pprint(sorted_data)
print()
print(f"Text file of all dependabot repos written to {parsed_data_txt}")
def main():
parsed_data = []
input_files, files_no_alerts, files_dependabot_disabled = get_files(
"./output"
)
# valid_files and invalid_files vars not used, but potentially needed
# to track paginated requests from repos with a lot of json data
valid_files, invalid_files = validate_json(input_files)
for input_file in input_files:
with open(input_file, "r") as f:
dependa_dict = json.load(f)
# create object for every repo
input_file = Repo(input_file.stem, dependa_dict)
# parsed_data.append(vars(input_file))
parsed_data.append(input_file.parsed_data)
all_slos = input_file.get_slo()
print(all_slos)
# sort the data by priority number (sum of high and critical vulns)
sorted_data = sorted(
parsed_data, key=lambda d: d["Priority"], reverse=True
)
write_csv_data(sorted_data)
write_txt_data(sorted_data)
write_org_data(
files_no_alerts, input_files, files_dependabot_disabled, parsed_data
)
if __name__ == "__main__":
main()