Skip to content

Commit 1b8bac2

Browse files
committed
Update Golang API with defaults and CEL validation with kubebuilder
1 parent c937366 commit 1b8bac2

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

docs/proposals/authentication-filter.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ package v1alpha1
6666

6767
import (
6868
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
69+
"github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1"
6970
)
7071

7172
// +genclient
@@ -102,6 +103,12 @@ type AuthenticationFilterList struct {
102103

103104
// AuthenticationFilterSpec defines the desired configuration.
104105
// Exactly one of Basic or JWT must be set according to Type.
106+
// +kubebuilder:validation:XValidation:message="for type=Basic, spec.basic must be set and spec.jwt must be empty; for type=JWT, spec.jwt must be set and spec.basic must be empty",rule="self.type == 'Basic' ? self.basic != null && self.jwt == null : self.type == 'JWT' ? self.jwt != null && self.basic == null : false"
107+
108+
// +kubebuilder:validation:XValidation:message="type 'Basic' requires spec.basic to be set. All other spec types must be unset",rule="self.type == 'Basic' ? self.type != null && self.jwt == null : true"
109+
// +kubebuilder:validation:XValidation:message="type 'JWT' requires spec.jwt to be set. All other spec types must be unset",rule="self.type == 'JWT' ? self.type != null && self.basic == null : true"
110+
// +kubebuilder:validation:XValidation:message="when spec.basic is set, type must be 'Basic'",rule="self.basic != null ? self.type == 'Basic' : true"
111+
// +kubebuilder:validation:XValidation:message="when spec.jwt is set, type must be 'JWT'",rule="self.jwt != null ? self.type == 'JWT' : true"
105112
type AuthenticationFilterSpec struct {
106113
// Type selects the authentication mechanism.
107114
//
@@ -134,7 +141,6 @@ type BasicAuth struct {
134141
Secret string `json:"secret"`
135142

136143
// Key is the key within the Secret that contains the htpasswd data.
137-
// Default: "htpasswd".
138144
//
139145
// +optional
140146
Key *string `json:"key,omitempty"`
@@ -150,7 +156,11 @@ type BasicAuth struct {
150156
OnFailure *AuthFailureResponse `json:"onFailure,omitempty"`
151157
}
152158

153-
// JWTAuth configures JWT-based authentication (NGINX Plus).
159+
// JWTAuth configures JWT-based authentication (NGINX Plus).
160+
// +kubebuilder:validation:XValidation:message="mode 'File' requires file set and remote unset",rule="self.mode == 'File' ? self.file != null && self.remote == null : true"
161+
// +kubebuilder:validation:XValidation:message="mode 'Remote' requires remote set and file unset",rule="self.mode == 'Remote' ? self.remote != null && self.file == null : true"
162+
// +kubebuilder:validation:XValidation:message="when file is set, mode must be 'File'",rule="self.file != null ? self.mode == 'File' : true"
163+
// +kubebuilder:validation:XValidation:message="when remote is set, mode must be 'Remote'",rule="self.remote != null ? self.mode == 'Remote' : true"
154164
type JWTAuth struct {
155165
// Realm used by NGINX auth_jwt; sets realm in the auth challenge.
156166
//
@@ -162,6 +172,8 @@ type JWTAuth struct {
162172
//
163173
// +optional
164174
// +kubebuilder:validation:Enum=File;Remote
175+
// +kubebuilder:default=File
176+
// +kubebuilder:validation:XValidation:message="mode must be one of [File, Remote]",rule="self in ['File','Remote']"
165177
Mode JWTKeyMode `json:"mode,omitempty"`
166178

167179
// File specifies local JWKS configuration (Secret or ConfigMap, mount path, file name).
@@ -180,7 +192,8 @@ type JWTAuth struct {
180192
// Example: "60s".
181193
//
182194
// +optional
183-
Leeway *string `json:"leeway,omitempty"`
195+
// +kubebuilder:default=60s
196+
Leeway *v1alpha1.Duration `json:"leeway,omitempty"`
184197

185198
// Type sets token type: signed | encrypted | nested (auth_jwt_type).
186199
// Default: "signed".
@@ -333,26 +346,31 @@ type JWTTokenSource struct {
333346
// Read token from Authorization header. Default: true.
334347
//
335348
// +optional
349+
// +kubebuilder:default=true
336350
Header *bool `json:"header,omitempty"`
337351

338352
// Read token from a cookie. Default: false.
339353
//
340354
// +optional
355+
// +kubebuilder:default=false
341356
Cookie *bool `json:"cookie,omitempty"`
342357

343358
// CookieName when Cookie is true. Example: "access_token".
344359
//
345360
// +optional
361+
// +kubebuilder:default=access_token
346362
CookieName *string `json:"cookieName,omitempty"`
347363

348364
// Read token from query string. Default: false.
349365
//
350366
// +optional
367+
// +kubebuilder:default=false
351368
Query *bool `json:"query,omitempty"`
352369

353370
// QueryParam when Query is true. Example: "access_token".
354371
//
355372
// +optional
373+
// +kubebuilder:default=access_token
356374
QueryParam *string `json:"queryParam,omitempty"`
357375
}
358376

@@ -395,9 +413,12 @@ const (
395413

396414
// AuthFailureResponse customizes 401/403 failures.
397415
type AuthFailureResponse struct {
398-
// Allowed: 401, 403. Default: 401.
416+
// Allowed: 401, 403.
417+
// Default: 401.
399418
//
400419
// +optional
420+
// +kubebuilder:default=401
421+
// +kubebuilder:validation:XValidation:message="statusCode must be 401 or 403",rule="self == null || self in [401, 403]"
401422
StatusCode *int32 `json:"statusCode,omitempty"`
402423

403424
// Challenge scheme. If omitted, inferred from filter Type (Basic|Bearer).
@@ -407,10 +428,11 @@ type AuthFailureResponse struct {
407428
Scheme *AuthScheme `json:"scheme,omitempty"`
408429

409430
// Controls whether a default canned body is sent or an empty body.
410-
// Default: Default.
431+
// Default: Unauthorized.
411432
//
412433
// +optional
413434
// +kubebuilder:validation:Enum=Unauthorized;Forbidden;Empty
435+
// +kubebuilder:default=Unauthorized
414436
BodyPolicy *AuthFailureBodyPolicy `json:"bodyPolicy,omitempty"`
415437
}
416438

@@ -422,13 +444,12 @@ type LocalObjectReference struct {
422444
// SecretKeyReference references a Secret and an optional key.
423445
type SecretKeyReference struct {
424446
Name string `json:"name"`
447+
425448
// Key within the Secret data. If omitted, controller defaults apply (e.g., "jwks.json").
426-
//
427-
// +optional
428-
Key *string `json:"key,omitempty"`
449+
Key string `json:"key,omitempty"`
429450
}
430451

431-
// AuthenticationFilterStatus defines the state of AuthenticationFilter (similar to SnippetsFilter).
452+
// AuthenticationFilterStatus defines the state of AuthenticationFilter.
432453
type AuthenticationFilterStatus struct {
433454
// Controllers is a list of Gateway API controllers that processed the AuthenticationFilter
434455
// and the status of the AuthenticationFilter with respect to each controller.

0 commit comments

Comments
 (0)