diff --git a/CHANGELOG.md b/CHANGELOG.md index f83521a..e0ca311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -97,6 +97,11 @@ APIs change between minors without a deprecation period. the request was priced as the model asked for. Fallbacks a key may not use are now skipped, and a request is charged what the model that answered costs. +- **Suspending an account stops its keys.** Suspension closed the person's + sessions and left every API key they had made working. A key whose owner's + account is not active — suspended, pending, or gone — is now refused as a + revoked key is, within `auth.cache_ttl`, and works again when the account + is reinstated. ### Everything that can be changed on a running cluster, from the console diff --git a/internal/auth/auth.go b/internal/auth/auth.go index a158b2e..6d25be0 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -185,6 +185,11 @@ func (a *Authenticator) resolve(ctx context.Context, hash []byte) (Principal, er } eff, err := a.store.Resolve(ctx, k.ID) + if errors.Is(err, store.ErrOwnerInactive) { + // Said as a revoked key is: whether the key exists is not the + // caller's to learn. + return Principal{}, ErrRevoked + } if err != nil { return Principal{}, err } diff --git a/internal/store/memberships.go b/internal/store/memberships.go index 4548b20..ed9427d 100644 --- a/internal/store/memberships.go +++ b/internal/store/memberships.go @@ -190,9 +190,25 @@ func (s *DB) Resolve(ctx context.Context, keyID string) (Effective, error) { } e.Band, e.RoutingProfile = band.String, profile.String e.ProjectID, e.UserID = project.String, user.String + // A person's key works while the person's account does: suspending an + // account closed its sessions and left every key it had made working. + if e.UserID != "" { + var state string + err := s.db.QueryRowContext(ctx, `SELECT state FROM users WHERE id = ?`, e.UserID).Scan(&state) + if errors.Is(err, sql.ErrNoRows) || (err == nil && UserState(state) != UserActive) { + return Effective{}, ErrOwnerInactive + } + if err != nil { + return Effective{}, err + } + } return e, s.resolveChain(ctx, &e, true) } +// ErrOwnerInactive is a key whose owner's account is suspended, pending, or +// gone. +var ErrOwnerInactive = errors.New("the key's owner is not an active account") + // 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) { diff --git a/internal/store/memberships_test.go b/internal/store/memberships_test.go index 24406be..5d6fc8e 100644 --- a/internal/store/memberships_test.go +++ b/internal/store/memberships_test.go @@ -283,3 +283,27 @@ func TestTheChatPagesUseIsReportedWhereItWasSpent(t *testing.T) { t.Errorf("gateway's spend on the quota page: %d, %v", got, err) } } + +func TestASuspendedPersonsKeysStopWorkingAndComeBackWithThem(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + _, k, _ := NewKey("key-s", "personal", "") + if err := f.s.CreateUserKey(ctx, f.user, k); err != nil { + t.Fatal(err) + } + if _, err := f.s.Resolve(ctx, k.ID); err != nil { + t.Fatalf("an active person's key: %v", err) + } + if err := f.s.SetUserState(ctx, f.user, UserSuspended); err != nil { + t.Fatal(err) + } + if _, err := f.s.Resolve(ctx, k.ID); !errors.Is(err, ErrOwnerInactive) { + t.Errorf("a suspended person's key resolved: %v", err) + } + if err := f.s.SetUserState(ctx, f.user, UserActive); err != nil { + t.Fatal(err) + } + if _, err := f.s.Resolve(ctx, k.ID); err != nil { + t.Errorf("reinstated, the key still refused: %v", err) + } +}