-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathscandetails.go
234 lines (201 loc) · 6.97 KB
/
scandetails.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
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
package utils
import (
"context"
"fmt"
"path/filepath"
"time"
clientservices "github.com/jfrog/jfrog-client-go/xsc/services"
"github.com/jfrog/froggit-go/vcsclient"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-security/commands/audit"
"github.com/jfrog/jfrog-cli-security/utils"
"github.com/jfrog/jfrog-cli-security/utils/results"
"github.com/jfrog/jfrog-cli-security/utils/severityutils"
"github.com/jfrog/jfrog-client-go/utils/log"
xscservices "github.com/jfrog/jfrog-client-go/xsc/services"
)
type ScanDetails struct {
*Project
*Git
*xscservices.XscGitInfoContext
*config.ServerDetails
client vcsclient.VcsClient
failOnInstallationErrors bool
fixableOnly bool
disableJas bool
skipAutoInstall bool
minSeverityFilter severityutils.Severity
baseBranch string
configProfile *clientservices.ConfigProfile
allowPartialResults bool
results.ResultContext
MultiScanId string
XrayVersion string
XscVersion string
StartTime time.Time
}
func NewScanDetails(client vcsclient.VcsClient, server *config.ServerDetails, git *Git) *ScanDetails {
return &ScanDetails{client: client, ServerDetails: server, Git: git}
}
func (sc *ScanDetails) SetJfrogVersions(xrayVersion, xscVersion string) *ScanDetails {
sc.XrayVersion = xrayVersion
sc.XscVersion = xscVersion
return sc
}
func (sc *ScanDetails) SetDisableJas(disable bool) *ScanDetails {
sc.disableJas = disable
return sc
}
func (sc *ScanDetails) SetFailOnInstallationErrors(toFail bool) *ScanDetails {
sc.failOnInstallationErrors = toFail
return sc
}
func (sc *ScanDetails) SetProject(project *Project) *ScanDetails {
sc.Project = project
return sc
}
func (sc *ScanDetails) SetResultsContext(httpCloneUrl string, watches []string, jfrogProjectKey string, includeVulnerabilities, includeLicenses bool) *ScanDetails {
sc.ResultContext = audit.CreateAuditResultsContext(sc.ServerDetails, sc.XrayVersion, watches, sc.RepoPath, jfrogProjectKey, httpCloneUrl, includeVulnerabilities, includeLicenses, false)
return sc
}
func (sc *ScanDetails) SetFixableOnly(fixable bool) *ScanDetails {
sc.fixableOnly = fixable
return sc
}
func (sc *ScanDetails) SetSkipAutoInstall(skipAutoInstall bool) *ScanDetails {
sc.skipAutoInstall = skipAutoInstall
return sc
}
func (sc *ScanDetails) SetMinSeverity(minSeverity string) (*ScanDetails, error) {
if minSeverity == "" {
return sc, nil
}
if severity, err := severityutils.ParseSeverity(minSeverity, false); err != nil {
return sc, err
} else {
sc.minSeverityFilter = severity
}
return sc, nil
}
func (sc *ScanDetails) SetAllowPartialResults(allowPartialResults bool) *ScanDetails {
sc.allowPartialResults = allowPartialResults
return sc
}
func (sc *ScanDetails) SetBaseBranch(branch string) *ScanDetails {
sc.baseBranch = branch
return sc
}
func (sc *ScanDetails) SetConfigProfile(configProfile *clientservices.ConfigProfile) *ScanDetails {
sc.configProfile = configProfile
return sc
}
func (sc *ScanDetails) Client() vcsclient.VcsClient {
return sc.client
}
func (sc *ScanDetails) BaseBranch() string {
return sc.baseBranch
}
func (sc *ScanDetails) FailOnInstallationErrors() bool {
return sc.failOnInstallationErrors
}
func (sc *ScanDetails) FixableOnly() bool {
return sc.fixableOnly
}
func (sc *ScanDetails) DisableJas() bool {
return sc.disableJas
}
func (sc *ScanDetails) MinSeverityFilter() severityutils.Severity {
return sc.minSeverityFilter
}
func (sc *ScanDetails) SetRepoOwner(owner string) *ScanDetails {
sc.RepoOwner = owner
return sc
}
func (sc *ScanDetails) SetRepoName(repoName string) *ScanDetails {
sc.RepoName = repoName
return sc
}
func (sc *ScanDetails) AllowPartialResults() bool {
return sc.allowPartialResults
}
func (sc *ScanDetails) RunInstallAndAudit(workDirs ...string) (auditResults *results.SecurityCommandResults) {
auditBasicParams := (&utils.AuditBasicParams{}).
SetXrayVersion(sc.XrayVersion).
SetXscVersion(sc.XscVersion).
SetPipRequirementsFile(sc.PipRequirementsFile).
SetUseWrapper(*sc.UseWrapper).
SetMaxTreeDepth(sc.MaxPnpmTreeDepth).
SetDepsRepo(sc.DepsRepo).
SetIgnoreConfigFile(true).
SetServerDetails(sc.ServerDetails).
SetInstallCommandName(sc.InstallCommandName).
SetInstallCommandArgs(sc.InstallCommandArgs).
SetTechnologies(sc.GetTechFromInstallCmdIfExists()).
SetSkipAutoInstall(sc.skipAutoInstall).
SetAllowPartialResults(sc.allowPartialResults).
SetExclusions(sc.PathExclusions).
SetIsRecursiveScan(sc.IsRecursiveScan).
SetUseJas(!sc.DisableJas())
auditParams := audit.NewAuditParams().
SetWorkingDirs(workDirs).
SetMinSeverityFilter(sc.MinSeverityFilter()).
SetFixableOnly(sc.FixableOnly()).
SetGraphBasicParams(auditBasicParams).
SetResultsContext(sc.ResultContext).
SetConfigProfile(sc.configProfile).
SetMultiScanId(sc.MultiScanId).
SetStartTime(sc.StartTime)
return audit.RunAudit(auditParams)
}
func (sc *ScanDetails) SetXscGitInfoContext(scannedBranch, gitProject string, client vcsclient.VcsClient) *ScanDetails {
XscGitInfoContext, err := sc.createGitInfoContext(scannedBranch, gitProject, client)
if err != nil {
log.Debug("Failed to create a GitInfoContext for Xsc due to the following error:", err.Error())
return sc
}
sc.XscGitInfoContext = XscGitInfoContext
return sc
}
// CreateGitInfoContext Creates GitInfoContext for XSC scans, this is optional.
// ScannedBranch - name of the branch we are scanning.
// GitProject - [Optional] relevant for azure repos and Bitbucket server.
// Client vscClient
func (sc *ScanDetails) createGitInfoContext(scannedBranch, gitProject string, client vcsclient.VcsClient) (gitInfo *xscservices.XscGitInfoContext, err error) {
latestCommit, err := client.GetLatestCommit(context.Background(), sc.RepoOwner, sc.RepoName, scannedBranch)
if err != nil {
return nil, fmt.Errorf("failed getting latest commit, repository: %s, branch: %s. error: %s ", sc.RepoName, scannedBranch, err.Error())
}
// In some VCS providers, there are no git projects, fallback to the repository owner.
if gitProject == "" {
gitProject = sc.RepoOwner
}
gitInfo = &xscservices.XscGitInfoContext{
Source: xscservices.CommitContext{
// Use Clone URLs as Repo Url, on browsers it will redirect to repository URLS.
GitRepoHttpsCloneUrl: sc.Git.RepositoryCloneUrl,
GitRepoName: sc.RepoName,
GitProject: gitProject,
BranchName: scannedBranch,
CommitHash: latestCommit.Hash,
CommitMessage: latestCommit.Message,
CommitAuthor: latestCommit.AuthorName,
},
GitProvider: sc.Git.GitProvider.String(),
}
return
}
func GetFullPathWorkingDirs(workingDirs []string, baseWd string) []string {
var fullPathWds []string
if len(workingDirs) != 0 {
for _, workDir := range workingDirs {
if workDir == RootDir {
fullPathWds = append(fullPathWds, baseWd)
continue
}
fullPathWds = append(fullPathWds, filepath.Join(baseWd, workDir))
}
} else {
fullPathWds = append(fullPathWds, baseWd)
}
return fullPathWds
}