-
Notifications
You must be signed in to change notification settings - Fork 0
/
impersonate.go
50 lines (42 loc) · 1.56 KB
/
impersonate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package client
import (
"net/http"
"k8s.io/kubernetes/pkg/auth/user"
authenticationapi "github.com/openshift/origin/pkg/auth/api"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
)
type impersonatingRoundTripper struct {
user user.Info
delegate http.RoundTripper
}
// NewImpersonatingRoundTripper will add headers to impersonate a user, including user, groups, and scopes
func NewImpersonatingRoundTripper(user user.Info, delegate http.RoundTripper) http.RoundTripper {
return &impersonatingRoundTripper{user, delegate}
}
func (rt *impersonatingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req = cloneRequest(req)
req.Header.Del(authenticationapi.ImpersonateUserHeader)
req.Header.Del(authenticationapi.ImpersonateGroupHeader)
req.Header.Del(authenticationapi.ImpersonateUserScopeHeader)
req.Header.Set(authenticationapi.ImpersonateUserHeader, rt.user.GetName())
for _, group := range rt.user.GetGroups() {
req.Header.Add(authenticationapi.ImpersonateGroupHeader, group)
}
for _, scope := range rt.user.GetExtra()[authorizationapi.ScopesKey] {
req.Header.Add(authenticationapi.ImpersonateUserScopeHeader, scope)
}
return rt.delegate.RoundTrip(req)
}
// cloneRequest returns a clone of the provided *http.Request.
// The clone is a shallow copy of the struct and its Header map.
func cloneRequest(r *http.Request) *http.Request {
// shallow copy of the struct
r2 := new(http.Request)
*r2 = *r
// deep copy of the Header
r2.Header = make(http.Header)
for k, s := range r.Header {
r2.Header[k] = s
}
return r2
}