@@ -5,52 +5,23 @@ import (
5
5
6
6
"k8s.io/apimachinery/pkg/runtime/schema"
7
7
"k8s.io/apimachinery/pkg/types"
8
- utilnet "k8s.io/apimachinery/pkg/util/net"
9
8
"k8s.io/apiserver/pkg/authentication/user"
10
9
restclient "k8s.io/client-go/rest"
10
+ "k8s.io/client-go/transport"
11
11
"k8s.io/client-go/util/flowcontrol"
12
12
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
13
-
14
- authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
15
- )
16
-
17
- const (
18
- ImpersonateUserHeader = "Impersonate-User"
19
- ImpersonateGroupHeader = "Impersonate-Group"
20
- ImpersonateUserScopeHeader = "Impersonate-User-Scope"
21
13
)
22
14
23
- type impersonatingRoundTripper struct {
24
- user user.Info
25
- delegate http.RoundTripper
26
- }
27
-
28
- // newImpersonatingRoundTripper will add headers to impersonate a user, including user, groups, and scopes
29
- func newImpersonatingRoundTripper (user user.Info , delegate http.RoundTripper ) http.RoundTripper {
30
- return & impersonatingRoundTripper {user : user , delegate : delegate }
31
- }
32
-
33
- func (rt * impersonatingRoundTripper ) RoundTrip (req * http.Request ) (* http.Response , error ) {
34
- req = utilnet .CloneRequest (req )
35
- req .Header .Del (ImpersonateUserHeader )
36
- req .Header .Del (ImpersonateGroupHeader )
37
- req .Header .Del (ImpersonateUserScopeHeader )
38
-
39
- req .Header .Set (ImpersonateUserHeader , rt .user .GetName ())
40
- for _ , group := range rt .user .GetGroups () {
41
- req .Header .Add (ImpersonateGroupHeader , group )
42
- }
43
- for _ , scope := range rt .user .GetExtra ()[authorizationapi .ScopesKey ] {
44
- req .Header .Add (ImpersonateUserScopeHeader , scope )
45
- }
46
- return rt .delegate .RoundTrip (req )
47
- }
48
-
49
15
// NewImpersonatingConfig wraps the config's transport to impersonate a user, including user, groups, and scopes
50
16
func NewImpersonatingConfig (user user.Info , config restclient.Config ) restclient.Config {
51
17
oldWrapTransport := config .WrapTransport
52
18
config .WrapTransport = func (rt http.RoundTripper ) http.RoundTripper {
53
- return newImpersonatingRoundTripper (user , oldWrapTransport (rt ))
19
+ newConfig := transport.ImpersonationConfig {
20
+ UserName : user .GetName (),
21
+ Groups : user .GetGroups (),
22
+ Extra : user .GetExtra (),
23
+ }
24
+ return transport .NewImpersonatingRoundTripper (newConfig , oldWrapTransport (rt ))
54
25
}
55
26
return config
56
27
}
@@ -73,9 +44,11 @@ func NewImpersonatingRESTClient(user user.Info, client restclient.Interface) res
73
44
74
45
// Verb does the impersonation per request by setting the proper headers
75
46
func (c impersonatingRESTClient ) impersonate (req * restclient.Request ) * restclient.Request {
76
- req .SetHeader (ImpersonateUserHeader , c .user .GetName ())
77
- req .SetHeader (ImpersonateGroupHeader , c .user .GetGroups ()... )
78
- req .SetHeader (ImpersonateUserScopeHeader , c .user .GetExtra ()[authorizationapi .ScopesKey ]... )
47
+ req .SetHeader (transport .ImpersonateUserHeader , c .user .GetName ())
48
+ req .SetHeader (transport .ImpersonateGroupHeader , c .user .GetGroups ()... )
49
+ for k , vv := range c .user .GetExtra () {
50
+ req .SetHeader (transport .ImpersonateUserExtraHeaderPrefix + k , vv ... )
51
+ }
79
52
return req
80
53
}
81
54
0 commit comments