Skip to content

Commit

Permalink
improve regex for cidr
Browse files Browse the repository at this point in the history
  • Loading branch information
salonichf5 committed Sep 5, 2024
1 parent 44fae5a commit 64547ff
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 9 deletions.
5 changes: 3 additions & 2 deletions apis/v1alpha1/nginxproxy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ type RewriteClientIP struct {
// Sets NGINX directive set_real_ip_from: https://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from
// This field is required if mode is set.
// +kubebuilder:validation:MaxItems=16
// +listType=atomic
// +listType=map
// +listMapKey=type
//
//
// +optional
Expand Down Expand Up @@ -200,6 +201,6 @@ type AddressType string

const (
// AddressTypeCIDR specifies that the address is a CIDR block.
// kubebuilder:validation:Pattern=`(\/([0-9]?[0-9]?[0-8]))$`
// kubebuilder:validation:Pattern=`^[\.a-zA-Z0-9::]*(\/([0-9]?[0-9]?[0-8]))$`
AddressTypeCIDR AddressType = "cidr"
)
4 changes: 3 additions & 1 deletion config/crd/bases/gateway.nginx.org_nginxproxies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ spec:
type: object
maxItems: 16
type: array
x-kubernetes-list-type: atomic
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
type: object
x-kubernetes-validations:
- message: if mode is set, trustedAddresses is a required field
Expand Down
4 changes: 3 additions & 1 deletion deploy/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,9 @@ spec:
type: object
maxItems: 16
type: array
x-kubernetes-list-type: atomic
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
type: object
x-kubernetes-validations:
- message: if mode is set, trustedAddresses is a required field
Expand Down
19 changes: 15 additions & 4 deletions internal/mode/static/state/graph/nginxproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,23 @@ func validateRewriteClientIP(npCfg *ngfAPI.NginxProxy) field.ErrorList {
}

for _, addr := range rewriteClientIP.TrustedAddresses {
if err := k8svalidation.IsValidCIDR(trustedAddressesPath, addr.Value); err != nil {
switch addr.Type {
case ngfAPI.AddressTypeCIDR:
if err := k8svalidation.IsValidCIDR(trustedAddressesPath, addr.Value); err != nil {
allErrs = append(
allErrs,
field.Invalid(trustedAddressesPath.Child(addr.Value),
addr,
err.ToAggregate().Error(),
),
)
}
default:
allErrs = append(
allErrs,
field.Invalid(trustedAddressesPath.Child(addr.Value),
addr,
err.ToAggregate().Error(),
field.NotSupported(trustedAddressesPath.Child(addr.Value),
addr.Type,
[]string{string(ngfAPI.AddressTypeCIDR)},
),
)
}
Expand Down
21 changes: 21 additions & 0 deletions internal/mode/static/state/graph/nginxproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,27 @@ func TestValidateRewriteClientIP(t *testing.T) {
"required when mode is set, spec.rewriteClientIP.mode: " +
"Unsupported value: \"invalid\": supported values: \"ProxyProtocol\", \"XForwardedFor\"]",
},
{
name: "invalid address type in trustedAddresses",
validator: createInvalidValidator(),
np: &ngfAPI.NginxProxy{
Spec: ngfAPI.NginxProxySpec{
RewriteClientIP: &ngfAPI.RewriteClientIP{
SetIPRecursively: helpers.GetPointer(true),
TrustedAddresses: []ngfAPI.Address{
{
Type: ngfAPI.AddressType("invalid"),
Value: "2001:db8::/129",
},
},
Mode: helpers.GetPointer(ngfAPI.RewriteClientIPModeProxyProtocol),
},
},
},
expectErrCount: 1,
errorString: "spec.rewriteClientIP.trustedAddresses.2001:db8::/129: " +
"Unsupported value: \"invalid\": supported values: \"cidr\"",
},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion site/content/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ string
</thead>
<tbody><tr><td><p>&#34;cidr&#34;</p></td>
<td><p>AddressTypeCIDR specifies that the address is a CIDR block.
kubebuilder:validation:Pattern=<code>(\/([0-9]?[0-9]?[0-8]))$</code></p>
kubebuilder:validation:Pattern=<code>^[\.a-zA-Z0-9::]*(\/([0-9]?[0-9]?[0-8]))$</code></p>
</td>
</tr></tbody>
</table>
Expand Down

0 comments on commit 64547ff

Please sign in to comment.