forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessiontracker.go
290 lines (247 loc) · 9.15 KB
/
sessiontracker.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
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package local
import (
"context"
"time"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/services"
)
const (
sessionPrefix = "session_tracker"
retryDelay = time.Second
terminatedTTL = 3 * time.Minute
casRetryLimit = 7
casErrorMessage = "CompareAndSwap reached retry limit"
)
type sessionTracker struct {
bk backend.Backend
}
func NewSessionTrackerService(bk backend.Backend) (services.SessionTrackerService, error) {
return &sessionTracker{bk}, nil
}
func (s *sessionTracker) loadSession(ctx context.Context, sessionID string) (types.SessionTracker, error) {
sessionJSON, err := s.bk.Get(ctx, backend.Key(sessionPrefix, sessionID))
if err != nil {
return nil, trace.Wrap(err)
}
session, err := services.UnmarshalSessionTracker(sessionJSON.Value)
if err != nil {
return nil, trace.Wrap(err)
}
return session, nil
}
// UpdatePresence updates the presence status of a user in a session.
func (s *sessionTracker) UpdatePresence(ctx context.Context, sessionID, user string) error {
for i := 0; i < casRetryLimit; i++ {
sessionItem, err := s.bk.Get(ctx, backend.Key(sessionPrefix, sessionID))
if err != nil {
return trace.Wrap(err)
}
session, err := services.UnmarshalSessionTracker(sessionItem.Value)
if err != nil {
return trace.Wrap(err)
}
if err := session.UpdatePresence(user, s.bk.Clock().Now().UTC()); err != nil {
return trace.Wrap(err)
}
sessionJSON, err := services.MarshalSessionTracker(session)
if err != nil {
return trace.Wrap(err)
}
item := backend.Item{
Key: backend.Key(sessionPrefix, sessionID),
Value: sessionJSON,
Expires: session.Expiry(),
Revision: sessionItem.Revision,
}
_, err = s.bk.CompareAndSwap(ctx, *sessionItem, item)
if trace.IsCompareFailed(err) {
select {
case <-ctx.Done():
return trace.Wrap(ctx.Err())
case <-time.After(retryDelay):
continue
}
}
return trace.Wrap(err)
}
return trace.CompareFailed(casErrorMessage)
}
// GetSessionTracker returns the current state of a session tracker for an active session.
func (s *sessionTracker) GetSessionTracker(ctx context.Context, sessionID string) (types.SessionTracker, error) {
session, err := s.loadSession(ctx, sessionID)
if err != nil {
return nil, trace.Wrap(err)
}
return session, nil
}
func (s *sessionTracker) getActiveSessionTrackers(ctx context.Context, filter *types.SessionTrackerFilter) ([]types.SessionTracker, error) {
prefix := backend.ExactKey(sessionPrefix)
result, err := s.bk.GetRange(ctx, prefix, backend.RangeEnd(prefix), backend.NoLimit)
if err != nil {
return nil, trace.Wrap(err)
}
sessions := make([]types.SessionTracker, 0, len(result.Items))
// We don't overallocate expired since cleaning up sessions here should be rare.
var noExpiry []backend.Item
now := s.bk.Clock().Now()
for _, item := range result.Items {
session, err := services.UnmarshalSessionTracker(item.Value)
if err != nil {
return nil, trace.Wrap(err)
}
// NOTE: This is the session expiry timestamp, not the backend timestamp stored in `item.Expires`.
exp := session.GetExpires()
if session.Expiry().After(exp) {
exp = session.Expiry()
}
after := exp.After(now)
switch {
case after:
// Keep any items that aren't expired and which match the filter.
if filter == nil || (filter != nil && filter.Match(session)) {
sessions = append(sessions, session)
}
case !after && item.Expires.IsZero():
// Clear item if expiry is not set on the backend.
noExpiry = append(noExpiry, item)
default:
// If we take this branch, the expiry is set and the backend is responsible for cleaning up the item.
}
}
if len(noExpiry) > 0 {
go func() {
for _, item := range noExpiry {
if err := s.bk.Delete(ctx, item.Key); err != nil {
if !trace.IsNotFound(err) {
logrus.WithError(err).Error("Failed to remove stale session tracker")
}
}
}
}()
}
return sessions, nil
}
// GetActiveSessionTrackers returns a list of active session trackers.
func (s *sessionTracker) GetActiveSessionTrackers(ctx context.Context) ([]types.SessionTracker, error) {
return s.getActiveSessionTrackers(ctx, nil)
}
// GetActiveSessionTrackersWithFilter returns a list of active sessions filtered by a filter.
func (s *sessionTracker) GetActiveSessionTrackersWithFilter(ctx context.Context, filter *types.SessionTrackerFilter) ([]types.SessionTracker, error) {
return s.getActiveSessionTrackers(ctx, filter)
}
// CreateSessionTracker creates a tracker resource for an active session.
func (s *sessionTracker) CreateSessionTracker(ctx context.Context, tracker types.SessionTracker) (types.SessionTracker, error) {
json, err := services.MarshalSessionTracker(tracker)
if err != nil {
return nil, trace.Wrap(err)
}
item := backend.Item{
Key: backend.Key(sessionPrefix, tracker.GetSessionID()),
Value: json,
Expires: tracker.Expiry(),
}
_, err = s.bk.Put(ctx, item)
if err != nil {
return nil, trace.Wrap(err)
}
return tracker, nil
}
// UpdateSessionTracker updates a tracker resource for an active session.
func (s *sessionTracker) UpdateSessionTracker(ctx context.Context, req *proto.UpdateSessionTrackerRequest) error {
for i := 0; i < casRetryLimit; i++ {
sessionItem, err := s.bk.Get(ctx, backend.Key(sessionPrefix, req.SessionID))
if err != nil {
return trace.Wrap(err)
}
session, err := services.UnmarshalSessionTracker(sessionItem.Value)
if err != nil {
return trace.Wrap(err)
}
switch session := session.(type) {
case *types.SessionTrackerV1:
switch update := req.Update.(type) {
case *proto.UpdateSessionTrackerRequest_UpdateState:
// Since we are using a CAS loop, we can safely check the state of the session
// before updating it. If the session is already closed, we can return an error
// to the caller to indicate that the session is no longer active and the update
// should not be applied.
// Before, we were relying on the caller to send the updates in the correct order
// and to not send updates for closed sessions. This was error prone and led to
// sessions getting stuck as active. The expiry of the session was correctly stored
// in the backend, but since dynamodb deletion is eventually consistent, the session
// could still be returned by GetActiveSessionTrackers for days if a
// running event is received after the session termination event.
if session.GetState() == types.SessionState_SessionStateTerminated {
return trace.BadParameter("session %q is already closed", session.GetSessionID())
}
if err := session.SetState(update.UpdateState.State); err != nil {
return trace.Wrap(err)
}
case *proto.UpdateSessionTrackerRequest_AddParticipant:
session.AddParticipant(*update.AddParticipant.Participant)
case *proto.UpdateSessionTrackerRequest_RemoveParticipant:
if err := session.RemoveParticipant(update.RemoveParticipant.ParticipantID); err != nil {
return trace.Wrap(err)
}
case *proto.UpdateSessionTrackerRequest_UpdateExpiry:
session.SetExpiry(*update.UpdateExpiry.Expires)
}
default:
return trace.BadParameter("unrecognized session version %T", session)
}
sessionJSON, err := services.MarshalSessionTracker(session)
if err != nil {
return trace.Wrap(err)
}
expiry := session.Expiry()
// Terminated sessions don't need to stick around for the full TTL.
// Instead of explicitly deleting the item from the backend the TTL
// is set to a sooner time so that the backend can automatically
// clean it up.
if session.GetState() == types.SessionState_SessionStateTerminated {
expiry = s.bk.Clock().Now().UTC().Add(terminatedTTL)
}
item := backend.Item{
Key: backend.Key(sessionPrefix, req.SessionID),
Value: sessionJSON,
Expires: expiry,
Revision: sessionItem.Revision,
}
_, err = s.bk.CompareAndSwap(ctx, *sessionItem, item)
if trace.IsCompareFailed(err) {
select {
case <-ctx.Done():
return trace.Wrap(ctx.Err())
case <-time.After(retryDelay):
continue
}
}
return trace.Wrap(err)
}
return trace.CompareFailed(casErrorMessage)
}
// RemoveSessionTracker removes a tracker resource for an active session.
func (s *sessionTracker) RemoveSessionTracker(ctx context.Context, sessionID string) error {
return trace.Wrap(s.bk.Delete(ctx, backend.Key(sessionPrefix, sessionID)))
}