-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsessions.go
355 lines (322 loc) · 12.4 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
/*
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 auth
import (
"context"
"time"
"github.com/google/uuid"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/utils/keys"
"github.com/gravitational/teleport/lib/auth/native"
"github.com/gravitational/teleport/lib/jwt"
"github.com/gravitational/teleport/lib/modules"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/services/local"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
)
// CreateAppSession creates and inserts a services.WebSession into the
// backend with the identity of the caller used to generate the certificate.
// The certificate is used for all access requests, which is where access
// control is enforced.
func (s *Server) CreateAppSession(ctx context.Context, req types.CreateAppSessionRequest, user types.User, identity tlsca.Identity, checker services.AccessChecker) (types.WebSession, error) {
if !modules.GetModules().Features().App {
return nil, trace.AccessDenied(
"this Teleport cluster is not licensed for application access, please contact the cluster administrator")
}
// Don't let the app session go longer than the identity expiration,
// which matches the parent web session TTL as well.
//
// When using web-based app access, the browser will send a cookie with
// sessionID which will be used to fetch services.WebSession which
// contains a certificate whose life matches the life of the session
// that will be used to establish the connection.
ttl := checker.AdjustSessionTTL(identity.Expires.Sub(s.clock.Now()))
// Encode user traits in the app access certificate. This will allow to
// pass user traits when talking to app servers in leaf clusters.
_, traits, err := services.ExtractFromIdentity(s, identity)
if err != nil {
return nil, trace.Wrap(err)
}
// Create certificate for this session.
privateKey, publicKey, err := native.GenerateKeyPair()
if err != nil {
return nil, trace.Wrap(err)
}
certs, err := s.generateUserCert(certRequest{
user: user,
publicKey: publicKey,
checker: checker,
ttl: ttl,
traits: traits,
activeRequests: services.RequestIDs{AccessRequests: identity.ActiveRequests},
// Only allow this certificate to be used for applications.
usage: []string{teleport.UsageAppsOnly},
// Add in the application routing information.
appSessionID: uuid.New().String(),
appPublicAddr: req.PublicAddr,
appClusterName: req.ClusterName,
awsRoleARN: req.AWSRoleARN,
azureIdentity: req.AzureIdentity,
gcpServiceAccount: req.GCPServiceAccount,
// Since we are generating the keys and certs directly on the Auth Server,
// we need to skip attestation.
skipAttestation: true,
// Pass along device extensions from the user.
deviceExtensions: DeviceExtensions(identity.DeviceExtensions),
})
if err != nil {
return nil, trace.Wrap(err)
}
// Create services.WebSession for this session.
sessionID, err := utils.CryptoRandomHex(SessionTokenBytes)
if err != nil {
return nil, trace.Wrap(err)
}
bearer, err := utils.CryptoRandomHex(SessionTokenBytes)
if err != nil {
return nil, trace.Wrap(err)
}
session, err := types.NewWebSession(sessionID, types.KindAppSession, types.WebSessionSpecV2{
User: req.Username,
Priv: privateKey,
Pub: certs.SSH,
TLSCert: certs.TLS,
Expires: s.clock.Now().Add(ttl),
BearerToken: bearer,
})
if err != nil {
return nil, trace.Wrap(err)
}
if err = s.UpsertAppSession(ctx, session); err != nil {
return nil, trace.Wrap(err)
}
log.Debugf("Generated application web session for %v with TTL %v.", req.Username, ttl)
UserLoginCount.Inc()
return session, nil
}
// WaitForAppSession will block until the requested application session shows up in the
// cache or a timeout occurs.
func WaitForAppSession(ctx context.Context, sessionID, user string, ap ReadProxyAccessPoint) error {
req := waitForWebSessionReq{
newWatcherFn: ap.NewWatcher,
getSessionFn: func(ctx context.Context, sessionID string) (types.WebSession, error) {
return ap.GetAppSession(ctx, types.GetAppSessionRequest{SessionID: sessionID})
},
}
return trace.Wrap(waitForWebSession(ctx, sessionID, user, types.KindAppSession, req))
}
// WaitForSnowflakeSession waits until the requested Snowflake session shows up int the cache
// or a timeout occurs.
func WaitForSnowflakeSession(ctx context.Context, sessionID, user string, ap SnowflakeSessionWatcher) error {
req := waitForWebSessionReq{
newWatcherFn: ap.NewWatcher,
getSessionFn: func(ctx context.Context, sessionID string) (types.WebSession, error) {
return ap.GetSnowflakeSession(ctx, types.GetSnowflakeSessionRequest{SessionID: sessionID})
},
}
return trace.Wrap(waitForWebSession(ctx, sessionID, user, types.KindSnowflakeSession, req))
}
// waitForWebSessionReq is a request to wait for web session to be populated in the application cache.
type waitForWebSessionReq struct {
// newWatcherFn is a function that returns new event watcher.
newWatcherFn func(ctx context.Context, watch types.Watch) (types.Watcher, error)
// getSessionFn is a function that returns web session by given ID.
getSessionFn func(ctx context.Context, sessionID string) (types.WebSession, error)
}
// waitForWebSession is an implementation for web session wait functions.
func waitForWebSession(ctx context.Context, sessionID, user string, evenSubKind string, req waitForWebSessionReq) error {
_, err := req.getSessionFn(ctx, sessionID)
if err == nil {
return nil
}
logger := log.WithField("session", sessionID)
if !trace.IsNotFound(err) {
logger.WithError(err).Debug("Failed to query web session.")
}
// Establish a watch on application session.
watcher, err := req.newWatcherFn(ctx, types.Watch{
Name: teleport.ComponentAppProxy,
Kinds: []types.WatchKind{
{
Kind: types.KindWebSession,
SubKind: evenSubKind,
Filter: (&types.WebSessionFilter{User: user}).IntoMap(),
},
},
MetricComponent: teleport.ComponentAppProxy,
})
if err != nil {
return trace.Wrap(err)
}
defer watcher.Close()
matchEvent := func(event types.Event) (types.Resource, error) {
if event.Type == types.OpPut &&
event.Resource.GetKind() == types.KindWebSession &&
event.Resource.GetSubKind() == evenSubKind &&
event.Resource.GetName() == sessionID {
return event.Resource, nil
}
return nil, trace.CompareFailed("no match")
}
_, err = local.WaitForEvent(ctx, watcher, local.EventMatcherFunc(matchEvent), clockwork.NewRealClock())
if err != nil {
logger.WithError(err).Warn("Failed to wait for web session.")
// See again if we maybe missed the event but the session was actually created.
if _, err := req.getSessionFn(ctx, sessionID); err == nil {
return nil
}
}
return trace.Wrap(err)
}
// generateAppToken generates an JWT token that will be passed along with every
// application request.
func (s *Server) generateAppToken(ctx context.Context, username string, roles []string, traits map[string][]string, uri string, expires time.Time) (string, error) {
// Get the clusters CA.
clusterName, err := s.GetDomainName()
if err != nil {
return "", trace.Wrap(err)
}
ca, err := s.GetCertAuthority(ctx, types.CertAuthID{
Type: types.JWTSigner,
DomainName: clusterName,
}, true)
if err != nil {
return "", trace.Wrap(err)
}
// Filter out empty traits so the resulting JWT doesn't have a bunch of
// entries with nil values.
filteredTraits := map[string][]string{}
for trait, values := range traits {
if len(values) > 0 {
filteredTraits[trait] = values
}
}
// Extract the JWT signing key and sign the claims.
signer, err := s.GetKeyStore().GetJWTSigner(ctx, ca)
if err != nil {
return "", trace.Wrap(err)
}
privateKey, err := services.GetJWTSigner(signer, ca.GetClusterName(), s.clock)
if err != nil {
return "", trace.Wrap(err)
}
token, err := privateKey.Sign(jwt.SignParams{
Username: username,
Roles: roles,
Traits: filteredTraits,
URI: uri,
Expires: expires,
})
if err != nil {
return "", trace.Wrap(err)
}
return token, nil
}
func (s *Server) CreateWebSessionFromReq(ctx context.Context, req types.NewWebSessionRequest) (types.WebSession, error) {
session, err := s.NewWebSession(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
err = s.upsertWebSession(ctx, req.User, session)
if err != nil {
return nil, trace.Wrap(err)
}
return session, nil
}
func (s *Server) CreateSessionCert(user types.User, sessionTTL time.Duration, publicKey []byte, compatibility, routeToCluster, kubernetesCluster string, attestationReq *keys.AttestationStatement) ([]byte, []byte, error) {
// It's safe to extract the access info directly from services.User because
// this occurs during the initial login before the first certs have been
// generated, so there's no possibility of any active access requests.
accessInfo := services.AccessInfoFromUser(user)
clusterName, err := s.GetClusterName()
if err != nil {
return nil, nil, trace.Wrap(err)
}
checker, err := services.NewAccessChecker(accessInfo, clusterName.GetClusterName(), s)
if err != nil {
return nil, nil, trace.Wrap(err)
}
certs, err := s.generateUserCert(certRequest{
user: user,
ttl: sessionTTL,
publicKey: publicKey,
compatibility: compatibility,
checker: checker,
traits: user.GetTraits(),
routeToCluster: routeToCluster,
kubernetesCluster: kubernetesCluster,
attestationStatement: attestationReq,
})
if err != nil {
return nil, nil, trace.Wrap(err)
}
return certs.SSH, certs.TLS, nil
}
func (s *Server) CreateSnowflakeSession(ctx context.Context, req types.CreateSnowflakeSessionRequest,
identity tlsca.Identity, checker services.AccessChecker,
) (types.WebSession, error) {
if !modules.GetModules().Features().DB {
return nil, trace.AccessDenied(
"this Teleport cluster is not licensed for database access, please contact the cluster administrator")
}
// Don't let the app session go longer than the identity expiration,
// which matches the parent web session TTL as well.
//
// When using web-based app access, the browser will send a cookie with
// sessionID which will be used to fetch services.WebSession which
// contains a certificate whose life matches the life of the session
// that will be used to establish the connection.
ttl := checker.AdjustSessionTTL(identity.Expires.Sub(s.clock.Now()))
// Create services.WebSession for this session.
sessionID, err := utils.CryptoRandomHex(SessionTokenBytes)
if err != nil {
return nil, trace.Wrap(err)
}
session, err := types.NewWebSession(sessionID, types.KindSnowflakeSession, types.WebSessionSpecV2{
User: req.Username,
Expires: s.clock.Now().Add(ttl),
BearerToken: req.SessionToken,
BearerTokenExpires: s.clock.Now().Add(req.TokenTTL),
})
if err != nil {
return nil, trace.Wrap(err)
}
if err = s.UpsertSnowflakeSession(ctx, session); err != nil {
return nil, trace.Wrap(err)
}
log.Debugf("Generated Snowflake web session for %v with TTL %v.", req.Username, ttl)
return session, nil
}
func (s *Server) CreateSAMLIdPSession(ctx context.Context, req types.CreateSAMLIdPSessionRequest,
identity tlsca.Identity, checker services.AccessChecker,
) (types.WebSession, error) {
// TODO(mdwn): implement a module.Features() check.
// Create services.WebSession for this session.
session, err := types.NewWebSession(req.SessionID, types.KindSAMLIdPSession, types.WebSessionSpecV2{
User: req.Username,
Expires: req.SAMLSession.ExpireTime,
SAMLSession: req.SAMLSession,
})
if err != nil {
return nil, trace.Wrap(err)
}
if err = s.UpsertSAMLIdPSession(ctx, session); err != nil {
return nil, trace.Wrap(err)
}
log.Debugf("Generated SAML IdP web session for %v.", req.Username)
return session, nil
}