Skip to content

Commit c3191f0

Browse files
committed
use normal impersonation headers to speaking back to ourselves
1 parent c2aed8d commit c3191f0

File tree

1 file changed

+11
-34
lines changed

1 file changed

+11
-34
lines changed

pkg/oauthserver/client/impersonate.go

+11-34
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,22 @@ import (
55

66
"k8s.io/apimachinery/pkg/runtime/schema"
77
"k8s.io/apimachinery/pkg/types"
8-
utilnet "k8s.io/apimachinery/pkg/util/net"
98
"k8s.io/apiserver/pkg/authentication/user"
109
restclient "k8s.io/client-go/rest"
10+
"k8s.io/client-go/transport"
1111
"k8s.io/client-go/util/flowcontrol"
1212
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"
1613
)
1714

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-
4415
// NewImpersonatingConfig wraps the config's transport to impersonate a user, including user, groups, and scopes
4516
func NewImpersonatingConfig(user user.Info, config restclient.Config) restclient.Config {
4617
oldWrapTransport := config.WrapTransport
4718
config.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
48-
return newImpersonatingRoundTripper(user, oldWrapTransport(rt))
19+
return transport.NewImpersonatingRoundTripper(transport.ImpersonationConfig{
20+
UserName: user.GetName(),
21+
Groups: user.GetGroups(),
22+
Extra: user.GetExtra(),
23+
}, oldWrapTransport(rt))
4924
}
5025
return config
5126
}
@@ -68,9 +43,11 @@ func NewImpersonatingRESTClient(user user.Info, client restclient.Interface) res
6843

6944
// Verb does the impersonation per request by setting the proper headers
7045
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]...)
46+
req.SetHeader(transport.ImpersonateUserHeader, c.user.GetName())
47+
req.SetHeader(transport.ImpersonateGroupHeader, c.user.GetGroups()...)
48+
for k, vv := range c.user.GetExtra() {
49+
req.SetHeader(transport.ImpersonateUserExtraHeaderPrefix+k, vv...)
50+
}
7451
return req
7552
}
7653

0 commit comments

Comments
 (0)