forked from yanivagman/tracee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.go
279 lines (238 loc) · 7.35 KB
/
printer.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
package main
import (
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"text/template"
"time"
"github.com/aquasecurity/tracee/pkg/metrics"
"github.com/aquasecurity/tracee/types/trace"
)
type eventPrinter interface {
// Init serves as the initializer method for every event Printer type
Init() error
// Preamble prints something before event printing begins (one time)
Preamble()
// Epilogue prints something after event printing ends (one time)
Epilogue(stats metrics.Stats)
// Print prints a single event
Print(event trace.Event)
// Error prints a single error
Error(err error)
// dispose of resources
Close()
}
func newEventPrinter(kind string, containerMode bool, relativeTS bool, out io.WriteCloser, err io.WriteCloser) (eventPrinter, error) {
var res eventPrinter
var initError error
switch {
case kind == "ignore":
res = &ignoreEventPrinter{
err: err,
}
case kind == "table":
res = &tableEventPrinter{
out: out,
err: err,
verbose: false,
containerMode: containerMode,
relativeTS: relativeTS,
}
case kind == "table-verbose":
res = &tableEventPrinter{
out: out,
err: err,
verbose: true,
containerMode: containerMode,
relativeTS: relativeTS,
}
case kind == "json":
res = &jsonEventPrinter{
out: out,
err: err,
}
case kind == "gob":
res = &gobEventPrinter{
out: out,
err: err,
}
case strings.HasPrefix(kind, "gotemplate="):
res = &templateEventPrinter{
out: out,
err: err,
containerMode: containerMode,
templatePath: strings.Split(kind, "=")[1],
}
}
initError = res.Init()
if initError != nil {
return nil, initError
}
return res, nil
}
type tableEventPrinter struct {
out io.WriteCloser
err io.WriteCloser
verbose bool
containerMode bool
relativeTS bool
}
func (p tableEventPrinter) Init() error { return nil }
func (p tableEventPrinter) Preamble() {
if p.verbose {
if p.containerMode {
fmt.Fprintf(p.out, "%-16s %-16s %-13s %-12s %-12s %-6s %-16s %-15s %-15s %-15s %-16s %-20s %s", "TIME", "UTS_NAME", "CONTAINER_ID", "MNT_NS", "PID_NS", "UID", "COMM", "PID/host", "TID/host", "PPID/host", "RET", "EVENT", "ARGS")
} else {
fmt.Fprintf(p.out, "%-16s %-16s %-13s %-12s %-12s %-6s %-16s %-7s %-7s %-7s %-16s %-20s %s", "TIME", "UTS_NAME", "CONTAINER_ID", "MNT_NS", "PID_NS", "UID", "COMM", "PID", "TID", "PPID", "RET", "EVENT", "ARGS")
}
} else {
if p.containerMode {
fmt.Fprintf(p.out, "%-16s %-13s %-6s %-16s %-15s %-15s %-16s %-20s %s", "TIME", "CONTAINER_ID", "UID", "COMM", "PID/host", "TID/host", "RET", "EVENT", "ARGS")
} else {
fmt.Fprintf(p.out, "%-16s %-6s %-16s %-7s %-7s %-16s %-20s %s", "TIME", "UID", "COMM", "PID", "TID", "RET", "EVENT", "ARGS")
}
}
fmt.Fprintln(p.out)
}
func (p tableEventPrinter) Print(event trace.Event) {
ut := time.Unix(0, int64(event.Timestamp))
if p.relativeTS {
ut = ut.UTC()
}
timestamp := fmt.Sprintf("%02d:%02d:%02d:%06d", ut.Hour(), ut.Minute(), ut.Second(), ut.Nanosecond()/1000)
containerId := event.ContainerID
if len(containerId) > 12 {
containerId = containerId[:12]
}
if p.verbose {
if p.containerMode {
fmt.Fprintf(p.out, "%-16s %-16s %-13s %-12d %-12d %-6d %-16s %-7d/%-7d %-7d/%-7d %-7d/%-7d %-16d %-20s ", timestamp, event.HostName, containerId, event.MountNS, event.PIDNS, event.UserID, event.ProcessName, event.ProcessID, event.HostProcessID, event.ThreadID, event.HostThreadID, event.ParentProcessID, event.HostParentProcessID, event.ReturnValue, event.EventName)
} else {
fmt.Fprintf(p.out, "%-16s %-16s %-13s %-12d %-12d %-6d %-16s %-7d %-7d %-7d %-16d %-20s ", timestamp, event.HostName, containerId, event.MountNS, event.PIDNS, event.UserID, event.ProcessName, event.ProcessID, event.ThreadID, event.ParentProcessID, event.ReturnValue, event.EventName)
}
} else {
if p.containerMode {
fmt.Fprintf(p.out, "%-16s %-13s %-6d %-16s %-7d/%-7d %-7d/%-7d %-16d %-20s ", timestamp, containerId, event.UserID, event.ProcessName, event.ProcessID, event.HostProcessID, event.ThreadID, event.HostThreadID, event.ReturnValue, event.EventName)
} else {
fmt.Fprintf(p.out, "%-16s %-6d %-16s %-7d %-7d %-16d %-20s ", timestamp, event.UserID, event.ProcessName, event.ProcessID, event.ThreadID, event.ReturnValue, event.EventName)
}
}
for i, arg := range event.Args {
if i == 0 {
fmt.Fprintf(p.out, "%s: %v", arg.Name, arg.Value)
} else {
fmt.Fprintf(p.out, ", %s: %v", arg.Name, arg.Value)
}
}
fmt.Fprintln(p.out)
}
func (p tableEventPrinter) Error(err error) {
fmt.Fprintf(p.err, "%v\n", err)
}
func (p tableEventPrinter) Epilogue(stats metrics.Stats) {
fmt.Println()
fmt.Fprintf(p.out, "End of events stream\n")
fmt.Fprintf(p.out, "Stats: %+v\n", stats)
}
func (p tableEventPrinter) Close() {
}
type templateEventPrinter struct {
out io.WriteCloser
err io.WriteCloser
containerMode bool
templatePath string
templateObj **template.Template
}
func (p *templateEventPrinter) Init() error {
tmplPath := p.templatePath
if tmplPath != "" {
tmpl, err := template.ParseFiles(tmplPath)
if err != nil {
return err
}
p.templateObj = &tmpl
} else {
return errors.New("Please specify a gotemplate for event-based output")
}
return nil
}
func (p templateEventPrinter) Preamble() {}
func (p templateEventPrinter) Error(err error) {
fmt.Fprintf(p.err, "%v\n", err)
}
func (p templateEventPrinter) Print(event trace.Event) {
if p.templateObj != nil {
err := (*p.templateObj).Execute(p.out, event)
if err != nil {
p.Error(err)
}
} else {
fmt.Fprintf(p.out, "Template Obj is nil")
}
}
func (p templateEventPrinter) Epilogue(stats metrics.Stats) {}
func (p templateEventPrinter) Close() {
}
type jsonEventPrinter struct {
out io.WriteCloser
err io.WriteCloser
}
func (p jsonEventPrinter) Init() error { return nil }
func (p jsonEventPrinter) Preamble() {}
func (p jsonEventPrinter) Print(event trace.Event) {
eBytes, err := json.Marshal(event)
if err != nil {
p.Error(err)
}
fmt.Fprintln(p.out, string(eBytes))
}
func (p jsonEventPrinter) Error(err error) {
fmt.Fprintf(p.err, "%v\n", err)
}
func (p jsonEventPrinter) Epilogue(stats metrics.Stats) {}
func (p jsonEventPrinter) Close() {
}
// gobEventPrinter is printing events using golang's builtin Gob serializer
type gobEventPrinter struct {
out io.WriteCloser
err io.WriteCloser
outEnc *gob.Encoder
}
func (p *gobEventPrinter) Init() error {
p.outEnc = gob.NewEncoder(p.out)
gob.Register(trace.Event{})
gob.Register(trace.SlimCred{})
gob.Register(make(map[string]string))
gob.Register(trace.PktMeta{})
return nil
}
func (p *gobEventPrinter) Preamble() {}
func (p *gobEventPrinter) Print(event trace.Event) {
err := p.outEnc.Encode(event)
if err != nil {
p.Error(err)
}
}
func (p *gobEventPrinter) Error(err error) {
fmt.Fprintf(p.err, "%v\n", err)
}
func (p *gobEventPrinter) Epilogue(stats metrics.Stats) {}
func (p gobEventPrinter) Close() {
}
// ignoreEventPrinter ignores events
type ignoreEventPrinter struct {
err io.WriteCloser
}
func (p *ignoreEventPrinter) Init() error {
return nil
}
func (p *ignoreEventPrinter) Preamble() {}
func (p *ignoreEventPrinter) Print(event trace.Event) {}
func (p *ignoreEventPrinter) Error(err error) {
fmt.Fprintf(p.err, "%v\n", err)
}
func (p *ignoreEventPrinter) Epilogue(stats metrics.Stats) {}
func (p ignoreEventPrinter) Close() {}