validation_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Copyright 2014 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 validation
  14. import (
  15. "strings"
  16. "testing"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
  20. )
  21. func TestValidateSARSpec(t *testing.T) {
  22. successCases := []authorizationapi.SubjectAccessReviewSpec{
  23. {ResourceAttributes: &authorizationapi.ResourceAttributes{}, User: "me"},
  24. {NonResourceAttributes: &authorizationapi.NonResourceAttributes{}, Groups: []string{"my-group"}},
  25. }
  26. for _, successCase := range successCases {
  27. if errs := ValidateSubjectAccessReviewSpec(successCase, field.NewPath("spec")); len(errs) != 0 {
  28. t.Errorf("expected success: %v", errs)
  29. }
  30. }
  31. errorCases := []struct {
  32. name string
  33. obj authorizationapi.SubjectAccessReviewSpec
  34. msg string
  35. }{
  36. {
  37. name: "neither request",
  38. obj: authorizationapi.SubjectAccessReviewSpec{User: "me"},
  39. msg: "exactly one of nonResourceAttributes or resourceAttributes must be specified",
  40. },
  41. {
  42. name: "both requests",
  43. obj: authorizationapi.SubjectAccessReviewSpec{
  44. ResourceAttributes: &authorizationapi.ResourceAttributes{},
  45. NonResourceAttributes: &authorizationapi.NonResourceAttributes{},
  46. User: "me",
  47. },
  48. msg: "cannot be specified in combination with resourceAttributes",
  49. },
  50. {
  51. name: "no subject",
  52. obj: authorizationapi.SubjectAccessReviewSpec{
  53. ResourceAttributes: &authorizationapi.ResourceAttributes{},
  54. },
  55. msg: `spec.user: Invalid value: "": at least one of user or group must be specified`,
  56. },
  57. }
  58. for _, c := range errorCases {
  59. errs := ValidateSubjectAccessReviewSpec(c.obj, field.NewPath("spec"))
  60. if len(errs) == 0 {
  61. t.Errorf("%s: expected failure for %q", c.name, c.msg)
  62. } else if !strings.Contains(errs[0].Error(), c.msg) {
  63. t.Errorf("%s: unexpected error: %q, expected: %q", c.name, errs[0], c.msg)
  64. }
  65. errs = ValidateSubjectAccessReview(&authorizationapi.SubjectAccessReview{Spec: c.obj})
  66. if len(errs) == 0 {
  67. t.Errorf("%s: expected failure for %q", c.name, c.msg)
  68. } else if !strings.Contains(errs[0].Error(), c.msg) {
  69. t.Errorf("%s: unexpected error: %q, expected: %q", c.name, errs[0], c.msg)
  70. }
  71. errs = ValidateLocalSubjectAccessReview(&authorizationapi.LocalSubjectAccessReview{Spec: c.obj})
  72. if len(errs) == 0 {
  73. t.Errorf("%s: expected failure for %q", c.name, c.msg)
  74. } else if !strings.Contains(errs[0].Error(), c.msg) {
  75. t.Errorf("%s: unexpected error: %q, expected: %q", c.name, errs[0], c.msg)
  76. }
  77. }
  78. }
  79. func TestValidateSelfSAR(t *testing.T) {
  80. successCases := []authorizationapi.SelfSubjectAccessReviewSpec{
  81. {ResourceAttributes: &authorizationapi.ResourceAttributes{}},
  82. }
  83. for _, successCase := range successCases {
  84. if errs := ValidateSelfSubjectAccessReviewSpec(successCase, field.NewPath("spec")); len(errs) != 0 {
  85. t.Errorf("expected success: %v", errs)
  86. }
  87. }
  88. errorCases := []struct {
  89. name string
  90. obj authorizationapi.SelfSubjectAccessReviewSpec
  91. msg string
  92. }{
  93. {
  94. name: "neither request",
  95. obj: authorizationapi.SelfSubjectAccessReviewSpec{},
  96. msg: "exactly one of nonResourceAttributes or resourceAttributes must be specified",
  97. },
  98. {
  99. name: "both requests",
  100. obj: authorizationapi.SelfSubjectAccessReviewSpec{
  101. ResourceAttributes: &authorizationapi.ResourceAttributes{},
  102. NonResourceAttributes: &authorizationapi.NonResourceAttributes{},
  103. },
  104. msg: "cannot be specified in combination with resourceAttributes",
  105. },
  106. }
  107. for _, c := range errorCases {
  108. errs := ValidateSelfSubjectAccessReviewSpec(c.obj, field.NewPath("spec"))
  109. if len(errs) == 0 {
  110. t.Errorf("%s: expected failure for %q", c.name, c.msg)
  111. } else if !strings.Contains(errs[0].Error(), c.msg) {
  112. t.Errorf("%s: unexpected error: %q, expected: %q", c.name, errs[0], c.msg)
  113. }
  114. errs = ValidateSelfSubjectAccessReview(&authorizationapi.SelfSubjectAccessReview{Spec: c.obj})
  115. if len(errs) == 0 {
  116. t.Errorf("%s: expected failure for %q", c.name, c.msg)
  117. } else if !strings.Contains(errs[0].Error(), c.msg) {
  118. t.Errorf("%s: unexpected error: %q, expected: %q", c.name, errs[0], c.msg)
  119. }
  120. }
  121. }
  122. func TestValidateLocalSAR(t *testing.T) {
  123. successCases := []authorizationapi.LocalSubjectAccessReview{
  124. {
  125. Spec: authorizationapi.SubjectAccessReviewSpec{
  126. ResourceAttributes: &authorizationapi.ResourceAttributes{},
  127. User: "user",
  128. },
  129. },
  130. }
  131. for _, successCase := range successCases {
  132. if errs := ValidateLocalSubjectAccessReview(&successCase); len(errs) != 0 {
  133. t.Errorf("expected success: %v", errs)
  134. }
  135. }
  136. errorCases := []struct {
  137. name string
  138. obj *authorizationapi.LocalSubjectAccessReview
  139. msg string
  140. }{
  141. {
  142. name: "name",
  143. obj: &authorizationapi.LocalSubjectAccessReview{
  144. ObjectMeta: metav1.ObjectMeta{Name: "a"},
  145. Spec: authorizationapi.SubjectAccessReviewSpec{
  146. ResourceAttributes: &authorizationapi.ResourceAttributes{},
  147. User: "user",
  148. },
  149. },
  150. msg: "must be empty except for namespace",
  151. },
  152. {
  153. name: "namespace conflict",
  154. obj: &authorizationapi.LocalSubjectAccessReview{
  155. ObjectMeta: metav1.ObjectMeta{Namespace: "a"},
  156. Spec: authorizationapi.SubjectAccessReviewSpec{
  157. ResourceAttributes: &authorizationapi.ResourceAttributes{},
  158. User: "user",
  159. },
  160. },
  161. msg: "must match metadata.namespace",
  162. },
  163. {
  164. name: "nonresource",
  165. obj: &authorizationapi.LocalSubjectAccessReview{
  166. ObjectMeta: metav1.ObjectMeta{Namespace: "a"},
  167. Spec: authorizationapi.SubjectAccessReviewSpec{
  168. NonResourceAttributes: &authorizationapi.NonResourceAttributes{},
  169. User: "user",
  170. },
  171. },
  172. msg: "disallowed on this kind of request",
  173. },
  174. }
  175. for _, c := range errorCases {
  176. errs := ValidateLocalSubjectAccessReview(c.obj)
  177. if len(errs) == 0 {
  178. t.Errorf("%s: expected failure for %q", c.name, c.msg)
  179. } else if !strings.Contains(errs[0].Error(), c.msg) {
  180. t.Errorf("%s: unexpected error: %q, expected: %q", c.name, errs[0], c.msg)
  181. }
  182. }
  183. }