forked from yanivagman/tracee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_test.go
346 lines (318 loc) · 9.33 KB
/
output_test.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/aquasecurity/tracee/types/detect"
"github.com/aquasecurity/tracee/types/protocol"
"github.com/aquasecurity/tracee/types/trace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fakeSignature struct {
detect.Signature
getMetadata func() (detect.SignatureMetadata, error)
getSelectedEvents func() ([]detect.SignatureEventSelector, error)
}
func (f fakeSignature) GetMetadata() (detect.SignatureMetadata, error) {
if f.getMetadata != nil {
return f.getMetadata()
}
return detect.SignatureMetadata{
ID: "FOO-666",
Name: "foo bar signature",
Description: "the most evil",
}, nil
}
func (f fakeSignature) GetSelectedEvents() ([]detect.SignatureEventSelector, error) {
if f.getSelectedEvents != nil {
return f.getSelectedEvents()
}
return []detect.SignatureEventSelector{
{
Source: "tracee",
Name: "execve",
Origin: "foobar",
},
{
Source: "tracee",
Name: "ptrace",
Origin: "bazfoo",
},
}, nil
}
func Test_setupOutput(t *testing.T) {
var testCases = []struct {
name string
inputEvent protocol.Event
outputFormat string
expectedOutput string
}{
{
name: "happy path with tracee event and default output",
inputEvent: trace.Event{
ProcessName: "foobar.exe",
HostName: "foobar.local",
}.ToProtocol(),
expectedOutput: `
*** Detection ***
Time: 2021-02-23T01:54:57Z
Signature ID: FOO-666
Signature: foo bar signature
Data: map[foo1:bar1, baz1 foo2:[bar2 baz2]]
Command: foobar.exe
Hostname: foobar.local
`,
},
{
name: "happy path with tracee event and simple custom output template",
inputEvent: trace.Event{
ProcessName: "foobar.exe",
HostName: "foobar.local",
}.ToProtocol(),
expectedOutput: `*** Detection ***
Timestamp: 2021-02-23T01:54:57Z
ProcessName: foobar.exe
HostName: foobar.local
`,
outputFormat: "templates/simple.tmpl",
},
{
name: "sad path with unknown event",
inputEvent: protocol.Event{
Headers: protocol.EventHeaders{
ContentType: "application/wrong",
Origin: "/untrusted",
},
Payload: "something wrong",
},
expectedOutput: ``,
},
{
name: "sad path with invalid custom template",
inputEvent: trace.Event{
ProcessName: "foobar.exe",
HostName: "foobar.local",
}.ToProtocol(),
outputFormat: "goldens/broken.tmpl",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualOutput := NewSyncBuffer([]byte{})
findingCh, err := setupOutput(actualOutput, "", "", "", tc.outputFormat)
require.NoError(t, err, tc.name)
sm, err := fakeSignature{}.GetMetadata()
require.NoError(t, err)
findingCh <- detect.Finding{
Data: map[string]interface{}{
"foo1": "bar1, baz1",
"foo2": []string{"bar2", "baz2"},
},
Event: tc.inputEvent,
SigMetadata: sm,
}
time.Sleep(time.Millisecond)
checkOutput(t, tc.name, actualOutput, tc.expectedOutput)
})
}
}
func checkOutput(t *testing.T, testName string, actualOutput *SyncBuffer, expectedOutput string) {
got := strings.Split(actualOutput.String(), "\n")
for _, g := range got {
switch {
case strings.Contains(g, "Time"):
_, err := time.Parse("2006-01-02T15:04:05Z", strings.Split(g, " ")[1])
assert.NoError(t, err, testName) // check if time is parsable
case strings.Contains(g, "time"):
var gotPayload struct {
Time time.Time `json:"time"`
}
err := json.Unmarshal([]byte(g), &gotPayload)
assert.NoError(t, err, testName)
assert.NotEmpty(t, gotPayload, testName) // check if time is parsable
default:
assert.Contains(t, expectedOutput, g, testName)
}
}
}
func Test_sendToWebhook(t *testing.T) {
var testCases = []struct {
name string
inputTemplateFile string
inputSignature fakeSignature
inputTestServerURL string
contentType string
expectedOutput string
expectedError string
}{
{
name: "happy path with falcosidekick template",
contentType: "application/json",
expectedOutput: `{"output":"Rule \"foo bar signature\" detection:\n map[foo1:bar1, baz1 foo2:[bar2 baz2]]","rule":"foo bar signature","time":"2021-02-23T01:54:57Z","output_fields":{"value":0}}`,
inputTemplateFile: "templates/falcosidekick.tmpl",
},
{
name: "happy path, with simple template",
contentType: "text/plain",
expectedOutput: `*** Detection ***
Timestamp: 2021-02-23T01:54:57Z
ProcessName: foobar.exe
HostName: foobar.local
`,
inputTemplateFile: "templates/simple.tmpl",
},
{
name: "happy path with functions from sprig template",
contentType: "text/plain",
expectedOutput: `{
"foo1": "bar1, baz1",
"foo2": [
"bar2",
"baz2"
]
}`,
inputTemplateFile: "templates/sprig.tmpl",
},
{
name: "sad path, error reaching webhook",
inputTestServerURL: "foo://bad.host",
expectedError: `error calling webhook Post "foo://bad.host": unsupported protocol scheme "foo"`,
inputTemplateFile: "templates/simple.tmpl",
},
{
name: "sad path, with missing template",
inputTemplateFile: "invalid/template",
expectedError: `error writing to template: template not initialized`,
},
{
name: "sad path, with an invalid template",
contentType: "application/foo",
inputTemplateFile: "goldens/broken.tmpl",
expectedError: `error writing to the template: template: broken.tmpl:1:3: executing "broken.tmpl" at <.InvalidField>: can't evaluate field InvalidField in type detect.Finding`,
},
{
name: "sad path, no --webhook-template flag specified",
expectedError: `error sending to webhook: --webhook-template flag is required when using --webhook flag`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
got, err := ioutil.ReadAll(request.Body)
require.NoError(t, err)
checkOutput(t, tc.name, NewSyncBuffer(got), tc.expectedOutput)
assert.Equal(t, tc.contentType, request.Header.Get("content-type"), tc.name)
}))
defer ts.Close()
if tc.inputTestServerURL != "" {
ts.URL = tc.inputTestServerURL
}
inputTemplate, _ := setupTemplate(tc.inputTemplateFile)
m, _ := tc.inputSignature.GetMetadata()
actualError := sendToWebhook(inputTemplate, detect.Finding{
Data: map[string]interface{}{
"foo1": "bar1, baz1",
"foo2": []string{"bar2", "baz2"},
},
Event: trace.Event{
ProcessName: "foobar.exe",
HostName: "foobar.local",
}.ToProtocol(),
SigMetadata: m,
}, ts.URL, tc.inputTemplateFile, tc.contentType)
switch {
case tc.expectedError != "":
assert.EqualError(t, actualError, tc.expectedError, tc.name)
default:
assert.NoError(t, actualError, tc.name)
}
})
}
}
func TestOutputTemplates(t *testing.T) {
testCases := []struct {
testName string
finding detect.Finding
expectedJson string
}{
{
testName: "Should output finding as raw JSON",
finding: detect.Finding{
Data: map[string]interface{}{
"a": 123,
"b": "c",
"d": true,
"f": map[string]string{
"123": "456",
"foo": "bar",
},
},
Event: trace.Event{
ProcessID: 21312,
Timestamp: 1321321,
UserID: 0,
ContainerID: "abbc123",
EventName: "execve",
}.ToProtocol(),
SigMetadata: detect.SignatureMetadata{
ID: "TRC-1",
Version: "0.1.0",
Name: "Standard Input/Output Over Socket",
Description: "Redirection of process's standard input/output to socket",
Tags: []string{"linux", "container"},
Properties: map[string]interface{}{
"Severity": 3,
"MITRE ATT&CK": "Persistence: Server Software Component",
},
},
},
expectedJson: `{
"Data": {
"a":123,"b":"c","d":true,"f":{"123":"456","foo":"bar"}
},
"Context":{
"timestamp":1321321,"processorId":0,"processId":21312,"threadId":0,"parentProcessId":0,"hostProcessId":0,"hostThreadId":0,"hostParentProcessId":0,"userId":0,"mountNamespace":0,"pidNamespace":0,"processName":"","hostName":"","containerId":"abbc123","eventId":"0","eventName":"execve","argsNum":0,"returnValue":0,"stackAddresses":null,"args":null
},
"SigMetadata":{
"ID":"TRC-1","Version":"0.1.0","Name":"Standard Input/Output Over Socket","Description":"Redirection of process's standard input/output to socket","Tags":["linux","container"],"Properties":{"MITRE ATT\u0026CK":"Persistence: Server Software Component","Severity":3}
}
}`,
},
}
jsonTemplate, err := setupTemplate("templates/rawjson.tmpl")
require.NoError(t, err)
for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
var buf bytes.Buffer
err := jsonTemplate.Execute(&buf, tc.finding)
require.NoError(t, err)
assert.JSONEq(t, tc.expectedJson, buf.String())
})
}
}
type SyncBuffer struct {
b *bytes.Buffer
m sync.Mutex
}
func (b *SyncBuffer) Write(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Write(p)
}
func (b *SyncBuffer) String() string {
b.m.Lock()
defer b.m.Unlock()
return b.b.String()
}
func NewSyncBuffer(buf []byte) *SyncBuffer {
return &SyncBuffer{
b: bytes.NewBuffer(buf),
}
}