|
1 | 1 | package origin
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "io/ioutil"
|
5 | 6 | "net/http"
|
6 | 7 | "net/http/httptest"
|
| 8 | + "reflect" |
7 | 9 | "strings"
|
| 10 | + "sync" |
8 | 11 | "testing"
|
9 | 12 |
|
| 13 | + kapi "k8s.io/kubernetes/pkg/api" |
| 14 | + "k8s.io/kubernetes/pkg/auth/user" |
| 15 | + "k8s.io/kubernetes/pkg/util/sets" |
| 16 | + "k8s.io/kubernetes/pkg/watch" |
| 17 | + |
| 18 | + "github.com/openshift/origin/pkg/authorization/authorizer" |
10 | 19 | configapi "github.com/openshift/origin/pkg/cmd/server/api"
|
| 20 | + userapi "github.com/openshift/origin/pkg/user/api" |
| 21 | + usercache "github.com/openshift/origin/pkg/user/cache" |
11 | 22 | )
|
12 | 23 |
|
| 24 | +type impersonateAuthorizer struct{} |
| 25 | + |
| 26 | +func (impersonateAuthorizer) Authorize(ctx kapi.Context, a authorizer.AuthorizationAttributes) (allowed bool, reason string, err error) { |
| 27 | + user, exists := kapi.UserFrom(ctx) |
| 28 | + if !exists { |
| 29 | + return false, "missing user", nil |
| 30 | + } |
| 31 | + |
| 32 | + switch { |
| 33 | + case user.GetName() == "system:admin": |
| 34 | + return true, "", nil |
| 35 | + |
| 36 | + case user.GetName() == "tester": |
| 37 | + return false, "", fmt.Errorf("works on my machine") |
| 38 | + |
| 39 | + case user.GetName() == "deny-me": |
| 40 | + return false, "denied", nil |
| 41 | + } |
| 42 | + |
| 43 | + if len(user.GetGroups()) == 1 && user.GetGroups()[0] == "wheel" && a.GetVerb() == "impersonate" && a.GetResource() == "systemusers" { |
| 44 | + return true, "", nil |
| 45 | + } |
| 46 | + |
| 47 | + if len(user.GetGroups()) == 1 && user.GetGroups()[0] == "sa-impersonater" && a.GetVerb() == "impersonate" && a.GetResource() == "serviceaccounts" { |
| 48 | + return true, "", nil |
| 49 | + } |
| 50 | + |
| 51 | + if len(user.GetGroups()) == 1 && user.GetGroups()[0] == "regular-impersonater" && a.GetVerb() == "impersonate" && a.GetResource() == "users" { |
| 52 | + return true, "", nil |
| 53 | + } |
| 54 | + |
| 55 | + return false, "deny by default", nil |
| 56 | +} |
| 57 | + |
| 58 | +func (impersonateAuthorizer) GetAllowedSubjects(ctx kapi.Context, attributes authorizer.AuthorizationAttributes) (sets.String, sets.String, error) { |
| 59 | + return nil, nil, nil |
| 60 | +} |
| 61 | + |
| 62 | +type groupCache struct { |
| 63 | +} |
| 64 | + |
| 65 | +func (*groupCache) ListGroups(ctx kapi.Context, options *kapi.ListOptions) (*userapi.GroupList, error) { |
| 66 | + return &userapi.GroupList{}, nil |
| 67 | +} |
| 68 | +func (*groupCache) GetGroup(ctx kapi.Context, name string) (*userapi.Group, error) { |
| 69 | + return nil, nil |
| 70 | +} |
| 71 | +func (*groupCache) CreateGroup(ctx kapi.Context, group *userapi.Group) (*userapi.Group, error) { |
| 72 | + return nil, nil |
| 73 | +} |
| 74 | +func (*groupCache) UpdateGroup(ctx kapi.Context, group *userapi.Group) (*userapi.Group, error) { |
| 75 | + return nil, nil |
| 76 | +} |
| 77 | +func (*groupCache) DeleteGroup(ctx kapi.Context, name string) error { |
| 78 | + return nil |
| 79 | +} |
| 80 | +func (*groupCache) WatchGroups(ctx kapi.Context, options *kapi.ListOptions) (watch.Interface, error) { |
| 81 | + return watch.NewFake(), nil |
| 82 | +} |
| 83 | + |
| 84 | +func TestImpersonationFilter(t *testing.T) { |
| 85 | + testCases := []struct { |
| 86 | + name string |
| 87 | + user user.Info |
| 88 | + impersonationString string |
| 89 | + expectedUser user.Info |
| 90 | + expectedCode int |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "not-impersonating", |
| 94 | + user: &user.DefaultInfo{ |
| 95 | + Name: "tester", |
| 96 | + }, |
| 97 | + expectedUser: &user.DefaultInfo{ |
| 98 | + Name: "tester", |
| 99 | + }, |
| 100 | + expectedCode: http.StatusOK, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "impersonating-error", |
| 104 | + user: &user.DefaultInfo{ |
| 105 | + Name: "tester", |
| 106 | + }, |
| 107 | + impersonationString: "anyone", |
| 108 | + expectedUser: &user.DefaultInfo{ |
| 109 | + Name: "tester", |
| 110 | + }, |
| 111 | + expectedCode: http.StatusForbidden, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "allowed-systemusers-impersonation", |
| 115 | + user: &user.DefaultInfo{ |
| 116 | + Name: "dev", |
| 117 | + Groups: []string{"wheel"}, |
| 118 | + }, |
| 119 | + impersonationString: "system:admin", |
| 120 | + expectedUser: &user.DefaultInfo{ |
| 121 | + Name: "system:admin", |
| 122 | + Groups: []string{"system:authenticated"}, |
| 123 | + }, |
| 124 | + expectedCode: http.StatusOK, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "allowed-users-impersonation", |
| 128 | + user: &user.DefaultInfo{ |
| 129 | + Name: "dev", |
| 130 | + Groups: []string{"regular-impersonater"}, |
| 131 | + }, |
| 132 | + impersonationString: "tester", |
| 133 | + expectedUser: &user.DefaultInfo{ |
| 134 | + Name: "tester", |
| 135 | + Groups: []string{"system:authenticated", "system:authenticated:oauth"}, |
| 136 | + }, |
| 137 | + expectedCode: http.StatusOK, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "disallowed-impersonating", |
| 141 | + user: &user.DefaultInfo{ |
| 142 | + Name: "dev", |
| 143 | + Groups: []string{"sa-impersonater"}, |
| 144 | + }, |
| 145 | + impersonationString: "tester", |
| 146 | + expectedUser: &user.DefaultInfo{ |
| 147 | + Name: "dev", |
| 148 | + Groups: []string{"sa-impersonater"}, |
| 149 | + }, |
| 150 | + expectedCode: http.StatusForbidden, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "allowed-sa-impersonating", |
| 154 | + user: &user.DefaultInfo{ |
| 155 | + Name: "dev", |
| 156 | + Groups: []string{"sa-impersonater"}, |
| 157 | + }, |
| 158 | + impersonationString: "system:serviceaccount:foo:default", |
| 159 | + expectedUser: &user.DefaultInfo{ |
| 160 | + Name: "system:serviceaccount:foo:default", |
| 161 | + Groups: []string{"system:serviceaccounts", "system:serviceaccounts:foo", "system:authenticated"}, |
| 162 | + }, |
| 163 | + expectedCode: http.StatusOK, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + config := MasterConfig{} |
| 168 | + config.RequestContextMapper = kapi.NewRequestContextMapper() |
| 169 | + config.Authorizer = impersonateAuthorizer{} |
| 170 | + config.GroupCache = usercache.NewGroupCache(&groupCache{}) |
| 171 | + var ctx kapi.Context |
| 172 | + var actualUser user.Info |
| 173 | + var lock sync.Mutex |
| 174 | + |
| 175 | + doNothingHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 176 | + currentCtx, _ := config.RequestContextMapper.Get(req) |
| 177 | + user, exists := kapi.UserFrom(currentCtx) |
| 178 | + if !exists { |
| 179 | + actualUser = nil |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + actualUser = user |
| 184 | + }) |
| 185 | + handler := func(delegate http.Handler) http.Handler { |
| 186 | + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 187 | + defer func() { |
| 188 | + if r := recover(); r != nil { |
| 189 | + t.Errorf("Recovered %v", r) |
| 190 | + } |
| 191 | + }() |
| 192 | + lock.Lock() |
| 193 | + defer lock.Unlock() |
| 194 | + config.RequestContextMapper.Update(req, ctx) |
| 195 | + currentCtx, _ := config.RequestContextMapper.Get(req) |
| 196 | + |
| 197 | + user, exists := kapi.UserFrom(currentCtx) |
| 198 | + if !exists { |
| 199 | + actualUser = nil |
| 200 | + return |
| 201 | + } else { |
| 202 | + actualUser = user |
| 203 | + } |
| 204 | + |
| 205 | + delegate.ServeHTTP(w, req) |
| 206 | + }) |
| 207 | + }(config.impersonationFilter(doNothingHandler)) |
| 208 | + handler, _ = kapi.NewRequestContextFilter(config.RequestContextMapper, handler) |
| 209 | + |
| 210 | + server := httptest.NewServer(handler) |
| 211 | + defer server.Close() |
| 212 | + |
| 213 | + for _, tc := range testCases { |
| 214 | + func() { |
| 215 | + lock.Lock() |
| 216 | + defer lock.Unlock() |
| 217 | + ctx = kapi.WithUser(kapi.NewContext(), tc.user) |
| 218 | + }() |
| 219 | + |
| 220 | + req, err := http.NewRequest("GET", server.URL, nil) |
| 221 | + if err != nil { |
| 222 | + t.Errorf("%s: unexpected error: %v", tc.name, err) |
| 223 | + continue |
| 224 | + } |
| 225 | + req.Header.Add("Impersonate-User", tc.impersonationString) |
| 226 | + resp, err := http.DefaultClient.Do(req) |
| 227 | + if err != nil { |
| 228 | + t.Errorf("%s: unexpected error: %v", tc.name, err) |
| 229 | + continue |
| 230 | + } |
| 231 | + if resp.StatusCode != tc.expectedCode { |
| 232 | + t.Errorf("%s: expected %v, actual %v", tc.name, tc.expectedCode, resp.StatusCode) |
| 233 | + continue |
| 234 | + } |
| 235 | + |
| 236 | + if !reflect.DeepEqual(actualUser, tc.expectedUser) { |
| 237 | + t.Errorf("%s: expected %#v, actual %#v", tc.name, tc.expectedUser, actualUser) |
| 238 | + continue |
| 239 | + } |
| 240 | + } |
| 241 | +} |
| 242 | + |
13 | 243 | var (
|
14 | 244 | currentOCKubeResources = "oc/v1.2.0 (linux/amd64) kubernetes/bc4550d"
|
15 | 245 | currentOCOriginResources = "oc/v1.1.3 (linux/amd64) openshift/b348c2f"
|
|
0 commit comments