mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
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.
201 lines
6.2 KiB
Go
201 lines
6.2 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.
|
|
|
|
// ZAP inter-service transport for cloud operations.
|
|
//
|
|
// Listens on CLOUD_ZAP_PORT (default 9320) and serves the deploy, undeploy
|
|
// and status opcodes over ZAP rather than HTTP.
|
|
//
|
|
// Message type 110 (cloud ops):
|
|
// Request: method(0:Text) + auth(8:Text) + body(16:Bytes)
|
|
// Response: status(0:Uint32) + body(4:Bytes) + error(12:Text)
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"github.com/hanzoai/ai/log"
|
|
"github.com/luxfi/zap"
|
|
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
|
|
"github.com/hanzoai/ai/cluster"
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
// MsgTypeCloudOps is the ZAP message type for inter-service cloud operations.
|
|
const MsgTypeCloudOps uint16 = 110
|
|
|
|
var interserviceNode *zap.Node
|
|
|
|
// InitInterserviceZap starts a dedicated ZAP node for inter-service operations.
|
|
// Separate from the main inference ZAP node (port 9999).
|
|
func InitInterserviceZap() {
|
|
port := 9320
|
|
if p := os.Getenv("CLOUD_ZAP_PORT"); p != "" {
|
|
fmt.Sscanf(p, "%d", &port)
|
|
}
|
|
|
|
node := zap.NewNode(zap.NodeConfig{
|
|
NodeID: "cloud-ops",
|
|
Port: port,
|
|
NoDiscovery: true,
|
|
Logger: slog.Default(),
|
|
})
|
|
|
|
if err := node.Start(); err != nil {
|
|
log.Error("ZAP ops: failed to start on :%d: %v", port, err)
|
|
return
|
|
}
|
|
|
|
node.Handle(MsgTypeCloudOps, handleCloudOps)
|
|
interserviceNode = node
|
|
log.Info("ZAP ops: listening on :%d (msg_type=%d)", port, MsgTypeCloudOps)
|
|
}
|
|
|
|
// StopInterserviceZap gracefully shuts down the inter-service ZAP node.
|
|
func StopInterserviceZap() {
|
|
if interserviceNode != nil {
|
|
interserviceNode.Stop()
|
|
interserviceNode = nil
|
|
log.Info("ZAP ops: stopped")
|
|
}
|
|
}
|
|
|
|
func handleCloudOps(ctx context.Context, from string, msg *zap.Message) (*zap.Message, error) {
|
|
root := msg.Root()
|
|
method := root.Text(object.CloudReqMethod)
|
|
auth := root.Text(object.CloudReqAuth)
|
|
body := root.Bytes(object.CloudReqBody)
|
|
if confinedIn(auth) {
|
|
return buildOpsResponse(401, nil, zapConfinedRefusal)
|
|
}
|
|
|
|
// The message carries an auth field because these opcodes act on a cluster.
|
|
// Reading it is what makes the field mean anything.
|
|
user := zapPrincipal(ctx, auth)
|
|
if user == nil {
|
|
return buildOpsResponse(401, nil, "auth:Please sign in first")
|
|
}
|
|
|
|
switch method {
|
|
case "deploy":
|
|
return opsDeployHandler(user, body)
|
|
case "undeploy":
|
|
return opsUndeployHandler(user, body)
|
|
case "status":
|
|
return opsStatusHandler()
|
|
default:
|
|
return buildOpsResponse(404, nil, "unknown op: "+method)
|
|
}
|
|
}
|
|
|
|
func buildOpsResponse(status uint32, body []byte, errMsg string) (*zap.Message, error) {
|
|
return object.BuildCloudResponse(status, body, errMsg)
|
|
}
|
|
|
|
// ── deploy ──────────────────────────────────────────────────────────────
|
|
|
|
func opsDeployHandler(user *iam.User, body []byte) (*zap.Message, error) {
|
|
var params struct {
|
|
ID string `json:"id"`
|
|
}
|
|
if err := json.Unmarshal(body, ¶ms); err != nil {
|
|
return buildOpsResponse(400, nil, "invalid body: "+err.Error())
|
|
}
|
|
if params.ID == "" {
|
|
return buildOpsResponse(400, nil, "id required")
|
|
}
|
|
|
|
app, err := object.GetApplication(params.ID)
|
|
if err != nil {
|
|
return buildOpsResponse(500, nil, err.Error())
|
|
}
|
|
if app == nil || !reaches(user, app.Owner) {
|
|
return buildOpsResponse(404, nil, "application not found: "+params.ID)
|
|
}
|
|
|
|
ok, err := cluster.DeploySync(app, "en")
|
|
if err != nil {
|
|
return buildOpsResponse(500, nil, "deploy failed: "+err.Error())
|
|
}
|
|
if !ok {
|
|
return buildOpsResponse(500, nil, "deploy returned false")
|
|
}
|
|
|
|
updated, err := object.GetApplication(params.ID)
|
|
if err != nil {
|
|
return buildOpsResponse(500, nil, err.Error())
|
|
}
|
|
|
|
data, _ := json.Marshal(updated)
|
|
return buildOpsResponse(200, data, "")
|
|
}
|
|
|
|
// ── undeploy ────────────────────────────────────────────────────────────
|
|
|
|
func opsUndeployHandler(user *iam.User, body []byte) (*zap.Message, error) {
|
|
var params struct {
|
|
Owner string `json:"owner"`
|
|
Name string `json:"name"`
|
|
}
|
|
if err := json.Unmarshal(body, ¶ms); err != nil {
|
|
return buildOpsResponse(400, nil, "invalid body: "+err.Error())
|
|
}
|
|
if params.Owner == "" || params.Name == "" {
|
|
return buildOpsResponse(400, nil, "owner and name required")
|
|
}
|
|
|
|
// The namespace comes from the application's own row, the way the HTTP twin
|
|
// takes it. A namespace named by the caller is a namespace of their choosing,
|
|
// and undeploy deletes the whole of one.
|
|
id := util.GetIdFromOwnerAndName(params.Owner, params.Name)
|
|
app, err := object.GetApplication(id)
|
|
if err != nil {
|
|
return buildOpsResponse(500, nil, err.Error())
|
|
}
|
|
if app == nil || !reaches(user, app.Owner) {
|
|
return buildOpsResponse(404, nil, "application not found: "+id)
|
|
}
|
|
|
|
ok, err := cluster.UndeploySync(params.Owner, params.Name, app.Namespace, "en")
|
|
if err != nil {
|
|
return buildOpsResponse(500, nil, "undeploy failed: "+err.Error())
|
|
}
|
|
if !ok {
|
|
return buildOpsResponse(500, nil, "undeploy returned false")
|
|
}
|
|
|
|
data, _ := json.Marshal(map[string]string{"status": "undeployed", "owner": params.Owner, "name": params.Name})
|
|
return buildOpsResponse(200, data, "")
|
|
}
|
|
|
|
// ── status ──────────────────────────────────────────────────────────────
|
|
|
|
func opsStatusHandler() (*zap.Message, error) {
|
|
status, err := cluster.Status("en")
|
|
if err != nil {
|
|
return buildOpsResponse(500, nil, err.Error())
|
|
}
|
|
|
|
data, _ := json.Marshal(map[string]any{"k8s": status})
|
|
return buildOpsResponse(200, data, "")
|
|
}
|