mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
The ad-hoc ZAP gateway registry (MsgType 200) was a SECOND router: a
hand-maintained map of ~180 path prefixes to handlers, matched longest-prefix on
the PATH ALONE.
That worked only while every route encoded its verb in its name. /v1/get-store
and /v1/update-store are different prefixes, so a path was a complete request
description. A RESTful surface breaks both halves of that assumption at once:
/v1/rag/stores/:owner/:name answers GET, PATCH and DELETE at one path, which
prefix matching cannot tell apart — and /v1/rag/stores/acme/thing prefix-matches
/v1/rag/stores, so a member request would silently reach the LIST handler with
the id segments discarded. A wrong handler, not an error.
The fix is not to teach the second router about verbs and path parameters — that
is two routers to keep in agreement forever. A miss now falls through to the
FIRST one. routers.App already resolves method + path + parameters and runs every
filter (auth, tenant, balance), which is exactly what InitForwardBridge does for
MsgTypeForward and what cloud's zapface dispatcher does for its own ZAP surface.
So the 181 stale registrations are deleted rather than rewritten. They named
routes that no longer exist, and everything they used to claim is now served by
the router that already knows how.
Worth recording why this was load-bearing: the gateway sends MsgType 200 (see
gateway/zap_backend.go), and its /v1/cloud/{path} endpoint carries the CRUD
surface. Without the fallback, every one of those calls would 404 — this is not
a cleanup, it is what makes the REST surface reachable over ZAP at all.
Two obsolete test concerns went with it: the registry-size assertions (the
gateway table is now correctly EMPTY for these groups), and the plural/singular
longest-prefix disambiguation checks — that hazard is gone with the compound
names that caused it.
The registry keeps serving what it is actually good at: the hot inference paths
that skip the HTTP machinery on purpose.
Co-authored-by: Hanzo Dev <[email protected]>
130 lines
5.1 KiB
Go
130 lines
5.1 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"
|
|
"testing"
|
|
|
|
"github.com/luxfi/zap"
|
|
|
|
"github.com/hanzoai/ai/object"
|
|
)
|
|
|
|
// decodeCloudApp pulls the (status, envelope) out of a native ZAP cloud response.
|
|
func decodeCloudApp(t *testing.T, msg *zap.Message, err error) (uint32, Response) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatalf("handler returned err: %v (a ZAP handler encodes policy denials in the message, never as err)", err)
|
|
}
|
|
status := msg.Root().Uint32(object.CloudRespStatus)
|
|
var env Response
|
|
if body := msg.Root().Bytes(object.CloudRespBody); len(body) > 0 {
|
|
if e := json.Unmarshal(body, &env); e != nil {
|
|
t.Fatalf("decode envelope: %v (body=%s)", e, string(body))
|
|
}
|
|
}
|
|
return status, env
|
|
}
|
|
|
|
// TestZapApplicationDeployRegistered proves the group exposes every native cloud
|
|
// method and gateway path from its own dispatch tables (the strangler wiring
|
|
// seam Integrate ranges over), and that a route's cloud + gateway entry point at
|
|
// the SAME handler.
|
|
func TestZapApplicationDeployRegistered(t *testing.T) {
|
|
cloud := []string{
|
|
"application.list", "application.get", "application.update",
|
|
"application.add", "application.delete", "application.deploy",
|
|
"application.undeploy", "k8s.status",
|
|
}
|
|
for _, m := range cloud {
|
|
if zapApplicationDeployCloud[m] == nil {
|
|
t.Errorf("cloud method %q not registered", m)
|
|
}
|
|
}
|
|
// The gateway table is deliberately EMPTY. Its entries were a second
|
|
// router keyed on path alone, which cannot express a RESTful surface
|
|
// (one path, several verbs, and id segments to capture). Those paths are
|
|
// served by the native router via the MsgType 200 fallback instead —
|
|
// see zap_gateway_fallback.go.
|
|
if len(zapApplicationDeployGateway) != 0 {
|
|
t.Errorf("gateway table has %d entries, want 0 — a second router is back", len(zapApplicationDeployGateway))
|
|
}
|
|
if len(zapApplicationDeployCloud) != len(cloud) {
|
|
t.Errorf("cloud table = %d, want %d", len(zapApplicationDeployCloud), len(cloud))
|
|
}
|
|
}
|
|
|
|
// TestZapApplicationDeployAuthRejection asserts the ONE identity seam refuses an
|
|
// unauthenticated caller on EVERY handler, before any object/DB call — an empty
|
|
// credential resolves to a nil principal, so each handler returns 401 with the
|
|
// {status:"error"} envelope the beego ResponseUnauthorized returns. The k8s.status
|
|
// super-admin gate also fails closed at 401 for no principal (403 is reserved for
|
|
// an authenticated non-super-admin).
|
|
func TestZapApplicationDeployAuthRejection(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
h zapApplicationDeployFn
|
|
body string
|
|
}{
|
|
{"application.list", zapGetApplicationsHandler, `{}`},
|
|
{"application.get", zapGetApplicationHandler, `{"id":"admin/x"}`},
|
|
{"application.update", zapUpdateApplicationHandler, `{"owner":"admin","name":"x"}`},
|
|
{"application.add", zapAddApplicationHandler, `{"owner":"admin","name":"x","template":"t"}`},
|
|
{"application.delete", zapDeleteApplicationHandler, `{"owner":"admin","name":"x"}`},
|
|
{"application.deploy", zapDeployApplicationHandler, `{"owner":"admin","name":"x"}`},
|
|
{"application.undeploy", zapUndeployApplicationHandler, `{"id":"admin/x"}`},
|
|
{"k8s.status", zapGetK8sStatusHandler, ``},
|
|
}
|
|
for _, tc := range cases {
|
|
msg, err := tc.h(context.Background(), "", []byte(tc.body))
|
|
st, env := decodeCloudApp(t, msg, err)
|
|
if st != 401 {
|
|
t.Errorf("%s empty auth: status = %d, want 401", tc.name, st)
|
|
}
|
|
if env.Status != "error" {
|
|
t.Errorf("%s empty auth: envelope status = %q, want error", tc.name, env.Status)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestZapAppOkEnvelope pins the happy-path response shape to ResponseOk EXACTLY:
|
|
// a single data arg populates Data (Data2 nil); a second arg populates Data2 (the
|
|
// GetApplications pagination count). This is the response-encoding contract every
|
|
// handler's success path returns — asserted without a DB.
|
|
func TestZapAppOkEnvelope(t *testing.T) {
|
|
apps := []*object.Application{{Owner: "admin", Name: "a"}, {Owner: "admin", Name: "b"}}
|
|
|
|
msg, err := zapAppOk(apps)
|
|
st, env := decodeCloudApp(t, msg, err)
|
|
if st != 200 || env.Status != "ok" {
|
|
t.Fatalf("single-arg: status=%d envelope=%q, want 200/ok", st, env.Status)
|
|
}
|
|
if env.Data2 != nil {
|
|
t.Errorf("single-arg: Data2 = %v, want nil", env.Data2)
|
|
}
|
|
|
|
msg, err = zapAppOk(apps, 7)
|
|
st, env = decodeCloudApp(t, msg, err)
|
|
if st != 200 || env.Status != "ok" {
|
|
t.Fatalf("two-arg: status=%d envelope=%q, want 200/ok", st, env.Status)
|
|
}
|
|
// JSON round-trips the count through interface{} as a float64.
|
|
if got, ok := env.Data2.(float64); !ok || got != 7 {
|
|
t.Errorf("two-arg: Data2 = %v (%T), want 7", env.Data2, env.Data2)
|
|
}
|
|
}
|