mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
326 lines
11 KiB
Go
326 lines
11 KiB
Go
// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"context"
|
|
|
|
"strings"
|
|
"testing"
|
|
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/luxfi/zap"
|
|
)
|
|
|
|
// The listings for these tables are scoped and the writes were not, so a row
|
|
// could be written outside what its own listing would ever show. A write now
|
|
// lands where that listing looks for it.
|
|
func TestARowIsWrittenWhereItsListingLooks(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
acme := people.signedIn(t, &iam.User{Owner: "acme", Name: "alice", IsAdmin: true})
|
|
|
|
// A body naming another organization writes into the caller's own.
|
|
c := as(visit("POST", "/v1/ai/add-template"), acme)
|
|
c.Fiber().Request().SetBody([]byte(`{"owner":"other","name":"t1","manifest":"kind: Pod"}`))
|
|
c.AddTemplate()
|
|
if strings.Contains(sent(c), "error") {
|
|
t.Fatalf("adding a template answered %s", sent(c))
|
|
}
|
|
if stored, err := object.GetTemplate("other/t1"); err != nil {
|
|
t.Fatal(err)
|
|
} else if stored != nil {
|
|
t.Error("it was written into the organization the body named")
|
|
}
|
|
stored, err := object.GetTemplate("acme/t1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stored == nil {
|
|
t.Fatal("it was not written into the caller's own organization")
|
|
}
|
|
|
|
// And a table that belongs to people is written under the person.
|
|
c = as(visit("POST", "/v1/ai/add-task"), acme)
|
|
c.Fiber().Request().SetBody([]byte(`{"owner":"someone-else","name":"k1"}`))
|
|
c.AddTask()
|
|
if task, err := object.GetTask("alice/k1"); err != nil {
|
|
t.Fatal(err)
|
|
} else if task == nil {
|
|
t.Error("a task was not written under the person who filed it")
|
|
}
|
|
}
|
|
|
|
// And a replace reaches only what the caller's own listing would show.
|
|
func TestAReplaceReachesOnlyYourOwn(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
if _, err := object.AddTemplate(&object.Template{
|
|
Owner: "other", Name: "theirs", Manifest: "kind: ConfigMap",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
acme := people.signedIn(t, &iam.User{Owner: "acme", Name: "alice", IsAdmin: true})
|
|
c := as(visit("POST", "/v1/ai/update-template?id=other/theirs"), acme)
|
|
c.Fiber().Request().SetBody([]byte(`{"owner":"other","name":"theirs","manifest":"kind: Pod"}`))
|
|
c.UpdateTemplate()
|
|
if !strings.Contains(sent(c), "does not exist") {
|
|
t.Errorf("reaching another organization's template answered %s", sent(c))
|
|
}
|
|
after, err := object.GetTemplate("other/theirs")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if after.Manifest != "kind: ConfigMap" {
|
|
t.Errorf("it was replaced with %q", after.Manifest)
|
|
}
|
|
}
|
|
|
|
// A table with no owner to write is refused rather than stored unscoped —
|
|
// silence is how a table nobody decided the ownership of would go unnoticed.
|
|
func TestATableWithNoOwnerIsRefused(t *testing.T) {
|
|
type ownerless struct{ Name string }
|
|
if err := ownedBy(&ownerless{}, "acme"); err == nil {
|
|
t.Error("a row with no owner was stored anyway")
|
|
}
|
|
type owned struct{ Owner, Name string }
|
|
row := &owned{Owner: "other"}
|
|
if err := ownedBy(row, "acme"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if row.Owner != "acme" {
|
|
t.Errorf("the owner came out %q", row.Owner)
|
|
}
|
|
if err := ownedBy(nil, "acme"); err == nil {
|
|
t.Error("nothing at all was stored anyway")
|
|
}
|
|
}
|
|
|
|
// The ZAP surface files a row the same way the HTTP one does. Two of the four
|
|
// tables that reach it are the scan surface, so a scan filed either way lands in
|
|
// the organization that filed it.
|
|
func TestTheZapSurfaceWritesWhereTheListingLooksToo(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
key := people.asUser(t, &iam.User{Owner: "acme", Name: "alice", IsAdmin: true})
|
|
|
|
if _, err := zapAddScanHandler(context.Background(), key,
|
|
[]byte(`{"owner":"other","name":"s1","provider":"nmap","state":"Pending"}`)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := zapAddAssetHandler(context.Background(), key,
|
|
[]byte(`{"owner":"other","name":"a1"}`)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, id := range []string{"other/s1", "other/a1"} {
|
|
var landed bool
|
|
if strings.HasSuffix(id, "s1") {
|
|
row, err := object.GetScan(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
landed = row != nil
|
|
} else {
|
|
row, err := object.GetAsset(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
landed = row != nil
|
|
}
|
|
if landed {
|
|
t.Errorf("%s was written into the organization the body named", id)
|
|
}
|
|
}
|
|
|
|
// And into the caller's own, which is where its listing looks.
|
|
if row, err := object.GetScan("acme/s1"); err != nil {
|
|
t.Fatal(err)
|
|
} else if row == nil {
|
|
t.Error("the scan was not written into the caller's own organization")
|
|
}
|
|
}
|
|
|
|
// The application and scan surfaces answer the same on both surfaces. An
|
|
// application is a manifest and a namespace to apply it in; a scan is a command
|
|
// line handed to a security tool. Neither reaches another organization's.
|
|
func TestBothSurfacesAgreeOnWhoseApplicationAndScan(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
key := people.asUser(t, &iam.User{Owner: "acme", Name: "alice", IsAdmin: true})
|
|
|
|
for _, seed := range []func() error{
|
|
func() error {
|
|
_, err := object.AddTemplate(&object.Template{Owner: "other", Name: "t", Manifest: "kind: ConfigMap"})
|
|
return err
|
|
},
|
|
func() error {
|
|
_, err := object.AddApplication(&object.Application{Owner: "other", Name: "theirs", Template: "t", DisplayName: "theirs"})
|
|
return err
|
|
},
|
|
func() error {
|
|
_, err := object.AddScan(&object.Scan{Owner: "other", Name: "theirs", Provider: "nmap"})
|
|
return err
|
|
},
|
|
} {
|
|
if err := seed(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Reaching another organization's application by id.
|
|
for _, run := range []func(context.Context, string, []byte) (*zap.Message, error){
|
|
zapUpdateApplicationHandler,
|
|
zapDeployApplicationHandler,
|
|
} {
|
|
if _, err := run(context.Background(), key,
|
|
[]byte(`{"id":"other/theirs","owner":"other","name":"theirs","template":"t","displayName":"taken over"}`)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
app, err := object.GetApplication("other/theirs")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if app.DisplayName != "theirs" {
|
|
t.Errorf("another organization's application was rewritten to %q", app.DisplayName)
|
|
}
|
|
// The namespace is derived either way, which is the other rule, not this one.
|
|
if app.Namespace != "hanzo-cloud-theirs" {
|
|
t.Errorf("its namespace became %q", app.Namespace)
|
|
}
|
|
|
|
// And its scan.
|
|
if _, err := zapUpdateScanHandler(context.Background(), key,
|
|
[]byte(`{"id":"other/theirs","scan":{"owner":"other","name":"theirs","command":"-oN /tmp/x %s"}}`)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
scan, err := object.GetScan("other/theirs")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if scan.Command != "" {
|
|
t.Errorf("another organization's scan command became %q", scan.Command)
|
|
}
|
|
}
|
|
|
|
// The remaining tables answer the same on both surfaces too. Each row is written
|
|
// where its own listing looks, whichever surface filed it.
|
|
func TestTheZapTwinsWriteWhereTheListingLooks(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
key := people.asUser(t, &iam.User{Owner: "acme", Name: "alice", IsAdmin: true})
|
|
|
|
for _, c := range []struct {
|
|
what string
|
|
run func(context.Context, string, []byte) (*zap.Message, error)
|
|
body string
|
|
there func() (bool, error)
|
|
}{
|
|
{"a form", zapAddFormHandler, `{"owner":"other","name":"f1"}`,
|
|
func() (bool, error) { r, err := object.GetForm("other/f1"); return r != nil, err }},
|
|
{"an article", zapAddArticleHandler, `{"owner":"other","name":"a1"}`,
|
|
func() (bool, error) { r, err := object.GetArticle("other/a1"); return r != nil, err }},
|
|
{"a graph", zapAddGraphHandler, `{"owner":"other","name":"g1"}`,
|
|
func() (bool, error) { r, err := object.GetGraph("other/g1"); return r != nil, err }},
|
|
{"a vector", zapAddVectorHandler, `{"owner":"other","name":"v1"}`,
|
|
func() (bool, error) { r, err := object.GetVector("other/v1"); return r != nil, err }},
|
|
} {
|
|
if _, err := c.run(context.Background(), key, []byte(c.body)); err != nil {
|
|
t.Fatalf("%s: %v", c.what, err)
|
|
}
|
|
landed, err := c.there()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if landed {
|
|
t.Errorf("%s was written into the organization the body named", c.what)
|
|
}
|
|
}
|
|
|
|
// And into the caller's own.
|
|
if r, err := object.GetForm("acme/f1"); err != nil {
|
|
t.Fatal(err)
|
|
} else if r == nil {
|
|
t.Error("the form was not written into the caller's own organization")
|
|
}
|
|
}
|
|
|
|
// A connection is a session on a machine, and a tunnel is opened by asking
|
|
// whether the CONNECTION is yours. So which node a connection reaches decides
|
|
// whose it is — taking that off the body lets one name your organization while
|
|
// pointing at another's machine.
|
|
func TestAConnectionBelongsToTheNodeItReaches(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
if _, err := object.AddNode(&object.Node{Owner: "other", Name: "theirs"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := object.AddNode(&object.Node{Owner: "acme", Name: "mine"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
alice := people.signedIn(t, &iam.User{Owner: "acme", Name: "alice", IsAdmin: true})
|
|
|
|
// Their machine, named as ours.
|
|
c := as(visit("POST", "/v1/ai/add-connection"), alice)
|
|
c.Fiber().Request().SetBody([]byte(`{"owner":"acme","name":"c1","node":"other/theirs","protocol":"ssh"}`))
|
|
c.AddConnection()
|
|
if got, err := object.GetConnection("acme/c1"); err != nil {
|
|
t.Fatal(err)
|
|
} else if got != nil {
|
|
t.Errorf("a connection was filed onto another organization's machine: %+v", got)
|
|
}
|
|
|
|
// Our own machine is reachable, and the connection belongs to it.
|
|
c = as(visit("POST", "/v1/ai/add-connection"), alice)
|
|
c.Fiber().Request().SetBody([]byte(`{"owner":"other","name":"c2","node":"acme/mine","protocol":"ssh"}`))
|
|
c.AddConnection()
|
|
got, err := object.GetConnection("acme/c2")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got == nil {
|
|
t.Fatalf("a connection to our own machine was refused: %s", sent(c))
|
|
}
|
|
if got.Owner != "acme" {
|
|
t.Errorf("it was filed under %q", got.Owner)
|
|
}
|
|
}
|
|
|
|
// An asset is filed and reached the same way its listing reads it back.
|
|
func TestAnAssetIsFiledWhereItsListingLooks(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
alice := people.signedIn(t, &iam.User{Owner: "acme", Name: "alice", IsAdmin: true})
|
|
|
|
c := as(visit("POST", "/v1/ai/add-asset"), alice)
|
|
c.Fiber().Request().SetBody([]byte(`{"owner":"other","name":"a1"}`))
|
|
c.AddAsset()
|
|
if got, err := object.GetAsset("other/a1"); err != nil {
|
|
t.Fatal(err)
|
|
} else if got != nil {
|
|
t.Error("it was filed into the organization the body named")
|
|
}
|
|
if got, err := object.GetAsset("acme/a1"); err != nil {
|
|
t.Fatal(err)
|
|
} else if got == nil {
|
|
t.Error("it was not filed into the caller's own organization")
|
|
}
|
|
|
|
// And another organization's is out of reach.
|
|
if _, err := object.AddAsset(&object.Asset{Owner: "other", Name: "theirs"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c = as(visit("POST", "/v1/ai/delete-asset"), alice)
|
|
c.Fiber().Request().SetBody([]byte(`{"owner":"other","name":"theirs"}`))
|
|
c.DeleteAsset()
|
|
if got, err := object.GetAsset("other/theirs"); err != nil {
|
|
t.Fatal(err)
|
|
} else if got == nil {
|
|
t.Error("another organization's asset was deleted")
|
|
}
|
|
}
|