Skip to content

Commit ec4af3b

Browse files
committed
Add SNI related options and validation
1 parent 826ebf0 commit ec4af3b

File tree

5 files changed

+45
-17
lines changed

5 files changed

+45
-17
lines changed

internal/configs/version2/http.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,14 @@ func (rl LimitReqOptions) String() string {
428428

429429
// JWTAuth holds JWT authentication configuration.
430430
type JWTAuth struct {
431-
Key string
432-
Secret string
433-
Realm string
434-
Token string
435-
KeyCache string
436-
JwksURI JwksURI
431+
Key string
432+
Secret string
433+
Realm string
434+
Token string
435+
KeyCache string
436+
JwksSNIName string
437+
JwksSNIEnabled bool
438+
JwksURI JwksURI
437439
}
438440

439441
// JwksURI defines the components of a JwksURI

internal/configs/version2/nginx-plus.virtualserver.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ server {
236236
proxy_cache jwks_uri_{{ $s.VSName }};
237237
proxy_cache_valid 200 12h;
238238
{{- end }}
239+
{{- if .JwksSNIEnabled }}
240+
proxy_ssl_server_name on;
241+
{{- if .JwksSNIName }}
242+
proxy_ssl_name {{ .JwksSNIName }};
243+
{{- end }}
244+
{{- end }}
239245
{{- with .JwksURI }}
240246
proxy_set_header Host {{ .JwksHost }};
241247
set $idp_backend {{ .JwksHost }};

internal/configs/virtualserver.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,11 +1176,13 @@ func (p *policiesCfg) addJWTAuthConfig(
11761176
}
11771177

11781178
p.JWTAuth.Auth = &version2.JWTAuth{
1179-
Key: polKey,
1180-
JwksURI: *JwksURI,
1181-
Realm: jwtAuth.Realm,
1182-
Token: jwtAuth.Token,
1183-
KeyCache: jwtAuth.KeyCache,
1179+
Key: polKey,
1180+
JwksURI: *JwksURI,
1181+
Realm: jwtAuth.Realm,
1182+
Token: jwtAuth.Token,
1183+
KeyCache: jwtAuth.KeyCache,
1184+
JwksSNIEnabled: jwtAuth.SNIEnabled,
1185+
JwksSNIName: jwtAuth.SNIServerName,
11841186
}
11851187
p.JWTAuth.JWKSEnabled = true
11861188
return res

pkg/apis/configuration/v1/types.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,13 @@ type VariableCondition struct {
652652

653653
// JWTAuth holds JWT authentication configuration.
654654
type JWTAuth struct {
655-
Realm string `json:"realm"`
656-
Secret string `json:"secret"`
657-
Token string `json:"token"`
658-
JwksURI string `json:"jwksURI"`
659-
KeyCache string `json:"keyCache"`
655+
Realm string `json:"realm"`
656+
Secret string `json:"secret"`
657+
Token string `json:"token"`
658+
JwksURI string `json:"jwksURI"`
659+
KeyCache string `json:"keyCache"`
660+
SNIEnabled bool `json:"sniEnabled"`
661+
SNIServerName string `json:"sniServerName"`
660662
}
661663

662664
// BasicAuth holds HTTP Basic authentication configuration

pkg/apis/configuration/validation/policy.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
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"
@@ -213,7 +214,22 @@ func validateJWT(jwt *v1.JWTAuth, fieldPath *field.Path) field.ErrorList {
213214
if jwt.KeyCache == "" {
214215
allErrs = append(allErrs, field.Required(fieldPath.Child("keyCache"), "key cache must be set, example value: 1h"))
215216
}
216-
return allErrs
217+
218+
// if SNI server name is provided, but SNI is not enabled, return an error
219+
if jwt.SNIServerName != "" && !jwt.SNIEnabled {
220+
allErrs = append(allErrs, field.Forbidden(fieldPath.Child("sniServerName"), "sniServerName can only be set when sniEnabled is true"))
221+
}
222+
223+
// if SNI is enabled and SNI server name is provided, make sure it's a valid URI
224+
if jwt.SNIEnabled && jwt.SNIServerName != "" {
225+
err := validation2.ValidateURI(jwt.SNIServerName,
226+
validation2.WithAllowedSchemes("https"),
227+
validation2.WithUserAllowed(false),
228+
validation2.WithDefaultScheme("https"))
229+
if err != nil {
230+
allErrs = append(allErrs, field.Invalid(fieldPath.Child("sniServerName"), jwt.SNIServerName, "sniServerName is not a valid URI"))
231+
}
232+
}
217233
}
218234
return allErrs
219235
}

0 commit comments

Comments
 (0)