-
Notifications
You must be signed in to change notification settings - Fork 2
/
report_data.py
518 lines (400 loc) · 25.6 KB
/
report_data.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
'''
Copyright 2020 Flexera Software LLC
See LICENSE.TXT for full license text
SPDX-License-Identifier: MIT
Author : sgeary
Created On : Fri Aug 07 2020
File : report_data.py
'''
import logging, re
import common.api.project.get_child_projects
import common.api.project.get_project_information
import common.api.project.get_inventory_summary
import common.api.license.license_lookup
import common.api.component.get_component_details
logger = logging.getLogger(__name__)
#-------------------------------------------------------------------#
def gather_data_for_report(baseURL, projectID, authToken, reportData):
logger.info("Entering gather_data_for_report")
reportOptions = reportData["reportOptions"]
# Parse report options
includeChildProjects = reportOptions["includeChildProjects"] # True/False
includeComplianceInformation = reportOptions["includeComplianceInformation"] # True/False
cvssVersion = reportOptions["cvssVersion"] # 2.0/3.x
maxVersionsBack = reportOptions["maxVersionsBack"] # Postive Int value
projectList = [] # List to hold parent/child details for report
inventoryData = {} # Create a dictionary containing the inventory data using inventoryID as keys
projectData = {} # Create a dictionary containing the project level summary data using projectID as keys
licenseDetails = {} # Dictionary to store license details to avoid multiple lookups for same id
projectReviewStatus = {}
totalInventoryCount = 0
# Get the list of parent/child projects start at the base project
projectHierarchy = common.api.project.get_child_projects.get_child_projects_recursively(baseURL, projectID, authToken)
# Create a list of project data sorted by the project name at each level for report display
# Add details for the parent node
nodeDetails = {}
nodeDetails["parent"] = "#" # The root node
nodeDetails["projectName"] = projectHierarchy["name"]
nodeDetails["projectID"] = projectHierarchy["id"]
nodeDetails["projectLink"] = baseURL + "/codeinsight/FNCI#myprojectdetails/?id=" + str(projectHierarchy["id"]) + "&tab=projectInventory"
topLevelProjectName = projectHierarchy["name"]
projectList.append(nodeDetails)
if includeChildProjects == "true":
projectList = create_project_hierarchy(projectHierarchy, projectHierarchy["id"], projectList, baseURL)
else:
logger.debug("Child hierarchy disabled")
projectInventoryCount = {}
# Gather the details for each project and summerize the data
for project in projectList:
projectID = project["projectID"]
projectName = project["projectName"]
projectLink = project["projectLink"]
# Get project information with rollup summary data
try:
projectInformation = common.api.project.get_project_information.get_project_information_summary(baseURL, projectID, authToken)
except:
logger.error(" No Project Information Returned for %s!" %projectName)
print("No Project Information Returned for %s." %projectName)
# Get project summary information
if cvssVersion == "3.x":
projectInventorySummary = common.api.project.get_inventory_summary.get_project_inventory_with_v3_summary(baseURL, projectID, authToken)
else:
projectInventorySummary = common.api.project.get_inventory_summary.get_project_inventory_with_v2_summary(baseURL, projectID, authToken)
if not projectInventorySummary:
logger.warning(" Project %s contains no inventory items" %projectName)
print("Project %s contains no inventory items." %projectName)
projectInventoryCount[projectName] = len(projectInventorySummary)
totalInventoryCount += len(projectInventorySummary)
# Create empty dictionary for project level data for this project
projectData[projectName] = {}
#############################################
# This area will be replaced by 2020R4 APIs
numApproved = 0
numRejected = 0
numDraft = 0
currentItem=0
for inventoryItem in projectInventorySummary:
currentItem +=1
complianceIssues = {}
inventoryID = inventoryItem["id"]
inventoryItemName = inventoryItem["name"]
logger.debug("Processing inventory items %s of %s" %(currentItem, len(projectInventorySummary)))
logger.debug(" Project: %s Inventory Name: %s Inventory ID: %s" %(projectName, inventoryItemName, inventoryID))
componentName = inventoryItem["componentName"]
componentID = inventoryItem["componentId"]
inventoryPriority = inventoryItem["priority"]
componentVersionName = inventoryItem["componentVersionName"]
selectedLicenseID = inventoryItem["selectedLicenseId"]
selectedLicenseName = inventoryItem["selectedLicenseSPDXIdentifier"]
if selectedLicenseID in licenseDetails.keys():
selectedLicenseName = licenseDetails[selectedLicenseID]["selectedLicenseName"]
selectedLicenseUrl = licenseDetails[selectedLicenseID]["selectedLicenseUrl"]
selectedLicensePriority = licenseDetails[selectedLicenseID]["selectedLicensePriority"]
else:
if selectedLicenseID != "N/A":
logger.debug(" Fetching license details for %s with ID %s" %(selectedLicenseName, selectedLicenseID ))
licenseInformation = common.api.license.license_lookup.get_license_details(baseURL, selectedLicenseID, authToken)
licenseURL = licenseInformation["url"]
spdxIdentifier = licenseInformation["spdxIdentifier"]
licensePriority = licenseInformation["priority"]
if spdxIdentifier != "" and spdxIdentifier != "N/A":
licenseName = spdxIdentifier
else:
licenseName = licenseInformation["shortName"]
# There is not specific selected licesne just let it be blank
if licenseName == "I don't know":
licenseName = ""
licenseDetails[selectedLicenseID] = {}
licenseDetails[selectedLicenseID]["selectedLicenseName"] = licenseName
licenseDetails[selectedLicenseID]["selectedLicenseUrl"] = licenseURL
licenseDetails[selectedLicenseID]["selectedLicensePriority"] = licensePriority
selectedLicenseName = licenseName
selectedLicenseUrl = licenseURL
selectedLicensePriority = licensePriority
else:
# Typically a WIP item
selectedLicenseName = ""
selectedLicenseUrl = ""
selectedLicensePriority = ""
# If there is no specific version just leave it blank
if componentVersionName == "N/A":
componentVersionName = ""
componentUrl = inventoryItem["url"]
inventoryReviewStatus = inventoryItem["reviewStatus"]
inventoryLink = baseURL + "/codeinsight/FNCI#myprojectdetails/?id=" + str(projectID) + "&tab=projectInventory&pinv=" + str(inventoryID)
try:
if cvssVersion == "3.x":
vulnerabilities = inventoryItem["vulnerabilitySummary"][0]["CvssV3"]
else:
vulnerabilities = inventoryItem["vulnerabilitySummary"][0]["CvssV2"]
vulnerabilityData = create_inventory_summary_dict(vulnerabilities, cvssVersion)
except:
logger.debug("No vulnerabilies for %s - %s" %(componentName, componentVersionName))
vulnerabilityData = {'numTotalVulnerabilities': 0, 'numCriticalVulnerabilities': 0, 'numHighVulnerabilities': 0, 'numMediumVulnerabilities': 0, 'numLowVulnerabilities': 0, 'numNoneVulnerabilities': 0}
# # If there is a SPDX value use that otherwise use the full name based on the
# if selectedLicenseSPDXIdentifier == "":
# selectedLicenseName = selectedLicenseID
# else:
# selectedLicenseName = selectedLicenseSPDXIdentifier
############################################################################################
# Determine if there are any compliance issues to report on
if includeComplianceInformation:
# Check review status
if inventoryReviewStatus == "Rejected":
complianceIssues["Item rejected"] = "This item has been rejected for use. Please consult with your legal and/or security team for further guidance."
elif inventoryReviewStatus == "Draft":
complianceIssues["Item not reviewed"] = "This item has not been reviewed for use. Please consult with your legal and/or security team for further guidance."
# Check if there is vulnerability data
if sum(vulnerabilityData.values()) > 0:
complianceIssues["Security vulnerabilities"] = "This item has associated security vulnerabilites. Please consult with your security team for further guidance."
# License compliance issue
if selectedLicensePriority == 1:
complianceIssues["P1 license"] = "This item has a viral or strong copyleft license. Depnding on your usage there may be additional oblilgations. Please consult with your legal team.."
# Component version compliance issue
if componentVersionName == "":
complianceIssues["Unknown version"] = "This item has an unknown version. Additional analysis is recommended."
else:
if componentID == "55720": # Is it the linux kernel?
# The API call for the versions of linux takes a long time due to teh massive number of versions so bypass
logger.debug("Linux kernel so skipping version analysis")
complianceIssues["Version not analyzed"] = "This versions for this component have not beeen analyzed. Manual inspection is suggested"
elif componentID == "6682478": # Is it the vim-vim?
# The API call for the versions of vim-vim takes a long time due to the massive number of versions so bypass
logger.debug("vim-vim so skipping version analysis")
complianceIssues["Version not analyzed"] = "This versions for this component have not beeen analyzed. Manual inspection is suggested"
else:
# Determine if there are any issues with the version
componentVersionDetails = getVersionDetails(componentVersionName, componentID, baseURL, authToken)
numberVersionsBack = componentVersionDetails["numberVersionsBack"]
if int(numberVersionsBack) >= int(maxVersionsBack):
latestVersion = componentVersionDetails["latestVersion"]
complianceIssues["Old version"] = "The latest version is " + latestVersion + ". Your version is " + str(numberVersionsBack) + " versions back from the latest version. You should consider upgrading to a more recent version of this component."
elif numberVersionsBack == -1:
complianceIssues["Invalid Version"] = componentVersionName + " is not a valid version for the current component."
# Was there a license selected?
if selectedLicenseName == "":
complianceIssues["Unspecified license"] = "This item has does not have a license associated with it. Additional analysis is recommended."
# Store the data for the inventory item for reporting
inventoryData[inventoryID] = {
"projectName" : projectName,
"inventoryItemName" : inventoryItemName,
"componentName" : componentName,
"componentVersionName" : componentVersionName,
"selectedLicenseName" : selectedLicenseName,
"vulnerabilityData" : vulnerabilityData,
"inventoryPriority" : inventoryPriority,
"componentUrl" : componentUrl,
"selectedLicenseUrl" : selectedLicenseUrl,
"inventoryReviewStatus" : inventoryReviewStatus,
"inventoryLink" : inventoryLink,
"projectLink" : projectLink,
"complianceIssues" : complianceIssues
}
#############################################
# Sum up inventory review status data
if inventoryReviewStatus == "Approved":
numApproved += 1
elif inventoryReviewStatus == "Rejected":
numRejected += 1
elif inventoryReviewStatus == "Draft":
numDraft += 1
else:
logger.error("Unknown inventoryReview Status: %s" %inventoryReviewStatus)
# Group all inventory based data into a dict wit the project name as the key
projectData[projectName]["numApproved"] = numApproved
projectData[projectName]["numRejected"] = numRejected
projectData[projectName]["numDraft"] = numDraft
projectData[projectName]["numP1Licenses"] = projectInformation["licenses"]["P1"]
projectData[projectName]["numP2Licenses"] = projectInformation["licenses"]["P2"]
projectData[projectName]["numP3Licenses"] = projectInformation["licenses"]["P3"]
projectData[projectName]["numNALicenses"] = projectInformation["licenses"]["Unknown"]
# Determine the state of the project based on the "worst" status of inventory
if numRejected > 0:
project.update({"projectReviewStatus": "Rejected"})
projectReviewStatus[projectID] = "Rejected"
elif numDraft > 0:
project.update({"projectReviewStatus": "Draft"})
projectReviewStatus[projectID] = "Draft"
else:
project.update({"projectReviewStatus": "Approved"})
projectReviewStatus[projectID] = "Approved"
if cvssVersion == "3.x":
projectData[projectName]["numCriticalVulnerabilities"] = projectInformation["vulnerabilities"]["CvssV3"]["Critical"]
projectData[projectName]["numHighVulnerabilities"] = projectInformation["vulnerabilities"]["CvssV3"]["High"]
projectData[projectName]["numMediumVulnerabilities"] = projectInformation["vulnerabilities"]["CvssV3"]["Medium"]
projectData[projectName]["numLowVulnerabilities"] = projectInformation["vulnerabilities"]["CvssV3"]["Low"]
projectData[projectName]["numNoneVulnerabilities"] = projectInformation["vulnerabilities"]["CvssV3"]["None"]
else:
projectData[projectName]["numHighVulnerabilities"] = projectInformation["vulnerabilities"]["CvssV2"]["High"]
projectData[projectName]["numMediumVulnerabilities"] = projectInformation["vulnerabilities"]["CvssV2"]["Medium"]
projectData[projectName]["numLowVulnerabilities"] = projectInformation["vulnerabilities"]["CvssV2"]["Low"]
projectData[projectName]["numNoneVulnerabilities"] = projectInformation["vulnerabilities"]["CvssV2"]["Unknown"]
projectData[projectName]["projectLink"] = projectLink
# Roll up the inventortory data at a project level for display charts
projectSummaryData = create_project_summary_data_dict(projectData)
projectSummaryData["includeComplianceInformation"] = includeComplianceInformation
projectSummaryData["cvssVersion"] = cvssVersion
# Roll up the individual project data to the application level
applicationSummaryData = create_application_summary_data_dict(projectSummaryData)
# Roll up the project review status based on the status of child projects
projectReviewStatus = roll_up_project_review_level(projectHierarchy, projectReviewStatus, 1)
# Build up the data to return for the
reportData["topLevelProjectName"] = topLevelProjectName
reportData["projectHierarchy"] = projectHierarchy
reportData["projectName"] = projectHierarchy["name"]
reportData["projectID"] = projectHierarchy["id"]
reportData["inventoryData"] = inventoryData
reportData["projectList"] =projectList
reportData["projectSummaryData"] = projectSummaryData
reportData["applicationSummaryData"] = applicationSummaryData
reportData["projectInventoryCount"] = projectInventoryCount
reportData["totalInventoryCount"] = totalInventoryCount
reportData["projectReviewStatus"] = projectReviewStatus
logger.info("Exiting gather_data_for_report")
return reportData
#----------------------------------------------------------------------
def create_inventory_summary_dict(vulnerabilities,cvssVersion):
logger.info("Entering create_inventory_summary_dict")
vulnerabilityData = {}
vulnerabilityData["numTotalVulnerabilities"] = sum(vulnerabilities.values())
vulnerabilityData["numHighVulnerabilities"] = vulnerabilities["High"]
vulnerabilityData["numMediumVulnerabilities"] = vulnerabilities["Medium"]
vulnerabilityData["numLowVulnerabilities"] = vulnerabilities["Low"]
if cvssVersion == "2.0":
vulnerabilityData["numNoneVulnerabilities"] = vulnerabilities["Unknown"]
elif cvssVersion == "3.x":
vulnerabilityData["numCriticalVulnerabilities"] = vulnerabilities["Critical"]
vulnerabilityData["numNoneVulnerabilities"] = vulnerabilities["None"]
return vulnerabilityData
#----------------------------------------------#
def create_project_hierarchy(project, parentID, projectList, baseURL):
logger.debug("Entering create_project_hierarchy")
# Are there more child projects for this project?
if len(project["childProject"]):
# Sort by project name of child projects
for childProject in sorted(project["childProject"], key = lambda i: i['name'] ) :
uniqueProjectID = str(parentID) + "-" + str(childProject["id"])
nodeDetails = {}
nodeDetails["projectID"] = childProject["id"]
nodeDetails["parent"] = parentID
nodeDetails["uniqueID"] = uniqueProjectID
nodeDetails["projectName"] = childProject["name"]
nodeDetails["projectLink"] = baseURL + "/codeinsight/FNCI#myprojectdetails/?id=" + str(childProject["id"]) + "&tab=projectInventory"
projectList.append( nodeDetails )
create_project_hierarchy(childProject, uniqueProjectID, projectList, baseURL)
return projectList
#----------------------------------------------------------------------------------------#
def create_project_summary_data_dict(projectData):
logger.debug("Entering get_project_summary_data")
# For the chart data we need to create lists where each element is in the correct order based on the
# project name order. i.e. one list will # of approved items for each project in the correct order
projectSummaryData = {}
# Create empty lists for each metric that we need for the report
for projectName in projectData:
for metric in projectData[projectName]:
if metric not in ["P1InventoryItems", "projectLink"]: # We don't care about these for now
projectSummaryData[metric] = []
# Grab the data for each project and add it in the correct order
for projectName in projectData:
for metric in projectData[projectName]:
if metric not in ["P1InventoryItems", "projectLink", "cvssVersion"]: # We don't care about these for now
projectSummaryData[metric].append(projectData[projectName][metric])
projectSummaryData["projectNames"] = list(projectData.keys())
logger.debug("Exiting get_project_summary_data")
return projectSummaryData
#----------------------------------------------------------------------------------------#
def create_application_summary_data_dict(projectSummaryData):
logger.debug("Entering get_application_summary_data")
applicationSummaryData = {}
# For each metric sum the data up
for metric in projectSummaryData:
if metric == "cvssVersion":
applicationSummaryData[metric] = projectSummaryData[metric]
elif metric != "projectNames" and metric != "includeComplianceInformation":
applicationSummaryData[metric] = sum(projectSummaryData[metric])
logger.debug("Exiting get_application_summary_data")
return applicationSummaryData
#----------------------------------------------------------------------------------------#
def getVersionDetails(componentVersionName, componentID, baseURL, authToken):
logger.debug("Entering getVersionDetails")
componentVersionDetails = {}
# Is it a valid component
if componentID == "N/A":
return componentVersionDetails
versionDetails = common.api.component.get_component_details.get_component_details_v3_summary(baseURL, componentID, authToken)
componentVersionList = versionDetails["data"]["versionList"]
# Are there any versions?
if not len(componentVersionList):
return componentVersionDetails
componentVersions = [] # To hold just the version names that can be processed
ignoreVersions = ["unknown", "custom", "any version", "sample"]
# Extract just the version names for comparison purposes
for version in componentVersionList:
versionName = version["name"]
if versionName.lower() not in ignoreVersions:
componentVersions.append(versionName)
else:
logger.warning(" The version %s is contained in the ingnoreVersions list" %versionName)
componentVersions.sort(key=natural_sort)
# In case there is a combination of version types 1.2 vs snapshot etc
# move the ones starting with text to the begining of the list
insertLocation = 0
for version in componentVersions:
if len(version) and version[0].isalpha():
componentVersions.remove(version)
componentVersions.insert(insertLocation , version)
insertLocation +=1
totalNumberVersions = len(componentVersions)
if totalNumberVersions > 0:
# There is at least one version available
try:
selectedVersionIndex = componentVersions.index(componentVersionName)
numberVersionsBack = totalNumberVersions-selectedVersionIndex - 1 # How far back from most recent release
except:
logger.error(" versionName %s is not a valid version for the component with ID %s" %(componentVersionName, componentID))
numberVersionsBack = -1
componentVersionDetails["latestVersion"] = componentVersions[-1]
componentVersionDetails["numberVersionsBack"] = numberVersionsBack
else:
# The version that was selected must be in the ignoreVersions list above
componentVersionDetails["totalNumberVersions"] = 1
componentVersionDetails["latestVersion"] = componentVersionName
componentVersionDetails["numberVersionsBack"] = 0
componentVersionDetails["currentVersion"] = componentVersionName
logger.debug(" componentVersionDetails: %s" %componentVersionDetails)
return componentVersionDetails
#----------------------------------------------------------------------------------------#
def roll_up_project_review_level(projectHierarchy, projectReviewStatus, recursionLevel):
recursionIndent = recursionLevel * " "
logger.debug("%s Entering roll_up_project_review_level" %recursionIndent)
parentProjectID = projectHierarchy['id']
parentProjectName = projectHierarchy['name']
parentProjectReviewStatus = projectReviewStatus[parentProjectID]
logger.debug("%s %s - Initial project review status: %s" %(recursionIndent, parentProjectName, parentProjectReviewStatus))
childProject = projectHierarchy["childProject"]
recursionLevel +=1
recursionIndent = recursionLevel * " "
for project in childProject:
# Get the most recent value for the parent project review status in case a child already changed it
parentProjectReviewStatus = projectReviewStatus[parentProjectID]
projectReviewStatus = roll_up_project_review_level(project, projectReviewStatus, recursionLevel)
childProjectID = project["id"]
childProjectReviewSatus = projectReviewStatus[childProjectID]
logger.debug("%s %s - Current project review status: %s Current child status: %s" %(recursionIndent, parentProjectName, parentProjectReviewStatus, childProjectReviewSatus))
# Does the parent project status need to change to the a child project?
if childProjectReviewSatus == "Rejected":
logger.debug(" Change %s to rejected" %parentProjectName)
projectReviewStatus[parentProjectID] = "Rejected"
elif childProjectReviewSatus == "Draft" and parentProjectReviewStatus == "Approved":
logger.debug(" Change %s to draft" %parentProjectName)
projectReviewStatus[parentProjectID] = "Draft"
logger.debug("%s %s - Final project review status: %s" %(recursionIndent, parentProjectName, parentProjectReviewStatus))
return projectReviewStatus
#-----------------------------
# These two functions are used for naturally sorting the version names
# which was pulled from the web
# https://www.tutorialspoint.com/How-to-correctly-sort-a-string-with-a-number-inside-in-Python
def atoi(text):
return int(text) if text.isdigit() else text
def natural_sort(text):
return [ atoi(c) for c in re.split('(\d+)',text) ]