Skip to content

Commit

Permalink
Print both vulnerabilities and violations tables if needed (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas authored Sep 4, 2024
1 parent e5f675e commit aea8b7c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
5 changes: 5 additions & 0 deletions commands/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (auditCmd *AuditCommand) Run() (err error) {
}
if err = utils.NewResultsWriter(auditResults).
SetIsMultipleRootProject(auditResults.IsMultipleProject()).
SetHasViolationContext(auditCmd.HasViolationContext()).
SetIncludeVulnerabilities(auditCmd.IncludeVulnerabilities).
SetIncludeLicenses(auditCmd.IncludeLicenses).
SetOutputFormat(auditCmd.OutputFormat()).
Expand All @@ -160,6 +161,10 @@ func (auditCmd *AuditCommand) CommandName() string {
return "generic_audit"
}

func (auditCmd *AuditCommand) HasViolationContext() bool {
return len(auditCmd.watches) > 0 || auditCmd.projectKey != "" || auditCmd.targetRepoPath != ""
}

// Runs an audit scan based on the provided auditParams.
// Returns an audit Results object containing all the scan results.
// If the current server is entitled for JAS, the advanced security results will be included in the scan results.
Expand Down
7 changes: 6 additions & 1 deletion commands/scan/buildscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (bsc *BuildScanCommand) runBuildScanAndPrintResults(xrayManager *xray.XrayS

resultsPrinter := utils.NewResultsWriter(scanResults).
SetOutputFormat(bsc.outputFormat).
SetHasViolationContext(bsc.hasViolationContext()).
SetIncludeVulnerabilities(bsc.includeVulnerabilities).
SetIncludeLicenses(false).
SetIsMultipleRootProject(true).
Expand Down Expand Up @@ -187,7 +188,7 @@ func (bsc *BuildScanCommand) runBuildScanAndPrintResults(xrayManager *xray.XrayS
scanResults,
bsc.serverDetails,
bsc.includeVulnerabilities,
bsc.buildConfiguration.GetProject() != "",
bsc.hasViolationContext(),
params.BuildName, params.BuildNumber,
))
return
Expand All @@ -197,6 +198,10 @@ func (bsc *BuildScanCommand) CommandName() string {
return "xr_build_scan"
}

func (bsc *BuildScanCommand) hasViolationContext() bool {
return bsc.buildConfiguration.GetProject() != ""
}

// There are two cases. when serverDetails.Url is configured and when serverDetails.XrayUrl and serverDetails.ArtifactoryUrl are configured
// The function will return the Url if configured and will trim xray if serverDetails.Url is not configured
func getActualUrl(serverDetails config.ServerDetails) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion commands/scan/dockerscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (dsc *DockerScanCommand) Run() (err error) {
scanResults,
dsc.ScanCommand.serverDetails,
dsc.ScanCommand.includeVulnerabilities,
hasViolationContext(dsc.ScanCommand.watches, dsc.ScanCommand.projectKey),
dsc.ScanCommand.hasViolationContext(),
dsc.imageTag,
))
})
Expand Down
11 changes: 6 additions & 5 deletions commands/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ func (scanCmd *ScanCommand) SetAnalyticsMetricsService(analyticsMetricsService *
return scanCmd
}

func (scanCmd *ScanCommand) hasViolationContext() bool {
return len(scanCmd.watches) > 0 || scanCmd.projectKey != ""
}

func (scanCmd *ScanCommand) indexFile(filePath string) (*xrayUtils.BinaryGraphNode, error) {
var indexerResults xrayUtils.BinaryGraphNode
indexerCmd := exec.Command(scanCmd.indexerPath, indexingCommand, filePath, "--temp-dir", scanCmd.indexerTempDir)
Expand Down Expand Up @@ -195,15 +199,11 @@ func (scanCmd *ScanCommand) Run() (err error) {
scanResults,
scanCmd.serverDetails,
scanCmd.includeVulnerabilities,
hasViolationContext(scanCmd.watches, scanCmd.projectKey),
scanCmd.hasViolationContext(),
))
})
}

func hasViolationContext(watches []string, projectKey string) bool {
return len(watches) > 0 || projectKey != ""
}

func (scanCmd *ScanCommand) RunAndRecordResults(recordResFunc func(scanResults *utils.Results) error) (err error) {
defer func() {
if err != nil {
Expand Down Expand Up @@ -318,6 +318,7 @@ func (scanCmd *ScanCommand) RunAndRecordResults(recordResFunc func(scanResults *

if err = utils.NewResultsWriter(scanResults).
SetOutputFormat(scanCmd.outputFormat).
SetHasViolationContext(scanCmd.hasViolationContext()).
SetIncludeVulnerabilities(scanCmd.includeVulnerabilities).
SetIncludeLicenses(scanCmd.includeLicenses).
SetPrintExtendedTable(scanCmd.printExtendedTable).
Expand Down
21 changes: 15 additions & 6 deletions utils/resultwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type ResultsWriter struct {
format format.OutputFormat
// IncludeVulnerabilities If true, include all vulnerabilities as part of the output. Else, include violations only.
includeVulnerabilities bool
//
hasViolationContext bool
// IncludeLicenses If true, also include license violations as part of the output.
includeLicenses bool
// IsMultipleRoots multipleRoots is set to true, in case the given results array contains (or may contain) results of several projects (like in binary scan).
Expand All @@ -65,6 +67,11 @@ func GetScaScanFileName(r *Results) string {
return ""
}

func (rw *ResultsWriter) SetHasViolationContext(hasViolationContext bool) *ResultsWriter {
rw.hasViolationContext = hasViolationContext
return rw
}

func (rw *ResultsWriter) SetOutputFormat(f format.OutputFormat) *ResultsWriter {
rw.format = f
return rw
Expand Down Expand Up @@ -142,13 +149,15 @@ func (rw *ResultsWriter) printScanResultsTables() (err error) {
}
log.Output()
if shouldPrintTable(rw.subScansPreformed, ScaScan, rw.scanType) {
if rw.includeVulnerabilities {
err = PrintVulnerabilitiesTable(vulnerabilities, rw.results, rw.isMultipleRoots, rw.printExtended, rw.scanType)
} else {
err = PrintViolationsTable(violations, rw.results, rw.isMultipleRoots, rw.printExtended, rw.scanType)
if rw.hasViolationContext {
if err = PrintViolationsTable(violations, rw.results, rw.isMultipleRoots, rw.printExtended, rw.scanType); err != nil {
return
}
}
if err != nil {
return
if rw.includeVulnerabilities {
if err = PrintVulnerabilitiesTable(vulnerabilities, rw.results, rw.isMultipleRoots, rw.printExtended, rw.scanType); err != nil {
return
}
}
if rw.includeLicenses {
if err = PrintLicensesTable(licenses, rw.printExtended, rw.scanType); err != nil {
Expand Down

0 comments on commit aea8b7c

Please sign in to comment.