forked from bitflipp/gerberos
/
sources.go
161 lines (123 loc) · 2.7 KB
/
sources.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
package gerberos
import (
"errors"
"fmt"
"os"
)
type Source interface {
Initialize(r *Rule) error
Matches() (chan *Match, error)
}
var sources = map[string]func() Source{
"file": NewFileSource,
"systemd": NewSystemdSource,
"kernel": NewKernelSource,
"test": NewTestSource,
"process": NewProcessSource,
}
func RegisterSource(name string, fn func() Source) {
sources[name] = fn
}
func NewFileSource() Source {
return &fileSource{}
}
func NewSystemdSource() Source {
return &systemdSource{}
}
func NewKernelSource() Source {
return &kernelSource{}
}
func NewTestSource() Source {
return &testSource{}
}
func NewProcessSource() Source {
return &processSource{}
}
type fileSource struct {
Rule *Rule
path string
}
func (s *fileSource) Initialize(r *Rule) error {
s.Rule = r
if len(r.Source) < 2 {
return errors.New("missing path parameter")
}
s.path = r.Source[1]
if fi, err := os.Stat(s.path); err == nil && fi.IsDir() {
return fmt.Errorf(`"%s" is a directory`, s.path)
}
if len(r.Source) > 2 {
return errors.New("superfluous parameter(s)")
}
return nil
}
func (s *fileSource) Matches() (chan *Match, error) {
return s.Rule.ProcessScanner("tail", "-n", "0", "-F", s.path)
}
type systemdSource struct {
Rule *Rule
service string
}
func (s *systemdSource) Initialize(r *Rule) error {
s.Rule = r
if len(r.Source) < 2 {
return errors.New("missing service parameter")
}
s.service = r.Source[1]
if len(r.Source) > 2 {
return errors.New("superfluous parameter(s)")
}
return nil
}
func (s *systemdSource) Matches() (chan *Match, error) {
return s.Rule.ProcessScanner("journalctl", "-n", "0", "-f", "-u", s.service)
}
type kernelSource struct {
Rule *Rule
}
func (k *kernelSource) Initialize(r *Rule) error {
k.Rule = r
if len(r.Source) > 1 {
return errors.New("superfluous parameter(s)")
}
return nil
}
func (k *kernelSource) Matches() (chan *Match, error) {
return k.Rule.ProcessScanner("journalctl", "-kf", "-n", "0")
}
type testSource struct {
Rule *Rule
matchesErr error
processPath string
}
func (s *testSource) Initialize(r *Rule) error {
s.Rule = r
return nil
}
func (s *testSource) Matches() (chan *Match, error) {
if s.matchesErr != nil {
return nil, s.matchesErr
}
p := "test/producer"
if s.processPath != "" {
p = s.processPath
}
return s.Rule.ProcessScanner(p)
}
type processSource struct {
Rule *Rule
name string
args []string
}
func (s *processSource) Initialize(r *Rule) error {
s.Rule = r
if len(r.Source) < 2 {
return errors.New("missing process name")
}
s.name = r.Source[1]
s.args = r.Source[2:]
return nil
}
func (s *processSource) Matches() (chan *Match, error) {
return s.Rule.ProcessScanner(s.name, s.args...)
}