API Key Authentication (apiKeyAuth) strips Bearer from beginning of the string
Description:
When utilizing apiKeyAuth method in SecurityPolicy, when a secret key is prefixed with Bearer, eg. Bearer my-api-key and then user auths with the header Authorization: Bearer my-api-key the auth fails with 401. The solution to this issue is to specify the api-key in the Secret without the Bearer prefix.
Repro steps: Non-working configuration:
apiVersion: v1
kind: Secret
metadata:
name: api-keys
type: Opaque
stringData:
client1: "Bearer my-api-key"
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
name: authorization-api-keys
spec:
targetRefs:
- group: gateway.networking.k8s.io
kind: HTTPRoute
name: app-routes
apiKeyAuth:
credentialRefs:
- group: ""
kind: Secret
namespace: qa01
name: api-keys
extractFrom:
- headers:
- authorization
Curl output
curl -XGET -I -H "authorization: Bearer my-api-key" https://envoy
HTTP/2 401
Working configuration:
apiVersion: v1
kind: Secret
metadata:
name: api-keys
type: Opaque
stringData:
client1: "my-api-key"
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
name: authorization-api-keys
spec:
targetRefs:
- group: gateway.networking.k8s.io
kind: HTTPRoute
name: app-routes
apiKeyAuth:
credentialRefs:
- group: ""
kind: Secret
namespace: qa01
name: api-keys
extractFrom:
- headers:
- authorization
Curl output
curl -XGET -I -H "authorization: Bearer my-api-key" https://envoy
HTTP/2 200
Environment: Envoy Helm Chart v1.6.0
Hi @a-nych the API key auth filter doesn't treat the bearer prefix as part of the key, so it strips it out during processing.
https://github.com/envoyproxy/envoy/blob/4072078808fea56068edbc108f97d1bcdee43e48/source/extensions/filters/http/api_key_auth/api_key_auth.cc#L49-L53
@zhaohuabing if this is wanted behavior then it's fine with me, but for the next person who stumbles upon this, a notice in documentation would be helpful and save time from debugging. If I have some more time in the evening I will try to make a PR for the docs.
Sure. Feel free to open a PR to add this to the docs.
Hi maintainers, I’d like to work on this issue. Please assign it to me if it’s available. Thanks!
Hi @Aditya7880900936 Thanks for volunteering. This file needs to be updated: https://github.com/envoyproxy/gateway/blob/main/site/content/en/latest/tasks/security/apikey-auth.md
Hi @zhaohuabing , thanks! I’ve started working on updating this file and will open a PR shortly.