apiVersion:rbac.authorization.k8s.io/v1 kind:ClusterRole metadata: name:limited-impersonator rules: # Can impersonate the user "jane.doe@example.com" -apiGroups: [""] resources: ["users"] verbs: ["impersonate"] resourceNames: ["jane.doe@example.com"]
# Can impersonate the groups "developers" and "admins" -apiGroups: [""] resources: ["groups"] verbs: ["impersonate"] resourceNames: ["developers","admins"]
# Can impersonate the extras field "scopes" with the values "view" and "development" -apiGroups: ["authentication.k8s.io"] resources: ["userextras/scopes"] verbs: ["impersonate"] resourceNames: ["view", "development"]
# Can impersonate the uid "06f6ce97-e2c5-4ab8-7ba5-7654dd08d52b" -apiGroups: ["authentication.k8s.io"] resources: ["uids"] verbs: ["impersonate"] resourceNames: ["06f6ce97-e2c5-4ab8-7ba5-7654dd08d52b"]
// 读取当前的用户信息 ctx := req.Context() requestor, exists := request.UserFrom(ctx) if !exists { responsewriters.InternalError(w, req, errors.New("no user found for request")) return }
// if groups are not specified, then we need to look them up differently depending on the type of user // if they are specified, then they are the authority (including the inclusion of system:authenticated/system:unauthenticated groups) groupsSpecified := len(req.Header[authenticationv1.ImpersonateGroupHeader]) > 0
// make sure we're allowed to impersonate each thing we're requesting. While we're iterating through, start building username // and group information username := "" groups := []string{} userExtra := map[string][]string{} uid := "" for _, impersonationRequest := range impersonationRequests { gvk := impersonationRequest.GetObjectKind().GroupVersionKind() actingAsAttributes := &authorizer.AttributesRecord{ User: requestor, Verb: "impersonate", APIGroup: gvk.Group, APIVersion: gvk.Version, Namespace: impersonationRequest.Namespace, Name: impersonationRequest.Name, ResourceRequest: true, }
// 清除伪装头 // clear all the impersonation headers from the request req.Header.Del(authenticationv1.ImpersonateUserHeader) req.Header.Del(authenticationv1.ImpersonateGroupHeader) req.Header.Del(authenticationv1.ImpersonateUIDHeader) for headerName := range req.Header { if strings.HasPrefix(headerName, authenticationv1.ImpersonateUserExtraHeaderPrefix) { req.Header.Del(headerName) } }
func(rt *impersonatingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { // use the user header as marker for the rest. iflen(req.Header.Get(ImpersonateUserHeader)) != 0 { return rt.delegate.RoundTrip(req) } req = utilnet.CloneRequest(req) req.Header.Set(ImpersonateUserHeader, rt.impersonate.UserName) if rt.impersonate.UID != "" { req.Header.Set(ImpersonateUIDHeader, rt.impersonate.UID) } for _, group := range rt.impersonate.Groups { req.Header.Add(ImpersonateGroupHeader, group) } for k, vv := range rt.impersonate.Extra { for _, v := range vv { req.Header.Add(ImpersonateUserExtraHeaderPrefix+headerKeyEscape(k), v) } }