From 72af406725363ef31dda14e74aa3187c346760c3 Mon Sep 17 00:00:00 2001 From: Guy Sheffer Date: Mon, 4 Mar 2024 14:57:25 +0200 Subject: [PATCH] Run contextual analysis and secret detection in Docker scans #10 #4 --- commands/audit/audit.go | 22 +++---- commands/scan/scan.go | 62 +++++++++++++++++-- formats/conversion.go | 1 + formats/table.go | 3 +- .../applicability/applicabilitymanager.go | 55 +++++++++++++--- .../applicabilitymanager_test.go | 61 +++++++++++++----- {commands/audit/jas => jas}/common.go | 0 {commands/audit/jas => jas}/common_test.go | 0 {commands/audit/jas => jas}/commons_test.go | 0 {commands/audit/jas => jas}/iac/iacscanner.go | 2 +- .../audit/jas => jas}/iac/iacscanner_test.go | 2 +- {commands/audit => jas/runner}/jasrunner.go | 21 ++++--- .../audit => jas/runner}/jasrunner_test.go | 10 +-- .../audit/jas => jas}/sast/sastscanner.go | 2 +- .../jas => jas}/sast/sastscanner_test.go | 2 +- .../jas => jas}/secrets/secretsscanner.go | 21 ++++--- .../secrets/secretsscanner_test.go | 14 ++--- .../testdata/.jfrog/jfrog-apps-config.yml | 0 utils/jasutils.go | 16 +++++ 19 files changed, 215 insertions(+), 79 deletions(-) rename {commands/audit/jas => jas}/applicability/applicabilitymanager.go (74%) rename {commands/audit/jas => jas}/applicability/applicabilitymanager_test.go (81%) rename {commands/audit/jas => jas}/common.go (100%) rename {commands/audit/jas => jas}/common_test.go (100%) rename {commands/audit/jas => jas}/commons_test.go (100%) rename {commands/audit/jas => jas}/iac/iacscanner.go (98%) rename {commands/audit/jas => jas}/iac/iacscanner_test.go (97%) rename {commands/audit => jas/runner}/jasrunner.go (74%) rename {commands/audit => jas/runner}/jasrunner_test.go (86%) rename {commands/audit/jas => jas}/sast/sastscanner.go (98%) rename {commands/audit/jas => jas}/sast/sastscanner_test.go (99%) rename {commands/audit/jas => jas}/secrets/secretsscanner.go (82%) rename {commands/audit/jas => jas}/secrets/secretsscanner_test.go (89%) rename {commands/audit/jas => jas}/testdata/.jfrog/jfrog-apps-config.yml (100%) create mode 100644 utils/jasutils.go diff --git a/commands/audit/audit.go b/commands/audit/audit.go index 4d57bd26..9e8c3310 100644 --- a/commands/audit/audit.go +++ b/commands/audit/audit.go @@ -2,13 +2,14 @@ package audit import ( "errors" - "github.com/jfrog/jfrog-cli-security/scangraph" "os" + "github.com/jfrog/jfrog-cli-security/scangraph" + "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" - "github.com/jfrog/jfrog-cli-security/utils" + "github.com/jfrog/jfrog-cli-security/jas/applicability" + "github.com/jfrog/jfrog-cli-security/jas/runner" clientutils "github.com/jfrog/jfrog-client-go/utils" - "github.com/jfrog/jfrog-client-go/utils/log" "github.com/jfrog/jfrog-client-go/xray" "github.com/jfrog/jfrog-client-go/xray/services" "golang.org/x/sync/errgroup" @@ -160,7 +161,7 @@ func RunAudit(auditParams *AuditParams) (results *xrayutils.Results, err error) return } results.XrayVersion = auditParams.xrayVersion - results.ExtendedScanResults.EntitledForJas, err = isEntitledForJas(xrayManager, auditParams.xrayVersion) + results.ExtendedScanResults.EntitledForJas, err = xrayutils.IsEntitledForJas(xrayManager, auditParams.xrayVersion) if err != nil { return } @@ -168,7 +169,7 @@ func RunAudit(auditParams *AuditParams) (results *xrayutils.Results, err error) errGroup := new(errgroup.Group) if results.ExtendedScanResults.EntitledForJas { // Download (if needed) the analyzer manager in a background routine. - errGroup.Go(utils.DownloadAnalyzerManagerIfNeeded) + errGroup.Go(xrayutils.DownloadAnalyzerManagerIfNeeded) } if auditParams.xrayGraphScanParams.XscGitInfoContext != nil { @@ -188,16 +189,7 @@ func RunAudit(auditParams *AuditParams) (results *xrayutils.Results, err error) // Run scanners only if the user is entitled for Advanced Security if results.ExtendedScanResults.EntitledForJas { - results.JasError = runJasScannersAndSetResults(results, auditParams.DirectDependencies(), serverDetails, auditParams.workingDirs, auditParams.Progress(), auditParams.thirdPartyApplicabilityScan) - } - return -} - -func isEntitledForJas(xrayManager *xray.XrayServicesManager, xrayVersion string) (entitled bool, err error) { - if e := clientutils.ValidateMinimumVersion(clientutils.Xray, xrayVersion, xrayutils.EntitlementsMinVersion); e != nil { - log.Debug(e) - return + results.JasError = runner.RunJasScannersAndSetResults(results, auditParams.DirectDependencies(), serverDetails, auditParams.workingDirs, auditParams.Progress(), auditParams.thirdPartyApplicabilityScan, applicability.ApplicabilityScannerType) } - entitled, err = xrayManager.IsEntitled(xrayutils.ApplicabilityFeatureId) return } diff --git a/commands/scan/scan.go b/commands/scan/scan.go index b21e2ea5..9be3adce 100644 --- a/commands/scan/scan.go +++ b/commands/scan/scan.go @@ -10,12 +10,17 @@ import ( "regexp" "strings" + "golang.org/x/exp/maps" + "golang.org/x/exp/slices" + + "github.com/jfrog/jfrog-cli-security/jas/applicability" + "github.com/jfrog/jfrog-cli-security/jas/runner" "github.com/jfrog/jfrog-cli-security/scangraph" xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils" + "golang.org/x/sync/errgroup" "github.com/jfrog/gofrog/parallel" "github.com/jfrog/jfrog-cli-core/v2/common/format" - outputFormat "github.com/jfrog/jfrog-cli-core/v2/common/format" "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" @@ -47,7 +52,7 @@ type ScanCommand struct { // The location of the downloaded Xray indexer binary on the local file system. indexerPath string indexerTempDir string - outputFormat outputFormat.OutputFormat + outputFormat format.OutputFormat projectKey string minSeverityFilter string watches []string @@ -79,7 +84,7 @@ func (scanCmd *ScanCommand) SetThreads(threads int) *ScanCommand { return scanCmd } -func (scanCmd *ScanCommand) SetOutputFormat(format outputFormat.OutputFormat) *ScanCommand { +func (scanCmd *ScanCommand) SetOutputFormat(format format.OutputFormat) *ScanCommand { scanCmd.outputFormat = format return scanCmd } @@ -177,6 +182,16 @@ func (scanCmd *ScanCommand) Run() (err error) { return err } + scanResults := xrutils.NewAuditResults() + scanResults.XrayVersion = xrayVersion + + scanResults.ExtendedScanResults.EntitledForJas, err = xrutils.IsEntitledForJas(xrayManager, xrayVersion) + errGroup := new(errgroup.Group) + if scanResults.ExtendedScanResults.EntitledForJas { + // Download (if needed) the analyzer manager in a background routine. + errGroup.Go(xrutils.DownloadAnalyzerManagerIfNeeded) + } + // Validate Xray minimum version for graph scan command err = clientutils.ValidateMinimumVersion(clientutils.Xray, xrayVersion, scangraph.GraphScanMinXrayVersion) if err != nil { @@ -246,9 +261,22 @@ func (scanCmd *ScanCommand) Run() (err error) { scanErrors = appendErrorSlice(scanErrors, fileProducerErrors) scanErrors = appendErrorSlice(scanErrors, indexedFileProducerErrors) - scanResults := xrutils.NewAuditResults() - scanResults.XrayVersion = xrayVersion - scanResults.ScaResults = []xrutils.ScaScanResult{{XrayResults: flatResults}} + // Wait for the Download of the AnalyzerManager to complete. + if err = errGroup.Wait(); err != nil { + err = errors.New("failed while trying to get Analyzer Manager: " + err.Error()) + } + + if scanResults.ExtendedScanResults.EntitledForJas { + depsList := depsListFromVulnerabilities(flatResults) + + for _, scanResult := range flatResults { + scanResults.ScaResults = append(scanResults.ScaResults, xrutils.ScaScanResult{XrayResults: []services.ScanResponse{scanResult}, Technology: coreutils.Technology(scanResult.ScannedPackageType)}) + } + + workingDirs := []string{scanCmd.spec.Files[0].Pattern} + + scanResults.JasError = runner.RunJasScannersAndSetResults(scanResults, depsList, scanCmd.serverDetails, workingDirs, nil, false, applicability.ApplicabilityDockerScanScanType) + } if err = xrutils.NewResultsWriter(scanResults). SetOutputFormat(scanCmd.outputFormat). @@ -464,6 +492,28 @@ func appendErrorSlice(scanErrors []formats.SimpleJsonError, errorsToAdd [][]form return scanErrors } +func depsListFromVulnerabilities(flatResults []services.ScanResponse) []string { + var depsList []string + var technologiesList []coreutils.Technology + for _, result := range flatResults { + for _, vulnerability := range result.Vulnerabilities { + dependencies := maps.Keys(vulnerability.Components) + for _, dependency := range dependencies { + if !slices.Contains(depsList, dependency) { + depsList = append(depsList, dependency) + } + } + + if !slices.Contains(technologiesList, coreutils.Technology(vulnerability.Technology)) && (vulnerability.Technology != "") { + technologiesList = append(technologiesList, coreutils.Technology(vulnerability.Technology)) + } + + } + + } + return depsList +} + func ConditionalUploadDefaultScanFunc(serverDetails *config.ServerDetails, fileSpec *spec.SpecFiles, threads int, scanOutputFormat format.OutputFormat) error { return NewScanCommand().SetServerDetails(serverDetails).SetSpec(fileSpec).SetThreads(threads).SetOutputFormat(scanOutputFormat).SetFail(true).SetPrintExtendedTable(false).Run() } diff --git a/formats/conversion.go b/formats/conversion.go index 1a360ec1..8a602869 100644 --- a/formats/conversion.go +++ b/formats/conversion.go @@ -28,6 +28,7 @@ func ConvertToVulnerabilityScanTableRow(rows []VulnerabilityOrViolationRow) (tab tableRows = append(tableRows, vulnerabilityScanTableRow{ severity: rows[i].Severity, severityNumValue: rows[i].SeverityNumValue, + applicable: rows[i].Applicable, impactedPackageName: rows[i].ImpactedDependencyName, impactedPackageVersion: rows[i].ImpactedDependencyVersion, ImpactedPackageType: rows[i].ImpactedDependencyType, diff --git a/formats/table.go b/formats/table.go index 8dd71d1d..fc9486d9 100644 --- a/formats/table.go +++ b/formats/table.go @@ -20,7 +20,8 @@ type vulnerabilityTableRow struct { } type vulnerabilityScanTableRow struct { - severity string `col-name:"Severity"` + severity string `col-name:"Severity"` + applicable string `col-name:"Contextual\nAnalysis" omitempty:"true"` // For sorting severityNumValue int directPackages []directPackagesTableRow `embed-table:"true"` diff --git a/commands/audit/jas/applicability/applicabilitymanager.go b/jas/applicability/applicabilitymanager.go similarity index 74% rename from commands/audit/jas/applicability/applicabilitymanager.go rename to jas/applicability/applicabilitymanager.go index a727f1fd..e3b7a0d0 100644 --- a/commands/audit/jas/applicability/applicabilitymanager.go +++ b/jas/applicability/applicabilitymanager.go @@ -4,7 +4,7 @@ import ( "path/filepath" jfrogappsconfig "github.com/jfrog/jfrog-apps-config/go" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" + "github.com/jfrog/jfrog-cli-security/jas" "github.com/jfrog/gofrog/datastructures" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" @@ -17,11 +17,14 @@ import ( ) const ( - applicabilityScanType = "analyze-applicability" - applicabilityScanCommand = "ca" - applicabilityDocsUrlSuffix = "contextual-analysis" + ApplicabilityScannerType = "analyze-applicability" + applicabilityScanCommand = "ca" + applicabilityDocsUrlSuffix = "contextual-analysis" + ApplicabilityDockerScanScanType = "analyze-applicability-docker-scan" ) +type ApplicabilityScanType string + type ApplicabilityScanManager struct { applicabilityScanResults []*sarif.Run directDependenciesCves []string @@ -29,6 +32,7 @@ type ApplicabilityScanManager struct { xrayResults []services.ScanResponse scanner *jas.JasScanner thirdPartyScan bool + commandType string } // The getApplicabilityScanResults function runs the applicability scan flow, which includes the following steps: @@ -41,8 +45,8 @@ type ApplicabilityScanManager struct { // bool: true if the user is entitled to the applicability scan, false otherwise. // error: An error object (if any). func RunApplicabilityScan(xrayResults []services.ScanResponse, directDependencies []string, - scannedTechnologies []coreutils.Technology, scanner *jas.JasScanner, thirdPartyContextualAnalysis bool) (results []*sarif.Run, err error) { - applicabilityScanManager := newApplicabilityScanManager(xrayResults, directDependencies, scanner, thirdPartyContextualAnalysis) + scannedTechnologies []coreutils.Technology, scanner *jas.JasScanner, thirdPartyContextualAnalysis bool, scanType ApplicabilityScanType) (results []*sarif.Run, err error) { + applicabilityScanManager := newApplicabilityScanManager(xrayResults, directDependencies, scanner, thirdPartyContextualAnalysis, scanType) if !applicabilityScanManager.shouldRunApplicabilityScan(scannedTechnologies) { log.Debug("The technologies that have been scanned are currently not supported for contextual analysis scanning, or we couldn't find any vulnerable dependencies. Skipping....") return @@ -55,7 +59,38 @@ func RunApplicabilityScan(xrayResults []services.ScanResponse, directDependencie return } -func newApplicabilityScanManager(xrayScanResults []services.ScanResponse, directDependencies []string, scanner *jas.JasScanner, thirdPartyScan bool) (manager *ApplicabilityScanManager) { +// The getApplicabilityScanResults function runs the applicability scan flow, which includes the following steps: +// Creating an ApplicabilityScanManager object. +// Checking if the scanned project is eligible for applicability scan. +// Running the analyzer manager executable. +// Parsing the analyzer manager results. +// Return values: +// map[string]string: A map containing the applicability result of each XRAY CVE. +// bool: true if the user is entitled to the applicability scan, false otherwise. +// error: An error object (if any). +func RunApplicabilityWithScanCves(xrayResults []services.ScanResponse, cveList []string, + scannedTechnologies []coreutils.Technology, scanner *jas.JasScanner) (results []*sarif.Run, err error) { + applicabilityScanManager := newApplicabilityScanManagerCves(xrayResults, cveList, scanner) + if err = applicabilityScanManager.scanner.Run(applicabilityScanManager); err != nil { + err = utils.ParseAnalyzerManagerError(utils.Applicability, err) + return + } + results = applicabilityScanManager.applicabilityScanResults + return +} + +func newApplicabilityScanManagerCves(xrayScanResults []services.ScanResponse, cveList []string, scanner *jas.JasScanner) (manager *ApplicabilityScanManager) { + return &ApplicabilityScanManager{ + applicabilityScanResults: []*sarif.Run{}, + directDependenciesCves: cveList, + xrayResults: xrayScanResults, + scanner: scanner, + thirdPartyScan: false, + commandType: ApplicabilityDockerScanScanType, + } +} + +func newApplicabilityScanManager(xrayScanResults []services.ScanResponse, directDependencies []string, scanner *jas.JasScanner, thirdPartyScan bool, scanType ApplicabilityScanType) (manager *ApplicabilityScanManager) { directDependenciesCves, indirectDependenciesCves := extractDependenciesCvesFromScan(xrayScanResults, directDependencies) return &ApplicabilityScanManager{ applicabilityScanResults: []*sarif.Run{}, @@ -64,6 +99,7 @@ func newApplicabilityScanManager(xrayScanResults []services.ScanResponse, direct xrayResults: xrayScanResults, scanner: scanner, thirdPartyScan: thirdPartyScan, + commandType: string(scanType), } } @@ -133,7 +169,7 @@ func (asm *ApplicabilityScanManager) Run(module jfrogappsconfig.Module) (err err } func (asm *ApplicabilityScanManager) shouldRunApplicabilityScan(technologies []coreutils.Technology) bool { - return asm.cvesExists() && coreutils.ContainsApplicabilityScannableTech(technologies) + return asm.cvesExists() && (coreutils.ContainsApplicabilityScannableTech(technologies) || technologies[0] == "oci") } func (asm *ApplicabilityScanManager) cvesExists() bool { @@ -152,6 +188,7 @@ type scanConfiguration struct { CveWhitelist []string `yaml:"cve-whitelist"` IndirectCveWhitelist []string `yaml:"indirect-cve-whitelist"` SkippedDirs []string `yaml:"skipped-folders"` + ScanType string `yaml:"scantype"` } func (asm *ApplicabilityScanManager) createConfigFile(module jfrogappsconfig.Module) error { @@ -169,7 +206,7 @@ func (asm *ApplicabilityScanManager) createConfigFile(module jfrogappsconfig.Mod { Roots: roots, Output: asm.scanner.ResultsFileName, - Type: applicabilityScanType, + Type: asm.commandType, GrepDisable: false, CveWhitelist: asm.directDependenciesCves, IndirectCveWhitelist: asm.indirectDependenciesCves, diff --git a/commands/audit/jas/applicability/applicabilitymanager_test.go b/jas/applicability/applicabilitymanager_test.go similarity index 81% rename from commands/audit/jas/applicability/applicabilitymanager_test.go rename to jas/applicability/applicabilitymanager_test.go index 672f8623..6c240641 100644 --- a/commands/audit/jas/applicability/applicabilitymanager_test.go +++ b/jas/applicability/applicabilitymanager_test.go @@ -7,19 +7,20 @@ import ( jfrogappsconfig "github.com/jfrog/jfrog-apps-config/go" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" + "github.com/jfrog/jfrog-cli-security/jas" "github.com/jfrog/jfrog-client-go/xray/services" "github.com/stretchr/testify/assert" ) var mockDirectDependencies = []string{"issueId_2_direct_dependency", "issueId_1_direct_dependency"} +var mockCves = []string{"CVE-00000", "CVE-000AA"} var mockMultiRootDirectDependencies = []string{"issueId_2_direct_dependency", "issueId_1_direct_dependency", "issueId_3_direct_dependency", "issueId_4_direct_dependency"} func TestNewApplicabilityScanManager_InputIsValid(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() // Act - applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockDirectDependencies, scanner, false) + applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockDirectDependencies, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) // Assert if assert.NotNil(t, applicabilityManager) { @@ -29,11 +30,26 @@ func TestNewApplicabilityScanManager_InputIsValid(t *testing.T) { } } +func TestNewApplicabilityScanManager_Docker_InputIsValid(t *testing.T) { + scanner, cleanUp := jas.InitJasTest(t) + defer cleanUp() + // Act + applicabilityManager := newApplicabilityScanManagerCves(jas.FakeBasicXrayResults, mockCves, scanner) + + // Assert + if assert.NotNil(t, applicabilityManager) { + assert.NotEmpty(t, applicabilityManager.scanner.ConfigFileName) + assert.NotEmpty(t, applicabilityManager.scanner.ResultsFileName) + print(applicabilityManager.directDependenciesCves) + assert.Len(t, applicabilityManager.directDependenciesCves, 2) + } +} + func TestNewApplicabilityScanManager_DependencyTreeDoesntExist(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() // Act - applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, nil, scanner, false) + applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, nil, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) // Assert if assert.NotNil(t, applicabilityManager) { @@ -70,10 +86,10 @@ func TestNewApplicabilityScanManager_NoDirectDependenciesInScan(t *testing.T) { // Act scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - applicabilityManager := newApplicabilityScanManager(noDirectDependenciesResults, mockDirectDependencies, scanner, false) + applicabilityManager := newApplicabilityScanManager(noDirectDependenciesResults, mockDirectDependencies, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) assertApplicabilityScanner(t, applicabilityManager) // ThirdPartyContextual shouldn't change anything here as this is not npm. - applicabilityManager = newApplicabilityScanManager(noDirectDependenciesResults, mockDirectDependencies, scanner, true) + applicabilityManager = newApplicabilityScanManager(noDirectDependenciesResults, mockDirectDependencies, scanner, true, ApplicabilityScanType(ApplicabilityScannerType)) assertApplicabilityScanner(t, applicabilityManager) } @@ -91,7 +107,7 @@ func TestNewApplicabilityScanManager_MultipleDependencyTrees(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() // Act - applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockMultiRootDirectDependencies, scanner, false) + applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockMultiRootDirectDependencies, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) // Assert if assert.NotNil(t, applicabilityManager) { @@ -117,7 +133,7 @@ func TestNewApplicabilityScanManager_ViolationsDontExistInResults(t *testing.T) defer cleanUp() // Act - applicabilityManager := newApplicabilityScanManager(noViolationScanResponse, mockDirectDependencies, scanner, false) + applicabilityManager := newApplicabilityScanManager(noViolationScanResponse, mockDirectDependencies, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) // Assert if assert.NotNil(t, applicabilityManager) { @@ -143,7 +159,7 @@ func TestNewApplicabilityScanManager_VulnerabilitiesDontExist(t *testing.T) { defer cleanUp() // Act - applicabilityManager := newApplicabilityScanManager(noVulnerabilitiesScanResponse, mockDirectDependencies, scanner, false) + applicabilityManager := newApplicabilityScanManager(noVulnerabilitiesScanResponse, mockDirectDependencies, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) // Assert if assert.NotNil(t, applicabilityManager) { @@ -157,7 +173,7 @@ func TestApplicabilityScanManager_ShouldRun_TechnologiesNotEligibleForScan(t *te scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - results, err := RunApplicabilityScan(jas.FakeBasicXrayResults, mockDirectDependencies, []coreutils.Technology{coreutils.Nuget, coreutils.Go}, scanner, false) + results, err := RunApplicabilityScan(jas.FakeBasicXrayResults, mockDirectDependencies, []coreutils.Technology{coreutils.Nuget, coreutils.Go}, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) // Assert assert.Nil(t, results) @@ -169,7 +185,7 @@ func TestApplicabilityScanManager_ShouldRun_ScanResultsAreEmpty(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - applicabilityManager := newApplicabilityScanManager(nil, mockDirectDependencies, scanner, false) + applicabilityManager := newApplicabilityScanManager(nil, mockDirectDependencies, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) // Assert eligible := applicabilityManager.shouldRunApplicabilityScan([]coreutils.Technology{coreutils.Nuget}) @@ -264,7 +280,7 @@ func TestCreateConfigFile_VerifyFileWasCreated(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, []string{"issueId_1_direct_dependency", "issueId_2_direct_dependency"}, scanner, false) + applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, []string{"issueId_1_direct_dependency", "issueId_2_direct_dependency"}, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) currWd, err := coreutils.GetWorkingDirectory() assert.NoError(t, err) @@ -288,7 +304,7 @@ func TestParseResults_EmptyResults_AllCvesShouldGetUnknown(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockDirectDependencies, scanner, false) + applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockDirectDependencies, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) applicabilityManager.scanner.ResultsFileName = filepath.Join(jas.GetTestDataPath(), "applicability-scan", "empty-results.sarif") // Act @@ -305,7 +321,24 @@ func TestParseResults_ApplicableCveExist(t *testing.T) { // Arrange scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockDirectDependencies, scanner, false) + applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockDirectDependencies, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) + applicabilityManager.scanner.ResultsFileName = filepath.Join(jas.GetTestDataPath(), "applicability-scan", "applicable-cve-results.sarif") + + // Act + var err error + applicabilityManager.applicabilityScanResults, err = jas.ReadJasScanRunsFromFile(applicabilityManager.scanner.ResultsFileName, scanner.JFrogAppsConfig.Modules[0].SourceRoot, applicabilityDocsUrlSuffix) + + if assert.NoError(t, err) && assert.NotNil(t, applicabilityManager.applicabilityScanResults) { + assert.Len(t, applicabilityManager.applicabilityScanResults, 1) + assert.NotEmpty(t, applicabilityManager.applicabilityScanResults[0].Results) + } +} + +func TestParseResults_DockerApplicableCveExist(t *testing.T) { + // Arrange + scanner, cleanUp := jas.InitJasTest(t) + defer cleanUp() + applicabilityManager := newApplicabilityScanManagerCves(jas.FakeBasicXrayResults, mockCves, scanner) applicabilityManager.scanner.ResultsFileName = filepath.Join(jas.GetTestDataPath(), "applicability-scan", "applicable-cve-results.sarif") // Act @@ -322,7 +355,7 @@ func TestParseResults_AllCvesNotApplicable(t *testing.T) { // Arrange scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockDirectDependencies, scanner, false) + applicabilityManager := newApplicabilityScanManager(jas.FakeBasicXrayResults, mockDirectDependencies, scanner, false, ApplicabilityScanType(ApplicabilityScannerType)) applicabilityManager.scanner.ResultsFileName = filepath.Join(jas.GetTestDataPath(), "applicability-scan", "no-applicable-cves-results.sarif") // Act diff --git a/commands/audit/jas/common.go b/jas/common.go similarity index 100% rename from commands/audit/jas/common.go rename to jas/common.go diff --git a/commands/audit/jas/common_test.go b/jas/common_test.go similarity index 100% rename from commands/audit/jas/common_test.go rename to jas/common_test.go diff --git a/commands/audit/jas/commons_test.go b/jas/commons_test.go similarity index 100% rename from commands/audit/jas/commons_test.go rename to jas/commons_test.go diff --git a/commands/audit/jas/iac/iacscanner.go b/jas/iac/iacscanner.go similarity index 98% rename from commands/audit/jas/iac/iacscanner.go rename to jas/iac/iacscanner.go index 312dcf4f..99b7eb01 100644 --- a/commands/audit/jas/iac/iacscanner.go +++ b/jas/iac/iacscanner.go @@ -4,7 +4,7 @@ import ( "path/filepath" jfrogappsconfig "github.com/jfrog/jfrog-apps-config/go" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" + "github.com/jfrog/jfrog-cli-security/jas" "github.com/jfrog/jfrog-cli-security/utils" "github.com/jfrog/jfrog-client-go/utils/log" diff --git a/commands/audit/jas/iac/iacscanner_test.go b/jas/iac/iacscanner_test.go similarity index 97% rename from commands/audit/jas/iac/iacscanner_test.go rename to jas/iac/iacscanner_test.go index 1a5403f4..5d7bab41 100644 --- a/commands/audit/jas/iac/iacscanner_test.go +++ b/jas/iac/iacscanner_test.go @@ -6,7 +6,7 @@ import ( "testing" jfrogappsconfig "github.com/jfrog/jfrog-apps-config/go" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" + "github.com/jfrog/jfrog-cli-security/jas" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" "github.com/stretchr/testify/assert" diff --git a/commands/audit/jasrunner.go b/jas/runner/jasrunner.go similarity index 74% rename from commands/audit/jasrunner.go rename to jas/runner/jasrunner.go index 434d9044..a6522c0a 100644 --- a/commands/audit/jasrunner.go +++ b/jas/runner/jasrunner.go @@ -1,20 +1,21 @@ -package audit +package runner import ( "errors" + "github.com/jfrog/jfrog-cli-core/v2/utils/config" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas/applicability" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas/iac" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas/sast" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas/secrets" + "github.com/jfrog/jfrog-cli-security/jas" + "github.com/jfrog/jfrog-cli-security/jas/applicability" + "github.com/jfrog/jfrog-cli-security/jas/iac" + "github.com/jfrog/jfrog-cli-security/jas/sast" + "github.com/jfrog/jfrog-cli-security/jas/secrets" "github.com/jfrog/jfrog-cli-security/utils" "github.com/jfrog/jfrog-client-go/utils/io" "github.com/jfrog/jfrog-client-go/utils/log" ) -func runJasScannersAndSetResults(scanResults *utils.Results, directDependencies []string, - serverDetails *config.ServerDetails, workingDirs []string, progress io.ProgressMgr, thirdPartyApplicabilityScan bool) (err error) { +func RunJasScannersAndSetResults(scanResults *utils.Results, directDependencies []string, + serverDetails *config.ServerDetails, workingDirs []string, progress io.ProgressMgr, thirdPartyApplicabilityScan bool, scanType applicability.ApplicabilityScanType) (err error) { if serverDetails == nil || len(serverDetails.Url) == 0 { log.Warn("To include 'Advanced Security' scan as part of the audit output, please run the 'jf c add' command before running this command.") return @@ -30,7 +31,7 @@ func runJasScannersAndSetResults(scanResults *utils.Results, directDependencies if progress != nil { progress.SetHeadlineMsg("Running applicability scanning") } - scanResults.ExtendedScanResults.ApplicabilityScanResults, err = applicability.RunApplicabilityScan(scanResults.GetScaScansXrayResults(), directDependencies, scanResults.GetScaScannedTechnologies(), scanner, thirdPartyApplicabilityScan) + scanResults.ExtendedScanResults.ApplicabilityScanResults, err = applicability.RunApplicabilityScan(scanResults.GetScaScansXrayResults(), directDependencies, scanResults.GetScaScannedTechnologies(), scanner, thirdPartyApplicabilityScan, scanType) if err != nil { return } @@ -41,7 +42,7 @@ func runJasScannersAndSetResults(scanResults *utils.Results, directDependencies if progress != nil { progress.SetHeadlineMsg("Running secrets scanning") } - scanResults.ExtendedScanResults.SecretsScanResults, err = secrets.RunSecretsScan(scanner) + scanResults.ExtendedScanResults.SecretsScanResults, err = secrets.RunSecretsScan(scanner, secrets.SecretsScannerType) if err != nil { return } diff --git a/commands/audit/jasrunner_test.go b/jas/runner/jasrunner_test.go similarity index 86% rename from commands/audit/jasrunner_test.go rename to jas/runner/jasrunner_test.go index 20b28c8a..d9be96d4 100644 --- a/commands/audit/jasrunner_test.go +++ b/jas/runner/jasrunner_test.go @@ -1,11 +1,11 @@ -package audit +package runner import ( "os" "testing" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" + "github.com/jfrog/jfrog-cli-security/jas" "github.com/jfrog/jfrog-cli-security/utils" "github.com/jfrog/jfrog-client-go/utils/io/fileutils" "github.com/stretchr/testify/assert" @@ -22,14 +22,14 @@ func TestGetExtendedScanResults_AnalyzerManagerDoesntExist(t *testing.T) { assert.NoError(t, os.Unsetenv(coreutils.HomeDir)) }() scanResults := &utils.Results{ScaResults: []utils.ScaScanResult{{Technology: coreutils.Yarn, XrayResults: jas.FakeBasicXrayResults}}, ExtendedScanResults: &utils.ExtendedScanResults{}} - err = runJasScannersAndSetResults(scanResults, []string{"issueId_1_direct_dependency", "issueId_2_direct_dependency"}, &jas.FakeServerDetails, nil, nil, false) + err = RunJasScannersAndSetResults(scanResults, []string{"issueId_1_direct_dependency", "issueId_2_direct_dependency"}, &jas.FakeServerDetails, nil, nil, false) // Expect error: assert.Error(t, err) } func TestGetExtendedScanResults_ServerNotValid(t *testing.T) { scanResults := &utils.Results{ScaResults: []utils.ScaScanResult{{Technology: coreutils.Pip, XrayResults: jas.FakeBasicXrayResults}}, ExtendedScanResults: &utils.ExtendedScanResults{}} - err := runJasScannersAndSetResults(scanResults, []string{"issueId_1_direct_dependency", "issueId_2_direct_dependency"}, nil, nil, nil, false) + err := RunJasScannersAndSetResults(scanResults, []string{"issueId_1_direct_dependency", "issueId_2_direct_dependency"}, nil, nil, nil, false) assert.NoError(t, err) } @@ -37,7 +37,7 @@ func TestGetExtendedScanResults_AnalyzerManagerReturnsError(t *testing.T) { assert.NoError(t, utils.DownloadAnalyzerManagerIfNeeded()) scanResults := &utils.Results{ScaResults: []utils.ScaScanResult{{Technology: coreutils.Yarn, XrayResults: jas.FakeBasicXrayResults}}, ExtendedScanResults: &utils.ExtendedScanResults{}} - err := runJasScannersAndSetResults(scanResults, []string{"issueId_2_direct_dependency", "issueId_1_direct_dependency"}, &jas.FakeServerDetails, nil, nil, false) + err := RunJasScannersAndSetResults(scanResults, []string{"issueId_2_direct_dependency", "issueId_1_direct_dependency"}, &jas.FakeServerDetails, nil, nil, false) // Expect error: assert.ErrorContains(t, err, "failed to run Applicability scan") diff --git a/commands/audit/jas/sast/sastscanner.go b/jas/sast/sastscanner.go similarity index 98% rename from commands/audit/jas/sast/sastscanner.go rename to jas/sast/sastscanner.go index f81d869a..4f2925fa 100644 --- a/commands/audit/jas/sast/sastscanner.go +++ b/jas/sast/sastscanner.go @@ -6,7 +6,7 @@ import ( "path/filepath" jfrogappsconfig "github.com/jfrog/jfrog-apps-config/go" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" + "github.com/jfrog/jfrog-cli-security/jas" "github.com/jfrog/jfrog-cli-security/utils" "github.com/jfrog/jfrog-client-go/utils/log" "github.com/owenrumney/go-sarif/v2/sarif" diff --git a/commands/audit/jas/sast/sastscanner_test.go b/jas/sast/sastscanner_test.go similarity index 99% rename from commands/audit/jas/sast/sastscanner_test.go rename to jas/sast/sastscanner_test.go index 5dcd2110..3a67ab15 100644 --- a/commands/audit/jas/sast/sastscanner_test.go +++ b/jas/sast/sastscanner_test.go @@ -4,7 +4,7 @@ import ( "path/filepath" "testing" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" + "github.com/jfrog/jfrog-cli-security/jas" "github.com/jfrog/jfrog-cli-security/utils" "github.com/owenrumney/go-sarif/v2/sarif" diff --git a/commands/audit/jas/secrets/secretsscanner.go b/jas/secrets/secretsscanner.go similarity index 82% rename from commands/audit/jas/secrets/secretsscanner.go rename to jas/secrets/secretsscanner.go index ca9d2ce7..246d1954 100644 --- a/commands/audit/jas/secrets/secretsscanner.go +++ b/jas/secrets/secretsscanner.go @@ -5,21 +5,25 @@ import ( "strings" jfrogappsconfig "github.com/jfrog/jfrog-apps-config/go" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" + "github.com/jfrog/jfrog-cli-security/jas" "github.com/jfrog/jfrog-cli-security/utils" "github.com/jfrog/jfrog-client-go/utils/log" "github.com/owenrumney/go-sarif/v2/sarif" ) const ( - secretsScanCommand = "sec" - secretsScannerType = "secrets-scan" - secretsDocsUrlSuffix = "secrets" + secretsScanCommand = "sec" + SecretsScannerType = "secrets-scan" // #nosec + SecretsScannerDockerScanType = "secrets-docker-scan" // #nosec + secretsDocsUrlSuffix = "secrets" ) +type SecretsScanType string + type SecretScanManager struct { secretsScannerResults []*sarif.Run scanner *jas.JasScanner + scanType SecretsScanType } // The getSecretsScanResults function runs the secrets scan flow, which includes the following steps: @@ -29,8 +33,8 @@ type SecretScanManager struct { // Return values: // []utils.IacOrSecretResult: a list of the secrets that were found. // error: An error object (if any). -func RunSecretsScan(scanner *jas.JasScanner) (results []*sarif.Run, err error) { - secretScanManager := newSecretsScanManager(scanner) +func RunSecretsScan(scanner *jas.JasScanner, scanType SecretsScanType) (results []*sarif.Run, err error) { + secretScanManager := newSecretsScanManager(scanner, scanType) log.Info("Running secrets scanning...") if err = secretScanManager.scanner.Run(secretScanManager); err != nil { err = utils.ParseAnalyzerManagerError(utils.Secrets, err) @@ -43,10 +47,11 @@ func RunSecretsScan(scanner *jas.JasScanner) (results []*sarif.Run, err error) { return } -func newSecretsScanManager(scanner *jas.JasScanner) (manager *SecretScanManager) { +func newSecretsScanManager(scanner *jas.JasScanner, scanType SecretsScanType) (manager *SecretScanManager) { return &SecretScanManager{ secretsScannerResults: []*sarif.Run{}, scanner: scanner, + scanType: scanType, } } @@ -89,7 +94,7 @@ func (s *SecretScanManager) createConfigFile(module jfrogappsconfig.Module) erro { Roots: roots, Output: s.scanner.ResultsFileName, - Type: secretsScannerType, + Type: string(s.scanType), SkippedDirs: jas.GetExcludePatterns(module, module.Scanners.Secrets), }, }, diff --git a/commands/audit/jas/secrets/secretsscanner_test.go b/jas/secrets/secretsscanner_test.go similarity index 89% rename from commands/audit/jas/secrets/secretsscanner_test.go rename to jas/secrets/secretsscanner_test.go index 82f10c99..bee3e87a 100644 --- a/commands/audit/jas/secrets/secretsscanner_test.go +++ b/jas/secrets/secretsscanner_test.go @@ -6,7 +6,7 @@ import ( "testing" jfrogappsconfig "github.com/jfrog/jfrog-apps-config/go" - "github.com/jfrog/jfrog-cli-security/commands/audit/jas" + "github.com/jfrog/jfrog-cli-security/jas" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" "github.com/stretchr/testify/assert" @@ -15,7 +15,7 @@ import ( func TestNewSecretsScanManager(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - secretScanManager := newSecretsScanManager(scanner) + secretScanManager := newSecretsScanManager(scanner, SecretsScannerType) assert.NotEmpty(t, secretScanManager) assert.NotEmpty(t, secretScanManager.scanner.ConfigFileName) @@ -26,7 +26,7 @@ func TestNewSecretsScanManager(t *testing.T) { func TestSecretsScan_CreateConfigFile_VerifyFileWasCreated(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - secretScanManager := newSecretsScanManager(scanner) + secretScanManager := newSecretsScanManager(scanner, SecretsScannerType) currWd, err := coreutils.GetWorkingDirectory() assert.NoError(t, err) @@ -53,7 +53,7 @@ func TestRunAnalyzerManager_ReturnsGeneralError(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - secretScanManager := newSecretsScanManager(scanner) + secretScanManager := newSecretsScanManager(scanner, SecretsScannerType) assert.Error(t, secretScanManager.runAnalyzerManager()) } @@ -61,7 +61,7 @@ func TestParseResults_EmptyResults(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() // Arrange - secretScanManager := newSecretsScanManager(scanner) + secretScanManager := newSecretsScanManager(scanner, SecretsScannerType) secretScanManager.scanner.ResultsFileName = filepath.Join(jas.GetTestDataPath(), "secrets-scan", "no-secrets.sarif") // Act @@ -84,7 +84,7 @@ func TestParseResults_ResultsContainSecrets(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - secretScanManager := newSecretsScanManager(scanner) + secretScanManager := newSecretsScanManager(scanner, SecretsScannerType) secretScanManager.scanner.ResultsFileName = filepath.Join(jas.GetTestDataPath(), "secrets-scan", "contain-secrets.sarif") // Act @@ -107,7 +107,7 @@ func TestGetSecretsScanResults_AnalyzerManagerReturnsError(t *testing.T) { scanner, cleanUp := jas.InitJasTest(t) defer cleanUp() - secretsResults, err := RunSecretsScan(scanner) + secretsResults, err := RunSecretsScan(scanner, SecretsScannerType) assert.Error(t, err) assert.ErrorContains(t, err, "failed to run Secrets scan") diff --git a/commands/audit/jas/testdata/.jfrog/jfrog-apps-config.yml b/jas/testdata/.jfrog/jfrog-apps-config.yml similarity index 100% rename from commands/audit/jas/testdata/.jfrog/jfrog-apps-config.yml rename to jas/testdata/.jfrog/jfrog-apps-config.yml diff --git a/utils/jasutils.go b/utils/jasutils.go new file mode 100644 index 00000000..0aa92ea2 --- /dev/null +++ b/utils/jasutils.go @@ -0,0 +1,16 @@ +package utils + +import ( + clientutils "github.com/jfrog/jfrog-client-go/utils" + "github.com/jfrog/jfrog-client-go/utils/log" + "github.com/jfrog/jfrog-client-go/xray" +) + +func IsEntitledForJas(xrayManager *xray.XrayServicesManager, xrayVersion string) (entitled bool, err error) { + if e := clientutils.ValidateMinimumVersion(clientutils.Xray, xrayVersion, EntitlementsMinVersion); e != nil { + log.Debug(e) + return + } + entitled, err = xrayManager.IsEntitled(ApplicabilityFeatureId) + return +}