22package context
33
44import (
5- "context"
65 "net/http"
76
7+ "github.com/sebest/xff"
88 "k8s.io/apiserver/pkg/endpoints/request"
99 "k8s.io/client-go/transport"
1010)
@@ -20,37 +20,55 @@ const (
2020
2121 // bearerTokenKey is the context key for the bearer token.
2222 bearerTokenKey
23+
24+ // bearerTokenKey is the context key for the client address.
25+ clientAddressKey
2326)
2427
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+ // WithNoImpersonation returns a copy of the request in which the noImpersonation context value is set.
29+ func WithNoImpersonation (req * http. Request ) * http. Request {
30+ return req . WithContext ( request .WithValue (req . Context () , noImpersonationKey , true ) )
2831}
2932
30- // NoImpersonation returns whether the noImpersonation key has been set
31- func NoImpersonation (ctx context. Context ) bool {
32- noImp , _ := ctx .Value (noImpersonationKey ).(bool )
33+ // NoImpersonation returns whether the noImpersonation context key has been set
34+ func NoImpersonation (req * http. Request ) bool {
35+ noImp , _ := req . Context () .Value (noImpersonationKey ).(bool )
3336 return noImp
3437}
3538
3639// 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 )
40+ func WithImpersonationConfig (req * http. Request , conf * transport.ImpersonationConfig ) * http. Request {
41+ return req . WithContext ( request .WithValue (req . Context () , impersonationConfigKey , conf ) )
3942}
4043
4144// 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 )
45+ func ImpersonationConfig (req * http. Request ) * transport.ImpersonationConfig {
46+ conf , _ := req . Context () .Value (impersonationConfigKey ).(* transport.ImpersonationConfig )
4447 return conf
4548}
4649
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+ // WithBearerToken will add the bearer token to the request context from an http.Header to the request context.
51+ func WithBearerToken (req * http. Request , header http.Header ) * http. Request {
52+ return req . WithContext ( request .WithValue (req . Context () , bearerTokenKey , header .Get ("Authorization" ) ))
5053}
5154
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+ // BearerToken will return the bearer token stored in the request context.
56+ func BearerToken (req * http. Request ) string {
57+ token , _ := req . Context () .Value (bearerTokenKey ).(string )
5558 return token
5659}
60+
61+ // RemoteAddress will attempt to return the source client address if available
62+ // in the request context. If it is not, it will be gathered from the request
63+ // and entered into the context.
64+ func RemoteAddr (req * http.Request ) (* http.Request , string ) {
65+ ctx := req .Context ()
66+
67+ clientAddress , ok := ctx .Value (clientAddressKey ).(string )
68+ if ! ok {
69+ clientAddress = xff .GetRemoteAddr (req )
70+ req = req .WithContext (request .WithValue (ctx , clientAddressKey , clientAddress ))
71+ }
72+
73+ return req , clientAddress
74+ }
0 commit comments