Injecting custom headers to http route

Using Istio 1.34 (bundled with microk8s 1.18.2) I would like to add some custom headers to a http route. These custom headers must be injected to the http request before reaching the service:

  • My-Custom-Header1: “abc-123”
  • My-Custom-Header2: “[5, 6, 7]”

QUESTION1: Can you please show the correct way to configure the injection of the custom headers. I am confused by the istio documentation HTTPRoute. Contrary to the match field which accepts an array of HTTPMatchRequest[]. The headers field can accept only one value of type Headers. How is it possible to add more than one custome header?

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: myapp-virtsvc
spec:
  hosts:
  - "*"
  gateways:
  - my-istio-gateway
  http:
  - match:
    - uri:
        prefix: "/mysvc1/"
    route:
    - destination:
        host: myservice1
        port:
          number: 80
    name: "svc1-routes"
  - match:
    - uri:
        prefix: /
    route:
    - destination:
        host: my-main-service
        port:
          number: 80
    name: "main-svc-routes"
  headers:
    request:
      add:
        My-Custom-Header1: "abc-123"
      #---- how to add more custom headers? ----

QUESTION2: Notice in the VirtualService config above, there are two match under the http route. The headers field definition injects the custom headers for both match. How to restrict the custom header injection only for match.name: "main-svc-routes" ?

Thanks in advance for any help

Hi @TristanStanic

  1. you can restrict the custom header by puting it in the coressponding route of match.name: main-svc-routes.

See this Add headers using virtual service for ur question. ask here. having same issue.

May be this will help u.

Thanks @Shubham.
I have read carefully the post “Add headers using virtual service” you referred before creating this post. The answer is confusing. as it is out of context of a VirtualService. I tried to use that answer in my config above. And this seems NOT to work (in Firefox Dev Tool, I don’t see the custom header added to the http request)

In that post, at the end, member “roquesao” asked practically the same question as this post. There was no answer.

This seems trivial but I am confused by itsio doc (no example and no mention of array in the headers filed) and the yaml syntax. If you know the answer, can you please fix the YAML I mentioned in the original post?

Hi @TristanStanic

I also searched how to add 2 headers. but nothing found yet. you can ask there may be that member solved it. If i find something then i will tell you. you can also ask this on istio slack channel (for fast respone)

for the second question you can try (AFIK)

- match:
    name: "main-svc-routes"
    - uri:
        prefix: /
    route:
    - destination:
        host: my-main-service
        port:
          number: 80
      headers:
        request:
         add:
            My-Custom-Header1: "abc-123"

https://istiobyexample.dev/response-headers/ see this confiquration.

Thank you for the link to https://istiobyexample.dev/response-headers/. Now I see better the various positions of the headers field.

Asfor injecting multiple custome header, I think (but not sure) we can add them as list items. Because the documentation of Headers.HeaderOperations says that the value is map<string, string> so I think we can add each header as an map entry of a map collection.

I hope you can help to confirm. Because so far I could apply successfully the yaml but don’t see the custom header showing up in Firefox Dev Tool

Hi
i tried it. same problem vs configured but doesn’t show header (even when i applied single one). but when i add headers in response it show headers on calling. don’t know what is the issue.( have you try it with response instead of request it works.)
Its work in the response but not in request.

if i will find it i will tell you.

Hi @TristanStanic

i tried the ingress task. in which i add the headers for both request and response.
it show headers(request) in the cookies. not in the Firefox dev tool. but the headers (response) show in the Firefox dev tool.


virtual service i used:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: httpbin
spec:
  hosts:
  - "*"
  gateways:
  - httpbin-gateway
  http:
  - match:
    - uri:
        prefix: /headers
    route:
    - destination:
        port:
          number: 8000
        host: httpbin
      headers:
        response:
          add:
           "key1": "abc"
        request:
           add:
             "key2": "def"

gateway:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: httpbin-gateway
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"

see this:


Don’t much informartion why it is happened. if you find something then let me know.