@@ -5,47 +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
- authenticationapi "github.com/openshift/origin/pkg/oauthserver/api"
16
13
)
17
14
18
- type impersonatingRoundTripper struct {
19
- user user.Info
20
- delegate http.RoundTripper
21
- }
22
-
23
- // newImpersonatingRoundTripper will add headers to impersonate a user, including user, groups, and scopes
24
- func newImpersonatingRoundTripper (user user.Info , delegate http.RoundTripper ) http.RoundTripper {
25
- return & impersonatingRoundTripper {user : user , delegate : delegate }
26
- }
27
-
28
- func (rt * impersonatingRoundTripper ) RoundTrip (req * http.Request ) (* http.Response , error ) {
29
- req = utilnet .CloneRequest (req )
30
- req .Header .Del (authenticationapi .ImpersonateUserHeader )
31
- req .Header .Del (authenticationapi .ImpersonateGroupHeader )
32
- req .Header .Del (authenticationapi .ImpersonateUserScopeHeader )
33
-
34
- req .Header .Set (authenticationapi .ImpersonateUserHeader , rt .user .GetName ())
35
- for _ , group := range rt .user .GetGroups () {
36
- req .Header .Add (authenticationapi .ImpersonateGroupHeader , group )
37
- }
38
- for _ , scope := range rt .user .GetExtra ()[authorizationapi .ScopesKey ] {
39
- req .Header .Add (authenticationapi .ImpersonateUserScopeHeader , scope )
40
- }
41
- return rt .delegate .RoundTrip (req )
42
- }
43
-
44
15
// NewImpersonatingConfig wraps the config's transport to impersonate a user, including user, groups, and scopes
45
16
func NewImpersonatingConfig (user user.Info , config restclient.Config ) restclient.Config {
46
17
oldWrapTransport := config .WrapTransport
47
18
config .WrapTransport = func (rt http.RoundTripper ) http.RoundTripper {
48
- 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 ))
49
25
}
50
26
return config
51
27
}
@@ -68,9 +44,11 @@ func NewImpersonatingRESTClient(user user.Info, client restclient.Interface) res
68
44
69
45
// Verb does the impersonation per request by setting the proper headers
70
46
func (c impersonatingRESTClient ) impersonate (req * restclient.Request ) * restclient.Request {
71
- req .SetHeader (authenticationapi .ImpersonateUserHeader , c .user .GetName ())
72
- req .SetHeader (authenticationapi .ImpersonateGroupHeader , c .user .GetGroups ()... )
73
- req .SetHeader (authenticationapi .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
+ }
74
52
return req
75
53
}
76
54
0 commit comments