kubernetes-ingress icon indicating copy to clipboard operation
kubernetes-ingress copied to clipboard

Regular expressions with "?!" are not supported in VirtualServer

Open LuciferInLove opened this issue 5 years ago • 9 comments

Describe the bug Regular expressions that contains "?!" not supported in path or routes and subroutes in VirtualServer and VirtualServerRoute resources.

To Reproduce

  1. Deploy yamls:
apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: cafe
  namespace: default
spec:
  host: cafe.example.com
  upstreams:
  - name: tea
    service: tea-svc
    port: 80
  routes:
  - path: /tea
    action:
      pass: tea
  - path: /coffee
    route: default/coffee
apiVersion: k8s.nginx.org/v1
kind: VirtualServerRoute
metadata:
  name: coffee
  namespace: default
spec:
  host: cafe.example.com
  upstreams:
  - name: latte
    service: latte-svc
    port: 80
  - name: espresso
    service: espresso-svc
    port: 80
  subroutes:
  - path: ~ ^/coffee/latte$ # it's works
    action:
      pass: latte
  - path: ~ ^/coffee/(?!.*\/latte)(?!.*\/americano)(.*) # it isn't working
    action:
      pass: espresso
  1. View logs on ingress-controller pod
  2. See error:
I1223 08:46:01.744934       1 controller.go:1369] Skipping invalid VirtualServerRoute default/coffee: spec.subroutes[1].path: Invalid value: "~ ^/coffee/(?!.*\/latte)(?!.*\/americano)(.*)": must be a valid regular expression: error parsing regexp: invalid or unsupported Perl syntax: `(?!`

Expected behavior All regular expressions that work in bare nginx must work in path.

Your environment

  • Version of the Ingress Controller - release version or a specific commit 1.6.0
  • Version of Kubernetes 1.15.3
  • Kubernetes platform (e.g. Mini-kube or GCP) bare kubernetes
  • Using NGINX or NGINX Plus NGINX

Aha! Link: https://nginx.aha.io/features/IC-109

LuciferInLove avatar Dec 23 '19 08:12 LuciferInLove

Hi @LuciferInLove thanks for reporting!

The supported regex syntax is currently limited by regular expressions supported in golang https://github.com/google/re2/wiki/Syntax

We might overcome this limitation in the future.

Are there any workaround regular expressions that you could use that can cover your case?

pleshakov avatar Dec 23 '19 20:12 pleshakov

Are there any workaround regular expressions that you could use that can cover your case?

Unfortunately, no, but now I'm using bare nginx deployed in k8s with own customizations.

LuciferInLove avatar Dec 23 '19 21:12 LuciferInLove

Ok. We'll keep the issue open to track this limitation.

pleshakov avatar Dec 24 '19 01:12 pleshakov

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Mar 09 '21 02:03 github-actions[bot]

Just a comment to leave this issue open.

LuciferInLove avatar Mar 09 '21 08:03 LuciferInLove

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar May 10 '21 01:05 github-actions[bot]

Just a comment to leave this issue open.

LuciferInLove avatar May 10 '21 09:05 LuciferInLove

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Sep 01 '21 01:09 github-actions[bot]

Just a comment to leave this issue open.

LuciferInLove avatar Sep 01 '21 09:09 LuciferInLove

I came across this issue recently. Seems kic should implement regex libraries that more closely matches that which NGINX uses, such as go pcre2.

jnewfield avatar Oct 07 '22 00:10 jnewfield

any updates here? or any suggestions for work arounds?

artbegolli avatar Jul 05 '23 13:07 artbegolli

We could also leverage the following libraries:

https://pkg.go.dev/github.com/s-kozlov/goback/regexp/syntax

https://pkg.go.dev/github.com/dlclark/regexp2#section-readme

danielnginx avatar Sep 07 '23 15:09 danielnginx

test log:

I1003 13:52:25.597291       1 event.go:298] Event(v1.ObjectReference{Kind:"VirtualServer", Namespace:"default", Name:"cafe", UID:"eb5fa829-4f39-4cd1-973a-78282d2d76d2", APIVersion:"k8s.nginx.org/v1", ResourceVersion:"277941", FieldPath:""}): type: 'Warning' reason: 'AddedOrUpdatedWithWarning' Configuration for default/cafe was added or updated with warning(s): VirtualServerRoute default/coffee doesn't exist or invalid
I1003 13:52:46.350360       1 event.go:298] Event(v1.ObjectReference{Kind:"VirtualServerRoute", Namespace:"default", Name:"coffee", UID:"d9c0f987-02e7-4554-8a8e-6ac7088afa54", APIVersion:"k8s.nginx.org/v1", ResourceVersion:"277976", FieldPath:""}): type: 'Warning' reason: 'Rejected' VirtualServerRoute default/coffee was rejected with error: spec.subroutes[1].path: Invalid value: "~ ^/coffee/(?!.*\\/latte)(?!.*\\/americano)(.*)": must be a valid regular expression: error parsing regexp: invalid or unsupported Perl syntax: `(?!`

jjngx avatar Oct 03 '23 13:10 jjngx

Employing regexp2 package that uses Perl5 / .NET compatible regex implementation engine solves the path validation problem.

jjngx avatar Oct 04 '23 06:10 jjngx