Skip to content

Commit b0c30ce

Browse files
committed
add deduplication for report
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 0309532 commit b0c30ce

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

auditor/auditor.report.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auditor
22

33
import (
4+
"crypto/sha1" // #nosec G505
45
"encoding/json"
56
"regexp"
67
"sync"
@@ -33,6 +34,7 @@ type (
3334
RuleID string `json:"rule"`
3435
GroupBy interface{} `json:"groupBy"`
3536
Status bool `json:"status"`
37+
Count uint64 `json:"count"`
3638
}
3739
)
3840

@@ -44,6 +46,11 @@ func NewAzureAuditorReport() *AzureAuditorReport {
4446
return report
4547
}
4648

49+
func (reportLine *AzureAuditorReportLine) Hash() [20]byte {
50+
hashData, _ := json.Marshal(reportLine)
51+
return sha1.Sum(hashData) // #nosec G401
52+
}
53+
4754
func (reportLine *AzureAuditorReportLine) MarshalJSON() ([]byte, error) {
4855
data := map[string]interface{}{}
4956

@@ -52,6 +59,7 @@ func (reportLine *AzureAuditorReportLine) MarshalJSON() ([]byte, error) {
5259
data["rule"] = reportLine.RuleID
5360
data["status"] = reportLine.Status
5461
data["groupBy"] = reportLine.GroupBy
62+
data["count"] = reportLine.Count
5563

5664
data["resource"] = yamlCleanupRegexp.ReplaceAllString(data["resource"].(string), "$1: $2")
5765

main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,25 @@ func startHttpServer() {
327327
reportData = append(reportData, line)
328328
}
329329

330+
// unique filter
331+
reportDataUnique := map[[20]byte]auditor.AzureAuditorReportLine{}
332+
for _, line := range reportData {
333+
hash := line.Hash()
334+
335+
if existingLine, exists := reportDataUnique[hash]; exists {
336+
existingLine.Count++
337+
reportDataUnique[hash] = existingLine
338+
} else {
339+
line.Count = 1
340+
reportDataUnique[hash] = line
341+
}
342+
}
343+
reportData = []auditor.AzureAuditorReportLine{}
344+
for _, line := range reportDataUnique {
345+
reportData = append(reportData, line)
346+
}
347+
348+
// json encoding
330349
data, err := json.Marshal(reportData)
331350
if err != nil {
332351
w.WriteHeader(http.StatusInternalServerError)

templates/report.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ let table = new Tabulator("#report-table", {
127127
{title:"Status", field:"status", formatter:"tickCross", width:115},
128128
{title:"Resource", field:"resource", formatter:yamlFormatter, formatterPrint:yamlFormatter},
129129
{title:"Rule", field:"rule", formatter:"plaintext", width:300},
130+
{title:"Count", field:"count", formatter:"plaintext", width:100},
130131
],
131132

132133
groupBy: "groupBy",

0 commit comments

Comments
 (0)