handler_impersonation_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apiserver
  14. import (
  15. "fmt"
  16. "net/http"
  17. "net/http/httptest"
  18. "reflect"
  19. "sync"
  20. "testing"
  21. "k8s.io/kubernetes/pkg/api"
  22. authenticationapi "k8s.io/kubernetes/pkg/apis/authentication"
  23. "k8s.io/kubernetes/pkg/auth/authorizer"
  24. "k8s.io/kubernetes/pkg/auth/user"
  25. )
  26. type impersonateAuthorizer struct{}
  27. func (impersonateAuthorizer) Authorize(a authorizer.Attributes) (authorized bool, reason string, err error) {
  28. user := a.GetUser()
  29. switch {
  30. case user.GetName() == "system:admin":
  31. return true, "", nil
  32. case user.GetName() == "tester":
  33. return false, "", fmt.Errorf("works on my machine")
  34. case user.GetName() == "deny-me":
  35. return false, "denied", nil
  36. }
  37. if len(user.GetGroups()) > 0 && user.GetGroups()[0] == "wheel" && a.GetVerb() == "impersonate" && a.GetResource() == "users" {
  38. return true, "", nil
  39. }
  40. if len(user.GetGroups()) > 0 && user.GetGroups()[0] == "sa-impersonater" && a.GetVerb() == "impersonate" && a.GetResource() == "serviceaccounts" {
  41. return true, "", nil
  42. }
  43. if len(user.GetGroups()) > 0 && user.GetGroups()[0] == "regular-impersonater" && a.GetVerb() == "impersonate" && a.GetResource() == "users" {
  44. return true, "", nil
  45. }
  46. if len(user.GetGroups()) > 1 && user.GetGroups()[1] == "group-impersonater" && a.GetVerb() == "impersonate" && a.GetResource() == "groups" {
  47. return true, "", nil
  48. }
  49. if len(user.GetGroups()) > 1 && user.GetGroups()[1] == "extra-setter-scopes" && a.GetVerb() == "impersonate" && a.GetResource() == "userextras" && a.GetSubresource() == "scopes" {
  50. return true, "", nil
  51. }
  52. if len(user.GetGroups()) > 1 && user.GetGroups()[1] == "extra-setter-particular-scopes" &&
  53. a.GetVerb() == "impersonate" && a.GetResource() == "userextras" && a.GetSubresource() == "scopes" && a.GetName() == "scope-a" {
  54. return true, "", nil
  55. }
  56. if len(user.GetGroups()) > 1 && user.GetGroups()[1] == "extra-setter-project" && a.GetVerb() == "impersonate" && a.GetResource() == "userextras" && a.GetSubresource() == "project" {
  57. return true, "", nil
  58. }
  59. return false, "deny by default", nil
  60. }
  61. func TestImpersonationFilter(t *testing.T) {
  62. testCases := []struct {
  63. name string
  64. user user.Info
  65. impersonationUser string
  66. impersonationGroups []string
  67. impersonationUserExtras map[string][]string
  68. expectedUser user.Info
  69. expectedCode int
  70. }{
  71. {
  72. name: "not-impersonating",
  73. user: &user.DefaultInfo{
  74. Name: "tester",
  75. },
  76. expectedUser: &user.DefaultInfo{
  77. Name: "tester",
  78. },
  79. expectedCode: http.StatusOK,
  80. },
  81. {
  82. name: "impersonating-error",
  83. user: &user.DefaultInfo{
  84. Name: "tester",
  85. },
  86. impersonationUser: "anyone",
  87. expectedUser: &user.DefaultInfo{
  88. Name: "tester",
  89. },
  90. expectedCode: http.StatusForbidden,
  91. },
  92. {
  93. name: "impersonating-group-without-user",
  94. user: &user.DefaultInfo{
  95. Name: "tester",
  96. },
  97. impersonationGroups: []string{"some-group"},
  98. expectedUser: &user.DefaultInfo{
  99. Name: "tester",
  100. },
  101. expectedCode: http.StatusForbidden,
  102. },
  103. {
  104. name: "impersonating-extra-without-user",
  105. user: &user.DefaultInfo{
  106. Name: "tester",
  107. },
  108. impersonationUserExtras: map[string][]string{"scopes": {"scope-a"}},
  109. expectedUser: &user.DefaultInfo{
  110. Name: "tester",
  111. },
  112. expectedCode: http.StatusForbidden,
  113. },
  114. {
  115. name: "disallowed-group",
  116. user: &user.DefaultInfo{
  117. Name: "dev",
  118. Groups: []string{"wheel"},
  119. },
  120. impersonationUser: "system:admin",
  121. impersonationGroups: []string{"some-group"},
  122. expectedUser: &user.DefaultInfo{
  123. Name: "dev",
  124. Groups: []string{"wheel"},
  125. },
  126. expectedCode: http.StatusForbidden,
  127. },
  128. {
  129. name: "allowed-group",
  130. user: &user.DefaultInfo{
  131. Name: "dev",
  132. Groups: []string{"wheel", "group-impersonater"},
  133. },
  134. impersonationUser: "system:admin",
  135. impersonationGroups: []string{"some-group"},
  136. expectedUser: &user.DefaultInfo{
  137. Name: "system:admin",
  138. Groups: []string{"some-group"},
  139. Extra: map[string][]string{},
  140. },
  141. expectedCode: http.StatusOK,
  142. },
  143. {
  144. name: "disallowed-userextra-1",
  145. user: &user.DefaultInfo{
  146. Name: "dev",
  147. Groups: []string{"wheel"},
  148. },
  149. impersonationUser: "system:admin",
  150. impersonationGroups: []string{"some-group"},
  151. impersonationUserExtras: map[string][]string{"scopes": {"scope-a"}},
  152. expectedUser: &user.DefaultInfo{
  153. Name: "dev",
  154. Groups: []string{"wheel"},
  155. },
  156. expectedCode: http.StatusForbidden,
  157. },
  158. {
  159. name: "disallowed-userextra-2",
  160. user: &user.DefaultInfo{
  161. Name: "dev",
  162. Groups: []string{"wheel", "extra-setter-project"},
  163. },
  164. impersonationUser: "system:admin",
  165. impersonationGroups: []string{"some-group"},
  166. impersonationUserExtras: map[string][]string{"scopes": {"scope-a"}},
  167. expectedUser: &user.DefaultInfo{
  168. Name: "dev",
  169. Groups: []string{"wheel", "extra-setter-project"},
  170. },
  171. expectedCode: http.StatusForbidden,
  172. },
  173. {
  174. name: "disallowed-userextra-3",
  175. user: &user.DefaultInfo{
  176. Name: "dev",
  177. Groups: []string{"wheel", "extra-setter-particular-scopes"},
  178. },
  179. impersonationUser: "system:admin",
  180. impersonationGroups: []string{"some-group"},
  181. impersonationUserExtras: map[string][]string{"scopes": {"scope-a", "scope-b"}},
  182. expectedUser: &user.DefaultInfo{
  183. Name: "dev",
  184. Groups: []string{"wheel", "extra-setter-particular-scopes"},
  185. },
  186. expectedCode: http.StatusForbidden,
  187. },
  188. {
  189. name: "allowed-userextras",
  190. user: &user.DefaultInfo{
  191. Name: "dev",
  192. Groups: []string{"wheel", "extra-setter-scopes"},
  193. },
  194. impersonationUser: "system:admin",
  195. impersonationUserExtras: map[string][]string{"scopes": {"scope-a", "scope-b"}},
  196. expectedUser: &user.DefaultInfo{
  197. Name: "system:admin",
  198. Groups: []string{},
  199. Extra: map[string][]string{"scopes": {"scope-a", "scope-b"}},
  200. },
  201. expectedCode: http.StatusOK,
  202. },
  203. {
  204. name: "allowed-users-impersonation",
  205. user: &user.DefaultInfo{
  206. Name: "dev",
  207. Groups: []string{"regular-impersonater"},
  208. },
  209. impersonationUser: "tester",
  210. expectedUser: &user.DefaultInfo{
  211. Name: "tester",
  212. Groups: []string{},
  213. Extra: map[string][]string{},
  214. },
  215. expectedCode: http.StatusOK,
  216. },
  217. {
  218. name: "disallowed-impersonating",
  219. user: &user.DefaultInfo{
  220. Name: "dev",
  221. Groups: []string{"sa-impersonater"},
  222. },
  223. impersonationUser: "tester",
  224. expectedUser: &user.DefaultInfo{
  225. Name: "dev",
  226. Groups: []string{"sa-impersonater"},
  227. },
  228. expectedCode: http.StatusForbidden,
  229. },
  230. {
  231. name: "allowed-sa-impersonating",
  232. user: &user.DefaultInfo{
  233. Name: "dev",
  234. Groups: []string{"sa-impersonater"},
  235. Extra: map[string][]string{},
  236. },
  237. impersonationUser: "system:serviceaccount:foo:default",
  238. expectedUser: &user.DefaultInfo{
  239. Name: "system:serviceaccount:foo:default",
  240. Groups: []string{"system:serviceaccounts", "system:serviceaccounts:foo"},
  241. Extra: map[string][]string{},
  242. },
  243. expectedCode: http.StatusOK,
  244. },
  245. }
  246. requestContextMapper = api.NewRequestContextMapper()
  247. var ctx api.Context
  248. var actualUser user.Info
  249. var lock sync.Mutex
  250. doNothingHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  251. currentCtx, _ := requestContextMapper.Get(req)
  252. user, exists := api.UserFrom(currentCtx)
  253. if !exists {
  254. actualUser = nil
  255. return
  256. }
  257. actualUser = user
  258. })
  259. handler := func(delegate http.Handler) http.Handler {
  260. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  261. defer func() {
  262. if r := recover(); r != nil {
  263. t.Errorf("Recovered %v", r)
  264. }
  265. }()
  266. lock.Lock()
  267. defer lock.Unlock()
  268. requestContextMapper.Update(req, ctx)
  269. currentCtx, _ := requestContextMapper.Get(req)
  270. user, exists := api.UserFrom(currentCtx)
  271. if !exists {
  272. actualUser = nil
  273. return
  274. } else {
  275. actualUser = user
  276. }
  277. delegate.ServeHTTP(w, req)
  278. })
  279. }(WithImpersonation(doNothingHandler, requestContextMapper, impersonateAuthorizer{}))
  280. handler, _ = api.NewRequestContextFilter(requestContextMapper, handler)
  281. server := httptest.NewServer(handler)
  282. defer server.Close()
  283. for _, tc := range testCases {
  284. func() {
  285. lock.Lock()
  286. defer lock.Unlock()
  287. ctx = api.WithUser(api.NewContext(), tc.user)
  288. }()
  289. req, err := http.NewRequest("GET", server.URL, nil)
  290. if err != nil {
  291. t.Errorf("%s: unexpected error: %v", tc.name, err)
  292. continue
  293. }
  294. req.Header.Add(authenticationapi.ImpersonateUserHeader, tc.impersonationUser)
  295. for _, group := range tc.impersonationGroups {
  296. req.Header.Add(authenticationapi.ImpersonateGroupHeader, group)
  297. }
  298. for extraKey, values := range tc.impersonationUserExtras {
  299. for _, value := range values {
  300. req.Header.Add(authenticationapi.ImpersonateUserExtraHeaderPrefix+extraKey, value)
  301. }
  302. }
  303. resp, err := http.DefaultClient.Do(req)
  304. if err != nil {
  305. t.Errorf("%s: unexpected error: %v", tc.name, err)
  306. continue
  307. }
  308. if resp.StatusCode != tc.expectedCode {
  309. t.Errorf("%s: expected %v, actual %v", tc.name, tc.expectedCode, resp.StatusCode)
  310. continue
  311. }
  312. if !reflect.DeepEqual(actualUser, tc.expectedUser) {
  313. t.Errorf("%s: expected %#v, actual %#v", tc.name, tc.expectedUser, actualUser)
  314. continue
  315. }
  316. }
  317. }