-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_data.py
294 lines (223 loc) · 12.8 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
'''
Copyright 2021 Flexera Software LLC
See LICENSE.TXT for full license text
SPDX-License-Identifier: MIT
Author : sgeary
Created On : Wed Oct 06 2021
File : report_data.py
'''
import logging
from collections import OrderedDict
import common.application_details
import common.project_heirarchy
import common.api.project.get_inventory_summary
import common.api.project.get_project_information
import common.api.license.license_lookup
import purl
logger = logging.getLogger(__name__)
logging.getLogger("urllib3").setLevel(logging.WARNING) # Disable logging for requests module
#-------------------------------------------------------------------#
def gather_data_for_report(baseURL, projectID, authToken, reportData):
logger.info("Entering gather_data_for_report")
# Parse report options
reportOptions = reportData["reportOptions"]
includeChildProjects = reportOptions["includeChildProjects"] # True/False
includeVulnerabilities = reportOptions["includeVulnerabilities"] # True/False
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
applicationDetails = {} # Dictionary to allow a project to be mapped to an application name/version
# applicationDetails = common.application_details.determine_application_details(projectID, baseURL, authToken)
projectList = common.project_heirarchy.create_project_heirarchy(baseURL, authToken, projectID, includeChildProjects)
topLevelProjectName = projectList[0]["projectName"]
# 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)
projectInventoryCount = {}
# Gather the details for each project and summerize the data
for project in projectList:
projectID = project["projectID"]
projectName = project["projectName"]
projectLink = project["projectLink"]
applicationDetails[projectName] = determine_application_details(baseURL, projectName, projectID, authToken)
applicationNameVersion = applicationDetails[projectName]["applicationNameVersion"]
# Add the applicationNameVersion to the project hierarchy
project["applicationNameVersion"] = applicationNameVersion
# Include vulnerability data?
if includeVulnerabilities:
# Just default to v3 summary data
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_without_vulns_summary(baseURL, projectID, authToken)
if not projectInventorySummary:
logger.warning(" Project contains no inventory items")
print("Project contains no inventory items.")
# Create empty dictionary for project level data for this project
projectData[projectName] = {}
currentItem=0
projectInventoryCount[projectName] = len(projectInventorySummary)
for inventoryItem in projectInventorySummary:
inventoryType = inventoryItem["type"]
# This is not a component for move to the next item
if inventoryType != "Component":
continue
currentItem +=1
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"]
componentVersionName = inventoryItem["componentVersionName"]
selectedLicenseID = inventoryItem["selectedLicenseId"]
selectedLicenseName = inventoryItem["selectedLicenseSPDXIdentifier"]
if reportData["releaseVersion"] >= "2024R1":
purlString = inventoryItem["purl"]
else:
# Attempt to generate a purl string for the component
try:
purlString = purl.get_purl_string(inventoryItem, baseURL, authToken)
except:
logger.warning("Unable to create purl string for inventory item %s." %inventoryItemName)
purlString = ""
if selectedLicenseID in licenseDetails.keys():
selectedLicenseName = licenseDetails[selectedLicenseID]["selectedLicenseName"]
selectedLicenseUrl = licenseDetails[selectedLicenseID]["selectedLicenseUrl"]
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
else:
# Typically a WIP item
selectedLicenseName = ""
selectedLicenseUrl = ""
# If there is no specific version just leave it blank
if componentVersionName == "N/A":
componentVersionName = ""
# If there is no license URL set it to blank
if selectedLicenseUrl is None:
selectedLicenseUrl = ""
componentUrl = inventoryItem["url"]
inventoryLink = baseURL + "/codeinsight/FNCI#myprojectdetails/?id=" + str(projectID) + "&tab=projectInventory&pinv=" + str(inventoryID)
# Determine if there are any vulnerabilities
try:
vulnerabilities = inventoryItem["vulnerabilitySummary"][0]["CvssV3"]
if sum(vulnerabilities.values()):
hasVulnerabilities=True
else:
hasVulnerabilities=False
except:
logger.info(" No vulnerabilies for %s - %s" %(componentName, componentVersionName))
hasVulnerabilities=False
# Store the data for the inventory item for reporting
inventoryData[inventoryID] = {
"projectName" : projectName,
"inventoryItemName" : inventoryItemName,
"componentName" : componentName,
"componentVersionName" : componentVersionName,
"selectedLicenseName" : selectedLicenseName,
"componentUrl" : componentUrl,
"selectedLicenseUrl" : selectedLicenseUrl,
"inventoryLink" : inventoryLink,
"projectLink" : projectLink,
"hasVulnerabilities" : hasVulnerabilities,
"applicationNameVersion" : applicationNameVersion,
"purlString" : purlString
}
projectData[projectName]["projectLink"] = projectLink
# Sort the inventory data by Component Name / Component Version / Selected License Name
sortedInventoryData = OrderedDict(sorted(inventoryData.items(), key=lambda x: (x[1]['componentName'], x[1]['componentVersionName'], x[1]['selectedLicenseName']) ) )
# Build up the data to return for the
reportData["projectHierarchy"] = projectHierarchy
reportData["topLevelProjectName"] = topLevelProjectName
reportData["inventoryData"] = sortedInventoryData
reportData["projectList"] =projectList
reportData["reportOptions"] =reportOptions
reportData["projectInventoryCount"] = projectInventoryCount
reportData["applicationDetails"] = applicationDetails
return reportData
#----------------------------------------------#
def create_project_hierarchy(project, parentID, projectList, baseURL):
logger.debug("Entering create_project_hierarchy.")
logger.debug(" Project Details: %s" %project)
# 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 determine_application_details(baseURL, projectName, projectID, authToken):
logger.debug("Entering determine_application_details.")
# Create a application name for the report if the custom fields are populated
# Default values
applicationName = projectName
applicationVersion = ""
applicationPublisher = ""
applicationDetailsString = ""
projectInformation = common.api.project.get_project_information.get_project_information_summary(baseURL, projectID, authToken)
# Project level custom fields added in 2022R1
if "customFields" in projectInformation:
customFields = projectInformation["customFields"]
# See if the custom project fields were propulated for this project
for customField in customFields:
# Is there the reqired custom field available?
if customField["fieldLabel"] == "Application Name":
if customField["value"]:
applicationName = customField["value"]
# Is the custom version field available?
if customField["fieldLabel"] == "Application Version":
if customField["value"]:
applicationVersion = customField["value"]
# Is the custom Publisher field available?
if customField["fieldLabel"] == "Application Publisher":
if customField["value"]:
applicationPublisher = customField["value"]
# Join the custom values to create the application name for the report artifacts
if applicationName != projectName:
if applicationVersion != "":
applicationNameVersion = applicationName + " - " + applicationVersion
else:
applicationNameVersion = applicationName
else:
applicationNameVersion = projectName
if applicationPublisher != "":
applicationDetailsString += "Publisher: " + applicationPublisher + " | "
# This will either be the project name or the supplied application name
applicationDetailsString += "Application: " + applicationName + " | "
if applicationVersion != "":
applicationDetailsString += "Version: " + applicationVersion
else:
# Rip off the | from the end of the string if the version was not there
applicationDetailsString = applicationDetailsString[:-3]
applicationDetails = {}
applicationDetails["applicationName"] = applicationName
applicationDetails["applicationVersion"] = applicationVersion
applicationDetails["applicationPublisher"] = applicationPublisher
applicationDetails["applicationNameVersion"] = applicationNameVersion
applicationDetails["applicationDetailsString"] = applicationDetailsString
logger.info(" applicationDetails: %s" %applicationDetails)
return applicationDetails