Files
iam/internal/keys/superadmin_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

70 lines
2.3 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package keys
import (
"context"
"errors"
"testing"
policy "github.com/hanzoai/authz"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// operatorDB seeds admin/z, a SuperAdmin; hanzo/z, a hanzo account holding an
// admin-org membership; and hanzo/alice.
func operatorDB(t *testing.T) orm.DB {
t.Helper()
db := memDB(t)
for _, id := range [][2]string{{policy.AdminOrg, "z"}, {"hanzo", "z"}, {"hanzo", "alice"}} {
u := orm.New[schema.User](db)
u.Owner, u.Name = id[0], id[1]
u.SetId(id[0] + "/" + id[1])
if err := u.CreateCtx(context.Background()); err != nil {
t.Fatalf("seed %s/%s: %v", id[0], id[1], err)
}
}
if _, err := store.EnsureMembership(context.Background(), db, "hanzo/z", policy.AdminOrg, store.RoleMember); err != nil {
t.Fatalf("grant: %v", err)
}
return db
}
// No account in the admin org is minted a secret key by any of the three mints —
// its SuperAdmins and its machines alike — and a publishable key, which names
// only the org, is minted as for anyone. An admin-org membership held from a
// brand org is no bar: hanzo/z is minted a key like hanzo/alice.
func TestTheMintsIssueNoSuperAdminASecretKey(t *testing.T) {
ctx := context.Background()
db := operatorDB(t)
for _, user := range []string{"z", "Z"} {
if _, err := MintUserKey(ctx, db, policy.AdminOrg, user, ""); !errors.Is(err, ErrSuperAdminKey) {
t.Errorf("MintUserKey(admin, %s) = %v, want ErrSuperAdminKey", user, err)
}
}
if _, _, err := MintAccountKey(ctx, db, policy.AdminOrg, "provisioner"); !errors.Is(err, ErrSuperAdminKey) {
t.Errorf("MintAccountKey(admin, provisioner) = %v, want ErrSuperAdminKey", err)
}
rows, err := orm.TypedQuery[schema.Key](db).GetAll(ctx)
if err != nil || len(rows) != 0 {
t.Fatalf("refused mints left %d rows (%v)", len(rows), err)
}
if _, err := MintUserKey(ctx, db, policy.AdminOrg, "z", schema.KeyScopePublish); err != nil {
t.Errorf("a publishable key for the admin org: %v", err)
}
for _, user := range []string{"z", "alice"} {
if _, err := MintUserKey(ctx, db, "hanzo", user, ""); err != nil {
t.Errorf("hanzo/%s's key: %v", user, err)
}
}
if _, _, err := MintAccountKey(ctx, db, "hanzo", "hanzo-bot"); err != nil {
t.Errorf("a tenant's service account key: %v", err)
}
}