-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
session.go
580 lines (492 loc) · 15.2 KB
/
session.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
Copyright 2020 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 (
"context"
"fmt"
"time"
"github.com/gravitational/teleport/api/v7/defaults"
"github.com/gravitational/trace"
)
// WebSessionsGetter provides access to web sessions
type WebSessionsGetter interface {
// WebSessions returns the web session manager
WebSessions() WebSessionInterface
}
// WebSessionInterface defines interface to regular web sessions
type WebSessionInterface interface {
// Get returns a web session state for the given request.
Get(ctx context.Context, req GetWebSessionRequest) (WebSession, error)
// List gets all regular web sessions.
List(context.Context) ([]WebSession, error)
// Upsert updates existing or inserts a new web session.
Upsert(ctx context.Context, session WebSession) error
// Delete deletes the web session described by req.
Delete(ctx context.Context, req DeleteWebSessionRequest) error
// DeleteAll removes all web sessions.
DeleteAll(context.Context) error
}
// WebSession stores key and value used to authenticate with SSH
// notes on behalf of user
type WebSession interface {
// Resource represents common properties for all resources.
Resource
// GetShortName returns visible short name used in logging
GetShortName() string
// GetUser returns the user this session is associated with
GetUser() string
// SetUser sets user associated with this session
SetUser(string)
// GetPub is returns public certificate signed by auth server
GetPub() []byte
// GetPriv returns private OpenSSH key used to auth with SSH nodes
GetPriv() []byte
// SetPriv sets private key
SetPriv([]byte)
// GetTLSCert returns PEM encoded TLS certificate associated with session
GetTLSCert() []byte
// BearerToken is a special bearer token used for additional
// bearer authentication
GetBearerToken() string
// SetExpiryTime sets session expiry time
SetExpiryTime(time.Time)
// GetBearerTokenExpiryTime - absolute time when token expires
GetBearerTokenExpiryTime() time.Time
// GetExpiryTime - absolute time when web session expires
GetExpiryTime() time.Time
// GetLoginTime returns the time this user recently logged in.
GetLoginTime() time.Time
// SetLoginTime sets when this user logged in.
SetLoginTime(time.Time)
// WithoutSecrets returns copy of the web session but without private keys
WithoutSecrets() WebSession
// String returns string representation of the session.
String() string
}
// NewWebSession returns new instance of the web session based on the V2 spec
func NewWebSession(name string, subkind string, spec WebSessionSpecV2) (WebSession, error) {
ws := &WebSessionV2{
SubKind: subkind,
Metadata: Metadata{
Name: name,
Expires: &spec.Expires,
},
Spec: spec,
}
if err := ws.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return ws, nil
}
// GetKind gets resource Kind
func (ws *WebSessionV2) GetKind() string {
return ws.Kind
}
// GetSubKind gets resource SubKind
func (ws *WebSessionV2) GetSubKind() string {
return ws.SubKind
}
// SetSubKind sets resource SubKind
func (ws *WebSessionV2) SetSubKind(subKind string) {
ws.SubKind = subKind
}
// GetVersion gets resource Version
func (ws *WebSessionV2) GetVersion() string {
return ws.Version
}
// GetName gets resource Name
func (ws *WebSessionV2) GetName() string {
return ws.Metadata.Name
}
// SetName sets resource Name
func (ws *WebSessionV2) SetName(name string) {
ws.Metadata.Name = name
}
// Expiry returns resource Expiry
func (ws *WebSessionV2) Expiry() time.Time {
return ws.Metadata.Expiry()
}
// SetExpiry Sets resource Expiry
func (ws *WebSessionV2) SetExpiry(expiry time.Time) {
ws.Metadata.SetExpiry(expiry)
}
// GetMetadata gets resource Metadata
func (ws *WebSessionV2) GetMetadata() Metadata {
return ws.Metadata
}
// GetResourceID gets ResourceID
func (ws *WebSessionV2) GetResourceID() int64 {
return ws.Metadata.GetID()
}
// SetResourceID sets ResourceID
func (ws *WebSessionV2) SetResourceID(id int64) {
ws.Metadata.SetID(id)
}
// WithoutSecrets returns copy of the object but without secrets
func (ws *WebSessionV2) WithoutSecrets() WebSession {
ws.Spec.Priv = nil
return ws
}
// setStaticFields sets static resource header and metadata fields.
func (ws *WebSessionV2) setStaticFields() {
ws.Version = V2
ws.Kind = KindWebSession
}
// CheckAndSetDefaults checks and set default values for any missing fields.
func (ws *WebSessionV2) CheckAndSetDefaults() error {
ws.setStaticFields()
if err := ws.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if ws.Spec.User == "" {
return trace.BadParameter("missing User")
}
return nil
}
// String returns string representation of the session.
func (ws *WebSessionV2) String() string {
return fmt.Sprintf("WebSession(kind=%v/%v,user=%v,id=%v,expires=%v)",
ws.GetKind(), ws.GetSubKind(), ws.GetUser(), ws.GetName(), ws.GetExpiryTime())
}
// SetUser sets user associated with this session
func (ws *WebSessionV2) SetUser(u string) {
ws.Spec.User = u
}
// GetUser returns the user this session is associated with
func (ws *WebSessionV2) GetUser() string {
return ws.Spec.User
}
// GetShortName returns visible short name used in logging
func (ws *WebSessionV2) GetShortName() string {
if len(ws.Metadata.Name) < 4 {
return "<undefined>"
}
return ws.Metadata.Name[:4]
}
// GetTLSCert returns PEM encoded TLS certificate associated with session
func (ws *WebSessionV2) GetTLSCert() []byte {
return ws.Spec.TLSCert
}
// GetPub is returns public certificate signed by auth server
func (ws *WebSessionV2) GetPub() []byte {
return ws.Spec.Pub
}
// GetPriv returns private OpenSSH key used to auth with SSH nodes
func (ws *WebSessionV2) GetPriv() []byte {
return ws.Spec.Priv
}
// SetPriv sets private key
func (ws *WebSessionV2) SetPriv(priv []byte) {
ws.Spec.Priv = priv
}
// GetBearerToken gets a special bearer token used for additional
// bearer authentication
func (ws *WebSessionV2) GetBearerToken() string {
return ws.Spec.BearerToken
}
// SetExpiryTime sets session expiry time
func (ws *WebSessionV2) SetExpiryTime(tm time.Time) {
ws.Spec.Expires = tm
}
// GetBearerTokenExpiryTime - absolute time when token expires
func (ws *WebSessionV2) GetBearerTokenExpiryTime() time.Time {
return ws.Spec.BearerTokenExpires
}
// GetExpiryTime - absolute time when web session expires
func (ws *WebSessionV2) GetExpiryTime() time.Time {
return ws.Spec.Expires
}
// GetLoginTime returns the time this user recently logged in.
func (ws *WebSessionV2) GetLoginTime() time.Time {
return ws.Spec.LoginTime
}
// SetLoginTime sets when this user logged in.
func (ws *WebSessionV2) SetLoginTime(loginTime time.Time) {
ws.Spec.LoginTime = loginTime
}
// GetAppSessionRequest contains the parameters to request an application
// web session.
type GetAppSessionRequest struct {
// SessionID is the session ID of the application session itself.
SessionID string
}
// Check validates the request.
func (r *GetAppSessionRequest) Check() error {
if r.SessionID == "" {
return trace.BadParameter("session ID missing")
}
return nil
}
// CreateAppSessionRequest contains the parameters needed to request
// creating an application web session.
type CreateAppSessionRequest struct {
// Username is the identity of the user requesting the session.
Username string `json:"username"`
// PublicAddr is the public address of the application.
PublicAddr string `json:"public_addr"`
// ClusterName is the name of the cluster within which the application is running.
ClusterName string `json:"cluster_name"`
// AWSRoleARN is AWS role this the user wants to assume.
AWSRoleARN string `json:"aws_role_arn"`
}
// Check validates the request.
func (r CreateAppSessionRequest) Check() error {
if r.Username == "" {
return trace.BadParameter("username missing")
}
if r.PublicAddr == "" {
return trace.BadParameter("public address missing")
}
if r.ClusterName == "" {
return trace.BadParameter("cluster name missing")
}
return nil
}
// DeleteAppSessionRequest are the parameters used to request removal of
// an application web session.
type DeleteAppSessionRequest struct {
SessionID string `json:"session_id"`
}
// NewWebToken returns a new web token with the given expiration and spec
func NewWebToken(expires time.Time, spec WebTokenSpecV3) (WebToken, error) {
r := &WebTokenV3{
Metadata: Metadata{
Name: spec.Token,
Expires: &expires,
},
Spec: spec,
}
if err := r.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return r, nil
}
// WebTokensGetter provides access to web tokens
type WebTokensGetter interface {
// WebTokens returns the tokens manager
WebTokens() WebTokenInterface
}
// WebTokenInterface defines interface for managing web tokens
type WebTokenInterface interface {
// Get returns a token specified by the request.
Get(ctx context.Context, req GetWebTokenRequest) (WebToken, error)
// List gets all web tokens.
List(context.Context) ([]WebToken, error)
// Upsert updates existing or inserts a new web token.
Upsert(ctx context.Context, token WebToken) error
// Delete deletes the web token described by req.
Delete(ctx context.Context, req DeleteWebTokenRequest) error
// DeleteAll removes all web tokens.
DeleteAll(context.Context) error
}
// WebToken is a time-limited unique token bound to a user's session
type WebToken interface {
// Resource represents common properties for all resources.
Resource
// GetToken returns the token value
GetToken() string
// SetToken sets the token value
SetToken(token string)
// GetUser returns the user the token is bound to
GetUser() string
// SetUser sets the user the token is bound to
SetUser(user string)
// String returns the text representation of this token
String() string
}
var _ WebToken = &WebTokenV3{}
// GetMetadata returns the token metadata
func (r *WebTokenV3) GetMetadata() Metadata {
return r.Metadata
}
// GetKind returns the token resource kind
func (r *WebTokenV3) GetKind() string {
return r.Kind
}
// GetSubKind returns the token resource subkind
func (r *WebTokenV3) GetSubKind() string {
return r.SubKind
}
// SetSubKind sets the token resource subkind
func (r *WebTokenV3) SetSubKind(subKind string) {
r.SubKind = subKind
}
// GetVersion returns the token resource version
func (r *WebTokenV3) GetVersion() string {
return r.Version
}
// GetName returns the token value
func (r *WebTokenV3) GetName() string {
return r.Metadata.Name
}
// SetName sets the token value
func (r *WebTokenV3) SetName(name string) {
r.Metadata.Name = name
}
// GetResourceID returns the token resource ID
func (r *WebTokenV3) GetResourceID() int64 {
return r.Metadata.GetID()
}
// SetResourceID sets the token resource ID
func (r *WebTokenV3) SetResourceID(id int64) {
r.Metadata.SetID(id)
}
// GetToken returns the token value
func (r *WebTokenV3) GetToken() string {
return r.Spec.Token
}
// SetToken sets the token value
func (r *WebTokenV3) SetToken(token string) {
r.Spec.Token = token
}
// GetUser returns the user this token is bound to
func (r *WebTokenV3) GetUser() string {
return r.Spec.User
}
// SetUser sets the user this token is bound to
func (r *WebTokenV3) SetUser(user string) {
r.Spec.User = user
}
// Expiry returns the token absolute expiration time
func (r *WebTokenV3) Expiry() time.Time {
if r.Metadata.Expires == nil {
return time.Time{}
}
return *r.Metadata.Expires
}
// SetExpiry sets the token absolute expiration time
func (r *WebTokenV3) SetExpiry(t time.Time) {
r.Metadata.Expires = &t
}
// setStaticFields sets static resource header and metadata fields.
func (r *WebTokenV3) setStaticFields() {
r.Kind = KindWebToken
r.Version = V3
}
// CheckAndSetDefaults validates this token value and sets defaults
func (r *WebTokenV3) CheckAndSetDefaults() error {
r.setStaticFields()
if err := r.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if r.Spec.User == "" {
return trace.BadParameter("User required")
}
if r.Spec.Token == "" {
return trace.BadParameter("Token required")
}
return nil
}
// String returns string representation of the token.
func (r *WebTokenV3) String() string {
return fmt.Sprintf("WebToken(kind=%v,user=%v,token=%v,expires=%v)",
r.GetKind(), r.GetUser(), r.GetToken(), r.Expiry())
}
// CheckAndSetDefaults validates the request and sets defaults.
func (r *NewWebSessionRequest) CheckAndSetDefaults() error {
if r.User == "" {
return trace.BadParameter("user name required")
}
if len(r.Roles) == 0 {
return trace.BadParameter("roles required")
}
if len(r.Traits) == 0 {
return trace.BadParameter("traits required")
}
if r.SessionTTL == 0 {
r.SessionTTL = defaults.CertDuration
}
return nil
}
// NewWebSessionRequest defines a request to create a new user
// web session
type NewWebSessionRequest struct {
// User specifies the user this session is bound to
User string
// Roles optionally lists additional user roles
Roles []string
// Traits optionally lists role traits
Traits map[string][]string
// SessionTTL optionally specifies the session time-to-live.
// If left unspecified, the default certificate duration is used.
SessionTTL time.Duration
// LoginTime is the time that this user recently logged in.
LoginTime time.Time
}
// Check validates the request.
func (r *GetWebSessionRequest) Check() error {
if r.User == "" {
return trace.BadParameter("user name missing")
}
if r.SessionID == "" {
return trace.BadParameter("session ID missing")
}
return nil
}
// Check validates the request.
func (r *DeleteWebSessionRequest) Check() error {
if r.SessionID == "" {
return trace.BadParameter("session ID missing")
}
return nil
}
// Check validates the request.
func (r *GetWebTokenRequest) Check() error {
if r.User == "" {
return trace.BadParameter("user name missing")
}
if r.Token == "" {
return trace.BadParameter("token missing")
}
return nil
}
// Check validates the request.
func (r *DeleteWebTokenRequest) Check() error {
if r.Token == "" {
return trace.BadParameter("token missing")
}
return nil
}
// IntoMap makes this filter into a map.
//
// This filter is used with the cache watcher to make sure only sessions
// for a particular user are returned.
func (f *WebSessionFilter) IntoMap() map[string]string {
m := make(map[string]string)
if f.User != "" {
m[keyUser] = f.User
}
return m
}
// FromMap converts provided map into this filter.
//
// This filter is used with the cache watcher to make sure only sessions
// for a particular user are returned.
func (f *WebSessionFilter) FromMap(m map[string]string) error {
for key, val := range m {
switch key {
case keyUser:
f.User = val
default:
return trace.BadParameter("unknown filter key %s", key)
}
}
return nil
}
// Match checks if a given web session matches this filter.
func (f *WebSessionFilter) Match(session WebSession) bool {
if f.User != "" && session.GetUser() != f.User {
return false
}
return true
}