-
Notifications
You must be signed in to change notification settings - Fork 4
/
exfil.go
155 lines (137 loc) · 4.03 KB
/
exfil.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package limacharlie
import "encoding/json"
type ExfilRuleName = string
type ExfilRulesType struct {
Performance Dict `json:"perf,omitempty" yaml:"perf,omitempty"`
Events map[ExfilRuleName]ExfilRuleEvent `json:"list,omitempty" yaml:"list,omitempty"`
Watches map[ExfilRuleName]ExfilRuleWatch `json:"watch,omitempty" yaml:"watch,omitempty"`
}
type ExfilEventFilters struct {
Tags []string `json:"tags" yaml:"tags"`
Platforms []string `json:"platforms" yaml:"platforms"`
}
type ExfilRuleEvent struct {
LastUpdated uint64 `json:"updated,omitempty" yaml:"updated,omitempty"`
CreatedBy string `json:"by,omitempty" yaml:"by,omitempty"`
Events []string `json:"events" yaml:"events"`
Filters ExfilEventFilters `json:"filters" yaml:"filters"`
}
func (r ExfilRuleEvent) jsonMarhsalContent() ([]byte, error) {
if r.Filters.Platforms == nil {
r.Filters.Platforms = []string{}
}
if r.Filters.Tags == nil {
r.Filters.Tags = []string{}
}
return json.Marshal(Dict{
"events": r.Events,
"filters": r.Filters,
})
}
func (r ExfilRuleEvent) EqualsContent(other ExfilRuleEvent) bool {
bytes, err := r.jsonMarhsalContent()
if err != nil {
return false
}
otherBytes, err := other.jsonMarhsalContent()
if err != nil {
return false
}
return string(bytes) == string(otherBytes)
}
func (org Organization) exfil(responseData interface{}, action string, req Dict) error {
reqData := req
reqData["action"] = action
return org.client.serviceRequest(responseData, "exfil", reqData, false)
}
func (org Organization) ExfilRules() (ExfilRulesType, error) {
rules := ExfilRulesType{}
if err := org.exfil(&rules, "list_rules", Dict{}); err != nil {
return ExfilRulesType{}, err
}
return rules, nil
}
func (org Organization) ExfilRuleEventAdd(name ExfilRuleName, event ExfilRuleEvent) error {
tags := event.Filters.Tags
if tags == nil {
tags = []string{}
}
platforms := event.Filters.Platforms
if platforms == nil {
platforms = []string{}
}
data := Dict{
"name": name,
"events": event.Events,
"tags": tags,
"platforms": platforms,
}
resp := Dict{}
return org.exfil(&resp, "add_event_rule", data)
}
func (org Organization) ExfilRuleEventDelete(name ExfilRuleName) error {
resp := Dict{}
return org.exfil(&resp, "remove_event_rule", Dict{"name": name})
}
type ExfilRuleWatch struct {
LastUpdated uint64 `json:"updated,omitempty" yaml:"updated,omitempty"`
CreatedBy string `json:"by,omitempty" yaml:"by,omitempty"`
Event string `json:"event" yaml:"event"`
Value string `json:"value" yaml:"value"`
Path []string `json:"path" yaml:"path"`
Operator string `json:"operator" yaml:"operator"`
Filters ExfilEventFilters `json:"filters" yaml:"filters"`
}
func (r ExfilRuleWatch) jsonMarhsalContent() ([]byte, error) {
if r.Path == nil {
r.Path = []string{}
}
if r.Filters.Platforms == nil {
r.Filters.Platforms = []string{}
}
if r.Filters.Tags == nil {
r.Filters.Tags = []string{}
}
return json.Marshal(Dict{
"event": r.Event,
"value": r.Value,
"path": r.Path,
"operator": r.Operator,
"filters": r.Filters,
})
}
func (r ExfilRuleWatch) EqualsContent(other ExfilRuleWatch) bool {
bytes, err := r.jsonMarhsalContent()
if err != nil {
return false
}
otherBytes, err := other.jsonMarhsalContent()
if err != nil {
return false
}
return string(bytes) == string(otherBytes)
}
func (org Organization) ExfilRuleWatchAdd(name ExfilRuleName, watch ExfilRuleWatch) error {
tags := watch.Filters.Tags
if tags == nil {
tags = []string{}
}
platforms := watch.Filters.Platforms
if platforms == nil {
platforms = []string{}
}
resp := Dict{}
return org.exfil(&resp, "add_watch", Dict{
"name": name,
"operator": watch.Operator,
"event": watch.Event,
"value": watch.Value,
"path": watch.Path,
"tags": tags,
"platforms": platforms,
})
}
func (org Organization) ExfilRuleWatchDelete(name ExfilRuleName) error {
resp := Dict{}
return org.exfil(&resp, "remove_watch", Dict{"name": name})
}