-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docker scan - map vulnerabilities to Dockerfile commands #975
Merged
Merged
Changes from all commits
Commits
Show all changes
68 commits
Select commit
Hold shift + click to select a range
32c823a
Refactor scan to allow the reuse of docker scan
EyalDelarea da9bd27
stash
EyalDelarea 1a617cf
pull dev
EyalDelarea 5e891d7
stash
EyalDelarea 0011ffa
Init refactor, change calls with crashing tests
EyalDelarea 947f07b
pull dev
EyalDelarea 59a2513
pull dev
EyalDelarea a3152b8
fix static analysis and change order
EyalDelarea ffa9481
refactor
EyalDelarea 6f4ab1a
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into re…
EyalDelarea 08373b4
pull refactor table branch
EyalDelarea 4c57a72
add new table
EyalDelarea db07a5f
add set scan type
EyalDelarea 3fa6ee4
pull refactor
EyalDelarea 1d5cf13
customize docker table output
EyalDelarea 4d4fbf8
pull dev
EyalDelarea 3edbbe0
fix static check
EyalDelarea 0ae9afa
fix static check
EyalDelarea 343398b
pull dev
EyalDelarea eb06af3
replace to dev
EyalDelarea 87a30b3
remove predefined binary scan
EyalDelarea 38881b1
pull refactor branch
EyalDelarea f632481
change name
EyalDelarea 8575602
stash progress
EyalDelarea 455055d
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into re…
EyalDelarea 113d9e6
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into im…
EyalDelarea 7537a30
map commands to line number
EyalDelarea 391f666
handle more than one FROM command
EyalDelarea 6ba2220
stash docker progress
EyalDelarea 32f61cd
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into re…
EyalDelarea f33c559
pull dev
EyalDelarea 54da48c
Merge dev
EyalDelarea e03fbf6
Fix static check
EyalDelarea b316e92
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into re…
EyalDelarea 4f26435
pull refactor table
EyalDelarea fc61273
Add test
EyalDelarea 1b30588
change replacce
EyalDelarea 16e7d30
fix static check
EyalDelarea 0d61c59
remove unneeded field
EyalDelarea 3758764
pull dev
EyalDelarea 3c278e9
line numbers in table are optional
EyalDelarea 30c51f9
set dockerfile scanned bool
EyalDelarea 3d065bb
Don't show extended CVEs on dockerscan
EyalDelarea 8f2091f
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into im…
EyalDelarea 250c14c
pull dev
EyalDelarea ed804f6
Add error info to docker scan
EyalDelarea 08562fc
renames
EyalDelarea bc53591
remove dockerfile scan and scan only images
EyalDelarea daec5d9
remove dockerfile scanning code
EyalDelarea 3df21ac
Remove unused code
EyalDelarea a412623
Add some comments
EyalDelarea 93617a4
Change table order and remove duplicate print
EyalDelarea 29b22ca
CR
EyalDelarea 4be8aff
pull dev
EyalDelarea adb401d
pull dev
EyalDelarea 7b29288
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into do…
EyalDelarea 5cd11bf
Add comments
EyalDelarea 7c82022
pull dev
EyalDelarea 64438c7
update client go
EyalDelarea ee53c82
cr
EyalDelarea 576e872
CR
EyalDelarea 1b73d3a
CR
EyalDelarea ee337f4
Fix rename and remove docker scan type
EyalDelarea fb7b69c
fix static check
EyalDelarea 777ec51
cleanUp NPE
EyalDelarea c0de205
Fix table order
EyalDelarea 2c76d3f
Fix test
EyalDelarea 2d05a5e
Change struct to map of strings
EyalDelarea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" | ||
"github.com/jfrog/jfrog-cli-core/v2/xray/scangraph" | ||
xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils" | ||
"os/exec" | ||
|
@@ -15,7 +16,6 @@ import ( | |
"github.com/jfrog/gofrog/parallel" | ||
"github.com/jfrog/jfrog-cli-core/v2/common/spec" | ||
"github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" | ||
"github.com/jfrog/jfrog-cli-core/v2/xray/formats" | ||
xrutils "github.com/jfrog/jfrog-cli-core/v2/xray/utils" | ||
"github.com/jfrog/jfrog-client-go/artifactory/services/fspatterns" | ||
|
@@ -158,56 +158,96 @@ func (scanCmd *ScanCommand) indexFile(filePath string) (*xrayUtils.BinaryGraphNo | |
} | ||
|
||
func (scanCmd *ScanCommand) Run() (err error) { | ||
defer func() { | ||
if err != nil { | ||
var e *exec.ExitError | ||
if errors.As(err, &e) { | ||
if e.ExitCode() != coreutils.ExitCodeVulnerableBuild.Code { | ||
err = errors.New("Scan command failed. " + err.Error()) | ||
} | ||
} | ||
} | ||
}() | ||
// Preform Binary scan | ||
extendedScanResults, cleanup, scanErrors, err := scanCmd.binaryScan() | ||
defer cleanup() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cleanup can be nil? |
||
if err != nil { | ||
return | ||
} | ||
// Print results | ||
if err = xrutils.NewResultsWriter(extendedScanResults). | ||
SetOutputFormat(scanCmd.outputFormat). | ||
SetIncludeVulnerabilities(scanCmd.includeVulnerabilities). | ||
SetIncludeLicenses(scanCmd.includeLicenses). | ||
SetPrintExtendedTable(scanCmd.printExtendedTable). | ||
SetIsMultipleRootProject(true). | ||
PrintScanResults(); err != nil { | ||
return | ||
} | ||
return scanCmd.handlePossibleErrors(extendedScanResults.XrayResults, scanErrors, err) | ||
} | ||
|
||
// Validate Xray version, download indexer if needed and prepare temp folders | ||
func (scanCmd *ScanCommand) prepareScanCommand() (xrayVersion string, threads int, cleanup func(), err error) { | ||
xrayManager, xrayVersion, err := xrutils.CreateXrayServiceManagerAndGetVersion(scanCmd.serverDetails) | ||
if err != nil { | ||
return err | ||
return | ||
} | ||
|
||
// Validate Xray minimum version for graph scan command | ||
err = clientutils.ValidateMinimumVersion(clientutils.Xray, xrayVersion, scangraph.GraphScanMinXrayVersion) | ||
if err != nil { | ||
return err | ||
return | ||
} | ||
|
||
if scanCmd.bypassArchiveLimits { | ||
// Validate Xray minimum version for BypassArchiveLimits flag for indexer | ||
err = clientutils.ValidateMinimumVersion(clientutils.Xray, xrayVersion, BypassArchiveLimitsMinXrayVersion) | ||
if err != nil { | ||
return err | ||
return | ||
} | ||
} | ||
log.Info("JFrog Xray version is:", xrayVersion) | ||
// First download Xray Indexer if needed | ||
scanCmd.indexerPath, err = DownloadIndexerIfNeeded(xrayManager, xrayVersion) | ||
if err != nil { | ||
return err | ||
return | ||
} | ||
// Create Temp dir for Xray Indexer | ||
scanCmd.indexerTempDir, err = fileutils.CreateTempDir() | ||
if err != nil { | ||
return err | ||
return | ||
} | ||
defer func() { | ||
e := fileutils.RemoveTempDir(scanCmd.indexerTempDir) | ||
if err == nil { | ||
err = e | ||
} | ||
}() | ||
threads := 1 | ||
cleanup = func() { | ||
err = errors.Join(err, fileutils.RemoveTempDir(scanCmd.indexerTempDir)) | ||
} | ||
threads = 1 | ||
if scanCmd.threads > 1 { | ||
threads = scanCmd.threads | ||
} | ||
return | ||
} | ||
|
||
func (scanCmd *ScanCommand) handlePossibleErrors(flatResults []services.ScanResponse, scanErrors []formats.SimpleJsonError, err error) error { | ||
// If includeVulnerabilities is false it means that context was provided, so we need to check for build violations. | ||
// If user provided --fail=false, don't fail the build. | ||
if scanCmd.fail && !scanCmd.includeVulnerabilities { | ||
if xrutils.CheckIfFailBuild(flatResults) { | ||
return xrutils.NewFailBuildError() | ||
} | ||
} | ||
if len(scanErrors) > 0 { | ||
return errorutils.CheckErrorf(scanErrors[0].ErrorMessage) | ||
} | ||
|
||
if err != nil { | ||
var e *exec.ExitError | ||
if errors.As(err, &e) { | ||
if e.ExitCode() != coreutils.ExitCodeVulnerableBuild.Code { | ||
err = errors.New("Scan command failed. " + err.Error()) | ||
} | ||
} | ||
} | ||
|
||
log.Info("Scan completed successfully.") | ||
return err | ||
} | ||
|
||
func (scanCmd *ScanCommand) binaryScan() (extendedScanResults *xrutils.ExtendedScanResults, cleanup func(), scanErrors []formats.SimpleJsonError, err error) { | ||
xrayVersion, threads, cleanup, err := scanCmd.prepareScanCommand() | ||
if err != nil { | ||
return | ||
} | ||
// resultsArr is a two-dimensional array. Each array in it contains a list of ScanResponses that were requested and collected by a specific thread. | ||
resultsArr := make([][]*services.ScanResponse, threads) | ||
fileProducerConsumer := parallel.NewRunner(scanCmd.threads, 20000, false) | ||
|
@@ -229,46 +269,32 @@ func (scanCmd *ScanCommand) Run() (err error) { | |
} | ||
if scanCmd.progress != nil { | ||
if err = scanCmd.progress.Quit(); err != nil { | ||
return err | ||
return | ||
} | ||
|
||
} | ||
|
||
fileCollectingErr := fileCollectingErrorsQueue.GetError() | ||
var scanErrors []formats.SimpleJsonError | ||
if fileCollectingErr != nil { | ||
scanErrors = append(scanErrors, formats.SimpleJsonError{ErrorMessage: fileCollectingErr.Error()}) | ||
} | ||
scanErrors = appendErrorSlice(scanErrors, fileProducerErrors) | ||
scanErrors = appendErrorSlice(scanErrors, indexedFileProducerErrors) | ||
extendedScanResults := &xrutils.ExtendedScanResults{XrayResults: flatResults} | ||
|
||
if err = xrutils.NewResultsWriter(extendedScanResults). | ||
SetOutputFormat(scanCmd.outputFormat). | ||
SetIncludeVulnerabilities(scanCmd.includeVulnerabilities). | ||
SetIncludeLicenses(scanCmd.includeLicenses). | ||
SetPrintExtendedTable(scanCmd.printExtendedTable). | ||
SetIsMultipleRootProject(true). | ||
SetScanType(services.Binary). | ||
PrintScanResults(); err != nil { | ||
return | ||
} | ||
extendedScanResults = &xrutils.ExtendedScanResults{XrayResults: flatResults} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
// If includeVulnerabilities is false it means that context was provided, so we need to check for build violations. | ||
// If user provided --fail=false, don't fail the build. | ||
if scanCmd.fail && !scanCmd.includeVulnerabilities { | ||
if xrutils.CheckIfFailBuild(flatResults) { | ||
return xrutils.NewFailBuildError() | ||
err = xrutils.NewFailBuildError() | ||
return | ||
} | ||
} | ||
if len(scanErrors) > 0 { | ||
return errorutils.CheckErrorf(scanErrors[0].ErrorMessage) | ||
err = errorutils.CheckErrorf(scanErrors[0].ErrorMessage) | ||
return | ||
} | ||
log.Info("Scan completed successfully.") | ||
return nil | ||
return | ||
} | ||
|
||
func NewScanCommand() *ScanCommand { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored binary scan to allow to reuse it in the docker scan, as docker scan is basicliy a binary scan with other flow.