-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
201 lines (167 loc) · 6.06 KB
/
api.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
/*
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 events
import (
"fmt"
"io"
"time"
"github.com/gravitational/teleport/lib/session"
)
const (
// Common event fields:
EventType = "event" // event type/kind
EventTime = "time" // event time
EventLogin = "login" // OS login
EventUser = "user" // teleport user
LocalAddr = "addr.local" // address on the host
RemoteAddr = "addr.remote" // client (user's) address
EventCursor = "id" // event ID (used as cursor value for enumeration, not stored)
// EventNamespace is a namespace of the session event
EventNamespace = "namespace"
// SessionPrintEvent event happens every time a write occurs to
// temirnal I/O during a session
SessionPrintEvent = "print"
// SessionPrintEventBytes says how many bytes have been written into the session
// during "print" event
SessionPrintEventBytes = "bytes"
// SessionEventTimestamp is an offset (in milliseconds) since the beginning of the
// session when the terminal IO event happened
SessionEventTimestamp = "ms"
// SessionEvent indicates that session has been initiated
// or updated by a joining party on the server
SessionStartEvent = "session.start"
// SessionEndEvent indicates taht a session has ended
SessionEndEvent = "session.end"
SessionEventID = "sid"
SessionServerID = "server_id"
// SessionByteOffset is the number of bytes written to session stream since
// the beginning
SessionByteOffset = "offset"
// Join & Leave events indicate when someone joins/leaves a session
SessionJoinEvent = "session.join"
SessionLeaveEvent = "session.leave"
// ExecEvent is an exec command executed by script or user on
// the server side
ExecEvent = "exec"
ExecEventCommand = "command"
ExecEventCode = "exitCode"
ExecEventError = "exitError"
// Port forwarding event
PortForwardEvent = "port"
PortForwardAddr = "addr"
// AuthAttemptEvent is authentication attempt that either
// succeeded or failed based on event status
AuthAttemptEvent = "auth"
AuthAttemptSuccess = "success"
AuthAttemptErr = "error"
// SCPEvent means data transfer that occured on the server
SCPEvent = "scp"
SCPPath = "path"
SCPLengh = "len"
SCPAction = "action"
// ResizeEvent means that some user resized PTY on the client
ResizeEvent = "resize"
TerminalSize = "size" // expressed as 'W:H'
)
const (
// MaxChunkBytes defines the maximum size of a session stream chunk that
// can be requested via AuditLog.GetSessionChunk(). Set to 5MB
MaxChunkBytes = 1024 * 1024 * 5
)
// IAuditLog is the primary (and the only external-facing) interface for AUditLogger.
// If you wish to implement a different kind of logger (not filesystem-based), you
// have to implement this interface
type IAuditLog interface {
// Closer releases connection and resources associated with log if any
io.Closer
EmitAuditEvent(eventType string, fields EventFields) error
// PostSessionSlice sends chunks of recorded session to the event log
PostSessionSlice(SessionSlice) error
// PostSessionChunk returns a writer which SSH nodes use to submit
// their live sessions into the session log
PostSessionChunk(namespace string, sid session.ID, reader io.Reader) error
// GetSessionChunk returns a reader which can be used to read a byte stream
// of a recorded session starting from 'offsetBytes' (pass 0 to start from the
// beginning) up to maxBytes bytes.
//
// If maxBytes > MaxChunkBytes, it gets rounded down to MaxChunkBytes
GetSessionChunk(namespace string, sid session.ID, offsetBytes, maxBytes int) ([]byte, error)
// Returns all events that happen during a session sorted by time
// (oldest first).
//
// after tells to use only return events after a specified cursor Id
//
// This function is usually used in conjunction with GetSessionReader to
// replay recorded session streams.
GetSessionEvents(namespace string, sid session.ID, after int) ([]EventFields, error)
// SearchEvents is a flexible way to find events. The format of a query string
// depends on the implementing backend. A recommended format is urlencoded
// (good enough for Lucene/Solr)
//
// Pagination is also defined via backend-specific query format.
//
// The only mandatory requirement is a date range (UTC). Results must always
// show up sorted by date (newest first)
SearchEvents(fromUTC, toUTC time.Time, query string) ([]EventFields, error)
}
// EventFields instance is attached to every logged event
type EventFields map[string]interface{}
// String returns a string representation of an event structure
func (f EventFields) AsString() string {
return fmt.Sprintf("%s: login=%s, id=%v, bytes=%v",
f.GetString(EventType),
f.GetString(EventLogin),
f.GetInt(EventCursor),
f.GetInt(SessionPrintEventBytes))
}
// GetType returns the type (string) of the event
func (f EventFields) GetType() string {
return f.GetString(EventType)
}
// GetString returns a string representation of a logged field
func (f EventFields) GetString(key string) string {
val, found := f[key]
if !found {
return ""
}
v, _ := val.(string)
return v
}
// GetString returns an int representation of a logged field
func (f EventFields) GetInt(key string) int {
val, found := f[key]
if !found {
return 0
}
v, ok := val.(int)
if !ok {
f, ok := val.(float64)
if ok {
v = int(f)
}
}
return v
}
// GetString returns an int representation of a logged field
func (f EventFields) GetTime(key string) time.Time {
val, found := f[key]
if !found {
return time.Time{}
}
v, ok := val.(time.Time)
if !ok {
s := f.GetString(key)
v, _ = time.Parse(time.RFC3339, s)
}
return v
}