-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathmisconf.go
133 lines (112 loc) · 3.4 KB
/
misconf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package types
import (
"fmt"
"sort"
)
type Misconfiguration struct {
FileType string `json:",omitempty"`
FilePath string `json:",omitempty"`
Successes MisconfResults `json:",omitempty"`
Warnings MisconfResults `json:",omitempty"`
Failures MisconfResults `json:",omitempty"`
Exceptions MisconfResults `json:",omitempty"`
Layer Layer `json:",omitempty"`
}
type MisconfResult struct {
Namespace string `json:",omitempty"`
Query string `json:",omitempty"`
Message string `json:",omitempty"`
PolicyMetadata `json:",omitempty"`
CauseMetadata `json:",omitempty"`
// For debugging
Traces []string `json:",omitempty"`
}
type MisconfResults []MisconfResult
type CauseMetadata struct {
Resource string `json:",omitempty"`
Provider string `json:",omitempty"`
Service string `json:",omitempty"`
StartLine int `json:",omitempty"`
EndLine int `json:",omitempty"`
Code Code `json:",omitempty"`
}
type Code struct {
Lines []Line
}
type Line struct {
Number int `json:"Number"`
Content string `json:"Content"`
IsCause bool `json:"IsCause"`
Annotation string `json:"Annotation"`
Truncated bool `json:"Truncated"`
Highlighted string `json:"Highlighted,omitempty"`
FirstCause bool `json:"FirstCause"`
LastCause bool `json:"LastCause"`
}
type PolicyMetadata struct {
ID string `json:",omitempty"`
AVDID string `json:",omitempty"`
Type string `json:",omitempty"`
Title string `json:",omitempty"`
Description string `json:",omitempty"`
Severity string `json:",omitempty"`
RecommendedActions string `json:",omitempty" mapstructure:"recommended_actions"`
References []string `json:",omitempty"`
}
type PolicyInputOption struct {
Combine bool `mapstructure:"combine"`
Selectors []PolicyInputSelector `mapstructure:"selector"`
}
type PolicyInputSelector struct {
Type string `mapstructure:"type"`
}
func (r MisconfResults) Len() int {
return len(r)
}
func (r MisconfResults) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r MisconfResults) Less(i, j int) bool {
switch {
case r[i].Type != r[j].Type:
return r[i].Type < r[j].Type
case r[i].ID != r[j].ID:
return r[i].ID < r[j].ID
case r[i].Severity != r[j].Severity:
return r[i].Severity < r[j].Severity
}
return r[i].Message < r[j].Message
}
func ToMisconfigurations(misconfs map[string]Misconfiguration) []Misconfiguration {
var results []Misconfiguration
for _, misconf := range misconfs {
// Remove duplicates
misconf.Successes = uniqueResults(misconf.Successes)
// Sort results
sort.Sort(misconf.Successes)
sort.Sort(misconf.Warnings)
sort.Sort(misconf.Failures)
sort.Sort(misconf.Exceptions)
results = append(results, misconf)
}
// Sort misconfigurations
sort.Slice(results, func(i, j int) bool {
if results[i].FileType != results[j].FileType {
return results[i].FileType < results[j].FileType
}
return results[i].FilePath < results[j].FilePath
})
return results
}
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)
}
return uniqResults
}