mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Red flagged that hz_ widget keys skipped BOTH the balance gate (filter_balance.go)
AND usage metering (authResolveProvider returned authUser=nil, so reserveBudget +
recordUsage were skipped) → free, unbounded inference on the restricted model set.
Widget keys now bill the OWNER ORG that minted the key, exactly like sk- provider
keys: authResolveProvider sets authUser={Owner: WidgetKeyOwner(token), Type:
application} (empty Name → BillingSubjectForPrincipal collapses to the org ledger),
so the balance gate, budget reservation, and usage debit all engage. isWidget still
caps MaxTokens to widgetMaxTokens. An unattributable widget key (no WIDGET_KEY_OWNERS
mapping and no WIDGET_DEFAULT_OWNER) is refused — the same fail-secure invariant the
sk- path enforces (every call spending the shared upstream bills someone).
The router balance gate (filter_balance.go resolveBillingKey) resolves the same
owner so a zero-balance widget owner is gated at the router too.
DRY: the widget-key→owner resolver moved to object.WidgetKeyOwner (one source of
truth shared by the RAG tenant-isolation path, the controller billing path, and the
router gate); controllers.widgetKeyOwner is now a thin delegate.
Tests (green): object TestWidgetKeyOwner_Resolution (map/default/fail-secure);
existing controllers TestWidgetKeyOwnerCrossTenantIsolation still passes via delegate.
67 lines
2.4 KiB
Go
67 lines
2.4 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 (
|
|
"encoding/json"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// WidgetKeyOwner resolves a widget key (hz_*) to the IAM org it bills and whose
|
|
// data it may read. The owner is taken from the widget KEY itself — a per-tenant
|
|
// credential — via the WIDGET_KEY_OWNERS config (KMS first, env fallback) mapping
|
|
// key->owner. An unmapped key falls back to WIDGET_DEFAULT_OWNER (a single
|
|
// configured tenant), NEVER a header/Origin-derived org (that trust let widget
|
|
// callers read another tenant's RAG store). An empty return means the key is
|
|
// unattributable: callers MUST fail secure — refuse the request rather than spend
|
|
// the shared upstream for free, and read no tenant data.
|
|
//
|
|
// This is the ONE resolver shared by the controllers (RAG tenant isolation +
|
|
// per-org billing of widget inference) and the router balance gate, so a widget
|
|
// key means exactly the same org everywhere.
|
|
func WidgetKeyOwner(token string) string {
|
|
if o, ok := loadWidgetKeyOwners()[token]; ok && o != "" {
|
|
return o
|
|
}
|
|
return strings.TrimSpace(os.Getenv("WIDGET_DEFAULT_OWNER"))
|
|
}
|
|
|
|
// loadWidgetKeyOwners parses WIDGET_KEY_OWNERS (env first, then KMS) into a
|
|
// key->owner map. Accepts a JSON object or a comma-separated key=value list.
|
|
func loadWidgetKeyOwners() map[string]string {
|
|
raw := os.Getenv("WIDGET_KEY_OWNERS")
|
|
if raw == "" {
|
|
if v, err := GetKMSSecret("WIDGET_KEY_OWNERS"); err == nil {
|
|
raw = v
|
|
}
|
|
}
|
|
out := map[string]string{}
|
|
if raw == "" {
|
|
return out
|
|
}
|
|
if strings.HasPrefix(strings.TrimSpace(raw), "{") {
|
|
_ = json.Unmarshal([]byte(raw), &out)
|
|
return out
|
|
}
|
|
for _, part := range strings.Split(raw, ",") {
|
|
kv := strings.SplitN(strings.TrimSpace(part), "=", 2)
|
|
if len(kv) == 2 {
|
|
out[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
|
|
}
|
|
}
|
|
return out
|
|
}
|