mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
* feat(providers): admin management API + State-gated routing + DO-first defaults
New global-admin-gated provider-management surface, wired to the SAME
object.Provider store (one source of truth — the generic CRUD store is not
forked). Closes the gap where toggling a provider's State had no effect on
routing, and makes DO-first the default with OpenRouter manageable in the
same table.
Endpoints (routers/router.go + globalAdminEndpoints gate in authz_filter.go):
- GET /v1/admin/providers list Model providers as a clean view:
{name,displayName,type,enabled,primary,keyPresent,modelCount,providerUrl}
keyPresent is a boolean ONLY — never the key or any fragment of it.
- POST /v1/admin/providers/toggle {name,enabled} → set State Active/Disabled.
- POST /v1/admin/providers/primary {name} → IsDefault on target, cleared on all
other Model providers (exactly one primary).
- GET /v1/provider-flags PUBLIC, secret-free enabled-provider NAMES
feed for the pricing catalog sync (replaces standalone ENABLE_OPENROUTER):
{"enabledProviders":["do-ai","zen"]}.
Routing State gate (the toggle now actually takes effect):
- object.ModelProviderUsable: ONE predicate — a Model provider with State !=
"Active" is unavailable. Consulted by GetModelProviderByName and
GetModelProviderByNameForOrg (the completion/embeddings/image/BYOK chokepoints)
and getModelProviderFromName (store/knowledge path), so every caller sees a
disabled provider as "not configured" uniformly.
- Fallbacks honored: failover callProvider surfaces a nil (disabled/missing)
provider as "... is unavailable ..."; isRetryableError treats "unavailable" as
retryable so the failover loop advances to an Active fallback.
- object.InvalidateProviderNameCache: admin toggles evict the 60s hot-path cache
so changes take effect on the next request, not after the TTL.
DO-first seed defaults + non-clobber fix (object/init.go):
- do-ai = Active + IsDefault (primary router); fireworks/openai-direct = Disabled;
zen stays Active (branded first-party over do-ai); openrouter seeded Disabled.
- CRITICAL: State/IsDefault are set ONLY on initial CREATE and NEVER re-clobbered
on an existing record, so an admin toggle persists across restarts. Type/SubType/
ProviderUrl/ClientSecret continue to self-heal from the seed.
Secret safety:
- object.ProviderKeyPresent resolves on a COPY (env-first, mirroring the hot path),
returns a bool, never mutates the caller's record, fail-closed when KMS is off.
- adminProviderView carries no secret fields; regression test asserts the JSON
never contains clientSecret/providerKey/userKey/signKey or any resolved value.
Tests (all DB-free, passing): ModelProviderUsable, ProviderKeyPresent (present/
absent/env/kms-off/no-mutation), InvalidateProviderNameCache, adminProviderView
projection + secret-leak guard, modelCountByProvider static-map parity,
providersToRepointPrimary exactly-one, filterEnabledModelNames, provider-flags
JSON contract + OpenRouter default-off, isRetryableError "unavailable".
* fix(providers): close authz path-normalization bypass + atomic set-primary (red review)
Addresses the red review of the provider-admin backend (builds on 344a55e0).
C-1 [CRITICAL] AuthzFilter path-normalization bypass — permissionFilter keyed
globalAdminEndpoints on the RAW TrimPrefix path while Beego path.Cleans before
dispatch, so slash/dot variants of the gated multi-segment routes
(POST /v1/admin/providers/toggle/, /v1//admin/providers, /v1/./admin/providers,
/v1/admin/../admin/providers, …) bypassed the gate entirely and fell through to
the fully-open default → UNAUTHENTICATED disable of the primary provider =
platform-wide chat/embeddings/image DoS. Fixed with defense in depth:
1. normalizedControllerName() path.Cleans the request path BEFORE the map
lookup so the gate keys on the SAME canonical name Beego dispatches to —
closing the entire "//", "/./", "/../", trailing-slash variant set for
EVERY gated endpoint (admin/providers*, the get-*/*-provider CRUD, topology
reads), not just the 3 admin routes.
2. Controller self-guard RequireGlobalAdmin() at the top of GetAdminProviders,
ToggleAdminProvider, SetPrimaryAdminProvider — reuses the existing
c.principalUser() (session or VERIFIED Bearer JWT) + util.IsGlobalAdmin,
fail-closed 401/403. Even if the filter is ever bypassed the controller
refuses. NOT the preview-mode-relaxed org-level RequireAdmin.
H-1 [HIGH] set-primary non-atomic + default ignores State —
- GetDefaultModelProvider now filters state="Active" (with is_default +
category), so a disabled/ambiguous primary is never handed to the
store/KB/RAG default-model path (store.go returns it with no usability check).
- SetPrimaryAdminProvider replaced its per-record UpdateProvider loop (mid-loop
failure could leave 0 or 2 primaries) with object.SetPrimaryModelProvider: two
set-wide UPDATEs, PROMOTE-the-target-first then DEMOTE-the-rest, so there is
never a zero-primary window. Params-bound, no interpolation.
LOW-2 [frontend red] reject set-primary on a disabled provider —
SetPrimaryAdminProvider 4xx's if !object.ModelProviderUsable(provider) (the ONE
enabled/allowed-to-route predicate; getAdminModelProvider already confirmed Model).
M-2 [MEDIUM] router-level bypass regression test — routers/authz_filter_test.go
adds the normalization-variant matrix (permissionFilter, every variant → 401
unauth / 403 org-admin, global-admin passes) AND a real Beego router+filter+memory
session httptest round-trip (mirrors red's harness). Proven to FAIL against the
pre-fix filter (variants returned 200 / reached the controller) and pass after.
object/provider_default_atomic_test.go asserts the promote-before-demote ordering
+ WHERE scoping + the state="Active" default query via built SQL (no DB).
Removes now-dead providersToRepointPrimary (the atomic UPDATE supersedes it).
Contract unchanged: GET /v1/admin/providers, /toggle, /primary, /provider-flags
keep their shapes; provider-flags stays public. Kept intact: secret non-leak,
cache eviction, ModelProviderUsable, ProviderKeyPresent. go build/vet clean.
* fix(authz): self-guard naked provider/model-route/cluster handlers + preview lever
Defense-in-depth follow-up to the path-normalization fix this branch already
carries (normalizedControllerName in permissionFilter). The provider / model-route
CRUD and cluster-introspection handlers relied SOLELY on the routers.permissionFilter
gate; add c.RequireGlobalAdmin() — the helper introduced with provider-admin, which
resolves the session OR a verified Bearer JWT via principalUser — as the FIRST
statement of each so a future filter desync can never reach them unauthenticated or
as a mere org admin. Mirrors the provider_admin handlers' self-guard exactly.
Gated (15): UpdateProvider, DeleteProvider, RefreshMcpTools, GetModelRoutes,
GetModelRoute, AddModelRoute, UpdateModelRoute, DeleteModelRoute, ReloadModelConfig,
GetNodes, GetPods, GetContainers, GetImages, GetMachines, GetK8sStatus.
Also (LOW): account.go GetAccount now reads conf.DisablePreviewMode() (env-first
DISABLE_PREVIEW_MODE lever) instead of beego.AppConfig.Bool directly, so the lever
governs it like the authz filter and RequireAdmin; drop the now-unused beego import.
Tests: new TestNakedHandlersRequireGlobalAdmin drives every gated handler and asserts
401 (anonymous) / 403 (org admin) on the deny path (guard returns before any DB call,
so no datastore needed). Resolve a rebase-induced duplicate ctrlFakeSession (F1's
index_auth_test.go + provider-admin's provider_admin_test.go both defined it) by
reusing the single package-level definition.
go build ./...=0, go vet clean, gofmt clean; go test ./routers ./controllers green.
117 lines
5.0 KiB
Go
117 lines
5.0 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package object
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/dbx"
|
|
)
|
|
|
|
// buildDB returns a dbx.DB that builds SQL for a driver WITHOUT opening a
|
|
// connection (sql.Open / dbx.NewFromDB never dial), so we can assert the exact
|
|
// UPDATE statements SetPrimaryModelProvider would run without a live database.
|
|
func buildDB(t *testing.T) *dbx.DB {
|
|
t.Helper()
|
|
db, err := dbx.Open("mysql", "")
|
|
if err != nil {
|
|
t.Fatalf("dbx.Open: %v", err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
// TestPrimaryRepointSteps_PromoteBeforeDemote is the H-1 regression: the set-primary
|
|
// transition must PROMOTE the new primary (is_default=true) BEFORE it DEMOTEs the
|
|
// others (is_default=false), so there is never a window with zero primaries. The
|
|
// prior per-record loop could clear the old primary before setting the new one (0
|
|
// primaries) on a mid-loop failure. We assert the ORDER and the exact WHERE/SET of
|
|
// each step against the built SQL.
|
|
func TestPrimaryRepointSteps_PromoteBeforeDemote(t *testing.T) {
|
|
db := buildDB(t)
|
|
steps := primaryRepointSteps("fireworks")
|
|
|
|
if len(steps) != 2 {
|
|
t.Fatalf("expected exactly 2 UPDATE steps (promote, demote), got %d", len(steps))
|
|
}
|
|
|
|
// Step 0 = PROMOTE: is_default=true, scoped to the named admin Model provider.
|
|
promoteSQL := db.Update("provider", dbx.Params{"is_default": steps[0].setDefault}, steps[0].where).SQL()
|
|
if steps[0].setDefault != true {
|
|
t.Errorf("step 0 must set is_default=true (promote), got %v", steps[0].setDefault)
|
|
}
|
|
if !strings.Contains(promoteSQL, "`is_default`=") {
|
|
t.Errorf("promote SQL missing is_default assignment: %s", promoteSQL)
|
|
}
|
|
// Must target exactly the named row within the admin Model scope, positively.
|
|
assertContainsAll(t, "promote", promoteSQL,
|
|
"`owner`=", "`category`=", "name = ")
|
|
if strings.Contains(promoteSQL, "name <>") {
|
|
t.Errorf("promote must target name = (the target), not name <>: %s", promoteSQL)
|
|
}
|
|
|
|
// Step 1 = DEMOTE: is_default=false, scoped to the OTHER admin Model providers.
|
|
demoteSQL := db.Update("provider", dbx.Params{"is_default": steps[1].setDefault}, steps[1].where).SQL()
|
|
if steps[1].setDefault != false {
|
|
t.Errorf("step 1 must set is_default=false (demote), got %v", steps[1].setDefault)
|
|
}
|
|
assertContainsAll(t, "demote", demoteSQL,
|
|
"`owner`=", "`category`=", "name <> ")
|
|
if strings.Contains(demoteSQL, "name = ") {
|
|
t.Errorf("demote must target name <> (the others), not name =: %s", demoteSQL)
|
|
}
|
|
}
|
|
|
|
// TestPrimaryRepointSteps_ScopedToAdminModel proves both UPDATEs are confined to the
|
|
// admin-owned Model-category records — a Storage/Embedding record or another org's
|
|
// provider is never touched by a set-primary. (owner and category are bound values;
|
|
// we assert the columns and, via the built statement, that both predicates are ANDed
|
|
// into every step alongside the name predicate.)
|
|
func TestPrimaryRepointSteps_ScopedToAdminModel(t *testing.T) {
|
|
db := buildDB(t)
|
|
for i, step := range primaryRepointSteps("do-ai") {
|
|
sql := db.Update("provider", dbx.Params{"is_default": step.setDefault}, step.where).SQL()
|
|
// The scope columns must both be present in the WHERE of each step.
|
|
assertContainsAll(t, "step-scope", sql, "WHERE", "`owner`=", "`category`=")
|
|
// And a name predicate must also be present (so it is a subset of the scope,
|
|
// never the whole table).
|
|
if !strings.Contains(sql, "name = ") && !strings.Contains(sql, "name <> ") {
|
|
t.Errorf("step %d WHERE lacks a name predicate (would touch all admin Model rows): %s", i, sql)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestGetDefaultModelProvider_QueryRequiresActiveState locks in the H-1 State
|
|
// filter: the default-model query must constrain state="Active" (in addition to
|
|
// is_default + category) so a DISABLED primary is never returned to the store/KB/RAG
|
|
// default-model path (store.go returns this record with no further usability check).
|
|
// We assert the built SELECT WHERE carries the state predicate.
|
|
func TestGetDefaultModelProvider_QueryRequiresActiveState(t *testing.T) {
|
|
db := buildDB(t)
|
|
// The exact where used by GetDefaultModelProvider.
|
|
where := dbx.HashExp{"is_default": true, "category": "Model", "state": "Active"}
|
|
sql := db.Select().From("provider").Where(where).Build().SQL()
|
|
assertContainsAll(t, "default-model", sql, "`state`=", "`is_default`=", "`category`=")
|
|
}
|
|
|
|
func assertContainsAll(t *testing.T, label, sql string, subs ...string) {
|
|
t.Helper()
|
|
for _, sub := range subs {
|
|
if !strings.Contains(sql, sub) {
|
|
t.Errorf("[%s] built SQL missing %q\n SQL: %s", label, sub, sql)
|
|
}
|
|
}
|
|
}
|