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

Commit c22316f

Browse files
committed
Updates proxy to use http.Handler with passing context
Signed-off-by: JoshVanL <[email protected]>
1 parent 63ba131 commit c22316f

File tree

4 files changed

+260
-122
lines changed

4 files changed

+260
-122
lines changed

cmd/app/options/options.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ func (o *Options) Validate(cmd *cobra.Command) error {
8080
errs = append(errs, errors.New("unable to securely serve on port 8080 (used by readiness probe)"))
8181
}
8282

83+
if o.App.DisableImpersonation &&
84+
(o.App.ExtraHeaderOptions.EnableClientIPExtraUserHeader || len(o.App.ExtraHeaderOptions.ExtraUserHeaders) > 0) {
85+
errs = append(errs, errors.New("cannot add extra user headers when impersonation disabled"))
86+
}
87+
8388
if len(errs) > 0 {
8489
return k8sErrors.NewAggregate(errs)
8590
}

cmd/app/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
7272
return err
7373
}
7474

75-
proxyOptions := &proxy.Options{
75+
proxyConfig := &proxy.Config{
7676
TokenReview: opts.App.TokenPassthrough.Enabled,
7777
DisableImpersonation: opts.App.DisableImpersonation,
7878

@@ -84,7 +84,7 @@ func buildRunCommand(stopCh <-chan struct{}, opts *options.Options) *cobra.Comma
8484

8585
// Initialise proxy with OIDC token authenticator
8686
p, err := proxy.New(restConfig, opts.OIDCAuthentication,
87-
tokenReviewer, secureServingInfo, proxyOptions)
87+
tokenReviewer, secureServingInfo, proxyConfig)
8888
if err != nil {
8989
return err
9090
}

pkg/proxy/context/context.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright Jetstack Ltd. See LICENSE for details.
2+
package context
3+
4+
import (
5+
"context"
6+
"net/http"
7+
8+
"k8s.io/apiserver/pkg/endpoints/request"
9+
"k8s.io/client-go/transport"
10+
)
11+
12+
type key int
13+
14+
const (
15+
// noImpersonationKey is the context key for whether to use impersonation.
16+
noImpersonationKey key = iota
17+
18+
// impersonationConfigKey is the context key for the impersonation config.
19+
impersonationConfigKey
20+
21+
// bearerTokenKey is the context key for the bearer token.
22+
bearerTokenKey
23+
)
24+
25+
// WithNoImpersonation returns a copy of parent in which the noImpersonation value is set.
26+
func WithNoImpersonation(parent context.Context) context.Context {
27+
return request.WithValue(parent, noImpersonationKey, true)
28+
}
29+
30+
// NoImpersonation returns whether the noImpersonation key has been set
31+
func NoImpersonation(ctx context.Context) bool {
32+
noImp, _ := ctx.Value(noImpersonationKey).(bool)
33+
return noImp
34+
}
35+
36+
// WithImpersonationConfig returns a copy of parent in which contains the impersonation configuration.
37+
func WithImpersonationConfig(parent context.Context, conf *transport.ImpersonationConfig) context.Context {
38+
return request.WithValue(parent, impersonationConfigKey, conf)
39+
}
40+
41+
// ImpersonationConfig returns the impersonation configuration held in the context if existing.
42+
func ImpersonationConfig(ctx context.Context) *transport.ImpersonationConfig {
43+
conf, _ := ctx.Value(impersonationConfigKey).(*transport.ImpersonationConfig)
44+
return conf
45+
}
46+
47+
// WithBearerToken will add the bearer token from an http.Header to the context.
48+
func WithBearerToken(parent context.Context, header http.Header) context.Context {
49+
return request.WithValue(parent, bearerTokenKey, header.Get("Authorization"))
50+
}
51+
52+
// BearerToken will return the bearer token stored in the context.
53+
func BearerToken(ctx context.Context) string {
54+
token, _ := ctx.Value(bearerTokenKey).(string)
55+
return token
56+
}

0 commit comments

Comments
 (0)