Files
ai/controllers/gateway_bridge.go
zeekay e71dfbc4f3 SuperAdmin is the host boundary's verdict, never the org a token names
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.
2026-10-18 22:21:43 -07:00

103 lines
4.6 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"
"encoding/json"
"net/http"
"github.com/hanzoai/ai/object"
)
// RouterConfigBridge is the HTTP transport binding for the RESTful router-config
// nouns (/v1/ai/router/{policy,defaults,ledger,rewards,artifact-meta} and
// /v1/ai/org/settings[/list]). It dispatches IN-PROCESS through dispatchGateway —
// the SAME canonical ZAP gateway registry (zap_registry.go) that the MsgType 200
// handler serves over the gateway transport. The native ZAP
// handler is the ONE and ONLY implementation of these routes; this is purely the
// api.hanzo.ai HTTP binding, so there is NO controller twin to drift from and the
// split-brain the router refactor removed stays removed.
//
// Why a bridge and not a twin controller method: one handler serves both
// transports, so the HTTP route and the ZAP message read and write router settings
// through the same code and cannot come to disagree about them. Routing these nouns
// through the ZAP handler over one adapter keeps a single source of truth.
//
// Identity is the request's own Bearer credential (Authorization header), which
// the native handlers resolve exactly as the gateway does — every caller
// (console, chat, app) already sends it. The dispatched handler returns a ZAP
// message whose status is field 0 and body is field 4 (BuildCloudResponse /
// BuildGatewayResponse layout); both are relayed verbatim. The route is mapped
// "*" (any verb) because the native handler is method-aware: /v1/ai/router/policy
// splits GET (read) vs PUT (write), /v1/ai/org/settings GET/PUT/DELETE, and returns
// 405 for a verb it does not own.
func (c *ApiController) RouterConfigBridge() {
path := c.Path()
// nil router: this method IS a route inside routers.App, so it has nothing to
// fall back to — replaying an unclaimed path through the router would dispatch
// straight back here, forever. The registry is the only answer available at this
// seam, which is exactly what the !handled arm below reports.
// The org the request selects travels on the context exactly as the gateway
// transport carries it, so a handler scopes this request to the org the HTTP
// surface would (zapRPSScope), not to the credential's home org.
headers, _ := json.Marshal(map[string]string{"X-Org-Id": c.Header("X-Org-Id")})
// And the host's SuperAdmin verdict on THIS request, which the handler's
// principal carries (zapPrincipal). It is the bridge's to state because it holds
// the request the host boundary judged; a ZAP door holds no such request.
msg, handled, err := dispatchGateway(
vouch(context.WithValue(c.Context(), gatewayHeaders{}, headers), object.Super(c.Ctx)),
nil,
c.Method(),
path,
string(c.Fiber().Request().URI().QueryString()),
c.Header("Authorization"),
c.Body(),
)
switch {
case err != nil:
c.writeBridgeJSON(http.StatusInternalServerError, Response{Status: "error", Msg: err.Error()})
return
case !handled || msg == nil:
// dispatchGateway reporting no owner means the registry and this route table
// disagree — a wiring bug, not a client 404; surface it as 404 with the path.
c.writeBridgeJSON(http.StatusNotFound, Response{Status: "error", Msg: "not found: " + path})
return
}
root := msg.Root()
status := int(root.Uint32(object.CloudRespStatus))
if status == 0 {
status = http.StatusOK
}
c.writeBridgeRaw(status, root.Bytes(object.CloudRespBody))
}
// writeBridgeRaw relays a status + already-encoded body verbatim, matching the
// respondAnthropicError direct-write idiom (Content-Type, WriteHeader, Body,
// render off) so the native handler's exact JSON/JSONL bytes reach the client.
func (c *ApiController) writeBridgeRaw(status int, body []byte) {
c.SetHeader("Content-Type", "application/json; charset=utf-8")
_ = c.Bytes(status, body)
}
// writeBridgeJSON encodes and relays a bridge-local envelope (the only responses
// this adapter authors itself — dispatch wiring errors, never business logic).
func (c *ApiController) writeBridgeJSON(status int, resp Response) {
body, _ := json.Marshal(resp)
c.writeBridgeRaw(status, body)
}