tomMoulard / htransformation

A Traefik plugin to change on the fly header's value of a request
MIT License
77 stars 13 forks source link

Conditional execution of rules #45

Closed gumbo2k closed 1 year ago

gumbo2k commented 1 year ago

I think this would be a major feature request, but it would enable htransformation to replace and supersede several other plugins.

Just the check for existence or-non-existence of a specific header alone would allow rules that replace most of the plugins that comb through the existing headers that were added by external loadbalancers, to set the real client IP.

Just this example here would consolidate the "X-Real-IP" header, no matter if it is already set, or gets its value from "X-From-CDN" or from "X-Original-IP".

- Rule:
  Name: 'first cdn'
  If:
    - 'exists x-from-cdn'
    - 'not-exists X-Real-IP'
  Type: 'Rename'
  Header: 'x-from-cdn'
  Value: 'X-Real-IP'
- Rule:
  Name: 'second cdn'
  If:
    - 'exists x-original-ip'
    - 'not-exists X-Real-IP'
  Type: 'Rename'
  Header: 'x-original-ip'
  Value: 'X-Real-IP'

Conditions in the `If:' list would have to be evaluated one after another, and only if they are all true, the header manipulation is executed.

tomMoulard commented 1 year ago

Hello @gumbo2k,

Thanks for your interest in this Traefik Plugin!

I can see the use case for this, but I feel it would be a bad workaround (performance wise) as creating multiple middlewares on different routers. This would look something like:

- traefik.http.routers.router1.rule: Header(`x-from-cdn`) && !Header(`x-real-ip`)
- traefik.http.routers.router1.middlewares: htransformation-rename-first
- traefik.http.routers.router2.rule: Header(`x-original-ip`) && !Header(`x-real-ip`)
- traefik.http.routers.router2.middlewares: htransformation-rename-second

But I get that this is kind of cumbersome to configure.

As I do not think that is a valid use case, I will close the issue.