|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + kapi "k8s.io/kubernetes/pkg/api" |
| 8 | + "k8s.io/kubernetes/pkg/auth/user" |
| 9 | + "k8s.io/kubernetes/pkg/httplog" |
| 10 | + "k8s.io/kubernetes/pkg/serviceaccount" |
| 11 | + |
| 12 | + authenticationapi "github.com/openshift/origin/pkg/auth/api" |
| 13 | + authorizationapi "github.com/openshift/origin/pkg/authorization/api" |
| 14 | + "github.com/openshift/origin/pkg/authorization/authorizer" |
| 15 | + "github.com/openshift/origin/pkg/cmd/server/bootstrappolicy" |
| 16 | + userapi "github.com/openshift/origin/pkg/user/api" |
| 17 | + uservalidation "github.com/openshift/origin/pkg/user/api/validation" |
| 18 | +) |
| 19 | + |
| 20 | +type GroupCache interface { |
| 21 | + GroupsFor(string) ([]*userapi.Group, error) |
| 22 | +} |
| 23 | + |
| 24 | +// ImpersonationFilter checks for impersonation rules against the current user. |
| 25 | +func ImpersonationFilter(handler http.Handler, a authorizer.Authorizer, groupCache GroupCache, contextMapper kapi.RequestContextMapper) http.Handler { |
| 26 | + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 27 | + requestedUser := req.Header.Get(authenticationapi.ImpersonateUserHeader) |
| 28 | + if len(requestedUser) == 0 { |
| 29 | + handler.ServeHTTP(w, req) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + subjects := authorizationapi.BuildSubjects([]string{requestedUser}, req.Header[authenticationapi.ImpersonateGroupHeader], |
| 34 | + // validates whether the usernames are regular users or system users |
| 35 | + uservalidation.ValidateUserName, |
| 36 | + // validates group names are regular groups or system groups |
| 37 | + uservalidation.ValidateGroupName) |
| 38 | + |
| 39 | + ctx, exists := contextMapper.Get(req) |
| 40 | + if !exists { |
| 41 | + Forbidden("context not found", nil, w, req) |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + // if groups are not specified, then we need to look them up differently depending on the type of user |
| 46 | + // if they are specified, then they are the authority |
| 47 | + groupsSpecified := len(req.Header[authenticationapi.ImpersonateGroupHeader]) > 0 |
| 48 | + |
| 49 | + // make sure we're allowed to impersonate each subject. While we're iterating through, start building username |
| 50 | + // and group information |
| 51 | + username := "" |
| 52 | + groups := []string{} |
| 53 | + for _, subject := range subjects { |
| 54 | + actingAsAttributes := &authorizer.DefaultAuthorizationAttributes{ |
| 55 | + Verb: "impersonate", |
| 56 | + } |
| 57 | + |
| 58 | + switch subject.GetObjectKind().GroupVersionKind().GroupKind() { |
| 59 | + case userapi.Kind(authorizationapi.GroupKind): |
| 60 | + actingAsAttributes.APIGroup = userapi.GroupName |
| 61 | + actingAsAttributes.Resource = authorizationapi.GroupResource |
| 62 | + actingAsAttributes.ResourceName = subject.Name |
| 63 | + groups = append(groups, subject.Name) |
| 64 | + |
| 65 | + case userapi.Kind(authorizationapi.SystemGroupKind): |
| 66 | + actingAsAttributes.APIGroup = userapi.GroupName |
| 67 | + actingAsAttributes.Resource = authorizationapi.SystemGroupResource |
| 68 | + actingAsAttributes.ResourceName = subject.Name |
| 69 | + groups = append(groups, subject.Name) |
| 70 | + |
| 71 | + case userapi.Kind(authorizationapi.UserKind): |
| 72 | + actingAsAttributes.APIGroup = userapi.GroupName |
| 73 | + actingAsAttributes.Resource = authorizationapi.UserResource |
| 74 | + actingAsAttributes.ResourceName = subject.Name |
| 75 | + username = subject.Name |
| 76 | + if !groupsSpecified { |
| 77 | + if actualGroups, err := groupCache.GroupsFor(subject.Name); err == nil { |
| 78 | + for _, group := range actualGroups { |
| 79 | + groups = append(groups, group.Name) |
| 80 | + } |
| 81 | + } |
| 82 | + groups = append(groups, bootstrappolicy.AuthenticatedGroup, bootstrappolicy.AuthenticatedOAuthGroup) |
| 83 | + } |
| 84 | + |
| 85 | + case userapi.Kind(authorizationapi.SystemUserKind): |
| 86 | + actingAsAttributes.APIGroup = userapi.GroupName |
| 87 | + actingAsAttributes.Resource = authorizationapi.SystemUserResource |
| 88 | + actingAsAttributes.ResourceName = subject.Name |
| 89 | + username = subject.Name |
| 90 | + if !groupsSpecified { |
| 91 | + if subject.Name == bootstrappolicy.UnauthenticatedUsername { |
| 92 | + groups = append(groups, bootstrappolicy.UnauthenticatedGroup) |
| 93 | + } else { |
| 94 | + groups = append(groups, bootstrappolicy.AuthenticatedGroup) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + case kapi.Kind(authorizationapi.ServiceAccountKind): |
| 99 | + actingAsAttributes.APIGroup = kapi.GroupName |
| 100 | + actingAsAttributes.Resource = authorizationapi.ServiceAccountResource |
| 101 | + actingAsAttributes.ResourceName = subject.Name |
| 102 | + username = serviceaccount.MakeUsername(subject.Namespace, subject.Name) |
| 103 | + if !groupsSpecified { |
| 104 | + groups = append(serviceaccount.MakeGroupNames(subject.Namespace, subject.Name), bootstrappolicy.AuthenticatedGroup) |
| 105 | + } |
| 106 | + |
| 107 | + default: |
| 108 | + Forbidden(fmt.Sprintf("unknown subject type: %v", subject), actingAsAttributes, w, req) |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + authCheckCtx := kapi.WithNamespace(ctx, subject.Namespace) |
| 113 | + |
| 114 | + allowed, reason, err := a.Authorize(authCheckCtx, actingAsAttributes) |
| 115 | + if err != nil { |
| 116 | + Forbidden(err.Error(), actingAsAttributes, w, req) |
| 117 | + return |
| 118 | + } |
| 119 | + if !allowed { |
| 120 | + Forbidden(reason, actingAsAttributes, w, req) |
| 121 | + return |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + var extra map[string][]string |
| 126 | + if requestScopes, ok := req.Header[authenticationapi.ImpersonateUserScopeHeader]; ok { |
| 127 | + extra = map[string][]string{authorizationapi.ScopesKey: requestScopes} |
| 128 | + } |
| 129 | + |
| 130 | + newUser := &user.DefaultInfo{ |
| 131 | + Name: username, |
| 132 | + Groups: groups, |
| 133 | + Extra: extra, |
| 134 | + } |
| 135 | + contextMapper.Update(req, kapi.WithUser(ctx, newUser)) |
| 136 | + |
| 137 | + oldUser, _ := kapi.UserFrom(ctx) |
| 138 | + httplog.LogOf(req, w).Addf("%v is acting as %v", oldUser, newUser) |
| 139 | + |
| 140 | + handler.ServeHTTP(w, req) |
| 141 | + }) |
| 142 | +} |
0 commit comments