-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
439 lines (406 loc) · 12.4 KB
/
log.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
Copyright 2015 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 state
import (
"context"
"io"
"io/ioutil"
"time"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/session"
"github.com/cenkalti/backoff"
"github.com/gravitational/trace"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
const (
// DefaultQueueLen determines how many logging events to queue in-memory
// before start dropping them (probably because logging server is down)
DefaultQueueLen = 300
// DefaultFlushTimeout is a period to flush after no other events have been received
DefaultFlushTimeout = time.Second
// DefaultFlushChunks is a max chunks accumulated over period to flush
DefaultFlushChunks = 250
// DefaultFlushBytes is a max bytes of the chunks before the flush will be triggered
DefaultFlushBytes = 100000
// DefaultThrottleTimeout is a latency after we will
DefaultThrottleTimeout = 500 * time.Millisecond
// DefaultThrottleDuration is a period that we will throttle the slow network for
// before trying to send again
DefaultThrottleDuration = 10 * time.Second
// DefaultBackoffInitialInterval is initial interval for backoff
DefaultBackoffInitialInterval = 100 * time.Millisecond
// DefaultBackoffMaxInterval is maximum interval for backoff
DefaultBackoffMaxInterval = DefaultThrottleDuration
)
var (
errNotSupported = trace.BadParameter("method not supported")
)
var (
auditLatencies = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "audit_latency_microseconds",
Help: "Latency for audit log submission",
// Buckets in microsecnd latencies
Buckets: prometheus.ExponentialBuckets(5000, 1.5, 15),
},
)
auditChunks = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "audit_chunks_total",
Help: "Chunks per slice submitted",
Buckets: prometheus.LinearBuckets(10, 20, 10),
},
)
auditBytesPerChunk = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "audit_chunk_bytes",
Help: "Bytes per submitted per chunk",
Buckets: prometheus.ExponentialBuckets(10, 3.0, 10),
},
)
auditBytesPerSlice = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "audit_slice_bytes",
Help: "Bytes per submitted slice of chunks",
Buckets: prometheus.ExponentialBuckets(100, 3.0, 10),
},
)
auditRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "audit_requests_total",
Help: "Number of audit requests",
},
[]string{"result"},
)
)
func init() {
// Metrics have to be registered to be exposed:
prometheus.MustRegister(auditLatencies)
prometheus.MustRegister(auditChunks)
prometheus.MustRegister(auditBytesPerChunk)
prometheus.MustRegister(auditBytesPerSlice)
prometheus.MustRegister(auditRequests)
}
// CachingAuditLogConifig sets configuration for caching audit log
type CachingAuditLogConfig struct {
// Namespace is session namespace
Namespace string
// SessionID is session ID this log forwards for
SessionID string
// Server is the server receiving audit events
Server events.IAuditLog
// QueueLen is length of the caching queue
QueueLen int
// FlushChunks controls how many chunks to aggregate before submit
FlushChunks int
// Context is an optional context
Context context.Context
// ThrottleTimeout is a timeout that triggers throttling
ThrottleTimeout time.Duration
// ThrottleDuration is a duration for throttling
ThrottleDuration time.Duration
// FlushTimeout is a period to flush buffered chunks if the queue
// has not filled up yet
FlushTimeout time.Duration
// FlushBytes sets amount of bytes per slice that triggers
// the flush to the server
FlushBytes int64
// BackoffInitialInterval is initial interval for backoff
BackoffInitialInterval time.Duration
// BackoffMaxInterval is maximum interval for backoff
BackoffMaxInterval time.Duration
}
// CheckAndSetDefaults checks and sets defaults
func (c *CachingAuditLogConfig) CheckAndSetDefaults() error {
if c.Namespace == "" {
return trace.BadParameter("missing parameter Namespace")
}
if c.SessionID == "" {
return trace.BadParameter("missing parameter SessionID")
}
if c.Server == nil {
return trace.BadParameter("missing parameter Server")
}
if c.QueueLen == 0 {
c.QueueLen = DefaultQueueLen
}
if c.FlushChunks == 0 {
c.FlushChunks = DefaultFlushChunks
}
if c.Context == nil {
c.Context = context.TODO()
}
if c.ThrottleTimeout == 0 {
c.ThrottleTimeout = DefaultThrottleTimeout
}
if c.ThrottleTimeout == 0 {
c.ThrottleTimeout = DefaultThrottleTimeout
}
if c.ThrottleDuration == 0 {
c.ThrottleDuration = DefaultThrottleDuration
}
if c.FlushTimeout == 0 {
c.FlushTimeout = DefaultFlushTimeout
}
if c.FlushBytes == 0 {
c.FlushBytes = DefaultFlushBytes
}
if c.BackoffInitialInterval == 0 {
c.BackoffInitialInterval = DefaultBackoffInitialInterval
}
if c.BackoffMaxInterval == 0 {
c.BackoffMaxInterval = DefaultBackoffMaxInterval
}
return nil
}
// CachingAuditLog implements events.IAuditLog on the recording machine (SSH server)
// It captures the local recording and forwards it to the AuditLog network server
// Some important properties of this implementation:
//
// * Without back pressure on posting session chunks, audit log was loosing events
// because produce was much faster than consume and buffer was oveflowing
//
// * Throttle is important to continue the session in case if audit log
// slowness, as the session output will block and timeout on every request
//
// * It is important to pack chunnks, because ls -laR / would otherwise
// generate about 10K requests per second. With this packing approach
// we reduced this number to about 40-50 requests per second,
// we can now tweak this parameter now by setting queue size and flush buffers.
//
// * Current implementation attaches audit log forwarder per session
type CachingAuditLog struct {
CachingAuditLogConfig
queue chan []*events.SessionChunk
cancel context.CancelFunc
ctx context.Context
chunks []*events.SessionChunk
bytes int64
throttleStart time.Time
waitCtx context.Context
waitCtxCancel context.CancelFunc
}
func (ll *CachingAuditLog) add(chunks []*events.SessionChunk) {
ll.chunks = append(ll.chunks, chunks...)
for i := range chunks {
ll.bytes += int64(len(chunks[i].Data))
}
}
func (ll *CachingAuditLog) reset() []*events.SessionChunk {
out := ll.chunks
ll.chunks = make([]*events.SessionChunk, 0, ll.FlushChunks)
ll.bytes = 0
return out
}
// NewCachingAuditLog creaets a new & fully initialized instance of the alog
func NewCachingAuditLog(cfg CachingAuditLogConfig) (*CachingAuditLog, error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
// context to wait for all tail data to arrive
waitCtx, waitCtxCancel := context.WithCancel(context.TODO())
// context to singal stops
ctx, cancel := context.WithCancel(context.TODO())
ll := &CachingAuditLog{
CachingAuditLogConfig: cfg,
cancel: cancel,
ctx: ctx,
waitCtx: waitCtx,
waitCtxCancel: waitCtxCancel,
}
ll.queue = make(chan []*events.SessionChunk, ll.QueueLen)
go ll.run()
return ll, nil
}
func (ll *CachingAuditLog) collectRemainingChunks() {
for {
select {
case chunks := <-ll.queue:
ll.add(chunks)
default:
return
}
}
}
// run thread is picking up logging events and tries to forward them
// to the logging server
func (ll *CachingAuditLog) run() {
// signal that the logger has flushed all resources
defer ll.waitCtxCancel()
ticker := time.NewTicker(ll.FlushTimeout)
defer ticker.Stop()
var tickerC <-chan time.Time
for {
select {
case <-ll.ctx.Done():
ll.collectRemainingChunks()
ll.flush(flushOpts{force: true, noRetry: true})
return
case <-tickerC:
// tick received to force flush after time passed
tickerC = nil
ll.flush(flushOpts{force: true})
case chunks := <-ll.queue:
ll.add(chunks)
// we have received, set the timer
// if no other chunks will not arrive to flush it
tickerC = ticker.C
ll.flush(flushOpts{force: false})
}
}
}
func (ll *CachingAuditLog) newExponentialBackoff() *backoff.ExponentialBackOff {
b := &backoff.ExponentialBackOff{
InitialInterval: ll.BackoffInitialInterval,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: ll.BackoffMaxInterval,
Clock: backoff.SystemClock,
}
b.Reset()
return b
}
type flushOpts struct {
force bool
noRetry bool
}
func (ll *CachingAuditLog) flush(opts flushOpts) {
if len(ll.chunks) == 0 {
return
}
if !opts.force {
if len(ll.chunks) < ll.FlushChunks && ll.bytes < ll.FlushBytes {
return
}
}
chunks := ll.reset()
slice := events.SessionSlice{
Namespace: ll.Namespace,
SessionID: ll.SessionID,
Chunks: chunks,
}
err := ll.postSlice(slice)
if err == nil {
return
}
log.Warningf("lost connection: %v", err)
if opts.noRetry {
return
}
ticker := backoff.NewTicker(ll.newExponentialBackoff())
defer ticker.Stop()
for {
select {
case <-ll.ctx.Done():
return
case <-ticker.C:
err := ll.postSlice(slice)
if err == nil {
return
} else {
log.Warningf("lost connection, retried with error: %v", err)
}
}
}
}
func (ll *CachingAuditLog) postSlice(slice events.SessionSlice) error {
start := time.Now()
err := ll.Server.PostSessionSlice(slice)
auditRequests.WithLabelValues("total").Inc()
if err != nil {
auditRequests.WithLabelValues("fail").Inc()
} else {
auditRequests.WithLabelValues("ok").Inc()
}
auditLatencies.Observe(float64(time.Now().Sub(start) / time.Microsecond))
auditChunks.Observe(float64(len(slice.Chunks)))
var bytes int64
for _, c := range slice.Chunks {
bytes += int64(len(c.Data))
auditBytesPerChunk.Observe(float64(len(c.Data)))
}
auditBytesPerSlice.Observe(float64(bytes))
return err
}
func (ll *CachingAuditLog) post(chunks []*events.SessionChunk) error {
if time.Now().Before(ll.throttleStart) {
return nil
}
select {
case <-ll.ctx.Done():
return nil
case ll.queue <- chunks:
return nil
default:
// the queue is blocked, now we will create a timer
// to detect the timeout
}
timer := time.NewTimer(ll.ThrottleTimeout)
defer timer.Stop()
select {
case ll.queue <- chunks:
case <-ll.ctx.Done():
return nil
case <-timer.C:
ll.throttleStart = time.Now().Add(ll.ThrottleDuration)
log.Warningf("latency spiked over %v, will throttle audit log forward until %v", ll.ThrottleTimeout, ll.throttleStart)
}
return nil
}
// WaitForDelivery waits until all operations of the caching audit log complete
// after Close has been called, e.g. flushing remaining items
func (ll *CachingAuditLog) WaitForDelivery(ctx context.Context) error {
select {
case <-ll.waitCtx.Done():
return nil
case <-ctx.Done():
// interrupted by caller
return trace.ConnectionProblem(nil, "interrupted by caller: context closed")
}
}
func (ll *CachingAuditLog) Close() error {
ll.cancel()
return nil
}
func (ll *CachingAuditLog) EmitAuditEvent(eventType string, fields events.EventFields) error {
return ll.Server.EmitAuditEvent(eventType, fields)
}
func (ll *CachingAuditLog) PostSessionChunk(namespace string, sid session.ID, reader io.Reader) error {
data, err := ioutil.ReadAll(reader)
if err != nil {
return trace.Wrap(err)
}
chunks := []*events.SessionChunk{
{
Data: data,
Time: time.Now().UTC().UnixNano(),
},
}
return ll.post(chunks)
}
func (ll *CachingAuditLog) PostSessionSlice(slice events.SessionSlice) error {
return ll.post(slice.Chunks)
}
func (ll *CachingAuditLog) GetSessionChunk(string, session.ID, int, int) ([]byte, error) {
return nil, errNotSupported
}
func (ll *CachingAuditLog) GetSessionEvents(string, session.ID, int) ([]events.EventFields, error) {
return nil, errNotSupported
}
func (ll *CachingAuditLog) SearchEvents(time.Time, time.Time, string) ([]events.EventFields, error) {
return nil, errNotSupported
}
func (ll *CachingAuditLog) SearchSessionEvents(time.Time, time.Time) ([]events.EventFields, error) {
return nil, errNotSupported
}