storage_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2018 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 policybased
  14. import (
  15. "context"
  16. "testing"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. "k8s.io/apimachinery/pkg/api/errors"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apiserver/pkg/authentication/user"
  22. "k8s.io/apiserver/pkg/authorization/authorizer"
  23. "k8s.io/apiserver/pkg/endpoints/request"
  24. "k8s.io/apiserver/pkg/registry/rest"
  25. "k8s.io/kubernetes/pkg/apis/rbac"
  26. _ "k8s.io/kubernetes/pkg/apis/rbac/install"
  27. "k8s.io/kubernetes/pkg/registry/rbac/validation"
  28. )
  29. func TestEscalation(t *testing.T) {
  30. createContext := request.WithRequestInfo(request.WithNamespace(context.TODO(), "myns"), &request.RequestInfo{
  31. IsResourceRequest: true,
  32. Verb: "create",
  33. APIGroup: "rbac.authorization.k8s.io",
  34. APIVersion: "v1",
  35. Namespace: "myns",
  36. Resource: "roles",
  37. Name: "",
  38. })
  39. updateContext := request.WithRequestInfo(request.WithNamespace(context.TODO(), "myns"), &request.RequestInfo{
  40. IsResourceRequest: true,
  41. Verb: "update",
  42. APIGroup: "rbac.authorization.k8s.io",
  43. APIVersion: "v1",
  44. Namespace: "myns",
  45. Resource: "roles",
  46. Name: "myrole",
  47. })
  48. superuser := &user.DefaultInfo{Name: "superuser", Groups: []string{"system:masters"}}
  49. bob := &user.DefaultInfo{Name: "bob"}
  50. steve := &user.DefaultInfo{Name: "steve"}
  51. alice := &user.DefaultInfo{Name: "alice"}
  52. authzCalled := 0
  53. fakeStorage := &fakeStorage{}
  54. fakeAuthorizer := authorizer.AuthorizerFunc(func(attr authorizer.Attributes) (authorizer.Decision, string, error) {
  55. authzCalled++
  56. if attr.GetUser().GetName() == "steve" {
  57. return authorizer.DecisionAllow, "", nil
  58. }
  59. return authorizer.DecisionNoOpinion, "", nil
  60. })
  61. fakeRuleResolver, _ := validation.NewTestRuleResolver(
  62. nil,
  63. nil,
  64. []*rbacv1.ClusterRole{{ObjectMeta: metav1.ObjectMeta{Name: "alice-role"}, Rules: []rbacv1.PolicyRule{{APIGroups: []string{"*"}, Resources: []string{"*"}, Verbs: []string{"*"}}}}},
  65. []*rbacv1.ClusterRoleBinding{{RoleRef: rbacv1.RoleRef{Name: "alice-role", APIGroup: "rbac.authorization.k8s.io", Kind: "ClusterRole"}, Subjects: []rbacv1.Subject{{Name: "alice", Kind: "User", APIGroup: "rbac.authorization.k8s.io"}}}},
  66. )
  67. role := &rbac.Role{
  68. ObjectMeta: metav1.ObjectMeta{Name: "myrole", Namespace: "myns"},
  69. Rules: []rbac.PolicyRule{{APIGroups: []string{""}, Verbs: []string{"get"}, Resources: []string{"pods"}}},
  70. }
  71. s := NewStorage(fakeStorage, fakeAuthorizer, fakeRuleResolver)
  72. testcases := []struct {
  73. name string
  74. user user.Info
  75. expectAllowed bool
  76. expectAuthz bool
  77. }{
  78. // superuser doesn't even trigger an authz check, and is allowed
  79. {
  80. name: "superuser",
  81. user: superuser,
  82. expectAuthz: false,
  83. expectAllowed: true,
  84. },
  85. // bob triggers an authz check, is disallowed by the authorizer, and has no RBAC permissions, so is not allowed
  86. {
  87. name: "bob",
  88. user: bob,
  89. expectAuthz: true,
  90. expectAllowed: false,
  91. },
  92. // steve triggers an authz check, is allowed by the authorizer, and has no RBAC permissions, but is still allowed
  93. {
  94. name: "steve",
  95. user: steve,
  96. expectAuthz: true,
  97. expectAllowed: true,
  98. },
  99. // alice triggers an authz check, is denied by the authorizer, but has RBAC permissions in the fakeRuleResolver, so is allowed
  100. {
  101. name: "alice",
  102. user: alice,
  103. expectAuthz: true,
  104. expectAllowed: true,
  105. },
  106. }
  107. for _, tc := range testcases {
  108. t.Run(tc.name, func(t *testing.T) {
  109. authzCalled, fakeStorage.created, fakeStorage.updated = 0, 0, 0
  110. _, err := s.Create(request.WithUser(createContext, tc.user), role, nil, nil)
  111. if tc.expectAllowed {
  112. if err != nil {
  113. t.Error(err)
  114. return
  115. }
  116. if fakeStorage.created != 1 {
  117. t.Errorf("unexpected calls to underlying storage.Create: %d", fakeStorage.created)
  118. return
  119. }
  120. } else {
  121. if !errors.IsForbidden(err) {
  122. t.Errorf("expected forbidden, got %v", err)
  123. return
  124. }
  125. if fakeStorage.created != 0 {
  126. t.Errorf("unexpected calls to underlying storage.Create: %d", fakeStorage.created)
  127. return
  128. }
  129. }
  130. if tc.expectAuthz != (authzCalled > 0) {
  131. t.Fatalf("expected authz=%v, saw %d calls", tc.expectAuthz, authzCalled)
  132. }
  133. authzCalled, fakeStorage.created, fakeStorage.updated = 0, 0, 0
  134. _, _, err = s.Update(request.WithUser(updateContext, tc.user), role.Name, rest.DefaultUpdatedObjectInfo(role), nil, nil, false, nil)
  135. if tc.expectAllowed {
  136. if err != nil {
  137. t.Error(err)
  138. return
  139. }
  140. if fakeStorage.updated != 1 {
  141. t.Errorf("unexpected calls to underlying storage.Update: %d", fakeStorage.updated)
  142. return
  143. }
  144. } else {
  145. if !errors.IsForbidden(err) {
  146. t.Errorf("expected forbidden, got %v", err)
  147. return
  148. }
  149. if fakeStorage.updated != 0 {
  150. t.Errorf("unexpected calls to underlying storage.Update: %d", fakeStorage.updated)
  151. return
  152. }
  153. }
  154. if tc.expectAuthz != (authzCalled > 0) {
  155. t.Fatalf("expected authz=%v, saw %d calls", tc.expectAuthz, authzCalled)
  156. }
  157. })
  158. }
  159. }
  160. type fakeStorage struct {
  161. updated int
  162. created int
  163. rest.StandardStorage
  164. }
  165. func (f *fakeStorage) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
  166. f.created++
  167. return nil, nil
  168. }
  169. func (f *fakeStorage) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
  170. obj, err := objInfo.UpdatedObject(ctx, &rbac.Role{})
  171. if err != nil {
  172. return obj, false, err
  173. }
  174. f.updated++
  175. return nil, false, nil
  176. }