@@ -3,10 +3,14 @@ package auditor
3
3
import (
4
4
"crypto/sha1" // #nosec G505
5
5
"encoding/json"
6
+ "fmt"
6
7
"regexp"
8
+ "sort"
9
+ "strings"
7
10
"sync"
8
11
"time"
9
12
13
+ "github.com/webdevops/go-common/utils/to"
10
14
yaml "sigs.k8s.io/yaml"
11
15
12
16
"github.com/webdevops/azure-auditor/auditor/types"
@@ -32,12 +36,14 @@ type (
32
36
}
33
37
34
38
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"`
40
44
}
45
+
46
+ AzureAuditorReportLineResource map [string ]interface {}
41
47
)
42
48
43
49
func NewAzureAuditorReport () * AzureAuditorReport {
@@ -56,15 +62,13 @@ func (reportLine *AzureAuditorReportLine) Hash() [20]byte {
56
62
func (reportLine * AzureAuditorReportLine ) MarshalJSON () ([]byte , error ) {
57
63
data := map [string ]interface {}{}
58
64
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" )
61
67
data ["rule" ] = reportLine .RuleID
62
68
data ["status" ] = reportLine .Status
63
69
data ["groupBy" ] = reportLine .GroupBy
64
70
data ["count" ] = reportLine .Count
65
71
66
- data ["resource" ] = yamlCleanupRegexp .ReplaceAllString (data ["resource" ].(string ), "$1: $2" )
67
-
68
72
return json .Marshal (data )
69
73
}
70
74
@@ -82,7 +86,7 @@ func (report *AzureAuditorReport) Add(resource *validator.AzureObject, ruleID st
82
86
report .Lines = append (
83
87
report .Lines ,
84
88
& AzureAuditorReportLine {
85
- Resource : * resource ,
89
+ Resource : AzureAuditorReportLineResource ( * resource ) ,
86
90
RuleID : ruleID ,
87
91
Status : status .String (),
88
92
},
@@ -97,3 +101,34 @@ func (report *AzureAuditorReport) Add(resource *validator.AzureObject, ruleID st
97
101
report .Summary .Allow ++
98
102
}
99
103
}
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