Skip to content

Commit 9e5b4ae

Browse files
Merge pull request #15153 from openshift-cherrypick-robot/cherry-pick-15127-to-release-4.18
[release-4.18] OCPBUGS-57181: Fix '/metrics' endpoint token review to handle interna…
2 parents 33a390b + a6e4914 commit 9e5b4ae

File tree

6 files changed

+58
-34
lines changed

6 files changed

+58
-34
lines changed

pkg/auth/oauth2/auth.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,7 @@ func (a *OAuth2Authenticator) LoginFunc(w http.ResponseWriter, r *http.Request)
304304
http.Redirect(w, r, a.oauth2Config().AuthCodeURL(state), http.StatusSeeOther)
305305
}
306306

307-
func (a *OAuth2Authenticator) ReviewToken(r *http.Request) error {
308-
token, err := sessions.GetSessionTokenFromCookie(r)
309-
if err != nil {
310-
return err
311-
}
312-
307+
func (a *OAuth2Authenticator) ReviewToken(ctx context.Context, token string) error {
313308
tokenReview := &authv1.TokenReview{
314309
TypeMeta: metav1.TypeMeta{
315310
APIVersion: "authentication.k8s.io/v1",
@@ -324,7 +319,7 @@ func (a *OAuth2Authenticator) ReviewToken(r *http.Request) error {
324319
internalK8sClientset.
325320
AuthenticationV1().
326321
TokenReviews().
327-
Create(r.Context(), tokenReview, metav1.CreateOptions{})
322+
Create(ctx, tokenReview, metav1.CreateOptions{})
328323

329324
if err != nil {
330325
return fmt.Errorf("failed to create TokenReview, %v", err)

pkg/auth/static/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package static
22

33
import (
4+
"context"
45
"net/http"
56

67
"github.com/openshift/console/pkg/auth"
@@ -22,7 +23,7 @@ func (s *StaticAuthenticator) Authenticate(w http.ResponseWriter, req *http.Requ
2223
return &userCopy, nil
2324
}
2425

25-
func (s *StaticAuthenticator) ReviewToken(r *http.Request) error {
26+
func (s *StaticAuthenticator) ReviewToken(ctx context.Context, token string) error {
2627
return nil
2728
}
2829

pkg/auth/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package auth
22

33
import (
4+
"context"
45
"net/http"
56

67
"github.com/openshift/console/pkg/auth/sessions"
78
)
89

910
type Authenticator interface {
1011
Authenticate(w http.ResponseWriter, req *http.Request) (*User, error)
11-
ReviewToken(r *http.Request) error
12+
ReviewToken(context context.Context, token string) error
1213

1314
LoginFunc(w http.ResponseWriter, req *http.Request)
1415
LogoutFunc(w http.ResponseWriter, req *http.Request)

pkg/metrics/handler.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

pkg/server/middleware.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,51 @@ func authMiddlewareWithUser(authenticator auth.Authenticator, csrfVerifier *csrf
4444
}
4545

4646
func withTokenReview(authenticator auth.Authenticator, h http.HandlerFunc) http.HandlerFunc {
47+
return func(w http.ResponseWriter, r *http.Request) {
48+
user, err := authenticator.Authenticate(w, r)
49+
if err != nil {
50+
klog.V(4).Infof("TOKEN_REVIEW: '%s %s' unauthorized, unable to authenticate, %v", r.Method, r.URL.Path, err)
51+
w.WriteHeader(http.StatusUnauthorized)
52+
return
53+
}
54+
55+
err = authenticator.ReviewToken(r.Context(), user.Token)
56+
if err != nil {
57+
klog.V(4).Infof("TOKEN_REVIEW: '%s %s' unauthorized, invalid user token, %v", r.Method, r.URL.Path, err)
58+
w.WriteHeader(http.StatusUnauthorized)
59+
return
60+
}
61+
klog.V(4).Infof("TOKEN_REVIEW: '%s %s' user token successfully validated", r.Method, r.URL.Path)
62+
h(w, r)
63+
}
64+
}
65+
66+
func withBearerTokenReview(authenticator auth.Authenticator, h http.HandlerFunc) http.HandlerFunc {
4767
return http.HandlerFunc(
4868
func(w http.ResponseWriter, r *http.Request) {
49-
err := authenticator.ReviewToken(r)
69+
authorizationHeader := r.Header.Get("Authorization")
70+
if authorizationHeader == "" {
71+
klog.V(4).Infof("TOKEN_REVIEW: '%s %s' unauthorized, missing Authorization header", r.Method, r.URL.Path)
72+
w.WriteHeader(http.StatusUnauthorized)
73+
return
74+
}
75+
76+
if !strings.HasPrefix(authorizationHeader, "Bearer ") {
77+
klog.V(4).Infof("TOKEN_REVIEW: '%s %s' unauthorized, 'Bearer' type Authorization header required", r.Method, r.URL.Path)
78+
w.WriteHeader(http.StatusUnauthorized)
79+
return
80+
}
81+
82+
bearerToken := strings.TrimPrefix(authorizationHeader, "Bearer ")
83+
if bearerToken == "" {
84+
klog.V(4).Infof("TOKEN_REVIEW: '%s %s' unauthorized, empty or missing Bearer token", r.Method, r.URL.Path)
85+
w.WriteHeader(http.StatusUnauthorized)
86+
return
87+
}
88+
89+
err := authenticator.ReviewToken(r.Context(), bearerToken)
5090
if err != nil {
51-
klog.Errorf("TOKEN_REVIEW: '%s %s' unauthorized, invalid user token, %v", r.Method, r.URL.Path, err)
91+
klog.V(4).Infof("TOKEN_REVIEW: '%s %s' unauthorized, invalid user token, %v", r.Method, r.URL.Path, err)
5292
w.WriteHeader(http.StatusUnauthorized)
5393
return
5494
}

pkg/server/server.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/openshift/console/pkg/graphql/resolver"
3232
helmhandlerspkg "github.com/openshift/console/pkg/helm/handlers"
3333
"github.com/openshift/console/pkg/knative"
34-
"github.com/openshift/console/pkg/metrics"
3534
"github.com/openshift/console/pkg/olm"
3635
"github.com/openshift/console/pkg/plugins"
3736
"github.com/openshift/console/pkg/proxy"
@@ -292,8 +291,13 @@ func (s *Server) HTTPHandler() (http.Handler, error) {
292291
}
293292

294293
tokenReviewHandler := func(h http.HandlerFunc) http.HandlerFunc {
295-
return authHandler(withTokenReview(authenticator, h))
294+
return s.CSRFVerifier.WithCSRFVerification(withTokenReview(authenticator, h))
296295
}
296+
297+
bearerTokenReviewHandler := func(h http.HandlerFunc) http.HandlerFunc {
298+
return withBearerTokenReview(authenticator, h)
299+
}
300+
297301
handleFunc(authLoginEndpoint, s.Authenticator.LoginFunc)
298302
handleFunc(authLogoutEndpoint, allowMethod(http.MethodPost, s.handleLogout))
299303
handleFunc(AuthLoginCallbackEndpoint, s.Authenticator.CallbackFunc(fn))
@@ -603,11 +607,10 @@ func (s *Server) HTTPHandler() (http.Handler, error) {
603607
prometheus.MustRegister(usageMetrics.GetCollectors()...)
604608
prometheus.MustRegister(s.AuthMetrics.GetCollectors()...)
605609

606-
handle("/metrics", metrics.AddHeaderAsCookieMiddleware(
607-
tokenReviewHandler(func(w http.ResponseWriter, r *http.Request) {
608-
promhttp.Handler().ServeHTTP(w, r)
609-
}),
610-
))
610+
handle("/metrics", bearerTokenReviewHandler(func(w http.ResponseWriter, r *http.Request) {
611+
promhttp.Handler().ServeHTTP(w, r)
612+
}))
613+
611614
handleFunc("/metrics/usage", tokenReviewHandler(func(w http.ResponseWriter, r *http.Request) {
612615
usage.Handle(usageMetrics, w, r)
613616
}))

0 commit comments

Comments
 (0)