forked from jfrog/jfrog-cli-security
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscans_test.go
More file actions
432 lines (391 loc) · 17.3 KB
/
scans_test.go
File metadata and controls
432 lines (391 loc) · 17.3 KB
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
package main
import (
"encoding/json"
"path"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/ocicontainer"
"github.com/jfrog/jfrog-cli-artifactory/utils/tests"
"github.com/stretchr/testify/assert"
"github.com/jfrog/jfrog-cli-security/commands/scan"
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"
"github.com/jfrog/jfrog-cli-security/tests/validations"
"github.com/jfrog/jfrog-cli-security/utils"
"github.com/jfrog/jfrog-cli-security/utils/formats"
"github.com/jfrog/jfrog-cli-security/utils/jasutils"
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/container"
"github.com/jfrog/jfrog-cli-core/v2/common/build"
"github.com/jfrog/jfrog-cli-core/v2/common/format"
commonTests "github.com/jfrog/jfrog-cli-core/v2/common/tests"
coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
"github.com/jfrog/jfrog-cli-security/utils/xray/scangraph"
"github.com/jfrog/jfrog-client-go/utils/log"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils"
)
type binaryScanParams struct {
// The argument to scan, e.g., a binary file or a directory containing binaries
BinaryPattern string
// --format flag value if provided
Format format.OutputFormat
// --licenses flag value if provided
WithLicense bool
// --sbom flag value if provided
WithSbom bool
// selective scans to perform
WithSubScans []utils.SubScanType
// -- without-contextual-analysis flag value if provided
WithoutContextualAnalysis bool
// Will combined with "," if provided and be used as --watches flag value
Watches []string
// -- vuln flag 'True' value must be provided with 'createWatchesFuncs' to create watches for the test
WithVuln bool
// Number of threads to use for scanning
Threads int
}
func subScansToFlags(subScans []utils.SubScanType) (flags []string) {
for _, scanType := range subScans {
switch scanType {
case utils.ScaScan:
flags = append(flags, "--sca")
case utils.SecretsScan:
flags = append(flags, "--secrets")
}
}
return flags
}
func getBinaryScanCmdArgs(params binaryScanParams) (args []string) {
if params.WithLicense {
args = append(args, "--licenses")
}
if params.WithSbom {
args = append(args, "--sbom")
}
if params.Format != "" {
args = append(args, "--format="+string(params.Format))
}
if len(params.WithSubScans) > 0 {
args = append(args, subScansToFlags(params.WithSubScans)...)
}
if params.WithoutContextualAnalysis {
args = append(args, "--without-contextual-analysis")
}
if params.WithVuln {
args = append(args, "--vuln")
}
if len(params.Watches) > 0 {
args = append(args, "--watches="+strings.Join(params.Watches, ","))
}
if params.Threads > 0 {
args = append(args, "--threads="+strconv.Itoa(params.Threads))
}
args = append(args, params.BinaryPattern)
return args
}
func testXrayBinaryScan(t *testing.T, params binaryScanParams, errorExpected bool) string {
output, err := runXrayBinaryScan(t, params)
if errorExpected {
if err != nil {
log.Error("Xray binary scan failed with err: " + err.Error())
}
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
return output
}
func runXrayBinaryScan(t *testing.T, params binaryScanParams) (string, error) {
cleanUp := integration.UseTestHomeWithDefaultXrayConfig(t)
defer cleanUp()
return securityTests.PlatformCli.RunCliCmdWithOutputs(t, append([]string{"scan"}, getBinaryScanCmdArgs(params)...)...)
}
// Binary scan tests
func TestXrayBinaryScanJson(t *testing.T) {
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
output := testXrayMultipleBinariesScan(t, binaryScanParams{Format: format.Json, WithLicense: true}, false)
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Total: &validations.TotalCount{Licenses: 1, Vulnerabilities: 1},
})
}
func TestXrayBinaryScanSimpleJson(t *testing.T) {
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
output := testXrayBinaryScanWithWatch(t, format.SimpleJson, "xray-scan-binary-policy", "scan-binary-watch", true)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Total: &validations.TotalCount{Licenses: 1, Vulnerabilities: 1, Violations: 1},
})
}
func TestXrayBinaryScanCycloneDx(t *testing.T) {
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
output := testXrayBinaryScanJASArtifact(t, "backupfriend-client.tar.gz", false, binaryScanParams{Format: format.CycloneDx})
validations.VerifyCycloneDxResults(t, output, validations.ValidationParams{
Total: &validations.TotalCount{Vulnerabilities: 4},
Vulnerabilities: &validations.VulnerabilityCount{
ValidateScan: &validations.ScanCount{Sca: 3, Secrets: 1},
ValidateApplicabilityStatus: &validations.ApplicabilityStatusCount{Applicable: 2, Undetermined: 1},
},
})
}
func TestXrayBinaryScanJsonDocker(t *testing.T) {
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
// In Windows, indexer fails to index the tar, Caused by: failed to rename path for layer so we run in clean copy in temp dir
output := testXrayBinaryScanJASArtifact(t, "xmas.tar", true, binaryScanParams{Format: format.SimpleJson})
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Total: &validations.TotalCount{Vulnerabilities: 6},
Vulnerabilities: &validations.VulnerabilityCount{
ValidateScan: &validations.ScanCount{Sca: 4, Secrets: 2},
ValidateApplicabilityStatus: &validations.ApplicabilityStatusCount{Applicable: 2, NotApplicable: 1, NotCovered: 1},
},
})
}
func TestXrayBinaryScanJsonGeneric(t *testing.T) {
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
output := testXrayBinaryScanJASArtifact(t, "backupfriend-client.tar.gz", false, binaryScanParams{Format: format.SimpleJson})
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Total: &validations.TotalCount{Vulnerabilities: 4},
Vulnerabilities: &validations.VulnerabilityCount{
ValidateScan: &validations.ScanCount{Sca: 3, Secrets: 1},
ValidateApplicabilityStatus: &validations.ApplicabilityStatusCount{Applicable: 2, Undetermined: 1},
},
})
}
func TestXrayBinaryScanJsonJar(t *testing.T) {
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
output := testXrayBinaryScanJASArtifact(t, "student-services-security-0.0.1.jar", false, binaryScanParams{Format: format.SimpleJson})
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Total: &validations.TotalCount{Vulnerabilities: 41},
Vulnerabilities: &validations.VulnerabilityCount{
ValidateScan: &validations.ScanCount{Sca: 40, Secrets: 1},
ValidateApplicabilityStatus: &validations.ApplicabilityStatusCount{Applicable: 17, NotCovered: 3, NotApplicable: 20},
},
})
}
func TestXrayBinaryScanSelectiveScan(t *testing.T) {
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
testCases := []struct {
name string
subScans []utils.SubScanType
withoutCa bool
validate func(t *testing.T, issueCount validations.ValidationCountActualValues)
}{
{
name: "SCA only scan",
subScans: []utils.SubScanType{utils.ScaScan},
withoutCa: true,
validate: func(t *testing.T, issueCount validations.ValidationCountActualValues) {
// Expect only SCA vulnerabilities
assert.GreaterOrEqual(t, issueCount.ScaVulnerabilities, 3, "SCA vulnerabilities count mismatch - should be 3 or more")
// All other vulnerability types should be 0
assert.Equal(t, 0, issueCount.SecretsVulnerabilities, "Secrets vulnerabilities count mismatch - should be 0")
assert.Equal(t, 0, issueCount.ApplicableVulnerabilities, "Applicable vulnerabilities count mismatch - should be 0")
assert.Equal(t, 0, issueCount.UndeterminedVulnerabilities, "Undetermined vulnerabilities count mismatch - should be 0")
},
},
{
name: "Secrets only scan",
subScans: []utils.SubScanType{utils.SecretsScan},
validate: func(t *testing.T, issueCount validations.ValidationCountActualValues) {
// Expect only Secrets vulnerabilities
assert.GreaterOrEqual(t, issueCount.SecretsVulnerabilities, 1, "Secrets vulnerabilities count mismatch - should be 1 or more")
// All other vulnerability types should be 0
assert.Equal(t, 0, issueCount.ScaVulnerabilities, "SCA vulnerabilities count mismatch - should be 0")
assert.Equal(t, 0, issueCount.ApplicableVulnerabilities, "Applicable vulnerabilities count mismatch - should be 0")
assert.Equal(t, 0, issueCount.UndeterminedVulnerabilities, "Undetermined vulnerabilities count mismatch - should be 0")
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output := testXrayBinaryScanJASArtifact(t, "backupfriend-client.tar.gz", false, binaryScanParams{Format: format.CycloneDx, WithSubScans: tc.subScans, WithoutContextualAnalysis: tc.withoutCa})
tc.validate(t, validations.GetCycloneDxActualValues(t, output))
})
}
}
func TestXrayBinaryScanJsonWithProgress(t *testing.T) {
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
callback := commonTests.MockProgressInitialization()
defer callback()
output := testXrayMultipleBinariesScan(t, binaryScanParams{Format: format.Json, WithLicense: true}, false)
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Total: &validations.TotalCount{Licenses: 1, Vulnerabilities: 1},
})
}
func TestXrayBinaryScanSimpleJsonWithProgress(t *testing.T) {
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
callback := commonTests.MockProgressInitialization()
defer callback()
output := testXrayBinaryScanWithWatch(t, format.SimpleJson, "xray-scan-binary-progress-policy", "scan-binary-progress-watch", true)
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Total: &validations.TotalCount{Licenses: 1, Vulnerabilities: 1, Violations: 1},
})
}
func testXrayBinaryScanWithWatch(t *testing.T, format format.OutputFormat, policyName, watchName string, errorExpected bool) string {
params := binaryScanParams{
WithLicense: true,
Format: format,
}
if policyName != "" && watchName != "" {
watchName, deleteWatch := securityTestUtils.CreateTestPolicyAndWatch(t, "xray-scan-binary-policy", "scan-binary-watch", xrayUtils.High)
defer deleteWatch()
// Include violations and vulnerabilities
params.Watches = []string{watchName}
params.WithVuln = true
}
return testXrayMultipleBinariesScan(t, params, errorExpected)
}
func testXrayMultipleBinariesScan(t *testing.T, params binaryScanParams, errorExpected bool) string {
params.BinaryPattern = filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "binaries", "*")
return testXrayBinaryScan(t, params, errorExpected)
}
func testXrayBinaryScanJASArtifact(t *testing.T, artifact string, inTempDir bool, params binaryScanParams) string {
pathToScan := filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "jas-scan")
if inTempDir {
var cleanUp func()
pathToScan, cleanUp = securityTestUtils.CreateTestProjectEnvAndChdir(t, pathToScan)
defer cleanUp()
}
params.BinaryPattern = filepath.Join(pathToScan, artifact)
if params.Threads == 0 {
params.Threads = 5
}
return testXrayBinaryScan(t,
params,
false,
)
}
func TestXrayBinaryScanWithBypassArchiveLimits(t *testing.T) {
integration.InitScanTest(t, scan.BypassArchiveLimitsMinXrayVersion)
unsetEnv := clientTestUtils.SetEnvWithCallbackAndAssert(t, "JF_INDEXER_COMPRESS_MAXENTITIES", "10")
defer unsetEnv()
cleanUp := integration.UseTestHomeWithDefaultXrayConfig(t)
defer cleanUp()
binariesPath := filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "binaries", "*")
scanArgs := []string{"scan", binariesPath, "--format=json", "--licenses"}
// Run without bypass flag and expect scan to fail
err := securityTests.PlatformCli.Exec(scanArgs...)
// Expect error
assert.Error(t, err)
// Run with bypass flag and expect it to find vulnerabilities
scanArgs = append(scanArgs, "--bypass-archive-limits")
output := securityTests.PlatformCli.RunCliCmdWithOutput(t, scanArgs...)
validations.VerifyJsonResults(t, output, validations.ValidationParams{
Total: &validations.TotalCount{Licenses: 1, Vulnerabilities: 1},
})
}
// Docker scan tests
func TestDockerScanWithProgressBar(t *testing.T) {
callback := commonTests.MockProgressInitialization()
defer callback()
TestDockerScan(t)
}
func TestDockerScanWithTokenValidation(t *testing.T) {
securityTestUtils.GetAndValidateXrayVersion(t, jasutils.DynamicTokenValidationMinXrayVersion)
testCli, cleanup := integration.InitNativeDockerTest(t)
defer cleanup()
// #nosec G101 -- Image with dummy token for tests
tokensImageToScan := "srmishj/inactive_tokens:latest"
runDockerScan(t, testCli, tokensImageToScan, "", 0, 0, 0, 5, true)
}
func TestDockerScan(t *testing.T) {
testCli, cleanup := integration.InitNativeDockerTest(t)
defer cleanup()
watchName, deleteWatch := securityTestUtils.CreateTestPolicyAndWatch(t, "docker-policy", "docker-watch", xrayUtils.Low)
defer deleteWatch()
imagesToScan := []string{
// Image with RPM with vulnerabilities
"alpine:3.17",
}
for _, imageName := range imagesToScan {
runDockerScan(t, testCli, imageName, watchName, 3, 3, 3, 0, false)
}
// Image with 0 vulnerabilities
runDockerScan(t, testCli, "busybox:1.35", "", 0, 0, 0, 0, false)
}
func runDockerScan(t *testing.T, testCli *coreTests.JfrogCli, imageName, watchName string, minViolations, minVulnerabilities, minLicenses int, minInactives int, validateSecrets bool) {
// Pull image from docker repo
imageTag := path.Join(*securityTests.ContainerRegistry, securityTests.DockerVirtualRepo, imageName)
dockerPullCommand := container.NewPullCommand(ocicontainer.DockerClient)
dockerPullCommand.SetCmdParams([]string{"pull", imageTag}).SetImageTag(imageTag).SetRepo(securityTests.DockerVirtualRepo).SetServerDetails(securityTests.XrDetails).SetBuildConfiguration(new(build.BuildConfiguration))
if !assert.NoError(t, dockerPullCommand.Run()) {
return
}
defer tests.DeleteTestImage(t, imageTag, ocicontainer.DockerClient)
cleanUp := integration.UseTestHomeWithDefaultXrayConfig(t)
defer cleanUp()
// Run docker scan on image
cmdArgs := []string{"docker", "scan", imageTag, "--server-id=default", "--licenses", "--fail=false", "--min-severity=low", "--fixable-only"}
if validateSecrets {
cmdArgs = append(cmdArgs, "--secrets", "--validate-secrets", "--format=simple-json")
} else {
cmdArgs = append(cmdArgs, "--format=json")
}
if watchName != "" {
cmdArgs = append(cmdArgs, "--watches="+watchName, "--vuln")
}
output := testCli.WithoutCredentials().RunCliCmdWithOutput(t, cmdArgs...)
if assert.NotEmpty(t, output) {
if validateSecrets {
validations.VerifySimpleJsonResults(t, output, validations.ValidationParams{
Vulnerabilities: &validations.VulnerabilityCount{ValidateApplicabilityStatus: &validations.ApplicabilityStatusCount{Inactive: minInactives}},
})
} else {
validationParams := validations.ValidationParams{
Total: &validations.TotalCount{
Vulnerabilities: minVulnerabilities,
Licenses: minLicenses,
Violations: minViolations,
},
}
validations.VerifyJsonResults(t, output, validationParams)
}
}
}
// JAS docker scan tests
func TestAdvancedSecurityDockerScan(t *testing.T) {
testCli, cleanup := integration.InitNativeDockerTest(t)
defer cleanup()
runAdvancedSecurityDockerScan(t, testCli, "jfrog/demo-security:latest")
}
func runAdvancedSecurityDockerScan(t *testing.T, testCli *coreTests.JfrogCli, imageName string) {
// Pull image from docker repo
imageTag := path.Join(*securityTests.ContainerRegistry, securityTests.DockerVirtualRepo, imageName)
dockerPullCommand := container.NewPullCommand(ocicontainer.DockerClient)
dockerPullCommand.SetCmdParams([]string{"pull", imageTag}).SetImageTag(imageTag).SetRepo(securityTests.DockerVirtualRepo).SetServerDetails(securityTests.XrDetails).SetBuildConfiguration(new(build.BuildConfiguration))
if assert.NoError(t, dockerPullCommand.Run()) {
defer tests.DeleteTestImage(t, imageTag, ocicontainer.DockerClient)
args := []string{"docker", "scan", imageTag, "--server-id=default", "--format=simple-json", "--fail=false", "--min-severity=low", "--fixable-only"}
cleanUp := integration.UseTestHomeWithDefaultXrayConfig(t)
defer cleanUp()
// Run docker scan on image
output := testCli.WithoutCredentials().RunCliCmdWithOutput(t, args...)
if assert.NotEmpty(t, output) {
verifyAdvancedSecurityScanResults(t, output, true, true)
}
}
}
func verifyAdvancedSecurityScanResults(t *testing.T, content string, isApplicable bool, isSecret bool) {
var results formats.SimpleJsonResults
err := json.Unmarshal([]byte(content), &results)
assert.NoError(t, err)
// Verify that the scan succeeded, and that at least one "Applicable" status was received.
applicableStatusExists := false
for _, vulnerability := range results.Vulnerabilities {
if vulnerability.Applicable == string(jasutils.Applicable) {
applicableStatusExists = true
break
}
}
if isApplicable {
assert.True(t, applicableStatusExists)
}
if isSecret {
// Verify that secretes detection succeeded.
assert.NotEqual(t, 0, len(results.SecretsVulnerabilities))
} else {
// Verify that secretes detection succeeded.
assert.Equal(t, 0, len(results.SecretsVulnerabilities))
}
}