mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
281 lines
8.8 KiB
Go
281 lines
8.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package projects_test
|
|
|
|
// Project tests driven through the REAL registered router (routes.Route installs the
|
|
// Guard, the typed CRUD, and the legacy verb aliases). Every case is a bind
|
|
// request the console ScopeSwitcher / Projects page sends via the /org/iam proxy.
|
|
// The assertions pin the parity contract (add-project → get-organization-projects
|
|
// → delete-project) and the security ones: any org member may LIST its org's
|
|
// projects (the switcher is shown to everyone), a WRITE needs org-admin authority,
|
|
// and no request parameter can list another tenant's projects.
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"encoding/pem"
|
|
"io"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
"github.com/hanzoai/orm"
|
|
ormdb "github.com/hanzoai/orm/db"
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/iam/internal/keyring"
|
|
"github.com/hanzoai/iam/internal/routes"
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
|
|
"github.com/hanzoai/iam/internal/testhttp"
|
|
)
|
|
|
|
const signingKid = "cert-hanzo"
|
|
|
|
type harness struct {
|
|
app *zip.App
|
|
key *rsa.PrivateKey
|
|
db orm.DB
|
|
}
|
|
|
|
func newHarness(t *testing.T) *harness {
|
|
t.Helper()
|
|
_ = schema.Kinds()
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
t.Fatalf("rsa: %v", err)
|
|
}
|
|
db, err := orm.OpenSQLite(&ormdb.SQLiteDBConfig{
|
|
Path: filepath.Join(t.TempDir(), "projects.db"),
|
|
Config: ormdb.SQLiteConfig{BusyTimeout: 5000, JournalMode: "WAL"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = db.Close() })
|
|
|
|
seedCert(t, db, "admin", signingKid, pemOf(t, key))
|
|
seedUser(t, db, "admin", "root", true) // SuperAdmin
|
|
seedUser(t, db, "hanzo", "boss", true) // org-admin of hanzo
|
|
seedUser(t, db, "hanzo", "alice", false) // regular user in hanzo
|
|
|
|
app := zip.New(zip.Config{AppName: "projects-test", DisableStartupMessage: true})
|
|
routes.Route(app, db)
|
|
if err := app.Build(); err != nil {
|
|
t.Fatalf("build: %v", err)
|
|
}
|
|
return &harness{app: app, key: key, db: db}
|
|
}
|
|
|
|
func (h *harness) token(t *testing.T, sub string) string {
|
|
t.Helper()
|
|
tok := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
|
|
"tokenType": "access-token", "iss": "https://hanzo.id", "aud": "hanzo-test",
|
|
"sub": sub,
|
|
"iat": time.Now().Add(-time.Minute).Unix(),
|
|
"exp": time.Now().Add(time.Hour).Unix(),
|
|
})
|
|
tok.Header["kid"] = signingKid
|
|
s, err := tok.SignedString(h.key)
|
|
if err != nil {
|
|
t.Fatalf("sign: %v", err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (h *harness) do(t *testing.T, method, path, bearer, body string) (int, map[string]any) {
|
|
t.Helper()
|
|
var r io.Reader
|
|
if body != "" {
|
|
r = strings.NewReader(body)
|
|
}
|
|
req := httptest.NewRequest(method, path, r)
|
|
req.Host = "hanzo.id"
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if bearer != "" {
|
|
req.Header.Set("Authorization", "Bearer "+bearer)
|
|
}
|
|
resp, err := testhttp.Do(h.app, req)
|
|
if err != nil {
|
|
t.Fatalf("%s %s: %v", method, path, err)
|
|
}
|
|
b, _ := io.ReadAll(resp.Body)
|
|
_ = resp.Body.Close()
|
|
var m map[string]any
|
|
_ = json.Unmarshal(b, &m)
|
|
return resp.StatusCode, m
|
|
}
|
|
|
|
// names reads the project names out of a get-organization-projects envelope's data.
|
|
func names(m map[string]any) []string {
|
|
rows, _ := m["projects"].([]any)
|
|
out := make([]string, 0, len(rows))
|
|
for _, r := range rows {
|
|
if o, ok := r.(map[string]any); ok {
|
|
if n, _ := o["name"].(string); n != "" {
|
|
out = append(out, n)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func has(ss []string, want string) bool {
|
|
for _, s := range ss {
|
|
if s == want {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// TestProjects_lifecycle: an org-admin creates a project, any member lists it, the
|
|
// admin deletes it.
|
|
func TestProjects_lifecycle(t *testing.T) {
|
|
h := newHarness(t)
|
|
boss := h.token(t, "hanzo/boss")
|
|
alice := h.token(t, "hanzo/alice")
|
|
|
|
// add-project (org-admin).
|
|
body := `{"owner":"hanzo","name":"alpha","displayName":"Alpha","organization":"hanzo","description":"first"}`
|
|
if st, m := h.do(t, "POST", "/v1/iam/projects", boss, body); st != 200 {
|
|
t.Fatalf("add-project: status=%d body=%v", st, m)
|
|
}
|
|
|
|
// get-organization-projects — a REGULAR member sees it (the switcher is for all).
|
|
st, m := h.do(t, "GET", "/v1/iam/projects?owner=hanzo", alice, "")
|
|
if st != 200 {
|
|
t.Fatalf("list: status=%d body=%v", st, m)
|
|
}
|
|
if !has(names(m), "alpha") {
|
|
t.Fatalf("list missing 'alpha': %v", names(m))
|
|
}
|
|
|
|
// delete-project (org-admin), keyed by owner/name.
|
|
if st, m := h.do(t, "DELETE", "/v1/iam/projects/hanzo/alpha", boss, ""); st != 200 {
|
|
t.Fatalf("delete-project: status=%d body=%v", st, m)
|
|
}
|
|
_, m = h.do(t, "GET", "/v1/iam/projects?owner=hanzo", alice, "")
|
|
if has(names(m), "alpha") {
|
|
t.Fatalf("'alpha' still listed after delete: %v", names(m))
|
|
}
|
|
}
|
|
|
|
// TestProjects_pathAddressed: the URL is the address. One project is created,
|
|
// read and removed at /v1/iam/projects/{owner}/{name}, and no request body names
|
|
// which one.
|
|
func TestProjects_pathAddressed(t *testing.T) {
|
|
h := newHarness(t)
|
|
boss := h.token(t, "hanzo/boss")
|
|
|
|
if st, m := h.do(t, "POST", "/v1/iam/projects", boss,
|
|
`{"owner":"hanzo","name":"gamma","organization":"hanzo","displayName":"Gamma"}`); st != 200 {
|
|
t.Fatalf("POST /v1/iam/projects: status=%d body=%v", st, m)
|
|
}
|
|
|
|
st, m := h.do(t, "GET", "/v1/iam/projects/hanzo/gamma", boss, "")
|
|
if st != 200 {
|
|
t.Fatalf("GET /v1/iam/projects/hanzo/gamma: status=%d body=%v", st, m)
|
|
}
|
|
if got, _ := m["displayName"].(string); got != "Gamma" {
|
|
t.Fatalf("read the wrong project: %v", m)
|
|
}
|
|
|
|
// The body carries no owner and no name: the path alone says which project.
|
|
if st, m := h.do(t, "PUT", "/v1/iam/projects/hanzo/gamma", boss,
|
|
`{"organization":"hanzo","displayName":"Gamma II"}`); st != 200 {
|
|
t.Fatalf("PUT /v1/iam/projects/hanzo/gamma: status=%d body=%v", st, m)
|
|
}
|
|
_, m = h.do(t, "GET", "/v1/iam/projects/hanzo/gamma", boss, "")
|
|
if got, _ := m["displayName"].(string); got != "Gamma II" {
|
|
t.Fatalf("update did not bind the path target: %v", m)
|
|
}
|
|
|
|
if st, m := h.do(t, "DELETE", "/v1/iam/projects/hanzo/gamma", boss, ""); st != 200 {
|
|
t.Fatalf("DELETE /v1/iam/projects/hanzo/gamma: status=%d body=%v", st, m)
|
|
}
|
|
if st, _ := h.do(t, "GET", "/v1/iam/projects/hanzo/gamma", boss, ""); st != 404 {
|
|
t.Fatalf("read after delete: status=%d, want 404", st)
|
|
}
|
|
}
|
|
|
|
// TestProjects_writeNeedsAdmin: a regular member can LIST but not create/delete.
|
|
func TestProjects_writeNeedsAdmin(t *testing.T) {
|
|
h := newHarness(t)
|
|
alice := h.token(t, "hanzo/alice") // regular
|
|
|
|
body := `{"owner":"hanzo","name":"beta","organization":"hanzo"}`
|
|
if st, _ := h.do(t, "POST", "/v1/iam/projects", alice, body); st != 403 {
|
|
t.Fatalf("regular user add-project: status=%d, want 403", st)
|
|
}
|
|
if st, _ := h.do(t, "DELETE", "/v1/iam/projects/hanzo/beta", alice, ""); st != 403 {
|
|
t.Fatalf("regular user delete-project: status=%d, want 403", st)
|
|
}
|
|
// But listing is allowed (200) — the switcher is shown to every user.
|
|
if st, m := h.do(t, "GET", "/v1/iam/projects?owner=hanzo", alice, ""); st != 200 {
|
|
t.Fatalf("regular user list: status=%d body=%v", st, m)
|
|
}
|
|
}
|
|
|
|
// TestProjects_crossTenantScoping: a non-super cannot list another tenant's
|
|
// projects — principal.Scope pins the read to the caller's own org regardless of the
|
|
// ?organization= it asks for.
|
|
func TestProjects_crossTenantScoping(t *testing.T) {
|
|
h := newHarness(t)
|
|
super := h.token(t, "admin/root")
|
|
alice := h.token(t, "hanzo/alice")
|
|
|
|
// super seeds a project under a DIFFERENT tenant, orgb.
|
|
if st, m := h.do(t, "POST", "/v1/iam/projects", super,
|
|
`{"owner":"orgb","name":"secret","organization":"orgb"}`); st != 200 {
|
|
t.Fatalf("super add orgb project: status=%d body=%v", st, m)
|
|
}
|
|
|
|
// alice (hanzo) asks for orgb's projects → gets HER org's scope, never orgb's.
|
|
_, m := h.do(t, "GET", "/v1/iam/projects?owner=orgb", alice, "")
|
|
if has(names(m), "secret") {
|
|
t.Fatalf("VULN: hanzo user listed orgb's project 'secret': %v", names(m))
|
|
}
|
|
}
|
|
|
|
// ---- seed helpers ----
|
|
|
|
func seedCert(t *testing.T, db orm.DB, owner, name, privPEM string) {
|
|
t.Helper()
|
|
c := orm.New[schema.Cert](db)
|
|
c.Owner, c.Name = owner, name
|
|
keyring.Set(name, privPEM) // the deployment supplies key material; the row never carries it
|
|
c.SetId(owner + "/" + name)
|
|
if err := c.CreateCtx(context.Background()); err != nil {
|
|
t.Fatalf("seed cert: %v", err)
|
|
}
|
|
}
|
|
|
|
func seedUser(t *testing.T, db orm.DB, owner, name string, admin bool) {
|
|
t.Helper()
|
|
u := orm.New[schema.User](db)
|
|
u.Owner, u.Name = owner, name
|
|
u.IsAdmin = admin
|
|
u.PasswordHash = "$argon2id$SENTINEL"
|
|
u.PasswordType = "argon2id"
|
|
u.SetId(owner + "/" + name)
|
|
if err := u.CreateCtx(context.Background()); err != nil {
|
|
t.Fatalf("seed user: %v", err)
|
|
}
|
|
}
|
|
|
|
func pemOf(t *testing.T, k *rsa.PrivateKey) string {
|
|
t.Helper()
|
|
return string(pem.EncodeToMemory(&pem.Block{
|
|
Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k),
|
|
}))
|
|
}
|