rest_test.go 6.3 KB

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