Skip to content

Commit

Permalink
fix(misconf): deduplicate misconf results (#4588)
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 authored Jun 8, 2023
1 parent 9b531fa commit d52b0b7
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions pkg/fanal/types/misconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
"fmt"
"sort"

"github.com/samber/lo"
)

type Misconfiguration struct {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
})
}

0 comments on commit d52b0b7

Please sign in to comment.