Skip to content

Commit 88ad5c0

Browse files
committed
fix statuses bug and refactor for more simple way
1 parent d66f74f commit 88ad5c0

File tree

19 files changed

+384
-384
lines changed

19 files changed

+384
-384
lines changed

jas/runner/jasrunner.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func runSecretsScan(params *JasRunnerParams) parallel.TaskFunc {
149149
params.Runner.ResultsMu.Lock()
150150
defer params.Runner.ResultsMu.Unlock()
151151
// We first add the scan results and only then check for errors, so we can store the exit code in order to report it in the end
152-
params.ScanResults.JasResults.AddJasScanResults(jasutils.Secrets, vulnerabilitiesResults, violationsResults, jas.GetAnalyzerManagerExitCode(err))
152+
params.ScanResults.AddJasScanResults(jasutils.Secrets, vulnerabilitiesResults, violationsResults, jas.GetAnalyzerManagerExitCode(err))
153153
if err = jas.ParseAnalyzerManagerError(jasutils.Secrets, err); err != nil {
154154
return fmt.Errorf("%s%s", clientutils.GetLogMsgPrefix(threadId, false), err.Error())
155155
}
@@ -166,7 +166,7 @@ func runIacScan(params *JasRunnerParams) parallel.TaskFunc {
166166
params.Runner.ResultsMu.Lock()
167167
defer params.Runner.ResultsMu.Unlock()
168168
// We first add the scan results and only then check for errors, so we can store the exit code in order to report it in the end
169-
params.ScanResults.JasResults.AddJasScanResults(jasutils.IaC, vulnerabilitiesResults, violationsResults, jas.GetAnalyzerManagerExitCode(err))
169+
params.ScanResults.AddJasScanResults(jasutils.IaC, vulnerabilitiesResults, violationsResults, jas.GetAnalyzerManagerExitCode(err))
170170
if err = jas.ParseAnalyzerManagerError(jasutils.IaC, err); err != nil {
171171
return fmt.Errorf("%s%s", clientutils.GetLogMsgPrefix(threadId, false), err.Error())
172172
}
@@ -183,7 +183,7 @@ func runSastScan(params *JasRunnerParams) parallel.TaskFunc {
183183
params.Runner.ResultsMu.Lock()
184184
defer params.Runner.ResultsMu.Unlock()
185185
// We first add the scan results and only then check for errors, so we can store the exit code in order to report it in the end
186-
params.ScanResults.JasResults.AddJasScanResults(jasutils.Sast, vulnerabilitiesResults, violationsResults, jas.GetAnalyzerManagerExitCode(err))
186+
params.ScanResults.AddJasScanResults(jasutils.Sast, vulnerabilitiesResults, violationsResults, jas.GetAnalyzerManagerExitCode(err))
187187
if err = jas.ParseAnalyzerManagerError(jasutils.Sast, err); err != nil {
188188
return fmt.Errorf("%s%s", clientutils.GetLogMsgPrefix(threadId, false), err.Error())
189189
}
@@ -216,7 +216,7 @@ func runContextualScan(params *JasRunnerParams) parallel.TaskFunc {
216216
params.Runner.ResultsMu.Lock()
217217
defer params.Runner.ResultsMu.Unlock()
218218
// We first add the scan results and only then check for errors, so we can store the exit code in order to report it in the end
219-
params.ScanResults.JasResults.AddApplicabilityScanResults(jas.GetAnalyzerManagerExitCode(err), caScanResults...)
219+
params.ScanResults.AddApplicabilityScanResults(jas.GetAnalyzerManagerExitCode(err), caScanResults...)
220220
if err = jas.ParseAnalyzerManagerError(jasutils.Applicability, err); err != nil {
221221
return fmt.Errorf("%s%s", clientutils.GetLogMsgPrefix(threadId, false), err.Error())
222222
}

policy/enforcer/policyenforcer.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,6 @@ func locateBomVulnerabilityInfo(cmdResults *results.SecurityCommandResults, issu
257257
if target.ScaResults == nil || target.ScaResults.Sbom == nil || target.ScaResults.Sbom.Vulnerabilities == nil {
258258
continue
259259
}
260-
var applicabilityRuns []*sarif.Run
261-
if cmdResults.EntitledForJas && target.JasResults != nil {
262-
applicabilityRuns = results.ScanResultsToRuns(target.JasResults.ApplicabilityScanResults)
263-
}
264260
for _, vulnerability := range *target.ScaResults.Sbom.Vulnerabilities {
265261
if vulnerability.ID != issueId || vulnerability.Affects == nil || len(*vulnerability.Affects) == 0 {
266262
continue
@@ -269,7 +265,7 @@ func locateBomVulnerabilityInfo(cmdResults *results.SecurityCommandResults, issu
269265
if affected.Ref == impactedComponent.BOMRef {
270266
// Found the relevant component in a vulnerability
271267
relevantVulnerability = &vulnerability
272-
contextualAnalysis = results.GetCveApplicabilityField(vulnerability.BOMRef, applicabilityRuns)
268+
contextualAnalysis = results.GetCveApplicabilityField(vulnerability.BOMRef, target.JasResults.ApplicabilityScanResults)
273269
break
274270
}
275271
}

policy/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,20 @@ func getStatusCodeFromErr(err error) int {
5656
}
5757

5858
func CheckPolicyFailBuildError(cmdResults *results.SecurityCommandResults) (err error) {
59-
if cmdResults == nil || cmdResults.Violations.IsScanFailed() {
59+
if cmdResults == nil || (cmdResults.ViolationsStatusCode != nil && *cmdResults.ViolationsStatusCode != 0) {
6060
return
6161
}
62-
if cmdResults.Violations.Scan.ShouldFailBuild() {
62+
if cmdResults.Violations.ShouldFailBuild() {
6363
err = NewFailBuildError()
6464
}
6565
return
6666
}
6767

6868
func CheckPolicyFailPrError(cmdResults *results.SecurityCommandResults) (err error) {
69-
if cmdResults == nil || cmdResults.Violations.IsScanFailed() {
69+
if cmdResults == nil || (cmdResults.ViolationsStatusCode != nil && *cmdResults.ViolationsStatusCode != 0) {
7070
return
7171
}
72-
if cmdResults.Violations.Scan.ShouldFailPR() {
72+
if cmdResults.Violations.ShouldFailPR() {
7373
err = NewFailPrError()
7474
}
7575
return

policy/local/localconvertor.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ func (d *DeprecatedViolationGenerator) GenerateViolations(cmdResults *results.Se
5757
// JAS violations (from JasResults)
5858
if target.JasResults != nil {
5959
if len(target.JasResults.JasViolations.SecretsScanResults) > 0 {
60-
if e := results.ForEachJasIssue(results.ScanResultsToRuns(target.JasResults.JasViolations.SecretsScanResults), cmdResults.EntitledForJas, convertJasViolationsToPolicyViolations(&convertedViolations, jasutils.Secrets)); e != nil {
60+
if e := results.ForEachJasIssue(target.JasResults.JasViolations.SecretsScanResults, cmdResults.EntitledForJas, convertJasViolationsToPolicyViolations(&convertedViolations, jasutils.Secrets)); e != nil {
6161
err = errors.Join(err, fmt.Errorf("failed to convert JAS Secret violations for target %s: %w", target.ScanTarget.Target, e))
6262
}
6363
}
6464
if len(target.JasResults.JasViolations.IacScanResults) > 0 {
65-
if e := results.ForEachJasIssue(results.ScanResultsToRuns(target.JasResults.JasViolations.IacScanResults), cmdResults.EntitledForJas, convertJasViolationsToPolicyViolations(&convertedViolations, jasutils.IaC)); e != nil {
65+
if e := results.ForEachJasIssue(target.JasResults.JasViolations.IacScanResults, cmdResults.EntitledForJas, convertJasViolationsToPolicyViolations(&convertedViolations, jasutils.IaC)); e != nil {
6666
err = errors.Join(err, fmt.Errorf("failed to convert JAS IaC violations for target %s: %w", target.ScanTarget.Target, e))
6767
}
6868
}
6969
if len(target.JasResults.JasViolations.SastScanResults) > 0 {
70-
if e := results.ForEachJasIssue(results.ScanResultsToRuns(target.JasResults.JasViolations.SastScanResults), cmdResults.EntitledForJas, convertJasViolationsToPolicyViolations(&convertedViolations, jasutils.Sast)); e != nil {
70+
if e := results.ForEachJasIssue(target.JasResults.JasViolations.SastScanResults, cmdResults.EntitledForJas, convertJasViolationsToPolicyViolations(&convertedViolations, jasutils.Sast)); e != nil {
7171
err = errors.Join(err, fmt.Errorf("failed to convert JAS SAST violations for target %s: %w", target.ScanTarget.Target, e))
7272
}
7373
}
@@ -87,19 +87,19 @@ func (d *DeprecatedViolationGenerator) generateScaViolations(target *results.Tar
8787
_, _, e := ForEachScanGraphViolation(
8888
target.ScanTarget,
8989
target.ScaResults.Descriptors,
90-
deprecatedXrayResult.Scan.Violations,
90+
deprecatedXrayResult.Violations,
9191
entitledForJas,
9292
applicableRuns,
9393
convertScaSecurityViolationToPolicyViolation(convertedViolations),
9494
convertScaLicenseViolationToPolicyViolation(convertedViolations),
9595
convertOperationalRiskViolationToPolicyViolation(convertedViolations),
9696
)
9797
if e != nil {
98-
err = errors.Join(err, fmt.Errorf("failed to convert scan graph results (scanId: %s): %w", deprecatedXrayResult.Scan.ScanId, e))
98+
err = errors.Join(err, fmt.Errorf("failed to convert scan graph results (scanId: %s): %w", deprecatedXrayResult.ScanId, e))
9999
continue
100100
}
101101
// Collect licenses for local license violation generation
102-
licenses = append(licenses, deprecatedXrayResult.Scan.Licenses...)
102+
licenses = append(licenses, deprecatedXrayResult.Licenses...)
103103
}
104104
if len(convertedViolations.License) > 0 || len(d.AllowedLicenses) == 0 {
105105
// Deprecated option to provide allowed-licenses to generate local violations (only if no violations were found)

sca/bom/bomgenerator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func GenerateSbomForTarget(generator SbomGenerator, params SbomGeneratorParams)
3939
// Generate the SBOM for the target
4040
sbom, err := generator.GenerateSbom(params.Target.ScanTarget)
4141
if err != nil {
42+
params.Target.ResultsStatus.UpdateStatus(results.CmdStepSbom, utils.NewIntPtr(1))
4243
_ = params.Target.AddTargetError(fmt.Errorf("failed to generate SBOM for %s: %s", params.Target.Target, err.Error()), params.AllowPartialResults)
4344
return
4445
}
@@ -73,6 +74,7 @@ func getDiffSbom(sbom *cyclonedx.BOM, params SbomGeneratorParams) *cyclonedx.BOM
7374

7475
func updateTarget(target *results.TargetResults, sbom *cyclonedx.BOM) {
7576
target.SetSbom(sbom)
77+
target.ResultsStatus.UpdateStatus(results.CmdStepSbom, utils.NewIntPtr(0))
7678
if err := logLibComponents(sbom.Components); err != nil {
7779
log.Warn(fmt.Sprintf("Failed to log library components in SBOM for %s: %s", target.Target, err.Error()))
7880
}

tests/validations/test_mocks.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ import (
1414
"github.com/jfrog/jfrog-cli-security/utils/formats"
1515
"github.com/jfrog/jfrog-cli-security/utils/formats/sarifutils"
1616
"github.com/jfrog/jfrog-cli-security/utils/jasutils"
17-
"github.com/jfrog/jfrog-cli-security/utils/results"
1817
"github.com/jfrog/jfrog-cli-security/utils/severityutils"
1918
"github.com/jfrog/jfrog-client-go/artifactory"
20-
"github.com/jfrog/jfrog-client-go/xray/services"
2119
xrayutils "github.com/jfrog/jfrog-client-go/xray/services/utils"
2220
xscservices "github.com/jfrog/jfrog-client-go/xsc/services"
2321
xscutils "github.com/jfrog/jfrog-client-go/xsc/services/utils"
@@ -231,21 +229,6 @@ func XrayServer(t *testing.T, params MockServerParams) (*httptest.Server, *confi
231229
return serverMock, serverDetails, &apiCallCounts
232230
}
233231

234-
func NewMockJasRuns(runs ...*sarif.Run) []results.ScanResult[[]*sarif.Run] {
235-
return []results.ScanResult[[]*sarif.Run]{{Scan: runs}}
236-
}
237-
238-
func NewMockScaResults(responses ...services.ScanResponse) (converted []results.ScanResult[services.ScanResponse]) {
239-
for _, response := range responses {
240-
status := 0
241-
if response.ScannedStatus == "failed" {
242-
status = 1
243-
}
244-
converted = append(converted, results.ScanResult[services.ScanResponse]{Scan: response, StatusCode: status})
245-
}
246-
return
247-
}
248-
249232
func CreateDummyApplicabilityRule(cve string, applicableStatus jasutils.ApplicabilityStatus) *sarif.ReportingDescriptor {
250233
id := fmt.Sprintf("applic_%s", cve)
251234
properties := sarif.NewPropertyBag()

utils/results/common.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -563,14 +563,6 @@ func GetApplicableCveStatus(entitledForJas bool, applicabilityScanResults []*sar
563563
return GetFinalApplicabilityStatus(applicableStatuses)
564564
}
565565

566-
// We only care to update the status if it's the first time we see it or if status is 0 (completed) and the new status is not (failed)
567-
func ShouldUpdateStatus(currentStatus, newStatus *int) bool {
568-
if currentStatus == nil || (*currentStatus == 0 && newStatus != nil) {
569-
return true
570-
}
571-
return false
572-
}
573-
574566
func getApplicabilityStatusFromRule(rule *sarif.ReportingDescriptor) jasutils.ApplicabilityStatus {
575567
if rule != nil && rule.Properties != nil && rule.Properties.Properties[jasutils.ApplicabilitySarifPropertyKey] != nil {
576568
status, ok := rule.Properties.Properties[jasutils.ApplicabilitySarifPropertyKey].(string)
@@ -698,11 +690,12 @@ func ConvertPolicesToString(policies []services.Policy) []string {
698690
return policiesStr
699691
}
700692

701-
func ScanResultsToRuns(results []ScanResult[[]*sarif.Run]) (runs []*sarif.Run) {
702-
for _, result := range results {
703-
runs = append(runs, result.Scan...)
693+
func CollectRuns(runs ...[]*sarif.Run) []*sarif.Run {
694+
flat := []*sarif.Run{}
695+
for _, runSlice := range runs {
696+
flat = append(flat, runSlice...)
704697
}
705-
return
698+
return flat
706699
}
707700

708701
// Resolve the actual technology from multiple sources:

0 commit comments

Comments
 (0)