Files
iam/internal/routes/red_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

103 lines
3.8 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package routes_test
import (
"context"
"encoding/json"
"testing"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
const scimPassword = `{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"replace","path":"password","value":"a whole new password"}]}`
// SCIM is a second write surface for the same row. The branch covers it for an
// application; an org admin reaches SCIM through the same tenant rule.
func TestRed_SuperAdmin_OrgAdminCannotWriteThroughSCIM(t *testing.T) {
h := newHarness(t)
operatorFixtures(t, h)
boss := h.person(t, "hanzo/boss")
if status, body := h.send(t, boss, "PATCH", "/v1/iam/scim/v2/Users/admin/z", scimPassword); status != 403 {
t.Errorf("SCIM PATCH of the operator's password as hanzo's admin answered %d: %s", status, body)
}
if got := digest(t, h, "admin", "z"); got != secretUserHash {
t.Errorf("the operator's password changed through SCIM")
}
replace := `{"schemas":["urn:ietf:params:scim:schemas:core:2.0:User"],"userName":"z","emails":[{"value":"[email protected]","primary":true}]}`
if status, body := h.send(t, boss, "PUT", "/v1/iam/scim/v2/Users/admin/z", replace); status != 403 {
t.Errorf("SCIM PUT of the operator as hanzo's admin answered %d: %s", status, body)
}
if status, body := h.send(t, boss, "DELETE", "/v1/iam/scim/v2/Users/admin/z", ""); status != 403 {
t.Errorf("SCIM DELETE of the operator as hanzo's admin answered %d: %s", status, body)
}
if digest(t, h, "admin", "z") == "" {
t.Errorf("the operator's account was deleted through SCIM")
}
}
// Filing a passkey for the operator is refused; removing one is not asked.
func TestRed_SuperAdmin_OrgAdminCannotRemoveTheirPasskeyOrToken(t *testing.T) {
h := newHarness(t)
operatorFixtures(t, h)
boss := h.person(t, "hanzo/boss")
ctx := context.Background()
pk := orm.New[schema.WebauthnCredential](h.db)
pk.Owner, pk.Name, pk.User = "hanzo", "operator-key", "admin/z"
pk.SetId("hanzo/operator-key")
if err := pk.CreateCtx(ctx); err != nil {
t.Fatal(err)
}
if status, body := h.send(t, boss, "DELETE", "/v1/iam/webauthn-credentials/hanzo/operator-key", ""); status == 200 {
if _, err := orm.Get[schema.WebauthnCredential](h.db, "hanzo/operator-key"); err != nil {
t.Errorf("hanzo's admin removed the operator's passkey: %d %s", status, body)
}
}
tok := orm.New[schema.Token](h.db)
tok.Owner, tok.Name, tok.User = "hanzo", "operator-session", "admin/z"
tok.SetId("hanzo/operator-session")
if err := tok.CreateCtx(ctx); err != nil {
t.Fatal(err)
}
if status, body := h.send(t, boss, "DELETE", "/v1/iam/tokens/hanzo/operator-session", ""); status == 200 {
if _, err := orm.Get[schema.Token](h.db, "hanzo/operator-session"); err != nil {
t.Errorf("hanzo's admin revoked the operator's token: %d %s", status, body)
}
}
}
// An org admin mints an API key in their own org naming the operator, who works
// there by a membership. No such key resolves to the operator's row.
func TestRed_SuperAdmin_OrgAdminCannotMintAKeyForThem(t *testing.T) {
h := newHarness(t)
operatorFixtures(t, h)
boss := h.person(t, "hanzo/boss")
status, body := h.send(t, boss, "POST", "/v1/iam/keys", `{"owner":"hanzo","name":"planted","user":"admin/z"}`)
if status != 200 {
return // refused
}
var e struct {
Data struct {
AccessSecret string `json:"accessSecret"`
} `json:"data"`
AccessSecret string `json:"accessSecret"`
}
_ = json.Unmarshal([]byte(body), &e)
secret := e.Data.AccessSecret
if secret == "" {
secret = e.AccessSecret
}
u, err := store.UserByAccessKey(context.Background(), h.db, secret)
if err == nil && u != nil {
t.Errorf("hanzo's admin minted a key that resolves to %s/%s (SuperAdmin=%v)", u.Owner, u.Name, u.SuperAdmin())
}
}