feat(mcp): introduce injectionPolicy for MCP API key handling in per-backend security policy - #2663
Conversation
…hether the injected API key replaces existing values in the target header Signed-off-by: Aishwarya <aishraimule@gmail.com>
Signed-off-by: Aishwarya <aishraimule@gmail.com>
✅ Deploy Preview for theagentrouter ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| // | ||
| // +kubebuilder:default=true | ||
| // +optional | ||
| Overwrite *bool `json:"overwrite,omitempty"` |
There was a problem hiding this comment.
If the default is to have overwrite to be true. The field does not make sense as it was the behaviour before the client override option.
We should rather have a field which describes whether the client set keys are allowed to be passed?
There was a problem hiding this comment.
Yes, booleans with a default True are odd and confusing.
I have the following reasons to add a field here:
- There's only one BackendSecurity Policy header, forward headers is an array. The overwrite field on forwardHeaders would only be valid for 1 header at most.
- The client headers are populated earlier in the filter chain. Adding a
overwritethere doesn't make sense from a code perspective, as there's nothing to be overwritten at that time. But that's an implementation detail, and users need not know about the order.
Instead of overwrite, I can use an enum injectionPolicy with values IfNotPresent and Always as the default. WDYT?
Signed-off-by: Aishwarya <aishraimule@gmail.com>
…ay into inject-fwd-cred-header
|
Nice fix @aish1331 — traces directly to the line we flagged on #2661 ( Two things worth a look:
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
I think this probably belongs more to the AutZ policy than the credential injection policy. If someone wants to enforce that, the right way would be to not configure credential injection and have an authz policy that requires the header. |
This was intentional:
++ to @nacx's point. Moreover, reusing the CredentialOverride is not a great idea for the following reasons:
Having said that, these two behaviours (credential injection in MCP and credential override in LLM) are similar. |
|
/retest |
…route - how headers are forwarded as-is and if you want to forward Authorization header you have to add the prefix (Bearer, Basic, etc) when you send the header Signed-off-by: Aishwarya <aishraimule@gmail.com>
Description
Problem
When an MCP backend has both a security policy API key and forwardHeaders that land on the same header (typically
Authorizationbut can be any custom header key), the gateway always overwrites the forwarded value with the configured credential.That prevents a common setup: a default least-privilege service account on the backend, overridden by a caller-supplied personal access token when the client sends one. forwardHeaders is how clients inject that token today (usually via a dedicated header renamed with backendHeader), but the security policy wins and the backend only ever sees the service account.
Fix
This PR adds an overwrite field on
backendRef.securityPolicy.apiKeyso a default backend credential can coexist with a caller-supplied token forwarded onto the same header.This update adds an
InjectionPolicyenum to theMCPBackendAPIKeystruct (backendRef.securityPolicy.apiKey), controlling whether the injected API key replaces existing values in the target header.The default behavior is to overwrite the already present header (InjectionPolicy = Always), but when set to
IfNotPresent, the API key is only injected if the header is absent, allowing for caller-supplied tokens to be preserved.This change includes validation rules to ensure that
InjectionPolicycannot beIfNotPresentwhen using query parameters, as query-parameter injection always rewrites the backend URL.Additionally, tests have been added to verify the behavior of the new
InjectionPolicyfield in various scenarios, ensuring correct handling of inline and secret reference API keys.Changes:
MCPBackendAPIKeystruct to includeInjectionPolicyfield.InjectionPolicyin the CRD.InjectionPolicysetting.Related Issues/PRs (if applicable)
Fixes #2661
Related Issues:
#1966
Special notes for reviewers (if applicable)