2
2
package context
3
3
4
4
import (
5
- "context"
6
5
"net/http"
7
6
7
+ "github.com/sebest/xff"
8
8
"k8s.io/apiserver/pkg/endpoints/request"
9
9
"k8s.io/client-go/transport"
10
10
)
@@ -20,37 +20,55 @@ const (
20
20
21
21
// bearerTokenKey is the context key for the bearer token.
22
22
bearerTokenKey
23
+
24
+ // bearerTokenKey is the context key for the client address.
25
+ clientAddressKey
23
26
)
24
27
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 ) )
28
31
}
29
32
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 )
33
36
return noImp
34
37
}
35
38
36
39
// 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 ) )
39
42
}
40
43
41
44
// 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 )
44
47
return conf
45
48
}
46
49
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" ) ))
50
53
}
51
54
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 )
55
58
return token
56
59
}
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