Skip to content

Commit dd62e26

Browse files
authored
Xray scan small fixes (#192)
* Removed JFROG_CLI_OUTPUT_COLORS. * All results of a scan are saved to a single file.
1 parent ed938dc commit dd62e26

File tree

7 files changed

+30
-62
lines changed

7 files changed

+30
-62
lines changed

utils/coreutils/coreconsts.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@ const (
3131
BuildNumber = "JFROG_CLI_BUILD_NUMBER"
3232
Project = "JFROG_CLI_BUILD_PROJECT"
3333
TransitiveDownload = "JFROG_CLI_TRANSITIVE_DOWNLOAD_EXPERIMENTAL"
34-
ColoredOutput = "JFROG_CLI_OUTPUT_COLORS"
3534
CI = "CI"
3635
)

utils/coreutils/utils.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,6 @@ func IsTerminal() bool {
204204
return terminal.IsTerminal(int(os.Stdout.Fd()))
205205
}
206206

207-
var coloredOutput *bool
208-
209-
// IsColoredOutput returns true if the output can be colored.
210-
func IsColoredOutput() bool {
211-
if coloredOutput == nil {
212-
coloredOutputVal := true
213-
if os.Getenv(ColoredOutput) == "false" {
214-
coloredOutputVal = false
215-
} else {
216-
coloredOutputVal = IsTerminal()
217-
}
218-
coloredOutput = &coloredOutputVal
219-
}
220-
return *coloredOutput
221-
}
222-
223207
type Credentials interface {
224208
SetUser(string)
225209
SetPassword(string)

xray/commands/audit/javautils.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
xrutils "github.com/jfrog/jfrog-cli-core/v2/xray/utils"
1414
"github.com/jfrog/jfrog-client-go/artifactory/buildinfo"
1515
"github.com/jfrog/jfrog-client-go/utils/errorutils"
16-
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
1716
"github.com/jfrog/jfrog-client-go/utils/log"
1817
"github.com/jfrog/jfrog-client-go/xray/services"
1918
)
@@ -56,11 +55,6 @@ func runScanGraph(modulesDependencyTrees []*services.GraphNode, serverDetails *c
5655
return err
5756
}
5857

59-
tempDirPath, err := fileutils.CreateTempDir()
60-
if err != nil {
61-
return err
62-
}
63-
6458
var violations []services.Violation
6559
var vulnerabilities []services.Vulnerability
6660
var licenses []services.License
@@ -85,20 +79,22 @@ func runScanGraph(modulesDependencyTrees []*services.GraphNode, serverDetails *c
8579
if err != nil {
8680
return err
8781
}
88-
if outputFormat == Table {
89-
if err = xrutils.WriteJsonResults(scanResults, tempDirPath); err != nil {
90-
return err
91-
}
82+
results = append(results, *scanResults)
9283

84+
if outputFormat == Table {
9385
violations = append(violations, scanResults.Violations...)
9486
vulnerabilities = append(vulnerabilities, scanResults.Vulnerabilities...)
9587
licenses = append(licenses, scanResults.Licenses...)
96-
} else {
97-
results = append(results, *scanResults)
9888
}
9989
}
10090
if outputFormat == Table {
101-
fmt.Println("The full scan results are available here: " + tempDirPath)
91+
if len(results) > 0 {
92+
resultsPath, err := xrutils.WriteJsonResults(results)
93+
if err != nil {
94+
return err
95+
}
96+
fmt.Println("The full scan results are available here: " + resultsPath)
97+
}
10298
if includeVulnerabilities {
10399
xrutils.PrintVulnerabilitiesTable(vulnerabilities, false)
104100
} else {

xray/commands/audit/npm.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
npmutils "github.com/jfrog/jfrog-cli-core/v2/utils/npm"
99
"github.com/jfrog/jfrog-cli-core/v2/xray/commands"
1010
xrutils "github.com/jfrog/jfrog-cli-core/v2/xray/utils"
11-
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
1211
"github.com/jfrog/jfrog-client-go/xray/services"
1312
)
1413

@@ -124,14 +123,11 @@ func (auditCmd *AuditNpmCommand) Run() (err error) {
124123
return err
125124
}
126125
if auditCmd.outputFormat == Table {
127-
tempDirPath, err := fileutils.CreateTempDir()
126+
resultsPath, err := xrutils.WriteJsonResults([]services.ScanResponse{*scanResults})
128127
if err != nil {
129128
return err
130129
}
131-
if err = xrutils.WriteJsonResults(scanResults, tempDirPath); err != nil {
132-
return err
133-
}
134-
fmt.Println("The full scan results are available here: " + tempDirPath)
130+
fmt.Println("The full scan results are available here: " + resultsPath)
135131

136132
if auditCmd.includeVulnerabilities {
137133
xrutils.PrintVulnerabilitiesTable(scanResults.Vulnerabilities, false)

xray/commands/audit/scan.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ func getAddTaskToProducerFunc(producer parallel.Runner, errorsQueue *clientutils
218218
}
219219

220220
func (scanCmd *ScanCommand) performScanTasks(fileConsumer parallel.Runner, indexedFileConsumer parallel.Runner, resultsArr [][]*services.ScanResponse) (bool, error) {
221-
222221
go func() {
223222
// Blocking until consuming is finished.
224223
fileConsumer.Run()
@@ -233,30 +232,30 @@ func (scanCmd *ScanCommand) performScanTasks(fileConsumer parallel.Runner, index
233232
vulnerabilities := []services.Vulnerability{}
234233
licenses := []services.License{}
235234
flatResults := []services.ScanResponse{}
236-
tempDirPath, err := fileutils.CreateTempDir()
237-
if err != nil {
238-
return false, err
239-
}
240235
for _, arr := range resultsArr {
241236
for _, res := range arr {
237+
flatResults = append(flatResults, *res)
238+
242239
if scanCmd.outputFormat == Table {
243-
if err = xrutils.WriteJsonResults(res, tempDirPath); err != nil {
244-
return false, err
245-
}
246240
violations = append(violations, res.Violations...)
247241
vulnerabilities = append(vulnerabilities, res.Vulnerabilities...)
248242
licenses = append(licenses, res.Licenses...)
249-
} else {
250-
flatResults = append(flatResults, *res)
251243
}
252244
if len(res.Violations) > 0 || len(res.Vulnerabilities) > 0 {
253245
// A violation or vulnerability was found, the scan failed.
254246
scanPassed = false
255247
}
256248
}
257249
}
250+
var err error
258251
if scanCmd.outputFormat == Table {
259-
fmt.Println("The full scan results are available here: " + tempDirPath)
252+
if len(flatResults) > 0 {
253+
resultsPath, err := xrutils.WriteJsonResults(flatResults)
254+
if err != nil {
255+
return false, err
256+
}
257+
fmt.Println("The full scan results are available here: " + resultsPath)
258+
}
260259
if scanCmd.includeVulnerabilities {
261260
xrutils.PrintVulnerabilitiesTable(vulnerabilities, true)
262261
} else {
@@ -267,7 +266,6 @@ func (scanCmd *ScanCommand) performScanTasks(fileConsumer parallel.Runner, index
267266
}
268267
} else {
269268
err = xrutils.PrintJson(flatResults)
270-
271269
}
272270
if scanPassed {
273271
log.Info("Scan completed successfully.")

xray/utils/resultstable.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func PrintViolationsTable(violations []services.Violation, multipleRoots bool) e
2424
var licenseViolationsRows []licenseViolationRow
2525
failBuild := false
2626

27-
coloredOutput := coreutils.IsColoredOutput()
27+
coloredOutput := coreutils.IsTerminal()
2828

2929
for _, violation := range violations {
3030
impactedPackagesNames, impactedPackagesVersions, impactedPackagesTypes, fixedVersions, components := splitComponents(violation.Components, multipleRoots)
@@ -105,7 +105,7 @@ func PrintVulnerabilitiesTable(vulnerabilities []services.Vulnerability, multipl
105105
"Read more about configuring Xray policies here: https://www.jfrog.com/confluence/display/JFROG/Creating+Xray+Policies+and+Rules\n" +
106106
"Below are all vulnerabilities detected.")
107107

108-
coloredOutput := coreutils.IsColoredOutput()
108+
coloredOutput := coreutils.IsTerminal()
109109

110110
var vulnerabilitiesRows []vulnerabilityRow
111111

xray/utils/resultwriter.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,26 @@ package utils
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
7-
"strconv"
8-
"time"
9-
106
"github.com/jfrog/jfrog-client-go/utils/errorutils"
7+
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
118
"github.com/jfrog/jfrog-client-go/xray/services"
129
)
1310

14-
func WriteJsonResults(results *services.ScanResponse, dirPath string) error {
15-
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
16-
out, err := ioutil.TempFile(dirPath, timestamp+"-")
11+
func WriteJsonResults(results []services.ScanResponse) (string, error) {
12+
out, err := fileutils.CreateTempFile()
1713
if err != nil {
18-
return errorutils.CheckError(err)
14+
return "", errorutils.CheckError(err)
1915
}
2016
defer out.Close()
2117
bytesRes, err := json.Marshal(&results)
2218
if err != nil {
23-
return errorutils.CheckError(err)
19+
return "", errorutils.CheckError(err)
2420
}
2521
var content bytes.Buffer
2622
err = json.Indent(&content, bytesRes, "", " ")
2723
if err != nil {
28-
return errorutils.CheckError(err)
24+
return "", errorutils.CheckError(err)
2925
}
3026
_, err = out.Write([]byte(content.String()))
31-
return errorutils.CheckError(err)
32-
27+
return out.Name(), errorutils.CheckError(err)
3328
}

0 commit comments

Comments
 (0)