-
Notifications
You must be signed in to change notification settings - Fork 22
/
audit_test.go
722 lines (641 loc) · 28 KB
/
audit_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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/jfrog/jfrog-cli-security/utils/jasutils"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
"github.com/jfrog/jfrog-cli-security/cli"
"github.com/jfrog/jfrog-cli-security/cli/docs"
"github.com/jfrog/jfrog-cli-security/utils/formats"
"github.com/jfrog/jfrog-cli-security/utils/validations"
xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils"
"github.com/stretchr/testify/assert"
biutils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/jfrog-cli-core/v2/common/format"
"github.com/jfrog/jfrog-cli-core/v2/common/progressbar"
coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
securityTests "github.com/jfrog/jfrog-cli-security/tests"
securityTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils"
"github.com/jfrog/jfrog-cli-security/tests/utils/integration"
securityIntegrationTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils/integration"
"github.com/jfrog/jfrog-cli-security/utils/xray/scangraph"
clientTests "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/jfrog/jfrog-client-go/xray/services"
)
func TestXrayAuditNpmJson(t *testing.T) {
output := testAuditNpm(t, string(format.Json), false)
validations.VerifyJsonResults(t, output, validations.ValidationParams{
SecurityViolations: 1,
Licenses: 1,
})
}
func TestXrayAuditNpmSimpleJson(t *testing.T) {
output := testAuditNpm(t, string(format.SimpleJson), true)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
SecurityViolations: 1,
Vulnerabilities: 1,
Licenses: 1,
})
}
func testAuditNpm(t *testing.T, format string, withVuln bool) string {
integration.InitAuditJavaScriptTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "npm", "npm"))
defer cleanUp()
// Run npm install before executing jfrog xr npm-audit
assert.NoError(t, exec.Command("npm", "install").Run())
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, true)
watchName, deleteWatch := securityTestUtils.CreateTestWatch(t, "audit-policy", "audit-watch", xrayUtils.High)
defer deleteWatch()
args := []string{"audit", "--npm", "--licenses", "--format=" + format, "--watches=" + watchName, "--fail=false"}
if withVuln {
args = append(args, "--vuln")
}
return securityTests.PlatformCli.RunCliCmdWithOutput(t, args...)
}
func TestXrayAuditConanJson(t *testing.T) {
output := testAuditConan(t, string(format.Json), true)
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 8,
Licenses: 2,
})
}
func TestXrayAuditConanSimpleJson(t *testing.T) {
output := testAuditConan(t, string(format.SimpleJson), true)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 8,
Licenses: 2,
})
}
func testAuditConan(t *testing.T, format string, withVuln bool) string {
integration.InitAuditCTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "conan"))
defer cleanUp()
// Run conan install before executing jfrog audit
assert.NoError(t, exec.Command("conan").Run())
watchName, deleteWatch := securityTestUtils.CreateTestWatch(t, "audit-curation-policy", "audit-curation-watch", xrayUtils.High)
defer deleteWatch()
args := []string{"audit", "--licenses", "--format=" + format, "--watches=" + watchName, "--fail=false"}
if withVuln {
args = append(args, "--vuln")
}
return securityTests.PlatformCli.RunCliCmdWithOutput(t, args...)
}
func TestXrayAuditPnpmJson(t *testing.T) {
output := testXrayAuditPnpm(t, string(format.Json))
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 1,
Licenses: 1,
})
}
func TestXrayAuditPnpmSimpleJson(t *testing.T) {
output := testXrayAuditPnpm(t, string(format.SimpleJson))
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 1,
Licenses: 1,
})
}
func testXrayAuditPnpm(t *testing.T, format string) string {
integration.InitAuditJavaScriptTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "npm", "npm-no-lock"))
defer cleanUp()
// Run pnpm install before executing audit
assert.NoError(t, exec.Command("pnpm", "install").Run())
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, true)
return securityTests.PlatformCli.RunCliCmdWithOutput(t, "audit", "--pnpm", "--licenses", "--format="+format)
}
func TestXrayAuditYarnV2Json(t *testing.T) {
testXrayAuditYarn(t, "yarn-v2", func() {
output := runXrayAuditYarnWithOutput(t, string(format.Json))
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 1,
Licenses: 1,
})
})
}
func TestXrayAuditYarnV2SimpleJson(t *testing.T) {
testXrayAuditYarn(t, "yarn-v3", func() {
output := runXrayAuditYarnWithOutput(t, string(format.SimpleJson))
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 1,
Licenses: 1,
})
})
}
func TestXrayAuditYarnV1Json(t *testing.T) {
testXrayAuditYarn(t, "yarn-v1", func() {
output := runXrayAuditYarnWithOutput(t, string(format.Json))
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 1,
Licenses: 1,
})
})
}
func TestXrayAuditYarnV1JsonWithoutDevDependencies(t *testing.T) {
unsetEnv := clientTests.SetEnvWithCallbackAndAssert(t, "NODE_ENV", "production")
defer unsetEnv()
testXrayAuditYarn(t, "yarn-v1", func() {
output := runXrayAuditYarnWithOutput(t, string(format.Json))
var results []services.ScanResponse
err := json.Unmarshal([]byte(output), &results)
assert.NoError(t, err)
assert.Len(t, results[0].Vulnerabilities, 0)
})
}
func TestXrayAuditYarnV1SimpleJson(t *testing.T) {
testXrayAuditYarn(t, "yarn-v1", func() {
output := runXrayAuditYarnWithOutput(t, string(format.SimpleJson))
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 1,
Licenses: 1,
})
})
}
func testXrayAuditYarn(t *testing.T, projectDirName string, yarnCmd func()) {
integration.InitAuditJavaScriptTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "yarn", projectDirName))
defer cleanUp()
// Run yarn install before executing jf audit --yarn. Return error to assert according to test.
assert.NoError(t, exec.Command("yarn").Run())
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, true)
yarnCmd()
}
func runXrayAuditYarnWithOutput(t *testing.T, format string) string {
return securityTests.PlatformCli.RunCliCmdWithOutput(t, "audit", "--yarn", "--licenses", "--format="+format)
}
// Tests NuGet audit by providing simple NuGet project + multi-project NuGet project and asserts any error.
func TestXrayAuditNugetJson(t *testing.T) {
var testdata = []struct {
projectName string
format string
restoreTech string
minVulnerabilities int
minLicences int
}{
{
projectName: "single4.0",
format: string(format.Json),
restoreTech: "nuget",
minVulnerabilities: 2,
minLicences: 0,
},
{
projectName: "single5.0",
format: string(format.Json),
restoreTech: "dotnet",
minVulnerabilities: 3,
minLicences: 2,
},
{
projectName: "single5.0",
format: string(format.Json),
restoreTech: "",
minVulnerabilities: 3,
minLicences: 2,
},
{
projectName: "multi",
format: string(format.Json),
restoreTech: "dotnet",
minVulnerabilities: 4,
minLicences: 3,
},
{
projectName: "multi",
format: string(format.Json),
restoreTech: "",
minVulnerabilities: 4,
minLicences: 3,
},
}
for _, test := range testdata {
runInstallCommand := test.restoreTech != ""
t.Run(fmt.Sprintf("projectName:%s,runInstallCommand:%t", test.projectName, runInstallCommand),
func(t *testing.T) {
output := testXrayAuditNuget(t, test.projectName, test.format, test.restoreTech)
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: test.minVulnerabilities,
Licenses: test.minLicences,
})
})
}
}
func TestXrayAuditNugetSimpleJson(t *testing.T) {
var testdata = []struct {
projectName string
format string
restoreTech string
minVulnerabilities int
minLicences int
}{
{
projectName: "single4.0",
format: string(format.SimpleJson),
restoreTech: "nuget",
minVulnerabilities: 2,
minLicences: 0,
},
{
projectName: "single5.0",
format: string(format.SimpleJson),
restoreTech: "dotnet",
minVulnerabilities: 3,
minLicences: 2,
},
{
projectName: "single5.0",
format: string(format.SimpleJson),
restoreTech: "",
minVulnerabilities: 3,
minLicences: 2,
},
}
for _, test := range testdata {
runInstallCommand := test.restoreTech != ""
t.Run(fmt.Sprintf("projectName:%s,runInstallCommand:%t", test.projectName, runInstallCommand),
func(t *testing.T) {
output := testXrayAuditNuget(t, test.projectName, test.format, test.restoreTech)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: test.minVulnerabilities,
Licenses: test.minLicences,
})
})
}
}
func testXrayAuditNuget(t *testing.T, projectName, format string, restoreTech string) string {
integration.InitAuditCTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "nuget", projectName))
defer cleanUp()
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, false)
// Run NuGet/Dotnet restore before executing jfrog xr audit (NuGet)
if restoreTech != "" {
output, err := exec.Command(restoreTech, "restore").CombinedOutput()
assert.NoError(t, err, string(output))
}
return securityTests.PlatformCli.RunCliCmdWithOutput(t, "audit", "--nuget", "--format="+format, "--licenses")
}
func TestXrayAuditGradleJson(t *testing.T) {
output := testXrayAuditGradle(t, string(format.Json))
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 3,
Licenses: 3,
})
}
func TestXrayAuditGradleSimpleJson(t *testing.T) {
output := testXrayAuditGradle(t, string(format.SimpleJson))
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 3,
Licenses: 3,
})
}
func testXrayAuditGradle(t *testing.T, format string) string {
integration.InitAuditJavaTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "gradle", "gradle"))
defer cleanUp()
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, false)
return securityTests.PlatformCli.RunCliCmdWithOutput(t, "audit", "--gradle", "--licenses", "--format="+format)
}
func TestXrayAuditMavenJson(t *testing.T) {
output := testXscAuditMaven(t, string(format.Json))
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 1,
Licenses: 1,
})
}
func TestXrayAuditMavenSimpleJson(t *testing.T) {
output := testXscAuditMaven(t, string(format.SimpleJson))
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 1,
Licenses: 1,
})
}
func testXscAuditMaven(t *testing.T, format string) string {
integration.InitAuditJavaTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "maven", "maven"))
defer cleanUp()
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, false)
return securityTests.PlatformCli.RunCliCmdWithOutput(t, "audit", "--mvn", "--licenses", "--format="+format)
}
func TestXrayAuditGoJson(t *testing.T) {
output := testXrayAuditGo(t, false, string(format.Json), "simple-project")
validations.VerifyJsonResults(t, output, validations.ValidationParams{Licenses: 1, Vulnerabilities: 4})
}
func TestXrayAuditGoSimpleJson(t *testing.T) {
output := testXrayAuditGo(t, true, string(format.SimpleJson), "simple-project")
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{Licenses: 3, Vulnerabilities: 4, NotCovered: 2, NotApplicable: 2})
}
func testXrayAuditGo(t *testing.T, noCreds bool, format, project string) string {
integration.InitAuditGoTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "go", project))
defer cleanUp()
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, false)
cliToRun := securityTests.PlatformCli
if noCreds {
cliToRun = securityTests.PlatformCli.WithoutCredentials()
// Configure a new server named "default"
securityIntegrationTestUtils.CreateJfrogHomeConfig(t, true)
defer securityTestUtils.CleanTestsHomeEnv()
}
return cliToRun.RunCliCmdWithOutput(t, "audit", "--go", "--licenses", "--format="+format)
}
func TestXrayAuditNoTech(t *testing.T) {
integration.InitAuditGeneralTests(t, scangraph.GraphScanMinXrayVersion)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
prevWd := securityTestUtils.ChangeWD(t, tempDirPath)
defer clientTests.ChangeDirAndAssert(t, prevWd)
// Run audit on empty folder, expect an error
err := securityTests.PlatformCli.Exec("audit")
assert.NoError(t, err)
}
func TestXrayAuditMultiProjects(t *testing.T) {
integration.InitAuditGeneralTests(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects"))
defer cleanUp()
// Set working-dirs flag with multiple projects
workingDirsFlag := fmt.Sprintf("--working-dirs=%s, %s ,%s, %s",
filepath.Join("package-managers", "maven", "maven"), filepath.Join("package-managers", "nuget", "single4.0"),
filepath.Join("package-managers", "python", "pip", "pip-project"), filepath.Join("jas", "jas"))
// Configure a new server named "default"
securityIntegrationTestUtils.CreateJfrogHomeConfig(t, true)
defer securityTestUtils.CleanTestsHomeEnv()
output := securityTests.PlatformCli.WithoutCredentials().RunCliCmdWithOutput(t, "audit", "--format="+string(format.SimpleJson), workingDirsFlag)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Sast: 1,
Iac: 9,
Secrets: 6,
Vulnerabilities: 35,
Applicable: 3,
Undetermined: 0,
NotCovered: 22,
NotApplicable: 2,
})
}
func TestXrayAuditPipJson(t *testing.T) {
output := testXrayAuditPip(t, string(format.Json), "")
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 3,
Licenses: 1,
})
}
func TestXrayAuditPipSimpleJson(t *testing.T) {
output := testXrayAuditPip(t, string(format.SimpleJson), "")
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 3,
Licenses: 1,
})
}
func TestXrayAuditPipJsonWithRequirementsFile(t *testing.T) {
output := testXrayAuditPip(t, string(format.Json), "requirements.txt")
validations.VerifyJsonResults(t, output, validations.ValidationParams{Vulnerabilities: 2})
}
func TestXrayAuditPipSimpleJsonWithRequirementsFile(t *testing.T) {
output := testXrayAuditPip(t, string(format.SimpleJson), "requirements.txt")
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{Vulnerabilities: 2})
}
func testXrayAuditPip(t *testing.T, format, requirementsFile string) string {
integration.InitAuditPythonTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "python", "pip", "pip-project"))
defer cleanUp()
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, false)
args := []string{"audit", "--pip", "--licenses", "--format=" + format}
if requirementsFile != "" {
args = append(args, "--requirements-file="+requirementsFile)
}
return securityTests.PlatformCli.RunCliCmdWithOutput(t, args...)
}
func TestXrayAuditPipenvJson(t *testing.T) {
output := testXrayAuditPipenv(t, string(format.Json))
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 3,
Licenses: 1,
})
}
func TestXrayAuditPipenvSimpleJson(t *testing.T) {
output := testXrayAuditPipenv(t, string(format.SimpleJson))
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 3,
Licenses: 1,
})
}
func testXrayAuditPipenv(t *testing.T, format string) string {
integration.InitAuditPythonTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "python", "pipenv", "pipenv-project"))
defer cleanUp()
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, false)
return securityTests.PlatformCli.RunCliCmdWithOutput(t, "audit", "--pipenv", "--licenses", "--format="+format)
}
func TestXrayAuditPoetryJson(t *testing.T) {
output := testXrayAuditPoetry(t, string(format.Json))
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 3,
Licenses: 1,
})
}
func TestXrayAuditPoetrySimpleJson(t *testing.T) {
output := testXrayAuditPoetry(t, string(format.SimpleJson))
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 3,
Licenses: 1,
})
}
func testXrayAuditPoetry(t *testing.T, format string) string {
integration.InitAuditPythonTest(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "python", "poetry", "poetry-project"))
defer cleanUp()
// Add dummy descriptor file to check that we run only specific audit
addDummyPackageDescriptor(t, false)
return securityTests.PlatformCli.RunCliCmdWithOutput(t, "audit", "--poetry", "--licenses", "--format="+format)
}
func addDummyPackageDescriptor(t *testing.T, hasPackageJson bool) {
descriptor := "package.json"
if hasPackageJson {
descriptor = "pom.xml"
}
dummyFile, err := os.Create(descriptor)
assert.NoError(t, err)
assert.NoError(t, dummyFile.Close())
}
// JAS
func TestXrayAuditSastCppFlagSimpleJson(t *testing.T) {
output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("package-managers", "c"), "3", false, true)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: 1,
Sast: 1,
})
}
func TestXrayAuditWithoutSastCppFlagSimpleJson(t *testing.T) {
output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("package-managers", "c"), "3", false, false)
// verify no results for Sast
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{})
}
func TestXrayAuditJasMissingContextSimpleJson(t *testing.T) {
output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("package-managers", "maven", "missing-context"), "3", false, false)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{MissingContext: 1})
}
func TestXrayAuditNotEntitledForJas(t *testing.T) {
integration.InitAuditGeneralTests(t, scangraph.GraphScanMinXrayVersion)
cliToRun, cleanUp := integration.InitTestWithMockCommandOrParams(t, false, getNoJasAuditMockCommand)
defer cleanUp()
output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false, false)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{Vulnerabilities: 8})
}
func getNoJasAuditMockCommand() components.Command {
return components.Command{
Name: docs.Audit,
Flags: docs.GetCommandFlags(docs.Audit),
Action: func(c *components.Context) error {
auditCmd, err := cli.CreateAuditCmd(c)
if err != nil {
return err
}
// Disable Jas for this test
auditCmd.SetUseJas(false)
return progressbar.ExecWithProgress(auditCmd)
},
}
}
func TestXrayAuditJasSimpleJson(t *testing.T) {
output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("jas", "jas"), "3", false, false)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Sast: 1,
Iac: 9,
Secrets: 6,
Vulnerabilities: 8,
Applicable: 3,
Undetermined: 1,
NotCovered: 1,
NotApplicable: 2,
})
}
func TestXrayAuditJasSimpleJsonWithTokenValidation(t *testing.T) {
integration.InitAuditGeneralTests(t, jasutils.DynamicTokenValidationMinXrayVersion)
output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("jas", "jas"), "3", true, false)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{Vulnerabilities: 5, Inactive: 5})
}
func TestXrayAuditJasSimpleJsonWithOneThread(t *testing.T) {
output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("jas", "jas"), "1", false, false)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Sast: 1,
Iac: 9,
Secrets: 6,
Vulnerabilities: 8,
Applicable: 3,
Undetermined: 1,
NotCovered: 1,
NotApplicable: 2,
})
}
func TestXrayAuditJasSimpleJsonWithConfig(t *testing.T) {
output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("jas", "jas-config"), "3", false, false)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Secrets: 1,
Vulnerabilities: 8,
Applicable: 3,
Undetermined: 1,
NotCovered: 1,
NotApplicable: 2,
})
}
func TestXrayAuditJasNoViolationsSimpleJson(t *testing.T) {
output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("package-managers", "npm", "npm"), "3", false, false)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{Vulnerabilities: 1, NotApplicable: 1})
}
func testXrayAuditJas(t *testing.T, testCli *coreTests.JfrogCli, project string, threads string, validateSecrets, validateSastCpp bool) string {
integration.InitAuditGeneralTests(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), filepath.Join("projects", project)))
defer cleanUp()
// Configure a new server named "default"
securityIntegrationTestUtils.CreateJfrogHomeConfig(t, true)
defer securityTestUtils.CleanTestsHomeEnv()
args := []string{"audit", "--format=" + string(format.SimpleJson), "--threads=" + threads}
if validateSecrets {
args = append(args, "--secrets", "--validate-secrets")
}
if validateSastCpp {
unsetEnv := clientTests.SetEnvWithCallbackAndAssert(t, "JFROG_SAST_ENABLE_CPP", "1")
defer unsetEnv()
}
return testCli.WithoutCredentials().RunCliCmdWithOutput(t, args...)
}
func TestXrayAuditDetectTech(t *testing.T) {
integration.InitAuditGeneralTests(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "maven", "maven"))
defer cleanUp()
// Run generic audit on mvn project with a vulnerable dependency
output := securityTests.PlatformCli.RunCliCmdWithOutput(t, "audit", "--licenses", "--format="+string(format.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].ImpactedDependencyType), "maven")
}
func TestXrayRecursiveScan(t *testing.T) {
integration.InitAuditGeneralTests(t, scangraph.GraphScanMinXrayVersion)
projectDir := filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers")
// Creating an inner NPM project
tempDirPath, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(projectDir, "npm", "npm"))
defer cleanUp()
// Creating an inner .NET project
dotnetDirPath, err := os.MkdirTemp(tempDirPath, "dotnet-project")
assert.NoError(t, err)
dotnetProjectToCopyPath := filepath.Join(projectDir, "dotnet", "dotnet-single")
assert.NoError(t, biutils.CopyDir(dotnetProjectToCopyPath, dotnetDirPath, true, nil))
// We anticipate the execution of a recursive scan to encompass both the inner NPM project and the inner .NET project.
output := securityTests.PlatformCli.RunCliCmdWithOutput(t, "audit", "--format=json")
// We anticipate the identification of five vulnerabilities: four originating from the .NET project and one from the NPM project.
validations.VerifyJsonResults(t, output, validations.ValidationParams{Vulnerabilities: 4})
var results []services.ScanResponse
err = json.Unmarshal([]byte(output), &results)
assert.NoError(t, err)
// We anticipate receiving an array with a length of 2 to confirm that we have obtained results from two distinct inner projects.
assert.Len(t, results, 2)
}
func TestAuditOnEmptyProject(t *testing.T) {
integration.InitAuditGeneralTests(t, scangraph.GraphScanMinXrayVersion)
_, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), filepath.Join("projects", "empty_project", "python_project_with_no_deps")))
defer cleanUp()
// Configure a new server named "default"
securityIntegrationTestUtils.CreateJfrogHomeConfig(t, true)
defer securityTestUtils.CleanTestsHomeEnv()
output := securityTests.PlatformCli.WithoutCredentials().RunCliCmdWithOutput(t, "audit", "--format="+string(format.SimpleJson))
// No issues should be found in an empty project
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{})
}
// xray-url only - the following tests check the case of adding "xray-url", instead of "url", which is the more common one
func TestXrayAuditNotEntitledForJasWithXrayUrl(t *testing.T) {
cliToRun, cleanUp := integration.InitTestWithMockCommandOrParams(t, true, getNoJasAuditMockCommand)
defer cleanUp()
output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false, false)
// Verify that scan results are printed
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{Vulnerabilities: 8})
// Verify that JAS results are not printed
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{})
}
func TestXrayAuditJasSimpleJsonWithXrayUrl(t *testing.T) {
cliToRun := integration.GetTestCli(cli.GetJfrogCliSecurityApp(), true)
output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false, false)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Sast: 1,
Iac: 9,
Secrets: 6,
Vulnerabilities: 8,
Applicable: 3,
Undetermined: 1,
NotCovered: 1,
NotApplicable: 2,
})
}