rest_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright 2017 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 subjectaccessreview
  14. import (
  15. "errors"
  16. "strings"
  17. "testing"
  18. "reflect"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apiserver/pkg/authentication/user"
  21. "k8s.io/apiserver/pkg/authorization/authorizer"
  22. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  23. "k8s.io/apiserver/pkg/registry/rest"
  24. authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
  25. )
  26. type fakeAuthorizer struct {
  27. attrs authorizer.Attributes
  28. decision authorizer.Decision
  29. reason string
  30. err error
  31. }
  32. func (f *fakeAuthorizer) Authorize(attrs authorizer.Attributes) (authorizer.Decision, string, error) {
  33. f.attrs = attrs
  34. return f.decision, f.reason, f.err
  35. }
  36. func TestCreate(t *testing.T) {
  37. testcases := map[string]struct {
  38. spec authorizationapi.SubjectAccessReviewSpec
  39. decision authorizer.Decision
  40. reason string
  41. err error
  42. expectedErr string
  43. expectedAttrs authorizer.Attributes
  44. expectedStatus authorizationapi.SubjectAccessReviewStatus
  45. }{
  46. "empty": {
  47. expectedErr: "nonResourceAttributes or resourceAttributes",
  48. },
  49. "nonresource rejected": {
  50. spec: authorizationapi.SubjectAccessReviewSpec{
  51. User: "bob",
  52. NonResourceAttributes: &authorizationapi.NonResourceAttributes{Verb: "get", Path: "/mypath"},
  53. },
  54. decision: authorizer.DecisionNoOpinion,
  55. reason: "myreason",
  56. err: errors.New("myerror"),
  57. expectedAttrs: authorizer.AttributesRecord{
  58. User: &user.DefaultInfo{Name: "bob"},
  59. Verb: "get",
  60. Path: "/mypath",
  61. ResourceRequest: false,
  62. },
  63. expectedStatus: authorizationapi.SubjectAccessReviewStatus{
  64. Allowed: false,
  65. Reason: "myreason",
  66. EvaluationError: "myerror",
  67. },
  68. },
  69. "nonresource allowed": {
  70. spec: authorizationapi.SubjectAccessReviewSpec{
  71. User: "bob",
  72. NonResourceAttributes: &authorizationapi.NonResourceAttributes{Verb: "get", Path: "/mypath"},
  73. },
  74. decision: authorizer.DecisionAllow,
  75. reason: "allowed",
  76. err: nil,
  77. expectedAttrs: authorizer.AttributesRecord{
  78. User: &user.DefaultInfo{Name: "bob"},
  79. Verb: "get",
  80. Path: "/mypath",
  81. ResourceRequest: false,
  82. },
  83. expectedStatus: authorizationapi.SubjectAccessReviewStatus{
  84. Allowed: true,
  85. Reason: "allowed",
  86. EvaluationError: "",
  87. },
  88. },
  89. "resource rejected": {
  90. spec: authorizationapi.SubjectAccessReviewSpec{
  91. User: "bob",
  92. ResourceAttributes: &authorizationapi.ResourceAttributes{
  93. Namespace: "myns",
  94. Verb: "create",
  95. Group: "extensions",
  96. Version: "v1beta1",
  97. Resource: "deployments",
  98. Subresource: "scale",
  99. Name: "mydeployment",
  100. },
  101. },
  102. decision: authorizer.DecisionNoOpinion,
  103. reason: "myreason",
  104. err: errors.New("myerror"),
  105. expectedAttrs: authorizer.AttributesRecord{
  106. User: &user.DefaultInfo{Name: "bob"},
  107. Namespace: "myns",
  108. Verb: "create",
  109. APIGroup: "extensions",
  110. APIVersion: "v1beta1",
  111. Resource: "deployments",
  112. Subresource: "scale",
  113. Name: "mydeployment",
  114. ResourceRequest: true,
  115. },
  116. expectedStatus: authorizationapi.SubjectAccessReviewStatus{
  117. Allowed: false,
  118. Denied: false,
  119. Reason: "myreason",
  120. EvaluationError: "myerror",
  121. },
  122. },
  123. "resource allowed": {
  124. spec: authorizationapi.SubjectAccessReviewSpec{
  125. User: "bob",
  126. ResourceAttributes: &authorizationapi.ResourceAttributes{
  127. Namespace: "myns",
  128. Verb: "create",
  129. Group: "extensions",
  130. Version: "v1beta1",
  131. Resource: "deployments",
  132. Subresource: "scale",
  133. Name: "mydeployment",
  134. },
  135. },
  136. decision: authorizer.DecisionAllow,
  137. reason: "allowed",
  138. err: nil,
  139. expectedAttrs: authorizer.AttributesRecord{
  140. User: &user.DefaultInfo{Name: "bob"},
  141. Namespace: "myns",
  142. Verb: "create",
  143. APIGroup: "extensions",
  144. APIVersion: "v1beta1",
  145. Resource: "deployments",
  146. Subresource: "scale",
  147. Name: "mydeployment",
  148. ResourceRequest: true,
  149. },
  150. expectedStatus: authorizationapi.SubjectAccessReviewStatus{
  151. Allowed: true,
  152. Denied: false,
  153. Reason: "allowed",
  154. EvaluationError: "",
  155. },
  156. },
  157. "resource denied": {
  158. spec: authorizationapi.SubjectAccessReviewSpec{
  159. User: "bob",
  160. ResourceAttributes: &authorizationapi.ResourceAttributes{},
  161. },
  162. decision: authorizer.DecisionDeny,
  163. expectedAttrs: authorizer.AttributesRecord{
  164. User: &user.DefaultInfo{Name: "bob"},
  165. ResourceRequest: true,
  166. },
  167. expectedStatus: authorizationapi.SubjectAccessReviewStatus{
  168. Allowed: false,
  169. Denied: true,
  170. },
  171. },
  172. }
  173. for k, tc := range testcases {
  174. auth := &fakeAuthorizer{
  175. decision: tc.decision,
  176. reason: tc.reason,
  177. err: tc.err,
  178. }
  179. storage := NewREST(auth)
  180. result, err := storage.Create(genericapirequest.NewContext(), &authorizationapi.SubjectAccessReview{Spec: tc.spec}, rest.ValidateAllObjectFunc, &metav1.CreateOptions{})
  181. if err != nil {
  182. if tc.expectedErr != "" {
  183. if !strings.Contains(err.Error(), tc.expectedErr) {
  184. t.Errorf("%s: expected %s to contain %q", k, err, tc.expectedErr)
  185. }
  186. } else {
  187. t.Errorf("%s: %v", k, err)
  188. }
  189. continue
  190. }
  191. if !reflect.DeepEqual(auth.attrs, tc.expectedAttrs) {
  192. t.Errorf("%s: expected\n%#v\ngot\n%#v", k, tc.expectedAttrs, auth.attrs)
  193. }
  194. status := result.(*authorizationapi.SubjectAccessReview).Status
  195. if !reflect.DeepEqual(status, tc.expectedStatus) {
  196. t.Errorf("%s: expected\n%#v\ngot\n%#v", k, tc.expectedStatus, status)
  197. }
  198. }
  199. }