This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathteleport_events_watcher_test.go
416 lines (363 loc) · 10.6 KB
/
teleport_events_watcher_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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
Copyright 2015-2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"strconv"
"sync"
"testing"
"time"
"github.com/gravitational/teleport/api/client/proto"
auditlogpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/auditlog/v1"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/trace"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)
// mockTeleportEventWatcher is Teleport client mock
type mockTeleportEventWatcher struct {
mu sync.Mutex
// events is the mock list of events
events []events.AuditEvent
// mockSearchErr is an error to return
mockSearchErr error
}
func (c *mockTeleportEventWatcher) setEvents(events []events.AuditEvent) {
c.mu.Lock()
defer c.mu.Unlock()
c.events = events
}
func (c *mockTeleportEventWatcher) setSearchEventsError(err error) {
c.mu.Lock()
defer c.mu.Unlock()
c.mockSearchErr = err
}
func (c *mockTeleportEventWatcher) SearchEvents(ctx context.Context, fromUTC, toUTC time.Time, namespace string, eventTypes []string, limit int, order types.EventOrder, startKey string) ([]events.AuditEvent, string, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.mockSearchErr != nil {
return nil, "", c.mockSearchErr
}
var startIndex int
if startKey != "" {
startIndex, _ = strconv.Atoi(startKey)
}
endIndex := startIndex + limit
if endIndex >= len(c.events) {
endIndex = len(c.events)
}
// Get the next page
e := c.events[startIndex:endIndex]
// Check if we finished the page
var lastKey string
if len(e) == limit {
lastKey = strconv.Itoa(startIndex + (len(e) - 1))
}
return e, lastKey, nil
}
func (c *mockTeleportEventWatcher) StreamSessionEvents(ctx context.Context, sessionID string, startIndex int64) (chan events.AuditEvent, chan error) {
return nil, nil
}
func (c *mockTeleportEventWatcher) SearchUnstructuredEvents(ctx context.Context, fromUTC, toUTC time.Time, namespace string, eventTypes []string, limit int, order types.EventOrder, startKey string) ([]*auditlogpb.EventUnstructured, string, error) {
events, lastKey, err := c.SearchEvents(ctx, fromUTC, toUTC, namespace, eventTypes, limit, order, startKey)
if err != nil {
return nil, "", trace.Wrap(err)
}
protoEvents, err := eventsToProto(events)
if err != nil {
return nil, "", trace.Wrap(err)
}
return protoEvents, lastKey, nil
}
func (c *mockTeleportEventWatcher) StreamUnstructuredSessionEvents(ctx context.Context, sessionID string, startIndex int64) (chan *auditlogpb.EventUnstructured, chan error) {
return nil, nil
}
func (c *mockTeleportEventWatcher) UpsertLock(ctx context.Context, lock types.Lock) error {
return nil
}
func (c *mockTeleportEventWatcher) Ping(ctx context.Context) (proto.PingResponse, error) {
return proto.PingResponse{
ServerVersion: Version,
}, nil
}
func (c *mockTeleportEventWatcher) Close() error {
return nil
}
func newTeleportEventWatcher(t *testing.T, eventsClient TeleportSearchEventsClient) *TeleportEventsWatcher {
client := &TeleportEventsWatcher{
client: eventsClient,
pos: -1,
config: &StartCmdConfig{
IngestConfig: IngestConfig{
BatchSize: 5,
ExitOnLastEvent: true,
},
},
}
return client
}
func TestEvents(t *testing.T) {
ctx := context.Background()
// create fake audit events with ids 0-19
testAuditEvents := make([]events.AuditEvent, 20)
for i := 0; i < 20; i++ {
testAuditEvents[i] = &events.UserCreate{
Metadata: events.Metadata{
ID: strconv.Itoa(i),
},
}
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Add the 20 events to a mock event watcher.
mockEventWatcher := &mockTeleportEventWatcher{events: testAuditEvents}
client := newTeleportEventWatcher(t, mockEventWatcher)
// Start the events goroutine
chEvt, chErr := client.Events(ctx)
// Collect all 20 events
for i := 0; i < 20; i++ {
select {
case event, ok := <-chEvt:
require.NotNil(t, event, "Expected an event but got nil. i: %v", i)
require.Equal(t, strconv.Itoa(i), event.ID)
if !ok {
return
}
case err := <-chErr:
t.Fatalf("Received unexpected error from error channel: %v", err)
return
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
}
// Both channels should be closed once the last event is reached.
select {
case _, ok := <-chEvt:
require.False(t, ok, "Events channel should be closed")
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
select {
case _, ok := <-chErr:
require.False(t, ok, "Error channel should be closed")
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
// Events goroutine should return next page errors
mockErr := trace.Errorf("error")
mockEventWatcher.setSearchEventsError(mockErr)
select {
case err := <-chErr:
require.Error(t, mockErr, err)
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
// Both channels should be closed
select {
case _, ok := <-chEvt:
require.False(t, ok, "Events channel should be closed")
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
select {
case _, ok := <-chErr:
require.False(t, ok, "Error channel should be closed")
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
}
func TestUpdatePage(t *testing.T) {
ctx := context.Background()
// create fake audit events with ids 0-9
testAuditEvents := make([]events.AuditEvent, 10)
for i := 0; i < 10; i++ {
testAuditEvents[i] = &events.UserCreate{
Metadata: events.Metadata{
ID: strconv.Itoa(i),
},
}
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mockEventWatcher := &mockTeleportEventWatcher{}
client := newTeleportEventWatcher(t, mockEventWatcher)
client.config.ExitOnLastEvent = false
// Start the events goroutine
chEvt, chErr := client.Events(ctx)
// Add an incomplete page of 3 events and collect them.
mockEventWatcher.setEvents(testAuditEvents[:3])
var i int
for ; i < 3; i++ {
select {
case event, ok := <-chEvt:
require.NotNil(t, event, "Expected an event but got nil")
require.Equal(t, strconv.Itoa(i), event.ID)
if !ok {
return
}
case err := <-chErr:
t.Fatalf("Received unexpected error from error channel: %v", err)
return
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
}
// Both channels should still be open and empty.
select {
case <-chEvt:
t.Fatalf("Events channel should be open")
case <-chErr:
t.Fatalf("Events channel should be open")
case <-time.After(100 * time.Millisecond):
}
// Update the event watcher with the full page of events an collect.
mockEventWatcher.setEvents(testAuditEvents[:5])
for ; i < 5; i++ {
select {
case event, ok := <-chEvt:
require.NotNil(t, event, "Expected an event but got nil")
require.Equal(t, strconv.Itoa(i), event.ID)
if !ok {
return
}
case err := <-chErr:
t.Fatalf("Received unexpected error from error channel: %v", err)
return
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
}
// Both channels should still be open and empty.
select {
case <-chEvt:
t.Fatalf("Events channel should be open")
case <-chErr:
t.Fatalf("Events channel should be open")
case <-time.After(100 * time.Millisecond):
}
// Add another partial page and collect the events
mockEventWatcher.setEvents(testAuditEvents[:7])
for ; i < 7; i++ {
select {
case event, ok := <-chEvt:
require.NotNil(t, event, "Expected an event but got nil")
require.Equal(t, strconv.Itoa(i), event.ID)
if !ok {
return
}
case err := <-chErr:
t.Fatalf("Received unexpected error from error channel: %v", err)
return
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
}
// Events goroutine should return update page errors
mockErr := trace.Errorf("error")
mockEventWatcher.setSearchEventsError(mockErr)
select {
case err := <-chErr:
require.Error(t, mockErr, err)
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
// Both channels should be closed
select {
case _, ok := <-chEvt:
require.False(t, ok, "Events channel should be closed")
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
select {
case _, ok := <-chErr:
require.False(t, ok, "Error channel should be closed")
case <-time.After(100 * time.Millisecond):
t.Fatalf("No events received within deadline")
}
}
func TestValidateConfig(t *testing.T) {
for _, tc := range []struct {
name string
cfg StartCmdConfig
wantError bool
}{
{
name: "Identity file configured",
cfg: StartCmdConfig{
FluentdConfig{},
TeleportConfig{
TeleportIdentityFile: "not_empty_string",
},
IngestConfig{},
LockConfig{},
},
wantError: false,
}, {
name: "Cert, key, ca files configured",
cfg: StartCmdConfig{
FluentdConfig{},
TeleportConfig{
TeleportCA: "not_empty_string",
TeleportCert: "not_empty_string",
TeleportKey: "not_empty_string",
},
IngestConfig{},
LockConfig{},
},
wantError: false,
}, {
name: "Identity and teleport cert/ca/key files configured",
cfg: StartCmdConfig{
FluentdConfig{},
TeleportConfig{
TeleportIdentityFile: "not_empty_string",
TeleportCA: "not_empty_string",
TeleportCert: "not_empty_string",
TeleportKey: "not_empty_string",
},
IngestConfig{},
LockConfig{},
},
wantError: true,
}, {
name: "None set",
cfg: StartCmdConfig{
FluentdConfig{},
TeleportConfig{},
IngestConfig{},
LockConfig{},
},
wantError: true,
}, {
name: "Some of teleport cert/key/ca unset",
cfg: StartCmdConfig{
FluentdConfig{},
TeleportConfig{
TeleportCA: "not_empty_string",
},
IngestConfig{},
LockConfig{},
},
wantError: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
err := tc.cfg.Validate()
if tc.wantError {
require.True(t, trace.IsBadParameter(err))
return
}
require.NoError(t, err)
})
}
}