List in /v1/models only the models the key may use
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f9493aadb9
commit
ba5158d463
3 files changed
+40
-1
No files matched your search
@@ -183,6 +183,8 @@ APIs change between minors without a deprecation period.
|
||||
handler**, and **thread ids are bounded** to 128 letters, digits and
|
||||
`- _ . :`. A thread is readable by every key of its project that knows
|
||||
the id; the docs now say to use unguessable ids.
|
||||
- **`/v1/models` lists only what the key may use**, rather than every
|
||||
model the gateway serves to anyone.
|
||||
|
||||
### Everything that can be changed on a running cluster, from the console
|
||||
|
||||
|
||||
@@ -796,7 +796,10 @@ func (r *Router) servedModels() []string {
|
||||
}
|
||||
|
||||
// handleModels lists the models that at least one endpoint currently serves.
|
||||
func (r *Router) handleModels(w http.ResponseWriter, _ *http.Request) {
|
||||
//
|
||||
// Only those the caller's key may use: listing the rest tells a key holder
|
||||
// what the gateway serves to others, and invites requests that are refused.
|
||||
func (r *Router) handleModels(w http.ResponseWriter, req *http.Request) {
|
||||
seen := map[string]struct{}{}
|
||||
for _, s := range r.reg.Snapshots("") {
|
||||
for _, m := range s.Models {
|
||||
@@ -810,8 +813,12 @@ func (r *Router) handleModels(w http.ResponseWriter, _ *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
principal, authed := auth.FromContext(req.Context())
|
||||
names := make([]string, 0, len(seen))
|
||||
for n := range seen {
|
||||
if authed && !r.permits(principal, n) {
|
||||
continue
|
||||
}
|
||||
names = append(names, n)
|
||||
}
|
||||
slices.Sort(names)
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -718,3 +719,32 @@ func TestAFallbackTheKeyMayNotUseIsNotServed(t *testing.T) {
|
||||
t.Fatalf("a key allowed both was not given the fallback: %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelsEndpointListsOnlyWhatTheKeyMayUse(t *testing.T) {
|
||||
e := newFakeEngine(t, "real")
|
||||
cfg := baseConfig(config.Endpoint{ID: "a", URL: e.srv.URL, Models: []string{"real", "other"}})
|
||||
cfg.Models = []catalog.Model{catModel("real", []string{"real-alias"}), catModel("other", nil)}
|
||||
rt := newTestRouter(t, cfg)
|
||||
|
||||
p := auth.Principal{Limits: store.Effective{AllowedModels: []string{"real"}}}
|
||||
req := httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
||||
req = req.WithContext(auth.WithPrincipal(req.Context(), p))
|
||||
rec := httptest.NewRecorder()
|
||||
rt.handleModels(rec, req)
|
||||
|
||||
var out struct {
|
||||
Data []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ids []string
|
||||
for _, d := range out.Data {
|
||||
ids = append(ids, d.ID)
|
||||
}
|
||||
if !slices.Equal(ids, []string{"real", "real-alias"}) {
|
||||
t.Errorf("listed %v; the key may use real and its alias only", ids)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user