From 0e0e1ed6fb36e8d75b12b7d092388e23f8b16a95 Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Mon, 11 Mar 2024 09:22:03 +0200 Subject: [PATCH] remove repeating fixes Signed-off-by: David Wertenteil --- .../printer/v2/sarifprinter.go | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/core/pkg/resultshandling/printer/v2/sarifprinter.go b/core/pkg/resultshandling/printer/v2/sarifprinter.go index b530da8d..2d5fd5fe 100644 --- a/core/pkg/resultshandling/printer/v2/sarifprinter.go +++ b/core/pkg/resultshandling/printer/v2/sarifprinter.go @@ -2,6 +2,7 @@ package printer import ( "context" + "crypto/sha256" "encoding/json" "fmt" "net/url" @@ -224,14 +225,14 @@ func (sp *SARIFPrinter) printConfigurationScan(ctx context.Context, opaSessionOb // first get the failed path, then if cannot find it, use the Fix path, cui it to find the closest error. location, split := resolveFixLocation(subfileNodes, &ac) sp.addRule(run, ctl) - result := sp.addResult(run, ctl, filepath, location) - collectFixesFromMappingNodes(ctx, result, ac, opaSessionObj, resourceID, filepath, rsrcAbsPath, location, subfileNodes, split) + r := sp.addResult(run, ctl, filepath, location) + collectFixesFromMappingNodes(r, ac, opaSessionObj, resourceID, filepath, rsrcAbsPath, location, subfileNodes, split) } } else { location = sp.resolveFixLocation(opaSessionObj, locationResolver, &ac, resourceID) sp.addRule(run, ctl) - result := sp.addResult(run, ctl, filepath, location) - collectFixes(ctx, result, ac, opaSessionObj, resourceID, filepath, rsrcAbsPath) + r := sp.addResult(run, ctl, filepath, location) + collectFixes(ctx, r, ac, opaSessionObj, resourceID, filepath, rsrcAbsPath) } } @@ -342,6 +343,15 @@ func addFix(result *sarif.Result, filepath string, startLine, startColumn, endLi sarif.NewSimpleArtifactLocation(filepath), ).WithReplacement(replacement) + // check if the fix is already added + for _, fix := range result.Fixes { + for _, ac := range fix.ArtifactChanges { + if hashArtifactChange(ac) == hashArtifactChange(artifactChange) { + return + } + } + } + // Add the artifact change to the result's fixes. result.AddFix(sarif.NewFix().WithArtifactChanges([]*sarif.ArtifactChange{artifactChange})) } @@ -427,9 +437,6 @@ func collectFixes(ctx context.Context, result *sarif.Result, ac resourcesresults var fixedYamlString string - // if strings.HasPrefix(rulePaths.FixPath.Value, fixhandler.UserValuePrefix) { - // continue - // } documentIndex, ok := getDocIndex(opaSessionObj, resourceID) if !ok { continue @@ -450,7 +457,7 @@ func collectFixes(ctx context.Context, result *sarif.Result, ac resourcesresults } } -func collectFixesFromMappingNodes(ctx context.Context, result *sarif.Result, ac resourcesresults.ResourceAssociatedControl, opaSessionObj *cautils.OPASessionObj, resourceID string, filepath string, rsrcAbsPath string, location locationresolver.Location, subFileNodes map[string]cautils.MappingNode, split int) { +func collectFixesFromMappingNodes(result *sarif.Result, ac resourcesresults.ResourceAssociatedControl, opaSessionObj *cautils.OPASessionObj, resourceID string, filepath string, rsrcAbsPath string, location locationresolver.Location, subFileNodes map[string]cautils.MappingNode, split int) { for _, rule := range ac.ResourceAssociatedRules { if !rule.GetStatus(nil).IsFailed() { continue @@ -576,3 +583,7 @@ func getBasePathFromMetadata(opaSessionObj cautils.OPASessionObj) string { func (sp *SARIFPrinter) generateRemediationMessage(control reportsummary.IControlSummary) string { return fmt.Sprintf("Remediation: %s", control.GetRemediation()) } +func hashArtifactChange(artifactChange *sarif.ArtifactChange) [32]byte { + acJson, _ := json.Marshal(artifactChange) + return sha256.Sum256(acJson) +}