Skip to content

feat(models): serve /v1/models filtered by caller key - #1

Open
remotego wants to merge 1 commit into
mainfrom
feat/models-api
Open

remotego wants to merge 1 commit into
mainfrom
feat/models-api

Conversation

@remotego

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • feature
  • documentation
  • CI / tooling

What this PR does and why

First switchboard module (roadmap 2.3): GET /v1/models and GET /v1/models/{id}, listing only the models the calling key may use.

The list is derived from the gateway's own config, never hand-maintained, so it cannot drift from what Higress enforces. Enforcement stays in Higress, unchanged: model-router copies the body model into x-higress-llm-model, per-model Ingresses match on that header, and key-auth allow lists gate each route. switchboard only lists.

  • Source: per-model Ingresses (higress.io/exact-match-header-x-higress-llm-model: <model>) plus the key-auth WasmPlugin. Both are watched through informers with read-only RBAC, so changes apply without a restart.

  • Derivation mirrors Higress source in two stages:

    1. the controller turning the CR into plugin JSON (convertIstioWasmPlugin: pluginConfig used verbatim, configDisable, defaultConfigDisable);
    2. the plugin parsing and matching it (route_rule_matcher.h, key_auth parsePluginConfig / checkAllowSet).

    Semantics worth knowing:

    • The first matching rule wins.
    • A rule without allow admits nobody. This differs from the earlier design note, which assumed everyone.
    • Rule-local consumers replace the global set and ignore allow.
    • A route no rule matches is only authenticated under global_auth: true.
    • Configs the plugin would reject list nothing.
    • Ingress class, route names (name in higress-system, otherwise ns/name) and host/wildcard matching follow Higress and Envoy.
    • Routes key-auth does not authenticate are listed for nobody and logged as a misconfiguration.
  • API: OpenAI shape. The consumer comes from X-Mse-Consumer; a request without it gets 401. 404 model_not_found is the same for missing and disallowed models, so callers can't probe which models exist.

  • Also included:

    • CI: gofmt, vet, tests, and the LICENSE/credential gate.
    • A per-platform image check.
    • A release workflow: an X.Y.Z tag pushes 4pdosc/switchboard.
    • A Dockerfile with mirrorable base images.
    • Design doc and deployment doc.

Deployment requirement the chart cannot own: key-auth must authenticate the /v1/models route. Otherwise X-Mse-Consumer can be forged. docs/deploy.md gives both ways to set it up and a curl check.

Known limits, logged as warnings:

  • key-auth rules scoped by service are treated as never matching.
  • Prefix/regex model-header routes are skipped.
  • Canary Ingresses are not handled.

Which issue(s) this PR fixes

Refs modelsphere/higress#5. This PR doesn't close it: the helm-charts chart and the kind test below are still to come.

How it was tested

$ make check            # gofmt, go vet, go test ./...
ok  github.com/modelsphere/switchboard/internal/api
ok  github.com/modelsphere/switchboard/internal/gateway
$ go test -race -count=1 ./...
ok  github.com/modelsphere/switchboard/internal/api
ok  github.com/modelsphere/switchboard/internal/gateway
  • Unit tests:
    • every row of the derivation table in the design doc;
    • the controller and plugin accept/reject cases;
    • rule order, domain vs. route rules, Ingress hosts and wildcards, ingress class precedence;
    • 401 / 404 / 503 and model ids containing /.
  • Watcher tests (fake clients): plugin auto-detect, flag, none, several and other namespace; live allow edit and Ingress add/delete without restart.
  • Mutation checks: each mirrored decision was reverted by hand and a test failed every time. The decisions covered: no-allow, open route, 401, pluginConfig, local-config size count, duplicate credential, in_query/in_header, defaultConfigDisable, route prefix, class precedence, wildcard, exact+prefix, and the update handler.
  • Image: docker build --build-arg VERSION=0.0.0-local works, and switchboard --version prints 0.0.0-local. The local build used a cached Debian runtime base because gcr.io is unreachable from the build host; CI uses distroless.

Not yet run: the kind test with a real Higress controller. It covers:

  • two keys seeing different lists, each matching what that key can actually call;
  • an allow change showing up without restart;
  • key-auth evaluating the per-model route that model-router re-selects;
  • Host reaching switchboard unchanged;
  • Envoy wildcard host matching.

Checks

  • Tests and linters for what changed pass locally (see the repository's README or CONTRIBUTING)
  • Docs updated where behaviour, a config key or a chart value moved
  • No internal hostnames, IPs, registries or credentials

Release note

New: switchboard serves GET /v1/models and /v1/models/{id} listing only the models the calling key may use, derived from Higress per-model routes and key-auth allow lists. Requires key-auth to authenticate the /v1/models route (see docs/deploy.md).

🤖 Generated with Claude Code

First switchboard module (roadmap 2.3). GET /v1/models and
GET /v1/models/{id} list only the models the calling key may use. The list
is derived from the gateway's own config, so it cannot drift from what
Higress enforces: per-model Ingresses matched on the model-router header
(higress.io/exact-match-header-x-higress-llm-model) plus the key-auth
WasmPlugin, watched through informers with read-only access. Enforcement
stays in Higress; switchboard only lists.

Derivation mirrors Higress source in two stages: the controller turning the
WasmPlugin CR into the plugin's JSON (convertIstioWasmPlugin: pluginConfig
verbatim, configDisable, defaultConfigDisable), and the plugin parsing and
matching it (route_rule_matcher.h, key_auth parsePluginConfig and
checkAllowSet). First matching rule wins; a rule without allow admits
nobody; rule-local consumers replace the global set and ignore allow;
unmatched routes are only authenticated under global_auth: true; configs the
plugin rejects list nothing. Ingress class, route names, host and wildcard
matching follow Higress/Envoy. Routes key-auth does not authenticate are
listed for nobody and logged; service-scoped rules and prefix/regex model
routes are skipped with a warning.

The API uses the OpenAI shape, reads the consumer from X-Mse-Consumer (401
without it), and answers 404 alike for missing and disallowed models.

Also: CI (gofmt, vet, tests, LICENSE/credential gate), per-platform image
check, tag-driven release to 4pdosc/switchboard, Dockerfile with mirrorable
base images, design doc and deployment doc (RBAC, Service, NetworkPolicy,
the /v1/models route, and the key-auth requirement on that route).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@remotego

Copy link
Copy Markdown
Contributor Author

@zoboth please review, thanks.

@zoboth zoboth left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blockers. The diff is tightly scoped to what the description promises: a read-only, informer-backed catalog, a key-auth/Ingress derivation that mirrors the stated Higress semantics, and an OpenAI-shaped /v1/models + /v1/models/{id} handler. CI (gofmt/vet/test), the image-arch check, the release workflow and the Dockerfile all look correct and appropriately scoped (pinned actions, minimal permissions:, no secrets exposed). go.mod deps are the expected k8s client-go set.

Walked internal/gateway/{route,keyauth,catalog,watch}.go and internal/api/models.go line by line against docs/models-api-design.md's derivation table, including the trickier ones (rule-local consumers under global_auth: false, pluginConfig verbatim override, ingress vs. domain category precedence, wildcard host suffix matching). Everything checked out, and the tests exercise the documented edge cases (401/404/503, / in model ids, live allow edits, several/zero key-auth plugins).

Non-blocking notes:

  • keyauth.go's category() gives a rule that sets both ingress and service its own bucket (catRouteAndService), which Allowed() then treats the same as a pure service rule: never matching. The design doc's stated priority rule ("a lower category on the same rule is ignored") is demonstrated for ingress+domain in TestKeyAuthMirror ("ingress and domain on one rule: route category, domain ignored"), but there's no equivalent case for ingress+service, so it's not clear from the diff alone whether folding it into "never matching" is intentional (a conservative stand-in for the already-declared "service rules are never treated as matching" limit) or an oversight relative to the general precedence rule. Worth a one-line comment or a test pinning the intent, since it's a real behavior choice rather than an obviously-safe default (it does fail closed either way — the model just gets listed for nobody — so it's not a correctness blocker).
  • Matches the PR's own disclosure: the kind test against a real Higress controller hasn't run yet, so the three gateway-side assumptions (header re-matching order, Host passthrough, Envoy wildcard semantics) are still unverified end-to-end. That's explicitly called out as not done rather than claimed, so it doesn't block this PR, just flagging that roadmap item 2.3 isn't closed by this alone (the PR says as much).

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants