Posted community wiki answer for better visibility. Feel free to expand it.
Currently there is no simple solution for your issue in Isito using RequestAuthentication
. There is a topic on the Istio forum with a very similar question - Setting request headers with values from a JWT, last pinged 10 days ago (state for 03.12.2021) - you may consider subscribing to it.
There is also nice document - Copy JWT claims to headers which sum up this issue with proposals and possible solutions/workarounds from different websites:
There are several existing solutions and workarounds
- Solution 1: WASM filter
- A dedicated WASM filter is developed for copying claims to headers as a workaround for ASM customers.
- This solution requires the deployment and configuration of a dedicated WASM filter, the adoption process is a bit heavy and may not suitable for all ASM customers due to the use of EnvoyFIlter API.
- See more details in the Existing WASM based solution appendix.
- Solution 2: Lua filter
- The OSS community developed some example lua filter configuration for copying claims to headers.
- This solution is much lightweight as it just uses the native built-in Lua filter in upstream Envoy. The configuration is simple and has great flexibility as it's just some lua code. This solution still needs to use the EnvoyFilter API and the Lua filter is also not supported by ASM.
- See more details in the discuss,istio.io threads.
- Solution 3: Reuse an undocumented feature in VirtualService
- This solution was developed in this design to reuse an undocumented feature in the virtual service for copying dynamic metadata (JWT claims) to HTTP headers.
- This is probably the most lightweight solution as it only needs the first-class Istio virtual service API.
- There are some limitations in this solution, like the feature is undocumented and uses the Envoy logging format directly, the capability is also limited and may not support proper sanitization of the HTTP headers and claims of type other than string.
- The following is an example virtual service for copying the "group" claim to the "x-istio-jwt-group" header:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews-route
spec:
hosts:
- reviews.prod.svc.cluster.local
http:
- headers:
request:
set:
x-istio-jwt-group:
'%DYNAMIC_METADATA(["istio_authn", "request.auth.claims", "group"])%'
route: <...>
Check this article - Istio Request Control with Envoy Filters — Request Headers - a little bit outdated but with a good explanation. Check also updated code of the above solution on the GitHub page.
Another workaround is to change your app to read forwarded JWT payload from a header and get value from here. You must set outputPayloadToHeader
field:
This field specifies the header name to output a successfully verified JWT payload to the backend. The forwarded data is base64_encoded(jwt_payload_in_JSON)
. If it is not specified, the payload will not be emitted.
Keep in mind you can as well forward the original token to your app using forwardOriginalToken: true
field:
If set to true, the original token will be kept for the upstream request. Default is false.