Files
iam/internal/bootstrap/converge_test.go
zeekay ef2f42a2ec Decide SuperAdmin on the row's own org
schema.User.SuperAdmin is the one predicate: a person whose row's owner
is the admin org. The Guard sets Principal.Sudo from it when it loads
the caller, and assume/release, device approval, unlink, the bootstrap
credential freeze and the registry push gate ask the row directly.
store.IsSuperAdmin, which also admitted a membership of the admin org,
is gone. A brand org's user added to the admin org is an ordinary
member of their own org, and a service account or program in the admin
org is never SuperAdmin.

hanzoai/authz v1.10.42 reads the same rule off a token (Claims.Sudo on
orgs[0]) and lets a membership of the admin org open nothing, so such a
member no longer reads or edits the admin org's registry row either.
The token IAM mints is unchanged: orgs still opens with the row's
owner.

Two protective gates keep the whole admin org, machines and unheld
names included: no secret key names an account there
(store.SuperAdminKey), and only a SuperAdmin writes one
(users.Authorize). Both read the owner as written, so neither reads the
store.

Every request the Guard admits for a SuperAdmin is recorded as a
superadmin audit row, PlatformWritten, with its method, address, answer
and client address; so is a SuperAdmin's unlink of someone else's
sign-in method and a device approval into another org.

TestEverySuperAdminGateAdmitsTheAdminOrgsPeopleOnly drives ten
SuperAdmin-only requests through the router as two SuperAdmins, a brand
admin holding an admin-org membership and two admin-org machines, and
reads the trail row each one leaves.

Claude-Session: https://claude.ai/code/session_01HuMDfgFssPUptYUzgZ6Cp8
2026-09-27 19:24:51 -07:00

250 lines
10 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package bootstrap_test
import (
"context"
"fmt"
"testing"
policy "github.com/hanzoai/authz"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/internal/cred"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// seed writes a user row the way the surface named by kind writes it, so a
// converge meets the row a DIFFERENT surface left rather than one this test
// invented: "normal-user" is what signup, federation and wallet stamp, and
// "service-account" is what internal/serviceaccounts stamps.
func seed(t *testing.T, db orm.DB, owner, name, kind, display string, admin bool) {
t.Helper()
u := orm.New[schema.User](db)
u.Owner, u.Name, u.Type, u.DisplayName, u.IsAdmin = owner, name, kind, display, admin
u.SetId(owner + "/" + name)
if err := u.CreateCtx(context.Background()); err != nil {
t.Fatalf("seed %s/%s: %v", owner, name, err)
}
}
func row(t *testing.T, db orm.DB, owner, name string) *schema.User {
t.Helper()
u, err := store.GetUserByName(context.Background(), db, owner, name)
if err != nil {
t.Fatalf("read %s/%s: %v", owner, name, err)
}
return u
}
// A. A declaration may not GRANT org-admin to a row it did not create. Somebody
// signed up for themselves and reached the name first; converging that row hands
// whoever holds it whatever authority the document names.
func TestUpsertUser_refusesAuthorityOnARowItDidNotCreate(t *testing.T) {
app, db := boot(t)
seed(t, db, "acme", "z", "normal-user", "Squatter", false)
st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken,
`{"owner":"acme","name":"z","displayName":"Acme Owner","email":"[email protected]","isAdmin":true}`)
after := row(t, db, "acme", "z")
t.Logf("A status=%d action=%v isAdmin=%t msg=%v", st, m["action"], after.IsAdmin, m["msg"])
if st != 400 {
t.Errorf("status = %d, want 400 — a converge granted org-admin to a row it never created", st)
}
if after.IsAdmin {
t.Errorf("isAdmin = true, want false — the grant landed on somebody else's row")
}
}
// B. The row this endpoint's own earlier releases created carries no class at
// all, and re-converging it is every install's steady state.
func TestUpsertUser_convergesTheRowItCreated(t *testing.T) {
app, db := boot(t)
body := `{"owner":"acme","name":"ops","displayName":"Acme Ops","email":"[email protected]","password":"s3cret","isAdmin":true}`
st1, m1 := post(t, app, "/v1/iam/admin/users/upsert", svcToken, body)
if st1 != 200 || m1["action"] != "created" {
t.Fatalf("create: status=%d body=%v", st1, m1)
}
if k := row(t, db, "acme", "ops").Type; k != "" {
t.Fatalf("created row carries class %q; this endpoint stamps none", k)
}
st2, m2 := post(t, app, "/v1/iam/admin/users/upsert", svcToken, body)
after := row(t, db, "acme", "ops")
t.Logf("B status=%d action=%v isAdmin=%t msg=%v", st2, m2["action"], after.IsAdmin, m2["msg"])
if st2 != 200 || m2["action"] != "updated" {
t.Errorf("re-converge: status=%d action=%v, want 200 updated — steady state must be a no-op", st2, m2["action"])
}
if !after.IsAdmin {
t.Errorf("isAdmin = false, want true — the converge dropped the grant it made")
}
}
// C. A document that declares a new tenant's accounts meets no rows at all.
func TestUpsertUser_createsANewTenantsAccounts(t *testing.T) {
app, db := boot(t)
st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken,
`{"owner":"newco","name":"z","displayName":"Newco Owner","email":"[email protected]","password":"s3cret","isAdmin":true}`)
after := row(t, db, "newco", "z")
t.Logf("C status=%d action=%v exists=%t msg=%v", st, m["action"], after != nil, m["msg"])
if st != 200 || m["action"] != "created" {
t.Errorf("status=%d action=%v, want 200 created", st, m["action"])
}
if after == nil {
t.Errorf("account not created")
}
}
// D. A padded owner is stored trimmed, so a declaration that never spells the
// reserved org lands a principal inside it — and SuperAdmin is membership of that
// org (store.IsSuperAdmin).
func TestUpsertUser_refusesAnOwnerThatIsNotTheNameStored(t *testing.T) {
app, db := boot(t)
st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken,
`{"owner":" admin ","name":"root","displayName":"Root","isAdmin":false}`)
// The row is looked for under the TRIMMED name, which is where it would land —
// and anchored in the reserved org is SuperAdmin outright (store.IsSuperAdmin
// answers on the org alone), so the account existing there is the whole grant.
landed := row(t, db, "admin", "root")
t.Logf("D status=%d action=%v admin/root=%t msg=%v", st, m["action"], landed != nil, m["msg"])
if st != 400 {
t.Errorf("status = %d, want 400 — %q is not the name it would be stored under", st, " admin ")
}
if landed != nil {
t.Errorf("a declaration that never spells %q landed a principal in it", policy.AdminOrg)
}
}
// The same rule on the registration, where the organization is what resolveCert
// derives the app's signing identity from: an org that only becomes `admin` once
// it is stored derives `cert-admin`, the platform's own signing cert.
func TestUpsertApplication_refusesAnOrgThatIsNotTheNameStored(t *testing.T) {
app, db := boot(t)
st, m := post(t, app, "/v1/iam/admin/applications/upsert", svcToken,
`{"organization":" admin ","name":"acme-console","grantTypes":["authorization_code"]}`)
landed, err := store.GetApplicationByName(context.Background(), db, "admin", "acme-console")
if err != nil {
t.Fatalf("read application: %v", err)
}
t.Logf("org status=%d action=%v registered=%t msg=%v", st, m["action"], landed != nil, m["msg"])
if st != 400 {
t.Errorf("status = %d, want 400", st)
}
if landed != nil {
t.Errorf("registered with cert %q from an organization the document never spells", landed.Cert)
}
}
// A declaration presents the same credential on every run, and a digest carries a
// fresh salt — so re-hashing what is already stored rotates, on every reconcile
// loop, the credential the running services hold.
func TestUpsertUser_steadyStateKeepsTheStoredCredential(t *testing.T) {
app, db := boot(t)
body := `{"owner":"acme","name":"ops","email":"[email protected]","password":"s3cret","isAdmin":true}`
if st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken, body); st != 200 {
t.Fatalf("create: status=%d body=%v", st, m)
}
first := row(t, db, "acme", "ops")
if st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken, body); st != 200 {
t.Fatalf("re-converge: status=%d body=%v", st, m)
}
again := row(t, db, "acme", "ops")
if again.PasswordHash != first.PasswordHash {
t.Errorf("the stored credential was re-hashed by a converge that changed nothing")
}
if again.UpdatedTime != first.UpdatedTime {
t.Errorf("updatedTime = %q, was %q — it says when the reconciler ran, not when the account changed",
again.UpdatedTime, first.UpdatedTime)
}
}
// E. internal/serviceaccounts mints machine rows: it derives the canonical name,
// binds an agent and issues the key the machine authenticates with. A password
// declaration describes none of that.
func TestUpsertUser_refusesAMachineRow(t *testing.T) {
app, db := boot(t)
seed(t, db, "acme", "acme-bot", schema.ServiceAccount, "Acme Bot", false)
st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken,
`{"owner":"acme","name":"acme-bot","displayName":"Platform Bot","password":"s3cret","isAdmin":false}`)
after := row(t, db, "acme", "acme-bot")
t.Logf("E status=%d action=%v displayName=%q credential=%t msg=%v",
st, m["action"], after.DisplayName, after.PasswordHash != "", m["msg"])
if st != 400 {
t.Errorf("status = %d, want 400 — a machine row is not this endpoint's to write", st)
}
if after.DisplayName != "Acme Bot" {
t.Errorf("displayName = %q, want %q — the converge adopted a row another surface minted",
after.DisplayName, "Acme Bot")
}
if after.PasswordHash != "" {
t.Errorf("a password was written onto a machine identity")
}
}
// F. A SuperAdmin's password, address and phone are how they sign in and how they
// recover. The service token is not a SuperAdmin, so a declaration sets the three
// when it creates the account and never after — a rewrite is a takeover of the
// platform's operator. A SuperAdmin is a person declared IN the reserved org; a
// brand org's person holding an admin-org membership is not one, and converges
// like any other account.
func TestUpsertUser_neverRewritesASuperAdminsCredentials(t *testing.T) {
app, db := boot(t)
first := `{"owner":"%s","name":"%s","email":"[email protected]","phone":"+15550100","password":"their own"}`
taken := `{"owner":"%s","name":"%s","displayName":"Renamed","email":"[email protected]","phone":"+15550199","password":"taken"}`
for _, who := range [][2]string{{policy.AdminOrg, "z"}, {policy.AdminOrg, "ops"}, {"hanzo", "z"}, {"hanzo", "alice"}} {
owner, name := who[0], who[1]
if st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken, fmt.Sprintf(first, owner, name)); st != 200 {
t.Fatalf("create %s/%s: status=%d body=%v", owner, name, st, m)
}
}
if _, err := store.EnsureMembership(context.Background(), db, "hanzo/z", policy.AdminOrg, store.RoleMember); err != nil {
t.Fatalf("grant: %v", err)
}
for _, who := range [][2]string{{policy.AdminOrg, "z"}, {policy.AdminOrg, "Z"}, {policy.AdminOrg, "ops"}} {
owner, name := who[0], who[1]
before := row(t, db, owner, name)
if st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken, fmt.Sprintf(taken, owner, name)); st != 200 {
t.Fatalf("converge %s/%s: status=%d body=%v", owner, name, st, m)
}
after := row(t, db, owner, name)
if after.PasswordHash != before.PasswordHash || cred.Verify(after.PasswordType, "taken", after.PasswordHash) {
t.Errorf("%s/%s: a declaration rotated a SuperAdmin's password", owner, name)
}
if after.Email != before.Email || after.Phone != before.Phone {
t.Errorf("%s/%s: a declaration moved a SuperAdmin's address and phone to %q %q", owner, name, after.Email, after.Phone)
}
if after.DisplayName != "Renamed" {
t.Errorf("%s/%s: displayName = %q — the rest of the row still converges", owner, name, after.DisplayName)
}
}
// Everyone else converges exactly as before, the admin-org member included.
for _, name := range []string{"z", "alice"} {
if st, m := post(t, app, "/v1/iam/admin/users/upsert", svcToken, fmt.Sprintf(taken, "hanzo", name)); st != 200 {
t.Fatalf("converge hanzo/%s: status=%d body=%v", name, st, m)
}
u := row(t, db, "hanzo", name)
if !cred.Verify(u.PasswordType, "taken", u.PasswordHash) || u.Email != "[email protected]" {
t.Errorf("hanzo/%s: the declared password and address did not land", name)
}
}
}