forked from aquasecurity/tracee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput_test.go
More file actions
229 lines (210 loc) · 6.2 KB
/
output_test.go
File metadata and controls
229 lines (210 loc) · 6.2 KB
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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/aquasecurity/tracee/tracee-ebpf/external"
"github.com/aquasecurity/tracee/tracee-rules/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fakeSignature struct {
types.Signature
getMetadata func() (types.SignatureMetadata, error)
}
func (f fakeSignature) GetMetadata() (types.SignatureMetadata, error) {
if f.getMetadata != nil {
return f.getMetadata()
}
return types.SignatureMetadata{
ID: "FOO-666",
Name: "foo bar signature",
Description: "the most evil",
}, nil
}
func Test_setupOutput(t *testing.T) {
var testCases = []struct {
name string
inputContext interface{}
outputFormat string
expectedOutput string
}{
{
name: "happy path with tracee event and default output",
inputContext: external.Event{
ProcessName: "foobar.exe",
HostName: "foobar.local",
},
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",
inputContext: external.Event{
ProcessName: "foobar.exe",
HostName: "foobar.local",
},
expectedOutput: `*** Detection ***
Timestamp: 2021-02-23T01:54:57Z
ProcessName: foobar.exe
HostName: foobar.local
`,
outputFormat: "templates/simple.tmpl",
},
{
name: "sad path with unknown context",
inputContext: struct {
foo string
}{foo: "bad input context"},
expectedOutput: ``,
},
{
name: "sad path with invalid custom template",
inputContext: external.Event{
ProcessName: "foobar.exe",
HostName: "foobar.local",
},
outputFormat: "goldens/broken.tmpl",
},
}
for _, tc := range testCases {
var actualOutput bytes.Buffer
findingCh, err := setupOutput(&actualOutput, "", "", "", tc.outputFormat)
require.NoError(t, err, tc.name)
sm, _ := fakeSignature{}.GetMetadata()
findingCh <- types.Finding{
Data: map[string]interface{}{
"foo1": "bar1, baz1",
"foo2": []string{"bar2", "baz2"},
},
Context: tc.inputContext,
SigMetadata: sm,
}
time.Sleep(time.Millisecond)
checkOutput(t, tc.name, actualOutput, tc.expectedOutput)
}
}
func checkOutput(t *testing.T, testName string, actualOutput bytes.Buffer, 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 types.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, _ := ioutil.ReadAll(request.Body)
checkOutput(t, tc.name, *bytes.NewBuffer(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, types.Finding{
Data: map[string]interface{}{
"foo1": "bar1, baz1",
"foo2": []string{"bar2", "baz2"},
},
Context: external.Event{
ProcessName: "foobar.exe",
HostName: "foobar.local",
},
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)
}
})
}
}