Files
ai/controllers/org_switch_test.go
zeekayandHanzo Dev 9b96b749b8 auth: two key shapes, and the store decides which is which
hk- is gone. IAM resolves sk-, which authenticates, and pk-, which names an
org and authenticates nobody; every other spelling — hk- among them — answers
to no live credential and comes back key_unknown, which keyRefusal turns into
the one sentence a stranded holder can act on: mint a new one. Nothing here
special-cases the retired prefix, because inventing a case for it would put
the third family back.

That leaves the dispatch unable to tell two families apart by prefix: the key
this estate mints and an upstream vendor key wear the same three characters.
So it stops trying. The provider table is asked first — an exact lookup, so it
can never claim a key it does not hold — and anything it misses is put to IAM.
The order matters only for who answers when NEITHER owns the key, and IAM's
refusal names the cure where a provider miss can only say "invalid".

resolveBillingKey skipped every sk- before it reached the cache, back when
sk- meant "vendor key, billed elsewhere". Keeping that would now skip the main
credential and cost IAM keys their router-level gate, so only pk- skips. The
IAM miss is cached alongside the hit, or a vendor key would buy a fresh
round-trip on every request it makes.

TestGetUserByAccessKey_RetiredPrefixGetsTheActionableRefusal pins the outcome:
an hk- key gets "not recognized … Mint a new one", never the bare "the entity
does not exist" relay that sent people looking for a deleted organization, and
the refusal carries the prefix only.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-02 13:40:28 -07:00

251 lines
9.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 controllers
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/hanzoai/account"
"github.com/hanzoai/ai/internal/iam"
"github.com/hanzoai/ai/web"
)
// balanceProbe records the exact wire the balance gate puts on commerce: the
// billing SUBJECT (?user=) and the org NAMESPACE (X-Org-Id). Those two strings
// ARE the wallet — proving what the gate sends proves which wallet it read.
type balanceProbe struct {
mu sync.Mutex
subject string
org string
}
func (p *balanceProbe) seen() (string, string) {
p.mu.Lock()
defer p.mu.Unlock()
return p.subject, p.org
}
func newBalanceProbe(t *testing.T) *balanceProbe {
t.Helper()
p := &balanceProbe{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p.mu.Lock()
p.subject = r.URL.Query().Get("user")
p.org = r.Header.Get("X-Org-Id")
p.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"available": 100000}`)
}))
t.Cleanup(srv.Close)
t.Setenv("commerceEndpoint", srv.URL)
t.Setenv("commerceToken", "test-svc-token")
return p
}
// TestBalanceGateWalletIsTheLedger is the NO-OP proof for the read side.
//
// The gate used to key on user.Owner. It now keys on the ledger it is handed, so
// the cases that matter are the ones a non-switching user takes: an unresolved
// ledger ("") and a ledger that IS the home org must put the SAME subject and the
// SAME namespace on the wire as keying on user.Owner did. A switched ledger must
// move BOTH — a gate that moved only the namespace would read an org's balance
// under a person's key and find nothing.
func TestBalanceGateWalletIsTheLedger(t *testing.T) {
cases := []struct {
name string
user *iam.User
ledger string
wantSubject string
wantOrg string
}{
// hanzo IS account.SignupOrg: its members are strangers sharing one org, so
// each pays from a wallet of their own inside that one ledger.
{"personal wallet, ledger unresolved", &iam.User{Owner: "hanzo", Name: "alice"}, "", "hanzo/alice", "hanzo"},
{"personal wallet, ledger is home", &iam.User{Owner: "hanzo", Name: "alice"}, "hanzo", "hanzo/alice", "hanzo"},
{"pooled org, ledger unresolved", &iam.User{Owner: "acme", Name: "bob"}, "", "acme", "acme"},
{"pooled org, ledger is home", &iam.User{Owner: "acme", Name: "bob"}, "acme", "acme", "acme"},
{"machine principal, ledger is home", &iam.User{Owner: "acme", Type: "application"}, "acme", "acme", "acme"},
// The switch: BOTH halves of the address move to the selected org.
{"switched to a pooled org", &iam.User{Owner: "hanzo", Name: "alice"}, "acme", "acme", "acme"},
{"switched into the signup org", &iam.User{Owner: "acme", Name: "bob"}, "hanzo", "hanzo/bob", "hanzo"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
probe := newBalanceProbe(t)
if err := enforceBalanceGate(tc.user, tc.ledger, "glm-5.2"); err != nil {
t.Fatalf("gate refused a funded subject: %v", err)
}
subject, org := probe.seen()
if subject != tc.wantSubject || org != tc.wantOrg {
t.Fatalf("gate read wallet (subject=%q, org=%q), want (%q, %q)",
subject, org, tc.wantSubject, tc.wantOrg)
}
})
}
}
// TestBalanceGateUnresolvedLedgerIsHome states the safety invariant directly: a
// caller that passes no ledger gets the home org, never an empty subject. An
// empty subject would address no wallet at all, and every call on the ZAP
// transport — which has no headers and so can never carry a switch — relies on it.
func TestBalanceGateUnresolvedLedgerIsHome(t *testing.T) {
user := &iam.User{Owner: "hanzo", Name: "alice"}
probe := newBalanceProbe(t)
if err := enforceBalanceGate(user, "", "glm-5.2"); err != nil {
t.Fatalf("gate refused: %v", err)
}
blank, blankOrg := probe.seen()
probe = newBalanceProbe(t)
if err := enforceBalanceGate(user, user.Owner, "glm-5.2"); err != nil {
t.Fatalf("gate refused: %v", err)
}
home, homeOrg := probe.seen()
if blank != home || blankOrg != homeOrg {
t.Fatalf("unresolved ledger read (%q, %q) but home read (%q, %q) — they must be the same wallet",
blank, blankOrg, home, homeOrg)
}
if blank == "" || blankOrg == "" {
t.Fatalf("gate addressed an empty wallet (subject=%q, org=%q)", blank, blankOrg)
}
}
// orgController builds a controller carrying an Authorization header and an
// X-Org-Id, the two inputs billingOrg reads.
func orgController(auth, requested string) *ApiController {
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader("{}"))
if auth != "" {
req.Header.Set("Authorization", auth)
}
if requested != "" {
req.Header.Set("X-Org-Id", requested)
}
ctx := web.NewContext()
ctx.Reset(httptest.NewRecorder(), req)
c := &ApiController{}
c.Init(ctx, "ApiController", "X", nil)
return c
}
// TestBillingOrgHoldsUnlessTheClaimSaysOtherwise is the NO-OP proof for the
// resolver: every credential shape that reaches ai today resolves to its HOME
// org, whatever X-Org-Id says.
//
// Only a JWT carries the signed `orgs` claim, so only a JWT can switch. Every
// other credential — an sk- IAM key, an sk- provider key, an hz_ widget key, a
// cookie session, no credential at all — resolves nil membership, and nil admits
// nothing. That is what makes the raw header unspoofable here: a client that
// stamps X-Org-Id on an API-key request moves no money.
func TestBillingOrgHoldsUnlessTheClaimSaysOtherwise(t *testing.T) {
user := &iam.User{Owner: "hanzo", Name: "alice"}
cases := []struct {
name string
auth string
requested string
}{
{"no org header", "Bearer sk-abc", ""},
{"header names the home org", "Bearer sk-abc", "hanzo"},
{"sk- IAM key cannot switch", "Bearer sk-abc", "acme"},
{"sk- provider key cannot switch", "Bearer sk-abc", "acme"},
{"hz_ widget key cannot switch", "Bearer hz_abc", "acme"},
{"no credential at all cannot switch", "", "acme"},
{"a token that is not a JWT cannot switch", "Bearer not.a.jwt", "acme"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
c := orgController(tc.auth, tc.requested)
if got := c.billingOrg(user); got != user.Owner {
t.Fatalf("billingOrg = %q, want the home org %q — a credential with no signed membership set moved the wallet", got, user.Owner)
}
})
}
}
// TestBillingOrgNilUser pins the unattributable case: no user, no wallet. Callers
// refuse such a request rather than serve it free, and an empty string is what
// says so — never a defaulted org that would silently bill a real tenant.
func TestBillingOrgNilUser(t *testing.T) {
if got := orgController("Bearer sk-abc", "acme").billingOrg(nil); got != "" {
t.Fatalf("billingOrg(nil) = %q, want \"\"", got)
}
}
// TestGateAndDebitAddressOneWallet is the invariant the whole change exists to
// hold. The gate READS account.Payer(ledger, name); the debit WRITES
// account.PayerOf(record.Owner, record.User) with Owner=ledger and User the
// caller's IDENTITY ("<home>/<name>", deliberately NOT the ledger). Those two
// expressions must name the same (org, subject) or a funded org 402s while an
// unfunded one runs free — the exact failure the switcher would otherwise cause.
func TestGateAndDebitAddressOneWallet(t *testing.T) {
cases := []struct {
name string
home string
who string
ledger string
}{
{"no switch, personal wallet in the signup org", "hanzo", "alice", "hanzo"},
{"no switch, pooled org", "acme", "bob", "acme"},
{"switched from the signup org into a pooled org", "hanzo", "alice", "acme"},
{"switched from a pooled org into the signup org", "acme", "bob", "hanzo"},
{"switched between two pooled orgs", "acme", "bob", "zoo"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
user := &iam.User{Owner: tc.home, Name: tc.who}
probe := newBalanceProbe(t)
if err := enforceBalanceGate(user, tc.ledger, "glm-5.2"); err != nil {
t.Fatalf("gate refused: %v", err)
}
gateSubject, gateOrg := probe.seen()
// Exactly what recordUsage derives from the record ChatCompletions builds.
rec := &usageRecord{Owner: tc.ledger, User: user.Owner + "/" + user.Name}
debitSubject := account.PayerOf(rec.Owner, rec.User).Subject()
debitOrg := rec.Owner
if gateSubject != debitSubject || gateOrg != debitOrg {
t.Fatalf("gate read (%q, %q) but the debit lands on (%q, %q) — two wallets",
gateOrg, gateSubject, debitOrg, debitSubject)
}
})
}
}
// TestGetOrgScopeHoldsWithoutAClaim pins the SCOPE resolver's no-op half: without
// a signed membership set, a requested org never widens data scope either. GetOrg
// answers routing, pricing and usage reads; a spoofable one would be a
// cross-tenant read even where no money moves.
func TestGetOrgScopeHoldsWithoutAClaim(t *testing.T) {
// Non-member switch attempt with a non-JWT credential: nil membership set.
if got := account.EffectiveOrg("hanzo", nil, "acme"); got != "hanzo" {
t.Fatalf("EffectiveOrg admitted a switch with no claim: %q", got)
}
// And the controller path agrees for the same shape.
c := orgController("Bearer sk-abc", "acme")
if orgs := c.principalOrgs(); orgs != nil {
t.Fatalf("principalOrgs = %v for a non-JWT credential, want nil", orgs)
}
}