mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-21 11:41:56 +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.
139 lines
5.2 KiB
Go
139 lines
5.2 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/orm"
|
|
)
|
|
|
|
// A key works exactly as long as its holder may sign in. Suspending an account —
|
|
// by hand, or by SCIM active=false — or deleting it ends every key it holds, its
|
|
// org's default key above all: that one is an owner's, never expires, and outlived
|
|
// the founder an org offboarded.
|
|
func TestAKeyDiesWithItsHolder(t *testing.T) {
|
|
ctx := context.Background()
|
|
for _, tc := range []struct {
|
|
name string
|
|
disable func(*testing.T, orm.DB)
|
|
}{
|
|
{"suspended", func(t *testing.T, db orm.DB) { holderFlag(t, db, func(u *userFlags) { u.forbidden = true }) }},
|
|
{"deleted", func(t *testing.T, db orm.DB) { holderFlag(t, db, func(u *userFlags) { u.deleted = true }) }},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
db := memDB(t)
|
|
seedKeyUser(t, db, "acme", "alice", "[email protected]", "")
|
|
seedKey(t, db, "acme", "default", "acme/alice", "pk-live-ALICE", "sk-live-ALICE")
|
|
// A member's key in a second org dies the same way.
|
|
seedKey(t, db, "team", "alice-k", "acme/alice", "pk-live-TEAM", "sk-live-TEAM")
|
|
if _, err := EnsureMembership(ctx, db, "acme/alice", "team", RoleMember); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, sk := range []string{"sk-live-ALICE", "sk-live-TEAM"} {
|
|
if _, err := HolderByAccessKey(ctx, db, sk); err != nil {
|
|
t.Fatalf("%s before: %v, want it to resolve", sk, err)
|
|
}
|
|
}
|
|
tc.disable(t, db)
|
|
for _, sk := range []string{"sk-live-ALICE", "sk-live-TEAM"} {
|
|
h, err := HolderByAccessKey(ctx, db, sk)
|
|
if h.User != nil || !errors.Is(err, orm.ErrNotFound) || Reason(err) != KeyHolderDisabled {
|
|
t.Fatalf("%s after the holder was %s: %+v %v, want refused as %s", sk, tc.name, h.User, err, KeyHolderDisabled)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type userFlags struct{ forbidden, deleted bool }
|
|
|
|
// holderFlag sets acme/alice's account flags.
|
|
func holderFlag(t *testing.T, db orm.DB, set func(*userFlags)) {
|
|
t.Helper()
|
|
u, err := GetUserByName(context.Background(), db, "acme", "alice")
|
|
if err != nil || u == nil {
|
|
t.Fatalf("read acme/alice: %v", err)
|
|
}
|
|
var f userFlags
|
|
set(&f)
|
|
u.IsForbidden, u.IsDeleted = f.forbidden, f.deleted
|
|
if err := u.UpdateCtx(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// A key acting in an org the platform runs works for the people who run it and its
|
|
// machines, and for no plain member: a customer who landed in a brand org holds no
|
|
// credential that acts in it.
|
|
func TestAKeyInABrandOrgActsOnlyForThoseWhoRunIt(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := memDB(t)
|
|
seedKeyUser(t, db, "hanzo", "eve", "[email protected]", "")
|
|
seedKey(t, db, "hanzo", "eve-key", "hanzo/eve", "pk-live-EVE", "sk-live-EVE")
|
|
staff := seedKeyUser(t, db, "hanzo", "lead", "[email protected]", "")
|
|
staff.IsAdmin = true
|
|
if err := staff.UpdateCtx(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedKey(t, db, "hanzo", "lead-key", "hanzo/lead", "pk-live-LEAD", "sk-live-LEAD")
|
|
bot := seedKeyUser(t, db, "hanzo", "bot", "", "")
|
|
bot.Type = "service-account"
|
|
if err := bot.UpdateCtx(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedKey(t, db, "hanzo", "bot-key", "hanzo/bot", "pk-live-BOT", "sk-live-BOT")
|
|
|
|
if _, err := HolderByAccessKey(ctx, db, "sk-live-EVE"); Reason(err) != KeyNotTenant {
|
|
t.Fatalf("a plain member's key in hanzo: %v, want %s", err, KeyNotTenant)
|
|
}
|
|
for _, sk := range []string{"sk-live-LEAD", "sk-live-BOT"} {
|
|
if h, err := HolderByAccessKey(ctx, db, sk); err != nil || h.Org != "hanzo" {
|
|
t.Fatalf("%s: %+v %v, want it to act in hanzo", sk, h, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A brand's directory is no one's tenant: a key filed there acts for nobody,
|
|
// whatever its holder's standing, so a directory account's key can never read as
|
|
// one tenant shared by every customer in it.
|
|
func TestAKeyInADirectoryActsForNobody(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := memDB(t)
|
|
u := seedKeyUser(t, db, "hanzo-id", "ann", "[email protected]", "")
|
|
u.IsAdmin = true
|
|
if err := u.UpdateCtx(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedKey(t, db, "hanzo-id", "ann-key", "hanzo-id/ann", "pk-live-ANN", "sk-live-ANN")
|
|
h, err := HolderByAccessKey(ctx, db, "sk-live-ANN")
|
|
if h.User != nil || Reason(err) != KeyNotTenant {
|
|
t.Fatalf("a directory key resolved to %+v (%v), want nobody (%s)", h.User, err, KeyNotTenant)
|
|
}
|
|
}
|
|
|
|
// A machine acts in the org that made it. A service account of another org that
|
|
// holds a plain membership of a brand org is a plain member there, as a person is.
|
|
func TestAnotherOrgsMachineIsAPlainMemberOfABrandOrg(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := memDB(t)
|
|
bot := seedKeyUser(t, db, "evil", "evil-bot", "", "")
|
|
bot.Type = "service-account"
|
|
if err := bot.UpdateCtx(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := EnsureMembership(ctx, db, "evil/evil-bot", "lux", RoleMember); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedKey(t, db, "lux", "evil-bot-key", "evil/evil-bot", "pk-live-EVILBOT", "sk-live-EVILBOT")
|
|
if h, err := HolderByAccessKey(ctx, db, "sk-live-EVILBOT"); h.User != nil || Reason(err) != KeyNotTenant {
|
|
t.Fatalf("another org's machine acts in lux: %+v (%v)", h.User, err)
|
|
}
|
|
if !Acts("evil", bot, HomeRole(bot)) || Acts("lux", bot, RoleMember) {
|
|
t.Fatal("a machine acts in its own org and in no brand org it merely belongs to")
|
|
}
|
|
}
|