TykTechnologies / tyk

Tyk Open Source API Gateway written in Go, supporting REST, GraphQL, TCP and gRPC protocols
Other
9.72k stars 1.09k forks source link

[TT-2322] - Setting Host header in Tyk gateway when proxying the request to upstream #3535

Open anup-krai opened 3 years ago

anup-krai commented 3 years ago

Is your feature request related to a problem? Please describe.

We are running Tyk gateway (oss) with operator inside a Kubernetes cluster. We are trying to call an external service located outside of cluster through ingress. For this we have created a Service of type ExternalName.

apiVersion: v1
kind: Service
metadata:
  name: external-service
  namespace: tyk
spec:
  type: ExternalName
  externalName: httpbin.org
  ports:
  - port: 80

This service we have referred it in the Ingress -

apiVersion: tyk.tyk.io/v1alpha1
kind: ApiDefinition
metadata:
  name: httpbin-ingress-template
  labels:
    template: "true"
spec:
  name: tyk
  protocol: http
  active: true
  use_keyless: true
  proxy:
    target_url: http://httpbin.org
    listen_path: /v1/httpbin-ingress-api
    strip_listen_path: true
  version_data:
    default_version: Default
    not_versioned: true
    versions:
      Default:
        use_extended_paths: true
        name: Default
        global_headers:
          Host: "httpbin.org"
          uuid: "random"

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: httpbin-ingress
  annotations:
    kubernetes.io/ingress.class: tyk
    tyk.io/template: httpbin-ingress-template
spec:
  rules:
   - http: 
       paths:
         - path: /v1/httpbin-ingress-api
           pathType: Prefix
           backend:
             service:
               name: external-service
               port:
                 number: 80

When I call this API it is calling the ExternalName service properly but the call is failing because Host header is not set to “httpbin.org”. I tried setting the Host header in global_headers in ApiDefinition but looks like it is not getting set. Other headers are getting set properly but the Host header.

Describe the solution you'd like Looks like there is some issue while setting Host header in the gateway. Other headers are getting set properly. Host header should be set properly to a value given in the API Definition.

Describe alternatives you've considered I tried to create a simple API without ingress and ExternalName service and tried setting Host header there but looks like this is also not getting set.

Additional context I can see Host header addition in gateway logs -

time="Apr 19 07:48:34" level=debug msg="Adding: Host" api_id=dHlrL2V4dGVybmFsLWh0dHBiaW4 api_name=external-httpbin mw=TransformHeaders org_id=oss origin=*.*.*.* path="/external-httpbin/get"

In the error message we see the External service Name URL instead of httpbin.org GET http://external-service.tyk.svc.cluster.local/get

Moses-oyedeji commented 3 years ago

@anup-krai Thanks for contacting Tyk! We apologize for the delayed response. The request is currently being reviewed and we will keep you updated as soon as we can