mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
util.IsSuperAdmin compared the resolved user's owner to "admin", a second predicate beside the one the host boundary decides, and the two disagreed. An admin-org application (no orgs, owner claim admin), an admin-org person's sk- key, and an org spelled "admin " (the parse trimmed orgs[0]) were each SuperAdmin here and refused by the boundary. The host now states the verdict: object.SetSuper, which hanzo-inc/cloud fills with principal.Super, the X-User-IsAdmin its boundary deletes from every request and re-mints from authz.Claims.Sudo. Every HTTP seam that turns a token into a principal goes through object.Verify, which stamps it on iam.User.Sudo (never decoded); IsSuperAdmin and ScopeOwner read that and compare no org. util.AdminOrg is gone; the rows that are the platform's own are named by authz.AdminOrg. With no host there is no SuperAdmin, and a ZAP door is not the host: target deletes X-User-IsAdmin from a ZAP-built request, and zapPrincipal carries a verdict only when the HTTP bridge vouched for the request it was handed. A key is never the SuperAdmin, as at the host.
282 lines
10 KiB
Go
282 lines
10 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 controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/ai/internal/authtest"
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
"github.com/hanzoai/ai/object"
|
|
)
|
|
|
|
// ── adminProviderView projection ────────────────────────────────────────────
|
|
|
|
// TestToAdminProviderView_FieldsAndFlags verifies the projection maps State →
|
|
// enabled, IsDefault → primary, resolves keyPresent as a boolean, and carries the
|
|
// model count from the supplied map.
|
|
func TestToAdminProviderView_FieldsAndFlags(t *testing.T) {
|
|
// KMS disabled + no env key → a kms:// ref is fail-closed absent.
|
|
t.Setenv("HANZO_API_KEY", "")
|
|
|
|
p := &object.Provider{
|
|
Name: "do-ai",
|
|
DisplayName: "DigitalOcean AI (GenAI)",
|
|
Category: "Model",
|
|
Type: "DigitalOcean",
|
|
ProviderUrl: "https://inference.do-ai.run/v1",
|
|
ClientSecret: "kms://DO_AI_API_KEY_MISSING_FOR_TEST",
|
|
State: "Active",
|
|
IsDefault: true,
|
|
}
|
|
counts := map[string]int{"do-ai": 122}
|
|
|
|
v := toAdminProviderView(p, counts)
|
|
|
|
if v.Name != "do-ai" {
|
|
t.Errorf("Name = %q, want do-ai", v.Name)
|
|
}
|
|
if v.DisplayName != "DigitalOcean AI (GenAI)" {
|
|
t.Errorf("DisplayName = %q", v.DisplayName)
|
|
}
|
|
if v.Type != "DigitalOcean" {
|
|
t.Errorf("Type = %q, want DigitalOcean", v.Type)
|
|
}
|
|
if !v.Enabled {
|
|
t.Error("Enabled = false for State=Active, want true")
|
|
}
|
|
if !v.Primary {
|
|
t.Error("Primary = false for IsDefault=true, want true")
|
|
}
|
|
if v.KeyPresent {
|
|
t.Error("KeyPresent = true for an unresolved kms:// ref (KMS off), want false")
|
|
}
|
|
if v.ModelCount != 122 {
|
|
t.Errorf("ModelCount = %d, want 122", v.ModelCount)
|
|
}
|
|
if v.ProviderURL != "https://inference.do-ai.run/v1" {
|
|
t.Errorf("ProviderURL = %q", v.ProviderURL)
|
|
}
|
|
}
|
|
|
|
func TestToAdminProviderView_DisabledAndKeyFromEnv(t *testing.T) {
|
|
// Env-first resolution → keyPresent true; State != Active → enabled false;
|
|
// IsDefault false → primary false.
|
|
t.Setenv("FIREWORKS_API_KEY_TEST_PRESENT", "fw-key")
|
|
p := &object.Provider{
|
|
Name: "fireworks",
|
|
Category: "Model",
|
|
Type: "Fireworks",
|
|
ClientSecret: "kms://FIREWORKS_API_KEY_TEST_PRESENT",
|
|
State: "Disabled",
|
|
IsDefault: false,
|
|
}
|
|
v := toAdminProviderView(p, map[string]int{})
|
|
if v.Enabled {
|
|
t.Error("Enabled = true for State=Disabled, want false")
|
|
}
|
|
if v.Primary {
|
|
t.Error("Primary = true for IsDefault=false, want false")
|
|
}
|
|
if !v.KeyPresent {
|
|
t.Error("KeyPresent = false for a kms:// ref resolvable via env, want true")
|
|
}
|
|
if v.ModelCount != 0 {
|
|
t.Errorf("ModelCount = %d for a provider absent from the count map, want 0", v.ModelCount)
|
|
}
|
|
}
|
|
|
|
// TestAdminProviderView_NeverSerializesSecrets is the security regression guard:
|
|
// the JSON of the management view must NOT contain any secret-bearing key. If a
|
|
// future edit adds ClientSecret/ProviderKey to the struct, this fails.
|
|
func TestAdminProviderView_NeverSerializesSecrets(t *testing.T) {
|
|
t.Setenv("SUPER_SECRET_TEST_KEY", "sk-must-not-leak-123")
|
|
p := &object.Provider{
|
|
Name: "do-ai",
|
|
Category: "Model",
|
|
ClientSecret: "kms://SUPER_SECRET_TEST_KEY",
|
|
ProviderKey: "sk-provider-key-must-not-leak",
|
|
UserKey: "user-key-must-not-leak",
|
|
SignKey: "sign-key-must-not-leak",
|
|
State: "Active",
|
|
}
|
|
v := toAdminProviderView(p, map[string]int{"do-ai": 1})
|
|
// Sanity: env-first means keyPresent should be true here.
|
|
if !v.KeyPresent {
|
|
t.Fatal("precondition: expected keyPresent true via env resolution")
|
|
}
|
|
blob, err := json.Marshal(v)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
s := string(blob)
|
|
for _, forbidden := range []string{
|
|
"sk-must-not-leak-123", // resolved kms value
|
|
"kms://SUPER_SECRET_TEST_KEY", // the raw ref
|
|
"sk-provider-key-must-not-leak", // providerKey
|
|
"user-key-must-not-leak", // userKey
|
|
"sign-key-must-not-leak", // signKey
|
|
"clientSecret", "providerKey", // secret-bearing field names
|
|
"userKey", "signKey", "configText",
|
|
} {
|
|
if strings.Contains(s, forbidden) {
|
|
t.Errorf("admin provider view JSON leaked %q: %s", forbidden, s)
|
|
}
|
|
}
|
|
// Positive: the boolean signal IS present.
|
|
if !strings.Contains(s, `"keyPresent":true`) {
|
|
t.Errorf("expected keyPresent:true in JSON, got: %s", s)
|
|
}
|
|
}
|
|
|
|
// Note: the exactly-one-primary transition now lives in object.SetPrimaryModelProvider
|
|
// (one atomic, promote-first UPDATE pair — no in-memory diff to loop over). Its
|
|
// ordering + WHERE clauses are regression-tested in
|
|
// object/provider_default_atomic_test.go against the built SQL, without a DB.
|
|
|
|
// ── C-1 controller self-guard (RequireSuperAdmin) ──────────────────────────
|
|
//
|
|
// Belt-AND-suspenders: even if the authz filter is ever bypassed, the three admin
|
|
// controllers call c.RequireSuperAdmin() FIRST and refuse. These tests exercise
|
|
// that guard directly (session principal), asserting fail-closed 401/403 and pass
|
|
// for a super admin — the SAME policy as the filter (util.IsSuperAdmin).
|
|
|
|
// ctrlFakeSession (the minimal in-memory session.Store used to carry a test
|
|
// principal without a live session manager) is defined once in index_auth_test.go
|
|
// and shared across the package's controller tests — reused here to keep a single
|
|
// definition (it collided when this file, from the provider-admin PR, was rebased
|
|
// onto the branch that introduced index_auth_test.go).
|
|
|
|
// toggling is a request at the provider-admin toggle, carrying auth. The endpoint is
|
|
// named once here; who is calling is whatever credential the caller presents.
|
|
func toggling(auth string) *ApiController {
|
|
return presenting(visit(http.MethodPost, "/v1/admin/ai/providers/toggle"), auth)
|
|
}
|
|
|
|
func TestRequireSuperAdmin_NoPrincipal401(t *testing.T) {
|
|
c := toggling("")
|
|
if c.RequireSuperAdmin() {
|
|
t.Error("RequireSuperAdmin() = true for no principal, want false (deny)")
|
|
}
|
|
if answered(c) != http.StatusUnauthorized {
|
|
t.Errorf("no-principal status = %d, want 401", answered(c))
|
|
}
|
|
}
|
|
|
|
func TestRequireSuperAdmin_OrgAdminForbidden403(t *testing.T) {
|
|
orgAdmin := &iam.User{Owner: "maxpower", Name: "dave", IsAdmin: true}
|
|
c := toggling(authtest.Bearer(t, *orgAdmin))
|
|
if c.RequireSuperAdmin() {
|
|
t.Error("RequireSuperAdmin() = true for an ORG admin, want false (not a platform admin)")
|
|
}
|
|
if answered(c) != http.StatusForbidden {
|
|
t.Errorf("org-admin status = %d, want 403", answered(c))
|
|
}
|
|
}
|
|
|
|
func TestRequireSuperAdmin_SuperAdminPasses(t *testing.T) {
|
|
host(t)
|
|
superAdmin := &iam.User{Owner: "admin", Name: "admin", IsAdmin: true}
|
|
c := toggling(authtest.Bearer(t, *superAdmin))
|
|
c.Fiber().Request().Header.Set("X-User-IsAdmin", "true")
|
|
if !c.RequireSuperAdmin() {
|
|
t.Error("RequireSuperAdmin() = false for a super admin, want true (pass)")
|
|
}
|
|
// Pass path writes nothing (the handler continues).
|
|
if sent(c) != "" {
|
|
t.Errorf("super-admin pass wrote a body %q, want none", sent(c))
|
|
}
|
|
}
|
|
|
|
// ── stateForEnabled ─────────────────────────────────────────────────────────
|
|
|
|
func TestStateForEnabled(t *testing.T) {
|
|
if got := stateForEnabled(true); got != "Active" {
|
|
t.Errorf("stateForEnabled(true) = %q, want Active", got)
|
|
}
|
|
if got := stateForEnabled(false); got != "Disabled" {
|
|
t.Errorf("stateForEnabled(false) = %q, want Disabled", got)
|
|
}
|
|
}
|
|
|
|
// ── modelCountByProvider ────────────────────────────────────────────────────
|
|
|
|
// TestModelCountByProvider_MatchesStaticMap verifies the count helper agrees
|
|
// with an independent count over the static map — proving it is computed from the
|
|
// route table, not hardcoded. (In the unit env no YAML config is loaded, so the
|
|
// helper uses the static modelRoutes map.)
|
|
func TestModelCountByProvider_MatchesStaticMap(t *testing.T) {
|
|
if GetModelConfig() != nil {
|
|
t.Skip("YAML model config loaded; static-map parity check not applicable")
|
|
}
|
|
got := modelCountByProvider()
|
|
|
|
// Independent recount over the same source.
|
|
want := map[string]int{}
|
|
for _, route := range modelRoutes {
|
|
want[route.providerName]++
|
|
}
|
|
|
|
if len(got) != len(want) {
|
|
t.Errorf("provider count keys = %d, want %d (got=%v want=%v)", len(got), len(want), got, want)
|
|
}
|
|
for prov, n := range want {
|
|
if got[prov] != n {
|
|
t.Errorf("modelCountByProvider()[%q] = %d, want %d", prov, got[prov], n)
|
|
}
|
|
}
|
|
|
|
// Every count must equal the number of routes whose providerName == prov —
|
|
// asserted here for the known providers so drift in the table is visible.
|
|
for _, prov := range []string{"do-ai", "speech"} {
|
|
n := 0
|
|
for _, route := range modelRoutes {
|
|
if route.providerName == prov {
|
|
n++
|
|
}
|
|
}
|
|
if got[prov] != n {
|
|
t.Errorf("count for %q = %d, recount = %d", prov, got[prov], n)
|
|
}
|
|
if n == 0 {
|
|
t.Errorf("expected at least one route for provider %q", prov)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestModelCountByProvider_NoStaticZenRoute documents that ai holds no static zen
|
|
// route: the zen family is discovered from zen's /v1/models and routed via the
|
|
// dynamic zen passthrough, so modelCountByProvider (which counts the static table)
|
|
// reports no "zen" provider, while raw models still count under their provider.
|
|
func TestModelCountByProvider_NoStaticZenRoute(t *testing.T) {
|
|
if GetModelConfig() != nil {
|
|
t.Skip("YAML model config loaded; static-map behavior check not applicable")
|
|
}
|
|
counts := modelCountByProvider()
|
|
if _, ok := counts["zen"]; ok {
|
|
t.Errorf("no static route should target providerName=\"zen\" (zen is a dynamic passthrough); got count=%d", counts["zen"])
|
|
}
|
|
if counts["do-ai"] == 0 {
|
|
t.Error("expected do-ai to serve at least one static route")
|
|
}
|
|
// A raw do-ai model resolves to the do-ai provider.
|
|
if r := resolveModelRoute("bge-m3"); r == nil || r.providerName != "do-ai" {
|
|
t.Errorf("bge-m3 route = %+v, want providerName=do-ai", r)
|
|
}
|
|
}
|