Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas committed Oct 1, 2024
1 parent c96a26a commit 1f67a97
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ require (
)

// attiasas:refactor_output
replace github.com/jfrog/jfrog-cli-security => github.com/attiasas/jfrog-cli-security v0.0.0-20240930161129-6c0b4b6d1d47
replace github.com/jfrog/jfrog-cli-security => github.com/attiasas/jfrog-cli-security v0.0.0-20241001110908-d240a3175b56

// replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 dev

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,8 @@ github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/attiasas/jfrog-cli-security v0.0.0-20240930161129-6c0b4b6d1d47 h1:u9Z97zsMFKcJCsppn3WlAppSUjV/zQ4rI4U549re2yU=
github.com/attiasas/jfrog-cli-security v0.0.0-20240930161129-6c0b4b6d1d47/go.mod h1:0vBYBP1jztDf5e25Ww3CkQAA1C609CAccz9NJLoSoRk=
github.com/attiasas/jfrog-cli-security v0.0.0-20241001110908-d240a3175b56 h1:72vuWKOsrnMSCHuogwzqwRLsn92UIRfxJWYdtZE6kJ8=
github.com/attiasas/jfrog-cli-security v0.0.0-20241001110908-d240a3175b56/go.mod h1:0vBYBP1jztDf5e25Ww3CkQAA1C609CAccz9NJLoSoRk=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
Expand Down
12 changes: 6 additions & 6 deletions scanpullrequest/scanpullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func auditPullRequestInProject(repoConfig *utils.Repository, scanDetails *utils.

// Get all issues that exist in the source branch
if repoConfig.IncludeAllVulnerabilities {
if auditIssues, err = getAllIssues(sourceResults, repoConfig.AllowedLicenses); err != nil {
if auditIssues, err = getAllIssues(sourceResults, repoConfig.AllowedLicenses, scanDetails.HasViolationContext()); err != nil {
return
}
utils.ConvertSarifPathsToRelative(auditIssues, sourceBranchWd)
Expand Down Expand Up @@ -224,15 +224,15 @@ func auditTargetBranch(repoConfig *utils.Repository, scanDetails *utils.ScanDeta
}

// Get newly added issues
newIssues, err = getNewlyAddedIssues(targetResults, sourceScanResults, repoConfig.AllowedLicenses)
newIssues, err = getNewlyAddedIssues(targetResults, sourceScanResults, repoConfig.AllowedLicenses, scanDetails.HasViolationContext())
return
}

func getAllIssues(cmdResults *results.SecurityCommandResults, allowedLicenses []string) (*utils.IssuesCollection, error) {
func getAllIssues(cmdResults *results.SecurityCommandResults, allowedLicenses []string, hasViolationContext bool) (*utils.IssuesCollection, error) {
log.Info("Frogbot is configured to show all vulnerabilities")
simpleJsonResults, err := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{
IncludeVulnerabilities: true,
HasViolationContext: true,
HasViolationContext: hasViolationContext,
AllowedLicenses: allowedLicenses,
IncludeLicenses: true,
SimplifiedOutput: true,
Expand All @@ -250,9 +250,9 @@ func getAllIssues(cmdResults *results.SecurityCommandResults, allowedLicenses []
}

// Returns all the issues found in the source branch that didn't exist in the target branch.
func getNewlyAddedIssues(targetResults, sourceResults *results.SecurityCommandResults, allowedLicenses []string) (*utils.IssuesCollection, error) {
func getNewlyAddedIssues(targetResults, sourceResults *results.SecurityCommandResults, allowedLicenses []string, hasViolationContext bool) (*utils.IssuesCollection, error) {
var err error
convertor := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{IncludeVulnerabilities: true, HasViolationContext: true, IncludeLicenses: len(allowedLicenses) > 0, AllowedLicenses: allowedLicenses, SimplifiedOutput: true})
convertor := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{IncludeVulnerabilities: true, HasViolationContext: hasViolationContext, IncludeLicenses: len(allowedLicenses) > 0, AllowedLicenses: allowedLicenses, SimplifiedOutput: true})
simpleJsonSource, err := convertor.ConvertToSimpleJson(sourceResults)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion scanpullrequest/scanpullrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func TestGetAllIssues(t *testing.T) {
},
}

issuesRows, err := getAllIssues(auditResults, allowedLicenses)
issuesRows, err := getAllIssues(auditResults, allowedLicenses, false)

if assert.NoError(t, err) {
assert.ElementsMatch(t, expectedOutput.Vulnerabilities, issuesRows.Vulnerabilities)
Expand Down
10 changes: 5 additions & 5 deletions scanrepository/scanrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (cfp *ScanRepositoryCmd) scanAndFixProject(repository *utils.Repository) er
return err
}
if cfp.analyticsService.ShouldReportEvents() {
if summary, err := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{IncludeVulnerabilities: true, HasViolationContext: true}).ConvertToSummary(scanResults); err != nil {
if summary, err := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{IncludeVulnerabilities: true, HasViolationContext: cfp.scanDetails.HasViolationContext()}).ConvertToSummary(scanResults); err != nil {
return err
} else {
totalFindings := summary.GetTotalViolations()
Expand All @@ -172,7 +172,7 @@ func (cfp *ScanRepositoryCmd) scanAndFixProject(repository *utils.Repository) er
// Uploads Sarif results to GitHub in order to view the scan in the code scanning UI
// Currently available on GitHub only and JFrog Advance Security package
// Only if Jas entitlement is available
if err = utils.UploadSarifResultsToGithubSecurityTab(scanResults, repository, cfp.scanDetails.BaseBranch(), cfp.scanDetails.Client()); err != nil {
if err = utils.UploadSarifResultsToGithubSecurityTab(scanResults, repository, cfp.scanDetails.BaseBranch(), cfp.scanDetails.Client(), cfp.scanDetails.HasViolationContext()); err != nil {
log.Warn(err)
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (cfp *ScanRepositoryCmd) scan(currentWorkingDir string) (*results.SecurityC
}

func (cfp *ScanRepositoryCmd) getVulnerabilitiesMap(scanResults *results.SecurityCommandResults) (map[string]*utils.VulnerabilityDetails, error) {
vulnerabilitiesMap, err := cfp.createVulnerabilitiesMap(scanResults)
vulnerabilitiesMap, err := cfp.createVulnerabilitiesMap(scanResults, cfp.scanDetails.HasViolationContext())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -484,9 +484,9 @@ func (cfp *ScanRepositoryCmd) cloneRepositoryAndCheckoutToBranch() (tempWd strin
}

// Create a vulnerabilities map - a map with 'impacted package' as a key and all the necessary information of this vulnerability as value.
func (cfp *ScanRepositoryCmd) createVulnerabilitiesMap(scanResults *results.SecurityCommandResults) (map[string]*utils.VulnerabilityDetails, error) {
func (cfp *ScanRepositoryCmd) createVulnerabilitiesMap(scanResults *results.SecurityCommandResults, hasViolationContext bool) (map[string]*utils.VulnerabilityDetails, error) {
vulnerabilitiesMap := map[string]*utils.VulnerabilityDetails{}
simpleJsonResult, err := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{IncludeVulnerabilities: true, HasViolationContext: true}).ConvertToSimpleJson(scanResults)
simpleJsonResult, err := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{IncludeVulnerabilities: true, HasViolationContext: hasViolationContext}).ConvertToSimpleJson(scanResults)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion scanrepository/scanrepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func TestCreateVulnerabilitiesMap(t *testing.T) {

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
fixVersionsMap, err := cfp.createVulnerabilitiesMap(testCase.scanResults)
fixVersionsMap, err := cfp.createVulnerabilitiesMap(testCase.scanResults, true)
assert.NoError(t, err)
for name, expectedVuln := range testCase.expectedMap {
actualVuln, exists := fixVersionsMap[name]
Expand Down
4 changes: 4 additions & 0 deletions utils/scandetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (sc *ScanDetails) CreateCommonGraphScanParams() *scangraph.CommonGraphScanP
return commonParams
}

func (sc *ScanDetails) HasViolationContext() bool {
return sc.ProjectKey != "" || len(sc.Watches) > 0 || sc.RepoPath != ""
}

func createXrayScanParams(watches []string, project string, includeLicenses bool) (params *services.XrayGraphScanParams) {
params = &services.XrayGraphScanParams{
ScanType: services.Dependency,
Expand Down
8 changes: 4 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ func VulnerabilityDetailsToMD5Hash(vulnerabilities ...formats.VulnerabilityOrVio
return hex.EncodeToString(hash.Sum(nil)), nil
}

func UploadSarifResultsToGithubSecurityTab(scanResults *results.SecurityCommandResults, repo *Repository, branch string, client vcsclient.VcsClient) error {
report, err := GenerateFrogbotSarifReport(scanResults, scanResults.HasMultipleTargets(), repo.AllowedLicenses)
func UploadSarifResultsToGithubSecurityTab(scanResults *results.SecurityCommandResults, repo *Repository, branch string, client vcsclient.VcsClient, hasViolationContext bool) error {
report, err := GenerateFrogbotSarifReport(scanResults, scanResults.HasMultipleTargets(), hasViolationContext, repo.AllowedLicenses)
if err != nil {
return err
}
Expand All @@ -235,10 +235,10 @@ func UploadSarifResultsToGithubSecurityTab(scanResults *results.SecurityCommandR
return nil
}

func GenerateFrogbotSarifReport(extendedResults *results.SecurityCommandResults, isMultipleRoots bool, allowedLicenses []string) (string, error) {
func GenerateFrogbotSarifReport(extendedResults *results.SecurityCommandResults, isMultipleRoots, hasViolationContext bool, allowedLicenses []string) (string, error) {
convertor := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{
IncludeVulnerabilities: true,
HasViolationContext: true,
HasViolationContext: hasViolationContext,
IsMultipleRoots: &isMultipleRoots,
AllowedLicenses: allowedLicenses,
})
Expand Down

0 comments on commit 1f67a97

Please sign in to comment.