-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
sessions.go
512 lines (452 loc) · 13.5 KB
/
sessions.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
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 web
import (
"io"
"net/http"
"sync"
"time"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/utils"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/trace"
"github.com/gravitational/ttlmap"
"github.com/tstranex/u2f"
)
// SessionContext is a context associated with users'
// web session, it stores connected client that persists
// between requests for example to avoid connecting
// to the auth server on every page hit
type SessionContext struct {
sync.Mutex
*log.Entry
sess services.WebSession
user string
clt *auth.TunClient
parent *sessionCache
closers []io.Closer
}
// getTerminal finds and returns an active web terminal for a given session:
func (c *SessionContext) getTerminal(sessionID session.ID) (*terminalHandler, error) {
c.Lock()
defer c.Unlock()
for _, closer := range c.closers {
term, ok := closer.(*terminalHandler)
if ok && term.params.SessionID == sessionID {
return term, nil
}
}
return nil, trace.NotFound("no connected streams")
}
func (c *SessionContext) UpdateSessionTerminal(
namespace string, sessionID session.ID, params session.TerminalParams) error {
err := c.clt.UpdateSession(session.UpdateRequest{
ID: sessionID,
TerminalParams: ¶ms,
Namespace: namespace,
})
if err != nil {
return trace.Wrap(err)
}
term, err := c.getTerminal(sessionID)
if err != nil {
return trace.Wrap(err)
}
return trace.Wrap(term.resizePTYWindow(params))
}
func (c *SessionContext) AddClosers(closers ...io.Closer) {
c.Lock()
defer c.Unlock()
c.closers = append(c.closers, closers...)
}
func (c *SessionContext) RemoveCloser(closer io.Closer) {
c.Lock()
defer c.Unlock()
for i := range c.closers {
if c.closers[i] == closer {
c.closers = append(c.closers[:i], c.closers[i+1:]...)
return
}
}
}
func (c *SessionContext) TransferClosers() []io.Closer {
c.Lock()
defer c.Unlock()
closers := c.closers
c.closers = nil
return closers
}
func (c *SessionContext) Invalidate() error {
return c.parent.InvalidateSession(c)
}
// GetClient returns the client connected to the auth server
func (c *SessionContext) GetClient() (auth.ClientI, error) {
return c.clt, nil
}
// GetUser returns the authenticated teleport user
func (c *SessionContext) GetUser() string {
return c.user
}
// GetWebSession returns a web session
func (c *SessionContext) GetWebSession() services.WebSession {
return c.sess
}
// ExtendWebSession creates a new web session for this user
// based on the previous session
func (c *SessionContext) ExtendWebSession() (services.WebSession, error) {
sess, err := c.clt.ExtendWebSession(c.user, c.sess.GetName())
if err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
// GetAgent returns agent that can we used to answer challenges
// for the web to ssh connection
func (c *SessionContext) GetAgent() (auth.AgentCloser, error) {
return c.clt.GetAgent()
}
// Close cleans up connections associated with requests
func (c *SessionContext) Close() error {
closers := c.TransferClosers()
for _, closer := range closers {
c.Infof("closing %v", closer)
closer.Close()
}
if c.clt != nil {
return trace.Wrap(c.clt.Close())
}
return nil
}
// newSessionCache returns new instance of the session cache
func newSessionCache(servers []utils.NetAddr) (*sessionCache, error) {
m, err := ttlmap.New(1024, ttlmap.CallOnExpire(closeContext))
if err != nil {
return nil, err
}
cache := &sessionCache{
contexts: m,
authServers: servers,
closer: utils.NewCloseBroadcaster(),
}
// periodically close expired and unused sessions
go cache.expireSessions()
return cache, nil
}
// sessionCache handles web session authentication,
// and holds in memory contexts associated with each session
type sessionCache struct {
sync.Mutex
contexts *ttlmap.TTLMap
authServers []utils.NetAddr
closer *utils.CloseBroadcaster
}
// Close closes all allocated resources and stops goroutines
func (s *sessionCache) Close() error {
log.Infof("[WEB] closing session cache")
return s.closer.Close()
}
// closeContext is called when session context expires from
// cache and will clean up connections
func closeContext(key string, val interface{}) {
go func() {
log.Infof("[WEB] closing context %v", key)
ctx, ok := val.(*SessionContext)
if !ok {
log.Warningf("warning, not valid value type %T", val)
return
}
if err := ctx.Close(); err != nil {
log.Infof("failed to close context: %v", err)
}
}()
}
func (s *sessionCache) expireSessions() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.clearExpiredSessions()
case <-s.closer.C:
return
}
}
}
func (s *sessionCache) clearExpiredSessions() {
s.Lock()
defer s.Unlock()
expired := s.contexts.RemoveExpired(10)
if expired != 0 {
log.Infof("[WEB] removed %v expired sessions", expired)
}
}
func (s *sessionCache) Auth(user, pass string, otpToken string) (services.WebSession, error) {
method, err := auth.NewWebPasswordAuth(user, []byte(pass), otpToken)
if err != nil {
return nil, trace.Wrap(err)
}
// the act of creating a tunnel performs authentication. since we are
// only using the tunnel for authentication, we close it right afterwards
// because we will not be using this connection (initiated using password
// based credentials) later.
clt, err := auth.NewTunClient("web.client", s.authServers, user, method)
if err != nil {
return nil, trace.Wrap(err)
}
defer clt.Close()
session, err := clt.SignIn(user, []byte(pass))
if err != nil {
defer clt.Close()
return nil, trace.Wrap(err)
}
return session, nil
}
func (s *sessionCache) GetU2FSignRequest(user, pass string) (*u2f.SignRequest, error) {
method, err := auth.NewWebPasswordU2FSignAuth(user, []byte(pass))
if err != nil {
return nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.get-u2f-sign-request", s.authServers, user, method)
if err != nil {
return nil, trace.Wrap(err)
}
// we are always closing this client, because we will not be using
// this connection initiated using password based credentials
// down the road, so it's a one call client
defer clt.Close()
u2fSignReq, err := clt.GetU2FSignRequest(user, []byte(pass))
if err != nil {
defer clt.Close()
return nil, trace.Wrap(err)
}
return u2fSignReq, nil
}
func (s *sessionCache) AuthWithU2FSignResponse(user string, response *u2f.SignResponse) (services.WebSession, error) {
method, err := auth.NewWebU2FSignResponseAuth(user, response)
if err != nil {
return nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.client-u2f", s.authServers, user, method)
if err != nil {
return nil, trace.Wrap(err)
}
defer clt.Close()
session, err := clt.PreAuthenticatedSignIn(user)
if err != nil {
return nil, trace.Wrap(err)
}
return session, nil
}
func (s *sessionCache) GetCertificate(c client.CreateSSHCertReq) (*client.SSHLoginResponse, error) {
method, err := auth.NewWebPasswordAuth(c.User, []byte(c.Password), c.OTPToken)
if err != nil {
return nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.session", s.authServers, c.User, method)
if err != nil {
return nil, trace.Wrap(err)
}
defer clt.Close()
return createCertificate(c.User, c.PubKey, c.TTL, clt)
}
func createCertificate(user string, pubkey []byte, ttl time.Duration, clt *auth.TunClient) (*client.SSHLoginResponse, error) {
cert, err := clt.GenerateUserCert(pubkey, user, ttl)
if err != nil {
return nil, trace.Wrap(err)
}
hostSigners, err := clt.GetCertAuthorities(services.HostCA, false)
if err != nil {
return nil, trace.Wrap(err)
}
signers, err := services.CertAuthoritiesToV1(hostSigners)
if err != nil {
return nil, trace.Wrap(err)
}
return &client.SSHLoginResponse{
Cert: cert,
HostSigners: signers,
}, nil
}
func (s *sessionCache) GetCertificateWithU2F(c client.CreateSSHCertWithU2FReq) (*client.SSHLoginResponse, error) {
method, err := auth.NewWebU2FSignResponseAuth(c.User, &c.U2FSignResponse)
if err != nil {
return nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.session-u2f", s.authServers, c.User, method)
if err != nil {
return nil, trace.Wrap(err)
}
defer clt.Close()
return createCertificate(c.User, c.PubKey, c.TTL, clt)
}
func (s *sessionCache) GetUserInviteInfo(token string) (user string, otpQRCode []byte, err error) {
method, err := auth.NewSignupTokenAuth(token)
if err != nil {
return "", nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.get-user-invite", s.authServers, "tokenAuth", method)
if err != nil {
return "", nil, trace.Wrap(err)
}
defer clt.Close()
return clt.GetSignupTokenData(token)
}
func (s *sessionCache) GetUserInviteU2FRegisterRequest(token string) (u2fRegisterRequest *u2f.RegisterRequest, e error) {
method, err := auth.NewSignupTokenAuth(token)
if err != nil {
return nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.get-user-invite-u2f-register-request", s.authServers, "tokenAuth", method)
if err != nil {
return nil, trace.Wrap(err)
}
defer clt.Close()
return clt.GetSignupU2FRegisterRequest(token)
}
func (s *sessionCache) CreateNewUser(token, password, otpToken string) (services.WebSession, error) {
method, err := auth.NewSignupTokenAuth(token)
if err != nil {
return nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.create-user", s.authServers, "tokenAuth", method)
if err != nil {
return nil, trace.Wrap(err)
}
defer clt.Close()
sess, err := clt.CreateUserWithToken(token, password, otpToken)
return sess, trace.Wrap(err)
}
func (s *sessionCache) CreateNewU2FUser(token string, password string, u2fRegisterResponse u2f.RegisterResponse) (services.WebSession, error) {
method, err := auth.NewSignupTokenAuth(token)
if err != nil {
return nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.create-u2f-user", s.authServers, "tokenAuth", method)
if err != nil {
return nil, trace.Wrap(err)
}
defer clt.Close()
sess, err := clt.CreateUserWithU2FToken(token, password, u2fRegisterResponse)
return sess, trace.Wrap(err)
}
func (s *sessionCache) InvalidateSession(ctx *SessionContext) error {
defer ctx.Close()
if err := s.resetContext(ctx.GetUser(), ctx.GetWebSession().GetName()); err != nil {
return trace.Wrap(err)
}
clt, err := ctx.GetClient()
if err != nil {
return trace.Wrap(err)
}
err = clt.DeleteWebSession(ctx.GetUser(), ctx.GetWebSession().GetName())
return trace.Wrap(err)
}
func (s *sessionCache) getContext(user, sid string) (*SessionContext, error) {
s.Lock()
defer s.Unlock()
val, ok := s.contexts.Get(user + sid)
if ok {
return val.(*SessionContext), nil
}
return nil, trace.NotFound("sessionContext not found")
}
func (s *sessionCache) insertContext(user, sid string, ctx *SessionContext, ttl time.Duration) (*SessionContext, error) {
s.Lock()
defer s.Unlock()
val, ok := s.contexts.Get(user + sid)
if ok && val != nil { // nil means that we've just invalidated the context now and set it to nil in the cache
return val.(*SessionContext), trace.AlreadyExists("exists")
}
if err := s.contexts.Set(user+sid, ctx, ttl); err != nil {
return nil, trace.Wrap(err)
}
return ctx, nil
}
func (s *sessionCache) resetContext(user, sid string) error {
s.Lock()
defer s.Unlock()
context, ok := s.contexts.Remove(user + sid)
if ok {
closeContext(user+sid, context)
}
return nil
}
func (s *sessionCache) ValidateSession(user, sid string) (*SessionContext, error) {
ctx, err := s.getContext(user, sid)
if err == nil {
return ctx, nil
}
log.Debugf("ValidateSession(%s, %s)", user, sid)
method, err := auth.NewWebSessionAuth(user, []byte(sid))
if err != nil {
return nil, trace.Wrap(err)
}
// Note: do not close this auth API client now. It will exist inside of "session context"
clt, err := auth.NewTunClient("web.session-user", s.authServers, user, method)
if err != nil {
return nil, trace.Wrap(err)
}
sess, err := clt.GetWebSessionInfo(user, sid)
if err != nil {
return nil, trace.Wrap(err)
}
c := &SessionContext{
clt: clt,
user: user,
sess: sess,
parent: s,
}
c.Entry = log.WithFields(log.Fields{
"user": user,
"sess": sess.GetName()[:4],
})
out, err := s.insertContext(user, sid, c, auth.WebSessionTTL)
if err != nil {
// this means that someone has just inserted the context, so
// close our extra context and return
if trace.IsAlreadyExists(err) {
log.Infof("just created, returning the existing one")
defer c.Close()
return out, nil
}
return nil, trace.Wrap(err)
}
return out, nil
}
func (s *sessionCache) SetSession(w http.ResponseWriter, user, sid string) error {
d, err := EncodeCookie(user, sid)
if err != nil {
return err
}
c := &http.Cookie{
Name: "session",
Value: d,
Path: "/",
HttpOnly: true,
Secure: true,
}
http.SetCookie(w, c)
return nil
}
func (s *sessionCache) ClearSession(w http.ResponseWriter) {
http.SetCookie(w, &http.Cookie{
Name: "session",
Value: "",
Path: "/",
HttpOnly: true,
Secure: true,
})
}