-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathconfig.go
291 lines (250 loc) · 5.89 KB
/
config.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package config
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"regexp"
"strings"
"text/template"
"time"
common "github.com/ncabatoff/process-exporter"
"gopkg.in/yaml.v2"
)
type (
Matcher interface {
// Match returns empty string for no match, or the group name on success.
Match(common.ProcAttributes) bool
}
FirstMatcher struct {
matchers []common.MatchNamer
}
commMatcher struct {
comms map[string]struct{}
}
exeMatcher struct {
exes map[string]string
}
cmdlineMatcher struct {
regexes []*regexp.Regexp
captures map[string]string
}
andMatcher []Matcher
templateNamer struct {
template *template.Template
}
matchNamer struct {
andMatcher
templateNamer
}
templateParams struct {
Cgroups []string
Comm string
ExeBase string
ExeFull string
Username string
PID int
StartTime time.Time
Matches map[string]string
}
)
func (c *cmdlineMatcher) String() string {
return fmt.Sprintf("cmdlines: %+v", c.regexes)
}
func (e *exeMatcher) String() string {
return fmt.Sprintf("exes: %+v", e.exes)
}
func (c *commMatcher) String() string {
var comms = make([]string, 0, len(c.comms))
for cm := range c.comms {
comms = append(comms, cm)
}
return fmt.Sprintf("comms: %+v", comms)
}
func (f FirstMatcher) String() string {
return fmt.Sprintf("%v", f.matchers)
}
func (f FirstMatcher) MatchAndName(nacl common.ProcAttributes) (bool, string) {
for _, m := range f.matchers {
if matched, name := m.MatchAndName(nacl); matched {
return true, name
}
}
return false, ""
}
func (m *matchNamer) String() string {
return fmt.Sprintf("%+v", m.andMatcher)
}
func (m *matchNamer) MatchAndName(nacl common.ProcAttributes) (bool, string) {
if !m.Match(nacl) {
return false, ""
}
matches := make(map[string]string)
for _, m := range m.andMatcher {
if mc, ok := m.(*cmdlineMatcher); ok {
for k, v := range mc.captures {
matches[k] = v
}
}
}
exebase, exefull := nacl.Name, nacl.Name
if len(nacl.Cmdline) > 0 {
exefull = nacl.Cmdline[0]
exebase = filepath.Base(exefull)
}
var buf bytes.Buffer
m.template.Execute(&buf, &templateParams{
Comm: nacl.Name,
Cgroups: nacl.Cgroups,
ExeBase: exebase,
ExeFull: exefull,
Matches: matches,
Username: nacl.Username,
PID: nacl.PID,
StartTime: nacl.StartTime,
})
return true, buf.String()
}
func (m *commMatcher) Match(nacl common.ProcAttributes) bool {
_, found := m.comms[nacl.Name]
return found
}
func (m *exeMatcher) Match(nacl common.ProcAttributes) bool {
if len(nacl.Cmdline) == 0 {
return false
}
thisbase := filepath.Base(nacl.Cmdline[0])
fqpath, found := m.exes[thisbase]
if !found {
return false
}
if fqpath == "" {
return true
}
return fqpath == nacl.Cmdline[0]
}
func (m *cmdlineMatcher) Match(nacl common.ProcAttributes) bool {
for _, regex := range m.regexes {
captures := regex.FindStringSubmatch(strings.Join(nacl.Cmdline, " "))
if m.captures == nil {
return false
}
subexpNames := regex.SubexpNames()
if len(subexpNames) != len(captures) {
return false
}
for i, name := range subexpNames {
m.captures[name] = captures[i]
}
}
return true
}
func (m andMatcher) Match(nacl common.ProcAttributes) bool {
for _, matcher := range m {
if !matcher.Match(nacl) {
return false
}
}
return true
}
type Config struct {
MatchNamers FirstMatcher
}
func (c *Config) UnmarshalYAML(unmarshal func(v interface{}) error) error {
type (
root struct {
Matchers MatcherRules `yaml:"process_names"`
}
)
var r root
if err := unmarshal(&r); err != nil {
return err
}
cfg, err := r.Matchers.ToConfig()
if err != nil {
return err
}
*c = *cfg
return nil
}
type MatcherGroup struct {
Name string `yaml:"name"`
CommRules []string `yaml:"comm"`
ExeRules []string `yaml:"exe"`
CmdlineRules []string `yaml:"cmdline"`
}
type MatcherRules []MatcherGroup
func (r MatcherRules) ToConfig() (*Config, error) {
var cfg Config
for _, matcher := range r {
var matchers andMatcher
if matcher.CommRules != nil {
comms := make(map[string]struct{})
for _, c := range matcher.CommRules {
comms[c] = struct{}{}
}
matchers = append(matchers, &commMatcher{comms})
}
if matcher.ExeRules != nil {
exes := make(map[string]string)
for _, e := range matcher.ExeRules {
if strings.Contains(e, "/") {
exes[filepath.Base(e)] = e
} else {
exes[e] = ""
}
}
matchers = append(matchers, &exeMatcher{exes})
}
if matcher.CmdlineRules != nil {
var rs []*regexp.Regexp
for _, c := range matcher.CmdlineRules {
r, err := regexp.Compile(c)
if err != nil {
return nil, fmt.Errorf("bad cmdline regex %q: %v", c, err)
}
rs = append(rs, r)
}
matchers = append(matchers, &cmdlineMatcher{
regexes: rs,
captures: make(map[string]string),
})
}
if len(matchers) == 0 {
return nil, fmt.Errorf("no matchers provided")
}
nametmpl := matcher.Name
if nametmpl == "" {
nametmpl = "{{.ExeBase}}"
}
tmpl := template.New("cmdname")
tmpl, err := tmpl.Parse(nametmpl)
if err != nil {
return nil, fmt.Errorf("bad name template %q: %v", nametmpl, err)
}
matchNamer := &matchNamer{matchers, templateNamer{tmpl}}
cfg.MatchNamers.matchers = append(cfg.MatchNamers.matchers, matchNamer)
}
return &cfg, nil
}
// ReadRecipesFile opens the named file and extracts recipes from it.
func ReadFile(cfgpath string, debug bool) (*Config, error) {
content, err := ioutil.ReadFile(cfgpath)
if err != nil {
return nil, fmt.Errorf("error reading config file %q: %v", cfgpath, err)
}
if debug {
log.Printf("Config file %q contents:\n%s", cfgpath, content)
}
return GetConfig(string(content), debug)
}
// GetConfig extracts Config from content by parsing it as YAML.
func GetConfig(content string, debug bool) (*Config, error) {
var cfg Config
err := yaml.Unmarshal([]byte(content), &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}