File tree Expand file tree Collapse file tree 6 files changed +46
-9
lines changed
internal/mode/static/state/graph Expand file tree Collapse file tree 6 files changed +46
-9
lines changed Original file line number Diff line number Diff line change @@ -155,7 +155,8 @@ type RewriteClientIP struct {
155
155
// Sets NGINX directive set_real_ip_from: https://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from
156
156
// This field is required if mode is set.
157
157
// +kubebuilder:validation:MaxItems=16
158
- // +listType=atomic
158
+ // +listType=map
159
+ // +listMapKey=type
159
160
//
160
161
//
161
162
// +optional
@@ -200,6 +201,6 @@ type AddressType string
200
201
201
202
const (
202
203
// AddressTypeCIDR specifies that the address is a CIDR block.
203
- // kubebuilder:validation:Pattern=`(\/([0-9]?[0-9]?[0-8]))$`
204
+ // kubebuilder:validation:Pattern=`^[\.a-zA-Z0-9::]* (\/([0-9]?[0-9]?[0-8]))$`
204
205
AddressTypeCIDR AddressType = "cidr"
205
206
)
Original file line number Diff line number Diff line change @@ -118,7 +118,9 @@ spec:
118
118
type : object
119
119
maxItems : 16
120
120
type : array
121
- x-kubernetes-list-type : atomic
121
+ x-kubernetes-list-map-keys :
122
+ - type
123
+ x-kubernetes-list-type : map
122
124
type : object
123
125
x-kubernetes-validations :
124
126
- message : if mode is set, trustedAddresses is a required field
Original file line number Diff line number Diff line change @@ -703,7 +703,9 @@ spec:
703
703
type : object
704
704
maxItems : 16
705
705
type : array
706
- x-kubernetes-list-type : atomic
706
+ x-kubernetes-list-map-keys :
707
+ - type
708
+ x-kubernetes-list-type : map
707
709
type : object
708
710
x-kubernetes-validations :
709
711
- message : if mode is set, trustedAddresses is a required field
Original file line number Diff line number Diff line change @@ -172,12 +172,23 @@ func validateRewriteClientIP(npCfg *ngfAPI.NginxProxy) field.ErrorList {
172
172
}
173
173
174
174
for _ , addr := range rewriteClientIP .TrustedAddresses {
175
- if err := k8svalidation .IsValidCIDR (trustedAddressesPath , addr .Value ); err != nil {
175
+ switch addr .Type {
176
+ case ngfAPI .AddressTypeCIDR :
177
+ if err := k8svalidation .IsValidCIDR (trustedAddressesPath , addr .Value ); err != nil {
178
+ allErrs = append (
179
+ allErrs ,
180
+ field .Invalid (trustedAddressesPath .Child (addr .Value ),
181
+ addr ,
182
+ err .ToAggregate ().Error (),
183
+ ),
184
+ )
185
+ }
186
+ default :
176
187
allErrs = append (
177
188
allErrs ,
178
- field .Invalid (trustedAddressesPath .Child (addr .Value ),
179
- addr ,
180
- err . ToAggregate (). Error () ,
189
+ field .NotSupported (trustedAddressesPath .Child (addr .Value ),
190
+ addr . Type ,
191
+ [] string { string ( ngfAPI . AddressTypeCIDR )} ,
181
192
),
182
193
)
183
194
}
Original file line number Diff line number Diff line change @@ -517,6 +517,27 @@ func TestValidateRewriteClientIP(t *testing.T) {
517
517
"required when mode is set, spec.rewriteClientIP.mode: " +
518
518
"Unsupported value: \" invalid\" : supported values: \" ProxyProtocol\" , \" XForwardedFor\" ]" ,
519
519
},
520
+ {
521
+ name : "invalid address type in trustedAddresses" ,
522
+ validator : createInvalidValidator (),
523
+ np : & ngfAPI.NginxProxy {
524
+ Spec : ngfAPI.NginxProxySpec {
525
+ RewriteClientIP : & ngfAPI.RewriteClientIP {
526
+ SetIPRecursively : helpers .GetPointer (true ),
527
+ TrustedAddresses : []ngfAPI.Address {
528
+ {
529
+ Type : ngfAPI .AddressType ("invalid" ),
530
+ Value : "2001:db8::/129" ,
531
+ },
532
+ },
533
+ Mode : helpers .GetPointer (ngfAPI .RewriteClientIPModeProxyProtocol ),
534
+ },
535
+ },
536
+ },
537
+ expectErrCount : 1 ,
538
+ errorString : "spec.rewriteClientIP.trustedAddresses.2001:db8::/129: " +
539
+ "Unsupported value: \" invalid\" : supported values: \" cidr\" " ,
540
+ },
520
541
}
521
542
522
543
for _ , test := range tests {
Original file line number Diff line number Diff line change @@ -533,7 +533,7 @@ string
533
533
</thead >
534
534
<tbody ><tr ><td ><p >" ; cidr" ; </p ></td >
535
535
<td ><p >AddressTypeCIDR specifies that the address is a CIDR block.
536
- kubebuilder:validation: Pattern =<code >(\/ ([ 0-9] ?[ 0-9] ?[ 0-8] ))$</code ></p >
536
+ kubebuilder:validation: Pattern =<code >^ [ \. a-zA-Z0-9:: ] * (\/ ([ 0-9] ?[ 0-9] ?[ 0-8] ))$</code ></p >
537
537
</td >
538
538
</tr ></tbody >
539
539
</table >
You can’t perform that action at this time.
0 commit comments