99 "strings"
1010 "unicode"
1111
12+ validation2 "github.com/nginx/kubernetes-ingress/internal/validation"
1213 v1 "github.com/nginx/kubernetes-ingress/pkg/apis/configuration/v1"
1314 "k8s.io/apimachinery/pkg/util/validation"
1415 "k8s.io/apimachinery/pkg/util/validation/field"
@@ -198,6 +199,16 @@ func validateJWT(jwt *v1.JWTAuth, fieldPath *field.Path) field.ErrorList {
198199 if jwt .KeyCache != "" {
199200 allErrs = append (allErrs , field .Forbidden (fieldPath .Child ("keyCache" ), "key cache must not be used when using Secret" ))
200201 }
202+
203+ // If JwksURI is not set, then none of the SNI fields should be set.
204+ if jwt .SNIEnabled {
205+ return append (allErrs , field .Forbidden (fieldPath .Child ("sniEnabled" ), "sniEnabled can only be set when JwksURI is set" ))
206+ }
207+
208+ if jwt .SNIName != "" {
209+ return append (allErrs , field .Forbidden (fieldPath .Child ("sniName" ), "sniName can only be set when JwksURI is set" ))
210+ }
211+
201212 return allErrs
202213 }
203214
@@ -213,7 +224,22 @@ func validateJWT(jwt *v1.JWTAuth, fieldPath *field.Path) field.ErrorList {
213224 if jwt .KeyCache == "" {
214225 allErrs = append (allErrs , field .Required (fieldPath .Child ("keyCache" ), "key cache must be set, example value: 1h" ))
215226 }
216- return allErrs
227+
228+ // if SNI server name is provided, but SNI is not enabled, return an error
229+ if jwt .SNIName != "" && ! jwt .SNIEnabled {
230+ allErrs = append (allErrs , field .Forbidden (fieldPath .Child ("sniServerName" ), "sniServerName can only be set when sniEnabled is true" ))
231+ }
232+
233+ // if SNI is enabled and SNI server name is provided, make sure it's a valid URI
234+ if jwt .SNIEnabled && jwt .SNIName != "" {
235+ err := validation2 .ValidateURI (jwt .SNIName ,
236+ validation2 .WithAllowedSchemes ("https" ),
237+ validation2 .WithUserAllowed (false ),
238+ validation2 .WithDefaultScheme ("https" ))
239+ if err != nil {
240+ allErrs = append (allErrs , field .Invalid (fieldPath .Child ("sniServerName" ), jwt .SNIName , "sniServerName is not a valid URI" ))
241+ }
242+ }
217243 }
218244 return allErrs
219245}
0 commit comments