I use Ingress Nginx in my kubernetes cluster. I use Keycloak to manage users and groups, and oauth2-proxy. I filter access to some resources like :
User must belong to group1 to access my.app.com/resources/group1/page.html
I'm able to enforce group checking with this ingress, works as expected :
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/auth-url: "https://###/oauth2/auth?allowed_groups=group1"
nginx.ingress.kubernetes.io/auth-signin: "https://###/oauth2/start?rd=$escaped_request_uri"
name: app
spec:
ingressClassName: nginx
rules:
- host:###
http:
paths:
- path: /resources/group1/(/|$)(.*)
pathType: Prefix
backend:
service:
name: app
port:
number: 80
I would have to duplicate this ingress for every group, and for every new group added later... I'm looking for a way to input the group as a parameter from the path to auth-url. I tried :
nginx.ingress.kubernetes.io/auth-url: "https://###/oauth2/auth?allowed_groups=$3"
...
path: /resources(/|$)((\w*)(/|$)(.*))
But when I inspect the requests sent to oauth2, allowed_groups is empty. Tried with $2,$3,$4... it seems I just can't use the regex in this annotation.
How can I do it ?