@@ -3,10 +3,14 @@ package auditor
33import (
44 "crypto/sha1" // #nosec G505
55 "encoding/json"
6+ "fmt"
67 "regexp"
8+ "sort"
9+ "strings"
710 "sync"
811 "time"
912
13+ "github.com/webdevops/go-common/utils/to"
1014 yaml "sigs.k8s.io/yaml"
1115
1216 "github.com/webdevops/azure-auditor/auditor/types"
@@ -32,12 +36,14 @@ type (
3236 }
3337
3438 AzureAuditorReportLine struct {
35- Resource map [ string ] interface {} `json:"resource"`
36- RuleID string `json:"rule"`
37- GroupBy interface {} `json:"groupBy"`
38- Status string `json:"status"`
39- Count uint64 `json:"count"`
39+ Resource AzureAuditorReportLineResource `json:"resource"`
40+ RuleID string `json:"rule"`
41+ GroupBy interface {} `json:"groupBy"`
42+ Status string `json:"status"`
43+ Count uint64 `json:"count"`
4044 }
45+
46+ AzureAuditorReportLineResource map [string ]interface {}
4147)
4248
4349func NewAzureAuditorReport () * AzureAuditorReport {
@@ -56,15 +62,13 @@ func (reportLine *AzureAuditorReportLine) Hash() [20]byte {
5662func (reportLine * AzureAuditorReportLine ) MarshalJSON () ([]byte , error ) {
5763 data := map [string ]interface {}{}
5864
59- resourceInfo , _ := yaml . Marshal ( reportLine .Resource )
60- data ["resource" ] = string (resourceInfo )
65+ resourceInfo , _ := reportLine .Resource . MarshalJSON ( )
66+ data ["resource" ] = yamlCleanupRegexp . ReplaceAllString ( string (resourceInfo ), "$1: $2" )
6167 data ["rule" ] = reportLine .RuleID
6268 data ["status" ] = reportLine .Status
6369 data ["groupBy" ] = reportLine .GroupBy
6470 data ["count" ] = reportLine .Count
6571
66- data ["resource" ] = yamlCleanupRegexp .ReplaceAllString (data ["resource" ].(string ), "$1: $2" )
67-
6872 return json .Marshal (data )
6973}
7074
@@ -82,7 +86,7 @@ func (report *AzureAuditorReport) Add(resource *validator.AzureObject, ruleID st
8286 report .Lines = append (
8387 report .Lines ,
8488 & AzureAuditorReportLine {
85- Resource : * resource ,
89+ Resource : AzureAuditorReportLineResource ( * resource ) ,
8690 RuleID : ruleID ,
8791 Status : status .String (),
8892 },
@@ -97,3 +101,34 @@ func (report *AzureAuditorReport) Add(resource *validator.AzureObject, ruleID st
97101 report .Summary .Allow ++
98102 }
99103}
104+
105+ func (resource * AzureAuditorReportLineResource ) MarshalJSON () ([]byte , error ) {
106+ lines := map [string ]string {}
107+
108+ for key , value := range * resource {
109+ switch v := value .(type ) {
110+ case []* string :
111+ lines [key ] = strings .Join (to .Slice (v ), ", " )
112+ case []string :
113+ lines [key ] = strings .Join (v , ", " )
114+ case map [string ]interface {}:
115+ data , _ := yaml .Marshal (v )
116+ lines [key ] = string (data )
117+ default :
118+ lines [key ] = fmt .Sprintf ("%v" , v )
119+ }
120+ }
121+
122+ keys := make ([]string , 0 , len (lines ))
123+ for k := range lines {
124+ keys = append (keys , k )
125+ }
126+ sort .Strings (keys )
127+
128+ ret := ""
129+ for _ , key := range keys {
130+ ret += fmt .Sprintf ("%s: %s\n " , key , lines [key ])
131+ }
132+
133+ return []byte (ret ), nil
134+ }
0 commit comments