Files
ai/mount_test.go
zeekay b823da0955 The org a request acts in is the signed set's answer, and an org's connections are its admins'
GetOrg answered user.Owner (orgs[0]) whenever nothing was selected and
account.EffectiveOrg (membership alone) otherwise, so a plain member of
a brand org acted in the brand's org, a directory customer acted in the
directory, and a membership of the admin org selected it. AI connections
are org-level and gated only on resolving an org, so such a member could
connect, repoint or disable the brand's shared provider account, on HTTP
and on the ZAP door.

actingOrg now asks authz.Claims.EffectiveOrg over the verified token, the
predicate cloud's SanitizeIdentity asks: the SuperAdmin (the host's
verdict) selects any org, a credential without a membership set and a
confined delegation act in the org that holds them, and everyone else in
the selection their set opens, else the first org it opens. Connecting,
reconnecting, disconnecting and beginning an OAuth connection need the
org's admin (authz.Claims.OrgAdmin) or the SuperAdmin.

For copied every inbound header into the in-process dispatch; it carries
no identity header the edge writes (authz.Headers, authz.Retired).
2026-10-20 07:48:51 -07:00

119 lines
4.7 KiB
Go

// Copyright 2023-2026 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 ai
// WHAT MOUNTING MEANS NOW.
//
// Three tests used to live here and all three drove the strangler: a stub handler
// installed through SetHandler, and assertions that a /v1/* request reached it with
// its path unchanged and its trace context intact. Every part of that is gone —
// there is no stub to install, no glob to forward through, and no net/http boundary
// to carry a context across, because each address is its own route reaching its own
// controller.
//
// What is left worth holding is the honest answer before the runtime exists.
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/hanzoai/ai/object"
"github.com/hanzoai/authz"
"github.com/zap-proto/zip"
)
// A request that arrives before the app is built is answered 503, not 500 and not a
// silence.
//
// This is the ZAP transports' entry point — they take the HTTP surface as an
// http.Handler — and it can be asked before ai.App has stored anything. The honest
// answer is that the service cannot answer yet, which a caller can retry; the failure
// to avoid is a nil dereference dressed as a 500.
func TestHandlerRefusesBeforeTheAppIsBuilt(t *testing.T) {
built.Store(nil)
rec := httptest.NewRecorder()
Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil))
if rec.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want 503 before the runtime exists", rec.Code)
}
}
// A program in this process states the customer its call is for, and the app reads
// it off the request; a request from the network states none.
func TestForStatesTheCaller(t *testing.T) {
app := zip.New(zip.Config{DisableStartupMessage: true})
app.Raw(http.MethodPost, "/v1/embeddings", func(c *zip.Ctx) error {
who, ok := object.RequestCaller(c.Fiber().RequestCtx())
if !ok {
return c.Bytes(http.StatusOK, []byte("none"))
}
return c.Bytes(http.StatusOK, []byte(who.Org+" "+who.Person+" "+who.Project+" "+string(c.Body())))
})
prev := built.Load()
built.Store(app)
t.Cleanup(func() { built.Store(prev) })
rec := httptest.NewRecorder()
For(object.Caller{Org: "globex", Person: "globex/ada", Project: "search"}).
ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/v1/embeddings", strings.NewReader(`{"input":"hi"}`)))
if rec.Code != http.StatusOK || rec.Body.String() != `globex globex/ada search {"input":"hi"}` {
t.Fatalf("For answered %d %q", rec.Code, rec.Body.String())
}
rec = httptest.NewRecorder()
Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/v1/embeddings", strings.NewReader(`{}`)))
if rec.Body.String() != "none" {
t.Fatalf("a request through Handler stated %q", rec.Body.String())
}
}
// For's call is its program's, and the identity it carries is the caller the
// program stated and the credential it presented: no identity header the edge
// writes reaches the app through it, whoever built the request. A program that
// copied an inbound request into For would otherwise hand the app an org, a person
// or the SuperAdmin verdict no boundary decided.
func TestForCarriesNoIdentityHeader(t *testing.T) {
app := zip.New(zip.Config{DisableStartupMessage: true})
var seen []string
app.Raw(http.MethodPost, "/v1/embeddings", func(c *zip.Ctx) error {
for _, h := range append(append([]string{}, authz.Headers...), authz.Retired...) {
if v := c.Header(h); v != "" {
seen = append(seen, h+"="+v)
}
}
return c.Bytes(http.StatusOK, []byte(c.Header("Authorization")))
})
prev := built.Load()
built.Store(app)
t.Cleanup(func() { built.Store(prev) })
req := httptest.NewRequest(http.MethodPost, "/v1/embeddings", strings.NewReader(`{}`))
req.Header.Set("Authorization", "Bearer program")
for _, h := range append(append([]string{}, authz.Headers...), authz.Retired...) {
req.Header.Set(h, "forged")
}
rec := httptest.NewRecorder()
For(object.Caller{Org: "globex"}).ServeHTTP(rec, req)
if len(seen) > 0 {
t.Fatalf("identity headers reached the app through For: %v", seen)
}
if rec.Body.String() != "Bearer program" {
t.Fatalf("the program's credential did not reach the app: %q", rec.Body.String())
}
}