-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathserver.go
517 lines (446 loc) · 16 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
package handlers
import (
"fmt"
"net/http"
"net/url"
neturl "net/url"
"strings"
"github.com/mesosphere/traefik-forward-auth/internal/api/storage/v1alpha1"
"github.com/mesosphere/traefik-forward-auth/internal/authentication"
"github.com/mesosphere/traefik-forward-auth/internal/configuration"
"github.com/containous/traefik/pkg/rules"
"github.com/coreos/go-oidc"
"github.com/mesosphere/traefik-forward-auth/internal/authorization/rbac"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"k8s.io/client-go/kubernetes"
"github.com/mesosphere/traefik-forward-auth/internal/authorization"
internallog "github.com/mesosphere/traefik-forward-auth/internal/log"
)
const (
impersonateUserHeader = "Impersonate-User"
impersonateGroupHeader = "Impersonate-Group"
)
// Server implements the HTTP server handling forwardauth
type Server struct {
router *rules.Router
userinfo v1alpha1.UserInfoInterface
authorizer authorization.Authorizer
log logrus.FieldLogger
config *configuration.Config
authenticator *authentication.Authenticator
}
// NewServer creates a new forwardauth server
func NewServer(userinfo v1alpha1.UserInfoInterface, clientset kubernetes.Interface, config *configuration.Config) *Server {
s := &Server{
log: internallog.NewDefaultLogger(config.LogLevel, config.LogFormat),
config: config,
userinfo: userinfo,
authenticator: authentication.NewAuthenticator(config),
}
s.buildRoutes()
s.userinfo = userinfo
if config.EnableRBAC {
rbac := rbac.NewAuthorizer(clientset, s.log)
rbac.CaseInsensitiveSubjects = config.CaseInsensitiveSubjects
s.authorizer = rbac
}
return s
}
func (s *Server) buildRoutes() {
var err error
s.router, err = rules.NewRouter()
if err != nil {
s.log.Fatal(err)
}
// Let's build a router
for name, rule := range s.config.Rules {
var err error
if rule.Action == "allow" {
err = s.router.AddRoute(rule.FormattedRule(), 1, s.AllowHandler(name))
} else {
err = s.router.AddRoute(rule.FormattedRule(), 1, s.AuthHandler(name))
}
if err != nil {
panic(fmt.Sprintf("could not add route: %v", err))
}
}
// Add callback handler
s.router.Handle(s.config.Path, s.AuthCallbackHandler())
// Add a default handler
if s.config.DefaultAction == "allow" {
s.router.NewRoute().Handler(s.AllowHandler("default"))
} else {
s.router.NewRoute().Handler(s.AuthHandler("default"))
}
}
// RootHandler it the main handler (for / path)
func (s *Server) RootHandler(w http.ResponseWriter, r *http.Request) {
logger := s.log.WithFields(logrus.Fields{
"X-Forwarded-Method": r.Header.Get("X-Forwarded-Method"),
"X-Forwarded-Proto": r.Header.Get("X-Forwarded-Proto"),
"X-Forwarded-Host": r.Header.Get("X-Forwarded-Host"),
"X-Forwarded-Prefix": r.Header.Get("X-Forwarded-Prefix"),
"X-Forwarded-Uri": r.Header.Get("X-Forwarded-Uri"),
})
// Modify request
r.Method = r.Header.Get("X-Forwarded-Method")
r.Host = r.Header.Get("X-Forwarded-Host")
r.URL, _ = neturl.Parse(authentication.GetRequestURI(r))
if s.config.AuthHost == "" || len(s.config.CookieDomains) > 0 || r.Host == s.config.AuthHost {
s.router.ServeHTTP(w, r)
} else {
// Redirect the client to the authHost.
url := r.URL
url.Scheme = r.Header.Get("X-Forwarded-Proto")
url.Host = s.config.AuthHost
logger.Debugf("redirect to %v", url.String())
http.Redirect(w, r, url.String(), 307)
}
}
// AllowHandler handles the request as implicite "allow", returining HTTP 200 response to the Traefik
func (s *Server) AllowHandler(rule string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
s.logger(r, rule, "Allow request")
w.WriteHeader(200)
}
}
// AuthHandler handles the request as requiring authentication.
// It validates the existing session, starting a new auth flow if the session is not valid.
// Finally it also performs authorization (if enabled) to ensure the logged-in subject is authorized to perform the request.
func (s *Server) AuthHandler(rule string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Logging setup
logger := s.logger(r, rule, "Authenticate request")
// Get auth cookie
c, err := r.Cookie(s.config.CookieName)
if err != nil {
s.notAuthenticated(logger, w, r)
return
}
// Validate cookie
id, err := s.authenticator.ValidateCookie(r, c)
if err != nil {
logger.Info(fmt.Sprintf("cookie validaton failure: %s", err.Error()))
s.notAuthenticated(logger, w, r)
return
}
// Validate user
valid := s.authenticator.ValidateEmail(id.Email)
if !valid {
logger.WithFields(logrus.Fields{
"email": id.Email,
}).Errorf("Invalid email")
http.Error(w, "Not authorized", 401)
return
}
// Token forwarding requested now with no token stored in the session, reauth
if s.config.ForwardTokenHeaderName != "" && id.Token == "" {
logger.Info("re-auth forced because token forwarding enabled and no token stored")
s.notAuthenticated(logger, w, r)
return
}
// Authorize user
groups, err := s.getGroupsFromSession(r)
if err != nil {
logger.Errorf("error getting groups from session: %v", err)
s.notAuthenticated(logger, w, r)
return
}
if groups == nil {
logger.Info("groups session data is missing, re-authenticating")
s.notAuthenticated(logger, w, r)
return
}
if s.config.EnableRBAC && !s.authzIsBypassed(r) {
kubeUserInfo := s.makeKubeUserInfo(id.Email, groups)
requestURL := authentication.GetRequestURL(r)
targetURL, err := url.Parse(requestURL)
if err != nil {
logger.Errorf("unable to parse target URL %s: %v", requestURL, err)
http.Error(w, "Bad Gateway", 502)
return
}
logger.Debugf("authorizing user: %s, groups: %s", kubeUserInfo.Name, kubeUserInfo.Groups)
authorized, err := s.authorizer.Authorize(kubeUserInfo, r.Method, targetURL)
if err != nil {
logger.Errorf("error while authorizing %s: %v", kubeUserInfo, err)
http.Error(w, "Bad Gateway", 502)
return
}
if !authorized {
logger.Infof("user %s is not authorized to `%s` in %s", kubeUserInfo.GetName(), r.Method, targetURL)
//TODO:k3a: consider some kind of re-auth to recheck for new groups
http.Error(w, "Not Authorized", 401)
return
}
logger.Infof("user %s is authorized to `%s` in %s", kubeUserInfo.GetName(), r.Method, targetURL)
}
// Valid request
logger.Debugf("Allow request from %s", id.Email)
for _, headerName := range s.config.EmailHeaderNames {
w.Header().Set(headerName, id.Email)
}
if s.config.EnableImpersonation {
// Set impersonation headers
logger.Debug(fmt.Sprintf("setting authorization token and impersonation headers: email: %s, groups: %s", id.Email, groups))
w.Header().Set("Authorization", fmt.Sprintf("Bearer %s", s.config.ServiceAccountToken))
w.Header().Set(impersonateUserHeader, id.Email)
w.Header().Set(impersonateGroupHeader, "system:authenticated")
for _, group := range groups {
w.Header().Add(impersonateGroupHeader, fmt.Sprintf("%s%s", s.config.GroupClaimPrefix, group))
}
w.Header().Set("Connection", cleanupConnectionHeader(w.Header().Get("Connection")))
}
if s.config.ForwardTokenHeaderName != "" && id.Token != "" {
w.Header().Add(s.config.ForwardTokenHeaderName, s.config.ForwardTokenPrefix+id.Token)
}
w.WriteHeader(200)
}
}
var removeHeaders = map[string]bool{
strings.ToLower("Authorization"): true,
strings.ToLower(impersonateUserHeader): true,
strings.ToLower(impersonateGroupHeader): true,
}
// Traefik correctly removes any headers listed in the Connection header, but
// because it removes headers after forward auth has run, a specially crafted
// request can forward to the backend with the forward auth headers removed.
// Remove forward auth headers from the Connection header to ensure that they
// get passed to the backend.
func cleanupConnectionHeader(original string) string {
headers := strings.Split(original, ",")
passThrough := make([]string, 0, len(headers))
for _, header := range headers {
if remove := removeHeaders[strings.ToLower(strings.TrimSpace(header))]; !remove {
passThrough = append(passThrough, header)
}
}
return strings.TrimSpace(strings.Join(passThrough, ","))
}
// AuthCallbackHandler handles the request as a callback from authentication provider.
// It validates CSRF, exchanges code-token for id-token and extracts groups from the id-token.
func (s *Server) AuthCallbackHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Logging setup
logger := s.logger(r, "default", "Handling callback")
// Check for CSRF cookie
c, err := r.Cookie(s.config.CSRFCookieName)
if err != nil {
logger.Errorf("missing CSRF cookie: %v", err)
http.Error(w, "Not authorized", 401)
return
}
// Validate state
valid, redirect, err := authentication.ValidateCSRFCookie(r, c)
if !valid {
logger.Errorf("error validating CSRF cookie: %v", err)
http.Error(w, "Not authorized", 401)
return
}
// Clear CSRF cookie
http.SetCookie(w, s.authenticator.ClearCSRFCookie(r))
provider := s.config.OIDCProvider
// Mapping scope
var scope []string
if s.config.Scope != "" {
scope = []string{s.config.Scope}
} else {
scope = []string{oidc.ScopeOpenID, "profile", "email", "groups"}
}
oauth2Config := oauth2.Config{
ClientID: s.config.ClientID,
ClientSecret: s.config.ClientSecret,
RedirectURL: s.authenticator.ComposeRedirectURI(r),
Endpoint: provider.Endpoint(),
Scopes: scope,
}
// Exchange code for token
oauth2Token, err := oauth2Config.Exchange(s.config.OIDCContext, r.URL.Query().Get("code"))
if err != nil {
logger.Errorf("failed to exchange token: %v", err)
http.Error(w, "Bad Gateway", 502)
return
}
// Extract the ID Token from OAuth2 token.
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
logger.Error("missing ID token")
http.Error(w, "Bad Gateway", 502)
return
}
// Parse and verify ID Token payload.
verifier := provider.Verifier(&oidc.Config{ClientID: s.config.ClientID})
idToken, err := verifier.Verify(s.config.OIDCContext, rawIDToken)
if err != nil {
logger.Errorf("failed to verify token: %v", err)
http.Error(w, "Bad Gateway", 502)
return
}
// Extract custom claims
var claims map[string]interface{}
if err := idToken.Claims(&claims); err != nil {
logger.Errorf("failed to extract claims: %v", err)
http.Error(w, "Bad Gateway", 502)
return
}
email, ok := claims["email"]
if ok {
token := ""
if s.config.ForwardTokenHeaderName != "" {
token = rawIDToken
}
// Generate cookies
c, err := s.authenticator.MakeIDCookie(r, email.(string), token)
if err != nil {
logger.Errorf("error generating secure session cookie: %v", err)
http.Error(w, "Bad Gateway", 502)
return
}
http.SetCookie(w, c)
logger.WithFields(logrus.Fields{
"user": claims["email"].(string),
}).Infof("generated auth cookie")
} else {
logger.Warn("no email claim present in the ID token")
}
// If name in null, empty or whitespace, use email address for name
name, ok := claims["name"]
if !ok || (ok && strings.TrimSpace(name.(string)) == "") {
name = email.(string)
}
http.SetCookie(w, s.authenticator.MakeNameCookie(r, name.(string)))
logger.WithFields(logrus.Fields{
"name": name.(string),
}).Info("generated name cookie")
// Mapping groups
groups := []string{}
groupsClaim, ok := claims[s.config.GroupsAttributeName].([]interface{})
if ok {
for _, g := range groupsClaim {
groups = append(groups, g.(string))
}
} else {
logger.Warnf("failed to get groups claim from the ID token (GroupsAttributeName: %s)", s.config.GroupsAttributeName)
}
if err := s.userinfo.Save(r, w, &v1alpha1.UserInfo{
Username: name.(string),
Email: email.(string),
Groups: groups,
}); err != nil {
logger.Errorf("error saving session: %v", err)
http.Error(w, "Bad Gateway", 502)
return
}
// Redirect
http.Redirect(w, r, redirect, http.StatusTemporaryRedirect)
}
}
// notAuthenticated is used to signal the request does not include a valid authentication data.
// If the request came from a browser (having "text/html" in the Accept header), authentication
// redirect is made to start a new auth flow. Otherwise the "Authenticatio expired" message
// is passed as one of the known content-types or as a plain text.
func (s *Server) notAuthenticated(logger *logrus.Entry, w http.ResponseWriter, r *http.Request) {
bestFormat := ""
// Redirect if request accepts HTML. Fail if request is AJAX, image, etc
acceptHeader := r.Header.Get("Accept")
acceptParts := strings.Split(acceptHeader, ",")
for i, acceptPart := range acceptParts {
format := strings.Trim(strings.SplitN(acceptPart, ";", 2)[0], " ")
if format == "text/html" || (i == 0 && format == "*/*") {
s.authRedirect(logger, w, r)
return
} else if strings.HasPrefix(format, "application/json") {
bestFormat = "json"
} else if strings.HasPrefix(format, "application/xml") {
bestFormat = "xml"
}
}
logger.Warnf("Non-HTML request: %v", acceptHeader)
errStr := "Authentication expired. Reload page to re-authenticate."
if bestFormat == "json" {
w.Header().Set("Content-Type", "application/json")
http.Error(w, `{"error": "`+errStr+`"}`, 401)
} else if bestFormat == "xml" {
w.Header().Set("Content-Type", "application/xml")
http.Error(w, `<errors><error>`+errStr+`</error></errors>`, 401)
} else {
http.Error(w, errStr, 401)
}
}
// authRedirect generates CSRF cookie and redirests to authentication provider to start the authentication flow.
func (s *Server) authRedirect(logger *logrus.Entry, w http.ResponseWriter, r *http.Request) {
// Error indicates no cookie, generate nonce
nonce, err := authentication.GenerateNonce()
if err != nil {
logger.Errorf("error generating nonce, %v", err)
http.Error(w, "Service unavailable", 503)
return
}
// Set the CSRF cookie
http.SetCookie(w, s.authenticator.MakeCSRFCookie(r, nonce))
logger.Debug("sending CSRF cookie and a redirect to OIDC login")
// Mapping scope
var scope []string
if s.config.Scope != "" {
scope = []string{s.config.Scope}
} else {
scope = []string{oidc.ScopeOpenID, "profile", "email", "groups"}
}
// clear existing claims session
if err = s.userinfo.Clear(r, w); err != nil {
logger.Errorf("error clearing session: %v", err)
}
oauth2Config := oauth2.Config{
ClientID: s.config.ClientID,
ClientSecret: s.config.ClientSecret,
RedirectURL: s.authenticator.ComposeRedirectURI(r),
Endpoint: s.config.OIDCProvider.Endpoint(),
Scopes: scope,
}
state := fmt.Sprintf("%s:%s", nonce, authentication.GetRequestURL(r))
http.Redirect(w, r, oauth2Config.AuthCodeURL(state), http.StatusFound)
return
}
// logger provides a new logger enriched with request info
func (s *Server) logger(r *http.Request, rule, msg string) *logrus.Entry {
// Create logger
logger := s.log.WithFields(logrus.Fields{
"source_ip": r.Header.Get("X-Forwarded-For"),
})
// Log request
logger.WithFields(logrus.Fields{
"rule": rule,
"headers": r.Header,
}).Debug(msg)
return logger
}
// getGroupsFromSession returns list of groups present in the session
func (s *Server) getGroupsFromSession(r *http.Request) ([]string, error) {
userInfo, err := s.userinfo.Get(r)
if err != nil {
return nil, err
}
return userInfo.Groups, nil
}
// authzIsBypassed returns true if the request matches a bypass URI pattern
func (s *Server) authzIsBypassed(r *http.Request) bool {
for _, bypassURIPattern := range s.config.AuthZPassThrough {
if authorization.URLMatchesWildcardPattern(r.URL.Path, bypassURIPattern) {
s.log.Infof("authorization is disabled for %s", r.URL.Path)
return true
}
}
return false
}
// makeKubeUserInfo appends group prefix to all provided groups and adds "system:authenticated" group to the list
func (s *Server) makeKubeUserInfo(email string, groups []string) authorization.User {
g := []string{"system:authenticated"}
for _, group := range groups {
g = append(g, fmt.Sprintf("%s%s", s.config.GroupClaimPrefix, group))
}
return authorization.User{
Name: email,
Groups: g,
}
}