validation.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2015 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. "k8s.io/kubernetes/pkg/api"
  16. authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
  17. "k8s.io/kubernetes/pkg/util/validation/field"
  18. )
  19. func ValidateSubjectAccessReviewSpec(spec authorizationapi.SubjectAccessReviewSpec, fldPath *field.Path) field.ErrorList {
  20. allErrs := field.ErrorList{}
  21. if spec.ResourceAttributes != nil && spec.NonResourceAttributes != nil {
  22. allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`))
  23. }
  24. if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil {
  25. allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`))
  26. }
  27. if len(spec.User) == 0 && len(spec.Groups) == 0 {
  28. allErrs = append(allErrs, field.Invalid(fldPath.Child("user"), spec.User, `at least one of user or group must be specified`))
  29. }
  30. return allErrs
  31. }
  32. func ValidateSelfSubjectAccessReviewSpec(spec authorizationapi.SelfSubjectAccessReviewSpec, fldPath *field.Path) field.ErrorList {
  33. allErrs := field.ErrorList{}
  34. if spec.ResourceAttributes != nil && spec.NonResourceAttributes != nil {
  35. allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`))
  36. }
  37. if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil {
  38. allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`))
  39. }
  40. return allErrs
  41. }
  42. func ValidateSubjectAccessReview(sar *authorizationapi.SubjectAccessReview) field.ErrorList {
  43. allErrs := ValidateSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec"))
  44. if !api.Semantic.DeepEqual(api.ObjectMeta{}, sar.ObjectMeta) {
  45. allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty`))
  46. }
  47. return allErrs
  48. }
  49. func ValidateSelfSubjectAccessReview(sar *authorizationapi.SelfSubjectAccessReview) field.ErrorList {
  50. allErrs := ValidateSelfSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec"))
  51. if !api.Semantic.DeepEqual(api.ObjectMeta{}, sar.ObjectMeta) {
  52. allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty`))
  53. }
  54. return allErrs
  55. }
  56. func ValidateLocalSubjectAccessReview(sar *authorizationapi.LocalSubjectAccessReview) field.ErrorList {
  57. allErrs := ValidateSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec"))
  58. if !api.Semantic.DeepEqual(api.ObjectMeta{}, sar.ObjectMeta) {
  59. allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty`))
  60. }
  61. return allErrs
  62. }