Repository navigation
feat: upstream request headers, and the client API read from the path - #34
Merged
Merged
Conversation
Upstream credentials follow the enterprise gateway's header model:
- `key` is one API key, sent in the header the protocol uses
(`x-api-key`, `Authorization: Bearer`, `x-goog-api-key`)
- `headers` is an ordered map of any other headers to send, or the whole
credential for relays that authenticate with their own header. Values
may use `${ENV}`, `{{client}}` (the calling gateway key's name) and
`{{access_token}}` (requires `oauth`). Configured headers replace the
ones the client sent under the same name
- `oauth` moves out of `key` into its own block. OAuth credentials for
Claude subscriptions and the Gemini CLI's Google login are rejected
The control API mirrors this: `ProviderView` carries a masked `key`,
`auth_header`, masked `headers` and `oauth`; `ProviderInput` takes
`SecretChange` / `HeaderInput` / `OAuthChange`, where a missing header
value keeps the saved one. Provider preview accepts the chosen protocol
so the UI can say which header the key goes in.
The client API is now read from the request path instead of where the
gateway key was put. Fixes found along the way:
- Claude Code adopted with `ANTHROPIC_AUTH_TOKEN` sends a Bearer key and
was treated as an OpenAI client: Anthropic models were refused by model
admission and errors came back in OpenAI shape
- `POST /v1beta/models/{model}:generateContent` returned 405
- the gateway key in `?key=` was forwarded to the upstream URL
- `x-thinkwatch-*` request headers were forwarded upstream
`GET /v1/models` has no path to go by, so an `anthropic-version` header or
a key in `x-api-key` marks an Anthropic client there.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Thanks for the PR. One thing before review: this targets
No need to close and reopen: click Edit next to the PR title and change the base branch to See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Two problems surfaced while planning account-login upstreams:
X-Relay-Token), or needsanthropic-version/ an organization header pinned, could not be connected. The enterprise gateway already models upstream credentials as a header table.ANTHROPIC_AUTH_TOKENsendsAuthorization: Bearer, so it was treated as an OpenAI client. Anthropic models were then refused by model admission, and errors came back in the wrong shape.This is phase 1 of the upstream work (headers and dialect foundation). Any-to-any conversion and ChatGPT account login follow in later releases.
What changes
Credentials (
tw-config::credential)key: one API key, sent in the protocol's auth header (x-api-key/Authorization: Bearer/x-goog-api-key).headers: an orderedname: valuemap. Values may use${ENV},{{client}}(calling gateway key name) and{{access_token}}(requiresoauth). Configured headers replace same-name client headers.oauth: its own block instead of living insidekey. Tokens for Claude subscriptions (anthropic.com / claude.ai / claude.com) and the Gemini CLI login (cloudcode-pa) are rejected.Provider::check_credentialvalidates names, reserved headers, duplicates, placeholders, and key/oauth conflicts, and points at the offending row.Control API (
tw-api,tw-control)ProviderView: maskedkey(plus env var name when the whole value is${VAR}),auth_header,headerswith per-rowmasked,oauthendpoint and client id.ProviderInput:key: SecretChange(keep / none / set),headers: Vec<HeaderInput>(missing value keeps the saved one),oauth: OAuthChange.CredentialInputis removed.ProviderPreviewRequest.protocol(optional) lets the edit dialog show which header the key goes in for the chosen protocol.Gateway (
tw-gateway)ClientApi::of_pathdecides the dialect, the error shape and model admission (servable_by). Unknown paths still pass through.GET /v1/modelsandGET /v1/models/{id}have no body and no distinguishing path, so eitheranthropic-versionor a key inx-api-keymarks an Anthropic client.POST /v1beta/models/{model}:generateContent(and the other Gemini actions) now reaches the pipeline instead of returning 405.?key=before building the upstream URL, andx-thinkwatch-*headers are no longer forwarded.Breaking
key: { oauth: … }becomes a top-leveloauth:block on the provider. The project has no installed base to migrate, so there is no compatibility shim.Tests
tests/headers_and_paths.rs: Bearer-keyed Claude Code on an Anthropic upstream, model listing, GeminigenerateContentforwarding without the gateway key, configured headers replacing client headers, relay credentials in a custom header.tw-secret), control API keep/set/none and Claude token rejection, preview with a chosen protocol.an_error_comes_back_in_the_dialect_the_client_speaksnow asserts by path, including a Bearer key on/v1/messages.cargo fmt --check,cargo clippy --workspace --all-targets -D warnings,cargo test --workspaceand./scripts/smoke.shpass locally.🤖 Generated with Claude Code