mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
A key resolves only while its holder may sign in: a suspended or deleted account's keys answer key_holder_disabled, its org's default key included. An org the platform runs (authz.IsPlatformOrg) is a tenant only of its owners and admins, its own machines and a SuperAdmin, and a brand's directory of no one: store.Acts is that rule asked of one row, and the key resolver (key_not_tenant), the user-key mint (403, no key filed that acts for no one) and the delegation read it. principal.Scope and ScopeRead require MemberOf, the org listing filters by it, and the registry gives a person a token only as an admin of a candidate org. A delegated token (as(), naming its actor in act) founds no org and writes no account. A suspended account resolves nobody on its session or its bearer, and founds nothing. An act key names its holder and mints only while that holder runs the key's org. A machine signs in as an application or with its key: it is refused a password grant, a form sign-in and an invitation. The groups claim carries the orgs a person is a tenant of, so a relying party that maps groups onto access sees no directory and no brand org held as a plain member. A name a removed org still holds draws the answer a standing one does. authz v1.10.47.
169 lines
6.5 KiB
Go
169 lines
6.5 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package routes_test
|
|
|
|
// No secret key speaks for a SuperAdmin, or for anyone else in the admin org. A
|
|
// SuperAdmin signs in and holds short-lived tokens; the rule is asked of whoever
|
|
// writes the key, however the holder is spelled, on both write surfaces, and
|
|
// again at resolution. A brand org's person holding an admin-org membership is
|
|
// not a SuperAdmin, and holds keys like anyone.
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/orm"
|
|
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// keysNaming reports every key row that would speak for a SuperAdmin.
|
|
func keysNaming(t *testing.T, h *harness) []string {
|
|
t.Helper()
|
|
rows, err := orm.TypedQuery[schema.Key](h.db).GetAll(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("read keys: %v", err)
|
|
}
|
|
var out []string
|
|
for _, k := range rows {
|
|
if store.SuperAdminKey(k) {
|
|
out = append(out, k.Owner+"/"+k.Name+" -> "+k.User)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestNoSecretKeyIsWrittenForASuperAdmin(t *testing.T) {
|
|
eachOrgAdmin(t, func(t *testing.T, h *harness, boss caller) {
|
|
t.Setenv("IAM_KEY_MINT_ALLOWED_APPS", "hanzo-visor")
|
|
root := h.person(t, "admin/root")
|
|
for _, user := range []string{"admin/z", "hanzo/z", "hanzo/alice"} {
|
|
if _, err := store.EnsureMembership(context.Background(), h.db, user, "orgb", store.RoleMember); err != nil {
|
|
t.Fatalf("seed %s in orgb: %v", user, err)
|
|
}
|
|
}
|
|
|
|
refused := []struct {
|
|
who string
|
|
c caller
|
|
body string
|
|
}{
|
|
{"hanzo/boss", boss, `{"owner":"hanzo","name":"k1","user":"admin/z"}`},
|
|
{"hanzo/boss", boss, `{"owner":"hanzo","name":"k2","user":"admin/Z"}`},
|
|
{"hanzo/boss", boss, `{"owner":"hanzo","name":"k3","user":"alice"}`},
|
|
{"admin/root", root, `{"owner":"hanzo","name":"k5","user":"admin/z"}`},
|
|
{"admin/root", root, `{"owner":"admin","name":"k6","user":"root"}`},
|
|
{"admin/root", root, `{"owner":"admin","name":"k6","user":"nobody-yet"}`},
|
|
{"hanzo-visor", visor, `{"owner":"orgb","name":"k7","user":"admin/z"}`},
|
|
{"hanzo-visor", visor, `{"owner":"orgb","name":"k8","user":"admin/Z"}`},
|
|
}
|
|
for _, r := range refused {
|
|
if status, body := h.send(t, r.c, "POST", "/v1/iam/keys", r.body); status != 403 {
|
|
t.Errorf("%s wrote %s: %d %s", r.who, r.body, status, body)
|
|
}
|
|
}
|
|
|
|
// Everyone else is written for whoever a SuperAdmin or a minting application
|
|
// names (an org's admin names only themselves), and a publishable key, which
|
|
// names an org and no principal, is written whoever minted it.
|
|
for _, r := range []struct {
|
|
who string
|
|
c caller
|
|
body string
|
|
}{
|
|
{"admin/root", root, `{"owner":"hanzo","name":"alice-key","user":"alice"}`},
|
|
{"hanzo-visor", visor, `{"owner":"orgb","name":"alice-member","user":"hanzo/alice"}`},
|
|
{"admin/root", root, `{"owner":"hanzo","name":"z-key","user":"z"}`},
|
|
{"hanzo-visor", visor, `{"owner":"orgb","name":"z-member","user":"hanzo/z"}`},
|
|
{"admin/root", root, `{"owner":"admin","name":"beacon","user":"z","scope":"publish"}`},
|
|
} {
|
|
if status, body := h.send(t, r.c, "POST", "/v1/iam/keys", r.body); status != 200 {
|
|
t.Fatalf("%s could not write %s: %d %s", r.who, r.body, status, body)
|
|
}
|
|
}
|
|
|
|
// A key cannot be pointed at the operator after the fact either, and an update
|
|
// is asked by the class the key was minted with, not one the body claims.
|
|
for _, body := range []string{
|
|
`{"owner":"hanzo","name":"alice-key","user":"admin/z"}`,
|
|
`{"owner":"hanzo","name":"alice-key","user":"admin/Z"}`,
|
|
`{"owner":"hanzo","name":"alice-key","user":"admin/z","scope":"publish"}`,
|
|
} {
|
|
if status, resp := h.send(t, boss, "PUT", "/v1/iam/keys/hanzo/alice-key", body); status != 403 {
|
|
t.Errorf("hanzo's admin repointed a key with %s: %d %s", body, status, resp)
|
|
}
|
|
}
|
|
if k, err := orm.Get[schema.Key](h.db, "hanzo/alice-key"); err != nil || k.User != "alice" {
|
|
t.Fatalf("the refused update moved the key: %+v %v", k, err)
|
|
}
|
|
if got := keysNaming(t, h); len(got) != 1 || got[0] != "admin/beacon -> z" {
|
|
t.Fatalf("key rows speaking for the operator: %v, want only the publishable one", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTheMintIssuesNoSuperAdminASecretKey(t *testing.T) {
|
|
h := newHarness(t)
|
|
operatorFixtures(t, h)
|
|
mintFixtures(t, h)
|
|
t.Setenv("IAM_ADMIN_TOKEN_EXCHANGE_APPS", minterApp)
|
|
|
|
post := func(path string) (int, string) {
|
|
t.Helper()
|
|
req := httptest.NewRequest("POST", path, nil)
|
|
req.Host = "hanzo.id"
|
|
req.SetBasicAuth(minterApp, minterSecret)
|
|
return h.do(t, req)
|
|
}
|
|
for _, path := range []string{"/v1/iam/users/admin/z/keys", "/v1/iam/users/admin/Z/keys"} {
|
|
if status, body := post(path); status != 403 {
|
|
t.Errorf("POST %s: %d %s", path, status, body)
|
|
}
|
|
}
|
|
if got := keysNaming(t, h); len(got) != 0 {
|
|
t.Fatalf("the mint wrote %v under a refusal", got)
|
|
}
|
|
if status, body := post("/v1/iam/users/admin/z/keys?type=publishable"); status != 200 {
|
|
t.Fatalf("the operator's org could not get a publishable key: %d %s", status, body)
|
|
}
|
|
if got := whoHolds(t, h, mint(t, h, userKeys)); got.Data.Owner != "orgb" || got.Data.Name != "dana" {
|
|
t.Fatalf("an ordinary member's minted key resolved to %q/%q", got.Data.Owner, got.Data.Name)
|
|
}
|
|
}
|
|
|
|
// A secret key that already names a SuperAdmin — planted before the write gate
|
|
// refused it — resolves to nobody. A key naming hanzo/z, who holds an admin-org
|
|
// membership, resolves to hanzo/z: that membership makes nobody a SuperAdmin.
|
|
func TestASuperAdminsKeyResolvesToNobody(t *testing.T) {
|
|
h := newHarness(t)
|
|
operatorFixtures(t, h)
|
|
mintFixtures(t, h)
|
|
|
|
plant := func(name, user, secret string) {
|
|
t.Helper()
|
|
k := orm.New[schema.Key](h.db)
|
|
k.Owner, k.Name, k.User = "hanzo", name, user
|
|
k.AccessKey, k.AccessSecret = "pk-"+name, secret
|
|
k.SetId("hanzo/" + name)
|
|
if err := k.CreateCtx(context.Background()); err != nil {
|
|
t.Fatalf("plant %s: %v", name, err)
|
|
}
|
|
}
|
|
plant("operator", "admin/z", "sk-planted-oper-0001")
|
|
plant("folded", "admin/Z", "sk-planted-folded-01")
|
|
plant("member", "z", "sk-planted-member-01")
|
|
|
|
for _, secret := range []string{"sk-planted-oper-0001", "sk-planted-folded-01"} {
|
|
got := whoHolds(t, h, secret)
|
|
if got.Code != string(store.KeySuperAdmin) || got.Data.Name != "" {
|
|
t.Fatalf("%s resolved to %q/%q (code %q), want key_superadmin", secret, got.Data.Owner, got.Data.Name, got.Code)
|
|
}
|
|
}
|
|
if got := whoHolds(t, h, "sk-planted-member-01"); got.Data.Owner != "hanzo" || got.Data.Name != "z" {
|
|
t.Fatalf("the key naming hanzo/z resolved to %q/%q (code %q)", got.Data.Owner, got.Data.Name, got.Code)
|
|
}
|
|
}
|