tomMoulard / htransformation

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

Set header from the value of another header (logic already exists in Join) #73

Open shmuelarditi opened 3 months ago

shmuelarditi commented 3 months ago

Hi, @tomMoulard first of all thanks for creating this plugin we are using it in a few places and its great.

we would be happy if we could set an header based on another header value, for example using:

- name: ip-country-headers
  spec:
    plugin:
      htransformation:
        Rules:
        - Name: copy cf-connecting-ip value to new header cf-original-connecting-ip
          type: Set
          HeaderPrefix: "^"
          header: Client-Original-Connecting-Ip
          value: "^Cf-Connecting-Ip"

        - Name: copy cf-connecting-ip value to new header cf-original-connecting-ip
          type: Set
          HeaderPrefix: "^"
          header: Client-Original-Country
          value: "^Cf-Ipcountry"

it will be a great and very useful addition, and considering there is similar logic in the Join handler i hope it wont be much work.

shmuelarditi commented 3 months ago

UPDATE:

i was able to archive this using a workaround, example:

- name: ip-country-headers
  spec:
    plugin:
      htransformation:
        Rules:
        - Name: 'Initialize Client-Original-Connecting-Ip'
          Header: 'Client-Original-Connecting-Ip'
          Value: ''  # Setting it to empty
          Type: 'Set'

        - Name: 'Copy CF-Connecting-Ip'
          Header: 'Client-Original-Connecting-Ip'
          HeaderPrefix: '^'
          Sep: ' ' #cant use empty separators so just use space, it will remove it
          Values:
              - '^Cf-Connecting-Ip'
          Type: 'Join'

result from a dummy service i created on our k8s that prints headers:

"cf-connecting-ip": "176.231.166.59",
"client-original-connecting-ip": "176.231.166.59",

would still appreciate adding this feature to the SET handler, as this workout is working but a bit ugly :)

tomMoulard commented 3 months ago

Hello @shmuelarditi,

Thanks for your interest in this traefik plugin !

Indeed, this workaround exists, and might not be the best solution for this.

Feel free to open a PR to add this new feature in the Set Handler :smile:

redge76 commented 9 hours ago

I have found that the white space is not "removed", you have use the following code to copy a header to another one:

    mw-cf-copy-user-to-X-Forwarded-User:
      plugin:
        htransformation:
          Rules:
          - Name: 'Initialize X-Forwarded-User'
            Header: 'X-Forwarded-User'
            Value: ''  # Setting it to empty
            Type: 'Set'

          - Name: 'Copy cf-Access-Authenticated-User-Email'
            Header: 'X-Forwarded-User'
            HeaderPrefix: '^'
            Sep: ' ' #cant use empty separators so just use space, it will remove it
            Values:
              - '^Cf-Access-Authenticated-User-Email'
            Type: 'Join'

          - Name: 'Remove empty space'
            Header: 'X-Forwarded-User'
            Value: ' (.*)'
            ValueReplace: '$1'
            Type: 'RewriteValueRule'