diff --git a/internal/plugin/host.js b/internal/plugin/host.js index 0f5702594..600d35800 100644 --- a/internal/plugin/host.js +++ b/internal/plugin/host.js @@ -26,7 +26,7 @@ // plugin's getAuth, client.auth.set and loader see that account alone. import { AsyncLocalStorage } from "node:async_hooks" -import { randomUUID } from "node:crypto" +import { createHash, randomUUID } from "node:crypto" import dns from "node:dns" import fs from "node:fs" import http2 from "node:http2" @@ -2168,14 +2168,103 @@ async function fresh(provider, key) { clearTimeout(t) } +// Each magpie process runs a host of its own (the gateway, a CLI's quota +// or usage, a host restarting), and each renews the same sign-in when it +// comes due: a vendor that spends a refresh token once turned the second +// away, and the account was marked signed out under the token the first +// had saved (plugins #331). So a renewal is made under a lock file of the +// account's beside plugin-auth.json, held across the plugin's refresh, +// and the sign-in is read again under it. The lock names its holder's pid: +// one whose holder is gone is taken over at once, and one held longer +// than RENEW_LOCK_STALE by anyone. +const RENEW_LOCK_STALE = 60 * 1000 + +function alive(pid) { + try { + process.kill(pid, 0) + return true + } catch (e) { + return e?.code === "EPERM" // someone else's process + } +} + +async function lockRenew(key) { + const lock = `${authPath}.renew-${createHash("sha256").update(key).digest("hex").slice(0, 16)}.lock` + const me = JSON.stringify({ pid: process.pid, at: Date.now(), n: randomUUID() }) + fs.mkdirSync(path.dirname(authPath), { recursive: true }) + for (let start = Date.now(), wait = 10; ; wait = Math.min(wait * 2, 500)) { + try { + fs.writeFileSync(lock, me, { flag: "wx", mode: 0o600 }) + return () => { + try { + if (fs.readFileSync(lock, "utf8") === me) fs.unlinkSync(lock) + } catch {} + } + } catch (e) { + if (!["EEXIST", "EPERM", "EACCES"].includes(e?.code)) throw e + } + let raw = null + let h = null + let age = 0 + try { + raw = fs.readFileSync(lock, "utf8") + age = Date.now() - fs.statSync(lock).mtimeMs + h = JSON.parse(raw) + } catch {} + // a holder gone, or one too long at it; a file not written yet is + // judged by its age + const stale = Number.isInteger(h?.pid) ? !alive(h.pid) || Date.now() - h.at > RENEW_LOCK_STALE : age > RENEW_LOCK_STALE + if (raw !== null && (stale || Date.now() - start > 2 * RENEW_LOCK_STALE)) { + try { + if (fs.readFileSync(lock, "utf8") === raw) fs.unlinkSync(lock) + } catch {} + continue + } + await new Promise((ok) => setTimeout(ok, wait)) + } +} + +// changed says the sign-in now isn't was any longer: signed out, signed +// in again or renewed, by this host or another. +function changed(now, was) { + return !now || secretOf(now) !== secretOf(was) || now.access !== was.access +} + async function renew(provider, key, a) { const was = readAuth()[key] const at = renewAt(a, was) if (!at || Date.now() < at) return // renewed meanwhile + const unlock = await lockRenew(key) + try { + await renewLocked(provider, key, a, was) + } finally { + unlock() + } +} + +async function renewLocked(provider, key, a, was) { + // read past authSeen: another host renewed it while this one waited, + // and its token stands + const now = readAuthState().all[key] + if (changed(now, was)) { + unrenewed.delete(key) + forgetAccount(key) + return + } + const at = renewAt(a, now) + if (!at || Date.now() < at) return + was = now let got try { got = await inScope(provider, key, () => a.refresh(JSON.parse(JSON.stringify(was)), provider)) } catch (e) { + // the token was spent by a host that didn't wait for this one's lock + // (a magpie from before it) and saved what it got: not signed out + if (changed(readAuthState().all[key], was)) { + send({ event: "log", level: "error", message: `${auths().get(provider)?.spec ?? provider}: auth.refresh: ${e?.message ?? e} (renewed elsewhere meanwhile)` }) + forgetAccount(key) + return + } unrenewed.set(key, { at: Date.now(), secret: secretOf(was), gone: e?.signIn === "expired" }) if (e?.signIn === "expired") send({ event: "signIn", provider, account: key, said: "expired" }) send({ event: "log", level: "error", message: `${auths().get(provider)?.spec ?? provider}: auth.refresh: ${e?.message ?? e}` }) @@ -2187,8 +2276,7 @@ async function renew(provider, key, a) { // nothing comes between the two. const { type: _t, ...fields } = got const kept = changeAuth((all) => { - const now = all[key] - if (!now || secretOf(now) !== secretOf(was) || now.access !== was.access) return false + if (changed(all[key], was)) return false all[key] = { ...was, ...fields, type: "oauth" } }) if (kept === false) return diff --git a/internal/plugin/plugin_renew_race_test.go b/internal/plugin/plugin_renew_race_test.go new file mode 100644 index 000000000..a8f0c8187 --- /dev/null +++ b/internal/plugin/plugin_renew_race_test.go @@ -0,0 +1,338 @@ +package plugin + +import ( + "bufio" + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "strings" + "sync" + "testing" + "time" +) + +// raceVendor is a vendor that spends a refresh token once (plugins +// #331): the first exchange of r1 gives r2, and r1 again, or a token it +// never gave, is invalid_grant. hold, when set, keeps the first exchange +// from answering until it is closed, and arrived says it came. +type raceVendor struct { + mu sync.Mutex + valid map[string]string // refresh token → the one it gives + exchanges []string + hold chan struct{} + arrived chan struct{} + auths []string // each request's Authorization + store string // plugin-auth.json +} + +func newRaceVendor(t *testing.T) *raceVendor { + t.Helper() + v := &raceVendor{valid: map[string]string{"r1": "r2"}, arrived: make(chan struct{}, 8), store: AuthPath()} + tokens := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + v.mu.Lock() + v.exchanges = append(v.exchanges, string(b)) + next, ok := v.valid[string(b)] + delete(v.valid, string(b)) // spent, whatever comes of the answer + first := len(v.exchanges) == 1 + v.mu.Unlock() + v.arrived <- struct{}{} + if first && v.hold != nil { + <-v.hold + } + if key, ok := strings.CutPrefix(string(b), "r-raced "); ok { + // a magpie from before the lock spent it first and saved + // what it got, then this one is turned away + var all map[string]map[string]any + raw, _ := os.ReadFile(v.store) + json.Unmarshal(raw, &all) + all[key]["access"], all[key]["refresh"] = "a-elsewhere", "r-elsewhere" + all[key]["expires"] = time.Now().Add(time.Hour).UnixMilli() + raw, _ = json.Marshal(all) + os.WriteFile(v.store, raw, 0o600) + } + if !ok { + w.WriteHeader(400) + io.WriteString(w, `{"error":"invalid_grant"}`) + return + } + fmt.Fprintf(w, `{"access":"a-%s","refresh":%q}`, next, next) + })) + t.Cleanup(tokens.Close) + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + v.mu.Lock() + v.auths = append(v.auths, r.Header.Get("Authorization")) + v.mu.Unlock() + io.WriteString(w, "ok") + })) + t.Cleanup(api.Close) + t.Setenv("RACE_TOKEN_URL", tokens.URL) + t.Setenv("FAKE_BASE", api.URL+"/v1") + return v +} + +func (v *raceVendor) seen() (exchanges, auths []string) { + v.mu.Lock() + defer v.mu.Unlock() + return append([]string(nil), v.exchanges...), append([]string(nil), v.auths...) +} + +// raceAccount adds the race plugin and an account due for renewal. +func raceAccount(t *testing.T, ctx context.Context, refresh string) string { + t.Helper() + abs, _ := filepath.Abs("testdata/renewrace/index.js") + if _, err := Add(ctx, abs); err != nil { + t.Fatal(err) + } + soon := time.Now().Add(time.Minute).UnixMilli() // inside the 5 minutes + k, err := Import(ctx, "raceco", map[string]any{"type": "oauth", "refresh": refresh, "access": "a-old", "expires": soon, "accountId": "me@race"}) + if err != nil { + t.Fatal(err) + } + return k +} + +func raceSend(ctx context.Context, account string) error { + res, err := Fetch(ctx, FetchRequest{Provider: "raceco", Account: account, Model: "race-1", NPM: "@ai-sdk/openai-compatible", + URL: os.Getenv("FAKE_BASE") + "/chat/completions", Method: "POST", Body: []byte(`{}`)}) + if err != nil { + return err + } + io.ReadAll(res.Body) + return res.Body.Close() +} + +func raceStored(key string) map[string]any { + var all map[string]map[string]any + b, _ := os.ReadFile(AuthPath()) + json.Unmarshal(b, &all) + return all[key] +} + +// changeStored sets the refresh token of the account at key in the store. +func changeStored(key, refresh string) error { + var all map[string]map[string]any + raw, err := os.ReadFile(AuthPath()) + if err != nil { + return err + } + if err := json.Unmarshal(raw, &all); err != nil { + return err + } + all[key]["refresh"] = refresh + raw, _ = json.Marshal(all) + return os.WriteFile(AuthPath(), raw, 0o600) +} + +// TestRenewOtherHost is the second magpie of TestRenewOnceAcrossHosts: +// its host starts, it says "ready", and on "go" it sends a request on the +// account, says "renewing" once its host is renewing the sign-in, and +// "said …" for whatever its host says of the sign-in. +func TestRenewOtherHost(t *testing.T) { + account := os.Getenv("MAGPIE_RENEW_OTHER") + if account == "" { + t.Skip("run by TestRenewOnceAcrossHosts") + } + t.Cleanup(Settle) + var mu sync.Mutex + out := func(s string) { mu.Lock(); os.Stdout.WriteString(s + "\n"); mu.Unlock() } + OnSignIn(func(_, _, s string) { out("said " + s) }) + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + h, err := get(ctx) + if err != nil { + t.Fatal(err) + } + out("ready") + if line, _ := bufio.NewReader(os.Stdin).ReadString('\n'); line != "go\n" { + t.Fatalf("told %q", line) + } + sent := make(chan error, 1) + go func() { sent <- raceSend(ctx, account) }() + // renewing, or done already: a host that didn't wait for the other's + // renewal, which the first magpie's checks then find + for h.renewing.Load() == 0 && len(sent) == 0 { + time.Sleep(5 * time.Millisecond) + } + out("renewing") + if err := <-sent; err != nil { + t.Fatal(err) + } + Told() + out("sent") +} + +// plugins #331: each magpie process runs a host of its own, and two hosts +// renewing one sign-in at once each spent its refresh token, so a vendor +// that spends one once turned the second away and the account was marked +// signed out under the token the first had saved. Two hosts, two +// processes, one store: the first exchange is held until the second host +// is renewing too. The token is exchanged once, neither says the sign-in +// expired, and both send the new token. +func TestRenewOnceAcrossHosts(t *testing.T) { + sandbox(t) + v := newRaceVendor(t) + v.hold = make(chan struct{}) + release := sync.OnceFunc(func() { close(v.hold) }) + t.Cleanup(release) + var mu sync.Mutex + var said []string + OnSignIn(func(_, _, s string) { mu.Lock(); said = append(said, s); mu.Unlock() }) + t.Cleanup(func() { OnSignIn(nil) }) + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + key := raceAccount(t, ctx, "r1") + + other := exec.Command(os.Args[0], "-test.run=^TestRenewOtherHost$", "-test.count=1", "-test.v") + other.Env = append(os.Environ(), "MAGPIE_RENEW_OTHER="+key) + stdin, _ := other.StdinPipe() + stdout, _ := other.StdoutPipe() + other.Stderr = os.Stderr + if err := other.Start(); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { other.Process.Kill(); other.Wait() }) + lines := make(chan string, 64) + go func() { + sc := bufio.NewScanner(stdout) + for sc.Scan() { + lines <- sc.Text() + } + close(lines) + }() + var otherSaid []string + expect := func(want string) { + t.Helper() + for l := range lines { + if l == want { + return + } + if s, ok := strings.CutPrefix(l, "said "); ok { + otherSaid = append(otherSaid, s) + } + } + t.Fatalf("the other magpie ended before %q", want) + } + expect("ready") + + sent := make(chan error, 1) + go func() { sent <- raceSend(ctx, key) }() + <-v.arrived // this host's exchange, held + io.WriteString(stdin, "go\n") + expect("renewing") + release() + if err := <-sent; err != nil { + t.Fatal(err) + } + expect("sent") + if err := other.Wait(); err != nil { + t.Fatalf("the other magpie: %v", err) + } + Told() + + exchanges, auths := v.seen() + if len(exchanges) != 1 { + t.Errorf("exchanges = %v, want r1 once", exchanges) + } + mu.Lock() + all := append(append([]string(nil), said...), otherSaid...) + mu.Unlock() + for _, s := range all { + if s == "expired" { + t.Errorf("a host said the sign-in expired: this one %v, the other %v", said, otherSaid) + } + } + if len(auths) != 2 || auths[0] != "Bearer a-r2" || auths[1] != "Bearer a-r2" { + t.Errorf("requests went with %v, want the new token twice", auths) + } + if a := raceStored(key); a["refresh"] != "r2" || a["access"] != "a-r2" { + t.Errorf("kept %v", a) + } +} + +// A renewal lock left by a magpie that died is taken over at once: the +// renewal is made inside the wait a request gives it, not after the +// lock's minute. A token the vendor turns away still says the sign-in +// expired, and leaves it as it was; one turned away because another +// magpie spent it and saved the new one meanwhile doesn't. +func TestRenewLockOfDeadHostAndRefusal(t *testing.T) { + sandbox(t) + v := newRaceVendor(t) + said := make(chan string, 16) + OnSignIn(func(_, _, s string) { said <- s }) + t.Cleanup(func() { OnSignIn(nil) }) + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + key := raceAccount(t, ctx, "r1") + + dead := exec.Command(os.Args[0], "-test.run=^$") + if err := dead.Run(); err != nil { + t.Fatal(err) + } + sum := sha256.Sum256([]byte(key)) + lock := AuthPath() + ".renew-" + hex.EncodeToString(sum[:])[:16] + ".lock" + held := fmt.Sprintf(`{"pid":%d,"at":%d}`, dead.Process.Pid, time.Now().UnixMilli()) + if err := os.WriteFile(lock, []byte(held), 0o600); err != nil { + t.Fatal(err) + } + if err := raceSend(ctx, key); err != nil { + t.Fatal(err) + } + if s := waitSaid(t, said); s != "renewed" { + t.Fatalf("said %q", s) + } + if exchanges, auths := v.seen(); len(exchanges) != 1 || len(auths) != 1 || auths[0] != "Bearer a-r2" { + t.Fatalf("exchanges %v, requests %v: the dead magpie's lock was waited out", exchanges, auths) + } + if _, err := os.Stat(lock); !os.IsNotExist(err) { + t.Fatalf("the lock is left: %v", err) + } + + refused, err := Import(ctx, "raceco", map[string]any{"type": "oauth", "refresh": "r-never", "access": "a-old", "expires": time.Now().Add(time.Minute).UnixMilli(), "accountId": "refused@race"}) + if err != nil { + t.Fatal(err) + } + if err := raceSend(ctx, refused); err != nil { + t.Fatal(err) + } + if s := waitSaid(t, said); s != "expired" { + t.Fatalf("a refused token said %q, want expired", s) + } + if a := raceStored(refused); a["refresh"] != "r-never" || a["access"] != "a-old" { + t.Fatalf("a refused account was changed: %v", a) + } + + // turned away because another magpie, one that didn't take the lock, + // spent the token and saved what it got: not signed out, and its + // token is the one sent + raced, err := Import(ctx, "raceco", map[string]any{"type": "oauth", "refresh": "r-raced", "access": "a-old", "expires": time.Now().Add(time.Minute).UnixMilli(), "accountId": "raced@race"}) + if err != nil { + t.Fatal(err) + } + if err := changeStored(raced, "r-raced "+raced); err != nil { + t.Fatal(err) + } + if err := raceSend(ctx, raced); err != nil { + t.Fatal(err) + } + if err := raceSend(ctx, raced); err != nil { + t.Fatal(err) + } + Told() + select { + case s := <-said: + t.Fatalf("a token another magpie renewed meanwhile said %q", s) + default: + } + if _, auths := v.seen(); auths[len(auths)-1] != "Bearer a-elsewhere" { + t.Fatalf("requests went with %v, want the other magpie's token last", auths) + } +} diff --git a/internal/plugin/testdata/renewrace/index.js b/internal/plugin/testdata/renewrace/index.js new file mode 100644 index 000000000..317b5fac8 --- /dev/null +++ b/internal/plugin/testdata/renewrace/index.js @@ -0,0 +1,35 @@ +// A plugin that leaves renewing its sign-ins to magpie (auth.refresh) +// against a vendor that spends a refresh token once: $RACE_TOKEN_URL +// exchanges it, and answers one sent again, or never given, with +// invalid_grant, which signs the account out. Requests go to $FAKE_BASE. +export const RacePlugin = async () => ({ + config: async (cfg) => { + cfg.provider = cfg.provider ?? {} + cfg.provider.raceco = { + name: "RaceCo", + npm: "@ai-sdk/openai-compatible", + api: "https://race.invalid/v1", + models: { "race-1": { name: "Race One", limit: { context: 1000, output: 100 } } }, + } + }, + auth: { + provider: "raceco", + methods: [{ type: "api", label: "API key" }], + async refresh(auth) { + const res = await fetch(process.env.RACE_TOKEN_URL, { method: "POST", body: auth.refresh }) + const t = await res.json() + if (t.error === "invalid_grant") throw Object.assign(new Error("RaceCo: invalid_grant"), { signIn: "expired" }) + if (!res.ok) throw new Error("RaceCo: " + res.status) + return { access: t.access, refresh: t.refresh, expires: Date.now() + 3600e3 } + }, + loader: async (getAuth) => ({ + baseURL: process.env.FAKE_BASE, + async fetch(url, init) { + const a = await getAuth() + const h = new Headers(init.headers) + h.set("authorization", "Bearer " + a.access) + return fetch(url, { ...init, headers: h }) + }, + }), + }, +})