k8s.io/kubernetes@v1.29.3/pkg/apis/authorization/validation/validation.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package validation 18 19 import ( 20 apiequality "k8s.io/apimachinery/pkg/api/equality" 21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 "k8s.io/apimachinery/pkg/util/validation/field" 23 authorizationapi "k8s.io/kubernetes/pkg/apis/authorization" 24 ) 25 26 // ValidateSubjectAccessReviewSpec validates a SubjectAccessReviewSpec and returns an 27 // ErrorList with any errors. 28 func ValidateSubjectAccessReviewSpec(spec authorizationapi.SubjectAccessReviewSpec, fldPath *field.Path) field.ErrorList { 29 allErrs := field.ErrorList{} 30 if spec.ResourceAttributes != nil && spec.NonResourceAttributes != nil { 31 allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`)) 32 } 33 if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil { 34 allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`)) 35 } 36 if len(spec.User) == 0 && len(spec.Groups) == 0 { 37 allErrs = append(allErrs, field.Invalid(fldPath.Child("user"), spec.User, `at least one of user or group must be specified`)) 38 } 39 40 return allErrs 41 } 42 43 // ValidateSelfSubjectAccessReviewSpec validates a SelfSubjectAccessReviewSpec and returns an 44 // ErrorList with any errors. 45 func ValidateSelfSubjectAccessReviewSpec(spec authorizationapi.SelfSubjectAccessReviewSpec, fldPath *field.Path) field.ErrorList { 46 allErrs := field.ErrorList{} 47 if spec.ResourceAttributes != nil && spec.NonResourceAttributes != nil { 48 allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`)) 49 } 50 if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil { 51 allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`)) 52 } 53 54 return allErrs 55 } 56 57 // ValidateSubjectAccessReview validates a SubjectAccessReview and returns an 58 // ErrorList with any errors. 59 func ValidateSubjectAccessReview(sar *authorizationapi.SubjectAccessReview) field.ErrorList { 60 allErrs := ValidateSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec")) 61 objectMetaShallowCopy := sar.ObjectMeta 62 objectMetaShallowCopy.ManagedFields = nil 63 if !apiequality.Semantic.DeepEqual(metav1.ObjectMeta{}, objectMetaShallowCopy) { 64 allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty`)) 65 } 66 return allErrs 67 } 68 69 // ValidateSelfSubjectAccessReview validates a SelfSubjectAccessReview and returns an 70 // ErrorList with any errors. 71 func ValidateSelfSubjectAccessReview(sar *authorizationapi.SelfSubjectAccessReview) field.ErrorList { 72 allErrs := ValidateSelfSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec")) 73 objectMetaShallowCopy := sar.ObjectMeta 74 objectMetaShallowCopy.ManagedFields = nil 75 if !apiequality.Semantic.DeepEqual(metav1.ObjectMeta{}, objectMetaShallowCopy) { 76 allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty`)) 77 } 78 return allErrs 79 } 80 81 // ValidateLocalSubjectAccessReview validates a LocalSubjectAccessReview and returns an 82 // ErrorList with any errors. 83 func ValidateLocalSubjectAccessReview(sar *authorizationapi.LocalSubjectAccessReview) field.ErrorList { 84 allErrs := ValidateSubjectAccessReviewSpec(sar.Spec, field.NewPath("spec")) 85 86 objectMetaShallowCopy := sar.ObjectMeta 87 objectMetaShallowCopy.Namespace = "" 88 objectMetaShallowCopy.ManagedFields = nil 89 if !apiequality.Semantic.DeepEqual(metav1.ObjectMeta{}, objectMetaShallowCopy) { 90 allErrs = append(allErrs, field.Invalid(field.NewPath("metadata"), sar.ObjectMeta, `must be empty except for namespace`)) 91 } 92 93 if sar.Spec.ResourceAttributes != nil && sar.Spec.ResourceAttributes.Namespace != sar.Namespace { 94 allErrs = append(allErrs, field.Invalid(field.NewPath("spec.resourceAttributes.namespace"), sar.Spec.ResourceAttributes.Namespace, `must match metadata.namespace`)) 95 } 96 if sar.Spec.NonResourceAttributes != nil { 97 allErrs = append(allErrs, field.Invalid(field.NewPath("spec.nonResourceAttributes"), sar.Spec.NonResourceAttributes, `disallowed on this kind of request`)) 98 } 99 100 return allErrs 101 }