validation.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. apiequality "k8s.io/apimachinery/pkg/api/equality"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
  19. )
  20. // ValidateSubjectAccessReviewSpec validates a SubjectAccessReviewSpec and returns an
  21. // ErrorList with any errors.
  22. func ValidateSubjectAccessReviewSpec(spec authorizationapi.SubjectAccessReviewSpec, fldPath *field.Path) field.ErrorList {
  23. allErrs := field.ErrorList{}
  24. if spec.ResourceAttributes != nil && spec.NonResourceAttributes != nil {
  25. allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`))
  26. }
  27. if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil {
  28. allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`))
  29. }
  30. if len(spec.User) == 0 && len(spec.Groups) == 0 {
  31. allErrs = append(allErrs, field.Invalid(fldPath.Child("user"), spec.User, `at least one of user or group must be specified`))
  32. }
  33. return allErrs
  34. }
  35. // ValidateSelfSubjectAccessReviewSpec validates a SelfSubjectAccessReviewSpec and returns an
  36. // ErrorList with any errors.
  37. func ValidateSelfSubjectAccessReviewSpec(spec authorizationapi.SelfSubjectAccessReviewSpec, fldPath *field.Path) field.ErrorList {
  38. allErrs := field.ErrorList{}
  39. if spec.ResourceAttributes != nil && spec.NonResourceAttributes != nil {
  40. allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`))
  41. }
  42. if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil {
  43. allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`))
  44. }
  45. return allErrs
  46. }
  47. // ValidateSelfSubjectRulesReview validates a SelfSubjectRulesReview and returns an
  48. // ErrorList with any errors.
  49. func ValidateSelfSubjectRulesReview(review *authorizationapi.SelfSubjectRulesReview) field.ErrorList {
  50. return field.ErrorList{}
  51. }
  52. // ValidateSubjectAccessReview validates a SubjectAccessReview and returns an
  53. // ErrorList with any errors.
  54. func ValidateSubjectAccessReview(sar *authorizationapi.SubjectAccessReview) field.ErrorList {
  55. allErrs := ValidateSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec"))
  56. objectMetaShallowCopy := sar.ObjectMeta
  57. objectMetaShallowCopy.ManagedFields = nil
  58. if !apiequality.Semantic.DeepEqual(metav1.ObjectMeta{}, objectMetaShallowCopy) {
  59. allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty`))
  60. }
  61. return allErrs
  62. }
  63. // ValidateSelfSubjectAccessReview validates a SelfSubjectAccessReview and returns an
  64. // ErrorList with any errors.
  65. func ValidateSelfSubjectAccessReview(sar *authorizationapi.SelfSubjectAccessReview) field.ErrorList {
  66. allErrs := ValidateSelfSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec"))
  67. objectMetaShallowCopy := sar.ObjectMeta
  68. objectMetaShallowCopy.ManagedFields = nil
  69. if !apiequality.Semantic.DeepEqual(metav1.ObjectMeta{}, objectMetaShallowCopy) {
  70. allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty`))
  71. }
  72. return allErrs
  73. }
  74. // ValidateLocalSubjectAccessReview validates a LocalSubjectAccessReview and returns an
  75. // ErrorList with any errors.
  76. func ValidateLocalSubjectAccessReview(sar *authorizationapi.LocalSubjectAccessReview) field.ErrorList {
  77. allErrs := ValidateSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec"))
  78. objectMetaShallowCopy := sar.ObjectMeta
  79. objectMetaShallowCopy.Namespace = ""
  80. objectMetaShallowCopy.ManagedFields = nil
  81. if !apiequality.Semantic.DeepEqual(metav1.ObjectMeta{}, objectMetaShallowCopy) {
  82. allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty except for namespace`))
  83. }
  84. if sar.Spec.ResourceAttributes != nil && sar.Spec.ResourceAttributes.Namespace != sar.Namespace {
  85. allErrs = append(allErrs, field.Invalid(field.NewPath("spec.resourceAttributes.namespace"), sar.Spec.ResourceAttributes.Namespace, `must match metadata.namespace`))
  86. }
  87. if sar.Spec.NonResourceAttributes != nil {
  88. allErrs = append(allErrs, field.Invalid(field.NewPath("spec.nonResourceAttributes"), sar.Spec.NonResourceAttributes, `disallowed on this kind of request`))
  89. }
  90. return allErrs
  91. }