Forgetting a person and keeping requests in the cluster: users forget, keys residency --in-cluster
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
d67bd9b2cc
commit
30f7f3474a
17 files changed
+490
-11
No files matched your search
+7
-1
@@ -500,7 +500,13 @@ portal is not done — the page shows; the console and ctl change.
|
||||
**Secrets management (S–M).** Vault/KMS for `api_key_file`, the DSN, the
|
||||
CA key, `cluster_secrets` (`store/secrets.go`, `store/seal.go`);
|
||||
envelope-encrypt them in backups (with A8 backups).
|
||||
16. **GDPR and residency (S).** "Forget user" anonymises `user_id` in usage
|
||||
16. **Done (branch `features-c2`):** `users forget` (pseudonymised usage
|
||||
and sums, kept requests, files, batches, limits and audit names), `keys
|
||||
residency --in-cluster` (asked-for, fallback and canary). Left: an
|
||||
export of everything held about a person (article 15); residency by
|
||||
region between several clusters; backups keep what was forgotten until
|
||||
they age out.
|
||||
**GDPR and residency (S).** "Forget user" anonymises `user_id` in usage
|
||||
and rollups and erases threads/memory (`store/hierarchy.go:241` leaves
|
||||
it today); per-project "never route to an External provider".
|
||||
17. **Response/semantic cache (M).** Exact-match first, per project,
|
||||
|
||||
@@ -71,6 +71,17 @@ APIs change between minors without a deprecation period.
|
||||
- **Other ways in:** `GET /api/v1/usage/export` (CSV, or `format=json`)
|
||||
and **E** on the console's Usage view.
|
||||
|
||||
### Forgetting a person, and requests that stay in the cluster
|
||||
|
||||
- **`llmgw users forget`** erases a person for a request to be forgotten:
|
||||
their usage kept under a random pseudonym so totals stay true, their kept
|
||||
requests, personal files, batches and limits deleted, their name taken out
|
||||
of the audit log, the account removed.
|
||||
- **`llmgw keys residency --project P --in-cluster`**: no model served by an
|
||||
outside provider for the project's keys, asked for, as a fallback or as a
|
||||
canary.
|
||||
- Both are on the console's menus.
|
||||
|
||||
### Secrets in Vault
|
||||
|
||||
- **Secrets from Vault or the environment.** Every setting naming a secret's
|
||||
|
||||
+4
-2
@@ -162,7 +162,8 @@ and change it with the same commands as `llmgw users`, `llmgw keys` and
|
||||
dashboard, and what they belong to. **A** creates an account, already
|
||||
approved. On a row: approve one waiting, suspend or reinstate, give or take
|
||||
the cluster dashboard, add to or take out of an organization, team or
|
||||
project, their limits, a new password (their sessions close), delete.
|
||||
project, their limits, a new password (their sessions close), delete, and
|
||||
**Forget them…** for a request to be forgotten (`llmgw users forget`).
|
||||
|
||||
**API keys** — every key, its project, owner, state and last use. **A**
|
||||
issues one; the key is shown once, in the result, and never again. On a row:
|
||||
@@ -193,7 +194,8 @@ projects, with members, keys and limits. **A** adds an organization. On a
|
||||
row: add a team or project inside it, rename, move, limits for the level and
|
||||
for each of its members, its capacity quota, and for a project how long
|
||||
conversations and agent notes are kept, and whether its requests are
|
||||
(**Keep requests…**: for how long, and what share). **Longest key lifetime…** caps how
|
||||
(**Keep requests…**: for how long, and what share), and whether they stay in
|
||||
the cluster (**Requests stay in the cluster…**: never an outside provider). **Longest key lifetime…** caps how
|
||||
long a key made there may live, and is the expiry of one made without any
|
||||
(`llmgw keys ttl`; `auth.max_key_ttl` in server.yaml caps every key).
|
||||
**Remove** takes away only something empty.
|
||||
|
||||
@@ -161,3 +161,29 @@ touch, whatever the catalog says.
|
||||
| Scope is part of the lookup | `internal/store/threads.go`, `Thread` |
|
||||
| Deletion is a client endpoint | `internal/router/threads.go`, `handleDeleteThread` |
|
||||
| Weight fetching is the only serving-path egress | `internal/agent/weights/fetch.go` |
|
||||
|
||||
## Forgetting a person
|
||||
|
||||
`llmgw users forget <email> --yes` (or **Forget them…** in the console) is for
|
||||
a request to be forgotten:
|
||||
|
||||
- their usage — every request record and every sum — is kept, so totals and
|
||||
budgets stay true, but under a random pseudonym that links to nothing;
|
||||
- their kept requests (`keys requests`), personal files and batches, and
|
||||
their own limits are deleted;
|
||||
- their email and id are taken out of the audit log, which keeps what was
|
||||
done;
|
||||
- then the account goes, as `users delete` removes it: sessions ended, keys
|
||||
revoked and left without an owner.
|
||||
|
||||
Conversations and agent notes belong to projects, not to the people in them,
|
||||
and are not touched: a project forgets them by its retention, and a thread by
|
||||
`DELETE /v1/threads/{id}`. Backups taken before still hold everything until
|
||||
they age out; that is what their retention is for.
|
||||
|
||||
## Keeping a project's requests in the cluster
|
||||
|
||||
`llmgw keys residency --project <path> --in-cluster` refuses the project's
|
||||
keys any model served by an outside provider, and never uses one for them as
|
||||
a fallback or a canary: their prompts do not leave the machines the cluster
|
||||
runs on. `--anywhere` lifts it.
|
||||
@@ -43,6 +43,7 @@ func newTenancyCmd() *cobra.Command {
|
||||
newLimitsCmd(open),
|
||||
newThreadsCmd(open),
|
||||
newRequestLogCmd(open),
|
||||
newResidencyCmd(open),
|
||||
newMemoryCmd(open),
|
||||
newMoveStoreCmd(),
|
||||
)
|
||||
|
||||
@@ -72,3 +72,53 @@ func share(f float64) string {
|
||||
}
|
||||
return fmt.Sprintf("%g%%", f*100)
|
||||
}
|
||||
|
||||
func newResidencyCmd(open opener) *cobra.Command {
|
||||
var (
|
||||
project string
|
||||
inCluster, anyway bool
|
||||
)
|
||||
cmd := &cobra.Command{
|
||||
Use: "residency",
|
||||
Short: "Keep a project's requests in the cluster, never sent to an outside provider",
|
||||
Long: "With --in-cluster, a model served by an outside provider (external:) is\n" +
|
||||
"refused to the project's keys, and never used for them as a fallback or\n" +
|
||||
"a canary: its prompts do not leave the machines the cluster runs on.\n" +
|
||||
"--anywhere lifts it. Without either, says how it stands.",
|
||||
Example: " llmgw keys residency --project Acme/legal --in-cluster",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
if inCluster && anyway {
|
||||
return fmt.Errorf("--in-cluster or --anywhere, not both")
|
||||
}
|
||||
s, err := open(cmd.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer s.Close()
|
||||
p, err := resolveProject(cmd.Context(), s, project)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if inCluster || anyway {
|
||||
if err := s.SetInCluster(cmd.Context(), p.ID, inCluster); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
on, err := s.InCluster(cmd.Context(), p.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if on {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "%s's requests stay in the cluster.\n", p.Path)
|
||||
} else {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "%s's requests may go to outside providers its keys allow.\n", p.Path)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringVar(&project, "project", "", "project: id, path or unique name")
|
||||
cmd.Flags().BoolVar(&inCluster, "in-cluster", false, "keep its requests in the cluster")
|
||||
cmd.Flags().BoolVar(&anyway, "anywhere", false, "let them go to outside providers again")
|
||||
return cmd
|
||||
}
|
||||
@@ -264,6 +264,14 @@ func (m consoleModel) userMenu(u store.User) *menuDialog {
|
||||
yes: func(m *consoleModel) tea.Cmd { return m.runCLI("Delete "+who, "users", "delete", u.ID, "--yes") }}
|
||||
return nil
|
||||
}},
|
||||
menuItem{"Forget them…", func(m *consoleModel) tea.Cmd {
|
||||
m.confirmD = &confirmDialog{title: "Forget " + who,
|
||||
text: "For a request to be forgotten. Their usage stays, under a random name; their kept\n" +
|
||||
"requests, personal files and batches go; the audit log stops naming them; then\n" +
|
||||
"the account is removed. It cannot be undone.",
|
||||
yes: func(m *consoleModel) tea.Cmd { return m.runCLI("Forget "+who, "users", "forget", u.ID, "--yes") }}
|
||||
return nil
|
||||
}},
|
||||
)
|
||||
return &menuDialog{title: who, items: items}
|
||||
}
|
||||
@@ -508,6 +516,17 @@ func (m consoleModel) orgMenu(r orgRow) *menuDialog {
|
||||
items = append(items,
|
||||
retention("Keep conversations", "threads", "conversations", m.people.threads[r.id]),
|
||||
retention("Keep agent notes", "memory", "notes", m.people.notes[r.id]),
|
||||
menuItem{"Requests stay in the cluster…", func(m *consoleModel) tea.Cmd {
|
||||
m.menuD = &menuDialog{title: "Residency · " + r.path, items: []menuItem{
|
||||
{"In the cluster: never an outside provider", func(m *consoleModel) tea.Cmd {
|
||||
return m.runCLI("Residency", "keys", "residency", "--project="+r.id, "--in-cluster")
|
||||
}},
|
||||
{"Anywhere its keys allow", func(m *consoleModel) tea.Cmd {
|
||||
return m.runCLI("Residency", "keys", "residency", "--project="+r.id, "--anywhere")
|
||||
}},
|
||||
}}
|
||||
return nil
|
||||
}},
|
||||
menuItem{"Keep requests…", func(m *consoleModel) tea.Cmd {
|
||||
l := m.people.requests[r.id]
|
||||
sample := "1"
|
||||
|
||||
+44
-1
@@ -35,7 +35,7 @@ func newUsersCmd() *cobra.Command {
|
||||
newUserStateCmd(open, "reinstate", store.UserActive,
|
||||
"Let a suspended account work again"),
|
||||
newUserCreateCmd(open), newUserResetPasswordCmd(open),
|
||||
newUserJoinCmd(open), newUserLeaveCmd(open), newUserDeleteCmd(open),
|
||||
newUserJoinCmd(open), newUserLeaveCmd(open), newUserDeleteCmd(open), newUserForgetCmd(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"),
|
||||
newUserRoleCmd(open))
|
||||
@@ -489,3 +489,46 @@ func newUserDeleteCmd(open userOpener) *cobra.Command {
|
||||
cmd.Flags().BoolVar(&yes, "yes", false, "confirm")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func newUserForgetCmd(open userOpener) *cobra.Command {
|
||||
var yes bool
|
||||
cmd := &cobra.Command{
|
||||
Use: "forget <user>",
|
||||
Short: "Erase a person: their account, what was kept of their requests, and their name",
|
||||
Long: "For a request to be forgotten (GDPR article 17). Beyond delete:\n\n" +
|
||||
" - their usage — every request record and every sum — is kept, so the\n" +
|
||||
" totals stay true, under a random pseudonym that links to nothing;\n" +
|
||||
" - their requests kept by `keys requests`, and their personal files and\n" +
|
||||
" batches, are deleted;\n" +
|
||||
" - their name and email are taken out of the audit log;\n" +
|
||||
" - then the account goes, as delete does.\n\n" +
|
||||
"Conversations and agent notes belong to projects, not people, and are\n" +
|
||||
"left: a project forgets them by its retention. Backups taken before\n" +
|
||||
"still hold everything until they age out.",
|
||||
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
|
||||
}
|
||||
if !yes {
|
||||
return fmt.Errorf("this erases %s and cannot be undone; run again with --yes", u.Email)
|
||||
}
|
||||
f, err := s.ForgetUser(cmd.Context(), u.ID, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "%s forgotten. Their usage (%d requests, %d sums) is now %s; "+
|
||||
"%d kept requests, %d files and %d batches deleted; %d audit lines no longer name them.\n",
|
||||
u.Email, f.UsageRows, f.RollupRows, f.Pseudonym, f.Requests, f.Files, f.Batches, f.AuditEvents)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
cmd.Flags().BoolVar(&yes, "yes", false, "confirm")
|
||||
return cmd
|
||||
}
|
||||
@@ -361,6 +361,8 @@ type parsedRequest struct {
|
||||
guardIn guard.Verdict
|
||||
// noStore is a policy that keeps nothing of this request.
|
||||
noStore bool
|
||||
// inCluster is a project whose requests may not leave the cluster.
|
||||
inCluster bool
|
||||
// internal is a request the gateway makes of itself — a judge's — which
|
||||
// no guard filters.
|
||||
internal bool
|
||||
@@ -486,6 +488,18 @@ func (r *Router) handleInference(path string) http.HandlerFunc {
|
||||
w.Header().Set("x-llmgw-queue-ms", strconv.FormatInt(queued.Milliseconds(), 10))
|
||||
}
|
||||
|
||||
// A project whose requests stay in the cluster is not sent outside it,
|
||||
// as the model asked for or, below, as a fallback.
|
||||
if r.inCluster(req.Context(), parsed.ProjectID) {
|
||||
parsed.inCluster = true
|
||||
if _, entry := r.resolve(parsed.Model); entry.IsExternal() {
|
||||
writeErrorCode(w, http.StatusForbidden, "invalid_request_error", "in_cluster",
|
||||
"this project's requests stay in the cluster, and "+parsed.Model+" is served outside it")
|
||||
r.m.observeRequest(r.metricModel(parsed.Model), "", http.StatusForbidden, time.Since(start), nil, 0)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// A policy that keeps nothing is known before anything is kept: a
|
||||
// conversation asked for is refused rather than quietly not stored,
|
||||
// and a model outside the cluster is not sent the prompt.
|
||||
|
||||
@@ -26,8 +26,9 @@ type requestLogs struct {
|
||||
}
|
||||
|
||||
type cachedLog struct {
|
||||
l storepkg.RequestLog
|
||||
at time.Time
|
||||
l storepkg.RequestLog
|
||||
inCluster bool
|
||||
at time.Time
|
||||
}
|
||||
|
||||
// requestLogOf is a project's setting, read at most every requestLogTTL.
|
||||
@@ -41,18 +42,52 @@ func (r *Router) requestLogOf(ctx context.Context, project string) storepkg.Requ
|
||||
if ok && time.Since(c.at) < requestLogTTL {
|
||||
return c.l
|
||||
}
|
||||
l, err := r.store.RequestLogOf(ctx, project)
|
||||
c, err := r.readProjectPolicy(ctx, project)
|
||||
if err != nil {
|
||||
r.log.Warn("could not read whether a project keeps its requests", "project", project, "err", err)
|
||||
return c.l
|
||||
}
|
||||
return c.l
|
||||
}
|
||||
|
||||
// readProjectPolicy reads a project's keeping and residency, and caches them.
|
||||
func (r *Router) readProjectPolicy(ctx context.Context, project string) (cachedLog, error) {
|
||||
l, err := r.store.RequestLogOf(ctx, project)
|
||||
if err != nil {
|
||||
return cachedLog{}, err
|
||||
}
|
||||
in, err := r.store.InCluster(ctx, project)
|
||||
if err != nil {
|
||||
return cachedLog{}, err
|
||||
}
|
||||
c := cachedLog{l: l, inCluster: in, at: time.Now()}
|
||||
r.reqLogs.mu.Lock()
|
||||
if r.reqLogs.by == nil {
|
||||
r.reqLogs.by = map[string]cachedLog{}
|
||||
}
|
||||
r.reqLogs.by[project] = cachedLog{l: l, at: time.Now()}
|
||||
r.reqLogs.by[project] = c
|
||||
r.reqLogs.mu.Unlock()
|
||||
return l
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// inCluster is whether a project's requests stay in the cluster, read at
|
||||
// most every requestLogTTL. Unreadable, they do: refusing an outside model
|
||||
// is the safe way to be wrong.
|
||||
func (r *Router) inCluster(ctx context.Context, project string) bool {
|
||||
if r.store == nil || project == "" {
|
||||
return false
|
||||
}
|
||||
r.reqLogs.mu.Lock()
|
||||
c, ok := r.reqLogs.by[project]
|
||||
r.reqLogs.mu.Unlock()
|
||||
if ok && time.Since(c.at) < requestLogTTL {
|
||||
return c.inCluster
|
||||
}
|
||||
c, err := r.readProjectPolicy(ctx, project)
|
||||
if err != nil {
|
||||
r.log.Warn("could not read whether a project's requests stay in the cluster", "project", project, "err", err)
|
||||
return true
|
||||
}
|
||||
return c.inCluster
|
||||
}
|
||||
|
||||
// sampled says whether a request falls in a share of them, by its id, so
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/router/endpoint"
|
||||
)
|
||||
|
||||
func TestAProjectInTheClusterIsNeverSentOutside(t *testing.T) {
|
||||
e := newFakeEngine(t, "qwen2.5-14b-instruct")
|
||||
rt, secret := pricedRouter(t, e.srv.URL)
|
||||
var outside atomic.Int64
|
||||
provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
outside.Add(1)
|
||||
_, _ = w.Write([]byte(`{"choices":[{"message":{"content":"from outside"}}]}`))
|
||||
}))
|
||||
t.Cleanup(provider.Close)
|
||||
local := catModel("qwen2.5-14b-instruct", []string{"q14"}, "cloud")
|
||||
cloud := catalog.Model{Name: "cloud", Source: "external", External: &catalog.External{URL: provider.URL, Model: "m"}}
|
||||
cat, err := catalog.New([]catalog.Model{local, cloud})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rt.SetCatalog(cat)
|
||||
rt.SetEndpoints(t.Context(), []endpoint.Info{
|
||||
{ID: "a", URL: e.srv.URL, Models: []string{"qwen2.5-14b-instruct"}},
|
||||
{ID: "external:cloud", URL: provider.URL, Models: []string{"cloud"}, Engine: endpoint.EngineExternal,
|
||||
Pool: "external", Weight: 1, UpstreamModel: "m"},
|
||||
})
|
||||
auth := map[string]string{"Authorization": "Bearer " + secret}
|
||||
|
||||
if err := rt.store.SetInCluster(t.Context(), mustProjectOf(t, rt, "key-1"), true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rec := post(t, rt.Handler(), map[string]any{"model": "cloud", "messages": []any{}}, auth); rec.Code != http.StatusForbidden {
|
||||
t.Errorf("an outside model, asked for: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
// The local model down: its outside fallback is not used.
|
||||
e.status.Store(http.StatusServiceUnavailable)
|
||||
if rec := post(t, rt.Handler(), map[string]any{"model": "q14", "messages": []any{}}, auth); rec.Code == http.StatusOK {
|
||||
t.Errorf("served: %s", rec.Body.String())
|
||||
}
|
||||
if outside.Load() != 0 {
|
||||
t.Errorf("the outside provider was sent %d requests", outside.Load())
|
||||
}
|
||||
}
|
||||
@@ -793,6 +793,9 @@ func (r *Router) dispatch(ctx context.Context, w http.ResponseWriter, req *parse
|
||||
if onCanary && len(r.reg.Snapshots(target)) == 0 {
|
||||
onCanary = false
|
||||
}
|
||||
if _, te := r.resolve(target); onCanary && (req.noStore || req.inCluster) && te.IsExternal() {
|
||||
onCanary = false
|
||||
}
|
||||
setVariantHeader(w.Header(), onCanary)
|
||||
if onCanary {
|
||||
// The engine is asked for the model by the name it serves.
|
||||
@@ -826,7 +829,7 @@ func (r *Router) dispatchStable(ctx context.Context, w http.ResponseWriter, req
|
||||
continue
|
||||
}
|
||||
// Nor sent outside the cluster under a policy that keeps nothing.
|
||||
if _, fe := r.resolve(fb); req.noStore && fe.IsExternal() {
|
||||
if _, fe := r.resolve(fb); (req.noStore || req.inCluster) && fe.IsExternal() {
|
||||
continue
|
||||
}
|
||||
chain = append(chain, fb)
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Forgotten is what forgetting a person did, for saying so.
|
||||
type Forgotten struct {
|
||||
// Pseudonym is what their usage is counted under now: random, so it
|
||||
// links to nothing.
|
||||
Pseudonym string
|
||||
// UsageRows and RollupRows are the request records and sums moved to it.
|
||||
UsageRows int64
|
||||
RollupRows int64
|
||||
// Requests, Files and Batches are kept requests, and personal files and
|
||||
// batches, deleted; AuditEvents the audit lines their name was taken out of.
|
||||
Requests int64
|
||||
Files int64
|
||||
Batches int64
|
||||
AuditEvents int64
|
||||
}
|
||||
|
||||
// ForgetUser erases a person: what was kept of their requests, their
|
||||
// personal files and batches and their limits are deleted; their usage — the
|
||||
// request records and every sum of them — is kept, for the totals stay true,
|
||||
// under a random pseudonym; their name is taken out of the audit log; and the
|
||||
// account goes as DeleteUser removes it. What remains cannot be tied back to
|
||||
// them.
|
||||
//
|
||||
// Conversations and agent notes belong to projects, not people, and are not
|
||||
// touched: a project forgets them by its retention, or by
|
||||
// DELETE /v1/threads/{id}.
|
||||
func (s *DB) ForgetUser(ctx context.Context, userID string, at time.Time) (Forgotten, error) {
|
||||
u, err := s.UserByID(ctx, userID)
|
||||
if err != nil {
|
||||
return Forgotten{}, err
|
||||
}
|
||||
b := make([]byte, 8)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return Forgotten{}, err
|
||||
}
|
||||
f := Forgotten{Pseudonym: "forgotten-" + hex.EncodeToString(b)}
|
||||
err = s.inTx(ctx, func(c conn) error {
|
||||
n := func(q string, args ...any) (int64, error) {
|
||||
res, err := c.ExecContext(ctx, q, args...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
var err error
|
||||
if f.Requests, err = n(`DELETE FROM request_bodies WHERE request_id IN
|
||||
(SELECT request_id FROM usage WHERE user_id = ? AND request_id <> '')`, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
session, pseudoSession := "session:"+userID, "session:"+f.Pseudonym
|
||||
if f.UsageRows, err = n(`UPDATE usage SET user_id = ? WHERE user_id = ?`, f.Pseudonym, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = n(`UPDATE usage SET key_id = ? WHERE key_id = ?`, pseudoSession, session); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, t := range []string{"usage_5m", "usage_1h", "usage_1d"} {
|
||||
m, err := n(`UPDATE `+t+` SET user_id = ? WHERE user_id = ?`, f.Pseudonym, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.RollupRows += m
|
||||
if _, err := n(`UPDATE `+t+` SET key_id = ? WHERE key_id = ?`, pseudoSession, session); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
owner := "user:" + userID
|
||||
if _, err = n(`DELETE FROM batch_results WHERE batch_id IN (SELECT id FROM batches WHERE owner = ?)`, owner); err != nil {
|
||||
return err
|
||||
}
|
||||
if f.Batches, err = n(`DELETE FROM batches WHERE owner = ?`, owner); err != nil {
|
||||
return err
|
||||
}
|
||||
if f.Files, err = n(`DELETE FROM files WHERE owner = ?`, owner); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = n(`DELETE FROM limits WHERE scope_type = ? AND scope_id = ?`, string(ScopeUser), userID); err != nil {
|
||||
return err
|
||||
}
|
||||
// Who did what stays in the audit log; that it was them does not.
|
||||
for _, name := range []string{u.Email, userID} {
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
m, err := n(`UPDATE audit_events SET actor = REPLACE(actor, ?, ?), target = REPLACE(target, ?, ?),
|
||||
detail = REPLACE(detail, ?, ?) WHERE actor LIKE ? OR target LIKE ? OR detail LIKE ?`,
|
||||
name, f.Pseudonym, name, f.Pseudonym, name, f.Pseudonym, "%"+name+"%", "%"+name+"%", "%"+name+"%")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.AuditEvents += m
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return Forgotten{}, err
|
||||
}
|
||||
return f, s.DeleteUser(ctx, userID, at)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestForgettingAPersonLeavesNothingThatNamesThem(t *testing.T) {
|
||||
checkForget(t, capacityStore(t))
|
||||
}
|
||||
|
||||
func TestPostgresForgettingAPersonLeavesNothingThatNamesThem(t *testing.T) {
|
||||
checkForget(t, openPG(t))
|
||||
}
|
||||
|
||||
func checkForget(t *testing.T, db *DB) {
|
||||
ctx := t.Context()
|
||||
now := time.Now()
|
||||
org, _ := db.CreateOrg(ctx, "acme")
|
||||
team, _ := db.CreateTeam(ctx, org, "ml")
|
||||
project, _ := db.CreateProject(ctx, team, "train")
|
||||
u := User{ID: "usr-ayse", Email: "ayse@example.com", State: UserActive, CreatedAt: now}
|
||||
if err := db.CreateUser(ctx, u); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := User{ID: "usr-bob", Email: "bob@example.com", State: UserActive, CreatedAt: now}
|
||||
_ = db.CreateUser(ctx, other)
|
||||
rows := []Usage{
|
||||
{At: now, KeyID: "k1", UserID: u.ID, ProjectID: project, Model: "m", ServedModel: "m", PromptTokens: 10, CostMicros: 5, Status: 200, RequestID: "r-ayse"},
|
||||
{At: now, KeyID: "session:" + u.ID, UserID: u.ID, ProjectID: project, Model: "m", ServedModel: "m", PromptTokens: 1, Status: 200},
|
||||
{At: now, KeyID: "k2", UserID: other.ID, ProjectID: project, Model: "m", ServedModel: "m", PromptTokens: 7, Status: 200, RequestID: "r-bob"},
|
||||
}
|
||||
if err := db.WriteUsage(ctx, rows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, id := range []string{"r-ayse", "r-bob"} {
|
||||
_ = db.PutRequestBody(ctx, RequestBody{RequestID: id, ProjectID: project, At: now, ExpiresAt: now.Add(time.Hour),
|
||||
Path: "/v1/chat/completions", Model: "m", ServedModel: "m", Status: 200, Request: "{}", Response: "x"})
|
||||
}
|
||||
_ = db.PutFile(ctx, File{ID: "file-a", Owner: "user:" + u.ID, Purpose: "batch", Filename: "f", CreatedAt: now}, []byte("x"))
|
||||
_ = db.WriteAudit(ctx, AuditEvent{Actor: "admin", Source: "api", Action: "POST /api/v1/users", Target: u.Email,
|
||||
Detail: `{"email":"ayse@example.com"}`, Status: 200})
|
||||
|
||||
f, err := db.ForgetUser(ctx, u.ID, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.UsageRows != 2 || f.Requests != 1 || f.Files != 1 || f.AuditEvents != 1 || !strings.HasPrefix(f.Pseudonym, "forgotten-") {
|
||||
t.Errorf("%+v", f)
|
||||
}
|
||||
if _, err := db.UserByID(ctx, u.ID); err == nil {
|
||||
t.Error("the account is still there")
|
||||
}
|
||||
var n int
|
||||
for _, q := range []string{
|
||||
`SELECT COUNT(*) FROM usage WHERE user_id = 'usr-ayse' OR key_id = 'session:usr-ayse'`,
|
||||
`SELECT COUNT(*) FROM usage_1h WHERE user_id = 'usr-ayse' OR key_id = 'session:usr-ayse'`,
|
||||
`SELECT COUNT(*) FROM usage_1d WHERE user_id = 'usr-ayse'`,
|
||||
`SELECT COUNT(*) FROM audit_events WHERE target LIKE '%ayse%' OR detail LIKE '%ayse%'`,
|
||||
`SELECT COUNT(*) FROM request_bodies WHERE request_id = 'r-ayse'`,
|
||||
} {
|
||||
_ = db.db.QueryRowContext(ctx, q).Scan(&n)
|
||||
if n != 0 {
|
||||
t.Errorf("%s: %d", q, n)
|
||||
}
|
||||
}
|
||||
// The totals stay true, and someone else's are untouched.
|
||||
var total int64
|
||||
_ = db.db.QueryRowContext(ctx, `SELECT SUM(prompt_tokens) FROM usage WHERE user_id = ?`, f.Pseudonym).Scan(&total)
|
||||
if total != 11 {
|
||||
t.Errorf("their usage under the pseudonym: %d", total)
|
||||
}
|
||||
if _, err := db.RequestBodyByID(ctx, "r-bob"); err != nil {
|
||||
t.Error("another person's kept request went too")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Ported from the SQLite migration of the same number.
|
||||
-- A project whose requests never leave the cluster: no model served by an
|
||||
-- outside provider, as the one asked for or as a fallback.
|
||||
ALTER TABLE projects ADD COLUMN in_cluster BIGINT NOT NULL DEFAULT 0;
|
||||
@@ -0,0 +1,3 @@
|
||||
-- A project whose requests never leave the cluster: no model served by an
|
||||
-- outside provider, as the one asked for or as a fallback.
|
||||
ALTER TABLE projects ADD COLUMN in_cluster INTEGER NOT NULL DEFAULT 0;
|
||||
@@ -118,3 +118,30 @@ func (s *DB) PruneRequestBodies(ctx context.Context, now time.Time) (int64, erro
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
// SetInCluster says whether a project's requests stay in the cluster: no
|
||||
// model served by an outside provider, asked for or as a fallback.
|
||||
func (s *DB) SetInCluster(ctx context.Context, projectID string, on bool) error {
|
||||
v := 0
|
||||
if on {
|
||||
v = 1
|
||||
}
|
||||
res, err := s.db.ExecContext(ctx, `UPDATE projects SET in_cluster = ? WHERE id = ?`, v, projectID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return fmt.Errorf("store: no project %q", projectID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InCluster is whether a project's requests stay in the cluster.
|
||||
func (s *DB) InCluster(ctx context.Context, projectID string) (bool, error) {
|
||||
var v int
|
||||
err := s.db.QueryRowContext(ctx, `SELECT in_cluster FROM projects WHERE id = ?`, projectID).Scan(&v)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
return v == 1, err
|
||||
}
|
||||
Reference in new issue
Block a user