This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
/
filter.go
268 lines (217 loc) · 7.28 KB
/
filter.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package protocol
import (
"encoding/json"
"fmt"
"sync"
"github.com/choria-io/go-protocol/filter/agents"
"github.com/choria-io/go-protocol/filter/classes"
"github.com/choria-io/go-protocol/filter/facts"
"github.com/choria-io/go-protocol/filter/identity"
)
// CompoundFilter is a mcollective compound filter
type CompoundFilter []map[string]interface{}
// CompoundFilters is a set of mcollective compound filters
type CompoundFilters []CompoundFilter
// FactFilter is how a fact match is represented to the Filter
type FactFilter struct {
Fact string `json:"fact"`
Operator string `json:"operator"`
Value string `json:"value"`
}
// Filter is a MCollective filter
type Filter struct {
Fact []FactFilter `json:"fact"`
Class []string `json:"cf_class"`
Agent []string `json:"agent"`
Identity []string `json:"identity"`
Compound CompoundFilters `json:"compound"`
mu sync.Mutex
}
// NewFilter creates a new empty filter
func NewFilter() *Filter {
filter := &Filter{
Fact: []FactFilter{},
Class: []string{},
Agent: []string{},
Identity: []string{},
Compound: CompoundFilters{},
}
return filter
}
// Logger provides logging facilities
type Logger interface {
Warnf(format string, args ...interface{})
Debugf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
// MatchRequest determines if a request matches the filter
func (f *Filter) MatchRequest(request Request, agents []string, identity string, classesFile string, factsFile string, log Logger) bool {
filter, _ := request.Filter()
passed := 0
if filter.Empty() {
log.Debugf("Matching request %s with empty filter", request.RequestID())
return true
}
if len(filter.ClassFilters()) > 0 {
if filter.MatchClassesFile(classesFile, log) {
log.Debugf("Matching request %s with class filters '%#v'", request.RequestID(), filter.ClassFilters())
passed++
} else {
log.Debugf("Not matching request %s with class filters '%#v'", request.RequestID(), filter.ClassFilters())
return false
}
}
if len(filter.AgentFilters()) > 0 {
if filter.MatchAgents(agents) {
log.Debugf("Matching request %s with agent filters '%#v'", request.RequestID(), filter.AgentFilters())
passed++
} else {
log.Debugf("Not matching request %s with agent filters '%#v'", request.RequestID(), filter.AgentFilters())
return false
}
}
if len(filter.IdentityFilters()) > 0 {
if filter.MatchIdentity(identity) {
log.Debugf("Matching request %s with identity filters '%#v'", request.RequestID(), filter.IdentityFilters())
passed++
} else {
log.Debugf("Not matching request %s with identity filters '%#v'", request.RequestID(), filter.IdentityFilters())
return false
}
}
if len(filter.FactFilters()) > 0 {
if filter.MatchFactsFile(factsFile, log) {
log.Debugf("Matching request %s based on fact filters '%#v'", request.RequestID(), filter.FactFilters())
passed++
} else {
log.Debugf("Not matching request %s based on fact filters '%#v'", request.RequestID(), filter.FactFilters())
return false
}
}
if len(filter.CompoundFilters()) > 0 {
log.Warnf("Compound filters are not supported, not matching request %s with filter '%#v'", request.RequestID(), filter.CompoundFilters())
return false
}
return passed > 0
}
// MatchFactsFile determines if the filter would match a given set of facts found in a file
func (f *Filter) MatchFactsFile(file string, log Logger) bool {
return facts.MatchFile(f.FactFilters(), file, log)
}
// MatchFacts determines if the filter would match a given set of facts found in given JSON data
func (f *Filter) MatchFacts(factsj json.RawMessage, log Logger) bool {
return facts.MatchFacts(f.FactFilters(), factsj, log)
}
// MatchAgents determines if the filter would match a list of agents
func (f *Filter) MatchAgents(knownAgents []string) bool {
return agents.Match(f.AgentFilters(), knownAgents)
}
// MatchIdentity determines if the filter would match a given identity
func (f *Filter) MatchIdentity(ident string) bool {
return identity.Match(f.IdentityFilters(), ident)
}
// MatchClassesFile determines if the filter would match a list of classes
func (f *Filter) MatchClassesFile(file string, log Logger) bool {
return classes.MatchFile(f.ClassFilters(), file, log)
}
// MatchClasses determines if the filter would match against the list of classes
func (f *Filter) MatchClasses(knownClasses []string, log Logger) bool {
return classes.Match(f.ClassFilters(), knownClasses, log)
}
// Empty determines if a filter is empty - that is all its contained filter arrays are empty
func (f *Filter) Empty() bool {
if f.Fact == nil && f.Class == nil && f.Agent == nil && f.Identity == nil && f.Compound == nil {
return true
}
if len(f.Fact) == 0 && len(f.Class) == 0 && len(f.Agent) == 0 && len(f.Identity) == 0 && len(f.Compound) == 0 {
return true
}
return false
}
// CompoundFilters retrieve the list of compound filters
func (f *Filter) CompoundFilters() CompoundFilters {
return f.Compound
}
// IdentityFilters retrieve the list of identity filters
func (f *Filter) IdentityFilters() []string {
return f.Identity
}
// AgentFilters retrieve the list of agent filters
func (f *Filter) AgentFilters() []string {
return f.Agent
}
// ClassFilters retrieve the list of class filters
func (f *Filter) ClassFilters() []string {
return f.Class
}
// FactFilters retrieve the list of fact filters
func (f *Filter) FactFilters() [][3]string {
var filter [][3]string
filter = [][3]string{}
for _, f := range f.Fact {
filter = append(filter, [3]string{f.Fact, f.Operator, f.Value})
}
return filter
}
// AddCompoundFilter appends a filter to the compound filters,
// the filter should be a JSON string representing a valid mcollective
// compound filter as parsed by MCollective::Matcher.create_compound_callstack
func (f *Filter) AddCompoundFilter(query string) error {
f.mu.Lock()
defer f.mu.Unlock()
var cf CompoundFilter
err := json.Unmarshal([]byte(query), &cf)
if err != nil {
return fmt.Errorf("could not parse query as JSON: %s", err)
}
f.Compound = append(f.Compound, cf)
return nil
}
// AddIdentityFilter appends a filter to the identity filters
func (f *Filter) AddIdentityFilter(id string) {
f.mu.Lock()
defer f.mu.Unlock()
if !f.contains(id, f.Identity) {
f.Identity = append(f.Identity, id)
}
}
// AddAgentFilter appends a filter to the agent filters
func (f *Filter) AddAgentFilter(agent string) {
f.mu.Lock()
defer f.mu.Unlock()
if !f.contains(agent, f.Agent) {
f.Agent = append(f.Agent, agent)
}
}
// AddClassFilter appends a filter to the class filters
func (f *Filter) AddClassFilter(class string) {
f.mu.Lock()
defer f.mu.Unlock()
if !f.contains(class, f.Class) {
f.Class = append(f.Class, class)
}
}
// AddFactFilter appends a filter to the fact filters
func (f *Filter) AddFactFilter(fact string, operator string, value string) (err error) {
f.mu.Lock()
defer f.mu.Unlock()
if !f.contains(operator, []string{">=", "<=", "<", ">", "!=", "==", "=~"}) {
err = fmt.Errorf("%s is not a valid fact operator", operator)
return
}
filter := FactFilter{
Fact: fact,
Operator: operator,
Value: value,
}
f.Fact = append(f.Fact, filter)
return
}
func (f *Filter) contains(needle string, haystack []string) bool {
for _, i := range haystack {
if i == needle {
return true
}
}
return false
}