forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessionlog.go
300 lines (260 loc) · 8.45 KB
/
sessionlog.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
/*
Copyright 2017 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 events
import (
"bytes"
"encoding/json"
"fmt"
"os"
"sync"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
log "github.com/sirupsen/logrus"
)
// SessionLogger is an interface that all session loggers must implement.
type SessionLogger interface {
// LogEvent logs events associated with this session.
LogEvent(fields EventFields)
// Close is called when clients close on the requested "session writer".
// We ignore their requests because this writer (file) should be closed only
// when the session logger is closed.
Close() error
// Finalize is called by the session when it's closing. This is where we're
// releasing audit resources associated with the session
Finalize() error
// WriteChunk takes a stream of bytes (usually the output from a session
// terminal) and writes it into a "stream file", for future replay of
// interactive sessions.
WriteChunk(chunk *SessionChunk) (written int, err error)
}
// DiskSessionLoggerConfig sets up parameters for disk session logger
// associated with the session ID
type DiskSessionLoggerConfig struct {
// SessionID is the session id of the logger
SessionID session.ID
// EventsFileName is the events file name
EventsFileName string
// StreamFileName is the byte stream file name
StreamFileName string
// Clock is the clock replacement
Clock clockwork.Clock
// RecordSessions controls if sessions are recorded along with audit events.
RecordSessions bool
}
// NewDiskSessionLogger creates new disk based session logger
func NewDiskSessionLogger(cfg DiskSessionLoggerConfig) (*DiskSessionLogger, error) {
var err error
lastPrintEvent, err := readLastPrintEvent(cfg.EventsFileName)
if err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
// no last event is ok
lastPrintEvent = nil
}
// if session recording is on, create a stream file that stores all the
// bytes of the session
var fstream *os.File
if cfg.RecordSessions {
fstream, err = os.OpenFile(cfg.StreamFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
return nil, trace.Wrap(err)
}
}
// create a new session file that stores all the audit events that occured
// related to the session
fevents, err := os.OpenFile(cfg.EventsFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
return nil, trace.Wrap(err)
}
sessionLogger := &DiskSessionLogger{
Entry: log.WithFields(log.Fields{
trace.Component: teleport.ComponentAuditLog,
trace.ComponentFields: log.Fields{
"sid": cfg.SessionID,
},
}),
sid: cfg.SessionID,
streamFile: fstream,
eventsFile: fevents,
clock: cfg.Clock,
lastPrintEvent: lastPrintEvent,
recordSessions: cfg.RecordSessions,
}
return sessionLogger, nil
}
// DiskSessionLogger implements a disk based session logger. The imporant
// property of the disk based logger is that it never fails and can be used as
// a fallback implementation behind more sophisticated loggers.
type DiskSessionLogger struct {
*log.Entry
sync.Mutex
sid session.ID
// eventsFile stores logged events, just like the main logger, except
// these are all associated with this session
eventsFile *os.File
// streamFile stores bytes from the session terminal I/O for replaying
streamFile *os.File
// clock provides real of fake clock (for tests)
clock clockwork.Clock
// lastPrintEvent is the last written session event
lastPrintEvent *printEvent
// recordSessions controls if sessions are recorded along with audit events.
recordSessions bool
}
// LogEvent logs an event associated with this session
func (sl *DiskSessionLogger) LogEvent(fields EventFields) {
if _, ok := fields[EventTime]; !ok {
fields[EventTime] = sl.clock.Now().In(time.UTC).Round(time.Millisecond)
}
if sl.eventsFile != nil {
_, err := fmt.Fprintln(sl.eventsFile, eventToLine(fields))
if err != nil {
log.Error(trace.DebugReport(err))
}
}
}
// readLastEvent reads last event from the file, it opens
// the file in read only mode and closes it after
func readLastPrintEvent(fileName string) (*printEvent, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, trace.ConvertSystemError(err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return nil, trace.ConvertSystemError(err)
}
if info.Size() == 0 {
return nil, trace.NotFound("no events found")
}
bufSize := int64(512)
if info.Size() < bufSize {
bufSize = info.Size()
}
buf := make([]byte, bufSize)
_, err = f.ReadAt(buf, info.Size()-bufSize)
if err != nil {
return nil, trace.ConvertSystemError(err)
}
lines := bytes.Split(buf, []byte("\n"))
if len(lines) == 0 {
return nil, trace.BadParameter("expected some lines, got %q", string(buf))
}
for i := len(lines) - 1; i > 0; i-- {
line := bytes.TrimSpace(lines[i])
if len(line) == 0 {
continue
}
var event printEvent
if err = json.Unmarshal(line, &event); err != nil {
return nil, trace.Wrap(err)
}
if event.Type != SessionPrintEvent {
continue
}
return &event, nil
}
return nil, trace.NotFound("no session print events found")
}
// Close is called when clients close on the requested "session writer".
// We ignore their requests because this writer (file) should be closed only
// when the session logger is closed
func (sl *DiskSessionLogger) Close() error {
sl.Debugf("Close")
return nil
}
// Finalize is called by the session when it's closing. This is where we're
// releasing audit resources associated with the session
func (sl *DiskSessionLogger) Finalize() error {
sl.Lock()
defer sl.Unlock()
auditOpenFiles.Dec()
if sl.streamFile != nil {
sl.streamFile.Close()
sl.streamFile = nil
}
if sl.eventsFile != nil {
sl.eventsFile.Close()
sl.eventsFile = nil
}
return nil
}
// WriteChunk takes a stream of bytes (usually the output from a session terminal)
// and writes it into a "stream file", for future replay of interactive sessions.
func (sl *DiskSessionLogger) WriteChunk(chunk *SessionChunk) (written int, err error) {
sl.Lock()
defer sl.Unlock()
// when session recording is turned off, don't record the session byte stream
if sl.recordSessions == false {
return len(chunk.Data), nil
}
if sl.streamFile == nil || sl.eventsFile == nil {
return 0, trace.BadParameter("session %v: attempt to write to a closed file", sl.sid)
}
if written, err = sl.streamFile.Write(chunk.Data); err != nil {
return written, trace.Wrap(err)
}
err = sl.writePrintEvent(time.Unix(0, chunk.Time), len(chunk.Data))
return written, trace.Wrap(err)
}
// writePrintEvent logs print event indicating write to the session
func (sl *DiskSessionLogger) writePrintEvent(start time.Time, bytesWritten int) error {
start = start.In(time.UTC).Round(time.Millisecond)
offset := int64(0)
delayMilliseconds := int64(0)
if sl.lastPrintEvent != nil {
offset = sl.lastPrintEvent.Offset + sl.lastPrintEvent.Bytes
delayMilliseconds = diff(sl.lastPrintEvent.Start, start) + sl.lastPrintEvent.DelayMilliseconds
}
event := printEvent{
Start: start,
Type: SessionPrintEvent,
Bytes: int64(bytesWritten),
DelayMilliseconds: delayMilliseconds,
Offset: offset,
}
bytes, err := json.Marshal(event)
if err != nil {
return trace.Wrap(err)
}
_, err = fmt.Fprintln(sl.eventsFile, string(bytes))
if err != nil {
return trace.Wrap(err)
}
sl.lastPrintEvent = &event
return trace.Wrap(err)
}
func diff(before, after time.Time) int64 {
d := int64(after.Sub(before) / time.Millisecond)
if d < 0 {
return 0
}
return d
}
type printEvent struct {
// Start is event start
Start time.Time `json:"time"`
// Type is event type
Type string `json:"event"`
// Bytes is event bytes
Bytes int64 `json:"bytes"`
// DelayMilliseconds is the delay in milliseconds from the start of the session
DelayMilliseconds int64 `json:"ms"`
// Offset int64 is the offset in bytes in the session file
Offset int64 `json:"offset"`
}