mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
产品名早已是 ModelRouter,但仓库标识符仍沿用旧名。这次统一,读写两侧都做兼容, 已部署的机器不会因为顺序问题立刻失效。 - go module `llmsproxy` → `modelrouter`;全仓 import 与 `cmd/modelrouter` 同步。 - `Makefile BINARY=modelrouter`;发行包名、systemd unit、/usr/share 资源、 `/usr/local/bin` 目标全部改为 modelrouter。 - 默认配置目录 `/etc/llmsproxy` → `/etc/modelrouter`。**读取侧回退**:插件与 `storeDir` 仍会检查旧路径,所以"先换二进制、后改 unit"不会立即失效。 - 环境变量 `LLMSPROXY_*`(PLUGIN_DIR / XDISPLAY_BASE / ARCH / LIVE_ZEN / BUILD_DIR)改为 `MODELROUTER_*`;`LLMS_PROXY_MASTER_KEY` → `MODELROUTER_MASTER_KEY`。旧名在读取侧继续接受。 - 桌面端内嵌核心 `bin/llmsproxy` → `bin/modelrouter`,并保留旧文件名回退, 避免升级到一半的 profile 被判成"core binary missing"。 - 新增 `packaging/migrate-to-modelrouter.sh`:**默认 dry-run**,复制而非移动、 旧目录永不删除、支持 `--rollback`;随 release tar 分发并由 core-dist 校验。 - `deploy.sh` 增加改名保护:发现旧 `/etc/llmsproxy` 而新配置不存在时**中止**, 否则会部署出一个"能启动但没有 key、没有源"的空实例。 - 临时目录前缀 `/tmp/llmsproxy-*` → `/tmp/modelrouter-*`;清理逻辑同时回收两代。 兼容别名(`__llmsproxy_*` 全局、`llmsproxy` SDK 别名、旧路径/旧 env)刻意保留, 它们是升级路径而非遗留;docs/plugins.md 已标注为废弃。 本提交未包含:`adapters/opencodezen.lua`、`adapter_opencodezen_test.go`、 `adapter_opencodezen_live_test.go`(OpenCode UA/免费池的既有改动,另批提交)。
179 lines
5.6 KiB
Go
179 lines
5.6 KiB
Go
package core
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"modelrouter/internal/config"
|
|
)
|
|
|
|
// Startup must be idempotent, and every credential consumer must see PLAINTEXT.
|
|
//
|
|
// Two bugs shared one root cause: NewFromConfig unsealed the config at the END,
|
|
// after the steps that read credentials had already run.
|
|
//
|
|
// 1. seedKeys() deduped cfg.Keys[i].Key against the plaintext gateway_keys
|
|
// entries. With ciphertext keys the comparison never matched, so every
|
|
// restart appended another copy of the same admin key — production reached
|
|
// four identical admin keys.
|
|
// 2. rebuildRegistry() handed cfg.Sources[i].APIKey to provider.New, so with
|
|
// ciphertext it built every upstream client with "enc:v1:..." as its bearer
|
|
// token and every upstream call would 401.
|
|
//
|
|
// They masked each other: seedKeys' Save() unsealed memory as a side effect, so
|
|
// bug 2 was invisible until the redundant saves were removed. These tests drive
|
|
// the real entry point (core.New -> config.Load off disk), because building a
|
|
// fresh in-memory config per attempt hides both bugs — that is exactly how the
|
|
// first draft of this test failed to reproduce anything.
|
|
|
|
// writeSealedInstall creates a config on disk the way a real install looks
|
|
// after its first run: credentials already sealed, gateway_keys still plaintext.
|
|
func writeSealedInstall(t *testing.T, dir string) string {
|
|
t.Helper()
|
|
path := filepath.Join(dir, "config.yaml")
|
|
cfg := &config.Config{
|
|
Path: path,
|
|
AdapterDir: filepath.Join(dir, "adapters"),
|
|
RuntimeFile: filepath.Join(dir, "runtime.json"),
|
|
Listen: "127.0.0.1:0",
|
|
DefaultModel: "AUTO",
|
|
GatewayKeys: []string{"sk-gw-seed-me"},
|
|
Sources: []config.Source{{
|
|
Name: "up", BaseURL: "http://127.0.0.1:1/v1", APIKey: "sk-upstream-secret",
|
|
Adapter: "openai", Models: []config.Model{{ID: "gpt-4o"}},
|
|
}},
|
|
}
|
|
c, err := NewFromConfig(cfg)
|
|
if err != nil {
|
|
t.Fatalf("initial install: %v", err)
|
|
}
|
|
c.Close()
|
|
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(raw), "enc:v1:") {
|
|
t.Fatalf("fixture is wrong: on-disk config holds no sealed credential, "+
|
|
"so this test cannot detect ciphertext leaking into consumers:\n%s", raw)
|
|
}
|
|
return path
|
|
}
|
|
|
|
// writeSealedInstallNoSeed is writeSealedInstall without gateway_keys, so no
|
|
// key is ever seeded and no startup Save() can unseal the config as a side
|
|
// effect. That isolation is what makes the ciphertext leak observable.
|
|
func writeSealedInstallNoSeed(t *testing.T, dir string) string {
|
|
t.Helper()
|
|
path := filepath.Join(dir, "config.yaml")
|
|
cfg := &config.Config{
|
|
Path: path,
|
|
AdapterDir: filepath.Join(dir, "adapters"),
|
|
RuntimeFile: filepath.Join(dir, "runtime.json"),
|
|
Listen: "127.0.0.1:0",
|
|
DefaultModel: "AUTO",
|
|
Sources: []config.Source{{
|
|
Name: "up", BaseURL: "http://127.0.0.1:1/v1", APIKey: "sk-upstream-secret",
|
|
Adapter: "openai", Models: []config.Model{{ID: "gpt-4o"}},
|
|
}},
|
|
}
|
|
c, err := NewFromConfig(cfg)
|
|
if err != nil {
|
|
t.Fatalf("initial install: %v", err)
|
|
}
|
|
c.Close()
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(raw), "enc:v1:") {
|
|
t.Fatalf("fixture is wrong: no sealed credential on disk:\n%s", raw)
|
|
}
|
|
return path
|
|
}
|
|
|
|
// countKeysWithSecret loads the config, unseals it, and counts keys whose
|
|
// plaintext equals one of the gateway_keys entries.
|
|
func countKeysWithSecret(t *testing.T, path string) (total, matching int) {
|
|
t.Helper()
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
box, err := config.NewSecretBox(cfg.RuntimeFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := cfg.UnsealSecrets(box); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := map[string]bool{}
|
|
for _, g := range cfg.GatewayKeys {
|
|
want[g] = true
|
|
}
|
|
for _, k := range cfg.Keys {
|
|
total++
|
|
if want[k.Key] {
|
|
matching++
|
|
}
|
|
}
|
|
return total, matching
|
|
}
|
|
|
|
// Restarting must not grow the key list. Before the fix this went 2 -> 3 -> 4.
|
|
func TestRestartDoesNotDuplicateSeededKeys(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := writeSealedInstall(t, dir)
|
|
|
|
if _, n := countKeysWithSecret(t, path); n != 1 {
|
|
t.Fatalf("after install: %d keys carry the seeded secret, want 1", n)
|
|
}
|
|
|
|
for i := 2; i <= 4; i++ {
|
|
c, err := New(path) // production path: reads the file
|
|
if err != nil {
|
|
t.Fatalf("restart %d: %v", i, err)
|
|
}
|
|
c.Close()
|
|
total, n := countKeysWithSecret(t, path)
|
|
if n != 1 {
|
|
t.Fatalf("restart %d: %d keys carry the seeded secret (total %d), want "+
|
|
"exactly 1 — seedKeys is comparing ciphertext against plaintext and "+
|
|
"appending a duplicate on every start", i, n, total)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Every provider must hold the real upstream credential, not its ciphertext.
|
|
//
|
|
// This deliberately runs with gateway_keys EMPTY. The buggy order was masked
|
|
// whenever seedKeys had work to do, because its Save() unsealed memory as a
|
|
// side effect — providers then happened to see plaintext. With no key to seed,
|
|
// no Save runs, and the ciphertext reaches the registry directly. An earlier
|
|
// version of this test kept gateway_keys populated and was insensitive: it
|
|
// passed even with the bug present.
|
|
func TestProvidersGetPlaintextCredentials(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := writeSealedInstallNoSeed(t, dir)
|
|
|
|
c, err := New(path)
|
|
if err != nil {
|
|
t.Fatalf("restart: %v", err)
|
|
}
|
|
defer c.Close()
|
|
|
|
for _, p := range c.registry.Providers() {
|
|
key := p.Config().APIKey
|
|
if strings.HasPrefix(key, "enc:v1:") {
|
|
t.Errorf("provider %q holds CIPHERTEXT api_key %q — every upstream call "+
|
|
"would 401", p.Name(), key)
|
|
}
|
|
if key != "sk-upstream-secret" {
|
|
t.Errorf("provider %q api_key = %q, want the plaintext upstream secret",
|
|
p.Name(), key)
|
|
}
|
|
}
|
|
}
|