...
1
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
27
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
44
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
58
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
70
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
82
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 }
102
View as plain text