mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-21 02:05:26 +00:00
Drop the last old-stack coupling: ai no longer links the retired Casdoor SDK
module github.com/hanzoai/iam-v1 (which dragged beego/xorm/ldap/aliyun/aws into
ai's build for server code ai never ran). Replace it with an ai-internal
package internal/iam (package iam) that talks to Hanzo IAM (hanzo.id) over
standard OIDC — JWKS token verification against {endpoint}/v1/iam/.well-known/
jwks — and the IAM server's JSON REST API (/v1/iam/get-user, get-organization,
get-application, permissions, resources, send-email, oauth token exchange).
- internal/iam mirrors the IAM wire models. User/Permission/Resource keep the
FULL field set with json tags: object/redact.go reflects over every User
field by json tag as a fail-secure secret-redaction control, so a slim User
would silently weaken it and break redact_test. Read-only response types
(Organization/Application/Provider/Cert) are projections of the fields ai reads.
- All 79 importing files swap github.com/hanzoai/iam-v1 →
github.com/hanzoai/ai/internal/iam (package name unchanged, so call sites are
untouched). iam-v1 require dropped; go mod tidy.
- No Casdoor call was stubbed: SendEmail/UploadResource/GetResources/
DeleteResourceWithTag were already clean /v1/iam/ REST calls, reproduced
faithfully (zero behavior change), not faked.
Verify: CGO_ENABLED=0 go build ./... clean; go vet ./... clean; redaction
security tests pass (TestRedactUserSecrets/AllowlistFailSecure/Claims/NilSafe).
Co-authored-by: Hanzo Dev <[email protected]>
174 lines
6.5 KiB
Go
174 lines
6.5 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 (
|
|
"context"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
// GetCloudUsages
|
|
// @Title GetCloudUsages
|
|
// @Tag Usage API
|
|
// @Description Overview aggregation of the hanzo.cloud_usage ledger: totals +
|
|
// prior-period deltas, an evenly-spaced time series, spend-by-model (top-N +
|
|
// other), and the recent-activity feed, for a time range.
|
|
// @Param range query string false "24h | 7d | 30d | custom (default 24h)"
|
|
// @Param start query string false "custom range start (RFC3339 or unix seconds)"
|
|
// @Param end query string false "custom range end (RFC3339 or unix seconds)"
|
|
// @Param org query string false "super admin only: target org slug, 'all' for every org"
|
|
// @Param topModels query string false "spend-by-model top-N (default 6)"
|
|
// @Param activityType query string false "all | inference (default all)"
|
|
// @Param activityLimit query string false "recent-activity page size (default 20)"
|
|
// @Param activityOffset query string false "recent-activity page offset (default 0)"
|
|
// @Success 200 {object} object.CloudUsageOverview The Response object
|
|
// @router /get-cloud-usages [get]
|
|
//
|
|
// Auth is session-OR-Bearer (c.RequirePrincipal): the console sends a session
|
|
// cookie, while the token-bearing surfaces (app / chat / billing, which drive the
|
|
// unified UsagePanel) send an IAM Bearer. Both resolve to the SAME principal, and
|
|
// the scope below is decided from THAT principal alone.
|
|
//
|
|
// Scoping is the dual-use o11y read shared by tenant surfaces (own-org) and
|
|
// admin.hanzo.ai (god-view): a non-super-admin is pinned to their own org —
|
|
// request scope hints (X-Org-Id header, ?org=) are ignored, so a tenant can never
|
|
// read another org, no matter which auth it presents. A super admin targets one
|
|
// org via ?org=<slug> (or the X-Org-Id header console2 stamps), or omits it /
|
|
// passes ?org=all for the ALL-orgs view. The super-admin decision comes from the
|
|
// verified principal (util.IsSuperAdmin), never from a header or param.
|
|
func (c *ApiController) GetCloudUsages() {
|
|
user, ok := c.RequirePrincipal()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
// A financial read is never served to an anonymous guest. anonymousSignin binds
|
|
// a synthesized u-<hash> record to the deployment org (IAM_ORG) — so a persisted
|
|
// guest would otherwise read that org's AGGREGATE spend, and if IAM_ORG were ever
|
|
// "admin" it would inherit the all-orgs god-view. Reject every anonymous
|
|
// principal here; this endpoint requires a real signed-in identity.
|
|
if util.IsAnonymousUser(user) {
|
|
c.ResponseUnauthorized(c.T("auth:Please sign in first"))
|
|
return
|
|
}
|
|
|
|
org, allOrgs := c.resolveCloudUsageScope(user)
|
|
|
|
start, end, interval, err := object.ResolveCloudUsageWindow(
|
|
c.Input().Get("range"), c.Input().Get("start"), c.Input().Get("end"), time.Now(),
|
|
)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
params := object.CloudUsageParams{
|
|
RangeLabel: cloudUsageRangeLabel(c.Input().Get("range")),
|
|
Start: start,
|
|
End: end,
|
|
Interval: interval,
|
|
Org: org,
|
|
AllOrgs: allOrgs,
|
|
TopModels: cloudUsageIntParam(c.Input().Get("topModels"), 6, 1, 50),
|
|
ActivityType: strings.ToLower(strings.TrimSpace(c.Input().Get("activityType"))),
|
|
ActivityLimit: cloudUsageIntParam(c.Input().Get("activityLimit"), 20, 1, 200),
|
|
ActivityOffset: cloudUsageIntParam(c.Input().Get("activityOffset"), 0, 0, 1_000_000),
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
|
|
overview, err := object.GetCloudUsageOverview(ctx, params)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(overview)
|
|
}
|
|
|
|
// resolveCloudUsageScope decides whose data the caller may read. Non-global
|
|
// admins are pinned to their authenticated org (header/param hints ignored).
|
|
// Super admins may target one org (?org= / ?owner=, else the X-Org-Id header
|
|
// console2 sends) or get the all-orgs view (omitted, empty, "all", or "*").
|
|
func (c *ApiController) resolveCloudUsageScope(user *iam.User) (org string, allOrgs bool) {
|
|
// The all-orgs god-view and cross-org targeting expose EVERY customer's spend,
|
|
// so they require a SAME-BRAND super admin: a member of the reserved admin org
|
|
// (util.IsSuperAdmin) whose principal was minted by THIS deployment's own IAM
|
|
// (principalIsOwnBrand). A sibling white-label brand's super admin — a token the
|
|
// auth layer trusts for sign-in but that belongs to another brand — is pinned to
|
|
// its own org like any tenant. This binds all-customer financial disclosure to
|
|
// the deployment's OWN issuer, independent of which brand issuers the IAM SDK is
|
|
// later configured to VERIFY (today the cross-brand path is also shut by
|
|
// signature, but this closes it by POLICY regardless — see brand.go SDK-bump note).
|
|
if !util.IsSuperAdmin(user) || !c.principalIsOwnBrand() {
|
|
return user.Owner, false
|
|
}
|
|
|
|
target := strings.TrimSpace(c.Input().Get("org"))
|
|
if target == "" {
|
|
target = strings.TrimSpace(c.Input().Get("owner"))
|
|
}
|
|
if target == "" {
|
|
target = strings.TrimSpace(c.GetRequestTenantOrgID()) // X-Org-Id header
|
|
}
|
|
if target == "" || strings.EqualFold(target, "all") || target == "*" {
|
|
return "", true
|
|
}
|
|
return target, false
|
|
}
|
|
|
|
// cloudUsageRangeLabel normalizes the echoed range label.
|
|
func cloudUsageRangeLabel(raw string) string {
|
|
switch strings.ToLower(strings.TrimSpace(raw)) {
|
|
case "", "24h", "1d", "day", "today":
|
|
return "24h"
|
|
case "7d", "week":
|
|
return "7d"
|
|
case "30d", "month":
|
|
return "30d"
|
|
case "custom":
|
|
return "custom"
|
|
default:
|
|
return strings.ToLower(strings.TrimSpace(raw))
|
|
}
|
|
}
|
|
|
|
// cloudUsageIntParam parses a positive int query param with a default and clamp.
|
|
func cloudUsageIntParam(raw string, def, min, max int) int {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return def
|
|
}
|
|
v, err := strconv.Atoi(raw)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
if v < min {
|
|
return min
|
|
}
|
|
if v > max {
|
|
return max
|
|
}
|
|
return v
|
|
}
|