You can customize request headers and response headers by using the EnvoyFilter CustomResourceDefinition (CRD). The EnvoyFilter CRD allows you to directly modify the configuration of an Istio proxy (Envoy). This way, you can add, delete, or modify request or response headers when a request or response flows through the proxy.
Prerequisites
The HTTPBin application is deployed. For more information, see Deploy the HTTPBin application.
Step 1: Define an Envoy filter template
ASM allows you to create an Envoy filter by using an Envoy filter template. The same Envoy filter template can be used to create multiple Envoy filters, and these Envoy filters can be applied to different workloads and namespaces. This makes configurations reusable and improves management efficiency.
The following YAML code provides an example of an Envoy filter template. For more information, see Envoy Filter.
If Istio 1.9 or later is used, replace the value of the
proxyVersion
field with the Istio version.If Istio 1.8 or an earlier version is used, replace the value of the
proxyVersion
field with the Istio version. In addition, replaceenvoy.filters.network.http_connection_manager
withenvoy.http_connection_manager
,envoy.filters.http.router
withenvoy.router
, andtype.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
withtype.googleapis.com/envoy.config.filter.http.lua.v2.Lua
in the preceding YAML code.
Step 2: Create an Envoy filter by using the Envoy filter template
After you create an Envoy filter template, you must bind the Envoy filter template to workloads or namespaces. This way, the Envoy filters created based on this template take effects only on the specified workloads or namespaces. After you bind the Envoy filter template to workloads or namespaces, ASM automatically creates Envoy filters by using the template.
The created Envoy filter adds a custom request header to a request that enters the workload and adds a custom response header to a response that will be sent by the workload. The following example creates an Envoy filter named custom-header-filter
and configures a Lua filter to add request headers and response headers. The Envoy filter takes effect for all workloads with the app: my-app
label.
EnvoyFilter is a powerful but complex configuration method. It directly modifies the underlying Envoy configurations. Therefore, when you use EnvoyFilter, we recommend that you be familiar with the configuration model of Envoy and modify it with caution to avoid potential errors. Meanwhile, the configuration of EnvoyFilter may change with the upgrade of Istio versions. Pay attention to compatibility issues.
Step 3: View custom request headers and response headers in access logs
ASM allows you to customize log formats. The custom expressions of access logs can obtain values from request headers, response headers, and Envoy built-in values. For more information, see Customize the format of access logs.
The following table lists three new fields, which are used to display content in access logs.
Field | Type | Value |
my-x-custom-request-header | Request attribute | %REQ(x-custom-request-header)% |
baggage-from-request | Request attribute | %REQ(baggage)% |
my-x-custom-response-header | Response attribute | %RESP(x-custom-response-header)% |
Check the access logs of the HTTPBin pod and you can see content similar to the following:
{
"bytes_received": "0",
"bytes_sent": "490",
"duration": "1",
"istio_policy_status": "-",
"method": "GET",
"path": "/headers",
"protocol": "HTTP/1.1",
"response_code": "200",
"response_flags": "-",
"my-x-custom-request-header": "xxx",
"baggage-from-request": "x-custom-request-header=xxx",
"my-x-custom-response-header": "x-custom-request-header=xxx",
}