-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathscancommands.go
690 lines (653 loc) · 23.9 KB
/
scancommands.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
package cli
import (
"errors"
"fmt"
buildInfoUtils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/gofrog/datastructures"
"github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
commandsCommon "github.com/jfrog/jfrog-cli-core/v2/common/commands"
outputFormat "github.com/jfrog/jfrog-cli-core/v2/common/format"
"github.com/jfrog/jfrog-cli-core/v2/common/progressbar"
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
enrichDocs "github.com/jfrog/jfrog-cli-security/cli/docs/enrich"
"github.com/jfrog/jfrog-cli-security/commands/enrich"
"github.com/jfrog/jfrog-cli-security/utils/xray"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/urfave/cli"
"os"
"strings"
flags "github.com/jfrog/jfrog-cli-security/cli/docs"
auditSpecificDocs "github.com/jfrog/jfrog-cli-security/cli/docs/auditspecific"
auditDocs "github.com/jfrog/jfrog-cli-security/cli/docs/scan/audit"
buildScanDocs "github.com/jfrog/jfrog-cli-security/cli/docs/scan/buildscan"
curationDocs "github.com/jfrog/jfrog-cli-security/cli/docs/scan/curation"
dockerScanDocs "github.com/jfrog/jfrog-cli-security/cli/docs/scan/dockerscan"
scanDocs "github.com/jfrog/jfrog-cli-security/cli/docs/scan/scan"
"github.com/jfrog/jfrog-cli-security/commands/audit"
"github.com/jfrog/jfrog-cli-security/commands/curation"
"github.com/jfrog/jfrog-cli-security/commands/scan"
"github.com/jfrog/jfrog-cli-security/utils/severityutils"
"github.com/jfrog/jfrog-cli-security/utils/techutils"
"github.com/jfrog/jfrog-cli-security/utils/xsc"
)
const dockerScanCmdHiddenName = "dockerscan"
const SkipCurationAfterFailureEnv = "JFROG_CLI_SKIP_CURATION_AFTER_FAILURE"
func getAuditAndScansCommands() []components.Command {
return []components.Command{
{
Name: "scan",
Aliases: []string{"s"},
Flags: flags.GetCommandFlags(flags.XrScan),
Description: scanDocs.GetDescription(),
Arguments: scanDocs.GetArguments(),
Category: securityCategory,
Action: ScanCmd,
},
{
Name: "sbom-enrich",
Aliases: []string{"se"},
Flags: flags.GetCommandFlags(flags.Enrich),
Description: enrichDocs.GetDescription(),
Arguments: enrichDocs.GetArguments(),
Category: securityCategory,
Action: EnrichCmd,
},
{
Name: "build-scan",
Aliases: []string{"bs"},
Flags: flags.GetCommandFlags(flags.BuildScan),
Description: buildScanDocs.GetDescription(),
Arguments: buildScanDocs.GetArguments(),
Category: securityCategory,
Action: BuildScan,
},
{
// this command is hidden and have no logic, it will be run to provide 'help' as a part of the buildtools CLI for 'docker' commands. ('jf docker scan')
// CLI buildtools will run the command if requested: https://github.com/jfrog/jfrog-cli/blob/v2/buildtools/cli.go
Name: dockerScanCmdHiddenName,
Flags: flags.GetCommandFlags(flags.DockerScan),
Description: dockerScanDocs.GetDescription(),
Arguments: dockerScanDocs.GetArguments(),
UsageOptions: &components.UsageOptions{
Usage: dockerScanDocs.Usage,
ReplaceAutoGeneratedUsage: true,
},
Hidden: true,
},
{
Name: "audit",
Aliases: []string{"aud"},
Flags: flags.GetCommandFlags(flags.Audit),
Description: auditDocs.GetDescription(),
Category: securityCategory,
Action: AuditCmd,
},
{
Name: "curation-audit",
Aliases: []string{"ca"},
Flags: flags.GetCommandFlags(flags.CurationAudit),
Description: curationDocs.GetDescription(),
Category: securityCategory,
Action: CurationCmd,
},
// TODO: Deprecated commands (remove at next CLI major version)
{
Name: "audit-mvn",
Aliases: []string{"am"},
Flags: flags.GetCommandFlags(flags.AuditMvn),
Description: auditSpecificDocs.GetMvnDescription(),
Action: func(c *components.Context) error {
return AuditSpecificCmd(c, techutils.Maven)
},
Hidden: true,
},
{
Name: "audit-gradle",
Aliases: []string{"ag"},
Flags: flags.GetCommandFlags(flags.AuditGradle),
Description: auditSpecificDocs.GetGradleDescription(),
Action: func(c *components.Context) error {
return AuditSpecificCmd(c, techutils.Gradle)
},
Hidden: true,
},
{
Name: "audit-npm",
Aliases: []string{"an"},
Flags: flags.GetCommandFlags(flags.AuditNpm),
Description: auditSpecificDocs.GetNpmDescription(),
Action: func(c *components.Context) error {
return AuditSpecificCmd(c, techutils.Npm)
},
Hidden: true,
},
{
Name: "audit-go",
Aliases: []string{"ago"},
Flags: flags.GetCommandFlags(flags.AuditGo),
Description: auditSpecificDocs.GetGoDescription(),
Action: func(c *components.Context) error {
return AuditSpecificCmd(c, techutils.Go)
},
Hidden: true,
},
{
Name: "audit-pip",
Aliases: []string{"ap"},
Flags: flags.GetCommandFlags(flags.AuditPip),
Description: auditSpecificDocs.GetPipDescription(),
Action: func(c *components.Context) error {
return AuditSpecificCmd(c, techutils.Pip)
},
Hidden: true,
},
{
Name: "audit-pipenv",
Aliases: []string{"ape"},
Flags: flags.GetCommandFlags(flags.AuditPipenv),
Description: auditSpecificDocs.GetPipenvDescription(),
Action: func(c *components.Context) error {
return AuditSpecificCmd(c, techutils.Pipenv)
},
Hidden: true,
},
}
}
func EnrichCmd(c *components.Context) error {
if len(c.Arguments) == 0 {
return pluginsCommon.PrintHelpAndReturnError("providing a file path argument is mandatory", c)
}
serverDetails, err := createServerDetailsWithConfigOffer(c)
if err != nil {
return err
}
if err = validateConnectionAndViolationContextInputs(c, serverDetails); err != nil {
return err
}
specFile := createDefaultScanSpec(c, addTrailingSlashToRepoPathIfNeeded(c))
if err = spec.ValidateSpec(specFile.Files, false, false); err != nil {
return err
}
threads, err := pluginsCommon.GetThreadsCount(c)
if err != nil {
return err
}
EnrichCmd := enrich.NewEnrichCommand().
SetServerDetails(serverDetails).
SetThreads(threads).
SetSpec(specFile)
return commandsCommon.Exec(EnrichCmd)
}
func ScanCmd(c *components.Context) error {
if len(c.Arguments) == 0 && !c.IsFlagSet(flags.SpecFlag) {
return pluginsCommon.PrintHelpAndReturnError("providing either a <source pattern> argument or the 'spec' option is mandatory", c)
}
serverDetails, err := createServerDetailsWithConfigOffer(c)
if err != nil {
return err
}
if err = validateConnectionAndViolationContextInputs(c, serverDetails); err != nil {
return err
}
xrayVersion, xscVersion, err := xsc.GetJfrogServicesVersion(serverDetails)
if err != nil {
return err
}
var specFile *spec.SpecFiles
repoPath := ""
if c.IsFlagSet(flags.SpecFlag) && len(c.GetStringFlagValue(flags.SpecFlag)) > 0 {
specFile, err = pluginsCommon.GetFileSystemSpec(c)
if err != nil {
return err
}
} else {
repoPath = addTrailingSlashToRepoPathIfNeeded(c)
specFile = createDefaultScanSpec(c, repoPath)
}
err = spec.ValidateSpec(specFile.Files, false, false)
if err != nil {
return err
}
threads, err := pluginsCommon.GetThreadsCount(c)
if err != nil {
return err
}
format, err := outputFormat.GetOutputFormat(c.GetStringFlagValue(flags.OutputFormat))
if err != nil {
return err
}
if c.GetBoolFlagValue(flags.Sbom) && format != outputFormat.Table {
log.Warn("The '--sbom' flag is only supported with the 'table' output format. Ignoring the flag.")
}
pluginsCommon.FixWinPathsForFileSystemSourcedCmds(specFile, c)
minSeverity, err := getMinimumSeverity(c)
if err != nil {
return err
}
scanCmd := scan.NewScanCommand().
SetXrayVersion(xrayVersion).
SetXscVersion(xscVersion).
SetServerDetails(serverDetails).
SetThreads(threads).
SetSpec(specFile).
SetOutputFormat(format).
SetProject(getProject(c)).
SetBaseRepoPath(repoPath).
SetIncludeVulnerabilities(c.GetBoolFlagValue(flags.Vuln) || shouldIncludeVulnerabilities(c)).
SetIncludeLicenses(c.GetBoolFlagValue(flags.Licenses)).
SetIncludeSbom(c.GetBoolFlagValue(flags.Sbom)).
SetFail(c.GetBoolFlagValue(flags.Fail)).
SetPrintExtendedTable(c.GetBoolFlagValue(flags.ExtendedTable)).
SetBypassArchiveLimits(c.GetBoolFlagValue(flags.BypassArchiveLimits)).
SetFixableOnly(c.GetBoolFlagValue(flags.FixableOnly)).
SetMinSeverityFilter(minSeverity)
if c.IsFlagSet(flags.Watches) {
scanCmd.SetWatches(splitByCommaAndTrim(c.GetStringFlagValue(flags.Watches)))
}
return commandsCommon.Exec(scanCmd)
}
func getMinimumSeverity(c *components.Context) (severity severityutils.Severity, err error) {
flagSeverity := c.GetStringFlagValue(flags.MinSeverity)
if flagSeverity == "" {
return
}
severity, err = severityutils.ParseSeverity(flagSeverity, false)
if err != nil {
return
}
return
}
func addTrailingSlashToRepoPathIfNeeded(c *components.Context) string {
repoPath := c.GetStringFlagValue(flags.RepoPath)
if repoPath != "" && !strings.Contains(repoPath, "/") {
// In case only repo name was provided (no path) we are adding a trailing slash.
repoPath += "/"
}
return repoPath
}
func createDefaultScanSpec(c *components.Context, defaultTarget string) *spec.SpecFiles {
return spec.NewBuilder().
Pattern(c.Arguments[0]).
Target(defaultTarget).
Recursive(c.GetBoolFlagValue(flags.Recursive)).
Exclusions(pluginsCommon.GetStringsArrFlagValue(c, flags.Exclusions)).
Regexp(c.GetBoolFlagValue(flags.RegexpFlag)).
Ant(c.GetBoolFlagValue(flags.AntFlag)).
IncludeDirs(c.GetBoolFlagValue(flags.IncludeDirs)).
BuildSpec()
}
func shouldIncludeVulnerabilities(c *components.Context) bool {
// If no context was provided by the user, no Violations will be triggered by Xray, so include general vulnerabilities in the command output
return c.GetStringFlagValue(flags.Watches) == "" && !isProjectProvided(c) && c.GetStringFlagValue(flags.RepoPath) == ""
}
// Scan published builds with Xray
func BuildScan(c *components.Context) error {
if len(c.Arguments) > 2 {
return pluginsCommon.WrongNumberOfArgumentsHandler(c)
}
buildConfiguration := pluginsCommon.CreateBuildConfiguration(c)
if err := buildConfiguration.ValidateBuildParams(); err != nil {
return err
}
serverDetails, err := createServerDetailsWithConfigOffer(c)
if err != nil {
return err
}
if err = validateConnectionAndViolationContextInputs(c, serverDetails); err != nil {
return err
}
format, err := outputFormat.GetOutputFormat(c.GetStringFlagValue(flags.OutputFormat))
if err != nil {
return err
}
buildScanCmd := scan.NewBuildScanCommand().
SetServerDetails(serverDetails).
SetFailBuild(c.GetBoolFlagValue(flags.Fail)).
SetBuildConfiguration(buildConfiguration).
SetOutputFormat(format).
SetPrintExtendedTable(c.GetBoolFlagValue(flags.ExtendedTable)).
SetRescan(c.GetBoolFlagValue(flags.Rescan))
if format != outputFormat.Sarif {
// Sarif shouldn't include the additional all-vulnerabilities info that received by adding the vuln flag
buildScanCmd.SetIncludeVulnerabilities(c.GetBoolFlagValue(flags.Vuln))
}
return commandsCommon.Exec(buildScanCmd)
}
func AuditCmd(c *components.Context) error {
xrayVersion, xscVersion, serverDetails, auditCmd, err := CreateAuditCmd(c)
if err != nil {
return err
}
// Check if user used specific technologies flags
allTechnologies := techutils.GetAllTechnologiesList()
technologies := []string{}
for _, tech := range allTechnologies {
var techExists bool
if tech == techutils.Maven {
// On Maven we use '--mvn' flag
techExists = c.GetBoolFlagValue(flags.Mvn)
} else {
techExists = c.GetBoolFlagValue(tech.String())
}
if techExists {
technologies = append(technologies, tech.String())
}
}
auditCmd.SetTechnologies(technologies)
if c.GetBoolFlagValue(flags.WithoutCA) && !c.GetBoolFlagValue(flags.Sca) {
// No CA flag provided but sca flag is not provided, error
return pluginsCommon.PrintHelpAndReturnError(fmt.Sprintf("flag '--%s' cannot be used without '--%s'", flags.WithoutCA, flags.Sca), c)
}
if c.GetBoolFlagValue(flags.SecretValidation) && !c.GetBoolFlagValue(flags.Secrets) {
// No secrets flag but secret validation is provided, error
return pluginsCommon.PrintHelpAndReturnError(fmt.Sprintf("flag '--%s' cannot be used without '--%s'", flags.SecretValidation, flags.Secrets), c)
}
if subScans, err := getSubScansToPreform(c); err != nil {
return err
} else if len(subScans) > 0 {
auditCmd.SetScansToPerform(subScans)
}
threads, err := pluginsCommon.GetThreadsCount(c)
if err != nil {
return err
}
auditCmd.SetThreads(threads)
// Reporting error if Xsc service is enabled
return reportErrorIfExists(xrayVersion, xscVersion, serverDetails, progressbar.ExecWithProgress(auditCmd))
}
func CreateAuditCmd(c *components.Context) (string, string, *coreConfig.ServerDetails, *audit.AuditCommand, error) {
auditCmd := audit.NewGenericAuditCommand()
serverDetails, err := createServerDetailsWithConfigOffer(c)
if err != nil {
return "", "", nil, nil, err
}
if err = validateConnectionAndViolationContextInputs(c, serverDetails); err != nil {
return "", "", nil, nil, err
}
xrayVersion, xscVersion, err := xsc.GetJfrogServicesVersion(serverDetails)
if err != nil {
return "", "", nil, nil, err
}
format, err := outputFormat.GetOutputFormat(c.GetStringFlagValue(flags.OutputFormat))
if err != nil {
return "", "", nil, nil, err
}
if c.GetBoolFlagValue(flags.Sbom) && format != outputFormat.Table {
log.Warn("The '--sbom' flag is only supported with the 'table' output format. Ignoring the flag.")
}
minSeverity, err := getMinimumSeverity(c)
if err != nil {
return "", "", nil, nil, err
}
scansOutputDir, err := getAndValidateOutputDirExistsIfProvided(c)
if err != nil {
return "", "", nil, nil, err
}
auditCmd.SetTargetRepoPath(addTrailingSlashToRepoPathIfNeeded(c)).
SetProject(getProject(c)).
SetIncludeVulnerabilities(c.GetBoolFlagValue(flags.Vuln)).
SetIncludeLicenses(c.GetBoolFlagValue(flags.Licenses)).
SetIncludeSbom(c.GetBoolFlagValue(flags.Sbom)).
SetFail(c.GetBoolFlagValue(flags.Fail)).
SetPrintExtendedTable(c.GetBoolFlagValue(flags.ExtendedTable)).
SetMinSeverityFilter(minSeverity).
SetFixableOnly(c.GetBoolFlagValue(flags.FixableOnly)).
SetThirdPartyApplicabilityScan(c.GetBoolFlagValue(flags.ThirdPartyContextualAnalysis)).
SetScansResultsOutputDir(scansOutputDir).
SetSkipAutoInstall(c.GetBoolFlagValue(flags.SkipAutoInstall)).
SetAllowPartialResults(c.GetBoolFlagValue(flags.AllowPartialResults))
if c.GetStringFlagValue(flags.Watches) != "" {
auditCmd.SetWatches(splitByCommaAndTrim(c.GetStringFlagValue(flags.Watches)))
}
if c.GetStringFlagValue(flags.WorkingDirs) != "" {
auditCmd.SetWorkingDirs(splitByCommaAndTrim(c.GetStringFlagValue(flags.WorkingDirs)))
}
auditCmd.SetServerDetails(serverDetails).
SetXrayVersion(xrayVersion).
SetXscVersion(xscVersion).
SetExcludeTestDependencies(c.GetBoolFlagValue(flags.ExcludeTestDeps)).
SetOutputFormat(format).
SetUseJas(true).
SetUseWrapper(c.GetBoolFlagValue(flags.UseWrapper)).
SetInsecureTls(c.GetBoolFlagValue(flags.InsecureTls)).
SetNpmScope(c.GetStringFlagValue(flags.DepType)).
SetPipRequirementsFile(c.GetStringFlagValue(flags.RequirementsFile)).
SetMaxTreeDepth(c.GetStringFlagValue(flags.MaxTreeDepth)).
SetExclusions(pluginsCommon.GetStringsArrFlagValue(c, flags.Exclusions))
return xrayVersion, xscVersion, serverDetails, auditCmd, err
}
func logNonGenericAuditCommandDeprecation(cmdName string) {
if cliutils.ShouldLogWarning() {
log.Warn(
`You are using a deprecated syntax of the command.
Instead of:
$ ` + coreutils.GetCliExecutableName() + ` ` + cmdName + ` ...
Use:
$ ` + coreutils.GetCliExecutableName() + ` audit ...`)
}
}
func AuditSpecificCmd(c *components.Context, technology techutils.Technology) error {
logNonGenericAuditCommandDeprecation(c.CommandName)
xrayVersion, xscVersion, serverDetails, auditCmd, err := CreateAuditCmd(c)
if err != nil {
return err
}
technologies := []string{string(technology)}
auditCmd.SetTechnologies(technologies)
// Reporting error if Xsc service is enabled
return reportErrorIfExists(xrayVersion, xscVersion, serverDetails, progressbar.ExecWithProgress(auditCmd))
}
func CurationCmd(c *components.Context) error {
curationAuditCommand, err := getCurationCommand(c)
if err != nil {
return err
}
return progressbar.ExecWithProgress(curationAuditCommand)
}
var supportedCommandsForPostInstallationFailure = datastructures.MakeSetFromElements[string](
"install", "build", "i", "add", "ci", "get", "mod",
)
func IsSupportedCommandForCurationInspect(cmd string) bool {
return supportedCommandsForPostInstallationFailure.Exists(cmd)
}
func WrapCmdWithCurationPostFailureRun(c *cli.Context, cmd func(c *cli.Context) error, technology techutils.Technology, cmdName string) error {
if err := cmd(c); err != nil {
CurationInspectAfterFailure(c, cmdName, technology, err)
return err
}
return nil
}
func CurationInspectAfterFailure(c *cli.Context, cmdName string, technology techutils.Technology, errFromCmd error) {
if compContexts, errConvertCtx := components.ConvertContext(c); errConvertCtx == nil {
if errPostCuration := CurationCmdPostInstallationFailure(compContexts, technology, cmdName, errFromCmd); errPostCuration != nil {
log.Error(errPostCuration)
}
} else {
log.Error(errConvertCtx)
}
}
func CurationCmdPostInstallationFailure(c *components.Context, tech techutils.Technology, cmdName string, originError error) error {
// check the command supported
curationAuditCommand, err, runCuration := ShouldRunCurationAfterFailure(c, tech, cmdName, originError)
if err != nil {
return err
}
if !runCuration {
return nil
}
log.Info("Running curation audit after failure")
return progressbar.ExecWithProgress(curationAuditCommand)
}
func ShouldRunCurationAfterFailure(c *components.Context, tech techutils.Technology, cmdName string, originError error) (curationCmd *curation.CurationAuditCommand, err error, runCuration bool) {
if !IsSupportedCommandForCurationInspect(cmdName) {
return
}
if os.Getenv(coreutils.SummaryOutputDirPathEnv) == "" ||
os.Getenv(SkipCurationAfterFailureEnv) == "true" {
return
}
// check if the error is a forbidden error, if so, we don't want to run the curation audit automatically.
// this check have two parts:
// 1. check if the error is a forbidden error
// 2. check if the error message contains the forbidden error message, in case the output included in the error message.
forBiddenError := &buildInfoUtils.ForbiddenError{}
if !errors.Is(originError, forBiddenError) && !strings.Contains(originError.Error(), forBiddenError.Error()) &&
!buildInfoUtils.IsForbiddenOutput(buildInfoUtils.PackageManager(tech.String()), originError.Error()) {
return
}
// If the command is not running in the context of GitHub actions, we don't want to run the curation audit automatically
curationCmd, err = getCurationCommand(c)
if err != nil {
return
}
// check if user entitled for curation
serverDetails, err := curationCmd.GetAuth(tech)
if err != nil {
return
}
xrayManager, err := xray.CreateXrayServiceManager(serverDetails)
if err != nil {
return
}
entitled, err := curation.IsEntitledForCuration(xrayManager)
if err != nil {
return
}
if !entitled {
log.Info("Curation feature is not entitled, skipping curation audit")
return
}
return curationCmd, nil, true
}
func getCurationCommand(c *components.Context) (*curation.CurationAuditCommand, error) {
threads, err := pluginsCommon.GetThreadsCount(c)
if err != nil {
return nil, err
}
curationAuditCommand := curation.NewCurationAuditCommand().
SetWorkingDirs(splitByCommaAndTrim(c.GetStringFlagValue(flags.WorkingDirs))).
SetParallelRequests(threads)
serverDetails, err := pluginsCommon.CreateServerDetailsWithConfigOffer(c, true, cliutils.Rt)
if err != nil {
return nil, err
}
format, err := curation.GetCurationOutputFormat(c.GetStringFlagValue(flags.OutputFormat))
if err != nil {
return nil, err
}
curationAuditCommand.SetServerDetails(serverDetails).
SetIsCurationCmd(true).
SetExcludeTestDependencies(c.GetBoolFlagValue(flags.ExcludeTestDeps)).
SetOutputFormat(format).
SetUseWrapper(c.GetBoolFlagValue(flags.UseWrapper)).
SetInsecureTls(c.GetBoolFlagValue(flags.InsecureTls)).
SetNpmScope(c.GetStringFlagValue(flags.DepType)).
SetPipRequirementsFile(c.GetStringFlagValue(flags.RequirementsFile))
return curationAuditCommand, nil
}
func getAndValidateOutputDirExistsIfProvided(c *components.Context) (string, error) {
scansOutputDir := c.GetStringFlagValue(flags.OutputDir)
if scansOutputDir == "" {
return "", nil
}
exists, err := fileutils.IsDirExists(scansOutputDir, false)
if err != nil {
return "", err
}
if !exists {
return "", fmt.Errorf("output directory path for saving scans results was provided, but the directory doesn't exist: '%s'", scansOutputDir)
}
return scansOutputDir, nil
}
func DockerScanMockCommand() components.Command {
// Mock how the CLI handles docker commands:
// https://github.com/jfrog/jfrog-cli/blob/v2/buildtools/cli.go#L691
return components.Command{
Name: "docker",
Flags: flags.GetCommandFlags(flags.DockerScan),
Action: func(c *components.Context) error {
args := pluginsCommon.ExtractArguments(c)
var cmd, image string
// We may have prior flags before push/pull commands for the docker client.
for _, arg := range args {
if !strings.HasPrefix(arg, "-") {
if cmd == "" {
cmd = arg
} else {
image = arg
break
}
}
}
if cmd != "scan" {
return fmt.Errorf("unsupported command: %s", cmd)
}
return DockerScan(c, image)
},
}
}
func DockerScan(c *components.Context, image string) error {
// Since this command is not registered normally, we need to handle printing 'help' here by ourselves.
c.CommandName = dockerScanCmdHiddenName
printHelp := pluginsCommon.GetPrintCurrentCmdHelp(c)
if show, err := cliutils.ShowGenericCmdHelpIfNeeded(c.Arguments, printHelp); show || err != nil {
return err
}
if image == "" {
return printHelp()
}
// Run the command
threads, err := pluginsCommon.GetThreadsCount(c)
if err != nil {
return err
}
serverDetails, err := createServerDetailsWithConfigOffer(c)
if err != nil {
return err
}
if err = validateConnectionAndViolationContextInputs(c, serverDetails); err != nil {
return err
}
xrayVersion, xscVersion, err := xsc.GetJfrogServicesVersion(serverDetails)
if err != nil {
return err
}
containerScanCommand := scan.NewDockerScanCommand()
format, err := outputFormat.GetOutputFormat(c.GetStringFlagValue(flags.OutputFormat))
if err != nil {
return err
}
if c.GetBoolFlagValue(flags.Sbom) && format != outputFormat.Table {
log.Warn("The '--sbom' flag is only supported with the 'table' output format. Ignoring the flag.")
}
minSeverity, err := getMinimumSeverity(c)
if err != nil {
return err
}
containerScanCommand.SetImageTag(image).
SetServerDetails(serverDetails).
SetXrayVersion(xrayVersion).
SetXscVersion(xscVersion).
SetOutputFormat(format).
SetProject(getProject(c)).
SetBaseRepoPath(addTrailingSlashToRepoPathIfNeeded(c)).
SetIncludeVulnerabilities(c.GetBoolFlagValue(flags.Vuln) || shouldIncludeVulnerabilities(c)).
SetIncludeLicenses(c.GetBoolFlagValue(flags.Licenses)).
SetIncludeSbom(c.GetBoolFlagValue(flags.Sbom)).
SetFail(c.GetBoolFlagValue(flags.Fail)).
SetPrintExtendedTable(c.GetBoolFlagValue(flags.ExtendedTable)).
SetBypassArchiveLimits(c.GetBoolFlagValue(flags.BypassArchiveLimits)).
SetFixableOnly(c.GetBoolFlagValue(flags.FixableOnly)).
SetMinSeverityFilter(minSeverity).
SetThreads(threads).
SetSecretValidation(c.GetBoolFlagValue(flags.SecretValidation))
if c.GetStringFlagValue(flags.Watches) != "" {
containerScanCommand.SetWatches(splitByCommaAndTrim(c.GetStringFlagValue(flags.Watches)))
}
return progressbar.ExecWithProgress(containerScanCommand)
}