Adjust header names in a request

Uses: Kong Gateway decK
Related Resources
Minimum Version
Kong Gateway - 3.4
TL;DR

You can use the serverless Post-Function plugin to detect headers in a request and transform them into custom header names.

In this tutorial, we’ll edit two types of headers: headers set by a plugin (in this case, Rate Limiting), and latency headers from Kong Gateway.

We’ll enable the Post-Function plugin in the header_filter phase, where it will look for a configured list of headers, then transform those headers into different names. The upstream service then only sees the transformed header names.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

     export KONNECT_TOKEN='YOUR_KONNECT_PAT'
    
  3. Run the quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
    

    This sets up a Konnect Control Plane named quickstart, provisions a local Data Plane, and prints out the following environment variable exports:

     export DECK_KONNECT_TOKEN=$KONNECT_TOKEN
     export DECK_KONNECT_CONTROL_PLANE_NAME=quickstart
     export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
     export KONNECT_PROXY_URL='http://localhost:8000'
    

    Copy and paste these into your terminal to configure your session.

This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    

decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial you will first need to install decK.

For this tutorial, you’ll need Kong Gateway entities, like Gateway Services and Routes, pre-configured. These entities are essential for Kong Gateway to function but installing them isn’t the focus of this guide. Follow these steps to pre-configure them:

  1. Run the following command:

    echo '
    _format_version: "3.0"
    services:
      - name: example-service
        url: http://httpbin.konghq.com/anything
    routes:
      - name: example-route
        paths:
        - "/anything"
        service:
          name: example-service
    ' | deck gateway apply -
    

To learn more about entities, you can read our entities documentation.

Enable the Rate Limiting plugin

Add a Rate Limiting plugin to the example-service you created in the prerequisites:

echo '
_format_version: "3.0"
plugins:
  - name: rate-limiting
    service: example-service
    config:
      second: 5
      minute: 30
      policy: local
' | deck gateway apply -

Create a header transformation Lua function

The Post-Function plugin lets you execute Lua code. We’ll pass a function that renames the following headers:

  • Rate limiting headers: The Rate Limiting plugin returns headers such as X-RateLimit-Remaining-{time} and X-RateLimit-Limit-{time}, where {time} is the configured time span for the limit.
  • Latency headers: Kong Gateway adds latency headers to responses, such as X-Kong-Upstream-Latency and X-Kong-Proxy-Latency. While you can turn these headers on or off in kong.conf, they have fixed names that can’t be configured.

Run the following command to create a rename-headers.lua file:

cat <<EOF > rename-headers.lua
return function()

      local kong_rl_headers = {}
      kong_rl_headers["x-ratelimit-limit-second"]="X-Rlls"
      kong_rl_headers["x-ratelimit-remaining-second"]="X-Rlrs"
      kong_rl_headers["x-ratelimit-limit-minute"]="X-Rllm"
      kong_rl_headers["x-ratelimit-remaining-minute"]="X-Rlrm"
      kong_rl_headers["x-ratelimit-limit-hour"]="X-Rllh"
      kong_rl_headers["x-ratelimit-remaining-hour"]="X-Rlrh"
      kong_rl_headers["x-ratelimit-limit-day"]="X-Rlld"
      kong_rl_headers["x-ratelimit-remaining-day"]="X-Rlrd"
      kong_rl_headers["x-ratelimit-limit-month"]="X-Rlln"
      kong_rl_headers["x-ratelimit-remaining-month"]="X-Rlrn"
      kong_rl_headers["x-ratelimit-limit-year"]="X-Rlly"
      kong_rl_headers["x-ratelimit-remaining-year"]="X-Rlry"

      local headers = kong.response.get_headers()
      for k, v in pairs(headers) do
        if kong_rl_headers[k] ~= nil then
          kong.response.set_header(kong_rl_headers[k], v)
          kong.response.clear_header(k)
        end
      end

      -- Add custom headers for latency
      kong.response.set_header("My-Custom-Proxy-Latency", ngx.ctx.KONG_PROXY_LATENCY)
      kong.response.set_header("My-Custom-Upstream-Latency", ngx.ctx.KONG_WAITING_TIME)

    end
EOF

Add the rename-headers.lua file as a decK environment variable so that you can pass this file to decK:

export DECK_RENAME_HEADERS="$(cat rename-headers.lua)"

Enable the Post-Function plugin

To change the header names, set up a Post-Function plugin instance that runs globally in the header_filter phase, and pass the function as an environment variable:

echo '
_format_version: "3.0"
plugins:
- name: post-function
  route: example-route
  config:
    header_filter:
      - |
         ${{ env "DECK_RENAME_HEADERS" | indent 8 }}
' | deck gateway apply -

Validate

Let’s test that the response header names have changed:

curl -i "$KONNECT_PROXY_URL/anything"
curl -i "http://localhost:8000/anything"

The response should show the new header names.

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!