storage_test.go 6.2 KB

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