-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
642 lines (561 loc) · 21 KB
/
server.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
package server
import (
"context"
"crypto/rand"
"crypto/subtle"
"crypto/tls"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/alexedwards/scs"
"github.com/alexedwards/scs/stores/memstore"
"github.com/alexedwards/scs/stores/pgstore"
util "github.com/coreos/go-oidc/oidc"
"github.com/gorilla/mux"
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"github.com/chef/automate/components/session-service/migration"
"github.com/chef/automate/components/session-service/oidc"
"github.com/chef/automate/lib/grpc/secureconn"
"github.com/chef/automate/lib/logger"
"github.com/chef/automate/lib/tls/certs"
)
// This determines how often the PG backend will cleanup expired session
// records. It does not affect session expiration; only records that have
// already expired are dropped.
// Note 2017/12/07 (sr): 12hrs here is completely arbitrary.
const dbCleanupInterval = 12 * time.Hour
// This duration drives the refresh process: if the token expires within the
// next minute, we'll refresh. This is a first guess and might need tweaking.
const remainingDuration = time.Minute
// servingStatus is our -- currently hardcoded -- /health endpoint response.
// It mimics the grpc.health.v1 response we return in GRPC services.
const servingStatus = "SERVING"
// BldrClient holds the config for the bldr oauth2 client.
type BldrClient struct {
SignInURL *url.URL
ClientID string
ClientSecret string
}
// Server holds the server state
type Server struct {
mux http.Handler
log logger.Logger
mgr *scs.Manager
client oidc.Client
bldrClient *BldrClient
signInURL *url.URL
remainingDuration time.Duration
serviceCerts *certs.ServiceCerts
tokenCache *cache.Cache
}
const (
refreshTokenKey = "refresh_token"
relayStateKey = "relay_state" // random, passed to OIDC IdP
clientStateKey = "client_state" // coming from the browser, passed back on success
numRelayStateRandomBytes = 10
redirectURIKey = "redirect_uri"
)
// New returns a new instance of the server
func New(
l logger.Logger,
migrationConfig *migration.Config,
oidcCfg oidc.Config,
bldrClient *BldrClient,
signInURL *url.URL,
serviceCerts *certs.ServiceCerts) (*Server, error) {
oidcClient, err := oidc.New(oidcCfg, 1, serviceCerts, l)
if err != nil {
return nil, errors.Wrap(err, "OIDC client")
}
if err := migrationConfig.Go(); err != nil { // nolint: vetshadow
return nil, errors.Wrap(err, "migrations")
}
store, err := initStore(migrationConfig.PG)
if err != nil {
return nil, errors.Wrap(err, "init store")
}
scsManager := createSCSManager(store)
s := Server{
log: l,
client: oidcClient,
signInURL: signInURL,
bldrClient: bldrClient,
remainingDuration: remainingDuration,
mgr: scsManager,
serviceCerts: serviceCerts,
tokenCache: cache.New(1*time.Minute, 5*time.Minute),
}
s.initHandlers()
return &s, nil
}
// NewInMemory instantiates a memory-backed instance of the server, to be used
// for testing.
func NewInMemory(
l logger.Logger,
remainingDuration time.Duration,
oidcCfg oidc.Config,
signInURL *url.URL,
bldrClient *BldrClient,
serviceCerts *certs.ServiceCerts) (*Server, error) {
oidcClient, err := oidc.New(oidcCfg, 0, serviceCerts, l)
if err != nil {
return nil, errors.Wrap(err, "OIDC client")
}
scsManager := createSCSManager(memstore.New(time.Hour))
s := Server{
log: l,
client: oidcClient,
signInURL: signInURL,
bldrClient: bldrClient,
remainingDuration: remainingDuration,
mgr: scsManager,
tokenCache: cache.New(1*time.Minute, 5*time.Minute),
}
s.initHandlers()
return &s, nil
}
func initStore(pgURL *url.URL) (scs.Store, error) {
db, err := sql.Open("postgres", pgURL.String())
if err != nil {
return nil, errors.Wrap(err, "open DB")
}
return pgstore.New(db, dbCleanupInterval), nil
}
func (s *Server) initHandlers() {
r := mux.NewRouter()
r.HandleFunc("/health", s.healthHandler).Methods("GET")
r.HandleFunc("/new", s.newHandler).Methods("GET")
r.HandleFunc("/refresh", s.refreshHandler).
Methods("GET").
HeadersRegexp("Cookie", "session=.+")
r.HandleFunc("/callback", s.callbackHandler).
Methods("GET").
HeadersRegexp("Cookie", "session=.+")
// these are only to be used if Builder is configured to authenticate with Automate
r.HandleFunc("/token", s.tokenHandler).
Methods("POST")
r.HandleFunc("/userinfo", s.userinfoHandler).
Methods("GET")
r.PathPrefix("/").HandlerFunc(s.catchAllElseHandler)
// ^ if none of the above match, it's going to be a 401.
s.mux = r
}
// ListenAndServe starts a listener, and serves requests using Server's embedded
// http.ServeMux. Only ever returns with non-nil error.
func (s *Server) ListenAndServe(addr string) error {
s.log.Debugf("listening (https) on %s", addr)
httpServer := http.Server{
Addr: addr,
Handler: s.mux,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{*s.serviceCerts.ServiceKeyPair},
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
CipherSuites: secureconn.DefaultCipherSuites(),
},
}
return httpServer.ListenAndServeTLS("", "")
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
func (s *Server) catchAllElseHandler(w http.ResponseWriter, _ *http.Request) {
httpError(w, http.StatusUnauthorized)
}
// Authorization redirect callback from OAuth2 auth flow.
func (s *Server) callbackHandler(w http.ResponseWriter, r *http.Request) {
if errMsg := r.FormValue("error"); errMsg != "" {
http.Error(w, errMsg+": "+r.FormValue("error_description"), http.StatusBadRequest)
return
}
sess := s.mgr.Load(r)
relayState, err := sess.GetString(relayStateKey)
if err != nil {
s.log.Debugf("bad session data (relay state): %v", err)
http.Error(w, "bad session data (relay state)", http.StatusBadRequest)
return
}
s.log.Debugf("retrieved relay state %q", relayState)
code := r.FormValue("code")
if code == "" {
s.log.Debugf("no code in request: %q", r.Form)
http.Error(w, fmt.Sprintf("no code in request: %q", r.Form), http.StatusBadRequest)
return
}
if state := r.FormValue("state"); state != relayState {
s.log.Debugf("expected state %q got %q", relayState, state)
http.Error(w, fmt.Sprintf("expected state %q got %q", relayState, state), http.StatusBadRequest)
return
}
token, err := s.client.Exchange(r.Context(), code)
if err != nil {
s.log.Debugf("failed to get token: %v", err)
http.Error(w, fmt.Sprintf("failed to get token: %v", err), http.StatusInternalServerError)
return
}
// this code variable is stored for use by the builder authn flow in the token handler
code2, err := generateRelayState()
if err != nil {
s.log.Errorf("couldn't generate random relay state: %s", err)
httpError(w, http.StatusInternalServerError)
return
}
s.tokenCache.Set(code2, token, cache.DefaultExpiration)
rawIDToken, ok := token.Extra("id_token").(string)
if !ok {
s.log.Debug("no id_token in token response")
http.Error(w, "no id_token in token response", http.StatusInternalServerError)
return
}
if err := sess.RenewToken(w); err != nil { // nolint: vetshadow
s.log.Debugf("failed to renew session token: %v", err)
http.Error(w, errors.Wrap(err, "failed to renew session token").Error(), http.StatusInternalServerError)
return
}
if token.RefreshToken != "" {
err = sess.PutString(w, "refresh_token", token.RefreshToken)
if err != nil {
s.log.Debugf("failed to set session token: %v", err)
http.Error(w, "failed to set refresh_token", http.StatusInternalServerError)
return
}
}
clientState, err := sess.GetString(clientStateKey)
s.log.Debugf("retrieved clientState %q", clientState)
if err != nil {
s.log.Debugf("bad session data (client state): %v", err)
http.Error(w, "bad session data (client state)", http.StatusBadRequest)
return
}
redirectURI, err := sess.GetString(redirectURIKey)
s.log.Debugf("retrieved redirectURI %q", redirectURI)
if err != nil {
s.log.Debugf("bad session data (redirect uri): %v", err)
http.Error(w, "bad session data (redirect uri)", http.StatusBadRequest)
return
}
u := new(url.URL)
// different responses based on if the auth request came from builder or automate
if redirectURI != "" {
*u = *s.bldrClient.SignInURL
u.RawQuery = fmt.Sprintf("state=%s&code=%s", clientState, code2)
// In this flow, we don't need to keep a session -- there's no refresh yet.
err = sess.Destroy(w)
if err != nil {
s.log.Debugf("failed to destroy session: %v", err)
http.Error(w, "failed to destroy session", http.StatusInternalServerError)
return
}
http.Redirect(w, r, u.String(), http.StatusSeeOther)
} else {
*u = *s.signInURL
u.Fragment = fmt.Sprintf("id_token=%s&state=%s", rawIDToken, clientState)
http.Redirect(w, r, u.String(), http.StatusSeeOther)
}
}
func (s *Server) tokenHandler(w http.ResponseWriter, r *http.Request) {
// Need to perform all the required validation for the access token portion
// of the Authorization Code Flow.
//
// TODO (tc): We'd also want to prevent replay attacks, but we'd need a database (or at least a cache) for that:
// "Preventing Replay Attacks" https://www.oauth.com/oauth2-servers/access-tokens/authorization-code-request/
// If bldr was not configured.
if s.bldrClient == nil {
http.Error(w,
"no Oauth2 clients were configured; if you are trying to authenticate for builder, "+
"configure session-service's bldr settings",
http.StatusUnauthorized)
return
}
// We currently only support the bldr client.
if r.FormValue("client_id") != s.bldrClient.ClientID {
http.Error(w,
fmt.Sprintf("client_id %s was submitted to /token but /token currently only supports bldr Oauth2 clients. "+
"submit the client_id %s with the proper secret.",
r.FormValue("client_id"), s.bldrClient.ClientID),
http.StatusUnauthorized)
return
}
// Check that the redirect_uri passed is an exact match of the redirect URI that was used
// when generating the authorization code. Since we asserted that the redirect_uri matched
// our config for bldr on auth code generation, if we assert that again, we know they match.
//
// For more info, read "Granting Access Tokens" here:
// https://www.oauth.com/oauth2-servers/redirect-uris/redirect-uri-validation/
if redirectURI := r.FormValue("redirect_uri"); redirectURI != s.bldrClient.SignInURL.String() {
s.invalidRedirectURIError(w, s.bldrClient.ClientID, redirectURI, s.bldrClient.SignInURL.String())
return
}
// Validate the client secret. See "Step 4: Application Requests Access Token" for more info:
// https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2
// Do so while avoiding timing attacks (see safeCmpStrings).
if !safeCmpStrings(r.FormValue("client_secret"), s.bldrClient.ClientSecret) {
http.Error(w,
fmt.Sprintf("client_secret passed for client %s does not match stored value", s.bldrClient.ClientID),
http.StatusUnauthorized)
return
}
// We are good to grant for the bldr case
s.log.Debugf("redirect_uri and client_secret valid for client_id %s", s.bldrClient.ClientID)
// need to grab 'code' from the request body, exchange for token, return token
code := r.PostFormValue("code")
if "" == code {
http.Error(w, fmt.Sprint("no code in request"), http.StatusBadRequest)
return
}
token, ok := s.tokenCache.Get(code)
if !ok {
s.log.Debugf("failed to get token %v from map", code)
http.Error(w, fmt.Sprint("failed to get token"), http.StatusInternalServerError)
return
}
rawIDToken, ok := token.(*oauth2.Token).Extra("id_token").(string)
if !ok {
s.log.Debug("no id_token in token response")
http.Error(w, "no id_token in token response", http.StatusInternalServerError)
return
}
returnData := struct {
AccessToken string `json:"access_token"`
}{rawIDToken}
if err := json.NewEncoder(w).Encode(returnData); err != nil {
http.Error(w, errors.Wrap(err, "failed to set access token").Error(),
http.StatusInternalServerError)
return
}
}
func (s *Server) userinfoHandler(w http.ResponseWriter, r *http.Request) {
token, err := util.ExtractBearerToken(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
idToken, err := s.client.Verify(r.Context(), token)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
var claims struct {
Username string `json:"email"`
}
err = idToken.Claims(&claims)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
returnData := struct {
Sub string `json:"sub"`
PreferredUsername string `json:"preferred_username"`
}{idToken.Subject, claims.Username}
if err := json.NewEncoder(w).Encode(returnData); err != nil {
http.Error(w, errors.Wrap(err, "failed to set user info").Error(),
http.StatusInternalServerError)
return
}
}
// Note 2017/12/11 (sr): /refresh is called periodically by the UI, irregardless
// of whether we're using a connector (dex) that allows for refresh_tokens. To
// make that work, we default to returning the caller's id_token if it's still
// valid.
// That also ensures that our logic does not depend on client cooperation. The
// only (and correct) way to enforce session refresh is thus to hand out
// id_tokens with short expiry.
func (s *Server) refreshHandler(w http.ResponseWriter, r *http.Request) {
sess := s.mgr.Load(r)
refreshToken, err := sess.GetString(refreshTokenKey)
if err != nil {
http.Error(w, "bad session data", http.StatusBadRequest)
return
}
// Note: refreshToken may be "" here (e.g. for SAML); we don't bother, since
// as long as the idToken doesn't expire soon,
// maybeExchangeRefreshTokenForIDToken will return the still-valid idToken.
// Check if the session exists
// Note 2018/01/05 (sr): Couldn't find a better way.
if keys, err := sess.Keys(); err != nil { // nolint: vetshadow
httpError(w, http.StatusInternalServerError)
return
} else if len(keys) == 0 {
httpError(w, http.StatusUnauthorized)
return
}
idToken, err := util.ExtractBearerToken(r)
if err != nil {
httpError(w, http.StatusUnauthorized)
return
}
// Renew session: This ensures the old session is gone and cannot be re-used
if err := sess.RenewToken(w); err != nil { // nolint: vetshadow
s.log.Error("failed to renew token for session") // TODO this is too unspecific
// do we not know anything else to identify this request?
httpError(w, http.StatusInternalServerError)
return
}
// TODO 2017/12/11 (sr): should we kill the session on failure here?
token, err := s.maybeExchangeRefreshTokenForIDToken(r.Context(), refreshToken, idToken)
if err != nil {
s.log.Debugf("failed to exchange token: %s", err)
httpError(w, http.StatusUnauthorized)
return
}
rawIDToken, ok := token.Extra("id_token").(string)
if !ok {
http.Error(w, "no id_token in token response", http.StatusInternalServerError)
return
}
if token.RefreshToken != "" {
if err := sess.PutString(w, refreshTokenKey, token.RefreshToken); err != nil {
http.Error(w, "failed to set refresh_token", http.StatusInternalServerError)
return
}
}
returnData := struct {
IDToken string `json:"id_token"`
}{rawIDToken}
if err := json.NewEncoder(w).Encode(returnData); err != nil {
http.Error(w, errors.Wrap(err, "failed to set marshal id_token").Error(), http.StatusInternalServerError)
return
}
}
func (s *Server) healthHandler(w http.ResponseWriter, _ *http.Request) {
returnData := struct {
Status string `json:"status"`
}{servingStatus}
if err := json.NewEncoder(w).Encode(returnData); err != nil {
http.Error(w, errors.Wrap(err, "failed to set marshal health status").Error(),
http.StatusInternalServerError)
return
}
}
func (s *Server) newHandler(w http.ResponseWriter, r *http.Request) {
sess := s.mgr.Load(r)
// This could be a session token that has already been leaked. By renewing it
// here, we ensure that the session token sent with this request cannot be
// used afterwards.
//!\\ This mitigates the "stolen session-cookie reused after logout" threat.
if err := sess.RenewToken(w); err != nil { // nolint: vetshadow
httpError(w, http.StatusInternalServerError)
return
}
// TODO (tc): We should do all of this for Automate UI as well. But instead
// of rolling our own, we should just use an Oauth2 library imo.
//
// If the request came from builder and if builder's configured, we should
// 1) validate that the submitted redirect_url matches our config,
// and 2) validate that it matches what they send into the /token handler.
// We'll store what they sent to /new in the session so we can verify it in /token.
// If bldr is not configured, the only valid redirect_uri will be hardcoded to be
// automate UI, so bad actors at least can't maliciously redirect somewhere else, but
// we need to also lock down Automate UI.
//
// Read more under "Authorization Request" here:
// https://www.oauth.com/oauth2-servers/redirect-uris/redirect-uri-validation/
if s.bldrClient != nil {
if r.FormValue("client_id") != "" && r.FormValue("client_id") != s.bldrClient.ClientID {
http.Error(w,
fmt.Sprintf("currently only Habitat Builder is supported for Oauth2 integration. "+
"expected bldr client_id: %s, actual client_id: %s",
s.bldrClient.ClientID, r.FormValue("client_id")),
http.StatusUnauthorized)
return
}
if r.FormValue("client_id") == s.bldrClient.ClientID {
redirectURI := r.FormValue("redirect_uri")
if redirectURI != s.bldrClient.SignInURL.String() {
s.invalidRedirectURIError(w, s.bldrClient.ClientID, s.bldrClient.SignInURL.String(), redirectURI)
return
}
// TODO (tc) This is simply stored so we can determine if a request came from bldr or Automate UI
// when it comes back from the dex request chain in callbackHandler (the actual stored request_uri isn't used).
// It's not ideal but we currently don't have a way of threading redirect_uri back from the dex call chain.
// Might want to consider just switching this to a boolean with a descriptive name like bldrRequest as the
// key in the session.
if err := sess.PutString(w, redirectURIKey, redirectURI); err != nil {
http.Error(w, "failed to store redirect URI in session", http.StatusInternalServerError)
return
}
}
}
relayState, err := generateRelayState()
if err != nil {
s.log.Errorf("couldn't generate random relay state: %s", err)
httpError(w, http.StatusInternalServerError)
return
}
// take state we've gotten from the client, store it
if clientState := r.FormValue("state"); clientState != "" {
s.log.Debugf("storing clientState %s", clientState)
if err := sess.PutString(w, clientStateKey, clientState); err != nil {
http.Error(w, "failed to set client state", http.StatusInternalServerError)
return
}
}
// bind relay state to session
if err := sess.PutString(w, relayStateKey, relayState); err != nil {
s.log.Errorf("couldn't put relay state into session: %s", err)
httpError(w, http.StatusInternalServerError)
return
}
s.log.Debugf("stored relayState %s", relayState)
authCodeURL := s.client.AuthCodeURL(relayState)
http.Redirect(w, r, authCodeURL, http.StatusSeeOther)
}
func httpError(w http.ResponseWriter, code int) {
http.Error(w, http.StatusText(code), code)
}
func (s *Server) maybeExchangeRefreshTokenForIDToken(ctx context.Context,
refreshToken, rawIDToken string) (*oauth2.Token, error) {
idToken, err := s.client.Verify(ctx, rawIDToken)
if err != nil {
return nil, errors.Wrap(err, "verify id_token")
}
t, err := oidc.TokenFromIDToken(idToken, rawIDToken, refreshToken)
if err != nil {
return nil, errors.Wrap(err, "build token from id_token")
}
// This makes a refresh happen although it's not required "just yet" (but soon)
t.Expiry = t.Expiry.Add(-s.remainingDuration)
token, err := s.client.TokenSource(ctx, t).Token()
if err != nil {
return nil, errors.Wrap(err, "exchange refresh token")
}
return token, nil
}
func generateRelayState() (string, error) {
bs := make([]byte, numRelayStateRandomBytes)
n, err := rand.Read(bs)
if err != nil || n != numRelayStateRandomBytes {
return "", errors.Wrap(err, "read random bytes")
}
return base64.URLEncoding.EncodeToString(bs), nil
}
func createSCSManager(store scs.Store) *scs.Manager {
manager := scs.NewManager(store)
manager.Persist(true)
manager.Secure(true)
return manager
}
// To avoid timing attacks[1], we need to first check that the length of the two strings is
// the same (safe since len is a constant time operator for strings[2]) and then use
// subtle to compare the bytes in a constant time operation.
//
// [1] https://codahale.com/a-lesson-in-timing-attacks/
// [2] https://golang.org/ref/spec#Length_and_capacity
func safeCmpStrings(a, b string) bool {
if len(a) != len(b) {
return false
}
return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1
}
func (s *Server) invalidRedirectURIError(w http.ResponseWriter, clientID, expected, actual string) {
http.Error(w,
fmt.Sprintf("failed to pass valid redirect_uri for client_id: %s, expected: %s, registered for client: %s",
clientID, expected, actual),
http.StatusUnauthorized)
}