From 098886e0e474ff3671282b88408248441da00bea Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Mon, 28 Sep 2026 19:03:46 +0200 Subject: [PATCH] Let a person belong to many places, or to none A person was in exactly one project, chosen at approval, and could not use the gateway before that. People belong to several teams and to none, so: - memberships: a person is a member of any number of organizations, teams and projects, or of none; a member of a team or organization may use every project in it. users join/leave, and the people page adds and takes out from either side. Approval no longer needs a project. - personal keys: a key may have an owner and no project. /profile asks what a new key spends against; keys create --for does the same. auth.personal sets default limits for personal use or turns it off. - limits for people: --scope user holds a person across every key, project and the chat page; --per-member on an org, team or project gives each member their own share there. Resolution folds the tree, each per-member limit for the key's owner, and the owner's own, each in its own pool; the quotas page shows and sets both. - the chat page asks what to spend against and checks it per message. - usage records who spent it, so a person's budget survives a restart, personal use and chat included; restore no longer drops rows with no project. Leaving a project revokes the keys made there unless another membership still reaches it. A personal key keeps no threads or notes, which are per project, and is refused rather than dropped into the anonymous pool. Migration 0010 turns each person's project into a membership, adds usage.user_id and backfills it, and makes api_keys.project_id nullable. SQLite cannot drop NOT NULL, so the table is rebuilt; that needs foreign keys off, which only works outside a transaction, so migrations marked for it run on one connection with foreign_key_check before commit. Tested on a populated SQLite store with claims, and on a copy of the cluster's PostgreSQL store: 5 people became members, 17 keys and 3 claims intact, 145 of 577 usage rows and all 69 chat rows attributed. The PostgreSQL store tests, including new membership cases, pass on 16. users move is gone; join and leave replace it. Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 31 ++ docs/onboarding.md | 79 ++++- docs/reference.md | 42 +++ internal/account/account_test.go | 20 +- internal/accounting/accounting.go | 20 +- internal/accounting/people_test.go | 126 ++++++++ internal/auth/auth.go | 5 +- internal/cli/keys.go | 63 +++- internal/cli/org.go | 63 ++-- internal/cli/org_test.go | 15 +- internal/cli/serverinit_test.go | 3 +- internal/cli/users.go | 165 +++++++--- internal/config/config.go | 20 ++ internal/router/chat.go | 97 +++++- internal/router/handlers.go | 6 +- internal/router/router.go | 35 +++ internal/router/threads.go | 10 + internal/store/copy.go | 2 + internal/store/copy_test.go | 37 ++- internal/store/hierarchy.go | 94 +++--- internal/store/hierarchy_test.go | 56 ++-- internal/store/memberships.go | 284 ++++++++++++++++++ internal/store/memberships_test.go | 212 +++++++++++++ internal/store/migrate.go | 41 ++- internal/store/migrate0010_test.go | 128 ++++++++ .../migrations/postgres/0010_memberships.sql | 35 +++ .../migrations/sqlite/0010_memberships.sql | 60 ++++ internal/store/postgres_memberships_test.go | 87 ++++++ internal/store/postgres_test.go | 2 +- internal/store/sqlite.go | 99 ++---- internal/store/store.go | 42 ++- internal/store/usageseries.go | 12 +- internal/store/users.go | 114 +++---- internal/web/account.go | 113 +++++-- internal/web/admin_quotas.go | 107 ++++++- internal/web/admin_quotas_test.go | 47 +++ internal/web/admin_tenancy_test.go | 33 +- internal/web/admin_users.go | 139 +++++++-- internal/web/chat.go | 26 +- internal/web/chat_test.go | 2 +- internal/web/nav.go | 19 -- internal/web/static/base.css | 14 + internal/web/templates/account.html | 32 +- internal/web/templates/admin_quotas.html | 70 +++++ internal/web/templates/admin_users.html | 115 +++++-- internal/web/templates/chat.html | 18 +- 46 files changed, 2372 insertions(+), 468 deletions(-) create mode 100644 internal/accounting/people_test.go create mode 100644 internal/store/memberships.go create mode 100644 internal/store/memberships_test.go create mode 100644 internal/store/migrate0010_test.go create mode 100644 internal/store/migrations/postgres/0010_memberships.sql create mode 100644 internal/store/migrations/sqlite/0010_memberships.sql create mode 100644 internal/store/postgres_memberships_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 604af82..1c6493c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,37 @@ APIs change between minors without a deprecation period. ## Unreleased +### People belong to many places, or to none + +A person was in exactly one project, chosen at approval, and could not use the +gateway before that. Now: + +- **Memberships.** A person can be a member of any number of organizations, + teams and projects, or of none. A member of a team or organization may use + every project in it. `llmgw users join/leave`, and the people page adds and + removes them from either side. Approving no longer requires a project. +- **Personal keys.** A key can belong to a person with no project. On + `/profile` a person chooses what a new key spends against: one of their + projects, or Personal. `llmgw keys create --for ` does the same. + `auth.personal` sets default limits for personal use, or turns it off. +- **Limits for people.** `--scope user` limits a person across every key, + project and the chat page. `--per-member` on an organization, team or + project gives each member their own share there. Both are on the quotas + page, and are enforced alongside the tree, each in its own pool. +- **The chat page** asks what to spend against when a person has more than one + choice, and checks it on every message. +- **Spend is recorded with who spent it**, so a person's budget survives a + restart, personal use and the chat page included. + +Leaving a project revokes the keys the person made there, unless another +membership still reaches it. A personal key keeps no conversations or notes: +those are kept per project. + +**Upgrading.** Migration 0010 turns everyone's project into a membership and +attributes past usage to people where a key had an owner. On SQLite it rebuilds +the keys table; it was run against a copy of a production PostgreSQL store +before release. `llmgw users move` is gone: use `join` and `leave`. + ### One menu on every page Each page drew its own menu, and no two agreed: different links, in a different diff --git a/docs/onboarding.md b/docs/onboarding.md index 82e9e92..f7474ec 100644 --- a/docs/onboarding.md +++ b/docs/onboarding.md @@ -39,10 +39,11 @@ The landing page then links to it, and the rest is: ``` llmgw users list --pending --store … -ID EMAIL NAME STATE PROJECT SIGNED UP -usr-k4mq… ayse@example.com Ayşe pending - 4m12s ago +ID EMAIL STATE ADMIN MEMBER OF SIGNED UP +usr-k4mq… ayse@example.com pending - on their own 4m12s ago -llmgw users approve usr-k4mq… --project default --by ilker --store … +llmgw users approve ayse@example.com --by ilker --store … +llmgw users approve ayse@example.com --project Acme/ml/training --store … # or straight into a project ``` That is the whole operator loop. From then on they sign in at `/login`, and @@ -105,15 +106,53 @@ The tree is not fixed once made: llmgw org rename project Acme/platform/gateway api-gateway llmgw org move project Acme/platform/gateway --to Scania/ml llmgw org remove team Acme/old # only when nothing is in it -llmgw users move ayse@example.com --project Acme/ml/research llmgw users delete ayse@example.com --yes # signs out, revokes their keys -llmgw keys list --all # every key, its project and owner +llmgw keys list --all # every key, where it spends, and whose ``` -Moving a person takes the keys they made with them, so they stop spending the -old project's budget; keys an operator issued in the project stay with it. A -project that ever had a key cannot be removed, because usage history refers to -it — rename it instead. +A project that ever had a key cannot be removed, because usage history refers +to it — rename it instead. + +### Where people belong + +A person can be a member of any number of organizations, teams and projects, or +of none: + +``` +llmgw users join ayse@example.com --team Acme/ml # every project in ml +llmgw users join ayse@example.com --project Scania/research/vision +llmgw users leave ayse@example.com --project Acme/ml/training +``` + +A member of a team or organization may use every project in it. Leaving revokes +the keys the person made in projects they can no longer reach; a project they +still reach through another membership keeps them. + +Someone who is a member of nothing still uses the gateway, on their own: +personal keys, and the chat page, within their personal limits. When they make +a key on `/profile` they choose what it spends against — one of their projects, +or "Personal". An operator can do the same: + +``` +llmgw keys create laptop --for ayse@example.com # personal +llmgw keys create ci --for ayse@example.com --project Acme/ml/training +``` + +Personal use can be limited by default, or turned off so people use the gateway +only through their projects: + +```yaml +auth: + personal: + rpm: 20 + tpm: 50000 + budget: 10 # whole units + period: month + # disabled: true +``` + +A personal key has no project, so it keeps no conversations or agent notes; +those are kept per project. `llmgw keys init` still works for provisioning scripts and is idempotent, but it will no longer move a team or project that already exists under another parent; @@ -195,6 +234,20 @@ llmgw keys limits --store … --scope project --id Acme/platform/gateway \ --budget 50 --period month # 50 units a month ``` +Two more kinds of limit follow people rather than the tree: + +``` +# A person, everywhere: every key they hold, every project, the chat page +llmgw keys limits --scope user --id ayse@example.com --budget 25 + +# Each member of a project separately — a fair share, not one shared pool +llmgw keys limits --scope project --id Acme/ml/training --per-member --rpm 20 +``` + +A request is held to all of them at once: the key's own, its project's, team's +and organization's, each per-member limit on those for its owner, and the +owner's own. The strictest wins, and each is counted in its own pool. + ## The landing page Someone who reaches the gateway without a key sees the landing page. Tell it who @@ -314,9 +367,11 @@ page exists. ## Managing accounts from the browser `/admin/users` is the tree: organizations, teams and projects, each project -opening onto its people and its keys. From there an admin approves, moves, -suspends and removes people, revokes keys, and adds, renames, moves and removes -organizations, teams and projects. A searchable list of every account is at the +opening onto its members and its keys. From there an admin approves people +(into a project or on their own), adds them to and takes them out of +organizations, teams and projects, suspends and removes them, revokes keys, and +adds, renames, moves and removes organizations, teams and projects. `/admin/quotas` +sets limits on the tree, per member of any part of it, and per person. A searchable list of every account is at the bottom. The page works as plain forms; with scripts on, a change updates it in place and the projects you had open stay open. diff --git a/docs/reference.md b/docs/reference.md index fe3b760..e572d1a 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -29,6 +29,7 @@ rather than repeated. | `default_rpm` | `int` | [Auth](#auth) | | `default_tpm` | `int` | [Auth](#auth) | | `signup_open` | `bool` | [Auth](#auth) | +| `personal` | `Personal` | [Auth](#auth) | | `pricing` | `map[string]Price` | [Auth](#auth) | | `default_price` | `Price` | [Auth](#auth) | | `flush_interval` | `time.Duration` | [Auth](#auth) | @@ -102,6 +103,11 @@ rather than repeated. | `ib_root` | `string` | [Options](#options) | | `prefer_nvml` | `bool` | [Options](#options) | | `skip_gpus` | `bool` | [Options](#options) | +| `disabled` | `bool` | [Personal](#personal) | +| `rpm` | `int` | [Personal](#personal) | +| `tpm` | `int` | [Personal](#personal) | +| `budget` | `float64` | [Personal](#personal) | +| `period` | `string` | [Personal](#personal) | | `enabled` | `bool` | [Prefix](#prefix) | | `max_nodes` | `int` | [Prefix](#prefix) | | `min_match` | `int` | [Prefix](#prefix) | @@ -267,6 +273,15 @@ SignupOpen serves the signup form. Accounts it creates are pending until an operator approves them, so this is not a way in — but turning it off means accounts are made with `llmgw users create` instead. +### `personal` + +`Personal` + +Personal is what a person may do on their own, outside any project: make +personal keys and use the chat page personally, within these limits. + +Takes a [Personal](#personal) block. + ### `pricing` `map[string]Price` @@ -788,6 +803,33 @@ source of NVLink topology. SkipGPUs omits GPU discovery. A node without nvidia-smi is already found to have none, so this is for one whose driver is broken. +## Personal + +### `disabled` + +`bool` + +Disabled refuses personal keys and personal chat; people then use the +gateway only through projects they are members of. + +### `rpm` + +`int` + +### `tpm` + +`int` + +### `budget` + +`float64` + +Budget is in whole units per Period ("day" or "month"). + +### `period` + +`string` + ## Prefix ### `enabled` diff --git a/internal/account/account_test.go b/internal/account/account_test.go index 7256dfa..9dfa8d3 100644 --- a/internal/account/account_test.go +++ b/internal/account/account_test.go @@ -99,17 +99,21 @@ func TestApprovalIsWhatMakesAnAccountWork(t *testing.T) { if err != nil { t.Fatal(err) } - if got.ProjectID != "proj" { - t.Errorf("the approved account landed in project %q", got.ProjectID) + if ok, err := st.CanUseProject(ctx, got.ID, "proj"); err != nil || !ok { + t.Errorf("the approved account is not a member of the project it was approved into: %v", err) } } -func TestApprovalNeedsAProject(t *testing.T) { - // An approved account with nowhere to put its keys passes every check and - // then fails at the first key it tries to make. - _, st := newStoreAndUser(t) - if err := st.ApproveUser(context.Background(), "usr-1", "", "ilker", time.Now()); err == nil { - t.Error("an account was approved into no project") +func TestApprovalNeedNotNameAProject(t *testing.T) { + // A person need not belong to anything to use the gateway: approved into + // nothing, they work on their own within their personal limits. + svc, st := newStoreAndUser(t) + ctx := context.Background() + if err := st.ApproveUser(ctx, "usr-1", "", "ilker", time.Now()); err != nil { + t.Fatalf("approving with no project: %v", err) + } + if _, _, err := svc.SignIn(ctx, "ayse@example.com", goodPassword); err != nil { + t.Errorf("an account approved into nothing cannot sign in: %v", err) } } diff --git a/internal/accounting/accounting.go b/internal/accounting/accounting.go index 6ef3431..40f3237 100644 --- a/internal/accounting/accounting.go +++ b/internal/accounting/accounting.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log/slog" + "strings" "time" "gitea.mynodes.xyz/ilker/llmgw/internal/auth" @@ -79,14 +80,27 @@ func (a *Accountant) RestoreBudgets(ctx context.Context, s store.Store, period s // totals would bring the control plane back up believing the team had spent // nothing — which is a budget that resets on every restart, for exactly the // scopes where it matters most. - spend := make(map[string]int64, len(rows)*4) + // And every pool a person is in: their own, and each per-member limit of + // the project, team and organization they spent it in. + spend := make(map[string]int64, len(rows)*8) for _, r := range rows { - for _, o := range []store.LimitOwner{ + owners := []store.LimitOwner{ {Scope: store.ScopeKey, ID: r.KeyID}, {Scope: store.ScopeProject, ID: r.ProjectID}, {Scope: store.ScopeTeam, ID: r.TeamID}, {Scope: store.ScopeOrg, ID: r.OrgID}, - } { + {Scope: store.ScopeUser, ID: r.UserID}, + } + if r.UserID != "" && r.ProjectID != "" { + owners = append(owners, + store.MemberOwner(store.ScopeProjectMember, r.ProjectID, r.UserID), + store.MemberOwner(store.ScopeTeamMember, r.TeamID, r.UserID), + store.MemberOwner(store.ScopeOrgMember, r.OrgID, r.UserID)) + } + for _, o := range owners { + if o.ID == "" || strings.HasPrefix(o.ID, "/") { + continue + } if k := o.CounterKey(); k != "" { spend[k] += r.Micros } diff --git a/internal/accounting/people_test.go b/internal/accounting/people_test.go new file mode 100644 index 0000000..abb0db1 --- /dev/null +++ b/internal/accounting/people_test.go @@ -0,0 +1,126 @@ +package accounting + +import ( + "context" + "errors" + "log/slog" + "path/filepath" + "testing" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/auth" + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +// peopleStore has Acme/ml/training with two members, ayse and deniz. +func peopleStore(t *testing.T) (*store.DB, string) { + t.Helper() + ctx := context.Background() + s, err := store.OpenSQLite(ctx, filepath.Join(t.TempDir(), "p.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = s.Close() }) + org, _ := s.CreateOrg(ctx, "Acme") + team, _ := s.CreateTeam(ctx, org, "ml") + proj, _ := s.CreateProject(ctx, team, "training") + for _, id := range []string{"ayse", "deniz"} { + if err := s.CreateUser(ctx, store.User{ID: id, Email: id + "@x", Name: id, + PasswordHash: "h", State: store.UserPending, CreatedAt: time.Now()}); err != nil { + t.Fatal(err) + } + if err := s.ApproveUser(ctx, id, proj, "test", time.Now()); err != nil { + t.Fatal(err) + } + } + return s, proj +} + +// keyFor makes a key for a person and resolves it the way authentication does. +func keyFor(t *testing.T, s *store.DB, id, user, project string) auth.Principal { + t.Helper() + ctx := context.Background() + _, k, _ := store.NewKey(id, id, project) + if err := s.CreateUserKey(ctx, user, k); err != nil { + t.Fatal(err) + } + e, err := s.Resolve(ctx, id) + if err != nil { + t.Fatalf("resolving %s: %v", id, err) + } + return auth.Principal{KeyID: id, UserID: e.UserID, ProjectID: e.ProjectID, Limits: e} +} + +func TestAPersonsLimitIsOnePoolAcrossTheirKeys(t *testing.T) { + s, proj := peopleStore(t) + if err := s.PutLimits(context.Background(), + store.Limits{Scope: store.ScopeUser, ScopeID: "ayse", RPM: intp(2)}); err != nil { + t.Fatal(err) + } + a := New(pricing(t), nil, slog.New(slog.DiscardHandler)) + personal := keyFor(t, s, "k-personal", "ayse", "") + inProject := keyFor(t, s, "k-project", "ayse", proj) + + // Two requests, one on each key, use up the person's two a minute. + for _, p := range []auth.Principal{personal, inProject} { + if _, err := a.Check(p, "free", 1); err != nil { + t.Fatalf("%s: %v", p.KeyID, err) + } + } + if _, err := a.Check(personal, "free", 1); !errors.Is(err, ErrRateLimited) { + t.Errorf("a third request across her keys was allowed: %v", err) + } + // Deniz is somebody else. + if _, err := a.Check(keyFor(t, s, "k-deniz", "deniz", proj), "free", 1); err != nil { + t.Errorf("another person was limited by hers: %v", err) + } +} + +func TestAPerMemberLimitCountsEachMemberApart(t *testing.T) { + s, proj := peopleStore(t) + if err := s.PutLimits(context.Background(), + store.Limits{Scope: store.ScopeProjectMember, ScopeID: proj, RPM: intp(1)}); err != nil { + t.Fatal(err) + } + a := New(pricing(t), nil, slog.New(slog.DiscardHandler)) + ayse := keyFor(t, s, "k-a", "ayse", proj) + deniz := keyFor(t, s, "k-d", "deniz", proj) + + if _, err := a.Check(ayse, "free", 1); err != nil { + t.Fatal(err) + } + if _, err := a.Check(ayse, "free", 1); !errors.Is(err, ErrRateLimited) { + t.Errorf("her second request passed a one-a-minute per-member limit: %v", err) + } + // Her share is hers: Deniz still has his. + if _, err := a.Check(deniz, "free", 1); err != nil { + t.Errorf("one member's use counted against another: %v", err) + } + // And outside the project it does not apply at all. + if _, err := a.Check(keyFor(t, s, "k-a-own", "ayse", ""), "free", 1); err != nil { + t.Errorf("a project's per-member limit held her personal key: %v", err) + } +} + +func TestAPersonsBudgetSurvivesARestart(t *testing.T) { + s, _ := peopleStore(t) + ctx := context.Background() + if err := s.PutLimits(ctx, store.Limits{Scope: store.ScopeUser, ScopeID: "ayse", + Budget: i64p(1000), BudgetPeriod: store.PeriodMonth}); err != nil { + t.Fatal(err) + } + // Spent before the restart, personally and on the chat page. + if err := s.WriteUsage(ctx, []store.Usage{ + {At: time.Now(), KeyID: "k-old", UserID: "ayse", CostMicros: 600, Model: "paid", ServedModel: "paid"}, + {At: time.Now(), KeyID: "session:ayse", UserID: "ayse", CostMicros: 500, Model: "paid", ServedModel: "paid"}, + }); err != nil { + t.Fatal(err) + } + a := New(pricing(t), nil, slog.New(slog.DiscardHandler)) + if err := a.RestoreBudgets(ctx, s, store.PeriodMonth); err != nil { + t.Fatal(err) + } + if _, err := a.Check(keyFor(t, s, "k-new", "ayse", ""), "paid", 1); !errors.Is(err, ErrBudgetSpent) { + t.Errorf("a restart forgot what she had spent: %v", err) + } +} diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 161da15..a158b2e 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -21,6 +21,9 @@ import ( type Principal struct { KeyID string KeyPrefix string + // UserID is the person the key belongs to, empty for a key an operator + // issued in a project. A personal key has a person and no project. + UserID string ProjectID string TeamID string OrgID string @@ -186,7 +189,7 @@ func (a *Authenticator) resolve(ctx context.Context, hash []byte) (Principal, er return Principal{}, err } return Principal{ - KeyID: k.ID, KeyPrefix: k.Prefix, ProjectID: eff.ProjectID, + KeyID: k.ID, KeyPrefix: k.Prefix, UserID: eff.UserID, ProjectID: eff.ProjectID, TeamID: eff.TeamID, OrgID: eff.OrgID, Band: eff.Band, RoutingProfile: eff.RoutingProfile, Limits: eff, diff --git a/internal/cli/keys.go b/internal/cli/keys.go index 881ffdb..6d9ccc2 100644 --- a/internal/cli/keys.go +++ b/internal/cli/keys.go @@ -85,7 +85,7 @@ func newKeyInitCmd(open opener) *cobra.Command { } func newKeyCreateCmd(open opener) *cobra.Command { - var project, band, profile, baseURL string + var project, band, profile, baseURL, forUser string var expires, claimTTL time.Duration var claim bool @@ -108,9 +108,22 @@ func newKeyCreateCmd(open opener) *cobra.Command { } defer s.Close() - p, err := resolveProject(cmd.Context(), s, project) - if err != nil { - return err + // Three kinds: an operator's key in a project, a person's key in a + // project they are a member of, and a person's personal key. + var owner store.User + if forUser != "" { + if owner, err = resolveUser(cmd.Context(), s, forUser); err != nil { + return err + } + } + var p store.ProjectPath + if project != "" || forUser == "" { + if p, err = resolveProject(cmd.Context(), s, project); err != nil { + return err + } + } + if p.ID == "" { + p.Path = "personal, " + owner.Email } secret, k, err := store.NewKey(randomID("key"), args[0], p.ID) if err != nil { @@ -121,7 +134,15 @@ func newKeyCreateCmd(open opener) *cobra.Command { if expires > 0 { k.ExpiresAt = k.CreatedAt.Add(expires) } - if err := s.CreateKey(cmd.Context(), k); err != nil { + if owner.ID != "" { + err = s.CreateUserKey(cmd.Context(), owner.ID, k) + } else { + err = s.CreateKey(cmd.Context(), k) + } + if errors.Is(err, store.ErrNotMember) { + return fmt.Errorf("%s is not a member of %s; `llmgw users join` first", owner.Email, p.Path) + } + if err != nil { return fmt.Errorf("creating the key in %s: %w", p.Path, err) } @@ -161,6 +182,8 @@ func newKeyCreateCmd(open opener) *cobra.Command { } cmd.Flags().StringVar(&project, "project", "", "project the key belongs to: id, path (Acme/platform/gateway) or unique name") + cmd.Flags().StringVar(&forUser, "for", "", + "a person the key belongs to (id or email); without --project it is their personal key") cmd.Flags().StringVar(&band, "band", "", "admission band; empty inherits") cmd.Flags().StringVar(&profile, "profile", "", "routing profile; empty inherits") cmd.Flags().DurationVar(&expires, "expires-in", 0, "lifetime; 0 never expires") @@ -200,8 +223,12 @@ func newKeyListCmd(open opener) *cobra.Command { fmt.Fprintln(w, "ID\tNAME\tKEY\tPROJECT\tOWNER\tSTATE\tLAST USED") now := time.Now() for _, r := range rows { + where := r.ProjectPath + if where == "" { + where = "personal" + } fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", r.ID, r.Name, r.Prefix, - r.ProjectPath, dash(r.OwnerEmail), keyState(r.Key, now), ago(r.LastUsedAt, now)) + where, dash(r.OwnerEmail), keyState(r.Key, now), ago(r.LastUsedAt, now)) } return w.Flush() } @@ -260,6 +287,7 @@ func newKeyRevokeCmd(open opener) *cobra.Command { func newLimitsCmd(open opener) *cobra.Command { var scope, id, period string + var perMember bool var rpm, tpm int var budget float64 var models []string @@ -298,14 +326,27 @@ func newLimitsCmd(open opener) *cobra.Command { return err } scopeID = p.ID + case store.ScopeUser: + u, err := resolveUser(cmd.Context(), s, id) + if err != nil { + return err + } + scopeID = u.ID case store.ScopeKey: if id == "" { return errors.New("--scope key needs --id, the key's id") } default: - return fmt.Errorf("--scope is org, team, project or key, not %q", scope) + return fmt.Errorf("--scope is org, team, project, user or key, not %q", scope) } - l := store.Limits{Scope: store.Scope(scope), ScopeID: scopeID} + level := store.Scope(scope) + if perMember { + // Written on the scope, applied to each member separately. + if level = store.MemberScope(level); level == "" { + return errors.New("--per-member is for --scope org, team or project") + } + } + l := store.Limits{Scope: level, ScopeID: scopeID} if cmd.Flags().Changed("rpm") { l.RPM = &rpm } @@ -330,11 +371,13 @@ func newLimitsCmd(open opener) *cobra.Command { if err := s.PutLimits(cmd.Context(), l); err != nil { return err } - fmt.Fprintf(cmd.OutOrStdout(), "limits set on %s %s\n", scope, scopeID) + fmt.Fprintf(cmd.OutOrStdout(), "limits set on %s %s\n", level, scopeID) return nil }, } - cmd.Flags().StringVar(&scope, "scope", "project", "org | team | project | key") + cmd.Flags().StringVar(&scope, "scope", "project", "org | team | project | user | key") + cmd.Flags().BoolVar(&perMember, "per-member", false, + "with org, team or project: a limit for each member there, not one shared by all") cmd.Flags().StringVar(&id, "id", "", "which one: id, path or unique name (a key's id for --scope key)") cmd.Flags().IntVar(&rpm, "rpm", 0, "requests per minute") cmd.Flags().IntVar(&tpm, "tpm", 0, "tokens per minute") diff --git a/internal/cli/org.go b/internal/cli/org.go index bc1211a..d4929b7 100644 --- a/internal/cli/org.go +++ b/internal/cli/org.go @@ -46,21 +46,40 @@ func newOrgTreeCmd(open dbOpener) *cobra.Command { if err != nil { return err } - var byProject map[string][]store.User + var members map[string][]string + var alone []store.User if people { users, err := s.Users(cmd.Context(), "") if err != nil { return err } - byProject = map[string][]store.User{} + all, err := s.AllMemberships(cmd.Context()) + if err != nil { + return err + } + email := map[string]string{} for _, u := range users { - byProject[u.ProjectID] = append(byProject[u.ProjectID], u) + email[u.ID] = u.Email + " " + string(u.State) + if u.IsAdmin { + email[u.ID] += " admin" + } + } + members = map[string][]string{} + in := map[string]bool{} + for _, m := range all { + members[string(m.Scope)+":"+m.ScopeID] = append(members[string(m.Scope)+":"+m.ScopeID], email[m.UserID]) + in[m.UserID] = true + } + for _, u := range users { + if !in[u.ID] { + alone = append(alone, u) + } } } - printTree(cmd.OutOrStdout(), tree, byProject) - if people && len(byProject[""]) > 0 { - fmt.Fprintf(cmd.OutOrStdout(), "\nwaiting for approval\n") - for _, u := range byProject[""] { + printTree(cmd.OutOrStdout(), tree, members) + if people && len(alone) > 0 { + fmt.Fprintf(cmd.OutOrStdout(), "\non their own\n") + for _, u := range alone { fmt.Fprintf(cmd.OutOrStdout(), " %s %s %s\n", u.Email, u.State, u.ID) } } @@ -71,36 +90,44 @@ func newOrgTreeCmd(open dbOpener) *cobra.Command { return cmd } -func printTree(w io.Writer, tree []store.TreeOrg, people map[string][]store.User) { +func printTree(w io.Writer, tree []store.TreeOrg, members map[string][]string) { if len(tree) == 0 { fmt.Fprintln(w, "no organizations yet; `llmgw org add org ` starts one") return } + list := func(indent, key string) { + for _, m := range members[key] { + fmt.Fprintf(w, "%s%s\n", indent, m) + } + } for _, o := range tree { - fmt.Fprintf(w, "%s (%s)\n", o.Name, o.ID) + fmt.Fprintf(w, "%s (%s)%s\n", o.Name, o.ID, membersNote(o.Members)) + list(" · ", "org:"+o.ID) if len(o.Teams) == 0 { fmt.Fprintln(w, " no teams") } for _, t := range o.Teams { - fmt.Fprintf(w, " %s (%s)\n", t.Name, t.ID) + fmt.Fprintf(w, " %s (%s)%s\n", t.Name, t.ID, membersNote(t.Members)) + list(" · ", "team:"+t.ID) if len(t.Projects) == 0 { fmt.Fprintln(w, " no projects") } for _, p := range t.Projects { fmt.Fprintf(w, " %s (%s) %s, %s\n", p.Name, p.ID, - countOf(p.Users, "account"), keysOf(p)) - for _, u := range people[p.ID] { - admin := "" - if u.IsAdmin { - admin = " admin" - } - fmt.Fprintf(w, " %s %s%s\n", u.Email, u.State, admin) - } + countOf(p.Users, "member"), keysOf(p)) + list(" · ", "project:"+p.ID) } } } } +func membersNote(n int) string { + if n == 0 { + return "" + } + return " " + countOf(n, "member") +} + func keysOf(p store.TreeProject) string { s := countOf(p.ActiveKeys, "key") if revoked := p.Keys - p.ActiveKeys; revoked > 0 { diff --git a/internal/cli/org_test.go b/internal/cli/org_test.go index dad4fad..7c526b7 100644 --- a/internal/cli/org_test.go +++ b/internal/cli/org_test.go @@ -51,15 +51,24 @@ func TestTheTreeIsManagedByNameNotId(t *testing.T) { mustRun(t, db, "keys", "create", "laptop", "--project", "gateway") out := mustRun(t, db, "org", "tree", "--people") - for _, want := range []string{"Acme", "gateway", "1 account, 1 key", "ayse@example.com"} { + for _, want := range []string{"Acme", "gateway", "1 member, 1 key", "ayse@example.com"} { if !strings.Contains(out, want) { t.Errorf("tree lacks %q:\n%s", want, out) } } - out = mustRun(t, db, "users", "move", "ayse@example.com", "--project", "research") + out = mustRun(t, db, "users", "join", "ayse@example.com", "--project", "research") if !strings.Contains(out, "Scania / platform / research") { - t.Errorf("move said: %s", out) + t.Errorf("join said: %s", out) + } + mustRun(t, db, "users", "leave", "ayse@example.com", "--project", "Acme/platform/gateway") + // She now uses the gateway through research alone; a personal key is hers. + mustRun(t, db, "keys", "create", "mine", "--for", "ayse@example.com") + if out := mustRun(t, db, "keys", "list", "--all"); !strings.Contains(out, "personal") { + t.Errorf("a personal key is not listed as one:\n%s", out) + } + if _, err := run(t, db, "keys", "create", "x", "--for", "ayse@example.com", "--project", "gateway"); err == nil { + t.Error("a key was made for her in a project she left") } mustRun(t, db, "org", "rename", "project", "research", "lab") diff --git a/internal/cli/serverinit_test.go b/internal/cli/serverinit_test.go index e991206..3532304 100644 --- a/internal/cli/serverinit_test.go +++ b/internal/cli/serverinit_test.go @@ -59,7 +59,8 @@ func TestInitLeavesAControlPlaneThatStarts(t *testing.T) { if err != nil { t.Fatal(err) } - if !u.IsAdmin || u.State != store.UserActive || u.ProjectID != "default" { + inDefault, _ := s.CanUseProject(context.Background(), u.ID, "default") + if !u.IsAdmin || u.State != store.UserActive || !inDefault { t.Errorf("administrator not ready to use: %+v", u) } if !strings.Contains(out.String(), "password: ") { diff --git a/internal/cli/users.go b/internal/cli/users.go index bfb93fc..f29a1ab 100644 --- a/internal/cli/users.go +++ b/internal/cli/users.go @@ -1,7 +1,9 @@ package cli import ( + "errors" "fmt" + "strings" "text/tabwriter" "time" @@ -33,7 +35,7 @@ func newUsersCmd() *cobra.Command { newUserStateCmd(open, "reinstate", store.UserActive, "Let a suspended account work again"), newUserCreateCmd(open), newUserResetPasswordCmd(open), - newUserMoveCmd(open), newUserDeleteCmd(open), + newUserJoinCmd(open), newUserLeaveCmd(open), newUserDeleteCmd(open), newUserAdminCmd(open, "promote", true, "Let an account see the cluster dashboard at /admin"), newUserAdminCmd(open, "demote", false, "Take away access to the cluster dashboard")) return cmd @@ -67,16 +69,28 @@ func newUserListCmd(open userOpener) *cobra.Command { return nil } + all, err := s.AllMemberships(cmd.Context()) + if err != nil { + return err + } + where := map[string][]string{} + for _, m := range all { + where[m.UserID] = append(where[m.UserID], m.Path) + } w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0) - fmt.Fprintln(w, "ID\tEMAIL\tNAME\tSTATE\tADMIN\tPROJECT\tSIGNED UP\tLAST LOGIN") + fmt.Fprintln(w, "ID\tEMAIL\tSTATE\tADMIN\tMEMBER OF\tSIGNED UP\tLAST LOGIN") now := time.Now() for _, u := range users { admin := "-" if u.IsAdmin { admin = "yes" } - fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", - u.ID, u.Email, u.Name, u.State, admin, dash(u.ProjectID), + member := "on their own" + if ms := where[u.ID]; len(ms) > 0 { + member = strings.Join(ms, "; ") + } + fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", + u.ID, u.Email, u.State, admin, member, ago(u.CreatedAt, now), ago(u.LastLoginAt, now)) } return w.Flush() @@ -91,9 +105,9 @@ func newUserApproveCmd(open userOpener) *cobra.Command { cmd := &cobra.Command{ Use: "approve ", Short: "Let an account sign in and issue its own keys", - Long: "The project is required: it decides which limits this person's keys\n" + - "inherit. An account approved into no project would pass every check and\n" + - "then fail at the first key it tried to make.", + Long: "A project is optional. Approved into nothing, a person uses the gateway on\n" + + "their own — personal keys and chat within their personal limits — and can\n" + + "be added to organizations, teams and projects with 'llmgw users join'.", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { s, err := open(cmd) @@ -106,20 +120,26 @@ func newUserApproveCmd(open userOpener) *cobra.Command { if err != nil { return err } - p, err := resolveProject(cmd.Context(), s, project) - if err != nil { - return err + var p store.ProjectPath + if project != "" { + if p, err = resolveProject(cmd.Context(), s, project); err != nil { + return err + } } if err := s.ApproveUser(cmd.Context(), u.ID, p.ID, by, time.Now()); err != nil { return fmt.Errorf("approving %s: %w", u.Email, err) } - fmt.Fprintf(cmd.OutOrStdout(), "%s approved into %s\n", u.Email, p.Path) + if p.ID == "" { + fmt.Fprintf(cmd.OutOrStdout(), "%s approved, on their own for now\n", u.Email) + } else { + fmt.Fprintf(cmd.OutOrStdout(), "%s approved into %s\n", u.Email, p.Path) + } cmd.PrintErrln("They can sign in and create their own keys from /profile.") return nil }, } cmd.Flags().StringVar(&project, "project", "", - "project the person's keys belong to: id, path (Acme/platform/gateway) or unique name") + "a project to put them in (optional): id, path (Acme/platform/gateway) or unique name") cmd.Flags().StringVar(&by, "by", "", "who approved, for the record") return cmd } @@ -254,9 +274,11 @@ func newUserCreateCmd(open userOpener) *cobra.Command { } defer s.Close() - p, err := resolveProject(cmd.Context(), s, project) - if err != nil { - return err + var p store.ProjectPath + if project != "" { + if p, err = resolveProject(cmd.Context(), s, project); err != nil { + return err + } } hash, err := account.HashPassword(password) if err != nil { @@ -274,27 +296,64 @@ func newUserCreateCmd(open userOpener) *cobra.Command { return err } fmt.Fprintf(cmd.OutOrStdout(), "%s\n", id) - cmd.PrintErrf("%s created and approved into %s\n", args[0], p.Path) + if p.ID == "" { + cmd.PrintErrf("%s created and approved, on their own for now\n", args[0]) + } else { + cmd.PrintErrf("%s created and approved into %s\n", args[0], p.Path) + } return nil }, } cmd.Flags().StringVar(&project, "project", "", - "project the person's keys belong to: id, path or unique name (optional when there is one)") + "a project to put them in (optional): id, path or unique name") cmd.Flags().StringVar(&password, "password", "", "initial password") return cmd } -func newUserMoveCmd(open userOpener) *cobra.Command { - var project string +// scopeFlags names one org, team or project by whichever flag is given. +type scopeFlags struct{ org, team, project string } + +func (f *scopeFlags) bind(cmd *cobra.Command) { + cmd.Flags().StringVar(&f.org, "org", "", "an organization: id or name") + cmd.Flags().StringVar(&f.team, "team", "", "a team: id, path (Acme/ml) or unique name") + cmd.Flags().StringVar(&f.project, "project", "", "a project: id, path (Acme/ml/training) or unique name") +} + +func (f *scopeFlags) resolve(cmd *cobra.Command, s *store.DB) (store.Scope, string, string, error) { + n := 0 + for _, v := range []string{f.org, f.team, f.project} { + if v != "" { + n++ + } + } + if n != 1 { + return "", "", "", fmt.Errorf("give exactly one of --org, --team or --project") + } + ctx := cmd.Context() + switch { + case f.org != "": + o, err := resolveOrg(ctx, s, f.org) + return store.ScopeOrg, o.ID, o.Name, err + case f.team != "": + t, err := resolveTeam(ctx, s, f.team) + return store.ScopeTeam, t.ID, t.OrgName + " / " + t.Name, err + default: + p, err := resolveProject(ctx, s, f.project) + return store.ScopeProject, p.ID, p.Path, err + } +} + +func newUserJoinCmd(open userOpener) *cobra.Command { + var where scopeFlags + var role, by string cmd := &cobra.Command{ - Use: "move --project ", - Short: "Put an approved account in another project, with the keys it made", - Long: "The person's own keys go with them, so they stop spending the old project's\n" + - "budget. Keys an operator issued in the old project stay there. Usage already\n" + - "recorded is not rewritten. A running server picks the change up when its key\n" + - "cache expires (auth.cache_ttl).", - Example: " llmgw users move ayse@example.com --project Acme/ml/research", - Args: cobra.ExactArgs(1), + Use: "join --org|--team|--project ", + Short: "Make a person a member of an organization, team or project", + Long: "A person can be a member of any number of them. A member of a team or\n" + + "organization may use every project in it.", + Example: " llmgw users join ayse@example.com --team Acme/ml\n" + + " llmgw users join ayse@example.com --project Scania/research/vision", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { s, err := open(cmd) if err != nil { @@ -305,21 +364,59 @@ func newUserMoveCmd(open userOpener) *cobra.Command { if err != nil { return err } - p, err := resolveProject(cmd.Context(), s, project) + scope, id, label, err := where.resolve(cmd, s) if err != nil { return err } - moved, err := s.MoveUser(cmd.Context(), u.ID, p.ID) - if err != nil { + if err := s.AddMembership(cmd.Context(), u.ID, scope, id, role, by, time.Now()); err != nil { return err } - fmt.Fprintf(cmd.OutOrStdout(), "%s is now in %s (%s moved with them)\n", - u.Email, p.Path, countOf(moved, "key")) + fmt.Fprintf(cmd.OutOrStdout(), "%s is a %s of %s\n", u.Email, role, label) return nil }, } - cmd.Flags().StringVar(&project, "project", "", "where they go: id, path or unique name") - _ = cmd.MarkFlagRequired("project") + where.bind(cmd) + cmd.Flags().StringVar(&role, "role", store.RoleMember, "member (manager is reserved)") + cmd.Flags().StringVar(&by, "by", "", "who added them, for the record") + return cmd +} + +func newUserLeaveCmd(open userOpener) *cobra.Command { + var where scopeFlags + cmd := &cobra.Command{ + Use: "leave --org|--team|--project ", + Short: "Take a person out of an organization, team or project", + Long: "Keys they made in a project they can no longer use are revoked; a project\n" + + "they still reach through another membership keeps them. Their personal\n" + + "keys are not touched.", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + s, err := open(cmd) + if err != nil { + return err + } + defer s.Close() + u, err := resolveUser(cmd.Context(), s, args[0]) + if err != nil { + return err + } + scope, id, label, err := where.resolve(cmd, s) + if err != nil { + return err + } + revoked, err := s.RemoveMembership(cmd.Context(), u.ID, scope, id, time.Now()) + if errors.Is(err, store.ErrNotFound) { + return fmt.Errorf("%s is not a member of %s itself", u.Email, label) + } + if err != nil { + return err + } + fmt.Fprintf(cmd.OutOrStdout(), "%s is no longer in %s (%s revoked)\n", + u.Email, label, countOf(revoked, "key")) + return nil + }, + } + where.bind(cmd) return cmd } diff --git a/internal/config/config.go b/internal/config/config.go index e00a51e..e1ce404 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -204,6 +204,10 @@ type Auth struct { // means accounts are made with `llmgw users create` instead. SignupOpen bool `koanf:"signup_open"` + // Personal is what a person may do on their own, outside any project: make + // personal keys and use the chat page personally, within these limits. + Personal Personal `koanf:"personal"` + // Pricing is what each model costs, for budgets. A model absent from the // table costs whatever DefaultPrice says, which is nothing unless set. Pricing map[string]Price `koanf:"pricing"` @@ -218,6 +222,22 @@ type Auth struct { BufferSize int `koanf:"buffer_size"` } +// Personal configures use that belongs to a person rather than a project. +// +// On by default: a person need not belong to anything to use the gateway. The +// limits are each person's own pool across all their personal keys and their +// personal chat, and apply only where nobody wrote a limit for that person. +type Personal struct { + // Disabled refuses personal keys and personal chat; people then use the + // gateway only through projects they are members of. + Disabled bool `koanf:"disabled"` + RPM int `koanf:"rpm"` + TPM int `koanf:"tpm"` + // Budget is in whole units per Period ("day" or "month"). + Budget float64 `koanf:"budget"` + Period string `koanf:"period"` +} + // Price is a model's cost in micro-units per thousand tokens. // // What a unit means is the operator's choice — self-hosted models have no diff --git a/internal/router/chat.go b/internal/router/chat.go index bbf3cc9..99f0643 100644 --- a/internal/router/chat.go +++ b/internal/router/chat.go @@ -2,6 +2,7 @@ package router import ( "bytes" + "context" "encoding/json" "errors" "io" @@ -28,7 +29,15 @@ import ( // of the gateway enforces. func (r *Router) registerChat(mux *http.ServeMux, pages *web.AccountPages) { mux.HandleFunc("GET /chat", pages.Guard(func(w http.ResponseWriter, req *http.Request, user store.User) { - web.Chat(user, r.modelsFor(req, user), version.Version)(w, req) + contexts, _ := r.chatContexts(req.Context(), user) + current, err := r.pickContext(req.Context(), user, req.URL.Query().Get("context")) + if err != nil { + current = "" + } + if current == "" && !r.personalOff { + current = personalContext + } + web.Chat(user, r.modelsFor(req, user), contexts, current, version.Version)(w, req) })) mux.HandleFunc("POST /chat/send", pages.Guard(r.chatSend)) // Behind the same guard as the rest of the page: it takes a POST body and @@ -42,12 +51,6 @@ func (r *Router) registerChat(mux *http.ServeMux, pages *web.AccountPages) { // chatSend dispatches one turn on behalf of a signed-in person. func (r *Router) chatSend(w http.ResponseWriter, req *http.Request, user store.User) { - principal, err := r.principalFor(req, user) - if err != nil { - http.Error(w, "your account has no project to bill this to", http.StatusForbidden) - return - } - body, err := io.ReadAll(io.LimitReader(req.Body, r.cfg.MaxBodyBytes)) if err != nil { http.Error(w, "could not read the request", http.StatusBadRequest) @@ -57,11 +60,19 @@ func (r *Router) chatSend(w http.ResponseWriter, req *http.Request, user store.U var in struct { Model string `json:"model"` Messages []json.RawMessage `json:"messages"` + // Context is what this turn spends against: a project id, or + // "personal". Checked here, not trusted from the page. + Context string `json:"context"` } if err := json.Unmarshal(body, &in); err != nil || in.Model == "" || len(in.Messages) == 0 { http.Error(w, "a model and at least one message are required", http.StatusBadRequest) return } + principal, err := r.principalFor(req, user, in.Context) + if err != nil { + http.Error(w, err.Error(), http.StatusForbidden) + return + } // Rebuilt rather than forwarded, so the page cannot set fields it has no // business setting — a stream:false that would hang the reader, a @@ -115,23 +126,83 @@ func chatTokenCap(m catalog.Model) int { return chatMaxTokens } -// principalFor builds the identity a signed-in person acts under. +// personalContext is how the page names spending as oneself, outside any +// project. +const personalContext = "personal" + +// chatContexts is what a person may spend against on this page: the projects +// they can use, and themselves when personal use is allowed. +func (r *Router) chatContexts(ctx context.Context, user store.User) ([]web.ChatContext, error) { + var out []web.ChatContext + projects, err := r.store.ProjectsFor(ctx, user.ID) + if err != nil { + return nil, err + } + for _, p := range projects { + out = append(out, web.ChatContext{Value: p.ID, Label: p.Path}) + } + if !r.personalOff { + out = append(out, web.ChatContext{Value: personalContext, Label: "Personal"}) + } + return out, nil +} + +// pickContext resolves what a request asked to spend against, checking the +// person may; with nothing asked, their first project, else themselves. +// Returns the project id, empty for personal. +func (r *Router) pickContext(ctx context.Context, user store.User, asked string) (string, error) { + switch { + case asked == personalContext: + if r.personalOff { + return "", errors.New("personal use is off on this gateway") + } + return "", nil + case asked != "": + ok, err := r.store.CanUseProject(ctx, user.ID, asked) + if err != nil { + return "", err + } + if !ok { + return "", errors.New("you are not a member of that project") + } + return asked, nil + } + projects, err := r.store.ProjectsFor(ctx, user.ID) + if err != nil { + return "", err + } + if len(projects) > 0 { + return projects[0].ID, nil + } + if r.personalOff { + return "", errors.New("you are in no project, and personal use is off on this gateway") + } + return "", nil +} + +// principalFor builds the identity a signed-in person acts under, spending +// against a project they may use or, with none, themselves. // // The key id is synthetic and marked as a session, because there is no key: it // keeps usage rows attributable and rate limits per person, while making it // obvious in the usage table that these requests came from the page rather than // from a credential somebody holds. -func (r *Router) principalFor(req *http.Request, user store.User) (auth.Principal, error) { - if r.store == nil || user.ProjectID == "" { - return auth.Principal{}, errors.New("no project") +func (r *Router) principalFor(req *http.Request, user store.User, asked string) (auth.Principal, error) { + if r.store == nil { + return auth.Principal{}, errors.New("no store") } - eff, err := r.store.ResolveProject(req.Context(), user.ProjectID) + project, err := r.pickContext(req.Context(), user, asked) + if err != nil { + return auth.Principal{}, err + } + eff, err := r.store.ResolveFor(req.Context(), "session:"+user.ID, user.ID, project) if err != nil { return auth.Principal{}, err } return auth.Principal{ KeyID: "session:" + user.ID, KeyPrefix: "session", + UserID: user.ID, ProjectID: eff.ProjectID, TeamID: eff.TeamID, OrgID: eff.OrgID, @@ -159,7 +230,7 @@ func (r *Router) principalFor(req *http.Request, user store.User) (auth.Principa // by name, "big-model" came first and every visitor who pressed Send without // touching the picker got placeholder text. func (r *Router) modelsFor(req *http.Request, user store.User) []web.ChatModel { - principal, err := r.principalFor(req, user) + principal, err := r.principalFor(req, user, req.URL.Query().Get("context")) if err != nil { return nil } diff --git a/internal/router/handlers.go b/internal/router/handlers.go index 6734fc5..7d439ee 100644 --- a/internal/router/handlers.go +++ b/internal/router/handlers.go @@ -145,6 +145,8 @@ func (r *Router) Handler() http.Handler { // So the example on the profile page names a model that is actually // running, rather than a placeholder that gets sent as-is. pages.ServedModels(r.exampleModels) + pages.SetPersonalOff(r.personalOff) + pages.SetPersonalDefaults(r.personalDefaults) pages.Register(mux) r.registerChat(mux, pages) @@ -211,6 +213,7 @@ type parsedRequest struct { Band string // KeyID, ProjectID and Tenant are empty when authentication is off. KeyID string + UserID string ProjectID string Tenant string ProfileOverride string @@ -416,7 +419,7 @@ func (r *Router) account(req *parsedRequest, res dispatchResult, status int, too return } u := store.Usage{ - At: time.Now(), KeyID: req.KeyID, ProjectID: req.ProjectID, + At: time.Now(), KeyID: req.KeyID, UserID: req.UserID, ProjectID: req.ProjectID, Model: req.Model, ServedModel: res.model, Status: status, LatencyMs: took.Milliseconds(), } @@ -489,6 +492,7 @@ func (r *Router) parse(req *http.Request, path string) (*parsedRequest, error) { p.Band = principal.Band p.ProfileOverride = principal.RoutingProfile p.KeyID = principal.KeyID + p.UserID = principal.UserID p.ProjectID = principal.ProjectID // Tenant fairness within a band is per key rather than per session, so // one key cannot take a whole band by opening many sessions. diff --git a/internal/router/router.go b/internal/router/router.go index 02f39f2..408108e 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "log/slog" + "math" "net/http" "slices" "strings" @@ -86,6 +87,12 @@ type Router struct { // anonymous means threads has no project to scope to: everything shares the // one pool named by anonymousProject. anonymous bool + // personalOff refuses personal use: the chat page then only offers the + // projects a person is a member of. + personalOff bool + // personalDefaults is what personal use is held to when nobody wrote a + // limit for the person, for the quota page to say so. + personalDefaults *storepkg.Limits // ownsThreads is true when threads is not store and so must be closed here. ownsThreads bool recorder *accounting.Recorder @@ -237,6 +244,9 @@ func New(cfg config.Server, log *slog.Logger) (*Router, error) { defTPM = &cfg.Auth.DefaultTPM } st.SetDefaultLimits(defRPM, defTPM) + st.SetPersonalDefaults(personalLimits(cfg.Auth.Personal)) + r.personalOff = cfg.Auth.Personal.Disabled + r.personalDefaults = personalLimits(cfg.Auth.Personal) r.auth = auth.New(st, auth.Config{ TTL: cfg.Auth.CacheTTL, @@ -699,3 +709,28 @@ func (r *Router) dispatchOne(ctx context.Context, w http.ResponseWriter, req *pa func without(eps []endpoint.Snapshot, id string) []endpoint.Snapshot { return slices.DeleteFunc(slices.Clone(eps), func(e endpoint.Snapshot) bool { return e.ID == id }) } + +// personalLimits turns the personal block into limits, nil when it sets none. +func personalLimits(p config.Personal) *storepkg.Limits { + var l storepkg.Limits + if p.RPM > 0 { + v := p.RPM + l.RPM = &v + } + if p.TPM > 0 { + v := p.TPM + l.TPM = &v + } + if p.Budget > 0 { + v := int64(math.Round(p.Budget * 1e6)) + l.Budget = &v + l.BudgetPeriod = storepkg.PeriodMonth + if p.Period == "day" { + l.BudgetPeriod = storepkg.PeriodDay + } + } + if l.RPM == nil && l.TPM == nil && l.Budget == nil { + return nil + } + return &l +} diff --git a/internal/router/threads.go b/internal/router/threads.go index 0bedc33..871d56b 100644 --- a/internal/router/threads.go +++ b/internal/router/threads.go @@ -298,6 +298,14 @@ const anonymousProject = "_anonymous" var errThreadsNeedAuth = errors.New( "a conversation needs an API key: without one there is no project to keep it in") +// errThreadsPersonal is what a personal key meets. Conversations and notes are +// kept per project, with the project's retention; a personal key has none, and +// the shared pool an unauthenticated gateway uses is not somewhere a signed-in +// person's history belongs. +var errThreadsPersonal = errors.New( + "conversations and notes are kept per project, and this is a personal key; " + + "use a key made in a project that keeps them") + // errThreadsOff is what a thread header meets when nothing is configured to // keep threads. Refused rather than ignored: ignoring it sends the model a // conversation with no history, and it answers confidently about something it @@ -317,6 +325,8 @@ func (r *Router) threadProject(callerProject string) (string, error) { return "", errThreadsOff case callerProject != "": return callerProject, nil + case r.auth != nil: + return "", errThreadsPersonal case r.anonymous: return anonymousProject, nil default: diff --git a/internal/store/copy.go b/internal/store/copy.go index c58679d..6efa537 100644 --- a/internal/store/copy.go +++ b/internal/store/copy.go @@ -23,6 +23,8 @@ var copyOrder = []string{ // users is created in a later one than api_keys even though api_keys // points at it — so the copy failed on a foreign key three tables in. "users", + // After users and the three levels of the tree, which it points at. + "memberships", "api_keys", "key_claims", "sessions", diff --git a/internal/store/copy_test.go b/internal/store/copy_test.go index 050a92a..295c959 100644 --- a/internal/store/copy_test.go +++ b/internal/store/copy_test.go @@ -40,7 +40,13 @@ func TestEveryTableIsInTheCopyOrder(t *testing.T) { if len(declared) == 0 { t.Fatal("found no tables in the migrations, so this test proves nothing") } + renamed := renamedTables(t) for _, name := range declared { + if to, ok := renamed[name]; ok { + // A table rebuilt under a temporary name and renamed back is the + // table it became, not another one. + name = to + } if slices.Contains(copyOrder, name) { continue } @@ -204,10 +210,16 @@ func TestTheCopyOrderSatisfiesTheForeignKeys(t *testing.T) { body += string(b) } - // Table bodies, so a REFERENCES is attributed to the table holding it. + // Table bodies, so a REFERENCES is attributed to the table holding it, + // under the name it ends with for one rebuilt and renamed. + renamed := renamedTables(t) for _, m := range regexpCreateTable.FindAllStringSubmatch(body, -1) { + table := m[1] + if to, ok := renamed[table]; ok { + table = to + } for _, r := range regexpReferences.FindAllStringSubmatch(m[2], -1) { - note(m[1], r[1]) + note(table, r[1]) } } // And columns added later, which is exactly how api_keys got its user_id. @@ -235,8 +247,29 @@ var ( regexpCreateTable = regexp.MustCompile(`(?s)CREATE TABLE (\w+)\s*\((.*?)\n\);`) regexpReferences = regexp.MustCompile(`REFERENCES\s+(\w+)`) regexpAlterRef = regexp.MustCompile(`ALTER TABLE (\w+) ADD COLUMN[^;]*REFERENCES\s+(\w+)`) + regexpRename = regexp.MustCompile(`ALTER TABLE (\w+) RENAME TO (\w+)`) ) +// renamedTables maps a table a migration renames to the name it gets. +func renamedTables(t *testing.T) map[string]string { + t.Helper() + out := map[string]string{} + entries, err := migrationFS.ReadDir("migrations/sqlite") + if err != nil { + t.Fatal(err) + } + for _, e := range entries { + b, err := migrationFS.ReadFile("migrations/sqlite/" + e.Name()) + if err != nil { + t.Fatal(err) + } + for _, m := range regexpRename.FindAllStringSubmatch(string(b), -1) { + out[m[1]] = m[2] + } + } + return out +} + func TestAnEmptyBlobDoesNotBecomeNullInTheCopy(t *testing.T) { // key_claims.sealed is emptied the moment a claim is used, so a claimed // row holds an empty blob in a NOT NULL column. An empty blob scans as a diff --git a/internal/store/hierarchy.go b/internal/store/hierarchy.go index 813eee2..cc9d128 100644 --- a/internal/store/hierarchy.go +++ b/internal/store/hierarchy.go @@ -17,19 +17,24 @@ var ErrNotEmpty = errors.New("not empty") // whole. type TreeOrg struct { ID, Name string - Teams []TreeTeam + // Members counts people who are members of the organization itself. + Members int + Teams []TreeTeam } // TreeTeam is a team and its projects. type TreeTeam struct { ID, Name string + // Members counts people who are members of the team itself. + Members int Projects []TreeProject } // TreeProject is a project and what uses it. type TreeProject struct { ID, Name string - // Users counts accounts in the project, whatever their state. + // Users counts the project's own members, whatever their state; people + // who reach it through its team or organization are counted there. Users int // Keys counts every key ever issued in it; ActiveKeys those not revoked. Keys, ActiveKeys int @@ -52,7 +57,7 @@ func (s *DB) Tree(ctx context.Context) ([]TreeOrg, error) { } projects, err := queryAll(ctx, s.db, ` SELECT p.id, p.name, p.team_id, - (SELECT COUNT(*) FROM users u WHERE u.project_id = p.id), + (SELECT COUNT(*) FROM memberships m WHERE m.scope_type = 'project' AND m.scope_id = p.id), (SELECT COUNT(*) FROM api_keys k WHERE k.project_id = p.id), (SELECT COUNT(*) FROM api_keys k WHERE k.project_id = p.id AND k.revoked_at IS NULL) FROM projects p ORDER BY p.name`, nil, func(rows *sql.Rows) (proj, error) { @@ -67,13 +72,34 @@ func (s *DB) Tree(ctx context.Context) ([]TreeOrg, error) { for _, p := range projects { byTeam[p.teamID] = append(byTeam[p.teamID], p.TreeProject) } + members := map[string]int{} + counts, err := queryAll(ctx, s.db, + `SELECT scope_type || ':' || scope_id, COUNT(*) FROM memberships + WHERE scope_type IN ('org', 'team') GROUP BY scope_type, scope_id`, nil, + func(rows *sql.Rows) (struct { + k string + n int + }, error) { + var c struct { + k string + n int + } + return c, rows.Scan(&c.k, &c.n) + }) + if err != nil { + return nil, err + } + for _, c := range counts { + members[c.k] = c.n + } byOrg := map[string][]TreeTeam{} for _, t := range teams { - byOrg[t.OrgID] = append(byOrg[t.OrgID], TreeTeam{ID: t.ID, Name: t.Name, Projects: byTeam[t.ID]}) + byOrg[t.OrgID] = append(byOrg[t.OrgID], TreeTeam{ID: t.ID, Name: t.Name, + Members: members["team:"+t.ID], Projects: byTeam[t.ID]}) } out := make([]TreeOrg, 0, len(orgs)) for _, o := range orgs { - out = append(out, TreeOrg{ID: o.ID, Name: o.Name, Teams: byOrg[o.ID]}) + out = append(out, TreeOrg{ID: o.ID, Name: o.Name, Members: members["org:"+o.ID], Teams: byOrg[o.ID]}) } return out, nil } @@ -150,13 +176,15 @@ func (s *DB) MoveProject(ctx context.Context, projectID, teamID string) error { // DeleteOrg removes an organization with no teams, and its limits. func (s *DB) DeleteOrg(ctx context.Context, id string) error { return s.deleteEmpty(ctx, ScopeOrg, id, `DELETE FROM organizations WHERE id = ?`, - emptyCheck{`SELECT COUNT(*) FROM teams WHERE org_id = ?`, "team"}) + emptyCheck{`SELECT COUNT(*) FROM teams WHERE org_id = ?`, "team"}, + emptyCheck{`SELECT COUNT(*) FROM memberships WHERE scope_type = 'org' AND scope_id = ?`, "member"}) } // DeleteTeam removes a team with no projects, and its limits. func (s *DB) DeleteTeam(ctx context.Context, id string) error { return s.deleteEmpty(ctx, ScopeTeam, id, `DELETE FROM teams WHERE id = ?`, - emptyCheck{`SELECT COUNT(*) FROM projects WHERE team_id = ?`, "project"}) + emptyCheck{`SELECT COUNT(*) FROM projects WHERE team_id = ?`, "project"}, + emptyCheck{`SELECT COUNT(*) FROM memberships WHERE scope_type = 'team' AND scope_id = ?`, "member"}) } // DeleteProject removes a project nobody is in and no key was ever issued in. @@ -166,7 +194,7 @@ func (s *DB) DeleteTeam(ctx context.Context, id string) error { // such a project can be renamed or left, not removed. func (s *DB) DeleteProject(ctx context.Context, id string) error { return s.deleteEmpty(ctx, ScopeProject, id, `DELETE FROM projects WHERE id = ?`, - emptyCheck{`SELECT COUNT(*) FROM users WHERE project_id = ?`, "account"}, + emptyCheck{`SELECT COUNT(*) FROM memberships WHERE scope_type = 'project' AND scope_id = ?`, "member"}, emptyCheck{`SELECT COUNT(*) FROM api_keys WHERE project_id = ?`, "key"}) } @@ -190,7 +218,8 @@ func (s *DB) deleteEmpty(ctx context.Context, scope Scope, id, del string, check return fmt.Errorf("%w: it still has %s", ErrNotEmpty, strings.Join(busy, " and ")) } if _, err := c.ExecContext(ctx, - `DELETE FROM limits WHERE scope_type = ? AND scope_id = ?`, string(scope), id); err != nil { + `DELETE FROM limits WHERE scope_type IN (?, ?) AND scope_id = ?`, + string(scope), string(MemberScope(scope)), id); err != nil { return err } res, err := c.ExecContext(ctx, del, id) @@ -204,43 +233,6 @@ func (s *DB) deleteEmpty(ctx context.Context, scope Scope, id, del string, check }) } -// MoveUser puts an approved account in another project, with the keys it -// made for itself, and returns how many keys moved. -// -// The keys move because they are the person's: left behind they would keep -// spending the old project's budget under the new project's name. Keys an -// operator issued in the project belong to the project and stay. Usage already -// recorded stays where it was spent. -func (s *DB) MoveUser(ctx context.Context, userID, projectID string) (int, error) { - if err := s.exists(ctx, "project", `SELECT COUNT(*) FROM projects WHERE id = ?`, projectID); err != nil { - return 0, err - } - var moved int - err := s.inTx(ctx, func(c conn) error { - var state string - if err := c.QueryRowContext(ctx, `SELECT state FROM users WHERE id = ?`, userID).Scan(&state); err != nil { - if errors.Is(err, sql.ErrNoRows) { - return ErrNotFound - } - return err - } - if UserState(state) == UserPending { - return errors.New("this account is waiting for approval; approve it into a project instead") - } - if _, err := c.ExecContext(ctx, `UPDATE users SET project_id = ? WHERE id = ?`, projectID, userID); err != nil { - return err - } - res, err := c.ExecContext(ctx, `UPDATE api_keys SET project_id = ? WHERE user_id = ?`, projectID, userID) - if err != nil { - return err - } - n, _ := res.RowsAffected() - moved = int(n) - return nil - }) - return moved, err -} - // DeleteUser removes an account: its sessions end, its keys are revoked, and // the row goes. // @@ -278,17 +270,17 @@ type KeyRow struct { // AllKeys lists every key, newest first, with its project's path and owner. func (s *DB) AllKeys(ctx context.Context) ([]KeyRow, error) { - cols := make([]string, 0, 11) + cols := make([]string, 0, 12) for _, c := range strings.Split(keyColumns, ",") { cols = append(cols, "k."+strings.TrimSpace(c)) } return queryAll(ctx, s.db, ` SELECT `+strings.Join(cols, ", ")+`, - o.name || ' / ' || t.name || ' / ' || p.name, COALESCE(u.email, '') + COALESCE(o.name || ' / ' || t.name || ' / ' || p.name, ''), COALESCE(u.email, '') FROM api_keys k - JOIN projects p ON p.id = k.project_id - JOIN teams t ON t.id = p.team_id - JOIN organizations o ON o.id = t.org_id + LEFT JOIN projects p ON p.id = k.project_id + LEFT JOIN teams t ON t.id = p.team_id + LEFT JOIN organizations o ON o.id = t.org_id LEFT JOIN users u ON u.id = k.user_id ORDER BY k.created_at DESC`, nil, func(rows *sql.Rows) (KeyRow, error) { var r KeyRow diff --git a/internal/store/hierarchy_test.go b/internal/store/hierarchy_test.go index 9b15656..c60ea05 100644 --- a/internal/store/hierarchy_test.go +++ b/internal/store/hierarchy_test.go @@ -19,9 +19,16 @@ type fixture struct { } func newFixture(t *testing.T) fixture { + t.Helper() + return newFixtureOn(t, tenancyStore(t)) +} + +// newFixtureOn builds the fixture in a given store, so the same cases run on +// PostgreSQL as on SQLite. +func newFixtureOn(t *testing.T, db *DB) fixture { t.Helper() ctx := t.Context() - f := fixture{s: tenancyStore(t)} + f := fixture{s: db} must := func(id string, err error) string { t.Helper() if err != nil { @@ -73,48 +80,25 @@ func TestTreeCountsWhatUsesEachProject(t *testing.T) { } } -func TestMovingAUserTakesTheirKeysAndLeavesTheProjects(t *testing.T) { - f := newFixture(t) - ctx := t.Context() - moved, err := f.s.MoveUser(ctx, f.user, f.research) - if err != nil { - t.Fatal(err) - } - if moved != 1 { - t.Errorf("moved %d keys, want the one the person made", moved) - } - u, _ := f.s.UserByID(ctx, f.user) - if u.ProjectID != f.research { - t.Errorf("user is in %s", u.ProjectID) - } - keys, _ := f.s.Keys(ctx, f.gateway) - if len(keys) != 1 || keys[0].ID != f.operatorKey { - t.Errorf("the operator's key should stay in the project: %+v", keys) - } -} - -func TestAPendingAccountIsApprovedNotMoved(t *testing.T) { +func TestASignupNeverApprovedCannotBeReinstated(t *testing.T) { f := newFixture(t) ctx := t.Context() _ = f.s.CreateUser(ctx, User{ID: "usr-2", Email: "b@x", Name: "b", PasswordHash: "h", State: UserPending, CreatedAt: time.Now()}) - if _, err := f.s.MoveUser(ctx, "usr-2", f.gateway); err == nil { - t.Error("moving a pending account should be refused") - } -} - -func TestAnAccountWithNoProjectCannotBeMadeActive(t *testing.T) { - f := newFixture(t) - ctx := t.Context() - _ = f.s.CreateUser(ctx, User{ID: "usr-2", Email: "b@x", Name: "b", PasswordHash: "h", - State: UserPending, CreatedAt: time.Now()}) - // Rejected, then reinstated: the path that used to produce an active - // account whose first key failed. + // Suspended while pending, then reinstated: that would be an approval + // nobody made. if err := f.s.SetUserState(ctx, "usr-2", UserSuspended); err != nil { t.Fatal(err) } if err := f.s.SetUserState(ctx, "usr-2", UserActive); err == nil { - t.Error("an account with no project was made active") + t.Error("an account nobody approved was made active") + } + // One that was approved can be. + if err := f.s.SetUserState(ctx, f.user, UserSuspended); err != nil { + t.Fatal(err) + } + if err := f.s.SetUserState(ctx, f.user, UserActive); err != nil { + t.Errorf("an approved account could not be reinstated: %v", err) } } @@ -173,7 +157,7 @@ func TestOnlyEmptyThingsAreDeleted(t *testing.T) { f := newFixture(t) ctx := t.Context() err := f.s.DeleteProject(ctx, f.gateway) - if !errors.Is(err, ErrNotEmpty) || !strings.Contains(err.Error(), "1 account and 2 keys") { + if !errors.Is(err, ErrNotEmpty) || !strings.Contains(err.Error(), "1 member and 2 keys") { t.Errorf("deleting a used project: %v", err) } if err := f.s.DeleteTeam(ctx, f.platform); !errors.Is(err, ErrNotEmpty) { diff --git a/internal/store/memberships.go b/internal/store/memberships.go new file mode 100644 index 0000000..f665af8 --- /dev/null +++ b/internal/store/memberships.go @@ -0,0 +1,284 @@ +package store + +import ( + "context" + "database/sql" + "errors" + "fmt" + "time" +) + +// ErrNotMember means a person asked to use a project they are not in. +var ErrNotMember = errors.New("not a member of that project") + +// Roles a membership can carry. +const ( + RoleMember = "member" + // RoleManager is reserved for delegated administration of a scope. + RoleManager = "manager" +) + +// Membership is a person's place at one level of the tree. +type Membership struct { + UserID string + Scope Scope // org, team or project + ScopeID string + Role string + // Path is where it is: "Acme", "Acme / ml", "Acme / ml / training". + Path string + AddedAt time.Time + AddedBy string +} + +func validMembershipScope(s Scope) bool { + return s == ScopeOrg || s == ScopeTeam || s == ScopeProject +} + +// AddMembership puts a person in an organization, team or project. Adding one +// that exists changes nothing but its role. +func (s *DB) AddMembership(ctx context.Context, userID string, scope Scope, scopeID, role, by string, at time.Time) error { + if !validMembershipScope(scope) { + return fmt.Errorf("a membership is in an org, team or project, not %q", scope) + } + if role == "" { + role = RoleMember + } + table := map[Scope]string{ScopeOrg: "organizations", ScopeTeam: "teams", ScopeProject: "projects"}[scope] + if err := s.exists(ctx, string(scope), `SELECT COUNT(*) FROM `+table+` WHERE id = ?`, scopeID); err != nil { + return err + } + _, err := s.db.ExecContext(ctx, ` + INSERT INTO memberships (user_id, scope_type, scope_id, role, added_at, added_by) + VALUES (?, ?, ?, ?, ?, ?) + ON CONFLICT (user_id, scope_type, scope_id) DO UPDATE SET role = excluded.role`, + userID, string(scope), scopeID, role, millis(at), nullString(by)) + return err +} + +// RemoveMembership takes a person out of a scope and revokes the keys they +// made in any project they can no longer use, returning how many. +// +// Revoked rather than turned personal: a key made in a project spends that +// project's budget, and quietly moving it to the person would carry access +// out of the project the operator just removed them from. +func (s *DB) RemoveMembership(ctx context.Context, userID string, scope Scope, scopeID string, at time.Time) (int, error) { + var revoked int + err := s.inTx(ctx, func(c conn) error { + res, err := c.ExecContext(ctx, + `DELETE FROM memberships WHERE user_id = ? AND scope_type = ? AND scope_id = ?`, + userID, string(scope), scopeID) + if err != nil { + return err + } + if n, _ := res.RowsAffected(); n == 0 { + return ErrNotFound + } + res, err = c.ExecContext(ctx, ` + UPDATE api_keys SET revoked_at = ? + WHERE user_id = ? AND project_id IS NOT NULL AND revoked_at IS NULL + AND project_id NOT IN (`+usableProjects+`)`, + millis(at), userID, userID, userID, userID) + if err != nil { + return err + } + n, _ := res.RowsAffected() + revoked = int(n) + return nil + }) + return revoked, err +} + +// usableProjects selects every project a person may use: those they are a +// member of, and every project under a team or organization they are a member +// of. Takes the user id three times. +const usableProjects = ` + SELECT m.scope_id FROM memberships m + WHERE m.user_id = ? AND m.scope_type = 'project' + UNION + SELECT p.id FROM projects p JOIN memberships m ON m.scope_type = 'team' AND m.scope_id = p.team_id + WHERE m.user_id = ? + UNION + SELECT p.id FROM projects p JOIN teams t ON t.id = p.team_id + JOIN memberships m ON m.scope_type = 'org' AND m.scope_id = t.org_id + WHERE m.user_id = ?` + +// CanUseProject reports whether a person may make and use keys in a project. +func (s *DB) CanUseProject(ctx context.Context, userID, projectID string) (bool, error) { + var n int + err := s.db.QueryRowContext(ctx, + `SELECT COUNT(*) FROM (`+usableProjects+`) u WHERE u.scope_id = ?`, + userID, userID, userID, projectID).Scan(&n) + return n > 0, err +} + +// ProjectsFor lists the projects a person may use, with their paths. +func (s *DB) ProjectsFor(ctx context.Context, userID string) ([]ProjectPath, error) { + return queryAll(ctx, s.db, ` + SELECT p.id, o.name || ' / ' || t.name || ' / ' || p.name + FROM projects p + JOIN teams t ON t.id = p.team_id + JOIN organizations o ON o.id = t.org_id + WHERE p.id IN (`+usableProjects+`) + ORDER BY 2`, []any{userID, userID, userID}, func(rows *sql.Rows) (ProjectPath, error) { + var p ProjectPath + return p, rows.Scan(&p.ID, &p.Path) + }) +} + +// membershipPath names the scope of a membership row as a path. +const membershipPath = ` + CASE m.scope_type + WHEN 'org' THEN (SELECT o.name FROM organizations o WHERE o.id = m.scope_id) + WHEN 'team' THEN (SELECT o.name || ' / ' || t.name FROM teams t + JOIN organizations o ON o.id = t.org_id WHERE t.id = m.scope_id) + ELSE (SELECT o.name || ' / ' || t.name || ' / ' || p.name FROM projects p + JOIN teams t ON t.id = p.team_id JOIN organizations o ON o.id = t.org_id + WHERE p.id = m.scope_id) + END` + +func scanMembership(rows *sql.Rows) (Membership, error) { + var m Membership + var scope string + var path, by sql.NullString + var added int64 + err := rows.Scan(&m.UserID, &scope, &m.ScopeID, &m.Role, &added, &by, &path) + m.Scope, m.Path, m.AddedBy, m.AddedAt = Scope(scope), path.String, by.String, fromMillis(added) + return m, err +} + +// MembershipsOf lists where a person is a member, by path. +func (s *DB) MembershipsOf(ctx context.Context, userID string) ([]Membership, error) { + return queryAll(ctx, s.db, ` + SELECT m.user_id, m.scope_type, m.scope_id, m.role, m.added_at, m.added_by, `+membershipPath+` + FROM memberships m WHERE m.user_id = ? ORDER BY 7`, []any{userID}, scanMembership) +} + +// AllMemberships lists every membership, for a page that shows the tree with +// its people. +func (s *DB) AllMemberships(ctx context.Context) ([]Membership, error) { + return queryAll(ctx, s.db, ` + SELECT m.user_id, m.scope_type, m.scope_id, m.role, m.added_at, m.added_by, `+membershipPath+` + FROM memberships m ORDER BY 7`, nil, scanMembership) +} + +// SetPersonalDefaults sets the limits a person's own use falls under when +// nobody wrote a limit for that person: what a personal key, with no project +// to be bounded by, gets. Nil clears them. +func (s *DB) SetPersonalDefaults(l *Limits) { + s.mu.Lock() + defer s.mu.Unlock() + if l == nil { + s.personal = nil + return + } + c := *l + s.personal = &c +} + +// Resolve walks a key's chain and returns the most restrictive of each limit. +func (s *DB) Resolve(ctx context.Context, keyID string) (Effective, error) { + var e Effective + var band, profile, project, user sql.NullString + err := s.db.QueryRowContext(ctx, + `SELECT id, band, routing_profile, project_id, user_id FROM api_keys WHERE id = ?`, keyID). + Scan(&e.KeyID, &band, &profile, &project, &user) + if errors.Is(err, sql.ErrNoRows) { + return Effective{}, ErrNotFound + } + if err != nil { + return Effective{}, err + } + e.Band, e.RoutingProfile = band.String, profile.String + e.ProjectID, e.UserID = project.String, user.String + return e, s.resolveChain(ctx, &e, true) +} + +// ResolveFor resolves a caller that has no key row: the chat page, spending +// as a person in a project or, with no project, personally. +func (s *DB) ResolveFor(ctx context.Context, callerID, userID, projectID string) (Effective, error) { + e := Effective{KeyID: callerID, UserID: userID, ProjectID: projectID} + return e, s.resolveChain(ctx, &e, false) +} + +// resolveChain fills in the tree above a caller and folds every limit that +// applies: the project's chain, each per-member limit on it for this person, +// the person's own, and the key's. +// +// One pool per level. A budget on a team is spent by everything beneath it; a +// person's limit by every key they own; a per-member limit by one person +// within one scope. +func (s *DB) resolveChain(ctx context.Context, e *Effective, keyLevel bool) error { + if e.ProjectID != "" { + err := s.db.QueryRowContext(ctx, + `SELECT t.id, t.org_id FROM projects p JOIN teams t ON t.id = p.team_id WHERE p.id = ?`, + e.ProjectID).Scan(&e.TeamID, &e.OrgID) + if errors.Is(err, sql.ErrNoRows) { + return ErrNotFound + } + if err != nil { + return err + } + } + + type step struct { + scope Scope + id string + // counter is where use is counted, when it is not the scope's own id. + counter string + } + var steps []step + if e.ProjectID != "" { + steps = append(steps, step{ScopeOrg, e.OrgID, ""}, step{ScopeTeam, e.TeamID, ""}, + step{ScopeProject, e.ProjectID, ""}) + if e.UserID != "" { + for _, lvl := range []step{{ScopeOrg, e.OrgID, ""}, {ScopeTeam, e.TeamID, ""}, {ScopeProject, e.ProjectID, ""}} { + ms := MemberScope(lvl.scope) + steps = append(steps, step{ms, lvl.id, MemberOwner(ms, lvl.id, e.UserID).ID}) + } + } + } + if e.UserID != "" { + steps = append(steps, step{ScopeUser, e.UserID, ""}) + } + if keyLevel { + steps = append(steps, step{ScopeKey, e.KeyID, ""}) + } + + sawUser := false + for _, st := range steps { + l, err := s.LimitsFor(ctx, st.scope, st.id) + if errors.Is(err, ErrNotFound) { + continue + } + if err != nil { + return err + } + if st.counter != "" { + l.ScopeID = st.counter + } + if st.scope == ScopeUser { + sawUser = true + } + e.tighten(l) + // Kept whole as well as folded in. Enforcement walks this; the folded + // values are for showing somebody their ceiling. + e.Chain = append(e.Chain, l) + } + + // A personal caller has no project to be bounded by. With no limit written + // for the person, the personal defaults stand in for one — counted in the + // person's own pool, so every personal key they hold shares it. + if e.ProjectID == "" && e.UserID != "" && !sawUser { + s.mu.RLock() + p := s.personal + s.mu.RUnlock() + if p != nil { + l := *p + l.Scope, l.ScopeID = ScopeUser, e.UserID + e.tighten(l) + e.Chain = append(e.Chain, l) + } + } + s.applyDefaults(e) + return nil +} diff --git a/internal/store/memberships_test.go b/internal/store/memberships_test.go new file mode 100644 index 0000000..6deaf45 --- /dev/null +++ b/internal/store/memberships_test.go @@ -0,0 +1,212 @@ +package store + +import ( + "errors" + "testing" + "time" +) + +func TestApprovalNeedNotPutAnybodyAnywhere(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + _ = f.s.CreateUser(ctx, User{ID: "usr-2", Email: "b@x", Name: "b", PasswordHash: "h", + State: UserPending, CreatedAt: time.Now()}) + if err := f.s.ApproveUser(ctx, "usr-2", "", "test", time.Now()); err != nil { + t.Fatalf("approving with no project: %v", err) + } + ms, _ := f.s.MembershipsOf(ctx, "usr-2") + if len(ms) != 0 { + t.Errorf("memberships = %+v", ms) + } +} + +func TestATeamMemberUsesEveryProjectInTheTeam(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + if err := f.s.AddMembership(ctx, f.user, ScopeTeam, f.ml, RoleMember, "test", time.Now()); err != nil { + t.Fatal(err) + } + for proj, want := range map[string]bool{f.gateway: true, f.research: true, f.search: false} { + ok, err := f.s.CanUseProject(ctx, f.user, proj) + if err != nil || ok != want { + t.Errorf("%s: %v %v, want %v", proj, ok, err, want) + } + } + projects, _ := f.s.ProjectsFor(ctx, f.user) + if len(projects) != 2 { + t.Errorf("projects = %+v", projects) + } + ms, _ := f.s.MembershipsOf(ctx, f.user) + var paths []string + for _, m := range ms { + paths = append(paths, m.Path) + } + if len(paths) != 2 || paths[0] != "Acme / platform / gateway" || paths[1] != "Scania / ml" { + t.Errorf("paths = %v", paths) + } +} + +func TestAKeyCanOnlyBeMadeWhereThePersonIsAMember(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + _, k, _ := NewKey("key-x", "x", f.search) + if err := f.s.CreateUserKey(ctx, f.user, k); !errors.Is(err, ErrNotMember) { + t.Errorf("a key in a project the person is not in: %v", err) + } + _, k, _ = NewKey("key-p", "personal", "") + if err := f.s.CreateUserKey(ctx, f.user, k); err != nil { + t.Fatalf("a personal key: %v", err) + } + got, err := f.s.KeyByHash(ctx, k.Hash) + if err != nil || got.ProjectID != "" || got.UserID != f.user { + t.Errorf("personal key read back as %+v, %v", got, err) + } +} + +func TestLeavingAProjectRevokesTheKeysMadeThereOnly(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + _, personal, _ := NewKey("key-p", "personal", "") + if err := f.s.CreateUserKey(ctx, f.user, personal); err != nil { + t.Fatal(err) + } + revoked, err := f.s.RemoveMembership(ctx, f.user, ScopeProject, f.gateway, time.Now()) + if err != nil { + t.Fatal(err) + } + if revoked != 1 { + t.Errorf("revoked %d keys, want the one made in gateway", revoked) + } + keys, _ := f.s.KeysForUser(ctx, f.user) + for _, k := range keys { + if k.ID == personal.ID && !k.RevokedAt.IsZero() { + t.Error("leaving a project revoked a personal key") + } + } + gw, _ := f.s.Keys(ctx, f.gateway) + for _, k := range gw { + if k.ID == f.operatorKey && !k.RevokedAt.IsZero() { + t.Error("an operator's key went with the person") + } + } +} + +func TestLeavingKeepsKeysReachedAnotherWay(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + // Also a member of the whole organization, so gateway stays usable. + if err := f.s.AddMembership(ctx, f.user, ScopeOrg, f.acme, RoleMember, "", time.Now()); err != nil { + t.Fatal(err) + } + revoked, err := f.s.RemoveMembership(ctx, f.user, ScopeProject, f.gateway, time.Now()) + if err != nil || revoked != 0 { + t.Errorf("revoked %d, %v; the organization still reaches gateway", revoked, err) + } +} + +func i(v int) *int { return &v } + +func TestAPersonalKeyIsBoundedByThePerson(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + _, k, _ := NewKey("key-p", "personal", "") + if err := f.s.CreateUserKey(ctx, f.user, k); err != nil { + t.Fatal(err) + } + + // Nothing written for the person: the personal defaults apply, in the + // person's own pool. + f.s.SetPersonalDefaults(&Limits{RPM: i(10), TPM: i(5000)}) + e, err := f.s.Resolve(ctx, k.ID) + if err != nil { + t.Fatalf("resolving a personal key: %v", err) + } + if e.ProjectID != "" || e.UserID != f.user || e.RPM == nil || *e.RPM != 10 || + e.RPMOwner != (LimitOwner{ScopeUser, f.user}) { + t.Errorf("personal key resolved to %+v", e) + } + + // Written for the person: that wins over the defaults. + if err := f.s.PutLimits(ctx, Limits{Scope: ScopeUser, ScopeID: f.user, RPM: i(3)}); err != nil { + t.Fatal(err) + } + e, _ = f.s.Resolve(ctx, k.ID) + if *e.RPM != 3 || e.TPM != nil { + t.Errorf("the person's own limit should replace the defaults: %+v", e) + } +} + +func TestAProjectKeyAnswersToThePersonAndEachMemberLimit(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + _, k, _ := NewKey("key-g", "gw", f.gateway) + if err := f.s.CreateUserKey(ctx, f.user, k); err != nil { + t.Fatal(err) + } + must := func(l Limits) { + t.Helper() + if err := f.s.PutLimits(ctx, l); err != nil { + t.Fatal(err) + } + } + must(Limits{Scope: ScopeProject, ScopeID: f.gateway, RPM: i(100)}) + must(Limits{Scope: ScopeProjectMember, ScopeID: f.gateway, RPM: i(20)}) + must(Limits{Scope: ScopeUser, ScopeID: f.user, TPM: i(700)}) + + e, err := f.s.Resolve(ctx, k.ID) + if err != nil { + t.Fatal(err) + } + if *e.RPM != 20 || e.RPMOwner != MemberOwner(ScopeProjectMember, f.gateway, f.user) { + t.Errorf("the per-member limit should hold this person to 20: %+v", e) + } + if *e.TPM != 700 || e.TPMOwner != (LimitOwner{ScopeUser, f.user}) { + t.Errorf("the person's own limit should apply in a project too: %+v", e) + } + // Every pool is kept for enforcement, not just the tightest. + var scopes []Scope + for _, l := range e.Chain { + scopes = append(scopes, l.Scope) + } + if len(scopes) < 3 { + t.Errorf("chain = %v", scopes) + } + + // An operator's key in the same project has no member and no person. + e, _ = f.s.Resolve(ctx, f.operatorKey) + if *e.RPM != 100 { + t.Errorf("an ownerless key is bounded by the project alone: %+v", e) + } +} + +func TestSpendIsRestoredForPeopleAndPersonalKeys(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + now := time.Now() + if err := f.s.WriteUsage(ctx, []Usage{ + {At: now, KeyID: "key-p", UserID: f.user, CostMicros: 5, Model: "m", ServedModel: "m"}, + {At: now, KeyID: "key-u", UserID: f.user, ProjectID: f.gateway, CostMicros: 7, Model: "m", ServedModel: "m"}, + {At: now, KeyID: "session:" + f.user, UserID: f.user, CostMicros: 11, Model: "m", ServedModel: "m"}, + }); err != nil { + t.Fatal(err) + } + rows, err := f.s.SpendSinceByScope(ctx, now.Add(-time.Hour)) + if err != nil { + t.Fatal(err) + } + var personal, project, total int64 + for _, r := range rows { + if r.UserID != f.user { + t.Errorf("row not attributed to the person: %+v", r) + } + total += r.Micros + if r.ProjectID == "" { + personal += r.Micros + } else if r.OrgID == f.acme { + project += r.Micros + } + } + if total != 23 || personal != 16 || project != 7 { + t.Errorf("total %d personal %d project %d", total, personal, project) + } +} diff --git a/internal/store/migrate.go b/internal/store/migrate.go index 6905182..2df773b 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -95,8 +95,36 @@ func migrationNames(dialect string) ([]string, error) { return names, nil } +// fkOffMarker in a SQLite migration runs it with foreign keys off. +// +// Rebuilding a table — the only way SQLite changes a column's constraints — +// drops the old one, and with foreign keys on that deletes every row other +// tables point at. SQLite's procedure for this turns them off, which only +// works outside a transaction, so this takes one connection, turns them off +// on it, runs the migration in a transaction there, and refuses to commit +// unless foreign_key_check finds nothing. +const fkOffMarker = "-- migrate: foreign_keys=off" + func applyOne(ctx context.Context, db *sql.DB, dialect, name, body string) error { - tx, err := db.BeginTx(ctx, nil) + type execer interface { + BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) + } + var on execer = db + checkFKs := false + if dialect == string(SQLiteDialect) && strings.Contains(body, fkOffMarker) { + c, err := db.Conn(ctx) + if err != nil { + return err + } + defer c.Close() + if _, err := c.ExecContext(ctx, `PRAGMA foreign_keys = OFF`); err != nil { + return err + } + defer func() { _, _ = c.ExecContext(context.WithoutCancel(ctx), `PRAGMA foreign_keys = ON`) }() + on, checkFKs = c, true + } + + tx, err := on.BeginTx(ctx, nil) if err != nil { return err } @@ -105,6 +133,17 @@ func applyOne(ctx context.Context, db *sql.DB, dialect, name, body string) error if _, err := tx.ExecContext(ctx, body); err != nil { return err } + if checkFKs { + rows, err := tx.QueryContext(ctx, `PRAGMA foreign_key_check`) + if err != nil { + return err + } + broken := rows.Next() + rows.Close() + if broken { + return fmt.Errorf("leaves foreign keys pointing at nothing; not applied") + } + } if _, err := tx.ExecContext(ctx, Dialect(dialect).rebind(`INSERT INTO schema_migrations (version, applied_at) VALUES (?, ?)`), name, nowMillis(), diff --git a/internal/store/migrate0010_test.go b/internal/store/migrate0010_test.go new file mode 100644 index 0000000..11ae83b --- /dev/null +++ b/internal/store/migrate0010_test.go @@ -0,0 +1,128 @@ +package store + +import ( + "context" + "database/sql" + "path/filepath" + "strings" + "testing" +) + +// Migration 0010 rebuilds api_keys on SQLite, a kind of migration this store +// had never run. Checked against a store that already has what a running +// cluster has: a person in a project, their key, a pending claim pointing at +// the key, and usage — including the chat page's, which has no key row. +func TestMigration0010KeepsAnExistingStoreWhole(t *testing.T) { + ctx := context.Background() + path := filepath.Join(t.TempDir(), "old.db") + db, err := sql.Open("sqlite", path+"?_pragma=foreign_keys(1)") + if err != nil { + t.Fatal(err) + } + defer db.Close() + db.SetMaxOpenConns(1) + + if _, err := db.ExecContext(ctx, + `CREATE TABLE schema_migrations (version TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)`); err != nil { + t.Fatal(err) + } + names, err := migrationNames("sqlite") + if err != nil { + t.Fatal(err) + } + for _, name := range names { + if name >= "0010" { + break + } + body, _ := migrationFS.ReadFile("migrations/sqlite/" + name) + if err := applyOne(ctx, db, "sqlite", name, string(body)); err != nil { + t.Fatalf("%s: %v", name, err) + } + } + + for _, q := range []string{ + `INSERT INTO organizations (id, name) VALUES ('o', 'Acme')`, + `INSERT INTO teams (id, org_id, name) VALUES ('t', 'o', 'ml')`, + `INSERT INTO projects (id, team_id, name) VALUES ('p', 't', 'training')`, + `INSERT INTO users (id, email, name, password_hash, state, project_id, created_at, approved_at, approved_by) + VALUES ('u', 'a@x', 'a', 'h', 'active', 'p', 1, 2, 'admin@x')`, + `INSERT INTO users (id, email, name, password_hash, state, created_at) + VALUES ('w', 'w@x', 'w', 'h', 'pending', 1)`, + `INSERT INTO api_keys (id, key_hash, key_prefix, name, project_id, created_at, user_id) + VALUES ('k', X'01', 'sk-1', 'laptop', 'p', 1, 'u')`, + `INSERT INTO api_keys (id, key_hash, key_prefix, name, project_id, created_at) + VALUES ('ops', X'02', 'sk-2', 'ci', 'p', 1)`, + `INSERT INTO key_claims (token_hash, key_id, sealed, created_at, expires_at) VALUES (X'aa', 'k', X'00', 1, 9)`, + `INSERT INTO usage (ts, key_id, project_id, model, served_model, prompt_tokens, completion_tokens, + cost_micros, status, latency_ms) VALUES (5, 'k', 'p', 'm', 'm', 1, 1, 3, 200, 1)`, + `INSERT INTO usage (ts, key_id, project_id, model, served_model, prompt_tokens, completion_tokens, + cost_micros, status, latency_ms) VALUES (6, 'session:u', 'p', 'm', 'm', 1, 1, 4, 200, 1)`, + `INSERT INTO usage (ts, key_id, project_id, model, served_model, prompt_tokens, completion_tokens, + cost_micros, status, latency_ms) VALUES (7, 'ops', 'p', 'm', 'm', 1, 1, 5, 200, 1)`, + } { + if _, err := db.ExecContext(ctx, q); err != nil { + t.Fatalf("%s: %v", q, err) + } + } + + if err := migrate(ctx, db, "sqlite"); err != nil { + t.Fatalf("migrating: %v", err) + } + + one := func(q string, args ...any) string { + t.Helper() + var v sql.NullString + if err := db.QueryRowContext(ctx, q, args...).Scan(&v); err != nil { + t.Fatalf("%s: %v", q, err) + } + return v.String + } + if got := one(`SELECT COUNT(*) FROM api_keys`); got != "2" { + t.Errorf("keys after the rebuild: %s", got) + } + if got := one(`SELECT user_id FROM api_keys WHERE id = 'k'`); got != "u" { + t.Errorf("the key's owner: %q", got) + } + if got := one(`SELECT key_id FROM key_claims`); got != "k" { + t.Errorf("the claim lost its key: %q", got) + } + if got := one(`SELECT scope_type || ':' || scope_id || ':' || role || ':' || added_by FROM memberships WHERE user_id = 'u'`); got != "project:p:member:admin@x" { + t.Errorf("the person's project did not become a membership: %q", got) + } + if got := one(`SELECT COUNT(*) FROM memberships WHERE user_id = 'w'`); got != "0" { + t.Error("a pending signup was given a membership") + } + var users []string + rows, _ := db.QueryContext(ctx, `SELECT key_id || '=' || user_id FROM usage ORDER BY ts`) + for rows.Next() { + var s string + _ = rows.Scan(&s) + users = append(users, s) + } + rows.Close() + if strings.Join(users, ",") != "k=u,session:u=u,ops=" { + t.Errorf("usage attributed as %v", users) + } + // The rebuilt table takes a personal key, and still refuses a project + // that does not exist. + if _, err := db.ExecContext(ctx, `INSERT INTO api_keys (id, key_hash, key_prefix, name, created_at, user_id) + VALUES ('pk', X'03', 'sk-3', 'mine', 1, 'u')`); err != nil { + t.Errorf("a personal key was refused: %v", err) + } + if _, err := db.ExecContext(ctx, `INSERT INTO api_keys (id, key_hash, key_prefix, name, project_id, created_at) + VALUES ('bad', X'04', 'sk-4', 'x', 'nope', 1)`); err == nil { + t.Error("the rebuilt table lost its foreign key to projects") + } + fk, err := db.QueryContext(ctx, `PRAGMA foreign_key_check`) + if err != nil { + t.Fatal(err) + } + if fk.Next() { + t.Error("foreign keys are violated after the migration") + } + fk.Close() + // And they are back on for everything after it. + if one(`PRAGMA foreign_keys`) != "1" { + t.Error("the migration left foreign keys off") + } +} diff --git a/internal/store/migrations/postgres/0010_memberships.sql b/internal/store/migrations/postgres/0010_memberships.sql new file mode 100644 index 0000000..8a84657 --- /dev/null +++ b/internal/store/migrations/postgres/0010_memberships.sql @@ -0,0 +1,35 @@ +-- A person could be in exactly one project, chosen once at approval, and could +-- not use the gateway at all before that. People belong to several teams and +-- to none, so membership becomes its own table and a key need not have a +-- project: a personal key is owned by a person and bounded by that person's +-- own limits. + +-- Membership at any level of the tree; a member of a team may use every +-- project in it. Role is 'member' today; 'manager' is reserved for delegated +-- administration of the scope. +CREATE TABLE memberships ( + user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + scope_type TEXT NOT NULL, + scope_id TEXT NOT NULL, + role TEXT NOT NULL DEFAULT 'member', + added_at BIGINT NOT NULL, + added_by TEXT, + PRIMARY KEY (user_id, scope_type, scope_id) +); +CREATE INDEX memberships_by_scope ON memberships(scope_type, scope_id); + +-- Everybody keeps the project they had. +INSERT INTO memberships (user_id, scope_type, scope_id, role, added_at, added_by) +SELECT id, 'project', project_id, 'member', COALESCE(approved_at, created_at), approved_by +FROM users WHERE project_id IS NOT NULL; + +-- A personal key has an owner and no project. +ALTER TABLE api_keys ALTER COLUMN project_id DROP NOT NULL; + +-- Who spent it, recorded with the spend. Deriving it from the key at read time +-- counted a key's past against whoever owns it now, and could not attribute +-- the chat page's requests, which have no key row at all. +ALTER TABLE usage ADD COLUMN user_id TEXT NOT NULL DEFAULT ''; +UPDATE usage SET user_id = COALESCE((SELECT k.user_id FROM api_keys k WHERE k.id = usage.key_id), ''); +UPDATE usage SET user_id = substr(key_id, 9) WHERE key_id LIKE 'session:%'; +CREATE INDEX usage_by_user_ts ON usage(user_id, ts); diff --git a/internal/store/migrations/sqlite/0010_memberships.sql b/internal/store/migrations/sqlite/0010_memberships.sql new file mode 100644 index 0000000..f5950de --- /dev/null +++ b/internal/store/migrations/sqlite/0010_memberships.sql @@ -0,0 +1,60 @@ +-- A person could be in exactly one project, chosen once at approval, and could +-- not use the gateway at all before that. People belong to several teams and +-- to none, so membership becomes its own table and a key need not have a +-- project: a personal key is owned by a person and bounded by that person's +-- own limits. + +-- Membership at any level of the tree; a member of a team may use every +-- project in it. Role is 'member' today; 'manager' is reserved for delegated +-- administration of the scope. +CREATE TABLE memberships ( + user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + scope_type TEXT NOT NULL, + scope_id TEXT NOT NULL, + role TEXT NOT NULL DEFAULT 'member', + added_at INTEGER NOT NULL, + added_by TEXT, + PRIMARY KEY (user_id, scope_type, scope_id) +); +CREATE INDEX memberships_by_scope ON memberships(scope_type, scope_id); + +-- Everybody keeps the project they had. +INSERT INTO memberships (user_id, scope_type, scope_id, role, added_at, added_by) +SELECT id, 'project', project_id, 'member', COALESCE(approved_at, created_at), approved_by +FROM users WHERE project_id IS NOT NULL; + +-- A personal key has an owner and no project. SQLite cannot drop NOT NULL +-- from a column, so the table is rebuilt — with foreign keys off while it is, +-- as SQLite's own procedure for this requires, and checked before commit. +-- migrate: foreign_keys=off +CREATE TABLE api_keys_new ( + id TEXT PRIMARY KEY, + key_hash BLOB NOT NULL UNIQUE, + key_prefix TEXT NOT NULL, + name TEXT NOT NULL, + project_id TEXT REFERENCES projects(id), + band TEXT, + routing_profile TEXT, + created_at INTEGER NOT NULL, + expires_at INTEGER, + revoked_at INTEGER, + last_used_at INTEGER, + user_id TEXT REFERENCES users(id) +); +INSERT INTO api_keys_new (id, key_hash, key_prefix, name, project_id, band, routing_profile, + created_at, expires_at, revoked_at, last_used_at, user_id) +SELECT id, key_hash, key_prefix, name, project_id, band, routing_profile, + created_at, expires_at, revoked_at, last_used_at, user_id +FROM api_keys; +DROP TABLE api_keys; +ALTER TABLE api_keys_new RENAME TO api_keys; +CREATE INDEX api_keys_by_project ON api_keys(project_id); +CREATE INDEX api_keys_by_user ON api_keys(user_id); + +-- Who spent it, recorded with the spend. Deriving it from the key at read time +-- counted a key's past against whoever owns it now, and could not attribute +-- the chat page's requests, which have no key row at all. +ALTER TABLE usage ADD COLUMN user_id TEXT NOT NULL DEFAULT ''; +UPDATE usage SET user_id = COALESCE((SELECT k.user_id FROM api_keys k WHERE k.id = usage.key_id), ''); +UPDATE usage SET user_id = substr(key_id, 9) WHERE key_id LIKE 'session:%'; +CREATE INDEX usage_by_user_ts ON usage(user_id, ts); diff --git a/internal/store/postgres_memberships_test.go b/internal/store/postgres_memberships_test.go new file mode 100644 index 0000000..43fb436 --- /dev/null +++ b/internal/store/postgres_memberships_test.go @@ -0,0 +1,87 @@ +package store + +import ( + "testing" + "time" +) + +// The membership queries on PostgreSQL: the unions that decide which projects +// a person may use, the path each membership is named by, the revocation that +// follows leaving, a personal key's resolution, and spend with no project. +// Each passed on SQLite first; these are what would catch one that does not +// on the database the cluster runs. +func TestPostgresMemberships(t *testing.T) { + f := newFixtureOn(t, openPG(t)) + ctx := t.Context() + + if err := f.s.AddMembership(ctx, f.user, ScopeTeam, f.ml, RoleMember, "t", time.Now()); err != nil { + t.Fatal(err) + } + if err := f.s.AddMembership(ctx, f.user, ScopeTeam, f.ml, RoleMember, "t", time.Now()); err != nil { + t.Errorf("adding again should change nothing: %v", err) + } + for proj, want := range map[string]bool{f.gateway: true, f.research: true, f.search: false} { + if ok, err := f.s.CanUseProject(ctx, f.user, proj); err != nil || ok != want { + t.Errorf("CanUseProject %s = %v, %v; want %v", proj, ok, err, want) + } + } + if ps, err := f.s.ProjectsFor(ctx, f.user); err != nil || len(ps) != 2 { + t.Errorf("ProjectsFor = %+v, %v", ps, err) + } + ms, err := f.s.MembershipsOf(ctx, f.user) + if err != nil || len(ms) != 2 || ms[0].Path != "Acme / platform / gateway" || ms[1].Path != "Scania / ml" { + t.Errorf("MembershipsOf = %+v, %v", ms, err) + } + if all, err := f.s.AllMemberships(ctx); err != nil || len(all) != 2 { + t.Errorf("AllMemberships = %+v, %v", all, err) + } + + // Leaving gateway revokes the key made there, and leaves a personal one. + _, personal, _ := NewKey("key-p", "personal", "") + if err := f.s.CreateUserKey(ctx, f.user, personal); err != nil { + t.Fatal(err) + } + if n, err := f.s.RemoveMembership(ctx, f.user, ScopeProject, f.gateway, time.Now()); err != nil || n != 1 { + t.Errorf("RemoveMembership revoked %d, %v; want 1", n, err) + } + + // A personal key resolves, under the personal defaults. + f.s.SetPersonalDefaults(&Limits{RPM: i(9)}) + defer f.s.SetPersonalDefaults(nil) + e, err := f.s.Resolve(ctx, personal.ID) + if err != nil || e.UserID != f.user || e.ProjectID != "" || e.RPM == nil || *e.RPM != 9 { + t.Errorf("personal key resolved to %+v, %v", e, err) + } + if e, err := f.s.ResolveFor(ctx, "session:"+f.user, f.user, f.research); err != nil || e.OrgID != f.scania { + t.Errorf("ResolveFor in a project = %+v, %v", e, err) + } + + // Spend with no project is kept, and attributed. + now := time.Now() + if err := f.s.WriteUsage(ctx, []Usage{ + {At: now, KeyID: personal.ID, UserID: f.user, CostMicros: 5, Model: "m", ServedModel: "m"}, + {At: now, KeyID: "session:" + f.user, UserID: f.user, ProjectID: f.research, CostMicros: 7, Model: "m", ServedModel: "m"}, + }); err != nil { + t.Fatal(err) + } + rows, err := f.s.SpendSinceByScope(ctx, now.Add(-time.Minute)) + if err != nil { + t.Fatal(err) + } + var total int64 + for _, r := range rows { + if r.UserID == f.user { + total += r.Micros + } + } + if total != 12 { + t.Errorf("restored %d for the person, want 12: %+v", total, rows) + } + byUser, err := f.s.UsageByUser(ctx, now.Add(-time.Minute)) + if err != nil || len(byUser) != 1 || byUser[0].UserID != f.user || byUser[0].Requests != 2 { + t.Errorf("UsageByUser = %+v, %v", byUser, err) + } + if keys, err := f.s.AllKeys(ctx); err != nil || len(keys) != 3 { + t.Errorf("AllKeys = %d rows, %v", len(keys), err) + } +} diff --git a/internal/store/postgres_test.go b/internal/store/postgres_test.go index bddd493..32465de 100644 --- a/internal/store/postgres_test.go +++ b/internal/store/postgres_test.go @@ -41,7 +41,7 @@ func openPG(t *testing.T) *DB { // inherits the previous one's lease is refused for reasons that have // nothing to do with what it is testing. It passed on SQLite, where // every subtest gets a fresh file, and failed only here. - `TRUNCATE thread_messages, threads, memory_notes, usage, api_keys, + `TRUNCATE thread_messages, threads, memory_notes, usage, api_keys, memberships, key_claims, limits, models, sessions, users, projects, teams, organizations, leadership RESTART IDENTITY CASCADE`) if err != nil { diff --git a/internal/store/sqlite.go b/internal/store/sqlite.go index 9dcb8df..849592a 100644 --- a/internal/store/sqlite.go +++ b/internal/store/sqlite.go @@ -40,6 +40,9 @@ type DB struct { mu sync.RWMutex defaultRPM *int defaultTPM *int + // personal is what a person's own use is held to when nobody wrote a + // limit for them; see SetPersonalDefaults. + personal *Limits } // OpenSQLite opens (creating if needed) a database and brings it up to date. @@ -157,7 +160,7 @@ func (s *DB) Projects(ctx context.Context, teamID string) ([]Project, error) { // --- keys --- const keyColumns = `id, key_hash, key_prefix, name, project_id, band, routing_profile, - created_at, expires_at, revoked_at, last_used_at` + created_at, expires_at, revoked_at, last_used_at, user_id` // CreateKey stores a key. Only its hash is written; the key itself is the // caller's to show once and then forget. @@ -166,10 +169,11 @@ func (s *DB) CreateKey(ctx context.Context, k Key) error { return errors.New("store: a key needs a hash") } _, err := s.db.ExecContext(ctx, - `INSERT INTO api_keys (`+keyColumns+`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - k.ID, k.Hash, k.Prefix, k.Name, k.ProjectID, + `INSERT INTO api_keys (`+keyColumns+`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + k.ID, k.Hash, k.Prefix, k.Name, nullString(k.ProjectID), nullString(k.Band), nullString(k.RoutingProfile), - millis(k.CreatedAt), nullMillis(k.ExpiresAt), nullMillis(k.RevokedAt), nullMillis(k.LastUsedAt)) + millis(k.CreatedAt), nullMillis(k.ExpiresAt), nullMillis(k.RevokedAt), nullMillis(k.LastUsedAt), + nullString(k.UserID)) return err } @@ -295,56 +299,6 @@ func (s *DB) LimitsFor(ctx context.Context, scope Scope, id string) (Limits, err return l, nil } -// Resolve walks a key's chain and returns the most restrictive of each limit. -// -// One query rather than four lookups: the chain is a fixed three-level join, and -// this is called on a cache miss during authentication. -func (s *DB) Resolve(ctx context.Context, keyID string) (Effective, error) { - var e Effective - row := s.db.QueryRowContext(ctx, - `SELECT k.id, k.band, k.routing_profile, p.id, t.id, o.id - FROM api_keys k - JOIN projects p ON p.id = k.project_id - JOIN teams t ON t.id = p.team_id - JOIN organizations o ON o.id = t.org_id - WHERE k.id = ?`, keyID) - - var band, profile sql.NullString - if err := row.Scan(&e.KeyID, &band, &profile, &e.ProjectID, &e.TeamID, &e.OrgID); err != nil { - if errors.Is(err, sql.ErrNoRows) { - return Effective{}, ErrNotFound - } - return Effective{}, err - } - e.Band, e.RoutingProfile = band.String, profile.String - - // Widest first, so a narrower scope can only tighten what it inherits. - chain := []struct { - scope Scope - id string - }{ - {ScopeOrg, e.OrgID}, - {ScopeTeam, e.TeamID}, - {ScopeProject, e.ProjectID}, - {ScopeKey, e.KeyID}, - } - for _, step := range chain { - l, err := s.LimitsFor(ctx, step.scope, step.id) - if errors.Is(err, ErrNotFound) { - continue - } - if err != nil { - return Effective{}, err - } - e.tighten(l) - // Kept whole as well as folded in. Enforcement walks this; the folded - // values are for showing somebody their ceiling. - e.Chain = append(e.Chain, l) - } - s.applyDefaults(&e) - return e, nil -} - // SetDefaultLimits sets what applies to a key no scope gives a limit to. // // A default is a fallback, not a ceiling, so it is applied after the chain and @@ -390,17 +344,18 @@ func (s *DB) applyDefaults(e *Effective) { } } -// SpendSinceByScope totals cost per key, with the key's place in the tree. +// SpendSinceByScope totals cost per key and person, with the key's place in +// the tree. Personal keys and the chat page's requests have no project and are +// kept: dropping them is what would reset a person's budget on every restart. func (s *DB) SpendSinceByScope(ctx context.Context, since time.Time) ([]ScopeSpend, error) { rows, err := s.db.QueryContext(ctx, - `SELECT k.id, p.id, t.id, o.id, SUM(u.cost_micros) + `SELECT u.key_id, u.user_id, COALESCE(p.id, ''), COALESCE(t.id, ''), COALESCE(t.org_id, ''), + SUM(u.cost_micros) FROM usage u - JOIN api_keys k ON k.id = u.key_id - JOIN projects p ON p.id = k.project_id - JOIN teams t ON t.id = p.team_id - JOIN organizations o ON o.id = t.org_id + LEFT JOIN projects p ON p.id = NULLIF(u.project_id, '') + LEFT JOIN teams t ON t.id = p.team_id WHERE u.ts >= ? - GROUP BY k.id, p.id, t.id, o.id`, millis(since)) + GROUP BY u.key_id, u.user_id, p.id, t.id, t.org_id`, millis(since)) if err != nil { return nil, err } @@ -410,7 +365,7 @@ func (s *DB) SpendSinceByScope(ctx context.Context, since time.Time) ([]ScopeSpe for rows.Next() { var sp ScopeSpend var total sql.NullInt64 - if err := rows.Scan(&sp.KeyID, &sp.ProjectID, &sp.TeamID, &sp.OrgID, &total); err != nil { + if err := rows.Scan(&sp.KeyID, &sp.UserID, &sp.ProjectID, &sp.TeamID, &sp.OrgID, &total); err != nil { return nil, err } sp.Micros = total.Int64 @@ -469,8 +424,8 @@ func (s *DB) WriteUsage(ctx context.Context, records []Usage) error { return s.inTx(ctx, func(tx conn) error { stmt, err := tx.PrepareContext(ctx, `INSERT INTO usage (ts, key_id, project_id, model, served_model, - prompt_tokens, completion_tokens, cost_micros, status, latency_ms) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`) + prompt_tokens, completion_tokens, cost_micros, status, latency_ms, user_id) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`) if err != nil { return err } @@ -478,7 +433,7 @@ func (s *DB) WriteUsage(ctx context.Context, records []Usage) error { for _, r := range records { if _, err := stmt.ExecContext(ctx, millis(r.At), r.KeyID, r.ProjectID, r.Model, r.ServedModel, r.PromptTokens, r.CompletionTokens, - r.CostMicros, r.Status, r.LatencyMs); err != nil { + r.CostMicros, r.Status, r.LatencyMs, r.UserID); err != nil { return err } } @@ -539,13 +494,14 @@ type scanner interface{ Scan(...any) error } func scanKey(sc scanner) (Key, error) { var k Key - var band, profile sql.NullString + var band, profile, project, user sql.NullString var expires, revoked, lastUsed sql.NullInt64 var created int64 - if err := sc.Scan(&k.ID, &k.Hash, &k.Prefix, &k.Name, &k.ProjectID, - &band, &profile, &created, &expires, &revoked, &lastUsed); err != nil { + if err := sc.Scan(&k.ID, &k.Hash, &k.Prefix, &k.Name, &project, + &band, &profile, &created, &expires, &revoked, &lastUsed, &user); err != nil { return Key{}, err } + k.ProjectID, k.UserID = project.String, user.String k.Band, k.RoutingProfile = band.String, profile.String k.CreatedAt = fromMillis(created) k.ExpiresAt = fromNullMillis(expires) @@ -770,22 +726,21 @@ func (u UserUsage) Tokens() int64 { return u.Prompt + u.Completion } // existed — are grouped under an empty user, which is honest rather than tidy. func (s *DB) UsageByUser(ctx context.Context, since time.Time) ([]UserUsage, error) { rows, err := s.db.QueryContext(ctx, ` - SELECT COALESCE(k.user_id, ''), COALESCE(us.email, ''), COALESCE(us.name, ''), + SELECT u.user_id, COALESCE(us.email, ''), COALESCE(us.name, ''), COUNT(DISTINCT u.key_id), COUNT(*), SUM(u.prompt_tokens), SUM(u.completion_tokens), SUM(u.cost_micros), SUM(CASE WHEN u.status >= 400 THEN 1 ELSE 0 END), MAX(u.ts) FROM usage u - LEFT JOIN api_keys k ON k.id = u.key_id - LEFT JOIN users us ON us.id = k.user_id + LEFT JOIN users us ON us.id = NULLIF(u.user_id, '') WHERE u.ts >= ? -- Every selected column that is not an aggregate has to be grouped. -- SQLite picks an arbitrary row for the rest and carries on; PostgreSQL -- refuses, so this query worked in tests and returned nothing but an -- error on the cluster. The email and name are functionally dependent -- on the user id, so grouping by them changes no result. - GROUP BY COALESCE(k.user_id, ''), COALESCE(us.email, ''), COALESCE(us.name, '') + GROUP BY u.user_id, COALESCE(us.email, ''), COALESCE(us.name, '') ORDER BY SUM(u.cost_micros) DESC, COUNT(*) DESC`, millis(since)) if err != nil { return nil, err diff --git a/internal/store/store.go b/internal/store/store.go index 2cf517c..a89f10b 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -32,8 +32,38 @@ const ( ScopeTeam Scope = "team" ScopeProject Scope = "project" ScopeKey Scope = "key" + + // ScopeUser is a person, across every key they own and every project they + // use: "Ayşe may spend at most this, wherever she spends it". + ScopeUser Scope = "user" + + // The member scopes are a limit written on an org, team or project that + // applies to each member separately: "each member of this project gets at + // most this here". The limit is stored under the scope's own id; each + // member's use is counted under that id and theirs, see MemberOwner. + ScopeOrgMember Scope = "org-member" + ScopeTeamMember Scope = "team-member" + ScopeProjectMember Scope = "project-member" ) +// MemberScope is the per-member scope for a level of the tree, or "". +func MemberScope(level Scope) Scope { + switch level { + case ScopeOrg: + return ScopeOrgMember + case ScopeTeam: + return ScopeTeamMember + case ScopeProject: + return ScopeProjectMember + } + return "" +} + +// MemberOwner is the counter one member's use of a per-member limit goes to. +func MemberOwner(scope Scope, scopeID, userID string) LimitOwner { + return LimitOwner{Scope: scope, ID: scopeID + "/" + userID} +} + // Org, Team and Project are the tenancy tree. type Org struct { ID, Name string @@ -52,6 +82,8 @@ type Project struct { // Key is an API key as stored: never the key itself. type Key struct { ID string + // UserID owns the key; empty for one an operator issued in a project. + UserID string // Hash is sha256 of the key, and the only copy of anything derived from it. Hash []byte // Prefix is shown in listings so a key can be identified without revealing @@ -114,7 +146,10 @@ const ( // A team cannot raise a limit its organization set, which is the only // composition rule under which a budget means anything. type Effective struct { - KeyID string + KeyID string + // UserID is the key's owner, empty for a key an operator issued in a + // project. A personal key has an owner and no project. + UserID string ProjectID string TeamID string OrgID string @@ -192,6 +227,10 @@ func (e Effective) Allows(model string) bool { type Usage struct { At time.Time KeyID string + // UserID is who spent it, empty for a key with no owner. Recorded with the + // spend rather than looked up from the key later, so a key's past is not + // moved onto whoever owns it next. + UserID string // ProjectID is denormalized so reporting does not join on every row, and so // a row stays meaningful if the key is later revoked. ProjectID string @@ -287,6 +326,7 @@ type Bucket struct { // does. One query, and the folding happens where it can be tested. type ScopeSpend struct { KeyID string + UserID string ProjectID string TeamID string OrgID string diff --git a/internal/store/usageseries.go b/internal/store/usageseries.go index f24312a..71a5460 100644 --- a/internal/store/usageseries.go +++ b/internal/store/usageseries.go @@ -86,12 +86,12 @@ var groupColumns = map[Grouping]struct{ id, name, joins string }{ GroupByProject: {"p.id", "p.name", ` JOIN api_keys k ON k.id = u.key_id JOIN projects p ON p.id = k.project_id`}, - // Left joins, and coalesced: a key issued by an operator before accounts - // existed has no owner, and its traffic is real. It groups under an empty - // id, which the page names rather than hides. - GroupByUser: {"COALESCE(k.user_id, '')", "COALESCE(us.email, '')", ` - LEFT JOIN api_keys k ON k.id = u.key_id - LEFT JOIN users us ON us.id = k.user_id`}, + // Who spent it, as recorded with the spend — which includes personal keys + // and the chat page, neither of which a key's project reaches. Traffic + // from keys with no owner groups under an empty id, which the page names + // rather than hides. + GroupByUser: {"u.user_id", "COALESCE(us.email, '')", ` + LEFT JOIN users us ON us.id = NULLIF(u.user_id, '')`}, } // UsageBucketsBy returns each group's usage over time. diff --git a/internal/store/users.go b/internal/store/users.go index 4cf1b56..eb035e6 100644 --- a/internal/store/users.go +++ b/internal/store/users.go @@ -98,39 +98,45 @@ func (s *DB) Users(ctx context.Context, state UserState) ([]User, error) { }) } -// ApproveUser activates an account and places it in a project. +// ApproveUser activates an account, and puts it in a project when one is given. // -// The project is required: an approved account with nowhere to put its keys -// would pass every check and then fail at the first key it tried to make. +// A project is not required: a person need not belong to anything to use the +// gateway on their own, bounded by their personal limits. Memberships can be +// added at approval or at any time after. func (s *DB) ApproveUser(ctx context.Context, id, projectID, by string, at time.Time) error { - if projectID == "" { - return errors.New("approving an account needs a project for its keys") - } - res, err := s.db.ExecContext(ctx, - `UPDATE users SET state = ?, project_id = ?, approved_at = ?, approved_by = ? - WHERE id = ? AND state = ?`, - string(UserActive), projectID, millis(at), by, id, string(UserPending)) - if err != nil { + return s.inTx(ctx, func(c conn) error { + res, err := c.ExecContext(ctx, + `UPDATE users SET state = ?, approved_at = ?, approved_by = ? WHERE id = ? AND state = ?`, + string(UserActive), millis(at), by, id, string(UserPending)) + if err != nil { + return err + } + if n, _ := res.RowsAffected(); n == 0 { + return ErrNotFound + } + if projectID == "" { + return nil + } + _, err = c.ExecContext(ctx, ` + INSERT INTO memberships (user_id, scope_type, scope_id, role, added_at, added_by) + VALUES (?, 'project', ?, 'member', ?, ?) + ON CONFLICT (user_id, scope_type, scope_id) DO NOTHING`, + id, projectID, millis(at), nullString(by)) return err - } - if n, _ := res.RowsAffected(); n == 0 { - return ErrNotFound - } - return nil + }) } // SetUserState suspends or reinstates an account. // -// An account with no project cannot be made active. A pending account that -// was rejected and then reinstated used to become exactly that: able to sign -// in, and failing with "something went wrong" at the first key it made. -// Approving is what gives it a project. +// Only an account that was once approved can be reinstated. Otherwise a +// signup suspended while pending and then reinstated would become active +// without anybody approving it. func (s *DB) SetUserState(ctx context.Context, id string, state UserState) error { if state == UserActive { - var project sql.NullString - if err := s.db.QueryRowContext(ctx, `SELECT project_id FROM users WHERE id = ?`, id). - Scan(&project); err == nil && !project.Valid { - return errors.New("this account has no project; approve it into one instead") + var approved sql.NullInt64 + if err := s.db.QueryRowContext(ctx, `SELECT approved_at FROM users WHERE id = ?`, id). + Scan(&approved); err == nil && !approved.Valid { + return errors.New("this account was never approved; approve it instead") } } res, err := s.db.ExecContext(ctx, `UPDATE users SET state = ? WHERE id = ?`, string(state), id) @@ -175,17 +181,21 @@ func (s *DB) KeysForUser(ctx context.Context, userID string) ([]Key, error) { } // CreateUserKey issues a key owned by a person. +// +// With a project, the key is used there and the person must be able to use +// that project; without one it is personal, bounded by the person's own limits. func (s *DB) CreateUserKey(ctx context.Context, userID string, k Key) error { - if len(k.Hash) == 0 { - return errors.New("store: a key needs a hash") + if k.ProjectID != "" { + ok, err := s.CanUseProject(ctx, userID, k.ProjectID) + if err != nil { + return err + } + if !ok { + return ErrNotMember + } } - _, err := s.db.ExecContext(ctx, - `INSERT INTO api_keys (`+keyColumns+`, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - k.ID, k.Hash, k.Prefix, k.Name, k.ProjectID, - nullString(k.Band), nullString(k.RoutingProfile), - millis(k.CreatedAt), nullMillis(k.ExpiresAt), nullMillis(k.RevokedAt), - nullMillis(k.LastUsedAt), userID) - return err + k.UserID = userID + return s.CreateKey(ctx, k) } // RevokeUserKey revokes a key only if it belongs to this person. @@ -268,41 +278,3 @@ func (s *DB) AllProjects(ctx context.Context) ([]ProjectPath, error) { type ProjectPath struct { ID, Path string } - -// ResolveProject returns the limits an account inherits from its project. -// -// The same walk as Resolve, minus the key. A person signed in to the chat page -// has no key in their hand — the key was shown once and is theirs to keep — so -// their identity there is their project, and it must be resolved the same way -// or the page would be a hole in the limits everything else obeys. -func (s *DB) ResolveProject(ctx context.Context, projectID string) (Effective, error) { - var e Effective - row := s.db.QueryRowContext(ctx, - `SELECT p.id, t.id, o.id - FROM projects p - JOIN teams t ON t.id = p.team_id - JOIN organizations o ON o.id = t.org_id - WHERE p.id = ?`, projectID) - if err := row.Scan(&e.ProjectID, &e.TeamID, &e.OrgID); err != nil { - if errors.Is(err, sql.ErrNoRows) { - return Effective{}, ErrNotFound - } - return Effective{}, err - } - - // Widest first, so a narrower scope can only tighten what it inherits. - for _, step := range []struct { - scope Scope - id string - }{{ScopeOrg, e.OrgID}, {ScopeTeam, e.TeamID}, {ScopeProject, e.ProjectID}} { - l, err := s.LimitsFor(ctx, step.scope, step.id) - if errors.Is(err, ErrNotFound) { - continue - } - if err != nil { - return Effective{}, err - } - e.tighten(l) - } - return e, nil -} diff --git a/internal/web/account.go b/internal/web/account.go index ce6fc24..db40bfb 100644 --- a/internal/web/account.go +++ b/internal/web/account.go @@ -38,6 +38,10 @@ type KeyOwner interface { RevokeUserKey(ctx context.Context, userID, keyID string, at time.Time) error Resolve(ctx context.Context, keyID string) (store.Effective, error) LimitsFor(ctx context.Context, scope store.Scope, id string) (store.Limits, error) + // Where a person may make keys, and what applies there. + ProjectsFor(ctx context.Context, userID string) ([]store.ProjectPath, error) + MembershipsOf(ctx context.Context, userID string) ([]store.Membership, error) + ResolveFor(ctx context.Context, callerID, userID, projectID string) (store.Effective, error) } // AccountPages serves signup, sign-in and the profile. @@ -57,6 +61,10 @@ type AccountPages struct { hasMirror bool // hasQuotas is set when /admin/quotas is registered. hasQuotas bool + // personalOff refuses personal keys; see SetPersonalOff. + personalOff bool + // personalDefaults is what personal use falls under by default. + personalDefaults *store.Limits // priced says whether a budget can ever be spent against. priced Priced // registry browses a model registry, or is nil where there is no way out. @@ -216,13 +224,26 @@ func (p *AccountPages) logout(w http.ResponseWriter, r *http.Request) { type limitRow struct{ Name, Value string } +// contextLimits is what applies to a person in one place they can spend. +type contextLimits struct { + Label string + Rows []limitRow +} + type profileData struct { - Nav Nav - User store.User - Keys []store.Key - Limits []limitRow - NewKey string - Error string + Nav Nav + // Contexts are where a key can be made: the person's projects, and + // themselves when personal use is on. + Contexts []ChatContext + // Where names each key's project by path; a personal key has none. + Where map[string]string + Memberships []store.Membership + LimitsBy []contextLimits + User store.User + Keys []store.Key + Limits []limitRow + NewKey string + Error string PasswordChanged bool PasswordError string @@ -263,7 +284,15 @@ func (p *AccountPages) createKey(w http.ResponseWriter, r *http.Request) { if name == "" { name = "untitled" } - secret, k, err := store.NewKey(randomKeyID(), name, user.ProjectID) + project := r.FormValue("context") + if project == personalValue { + if p.personalOff { + p.renderProfile(w, r, sess, user, "", "personal keys are off on this gateway; choose a project") + return + } + project = "" + } + secret, k, err := store.NewKey(randomKeyID(), name, project) if err != nil { p.log.Error("could not mint a key", "err", err) p.renderProfile(w, r, sess, user, "", "something went wrong; try again") @@ -271,6 +300,10 @@ func (p *AccountPages) createKey(w http.ResponseWriter, r *http.Request) { } k.CreatedAt = time.Now() if err := p.keys.CreateUserKey(r.Context(), user.ID, k); err != nil { + if errors.Is(err, store.ErrNotMember) { + p.renderProfile(w, r, sess, user, "", "you are not a member of that project") + return + } p.log.Error("could not store a key", "err", err) p.renderProfile(w, r, sess, user, "", "something went wrong; try again") return @@ -331,34 +364,64 @@ func (p *AccountPages) render(w http.ResponseWriter, r *http.Request, p.log.Error("could not list keys", "err", err) } d.Keys = keys - d.Limits = p.limitsFor(r.Context(), user) + p.fillPlaces(r.Context(), user, &d) render(w, "profile", d) } -// limitsFor renders the limits a person's keys inherit from their project. -// -// From the project rather than from a key, because a person with no keys yet -// still wants to know what they are getting. -func (p *AccountPages) limitsFor(ctx context.Context, user store.User) []limitRow { - l, err := p.keys.LimitsFor(ctx, store.ScopeProject, user.ProjectID) +// personalValue is how a form names the person's own, project-less use. +const personalValue = "personal" + +// SetPersonalOff says personal keys are refused on this gateway. +func (p *AccountPages) SetPersonalOff(off bool) { p.personalOff = off } + +// fillPlaces adds where the person can make keys, where their keys are, and +// what applies in each place, resolved the way a request is: the whole chain +// above a project, per-member limits, and the person's own. +func (p *AccountPages) fillPlaces(ctx context.Context, user store.User, d *profileData) { + d.Where = map[string]string{} + projects, err := p.keys.ProjectsFor(ctx, user.ID) if err != nil { - return nil + p.log.Warn("could not list a person's projects", "err", err) } + for _, pp := range projects { + d.Contexts = append(d.Contexts, ChatContext{Value: pp.ID, Label: pp.Path}) + d.Where[pp.ID] = pp.Path + } + if !p.personalOff { + d.Contexts = append(d.Contexts, ChatContext{Value: personalValue, Label: "Personal"}) + } + if d.Memberships, err = p.keys.MembershipsOf(ctx, user.ID); err != nil { + p.log.Warn("could not list a person's memberships", "err", err) + } + for _, c := range d.Contexts { + project := c.Value + if project == personalValue { + project = "" + } + e, err := p.keys.ResolveFor(ctx, "session:"+user.ID, user.ID, project) + if err != nil { + continue + } + d.LimitsBy = append(d.LimitsBy, contextLimits{Label: c.Label, Rows: limitRows(e)}) + } +} + +func limitRows(e store.Effective) []limitRow { var out []limitRow - if l.RPM != nil { - out = append(out, limitRow{"Requests per minute", itoa(*l.RPM)}) + if e.RPM != nil { + out = append(out, limitRow{"Requests per minute", itoa(*e.RPM)}) } - if l.TPM != nil { - out = append(out, limitRow{"Tokens per minute", itoa(*l.TPM)}) + if e.TPM != nil { + out = append(out, limitRow{"Tokens per minute", itoa(*e.TPM)}) } - if l.Budget != nil { - out = append(out, limitRow{"Budget", units(*l.Budget) + " per " + string(l.BudgetPeriod)}) + if e.Budget != nil { + out = append(out, limitRow{"Budget", units(*e.Budget) + " per " + string(e.BudgetPeriod)}) } - if l.AllowedModels != nil { - if len(l.AllowedModels) == 0 { + if e.AllowedModels != nil { + if len(e.AllowedModels) == 0 { out = append(out, limitRow{"Models", "none"}) } else { - out = append(out, limitRow{"Models", strings.Join(l.AllowedModels, ", ")}) + out = append(out, limitRow{"Models", strings.Join(e.AllowedModels, ", ")}) } } return out @@ -534,6 +597,6 @@ func (p *AccountPages) renderWithCSRF(w http.ResponseWriter, r *http.Request, p.log.Error("could not list keys", "err", err) } d.Keys = keys - d.Limits = p.limitsFor(r.Context(), user) + p.fillPlaces(r.Context(), user, &d) render(w, "profile", d) } diff --git a/internal/web/admin_quotas.go b/internal/web/admin_quotas.go index d848467..0b78c46 100644 --- a/internal/web/admin_quotas.go +++ b/internal/web/admin_quotas.go @@ -30,6 +30,22 @@ type QuotaAdmin interface { // which put projects under the wrong team when two organizations shared a // name. Tree(ctx context.Context) ([]store.TreeOrg, error) + // People have limits of their own, across every key they hold. + Users(ctx context.Context, state store.UserState) ([]store.User, error) + UsageByUser(ctx context.Context, since time.Time) ([]store.UserUsage, error) +} + +// personRow is one person's own limits: across all their keys and projects. +type personRow struct { + ID, Email string + RPM, TPM string + Budget, Period string + Models string + HasAny bool + Spend string + Percent int + HasPct bool + Editing bool } // quotaRow is one place a quota can be written, and what is written there. @@ -80,11 +96,27 @@ type quotaRow struct { // ModelsFrom names the nearest ancestor with a model list, for a row with // none of its own. ModelsFrom string + + // Member is the per-member limit written here, summarised; MemberRaw holds + // its fields for the form, and MemberEditing opens that form. + Member string + MemberRaw quotaFields + MemberEditing bool +} + +// quotaFields is a limit as the form shows it. +type quotaFields struct { + RPM, TPM, Budget, Period, Models string + HasAny bool } type adminQuotasData struct { - Nav Nav - Rows []quotaRow + Nav Nav + Rows []quotaRow + People []personRow + // Personal summarises the defaults a person's own use falls under when + // nobody set a limit for them. + Personal string // BudgetsBind is false when no model has a price, which makes every budget // on this page a number that cannot move. Shown, because a limit that binds // on nothing and reads as "0% used" is worse than no limit at all. @@ -147,9 +179,10 @@ func (p *AccountPages) applyQuota(r *http.Request, s QuotaAdmin, admin store.Use // one that was clicked rather than one somebody named again in a box. scope, id := store.Scope(r.FormValue("scope")), r.FormValue("id") switch scope { - case store.ScopeOrg, store.ScopeTeam, store.ScopeProject: + case store.ScopeOrg, store.ScopeTeam, store.ScopeProject, store.ScopeUser, + store.ScopeOrgMember, store.ScopeTeamMember, store.ScopeProjectMember: default: - return "", errors.New("a quota belongs to an organization, a team or a project") + return "", errors.New("a quota belongs to an organization, team, project or person") } if id == "" { return "", errors.New("which one?") @@ -237,6 +270,16 @@ func (p *AccountPages) renderQuotas(w http.ResponseWriter, r *http.Request, d.Error = "could not read the organizations" } d.Rows = quotaRows(tree, limits, spend) + for i := range d.Rows { + ms := store.MemberScope(d.Rows[i].Scope) + if l, ok := limits[store.LimitOwner{Scope: ms, ID: d.Rows[i].ID}.CounterKey()]; ok { + d.Rows[i].MemberRaw = fieldsOf(l) + d.Rows[i].Member = describeLimits(l) + } + d.Rows[i].MemberEditing = d.Editing == string(ms)+":"+d.Rows[i].ID + } + d.People = p.personRows(r.Context(), s, limits, time.Now().Add(-window), d.Editing) + d.Personal = p.personalSummary() for i := range d.Rows { d.Rows[i].Editing = d.Editing == string(d.Rows[i].Scope)+":"+d.Rows[i].ID @@ -431,3 +474,59 @@ func dashUnits(v *int64) string { } return units(*v) } + +func fieldsOf(l store.Limits) quotaFields { + return quotaFields{ + RPM: dash(l.RPM), TPM: dash(l.TPM), Budget: dashUnits(l.Budget), + Period: string(l.BudgetPeriod), Models: strings.Join(l.AllowedModels, ", "), + HasAny: l.RPM != nil || l.TPM != nil || l.Budget != nil || l.AllowedModels != nil, + } +} + +// personRows lists every approved person with the limits written for them and +// what they spent, wherever they spent it. +func (p *AccountPages) personRows(ctx context.Context, s QuotaAdmin, + limits map[string]store.Limits, since time.Time, editing string, +) []personRow { + users, err := s.Users(ctx, "") + if err != nil { + p.log.Warn("could not list people for the quota page", "err", err) + return nil + } + spent := map[string]int64{} + if usage, err := s.UsageByUser(ctx, since); err == nil { + for _, u := range usage { + spent[u.UserID] = u.CostMicros + } + } + var out []personRow + for _, u := range users { + if u.State == store.UserPending { + continue + } + l := limits[store.LimitOwner{Scope: store.ScopeUser, ID: u.ID}.CounterKey()] + f := fieldsOf(l) + row := personRow{ID: u.ID, Email: u.Email, RPM: f.RPM, TPM: f.TPM, Budget: f.Budget, + Period: f.Period, Models: f.Models, HasAny: f.HasAny, Spend: units(spent[u.ID]), + Editing: editing == "user:"+u.ID} + if l.Budget != nil && *l.Budget > 0 { + row.Percent, row.HasPct = int(spent[u.ID]*100 / *l.Budget), true + } + out = append(out, row) + } + return out +} + +// SetPersonalDefaults tells the quota page what a person's own use falls under +// when nobody set a limit for them. +func (p *AccountPages) SetPersonalDefaults(l *store.Limits) { p.personalDefaults = l } + +func (p *AccountPages) personalSummary() string { + if p.personalOff { + return "Personal use is off: people use the gateway only through their projects." + } + if p.personalDefaults == nil { + return "Personal use has no default limit: someone with no limit of their own is bounded only by the cluster-wide defaults." + } + return "With no limit of their own, a person's personal use is held to " + describeLimits(*p.personalDefaults) + "." +} diff --git a/internal/web/admin_quotas_test.go b/internal/web/admin_quotas_test.go index 121a15f..b3aebc4 100644 --- a/internal/web/admin_quotas_test.go +++ b/internal/web/admin_quotas_test.go @@ -18,6 +18,7 @@ type fakeQuotas struct { teams []store.TeamPath projects []store.ProjectPath limits map[string]store.Limits + users []store.User usage map[store.Scope][]store.ScopeUsage put []store.Limits @@ -76,6 +77,14 @@ type pricedNo struct{} func (pricedNo) AnythingPriced() bool { return false } // oneTeam is an org with a team and a project under it. +func (f *fakeQuotas) Users(context.Context, store.UserState) ([]store.User, error) { + return f.users, nil +} + +func (f *fakeQuotas) UsageByUser(context.Context, time.Time) ([]store.UserUsage, error) { + return nil, nil +} + // Tree derives the tree from the flat lists the fake is given. func (f *fakeQuotas) Tree(context.Context) ([]store.TreeOrg, error) { var out []store.TreeOrg @@ -391,3 +400,41 @@ func TestALargerLimitBelowSaysItIsCapped(t *testing.T) { } func ptrInt(v int) *int { return &v } + +func TestAPerMemberAndAPersonsLimitAreSavedWhereTheyBelong(t *testing.T) { + q := oneTeam() + q.users = []store.User{{ID: "usr-a", Email: "ayse@x", State: store.UserActive}} + mux := quotasPage(t, q, true) + + mirrorPost(t, mux, "/admin/quotas", url.Values{"csrf": {"tok"}, + "scope": {"project-member"}, "id": {"proj-1"}, "rpm": {"20"}}) + mirrorPost(t, mux, "/admin/quotas", url.Values{"csrf": {"tok"}, + "scope": {"user"}, "id": {"usr-a"}, "budget": {"50"}, "period": {"month"}}) + + if len(q.put) != 2 { + t.Fatalf("saved %d limits, want 2", len(q.put)) + } + if q.put[0].Scope != store.ScopeProjectMember || q.put[0].ScopeID != "proj-1" || *q.put[0].RPM != 20 { + t.Errorf("per-member limit saved as %+v", q.put[0]) + } + if q.put[1].Scope != store.ScopeUser || q.put[1].ScopeID != "usr-a" || *q.put[1].Budget != 50_000_000 { + t.Errorf("person's limit saved as %+v", q.put[1]) + } +} + +func TestThePeopleSectionShowsEachPersonsOwnLimit(t *testing.T) { + q := oneTeam() + q.users = []store.User{{ID: "usr-a", Email: "ayse@x", State: store.UserActive}} + q.limits[store.LimitOwner{Scope: store.ScopeUser, ID: "usr-a"}.CounterKey()] = + store.Limits{Scope: store.ScopeUser, ScopeID: "usr-a", RPM: ptrInt(7)} + q.limits[store.LimitOwner{Scope: store.ScopeProjectMember, ID: "proj-1"}.CounterKey()] = + store.Limits{Scope: store.ScopeProjectMember, ScopeID: "proj-1", RPM: ptrInt(20)} + body := strings.Join(strings.Fields( + mirrorGet(t, quotasPage(t, q, true), "/admin/quotas").Body.String()), " ") + if !strings.Contains(body, `ayse@x`) || !strings.Contains(body, `7`) { + t.Error("the person and their limit are not on the page") + } + if !strings.Contains(body, "each member here: rpm 20") { + t.Error("the project's per-member limit is not shown") + } +} diff --git a/internal/web/admin_tenancy_test.go b/internal/web/admin_tenancy_test.go index 0f1fa4e..42566c9 100644 --- a/internal/web/admin_tenancy_test.go +++ b/internal/web/admin_tenancy_test.go @@ -23,6 +23,7 @@ type fakeAdmin struct { createdOrg [1]string createdTeam [2]string createdProject [2]string + joined, left []string err error } @@ -67,18 +68,26 @@ func (f *fakeAdmin) Tree(context.Context) ([]store.TreeOrg, error) { } return out, nil } -func (f *fakeAdmin) RenameOrg(context.Context, string, string) error { return f.err } -func (f *fakeAdmin) RenameTeam(context.Context, string, string) error { return f.err } -func (f *fakeAdmin) RenameProject(context.Context, string, string) error { return f.err } -func (f *fakeAdmin) MoveTeam(context.Context, string, string) error { return f.err } -func (f *fakeAdmin) MoveProject(context.Context, string, string) error { return f.err } -func (f *fakeAdmin) DeleteOrg(context.Context, string) error { return f.err } -func (f *fakeAdmin) DeleteTeam(context.Context, string) error { return f.err } -func (f *fakeAdmin) DeleteProject(context.Context, string) error { return f.err } -func (f *fakeAdmin) MoveUser(context.Context, string, string) (int, error) { return 0, f.err } -func (f *fakeAdmin) DeleteUser(context.Context, string, time.Time) error { return f.err } -func (f *fakeAdmin) AllKeys(context.Context) ([]store.KeyRow, error) { return nil, nil } -func (f *fakeAdmin) RevokeKey(context.Context, string, time.Time) error { return f.err } +func (f *fakeAdmin) RenameOrg(context.Context, string, string) error { return f.err } +func (f *fakeAdmin) RenameTeam(context.Context, string, string) error { return f.err } +func (f *fakeAdmin) RenameProject(context.Context, string, string) error { return f.err } +func (f *fakeAdmin) MoveTeam(context.Context, string, string) error { return f.err } +func (f *fakeAdmin) MoveProject(context.Context, string, string) error { return f.err } +func (f *fakeAdmin) DeleteOrg(context.Context, string) error { return f.err } +func (f *fakeAdmin) DeleteTeam(context.Context, string) error { return f.err } +func (f *fakeAdmin) DeleteProject(context.Context, string) error { return f.err } +func (f *fakeAdmin) AddMembership(_ context.Context, user string, scope store.Scope, id, _, _ string, _ time.Time) error { + f.joined = append(f.joined, user+"@"+string(scope)+":"+id) + return f.err +} +func (f *fakeAdmin) RemoveMembership(_ context.Context, user string, scope store.Scope, id string, _ time.Time) (int, error) { + f.left = append(f.left, user+"@"+string(scope)+":"+id) + return 1, f.err +} +func (f *fakeAdmin) AllMemberships(context.Context) ([]store.Membership, error) { return nil, nil } +func (f *fakeAdmin) DeleteUser(context.Context, string, time.Time) error { return f.err } +func (f *fakeAdmin) AllKeys(context.Context) ([]store.KeyRow, error) { return nil, nil } +func (f *fakeAdmin) RevokeKey(context.Context, string, time.Time) error { return f.err } func tenancyPage(t *testing.T, s *fakeAdmin, admin bool) *http.ServeMux { t.Helper() diff --git a/internal/web/admin_users.go b/internal/web/admin_users.go index 167e956..a7a0f9a 100644 --- a/internal/web/admin_users.go +++ b/internal/web/admin_users.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/http" + "sort" "strings" "time" @@ -41,7 +42,9 @@ type AccountAdmin interface { DeleteOrg(ctx context.Context, id string) error DeleteTeam(ctx context.Context, id string) error DeleteProject(ctx context.Context, id string) error - MoveUser(ctx context.Context, userID, projectID string) (int, error) + AddMembership(ctx context.Context, userID string, scope store.Scope, scopeID, role, by string, at time.Time) error + RemoveMembership(ctx context.Context, userID string, scope store.Scope, scopeID string, at time.Time) (int, error) + AllMemberships(ctx context.Context) ([]store.Membership, error) DeleteUser(ctx context.Context, userID string, at time.Time) error AllKeys(ctx context.Context) ([]store.KeyRow, error) RevokeKey(ctx context.Context, id string, at time.Time) error @@ -50,7 +53,17 @@ type AccountAdmin interface { // userRow is an account as the page shows it: with where it is, not an id. type userRow struct { store.User - ProjectPath string + // Memberships are where the person is, by path; none is a person who uses + // the gateway on their own. + Memberships []store.Membership + // Role is their role in the scope a list of members is drawn for. + Role string +} + +// scopeOption is somewhere a person can be put, for a chooser. +type scopeOption struct { + Value string // "project:proj-…" + Label string // "Acme / ml / training" } // keyRow is a key as the page shows it. @@ -64,8 +77,12 @@ type adminUsersData struct { Pending []store.User Accounts []userRow Tree []store.TreeOrg - // People and Keys are keyed by project id. - People map[string][]userRow + // Members is each scope's own members, keyed "project:" and likewise + // for teams and organizations; Keys is keyed by project id. + Members map[string][]userRow + // Scopes is every place a person can be added, organizations and teams + // included. + Scopes []scopeOption Keys map[string][]keyRow Projects []store.ProjectPath Orgs []store.Org @@ -105,12 +122,12 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) { mux.HandleFunc("POST /admin/users/{id}/approve", p.adminAction(s, func(ctx context.Context, admin store.User, id string, r *http.Request) (string, error) { project := r.FormValue("project") - if project == "" { - return "", errors.New("choose a project for their keys") - } if err := s.ApproveUser(ctx, id, project, admin.Email, time.Now()); err != nil { return "", err } + if project == "" { + return "approved, on their own for now", nil + } return "approved into " + projectPath(ctx, s, project), nil })) @@ -140,17 +157,35 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) { return "reinstated", nil })) - mux.HandleFunc("POST /admin/users/{id}/move", p.adminAction(s, - func(ctx context.Context, _ store.User, id string, r *http.Request) (string, error) { - project := r.FormValue("project") - if project == "" { - return "", errors.New("choose the project they go to") + mux.HandleFunc("POST /admin/users/{id}/join", p.adminAction(s, + func(ctx context.Context, admin store.User, id string, r *http.Request) (string, error) { + scope, scopeID, ok := strings.Cut(r.FormValue("scope"), ":") + if !ok || scopeID == "" { + return "", errors.New("choose where they go") } - moved, err := s.MoveUser(ctx, id, project) + if err := s.AddMembership(ctx, id, store.Scope(scope), scopeID, store.RoleMember, + admin.Email, time.Now()); err != nil { + return "", err + } + return "added to " + scopeLabel(ctx, s, store.Scope(scope), scopeID), nil + })) + + mux.HandleFunc("POST /admin/users/{id}/leave", p.adminAction(s, + func(ctx context.Context, _ store.User, id string, r *http.Request) (string, error) { + scope, scopeID, ok := strings.Cut(r.FormValue("scope"), ":") + if !ok { + return "", errors.New("nothing to leave") + } + label := scopeLabel(ctx, s, store.Scope(scope), scopeID) + revoked, err := s.RemoveMembership(ctx, id, store.Scope(scope), scopeID, time.Now()) if err != nil { return "", err } - return fmt.Sprintf("moved to %s, with %s", projectPath(ctx, s, project), plural(moved, "key", "keys")), nil + msg := "removed from " + label + if revoked > 0 { + msg += "; " + plural(revoked, "key", "keys") + " they made there revoked" + } + return msg, nil })) mux.HandleFunc("POST /admin/users/{id}/delete", p.adminAction(s, @@ -264,6 +299,25 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) { return "removed", nil })) + // The same as joining, from the scope's side: a form on a project, team or + // organization that names the person. + mux.HandleFunc("POST /admin/tenancy/{kind}/{id}/add", p.tenancyAction(s, + func(ctx context.Context, r *http.Request) (string, error) { + user := r.FormValue("user") + if user == "" { + return "", errors.New("choose who to add") + } + scope, id := store.Scope(r.PathValue("kind")), r.PathValue("id") + if err := s.AddMembership(ctx, user, scope, id, store.RoleMember, "", time.Now()); err != nil { + return "", err + } + who := user + if u, err := s.UserByID(ctx, user); err == nil { + who = u.Email + } + return who + " added to " + scopeLabel(ctx, s, scope, id), nil + })) + mux.HandleFunc("POST /admin/keys/{id}/revoke", p.tenancyAction(s, func(ctx context.Context, r *http.Request) (string, error) { if err := s.RevokeKey(ctx, r.PathValue("id"), time.Now()); err != nil { @@ -285,6 +339,31 @@ func projectPath(ctx context.Context, s AccountAdmin, id string) string { return id } +// scopeLabel names an org, team or project by its path. +func scopeLabel(ctx context.Context, s AccountAdmin, scope store.Scope, id string) string { + switch scope { + case store.ScopeProject: + return projectPath(ctx, s, id) + case store.ScopeTeam: + if teams, err := s.AllTeams(ctx); err == nil { + for _, t := range teams { + if t.ID == id { + return t.OrgName + " / " + t.Name + } + } + } + case store.ScopeOrg: + if orgs, err := s.AllOrgs(ctx); err == nil { + for _, o := range orgs { + if o.ID == id { + return o.Name + } + } + } + } + return id +} + // plainError says what a store error means to the person who caused it. func plainError(err error) error { switch { @@ -393,7 +472,7 @@ func (p *AccountPages) renderUsers(w http.ResponseWriter, r *http.Request, HasMirror: p.hasMirror, HasQuotas: p.hasQuotas, Now: time.Now().UTC().Format(time.RFC3339), - People: map[string][]userRow{}, + Members: map[string][]userRow{}, Keys: map[string][]keyRow{}, } var err error @@ -415,14 +494,26 @@ func (p *AccountPages) renderUsers(w http.ResponseWriter, r *http.Request, all, err := s.Users(ctx, "") logErr("the accounts", err) + memberships, err := s.AllMemberships(ctx) + logErr("the memberships", err) + byUser := map[string][]store.Membership{} + for _, m := range memberships { + byUser[m.UserID] = append(byUser[m.UserID], m) + } + users := map[string]store.User{} for _, u := range all { - if u.State == store.UserPending && u.ProjectID == "" { + users[u.ID] = u + if u.State == store.UserPending { d.Pending = append(d.Pending, u) continue } - row := userRow{User: u, ProjectPath: paths[u.ProjectID]} - d.Accounts = append(d.Accounts, row) - d.People[u.ProjectID] = append(d.People[u.ProjectID], row) + d.Accounts = append(d.Accounts, userRow{User: u, Memberships: byUser[u.ID]}) + } + for _, m := range memberships { + if u, ok := users[m.UserID]; ok { + k := string(m.Scope) + ":" + m.ScopeID + d.Members[k] = append(d.Members[k], userRow{User: u, Memberships: byUser[u.ID], Role: m.Role}) + } } keys, err := s.AllKeys(ctx) @@ -438,6 +529,16 @@ func (p *AccountPages) renderUsers(w http.ResponseWriter, r *http.Request, logErr("the organizations", err) d.Teams, err = s.AllTeams(ctx) logErr("the teams", err) + for _, o := range d.Orgs { + d.Scopes = append(d.Scopes, scopeOption{"org:" + o.ID, o.Name}) + } + for _, t := range d.Teams { + d.Scopes = append(d.Scopes, scopeOption{"team:" + t.ID, t.OrgName + " / " + t.Name}) + } + for _, pp := range d.Projects { + d.Scopes = append(d.Scopes, scopeOption{"project:" + pp.ID, pp.Path}) + } + sort.Slice(d.Scopes, func(i, j int) bool { return d.Scopes[i].Label < d.Scopes[j].Label }) render(w, "admin_users", d) } diff --git a/internal/web/chat.go b/internal/web/chat.go index cb7c545..932495a 100644 --- a/internal/web/chat.go +++ b/internal/web/chat.go @@ -20,21 +20,31 @@ type ChatModel struct { OnDemand bool } +// ChatContext is one thing the chat page can spend against: a project, or the +// person themselves. +type ChatContext struct { + Value string + Label string +} + type chatData struct { - Nav Nav - User store.User - Models []ChatModel - CSRF string - Version string - Now string + Nav Nav + Contexts []ChatContext + Context string + User store.User + Models []ChatModel + CSRF string + Version string + Now string } // Chat renders the in-browser chat for one signed-in person. -func Chat(user store.User, models []ChatModel, version string) http.HandlerFunc { +func Chat(user store.User, models []ChatModel, contexts []ChatContext, current, version string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { noStore(w) render(w, "chat", chatData{ - Nav: navFrom(r, "chat"), + Nav: navFrom(r, "chat"), + Contexts: contexts, Context: current, User: user, Models: models, CSRF: csrfFor(r), Version: version, Now: time.Now().UTC().Format(time.RFC3339), }) diff --git a/internal/web/chat_test.go b/internal/web/chat_test.go index c143330..9b51521 100644 --- a/internal/web/chat_test.go +++ b/internal/web/chat_test.go @@ -20,7 +20,7 @@ func chatPage(t *testing.T, names ...string) string { func chatPageWith(t *testing.T, models []ChatModel) string { t.Helper() - h := Chat(store.User{Email: "a@example.com", ProjectID: "proj"}, models, "v1") + h := Chat(store.User{Email: "a@example.com"}, models, nil, "", "v1") r := httptest.NewRequest(http.MethodGet, "http://x/chat", nil) w := httptest.NewRecorder() h(w, r) diff --git a/internal/web/nav.go b/internal/web/nav.go index 430c303..4576915 100644 --- a/internal/web/nav.go +++ b/internal/web/nav.go @@ -24,15 +24,6 @@ type Nav struct { Mirror, Quotas bool // CSRF signs the sign-out form. CSRF string - // Project is where the signed-in person's keys live, as a path. Carried - // here because every page that says whose session it is wants it, and the - // id it replaces ("project proj-3kq7…") told nobody anything. - Project string -} - -// projectPaths is the part of the store that can name a project. -type projectPaths interface { - AllProjects(ctx context.Context) ([]store.ProjectPath, error) } type navKey struct{} @@ -45,16 +36,6 @@ func (p *AccountPages) navFor(r *http.Request, user store.User, current string) Mirror: p.hasMirror, Quotas: p.hasQuotas, CSRF: csrfFor(r), - Project: user.ProjectID, - } - if pp, ok := p.keys.(projectPaths); ok && user.ProjectID != "" { - if all, err := pp.AllProjects(r.Context()); err == nil { - for _, x := range all { - if x.ID == user.ProjectID { - n.Project = x.Path - } - } - } } return n } diff --git a/internal/web/static/base.css b/internal/web/static/base.css index 7ff5209..de6da73 100644 --- a/internal/web/static/base.css +++ b/internal/web/static/base.css @@ -430,3 +430,17 @@ table.quota-tree tr.q-depth-0 .q-name { font-weight: 600; } details.explain { margin-top: 1.5rem; font-size: .88rem; color: var(--muted); } details.explain > summary { cursor: pointer; color: var(--accent); } details.explain p { max-width: 48rem; } + +/* Memberships as removable chips. */ +.chip-m { display: inline-flex; align-items: center; gap: .2rem; font-size: .78rem; + border: 1px solid var(--line); border-radius: 999px; padding: .05rem .15rem .05rem .55rem; + margin: .1rem .25rem .1rem 0; background: var(--card); white-space: nowrap; } +.chip-m form.inline { display: inline; } +.chip-x { background: none; color: var(--muted); padding: 0 .35rem; font-weight: 400; line-height: 1.2; } +.chip-x:hover { color: var(--bad); filter: none; } +td.where { max-width: 26rem; } +.scope-members { background: var(--card); border: 1px solid var(--line); border-radius: 6px; + padding: .5rem .6rem; margin: .4rem 0; min-width: 24rem; } +h3.limits-for { font-size: .92rem; margin: 1rem 0 .4rem; } +table.narrow-table { max-width: 30rem; } +.q-act a + a { margin-left: .8rem; } diff --git a/internal/web/templates/account.html b/internal/web/templates/account.html index 2c1df48..97a2390 100644 --- a/internal/web/templates/account.html +++ b/internal/web/templates/account.html @@ -51,7 +51,7 @@ Tell whoever you asked for access that you have signed up.

Your keys

-

{{.User.Email}}{{if ne .User.Name .User.Email}} ({{.User.Name}}){{end}} · {{or .Nav.Project "no project"}}

+

{{.User.Email}}{{if ne .User.Name .User.Email}} ({{.User.Name}}){{end}}

{{template "nav" .Nav}}
@@ -72,15 +72,25 @@ Tell whoever you asked for access that you have signed up.

+ {{if .Contexts}} + + {{else}} +

You are in no project yet, and personal keys are off here. + Ask an administrator to add you to one.

+ {{end}}

Keys

{{if .Keys}} - +
NameKeyCreatedLast usedState
{{range .Keys}} + @@ -111,11 +121,21 @@ signed in on has been signed out.

{{end}}

Changing it signs out every other browser, which is the point if you are changing it because something went wrong.

+

Where you are

+{{if .Memberships}} +

{{range $i, $m := .Memberships}}{{if $i}} · {{end}}{{$m.Path}} {{$m.Scope}}{{end}}

+{{else}}

You are not a member of any organization, team or project.

{{end}} +

Your limits

-{{if .Limits}} -
NameKeySpends againstCreatedLast usedState
{{.Name}} {{.Prefix}}{{if .ProjectID}}{{or (index $.Where .ProjectID) "a project you have left"}}{{else}}Personal{{end}} {{date .CreatedAt}} {{ago .LastUsedAt}} {{state .}}
-{{range .Limits}}{{end}}
LimitValue
{{.Name}}{{.Value}}
-{{else}}

No limits are set for you.

{{end}} +{{range .LimitsBy}} +

{{.Label}}

+{{if .Rows}} + +{{range .Rows}}{{end}}
LimitValue
{{.Name}}{{.Value}}
+{{else}}

No limits here.

{{end}} +{{else}}

Nowhere to spend yet.

{{end}} +

Everything that applies, from the organization down to you: the +strictest value wins.

llmgw {{.Version}}{{.Now}}
{{end}} diff --git a/internal/web/templates/admin_quotas.html b/internal/web/templates/admin_quotas.html index f3753ee..646dede 100644 --- a/internal/web/templates/admin_quotas.html +++ b/internal/web/templates/admin_quotas.html @@ -43,6 +43,7 @@ budgets bind.

{{.Path}} {{.Scope}} {{if .Models}}
only {{.Models}}
{{else if .ModelsFrom}}
models limited by {{.ModelsFrom}}
{{end}} + {{if .Member}}
each member here: {{.Member}}
{{end}} {{if .Shadowed}} {{/* A value that is written here and never reached. Resolution takes the most restrictive along the chain, so without saying so the page would @@ -61,8 +62,17 @@ budgets bind.

{{if .Editing}}close {{else}}{{if .HasAny}}change{{else}}set{{end}}{{end}} + {{if .MemberEditing}}close + {{else}}per member{{end}} +{{if .MemberEditing}} + + {{template "quota-form" (dict "Scope" (printf "%s-member" .Scope) "ID" .ID "F" .MemberRaw "CSRF" $.CSRF + "Title" (printf "Each member of %s, separately" .Crumb) + "Note" "Every member gets this much here, counted apart from the others — a fair share rather than one pool.")}} + +{{end}} {{if .Editing}} @@ -105,6 +115,38 @@ up — hover it to see where. A value set here is in normal type.

people page first.

{{end}} +

People

+

A person's own limit covers everything they do, across all their +keys, projects and the chat page. {{.Personal}}

+{{if .People}} + + + +{{range .People}} + + + + + + + + +{{if .Editing}} + +{{end}} +{{end}} +
PersonRequests/minTokens/minBudgetSpent
{{.Email}} + {{if .Models}}
only {{.Models}}
{{end}}
{{or .RPM "—"}}{{or .TPM "—"}}{{if .Budget}}{{.Budget}}{{if .Period}}/{{.Period}}{{end}}{{else}}—{{end}}{{.Spend}} + {{if .HasPct}} + {{.Percent}}%{{end}}{{if .Editing}}close + {{else}}{{if .HasAny}}change{{else}}set{{end}}{{end}}
+ {{template "quota-form" (dict "Scope" "user" "ID" .ID "CSRF" $.CSRF + "F" (dict "RPM" .RPM "TPM" .TPM "Budget" .Budget "Period" .Period "Models" .Models "HasAny" .HasAny) + "Title" (printf "%s, everywhere" .Email) + "Note" "Across every key this person holds, every project they use and the chat page.")}} +
+{{else}}

Nobody has been approved yet.

{{end}} +
How limits combine

Along a branch the most restrictive value wins, so a team cannot raise what @@ -122,4 +164,32 @@ up — hover it to see where. A value set here is in normal type.

{{/* A limit as the table shows it: the row's own value, else the one it is held to from above, marked as inherited and saying from where. */}} +{{/* The form for a limit that is not a branch's own: per member, or a person. */}} +{{define "quota-form"}} +
+ + + +

{{.Title}}. {{.Note}} A blank field means no limit of that kind.

+
+ + + + +
+ +
+ + Cancel + +
+
+{{end}} + {{define "quota-value"}}{{if .Own}}{{.Own}}{{else if .Up}}{{.Up}} ↑{{else}}—{{end}}{{end}} diff --git a/internal/web/templates/admin_users.html b/internal/web/templates/admin_users.html index 8aa9144..19d0d95 100644 --- a/internal/web/templates/admin_users.html +++ b/internal/web/templates/admin_users.html @@ -22,19 +22,18 @@ {{if .Pending}}

Waiting for approval

- +
EmailNameSigned upPut them in
{{range .Pending}} - - - {{if .Path}}{{end}} - - - + + + + + + +{{end}} + +{{/* A member in a scope's own list: taking them out of here is the action. */}} +{{define "member-row"}}{{$u := .U}}{{$d := .D}} + + + + + {{end}} + +{{/* Adding a person to a scope, from the scope. */}} +{{define "add-member"}}{{$d := .D}} + + + + + +{{end}} + +{{/* An org's or team's own members, folded away until asked for. */}} +{{define "scope-members"}}{{$d := .D}}{{$k := printf "%s:%s" .Kind .ID}} +
{{len (index $d.Members $k)}} member{{if ne (len (index $d.Members $k)) 1}}s{{end}} +
+ {{with index $d.Members $k}} +
EmailNameSigned upApprove, and put them in
{{.Email}}{{.Name}}{{ago .CreatedAt}} - {{if $.Projects}}
- {{else}}Create a project first, below.{{end}}
{{$org.Name}} {{len $org.Teams}} team{{if ne (len $org.Teams) 1}}s{{end}} + {{template "scope-members" (dict "Kind" "org" "ID" $org.ID "Name" $org.Name "D" $)}}
rename @@ -82,6 +82,7 @@ project in the team — an account can only be approved into a project.

{{$team.Name}} {{len $team.Projects}} project{{if ne (len $team.Projects) 1}}s{{end}} + {{template "scope-members" (dict "Kind" "team" "ID" $team.ID "Name" $team.Name "D" $)}}
rename @@ -113,7 +114,7 @@ project in the team — an account can only be approved into a project.

{{$p.Name}} - {{$p.Users}} {{if eq $p.Users 1}}person{{else}}people{{end}} + {{$p.Users}} member{{if ne $p.Users 1}}s{{end}} · {{$p.ActiveKeys}} key{{if ne $p.ActiveKeys 1}}s{{end}}{{if gt $p.Keys $p.ActiveKeys}} ({{sub $p.Keys $p.ActiveKeys}} revoked){{end}}
@@ -147,11 +148,12 @@ project in the team — an account can only be approved into a project.

{{else}}Removable once it has no people and never had a key.{{end}}
- {{with index $.People $p.ID}} - - {{range .}}{{template "user-row-actions" (dict "U" . "D" $ "Open" $p.ID)}}{{end}} + {{with index $.Members (printf "project:%s" $p.ID)}} +
PersonStateLast sign-inMove to
+ {{range .}}{{template "member-row" (dict "U" . "D" $ "Scope" (printf "project:%s" $p.ID) "Open" $p.ID)}}{{end}}
MemberStateLast sign-in
- {{else}}

Nobody is in this project.

{{end}} + {{else}}

No members of its own. Members of its team or organization can use it too.

{{end}} + {{template "add-member" (dict "Kind" "project" "ID" $p.ID "D" $ "Open" $p.ID)}} {{with index $.Keys $p.ID}} @@ -200,9 +202,11 @@ project in the team — an account can only be approved into a project.

All accounts

{{if .Accounts}} -
KeyOwnerStateLast used
-{{range .Accounts}}{{template "user-row-actions" (dict "U" . "D" $ "Open" "" "Path" true)}}{{end}} +
PersonStateProjectLast sign-inMove to
+{{range .Accounts}}{{template "account-row" (dict "U" . "D" $)}}{{end}}
PersonStateMember ofLast sign-inAdd to
+

Someone who is a member of nothing uses the gateway on their own: +personal keys and the chat page, within their personal limits.

{{else}}

No accounts yet.

{{end}}

Not here on purpose: making someone an admin, resetting a password @@ -247,23 +251,8 @@ access than it had, so they need a shell on the control plane — {{end}} -{{define "user-row-actions"}}{{$u := .U}}{{$d := .D}} -

{{$u.Email}}{{if $u.IsAdmin}} admin{{end}}{{$u.State}}{{or $u.ProjectPath "—"}}{{ago $u.LastLoginAt}} - - - - - - - +{{/* The actions every account row carries: suspend or reinstate, remove. */}} +{{define "state-actions"}}{{$u := .U}}{{$d := .D}} {{if ne $u.ID $d.AdminID}} {{if eq (printf "%s" $u.State) "active"}}
@@ -285,5 +274,77 @@ access than it had, so they need a shell on the control plane —
{{else}}you{{end}} +{{end}} + +{{/* A person in the list of every account: where they are, and where else + they could be. */}} +{{define "account-row"}}{{$u := .U}}{{$d := .D}} +
{{$u.Email}}{{if $u.IsAdmin}} admin{{end}}{{$u.State}} + {{range $u.Memberships}}{{.Path}} +
+ + + +
+ {{else}}on their own{{end}} +
{{ago $u.LastLoginAt}} +
+ + + +
+
{{template "state-actions" (dict "U" $u "D" $d "Open" "")}}
{{$u.Email}}{{if $u.IsAdmin}} admin{{end}} + {{if gt (len $u.Memberships) 1}}also in {{sub (len $u.Memberships) 1}} more{{end}}{{$u.State}}{{ago $u.LastLoginAt}} +
+ + + + +
+ {{range .}}{{template "member-row" (dict "U" . "D" $d "Scope" $k "Open" "")}}{{end}} +
+ {{else}}

Nobody is a member of {{.Name}} itself; a member here could use every project in it.

{{end}} + {{template "add-member" (dict "Kind" .Kind "ID" .ID "D" $d "Open" "")}} + +
{{end}} diff --git a/internal/web/templates/chat.html b/internal/web/templates/chat.html index ce329e5..0fb7881 100644 --- a/internal/web/templates/chat.html +++ b/internal/web/templates/chat.html @@ -9,10 +9,23 @@

Chat

-

{{.User.Email}} · {{or .Nav.Project "no project"}}

+

{{.User.Email}}

{{template "nav" .Nav}}
+{{if gt (len .Contexts) 1}} +{{/* What this conversation spends against. A plain GET, so the model list + below is the one this choice allows; the server checks it again on every + message rather than trusting the page. */}} +
+ + +
+{{else if .Contexts}}{{with index .Contexts 0}}{{end}}{{end}} + {{if .Models}}