forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
408 lines (363 loc) · 10.3 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
/*
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/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"
)
// 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 *auth.Session
user string
clt *auth.TunClient
parent *sessionCache
closers []io.Closer
}
func (c *SessionContext) getConnectHandler(sessionID session.ID) (*connectHandler, error) {
c.Lock()
defer c.Unlock()
for _, closer := range c.closers {
handler, ok := closer.(*connectHandler)
if ok && handler.req.SessionID == sessionID {
return handler, nil
}
}
return nil, trace.NotFound("no connected streams")
}
func (c *SessionContext) UpdateSessionTerminal(sessionID session.ID, params session.TerminalParams) error {
err := c.clt.UpdateSession(session.UpdateRequest{ID: sessionID, TerminalParams: ¶ms})
if err != nil {
return trace.Wrap(err)
}
handler, err := c.getConnectHandler(sessionID)
if err != nil {
return trace.Wrap(err)
}
return trace.Wrap(handler.resizePTYWindow(params))
}
func (c *SessionContext) AddClosers(closers ...io.Closer) {
c.Lock()
defer c.Unlock()
c.closers = append(c.closers, closers...)
}
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() *auth.Session {
return c.sess
}
// ExtendWebSession creates a new web session for this user
// based on the previous session
func (c *SessionContext) ExtendWebSession() (*auth.Session, error) {
sess, err := c.clt.ExtendWebSession(c.user, c.sess.ID)
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) {
a, err := c.clt.GetAgent()
if err != nil {
return nil, trace.Wrap(err)
}
return a, nil
}
// 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, hotpToken string) (*auth.Session, error) {
method, err := auth.NewWebPasswordAuth(user, []byte(pass), hotpToken)
if err != nil {
return nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.client", 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()
session, err := clt.SignIn(user, []byte(pass))
if err != nil {
defer clt.Close()
return nil, trace.Wrap(err)
}
return session, nil
}
func (s *sessionCache) GetCertificate(c createSSHCertReq) (*SSHLoginResponse, error) {
method, err := auth.NewWebPasswordAuth(c.User, []byte(c.Password),
c.HOTPToken)
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()
cert, err := clt.GenerateUserCert(c.PubKey, c.User, c.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 := []services.CertAuthority{}
for _, hs := range hostSigners {
signers = append(signers, *hs)
}
return &SSHLoginResponse{
Cert: cert,
HostSigners: signers,
}, nil
}
func (s *sessionCache) GetUserInviteInfo(token string) (user string,
QRImg []byte, hotpFirstValues []string, e error) {
method, err := auth.NewSignupTokenAuth(token)
if err != nil {
return "", nil, nil, trace.Wrap(err)
}
clt, err := auth.NewTunClient("web.get-user-invite", s.authServers, "tokenAuth", method)
if err != nil {
return "", nil, nil, trace.Wrap(err)
}
defer clt.Close()
return clt.GetSignupTokenData(token)
}
func (s *sessionCache) CreateNewUser(token, password, hotpToken string) (*auth.Session, 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, hotpToken)
return sess, trace.Wrap(err)
}
func (s *sessionCache) InvalidateSession(ctx *SessionContext) error {
defer ctx.Close()
if err := s.resetContext(ctx.GetUser(), ctx.GetWebSession().ID); err != nil {
return trace.Wrap(err)
}
clt, err := ctx.GetClient()
if err != nil {
return trace.Wrap(err)
}
err = clt.DeleteWebSession(ctx.GetUser(), ctx.GetWebSession().ID)
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.ID[: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,
})
}