Skip to content

Commit c44b4c2

Browse files
committed
add validation
1 parent 3b98f49 commit c44b4c2

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

pkg/api/validation/register.go

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func init() {
4242
func registerAll() {
4343
Validator.MustRegister(&authorizationapi.SelfSubjectRulesReview{}, authorizationvalidation.ValidateSelfSubjectRulesReview, nil)
4444
Validator.MustRegister(&authorizationapi.SubjectAccessReview{}, authorizationvalidation.ValidateSubjectAccessReview, nil)
45+
Validator.MustRegister(&authorizationapi.SubjectRulesReview{}, authorizationvalidation.ValidateSubjectRulesReview, nil)
4546
Validator.MustRegister(&authorizationapi.ResourceAccessReview{}, authorizationvalidation.ValidateResourceAccessReview, nil)
4647
Validator.MustRegister(&authorizationapi.LocalSubjectAccessReview{}, authorizationvalidation.ValidateLocalSubjectAccessReview, nil)
4748
Validator.MustRegister(&authorizationapi.LocalResourceAccessReview{}, authorizationvalidation.ValidateLocalResourceAccessReview, nil)

pkg/authorization/api/v1/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ type SubjectRulesReviewSpec struct {
171171
// Groups is optional. Groups is the list of groups to which the User belongs. At least one of User and Groups must be specified.
172172
Groups []string `json:"groups" protobuf:"bytes,2,rep,name=groups"`
173173
// Scopes to use for the evaluation. Empty means "use the unscoped (full) permissions of the user/groups".
174-
Scopes OptionalScopes `json:"scopes" protobuf:"bytes,3,rep,name=scopes"`
174+
Scopes OptionalScopes `json:"scopes" protobuf:"bytes,3,opt,name=scopes"`
175175
}
176176

177177
// SubjectRulesReviewStatus is contains the result of a rules check

pkg/authorization/api/validation/validation.go

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ func ValidateSelfSubjectRulesReview(review *authorizationapi.SelfSubjectRulesRev
1818
return field.ErrorList{}
1919
}
2020

21+
func ValidateSubjectRulesReview(rules *authorizationapi.SubjectRulesReview) field.ErrorList {
22+
allErrs := field.ErrorList{}
23+
24+
if len(rules.Spec.Groups) == 0 && len(rules.Spec.User) == 0 {
25+
allErrs = append(allErrs, field.Required(field.NewPath("user"), "at least one of user and groups must be specified"))
26+
}
27+
28+
return allErrs
29+
}
30+
2131
func ValidateSubjectAccessReview(review *authorizationapi.SubjectAccessReview) field.ErrorList {
2232
allErrs := field.ErrorList{}
2333

0 commit comments

Comments
 (0)