forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xray_test.go
325 lines (283 loc) · 12.9 KB
/
xray_test.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"encoding/json"
"errors"
"fmt"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/jfrog/gofrog/version"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
"github.com/jfrog/jfrog-cli-core/v2/xray/commands"
"github.com/jfrog/jfrog-cli-core/v2/xray/formats"
"github.com/jfrog/jfrog-cli-core/v2/xray/utils"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-client-go/auth"
clientUtils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/jfrog/jfrog-client-go/xray/services"
"github.com/stretchr/testify/assert"
)
var (
xrayDetails *config.ServerDetails
xrayAuth auth.ServiceDetails
// JFrog CLI for Xray commands
xrayCli *tests.JfrogCli
)
func InitXrayTests() {
initXrayCli()
}
func authenticateXray() string {
*tests.JfrogUrl = clientUtils.AddTrailingSlashIfNeeded(*tests.JfrogUrl)
xrayDetails = &config.ServerDetails{XrayUrl: *tests.JfrogUrl + tests.XrayEndpoint}
cred := fmt.Sprintf("--url=%s", xrayDetails.XrayUrl)
if *tests.JfrogAccessToken != "" {
xrayDetails.AccessToken = *tests.JfrogAccessToken
cred += fmt.Sprintf(" --access-token=%s", xrayDetails.AccessToken)
} else {
xrayDetails.User = *tests.JfrogUser
xrayDetails.Password = *tests.JfrogPassword
cred += fmt.Sprintf(" --user=%s --password=%s", xrayDetails.User, xrayDetails.Password)
}
var err error
if xrayAuth, err = xrayDetails.CreateXrayAuthConfig(); err != nil {
coreutils.ExitOnErr(errors.New("Failed while attempting to authenticate with Xray: " + err.Error()))
}
xrayDetails.XrayUrl = xrayAuth.GetUrl()
return cred
}
func initXrayCli() {
if xrayCli != nil {
return
}
cred := authenticateXray()
xrayCli = tests.NewJfrogCli(execMain, "jfrog", cred)
}
// Tests basic binary scan by providing pattern (path to testdata binaries) and --licenses flag
// and asserts any error.
func TestXrayBinaryScanJson(t *testing.T) {
output := testXrayBinaryScan(t, string(utils.Json))
verifyJsonScanResults(t, output, 0, 1, 1)
}
func TestXrayBinaryScanSimpleJson(t *testing.T) {
output := testXrayBinaryScan(t, string(utils.SimpleJson))
verifySimpleJsonScanResults(t, output, 0, 0, 1, 1)
}
func TestXrayBinaryScanJsonWithProgress(t *testing.T) {
callback := tests.MockProgressInitialization()
defer callback()
output := testXrayBinaryScan(t, string(utils.Json))
verifyJsonScanResults(t, output, 0, 1, 1)
}
func TestXrayBinaryScanSimpleJsonWithProgress(t *testing.T) {
callback := tests.MockProgressInitialization()
defer callback()
output := testXrayBinaryScan(t, string(utils.SimpleJson))
verifySimpleJsonScanResults(t, output, 0, 0, 1, 1)
}
func testXrayBinaryScan(t *testing.T, format string) string {
initXrayTest(t, commands.GraphScanMinXrayVersion)
binariesPath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "xray", "binaries", "*")
return xrayCli.RunCliCmdWithOutput(t, "scan", binariesPath, "--licenses", "--format="+format)
}
// Tests npm audit by providing simple npm project and asserts any error.
func TestXrayAuditNpmJson(t *testing.T) {
output := testXrayAuditNpm(t, string(utils.Json))
verifyJsonScanResults(t, output, 0, 1, 1)
}
func TestXrayAuditNpmSimpleJson(t *testing.T) {
output := testXrayAuditNpm(t, string(utils.SimpleJson))
verifySimpleJsonScanResults(t, output, 0, 0, 1, 1)
}
func testXrayAuditNpm(t *testing.T, format string) string {
initXrayTest(t, commands.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
npmProjectPath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "xray", "npm")
// Copy the npm project from the testdata to a temp dir
assert.NoError(t, fileutils.CopyDir(npmProjectPath, tempDirPath, true, nil))
prevWd := changeWD(t, tempDirPath)
defer clientTestUtils.ChangeDirAndAssert(t, prevWd)
// Run npm install before executing jfrog xr npm-audit
assert.NoError(t, exec.Command("npm", "install").Run())
return xrayCli.RunCliCmdWithOutput(t, "audit", "--npm", "--licenses", "--format="+format)
}
// Tests NuGet audit by providing simple NuGet project and asserts any error.
func TestXrayAuditNugetJson(t *testing.T) {
output := testXrayAuditNuget(t, "single", string(utils.Json))
verifyJsonScanResults(t, output, 0, 2, 0)
}
func TestXrayAuditNugetSimpleJson(t *testing.T) {
output := testXrayAuditNuget(t, "single", string(utils.SimpleJson))
verifySimpleJsonScanResults(t, output, 0, 0, 2, 0)
}
// Tests NuGet audit by providing a multi-project NuGet project and asserts any error.
func TestXrayAuditNugetMultiProject(t *testing.T) {
output := testXrayAuditNuget(t, "multi", string(utils.Json))
verifyJsonScanResults(t, output, 0, 5, 0)
}
func testXrayAuditNuget(t *testing.T, projectName, format string) string {
initXrayTest(t, commands.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
projectPath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "xray", "nuget", projectName)
assert.NoError(t, fileutils.CopyDir(projectPath, tempDirPath, true, nil))
prevWd := changeWD(t, tempDirPath)
defer clientTestUtils.ChangeDirAndAssert(t, prevWd)
// Run NuGet restore before executing jfrog xr audit (NuGet)
assert.NoError(t, exec.Command("nuget", "restore").Run())
return xrayCli.RunCliCmdWithOutput(t, "audit", "--nuget", "--format="+format)
}
func TestXrayAuditGradleJson(t *testing.T) {
output := testXrayAuditGradle(t, string(utils.Json))
verifyJsonScanResults(t, output, 0, 0, 0)
}
func TestXrayAuditGradleSimpleJson(t *testing.T) {
output := testXrayAuditGradle(t, string(utils.SimpleJson))
verifySimpleJsonScanResults(t, output, 0, 0, 0, 0)
}
func testXrayAuditGradle(t *testing.T, format string) string {
initXrayTest(t, commands.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
gradleProjectPath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "xray", "gradle")
// Copy the gradle project from the testdata to a temp dir
assert.NoError(t, fileutils.CopyDir(gradleProjectPath, tempDirPath, true, nil))
prevWd := changeWD(t, tempDirPath)
defer clientTestUtils.ChangeDirAndAssert(t, prevWd)
return xrayCli.RunCliCmdWithOutput(t, "audit", "--gradle", "--licenses", "--format="+format)
}
func TestXrayAuditMavenJson(t *testing.T) {
output := testXrayAuditMaven(t, string(utils.Json))
verifyJsonScanResults(t, output, 0, 1, 1)
}
func TestXrayAuditMavenSimpleJson(t *testing.T) {
output := testXrayAuditMaven(t, string(utils.SimpleJson))
verifySimpleJsonScanResults(t, output, 0, 0, 1, 1)
}
func testXrayAuditMaven(t *testing.T, format string) string {
initXrayTest(t, commands.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
mvnProjectPath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "xray", "maven")
// Copy the maven project from the testdata to a temp dir
assert.NoError(t, fileutils.CopyDir(mvnProjectPath, tempDirPath, true, nil))
prevWd := changeWD(t, tempDirPath)
defer clientTestUtils.ChangeDirAndAssert(t, prevWd)
return xrayCli.RunCliCmdWithOutput(t, "audit", "--mvn", "--licenses", "--format="+format)
}
func TestXrayAuditNoTech(t *testing.T) {
initXrayTest(t, commands.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
prevWd := changeWD(t, tempDirPath)
defer clientTestUtils.ChangeDirAndAssert(t, prevWd)
// Run audit on empty folder, expect an error
err := xrayCli.Exec("audit")
assert.EqualError(t, err, "could not determine the package manager / build tool used by this project.")
}
func TestXrayAuditDetectTech(t *testing.T) {
initXrayTest(t, commands.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
mvnProjectPath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "xray", "maven")
// Copy the maven project from the testdata to a temp dir
assert.NoError(t, fileutils.CopyDir(mvnProjectPath, tempDirPath, true, nil))
prevWd := changeWD(t, tempDirPath)
defer clientTestUtils.ChangeDirAndAssert(t, prevWd)
// Run generic audit on mvn project with a vulnerable dependency
output := xrayCli.RunCliCmdWithOutput(t, "audit", "--licenses", "--format="+string(utils.SimpleJson))
var results formats.SimpleJsonResults
err := json.Unmarshal([]byte(output), &results)
assert.NoError(t, err)
// Expects the ImpactedPackageType of the known vulnerability to be maven
assert.Equal(t, strings.ToLower(results.Vulnerabilities[0].ImpactedPackageType), "maven")
}
func TestXrayAuditPipJson(t *testing.T) {
output := testXrayAuditPip(t, string(utils.Json))
verifyJsonScanResults(t, output, 0, 3, 1)
}
func TestXrayAuditPipSimpleJson(t *testing.T) {
output := testXrayAuditPip(t, string(utils.SimpleJson))
verifySimpleJsonScanResults(t, output, 0, 0, 3, 1)
}
func testXrayAuditPip(t *testing.T, format string) string {
initXrayTest(t, commands.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
pipProjectPath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "xray", "python", "pip")
// Copy the pip project from the testdata to a temp dir
assert.NoError(t, fileutils.CopyDir(pipProjectPath, tempDirPath, true, nil))
prevWd := changeWD(t, tempDirPath)
defer clientTestUtils.ChangeDirAndAssert(t, prevWd)
return xrayCli.RunCliCmdWithOutput(t, "audit", "--pip", "--licenses", "--format="+format)
}
func TestXrayAuditPipenvJson(t *testing.T) {
output := testXrayAuditPipenv(t, string(utils.Json))
verifyJsonScanResults(t, output, 0, 3, 1)
}
func TestXrayAuditPipenvSimpleJson(t *testing.T) {
output := testXrayAuditPipenv(t, string(utils.SimpleJson))
verifySimpleJsonScanResults(t, output, 0, 0, 3, 1)
}
func testXrayAuditPipenv(t *testing.T, format string) string {
initXrayTest(t, commands.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
pipenvProjectPath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "xray", "python", "pipenv")
// Copy the pipenv project from the testdata to a temp dir
assert.NoError(t, fileutils.CopyDir(pipenvProjectPath, tempDirPath, true, nil))
prevWd := changeWD(t, tempDirPath)
defer clientTestUtils.ChangeDirAndAssert(t, prevWd)
return xrayCli.RunCliCmdWithOutput(t, "audit", "--pipenv", "--licenses", "--format="+format)
}
func initXrayTest(t *testing.T, minVersion string) {
if !*tests.TestXray {
t.Skip("Skipping Xray test. To run Xray test add the '-test.xray=true' option.")
}
validateXrayVersion(t, minVersion)
}
func validateXrayVersion(t *testing.T, minVersion string) {
xrayVersion, err := getXrayVersion()
if err != nil {
assert.NoError(t, err)
return
}
err = commands.ValidateXrayMinimumVersion(xrayVersion.GetVersion(), minVersion)
if err != nil {
t.Skip(err)
}
}
func getXrayVersion() (version.Version, error) {
xrayVersion, err := xrayAuth.GetVersion()
return *version.NewVersion(xrayVersion), err
}
func verifyJsonScanResults(t *testing.T, content string, minViolations, minVulnerabilities, minLicenses int) {
var results []services.ScanResponse
err := json.Unmarshal([]byte(content), &results)
assert.NoError(t, err)
var violations []services.Violation
var vulnerabilities []services.Vulnerability
var licenses []services.License
for _, result := range results {
violations = append(violations, result.Violations...)
vulnerabilities = append(vulnerabilities, result.Vulnerabilities...)
licenses = append(licenses, result.Licenses...)
}
assert.True(t, len(violations) >= minViolations, fmt.Sprintf("Expected at least %d violations in scan results, but got %d violations.", minViolations, len(violations)))
assert.True(t, len(vulnerabilities) >= minVulnerabilities, fmt.Sprintf("Expected at least %d vulnerabilities in scan results, but got %d vulnerabilities.", minVulnerabilities, len(vulnerabilities)))
assert.True(t, len(licenses) >= minLicenses, fmt.Sprintf("Expected at least %d Licenses in scan results, but got %d Licenses.", minLicenses, len(licenses)))
}
func verifySimpleJsonScanResults(t *testing.T, content string, minSecViolations, minLicViolations, minVulnerabilities, minLicenses int) {
var results formats.SimpleJsonResults
err := json.Unmarshal([]byte(content), &results)
assert.NoError(t, err)
assert.GreaterOrEqual(t, len(results.SecurityViolations), minSecViolations)
assert.GreaterOrEqual(t, len(results.LicensesViolations), minLicViolations)
assert.GreaterOrEqual(t, len(results.Vulnerabilities), minVulnerabilities)
assert.GreaterOrEqual(t, len(results.Licenses), minLicenses)
}