validation.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. func ValidateSubjectAccessReviewSpec(spec authorizationapi.SubjectAccessReviewSpec, fldPath *field.Path) field.ErrorList {
  21. allErrs := field.ErrorList{}
  22. if spec.ResourceAttributes != nil && spec.NonResourceAttributes != nil {
  23. allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`))
  24. }
  25. if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil {
  26. allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`))
  27. }
  28. if len(spec.User) == 0 && len(spec.Groups) == 0 {
  29. allErrs = append(allErrs, field.Invalid(fldPath.Child("user"), spec.User, `at least one of user or group must be specified`))
  30. }
  31. return allErrs
  32. }
  33. func ValidateSelfSubjectAccessReviewSpec(spec authorizationapi.SelfSubjectAccessReviewSpec, fldPath *field.Path) field.ErrorList {
  34. allErrs := field.ErrorList{}
  35. if spec.ResourceAttributes != nil && spec.NonResourceAttributes != nil {
  36. allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`))
  37. }
  38. if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil {
  39. allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`))
  40. }
  41. return allErrs
  42. }
  43. func ValidateSelfSubjectRulesReview(review *authorizationapi.SelfSubjectRulesReview) field.ErrorList {
  44. return field.ErrorList{}
  45. }
  46. func ValidateSubjectAccessReview(sar *authorizationapi.SubjectAccessReview) field.ErrorList {
  47. allErrs := ValidateSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec"))
  48. objectMetaShallowCopy := sar.ObjectMeta
  49. objectMetaShallowCopy.ManagedFields = nil
  50. if !apiequality.Semantic.DeepEqual(metav1.ObjectMeta{}, objectMetaShallowCopy) {
  51. allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty`))
  52. }
  53. return allErrs
  54. }
  55. func ValidateSelfSubjectAccessReview(sar *authorizationapi.SelfSubjectAccessReview) field.ErrorList {
  56. allErrs := ValidateSelfSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec"))
  57. objectMetaShallowCopy := sar.ObjectMeta
  58. objectMetaShallowCopy.ManagedFields = nil
  59. if !apiequality.Semantic.DeepEqual(metav1.ObjectMeta{}, objectMetaShallowCopy) {
  60. allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty`))
  61. }
  62. return allErrs
  63. }
  64. func ValidateLocalSubjectAccessReview(sar *authorizationapi.LocalSubjectAccessReview) field.ErrorList {
  65. allErrs := ValidateSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec"))
  66. objectMetaShallowCopy := sar.ObjectMeta
  67. objectMetaShallowCopy.Namespace = ""
  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 except for namespace`))
  71. }
  72. if sar.Spec.ResourceAttributes != nil && sar.Spec.ResourceAttributes.Namespace != sar.Namespace {
  73. allErrs = append(allErrs, field.Invalid(field.NewPath("spec.resourceAttributes.namespace"), sar.Spec.ResourceAttributes.Namespace, `must match metadata.namespace`))
  74. }
  75. if sar.Spec.NonResourceAttributes != nil {
  76. allErrs = append(allErrs, field.Invalid(field.NewPath("spec.nonResourceAttributes"), sar.Spec.NonResourceAttributes, `disallowed on this kind of request`))
  77. }
  78. return allErrs
  79. }