Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 1bdbaf5

Browse files
committed
Addressed code structure comments and removed flush interval
Signed-off-by: JoshVanL <[email protected]>
1 parent 3e2c947 commit 1bdbaf5

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

pkg/proxy/proxy.go

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -195,26 +195,14 @@ func (p *Proxy) RoundTrip(req *http.Request) (*http.Response, error) {
195195

196196
// withAuthenticateRequest adds the proxy authentication handler to a chain.
197197
func (p *Proxy) withAuthenticateRequest(handler http.Handler) http.Handler {
198+
tokenReviewHandler := p.withTokenReview(handler)
199+
198200
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
199201
// Auth request and handle unauthed
200202
info, ok, err := p.oidcRequestAuther.AuthenticateRequest(req)
201203
if err != nil {
202-
if !p.config.TokenReview {
203-
p.handleError(rw, req, errUnauthorized)
204-
return
205-
}
206-
207-
// Attempt to passthrough request if valid token
208-
if p.reviewToken(rw, req) {
209-
// Set no impersonation headers and re-add removed headers.
210-
req = req.WithContext(context.WithNoImpersonation(req.Context()))
211-
212-
handler.ServeHTTP(rw, req)
213-
return
214-
}
215-
216-
// Token review failed so error
217-
p.handleError(rw, req, errUnauthorized)
204+
// Since we have failed OIDC auth, we will try a token review, if enabled.
205+
tokenReviewHandler.ServeHTTP(rw, req)
218206
return
219207
}
220208

@@ -232,6 +220,30 @@ func (p *Proxy) withAuthenticateRequest(handler http.Handler) http.Handler {
232220
})
233221
}
234222

223+
// withTokenReview will attempt a token review on the incoming request, if
224+
// enabled.
225+
func (p *Proxy) withTokenReview(handler http.Handler) http.Handler {
226+
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
227+
// If token review is not enabled then error.
228+
if !p.config.TokenReview {
229+
p.handleError(rw, req, errUnauthorized)
230+
return
231+
}
232+
233+
// Attempt to passthrough request if valid token
234+
if !p.reviewToken(rw, req) {
235+
// Token review failed so error
236+
p.handleError(rw, req, errUnauthorized)
237+
return
238+
}
239+
240+
// Set no impersonation headers and re-add removed headers.
241+
req = req.WithContext(context.WithNoImpersonation(req.Context()))
242+
243+
handler.ServeHTTP(rw, req)
244+
})
245+
}
246+
235247
// withImpersonateRequest adds the impersonation request handler to the chain.
236248
func (p *Proxy) withImpersonateRequest(handler http.Handler) http.Handler {
237249
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
@@ -317,21 +329,21 @@ func (p *Proxy) reviewToken(rw http.ResponseWriter, req *http.Request) bool {
317329
req.RemoteAddr)
318330

319331
ok, err := p.tokenReviewer.Review(req)
332+
if err != nil {
333+
klog.Errorf("unable to authenticate the request via TokenReview due to an error (%s): %s",
334+
req.RemoteAddr, err)
335+
return false
336+
}
320337

321-
// No error and ok so passthrough the request
322-
if err == nil && ok {
338+
if !ok {
323339
klog.V(4).Infof("passing request with valid token through (%s)",
324340
req.RemoteAddr)
325341

326-
return true
327-
}
328-
329-
if err != nil {
330-
klog.Errorf("unable to authenticate the request via TokenReview due to an error (%s): %s",
331-
req.RemoteAddr, err)
342+
return false
332343
}
333344

334-
return false
345+
// No error and ok so passthrough the request
346+
return true
335347
}
336348

337349
func (p *Proxy) hasImpersonation(header http.Header) bool {

0 commit comments

Comments
 (0)