-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
session_tracker.go
367 lines (287 loc) · 10.7 KB
/
session_tracker.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
/*
Copyright 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 types
import (
"time"
"github.com/gravitational/trace"
"golang.org/x/exp/slices"
"github.com/gravitational/teleport/api/defaults"
)
const (
SSHSessionKind SessionKind = "ssh"
KubernetesSessionKind SessionKind = "k8s"
DatabaseSessionKind SessionKind = "db"
AppSessionKind SessionKind = "app"
WindowsDesktopSessionKind SessionKind = "desktop"
SessionObserverMode SessionParticipantMode = "observer"
SessionModeratorMode SessionParticipantMode = "moderator"
SessionPeerMode SessionParticipantMode = "peer"
)
// SessionKind is a type of session.
type SessionKind string
// SessionParticipantMode is the mode that determines what you can do when you join a session.
type SessionParticipantMode string
// SessionTracker is a resource which tracks an active session.
type SessionTracker interface {
Resource
// GetSessionID returns the ID of the session.
GetSessionID() string
// GetSessionKind returns the kind of the session.
GetSessionKind() SessionKind
// GetState returns the state of the session.
GetState() SessionState
// SetState sets the state of the session.
SetState(SessionState) error
// SetCreated sets the time at which the session was created.
SetCreated(time.Time)
// GetCreated returns the time at which the session was created.
GetCreated() time.Time
// GetExpires return the time at which the session expires.
GetExpires() time.Time
// GetReason returns the reason for the session.
GetReason() string
// GetInvited returns a list of people invited to the session.
GetInvited() []string
// GetHostname returns the hostname of the session target.
GetHostname() string
// GetAddress returns the address of the session target.
GetAddress() string
// GetClusterName returns the name of the Teleport cluster.
GetClusterName() string
// GetLogin returns the target machine username used for this session.
GetLogin() string
// GetParticipants returns the list of participants in the session.
GetParticipants() []Participant
// AddParticipant adds a participant to the session tracker.
AddParticipant(Participant)
// RemoveParticipant removes a participant from the session tracker.
RemoveParticipant(string) error
// UpdatePresence updates presence timestamp of a participant.
UpdatePresence(string, time.Time) error
// GetKubeCluster returns the name of the kubernetes cluster the session is running in.
GetKubeCluster() string
// GetDesktopName returns the name of the Windows desktop the session is running in.
GetDesktopName() string
// GetAppName returns the name of the app being accessed.
GetAppName() string
// GetDatabaseName returns the name of the database being accessed.
GetDatabaseName() string
// GetHostUser fetches the user marked as the "host" of the session.
// Things like RBAC policies are determined from this user.
GetHostUser() string
// GetHostPolicySets returns a list of policy sets held by the host user at the time of session creation.
// This a subset of a role that contains some versioning and naming information in addition to the require policies
GetHostPolicySets() []*SessionTrackerPolicySet
// GetLastActive returns the time at which the session was last active (i.e used by any participant).
GetLastActive() time.Time
// HostID is the target host id that created the session tracker.
GetHostID() string
// GetTargetSubKind returns the sub kind of the target server.
GetTargetSubKind() string
// GetCommand returns the command that initiated the session.
GetCommand() []string
}
func NewSessionTracker(spec SessionTrackerSpecV1) (SessionTracker, error) {
session := &SessionTrackerV1{
ResourceHeader: ResourceHeader{
Metadata: Metadata{
Name: spec.SessionID,
},
},
Spec: spec,
}
if err := session.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return session, nil
}
// setStaticFields sets static resource header and metadata fields.
func (s *SessionTrackerV1) setStaticFields() {
s.Kind = KindSessionTracker
s.Version = V1
}
// CheckAndSetDefaults sets defaults for the session resource.
func (s *SessionTrackerV1) CheckAndSetDefaults() error {
s.setStaticFields()
if err := s.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if s.GetCreated().IsZero() {
s.SetCreated(time.Now())
}
if s.Expiry().IsZero() {
// By default, resource expiration should match session expiration.
expiry := s.GetExpires()
if expiry.IsZero() {
expiry = s.GetCreated().Add(defaults.SessionTrackerTTL)
}
s.SetExpiry(expiry)
}
return nil
}
// GetSessionID returns the ID of the session.
func (s *SessionTrackerV1) GetSessionID() string {
return s.Spec.SessionID
}
// GetSessionKind returns the kind of the session.
func (s *SessionTrackerV1) GetSessionKind() SessionKind {
return SessionKind(s.Spec.Kind)
}
// GetState returns the state of the session.
func (s *SessionTrackerV1) GetState() SessionState {
return s.Spec.State
}
// SetState sets the state of the session.
func (s *SessionTrackerV1) SetState(state SessionState) error {
switch state {
case SessionState_SessionStateRunning, SessionState_SessionStatePending, SessionState_SessionStateTerminated:
s.Spec.State = state
return nil
default:
return trace.BadParameter("invalid session state: %v", state)
}
}
// GetCreated returns the time at which the session was created.
func (s *SessionTrackerV1) GetCreated() time.Time {
return s.Spec.Created
}
// SetCreated returns the time at which the session was created.
func (s *SessionTrackerV1) SetCreated(created time.Time) {
s.Spec.Created = created
}
// GetExpires return the time at which the session expires.
func (s *SessionTrackerV1) GetExpires() time.Time {
return s.Spec.Expires
}
// GetReason returns the reason for the session.
func (s *SessionTrackerV1) GetReason() string {
return s.Spec.Reason
}
// GetInvited returns a list of people invited to the session.
func (s *SessionTrackerV1) GetInvited() []string {
return s.Spec.Invited
}
// GetHostname returns the hostname of the session target.
func (s *SessionTrackerV1) GetHostname() string {
return s.Spec.Hostname
}
// GetAddress returns the address of the session target.
func (s *SessionTrackerV1) GetAddress() string {
return s.Spec.Address
}
// GetClustername returns the name of the cluster the session is running in.
func (s *SessionTrackerV1) GetClusterName() string {
return s.Spec.ClusterName
}
// GetLogin returns the target machine username used for this session.
func (s *SessionTrackerV1) GetLogin() string {
return s.Spec.Login
}
// GetParticipants returns a list of participants in the session.
func (s *SessionTrackerV1) GetParticipants() []Participant {
return s.Spec.Participants
}
// AddParticipant adds a participant to the session tracker.
func (s *SessionTrackerV1) AddParticipant(participant Participant) {
s.Spec.Participants = append(s.Spec.Participants, participant)
}
// RemoveParticipant removes a participant from the session tracker.
func (s *SessionTrackerV1) RemoveParticipant(id string) error {
for i, participant := range s.Spec.Participants {
if participant.ID == id {
s.Spec.Participants[i], s.Spec.Participants = s.Spec.Participants[len(s.Spec.Participants)-1], s.Spec.Participants[:len(s.Spec.Participants)-1]
return nil
}
}
return trace.NotFound("participant %v not found", id)
}
// GetKubeCluster returns the name of the kubernetes cluster the session is running in.
//
// This is only valid for kubernetes sessions.
func (s *SessionTrackerV1) GetKubeCluster() string {
return s.Spec.KubernetesCluster
}
// HostID is the target host id that created the session tracker.
func (s *SessionTrackerV1) GetHostID() string {
return s.Spec.HostID
}
// GetDesktopName returns the name of the Windows desktop the session is running in.
//
// This is only valid for Windows desktop sessions.
func (s *SessionTrackerV1) GetDesktopName() string {
return s.Spec.DesktopName
}
// GetAppName returns the name of the app being accessed in the session.
//
// This is only valid for app sessions.
func (s *SessionTrackerV1) GetAppName() string {
return s.Spec.AppName
}
// GetDatabaseName returns the name of the database being accessed in the session.
//
// This is only valid for database sessions.
func (s *SessionTrackerV1) GetDatabaseName() string {
return s.Spec.DatabaseName
}
// GetHostUser fetches the user marked as the "host" of the session.
// Things like RBAC policies are determined from this user.
func (s *SessionTrackerV1) GetHostUser() string {
return s.Spec.HostUser
}
// UpdatePresence updates presence timestamp of a participant.
func (s *SessionTrackerV1) UpdatePresence(user string, t time.Time) error {
idx := slices.IndexFunc(s.Spec.Participants, func(participant Participant) bool {
return participant.User == user
})
if idx < 0 {
return trace.NotFound("participant %v not found", user)
}
s.Spec.Participants[idx].LastActive = t
return nil
}
// GetHostPolicySets returns a list of policy sets held by the host user at the time of session creation.
// This a subset of a role that contains some versioning and naming information in addition to the require policies
func (s *SessionTrackerV1) GetHostPolicySets() []*SessionTrackerPolicySet {
return s.Spec.HostPolicies
}
// GetLastActive returns the time at which the session was last active (i.e used by any participant).
func (s *SessionTrackerV1) GetLastActive() time.Time {
var last time.Time
for _, participant := range s.Spec.Participants {
if participant.LastActive.After(last) {
last = participant.LastActive
}
}
return last
}
// GetTargetSubKind returns the sub kind of the target server.
func (s *SessionTrackerV1) GetTargetSubKind() string {
return s.Spec.TargetSubKind
}
// GetCommand returns command that intiated the session.
func (s *SessionTrackerV1) GetCommand() []string {
return s.Spec.InitialCommand
}
// Match checks if a given session tracker matches this filter.
func (f *SessionTrackerFilter) Match(s SessionTracker) bool {
if f.Kind != "" && string(s.GetSessionKind()) != f.Kind {
return false
}
if f.State != nil && s.GetState() != f.State.State {
return false
}
if f.DesktopName != "" && s.GetDesktopName() != f.DesktopName {
return false
}
return true
}