From a0cc553d2de19e826f63834e345f8ff5c98d4503 Mon Sep 17 00:00:00 2001 From: mrlee <470349233@qq.com> Date: Sun, 11 Oct 2026 15:59:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(native):=20updater=20Token=20=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E6=8B=96=E5=9E=AE=E9=9D=9E=20root=20=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=EF=BC=88=E9=A3=9E=E7=89=9B=E5=8E=9F=E7=94=9F=E7=89=88?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E5=90=AF=E7=94=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原生版以非 root 的 havline 用户运行,而 app.New 会无条件调用 updater.EnsureToken, 去写默认的 /run/havline-updater/updater.token:MkdirAll 因权限不足失败 → app.New 返回错误 → main 里 log.Fatalf → 进程秒退,飞牛应用中心报「本地应用启动失败」。 该逻辑由 v1.9.2 引入,v1.9.1 及之前不受影响。 - 新增 updater.EnsureTokenIfEnabled:updater 未启用(socket 为空)时直接返回, 完全不触碰 Token 文件 - app.New 改用该函数,只有真正启用一键升级 sidecar 时才准备共享 Token - 补充回归测试:socket 为空不创建文件、socket 非空照旧创建 --- internal/app/app.go | 4 +++- internal/updater/token.go | 14 ++++++++++++++ internal/updater/token_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/internal/app/app.go b/internal/app/app.go index 15031aa..5979ad5 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -70,7 +70,9 @@ func New(cfg config.Config, staticFS fs.FS, migrationsDir string) (*App, error) } logger.Info("会话密钥已就绪", "module", "SYSTEM", "source", secretSource) - updaterToken, updaterTokenSource, err := updater.EnsureToken(cfg.UpdaterToken, cfg.UpdaterTokenFile) + // updater Token 只给「一键升级」sidecar 用;原生版与飞牛 Docker 版都显式把 socket 置空, + // 此时不能去写 /run/havline-updater:原生版以非 root 运行,权限不足会让应用直接起不来。 + updaterToken, updaterTokenSource, err := updater.EnsureTokenIfEnabled(cfg.UpdaterSocket, cfg.UpdaterToken, cfg.UpdaterTokenFile) if err != nil { return nil, err } diff --git a/internal/updater/token.go b/internal/updater/token.go index 254dc8a..e6b6b9f 100644 --- a/internal/updater/token.go +++ b/internal/updater/token.go @@ -67,6 +67,20 @@ func EnsureToken(token, tokenFile string) (string, string, error) { return value, path, nil } +// EnsureTokenIfEnabled 只在 updater 启用(socket 非空)时准备共享 Token。 +// +// updater Token 只服务于「一键升级」sidecar,而 sidecar 与主程序是通过共享卷里的 +// Token 文件 + Unix Socket 对接的。原生版(飞牛 Native)与飞牛 Docker 版都显式把 +// socket 置空表示「不启用升级」,此时根本不需要这个文件: +// 原生版以非 root 用户(havline)运行,去写 /run/havline-updater 会因权限不足报错, +// 而启动期的这个错误会让整个应用起不来。 +func EnsureTokenIfEnabled(socketPath, token, tokenFile string) (string, string, error) { + if strings.TrimSpace(socketPath) == "" { + return "", "", nil + } + return EnsureToken(token, tokenFile) +} + // WaitForToken 等待主程序在共享卷中创建 Token 文件;环境变量已设置时立即返回。 func WaitForToken(token, tokenFile string, timeout time.Duration) (string, string, error) { if timeout <= 0 { diff --git a/internal/updater/token_test.go b/internal/updater/token_test.go index bf74eb7..4a89742 100644 --- a/internal/updater/token_test.go +++ b/internal/updater/token_test.go @@ -1,6 +1,7 @@ package updater import ( + "errors" "os" "path/filepath" "testing" @@ -105,3 +106,36 @@ func TestWaitForTokenTimesOut(t *testing.T) { t.Fatal("文件不存在时应等待超时") } } + +// updater 未启用(socket 为空)时不能去碰 Token 文件: +// 原生版以非 root 运行,写 /run/havline-updater 会失败并拖垮整个启动。 +func TestEnsureTokenIfEnabledSkipsWhenSocketDisabled(t *testing.T) { + tokenFile := filepath.Join(t.TempDir(), "nested", "updater.token") + + token, source, err := EnsureTokenIfEnabled("", "", tokenFile) + if err != nil { + t.Fatalf("socket 未启用时不应报错:%v", err) + } + if token != "" || source != "" { + t.Fatalf("socket 未启用时应返回空 Token,实际 token=%q source=%q", token, source) + } + if _, statErr := os.Stat(tokenFile); !errors.Is(statErr, os.ErrNotExist) { + t.Fatalf("socket 未启用时不应创建 Token 文件(statErr=%v)", statErr) + } +} + +// 对照组:updater 启用(socket 非空)时仍照旧生成并落盘。 +func TestEnsureTokenIfEnabledCreatesWhenSocketSet(t *testing.T) { + tokenFile := filepath.Join(t.TempDir(), "nested", "updater.token") + + token, source, err := EnsureTokenIfEnabled("/run/havline-updater/updater.sock", "", tokenFile) + if err != nil { + t.Fatalf("socket 启用时应生成 Token:%v", err) + } + if len(token) != 64 || source != tokenFile { + t.Fatalf("生成的 Token 不正确,实际 token=%q source=%q", token, source) + } + if _, statErr := os.Stat(tokenFile); statErr != nil { + t.Fatalf("socket 启用时应写入 Token 文件:%v", statErr) + } +}