-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgeneric.go
139 lines (122 loc) · 4.35 KB
/
generic.go
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
package audit
import (
"os"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
xrutils "github.com/jfrog/jfrog-cli-core/v2/xray/utils"
"github.com/jfrog/jfrog-client-go/xray/services"
)
type GenericAuditCommand struct {
watches []string
projectKey string
targetRepoPath string
excludeJasScan string
IncludeVulnerabilities bool
IncludeLicenses bool
Fail bool
PrintExtendedTable bool
Params
}
type Results struct {
IsMultipleRootProject bool
AuditError error
ExtendedScanResults *xrutils.ExtendedScanResults
}
func NewGenericAuditCommand() *GenericAuditCommand {
return &GenericAuditCommand{Params: *NewAuditParams()}
}
func (auditCmd *GenericAuditCommand) SetWatches(watches []string) *GenericAuditCommand {
auditCmd.watches = watches
return auditCmd
}
func (auditCmd *GenericAuditCommand) SetProject(project string) *GenericAuditCommand {
auditCmd.projectKey = project
return auditCmd
}
func (auditCmd *GenericAuditCommand) SetTargetRepoPath(repoPath string) *GenericAuditCommand {
auditCmd.targetRepoPath = repoPath
return auditCmd
}
func (auditCmd *GenericAuditCommand) SetIncludeVulnerabilities(include bool) *GenericAuditCommand {
auditCmd.IncludeVulnerabilities = include
return auditCmd
}
func (auditCmd *GenericAuditCommand) SetIncludeLicenses(include bool) *GenericAuditCommand {
auditCmd.IncludeLicenses = include
return auditCmd
}
func (auditCmd *GenericAuditCommand) SetFail(fail bool) *GenericAuditCommand {
auditCmd.Fail = fail
return auditCmd
}
func (auditCmd *GenericAuditCommand) SetPrintExtendedTable(printExtendedTable bool) *GenericAuditCommand {
auditCmd.PrintExtendedTable = printExtendedTable
return auditCmd
}
func (auditCmd *GenericAuditCommand) SetExcludeJasScan(excludeScan string) *GenericAuditCommand {
auditCmd.excludeJasScan = excludeScan
return auditCmd
}
func (auditCmd *GenericAuditCommand) CreateXrayGraphScanParams() *services.XrayGraphScanParams {
params := &services.XrayGraphScanParams{
RepoPath: auditCmd.targetRepoPath,
Watches: auditCmd.watches,
ScanType: services.Dependency,
}
if auditCmd.projectKey == "" {
params.ProjectKey = os.Getenv(coreutils.Project)
} else {
params.ProjectKey = auditCmd.projectKey
}
params.IncludeVulnerabilities = auditCmd.IncludeVulnerabilities
params.IncludeLicenses = auditCmd.IncludeLicenses
return params
}
func (auditCmd *GenericAuditCommand) Run() (err error) {
auditParams := NewAuditParams().
SetXrayGraphScanParams(auditCmd.CreateXrayGraphScanParams()).
SetWorkingDirs(auditCmd.workingDirs).
SetMinSeverityFilter(auditCmd.minSeverityFilter).
SetFixableOnly(auditCmd.fixableOnly).
SetGraphBasicParams(auditCmd.GraphBasicParams).
SetExcludeJasScan(auditCmd.excludeJasScan)
auditResults, err := RunAudit(auditParams)
if err != nil {
return err
}
if auditCmd.Progress() != nil {
if err = auditCmd.Progress().Quit(); err != nil {
return
}
}
var messages []string
if !auditResults.ExtendedScanResults.EntitledForJas {
messages = []string{coreutils.PrintTitle("The ‘jf audit’ command also supports the ‘Contextual Analysis’ feature, which is included as part of the ‘Advanced Security’ package. This package isn't enabled on your system. Read more - ") + coreutils.PrintLink("https://jfrog.com/security-and-compliance")}
}
// Print Scan results on all cases except if errors accrued on Generic Audit command and no security/license issues found.
printScanResults := !(auditResults.AuditError != nil && xrutils.IsEmptyScanResponse(auditResults.ExtendedScanResults.XrayResults))
if printScanResults {
err = xrutils.PrintScanResults(auditResults.ExtendedScanResults,
nil,
auditCmd.OutputFormat(),
auditCmd.IncludeVulnerabilities,
auditCmd.IncludeLicenses,
auditResults.IsMultipleRootProject,
auditCmd.PrintExtendedTable, false, messages,
)
if err != nil {
return
}
}
if auditResults.AuditError != nil {
err = auditResults.AuditError
return
}
// Only in case Xray's context was given (!auditCmd.IncludeVulnerabilities), and the user asked to fail the build accordingly, do so.
if auditCmd.Fail && !auditCmd.IncludeVulnerabilities && xrutils.CheckIfFailBuild(auditResults.ExtendedScanResults.XrayResults) {
err = xrutils.NewFailBuildError()
}
return
}
func (auditCmd *GenericAuditCommand) CommandName() string {
return "generic_audit"
}