From d52b0b7bc0371003ecfd4ef37f9a3643796b5cbd Mon Sep 17 00:00:00 2001 From: Teppei Fukuda Date: Thu, 8 Jun 2023 21:15:21 +0900 Subject: [PATCH] fix(misconf): deduplicate misconf results (#4588) --- pkg/fanal/types/misconf.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/fanal/types/misconf.go b/pkg/fanal/types/misconf.go index 7f021cd922ec..89e14359dee6 100644 --- a/pkg/fanal/types/misconf.go +++ b/pkg/fanal/types/misconf.go @@ -3,6 +3,8 @@ package types import ( "fmt" "sort" + + "github.com/samber/lo" ) type Misconfiguration struct { @@ -101,6 +103,8 @@ func ToMisconfigurations(misconfs map[string]Misconfiguration) []Misconfiguratio for _, misconf := range misconfs { // Remove duplicates misconf.Successes = uniqueResults(misconf.Successes) + misconf.Warnings = uniqueResults(misconf.Warnings) + misconf.Failures = uniqueResults(misconf.Failures) // Sort results sort.Sort(misconf.Successes) @@ -123,15 +127,11 @@ func ToMisconfigurations(misconfs map[string]Misconfiguration) []Misconfiguratio } func uniqueResults(results []MisconfResult) []MisconfResult { - uniq := map[string]MisconfResult{} - for _, result := range results { - key := fmt.Sprintf("%s::%s::%s", result.ID, result.Namespace, result.Message) - uniq[key] = result - } - - var uniqResults []MisconfResult - for _, s := range uniq { - uniqResults = append(uniqResults, s) + if len(results) == 0 { + return results } - return uniqResults + return lo.UniqBy(results, func(result MisconfResult) string { + return fmt.Sprintf("ID: %s, Namespace: %s, Messsage: %s, Cause: %v", + result.ID, result.Namespace, result.Message, result.CauseMetadata) + }) }