Skip to content

Commit 3a7a723

Browse files
committed
fix new json/yaml marshalling output for frontend
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent d5bafc2 commit 3a7a723

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

auditor/auditor.report.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package auditor
33
import (
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

4349
func NewAzureAuditorReport() *AzureAuditorReport {
@@ -56,15 +62,13 @@ func (reportLine *AzureAuditorReportLine) Hash() [20]byte {
5662
func (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+
}

auditor/validator/validation.field.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type (
2121
regexp *regexp.Regexp `json:"-"`
2222

2323
// STRINGLIST type
24-
AllOf *[]string `json:"allOf,omitempty,flow"`
25-
AnyOf *[]string `json:"anyOf,omitempty,flow"`
24+
AllOf *[]string `json:"allOf,omitempty"`
25+
AnyOf *[]string `json:"anyOf,omitempty"`
2626

2727
// NUMERIC
2828
Min *float64 `json:"min,omitempty"`

auditor/validator/validation.rule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type (
2929
CustomFunction *string `json:"func,omitempty"`
3030
customFunction *otto.Script
3131

32-
Stats AuditConfigValidationRuleStats `json:"stats,flow"`
32+
Stats AuditConfigValidationRuleStats `json:"stats"`
3333
}
3434

3535
AuditConfigValidationRuleStats struct {

templates/report.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ <h2>{{if $root.RequestReport}}{{ $root.ReportTitle }} report "{{ $root.RequestRe
153153

154154
<div class="input-group mb-3">
155155
<label class="input-group-text" id="reportFilterResourceLabel">
156-
<span class="d-inline-block" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-title="Resource filters" data-bs-content="One filter per line, formats:\n\nfilter by content (contains):\n<strong>field: value</strong>\n\nregexp filter:\n<strong>field: /regexp/">
156+
<span class="d-inline-block" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-title="Resource filters" data-bs-content="One filter per line, formats:\n\nfulltext search (like, contains):\n<strong>search term</strong>\n\nfulltext regexp:\n<strong>/regexp/</strong>\n\nfilter by content (must match):\n<strong>field: search term</strong>\n\nregexp filter:\n<strong>field: /regexp/">
157157
Resource
158158
</span>
159159
</label>

0 commit comments

Comments
 (0)