@@ -16,6 +16,7 @@ package v1alpha1
1616
1717import (
1818 "context"
19+ "strings"
1920
2021 "knative.dev/pkg/apis"
2122)
@@ -32,84 +33,115 @@ func (spec *ClusterImagePolicySpec) Validate(ctx context.Context) (errors *apis.
3233 return
3334}
3435
35- func (image * ImagePattern ) Validate (ctx context.Context ) (errors * apis.FieldError ) {
36+ func (image * ImagePattern ) Validate (ctx context.Context ) * apis.FieldError {
37+ var errs * apis.FieldError
3638 if image .Regex != "" && image .Glob != "" {
37- errors = errors .Also (apis .ErrMultipleOneOf ("regex" , "glob" )). ViaField ( "images" )
39+ errs = errs .Also (apis .ErrMultipleOneOf ("regex" , "glob" ))
3840 }
3941
4042 if image .Regex == "" && image .Glob == "" {
41- errors = errors .Also (apis .ErrMissingOneOf ("regex" , "glob" )).ViaField ("images" )
43+ errs = errs .Also (apis .ErrMissingOneOf ("regex" , "glob" ))
44+ }
45+
46+ if image .Glob != "" {
47+ errs = errs .Also (ValidateGlob (image .Glob ).ViaField ("glob" ))
48+ }
49+
50+ if image .Regex != "" {
51+ errs = errs .Also (apis .ErrDisallowedFields ("regex" ))
4252 }
4353
4454 if len (image .Authorities ) == 0 {
45- errors = errors .Also (apis .ErrGeneric ("At least one authority should be defined" )) .ViaField ("authorities" )
55+ errs = errs .Also (apis .ErrGeneric ("At least one authority should be defined" ).ViaField ("authorities" ) )
4656 }
47- for i , authority := range image .Authorities {
48- errors = errors .Also (authority . Validate (ctx )) .ViaFieldIndex ("authorities" , i )
57+ for i := range image .Authorities {
58+ errs = errs .Also (image . Authorities [ i ]. Validate (ctx ).ViaFieldIndex ("authorities" , i ) )
4959 }
5060
51- return
61+ return errs
5262}
5363
54- func (authority * Authority ) Validate (ctx context.Context ) (errors * apis.FieldError ) {
64+ func (authority * Authority ) Validate (ctx context.Context ) * apis.FieldError {
65+ var errs * apis.FieldError
5566 if authority .Key == nil && authority .Keyless == nil {
56- return errors .Also (apis .ErrMissingOneOf ("key" , "keyless" )). ViaField ( "authority" )
67+ errs = errs .Also (apis .ErrMissingOneOf ("key" , "keyless" ))
5768 }
5869 if authority .Key != nil && authority .Keyless != nil {
59- return errors .Also (apis .ErrMultipleOneOf ("key" , "keyless" )). ViaField ( "authority" )
70+ errs = errs .Also (apis .ErrMultipleOneOf ("key" , "keyless" ))
6071 }
6172
6273 if authority .Key != nil {
63- errors = errors .Also (authority .Key .Validate (ctx )) .ViaField ("authority" )
74+ errs = errs .Also (authority .Key .Validate (ctx ).ViaField ("key" ) )
6475 }
6576 if authority .Keyless != nil {
66- errors = errors .Also (authority .Keyless .Validate (ctx )) .ViaField ("authority" )
77+ errs = errs .Also (authority .Keyless .Validate (ctx ).ViaField ("keyless" ) )
6778 }
6879
69- return
80+ return errs
7081}
7182
72- func (key * KeyRef ) Validate (ctx context.Context ) (errors * apis.FieldError ) {
83+ func (key * KeyRef ) Validate (ctx context.Context ) * apis.FieldError {
84+ var errs * apis.FieldError
85+
7386 if key .Data == "" && key .KMS == "" && key .SecretRef == nil {
74- return errors .Also (apis .ErrMissingOneOf ("data" , "kms" , "secretref" )). ViaField ( "key" )
87+ errs = errs .Also (apis .ErrMissingOneOf ("data" , "kms" , "secretref" ))
7588 }
7689
7790 if key .Data != "" {
7891 if key .KMS != "" || key .SecretRef != nil {
79- return errors .Also (apis .ErrMultipleOneOf ("data" , "kms" , "secretref" )). ViaField ( "key" )
92+ errs = errs .Also (apis .ErrMultipleOneOf ("data" , "kms" , "secretref" ))
8093 }
8194 } else if key .KMS != "" && key .SecretRef != nil {
82- return errors .Also (apis .ErrMultipleOneOf ("data" , "kms" , "secretref" )). ViaField ( "key" )
95+ errs = errs .Also (apis .ErrMultipleOneOf ("data" , "kms" , "secretref" ))
8396 }
84- return
97+ return errs
8598}
8699
87- func (keyless * KeylessRef ) Validate (ctx context.Context ) (errors * apis.FieldError ) {
100+ func (keyless * KeylessRef ) Validate (ctx context.Context ) * apis.FieldError {
101+ var errs * apis.FieldError
88102 if keyless .URL == nil && keyless .Identities == nil && keyless .CAKey == nil {
89- return errors .Also (apis .ErrMissingOneOf ("url" , "identities" , "ca-key" )). ViaField ( "keyless" )
103+ errs = errs .Also (apis .ErrMissingOneOf ("url" , "identities" , "ca-key" ))
90104 }
91105
92106 if keyless .URL != nil {
93107 if keyless .CAKey != nil || keyless .Identities != nil {
94- return errors .Also (apis .ErrMultipleOneOf ("url" , "identities" , "ca-key" )). ViaField ( "keyless" )
108+ errs = errs .Also (apis .ErrMultipleOneOf ("url" , "identities" , "ca-key" ))
95109 }
96110 } else if keyless .CAKey != nil && keyless .Identities != nil {
97- return errors .Also (apis .ErrMultipleOneOf ("url" , "identities" , "ca-key" )). ViaField ( "keyless" )
111+ errs = errs .Also (apis .ErrMultipleOneOf ("url" , "identities" , "ca-key" ))
98112 }
99113
100114 if keyless .Identities != nil && len (keyless .Identities ) == 0 {
101- return errors .Also (apis .ErrGeneric ("At least one identity must be provided" )). ViaField ( "keyless" )
115+ errs = errs .Also (apis .ErrGeneric ("At least one identity must be provided" ))
102116 }
103117
104118 for i , identity := range keyless .Identities {
105- errors = errors .Also (identity .Validate (ctx )) .ViaFieldIndex ("identities" , i )
119+ errs = errs .Also (identity .Validate (ctx ).ViaFieldIndex ("identities" , i ) )
106120 }
107- return
121+ return errs
108122}
109123
110- func (identity * Identity ) Validate (ctx context.Context ) (errors * apis.FieldError ) {
124+ func (identity * Identity ) Validate (ctx context.Context ) * apis.FieldError {
125+ var errs * apis.FieldError
111126 if identity .Issuer == "" && identity .Subject == "" {
112- return apis .ErrMissingOneOf ("issuer" , "subject" ). ViaField ( "identity" )
127+ errs = errs . Also ( apis .ErrMissingOneOf ("issuer" , "subject" ))
113128 }
114- return
129+ return errs
130+ }
131+
132+ // ValidateGlob makes sure that if there's "*" specified it's the trailing
133+ // character.
134+ func ValidateGlob (glob string ) * apis.FieldError {
135+ c := strings .Count (glob , "*" )
136+ switch c {
137+ case 0 :
138+ return nil
139+ case 1 :
140+ if ! strings .HasSuffix (glob , "*" ) {
141+ return apis .ErrInvalidValue (glob , apis .CurrentField , "glob match supports only * as a trailing character" )
142+ }
143+ default :
144+ return apis .ErrInvalidValue (glob , apis .CurrentField , "glob match supports only a single * as a trailing character" )
145+ }
146+ return nil
115147}
0 commit comments