--force-update reports from/to versions; elevated parent no longer claims "update applied" when nothing changed
This commit is contained in:
+25
-22
@@ -173,11 +173,13 @@ func CleanupOld(exePath string) {
|
||||
|
||||
// Check performs a single update check. staged is true when a
|
||||
// signature-verified binary has been swapped into place at exePath; the
|
||||
// caller should then restart the process. A nil error with staged=false
|
||||
// means "no action" (up to date, APP_VER=dev, or no embedded public key);
|
||||
// a non-nil error means the check failed and the running binary is
|
||||
// untouched.
|
||||
func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err error) {
|
||||
// caller should then restart the process. to is the release tag the check
|
||||
// resolved (the latest release or the pinned tag), set once the release
|
||||
// fetch succeeded — even when staging afterwards fails. A nil error with
|
||||
// staged=false means "no action" (up to date, APP_VER=dev, or no embedded
|
||||
// public key); a non-nil error means the check failed and the running
|
||||
// binary is untouched.
|
||||
func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, to string, err error) {
|
||||
log := u.logger()
|
||||
desired := u.Desired
|
||||
if desired == "" {
|
||||
@@ -185,15 +187,15 @@ func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err e
|
||||
}
|
||||
if desired == "dev" {
|
||||
log.Debug("auto-update: APP_VER=dev, skipping")
|
||||
return false, nil
|
||||
return false, "", nil
|
||||
}
|
||||
if publicKeyPEM == "" {
|
||||
log.Debug("auto-update: no public key embedded, skipping")
|
||||
return false, nil
|
||||
return false, "", nil
|
||||
}
|
||||
api, err := u.apiURL()
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, "", err
|
||||
}
|
||||
|
||||
pinned := desired != "stable"
|
||||
@@ -203,29 +205,30 @@ func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err e
|
||||
}
|
||||
body, err := u.get(ctx, endpoint)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("fetch release: %w", err)
|
||||
return false, "", fmt.Errorf("fetch release: %w", err)
|
||||
}
|
||||
var rel release
|
||||
if err := json.Unmarshal(body, &rel); err != nil {
|
||||
return false, fmt.Errorf("parse release: %w", err)
|
||||
return false, "", fmt.Errorf("parse release: %w", err)
|
||||
}
|
||||
to = rel.TagName
|
||||
if pinned {
|
||||
// Pin mode: any difference from the target tag means stage it —
|
||||
// including downgrades and replacing a dev binary.
|
||||
if u.Version == rel.TagName {
|
||||
log.Debug("auto-update: already on pinned version", "version", u.Version)
|
||||
return false, nil
|
||||
return false, to, nil
|
||||
}
|
||||
} else if u.Version != "" && u.Version != "dev" {
|
||||
// Stable mode: only strictly newer releases count; a dev binary
|
||||
// cannot be compared and is always replaced by the latest release.
|
||||
newer, err := newerVersion(u.Version, rel.TagName)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, to, err
|
||||
}
|
||||
if !newer {
|
||||
log.Debug("auto-update: up to date", "version", u.Version, "latest", rel.TagName)
|
||||
return false, nil
|
||||
return false, to, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,42 +238,42 @@ func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err e
|
||||
}
|
||||
assetURL, ok := urls[u.Asset]
|
||||
if !ok {
|
||||
return false, fmt.Errorf("release %s has no asset %q", rel.TagName, u.Asset)
|
||||
return false, to, fmt.Errorf("release %s has no asset %q", rel.TagName, u.Asset)
|
||||
}
|
||||
sigURL, ok := urls[u.Asset+".sig"]
|
||||
if !ok {
|
||||
return false, fmt.Errorf("release %s has no signature asset %q", rel.TagName, u.Asset+".sig")
|
||||
return false, to, fmt.Errorf("release %s has no signature asset %q", rel.TagName, u.Asset+".sig")
|
||||
}
|
||||
|
||||
data, err := u.get(ctx, assetURL)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("download %s: %w", u.Asset, err)
|
||||
return false, to, fmt.Errorf("download %s: %w", u.Asset, err)
|
||||
}
|
||||
sig, err := u.get(ctx, sigURL)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("download signature: %w", err)
|
||||
return false, to, fmt.Errorf("download signature: %w", err)
|
||||
}
|
||||
|
||||
if sumURL, ok := urls[u.Asset+".sha256"]; ok {
|
||||
sumText, err := u.get(ctx, sumURL)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("download checksum: %w", err)
|
||||
return false, to, fmt.Errorf("download checksum: %w", err)
|
||||
}
|
||||
want := strings.Fields(string(sumText))[0]
|
||||
got := hex.EncodeToString(sha256Bytes(data))
|
||||
if !strings.EqualFold(want, got) {
|
||||
return false, fmt.Errorf("sha256 mismatch: got %s, want %s", got, want)
|
||||
return false, to, fmt.Errorf("sha256 mismatch: got %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
if err := verifySignature(publicKeyPEM, data, sig); err != nil {
|
||||
return false, err
|
||||
return false, to, err
|
||||
}
|
||||
|
||||
if err := stage(exePath, data); err != nil {
|
||||
return false, fmt.Errorf("stage update: %w", err)
|
||||
return false, to, fmt.Errorf("stage update: %w", err)
|
||||
}
|
||||
log.Info("auto-update: new version staged", "from", u.Version, "to", rel.TagName)
|
||||
return true, nil
|
||||
return true, to, nil
|
||||
}
|
||||
|
||||
func sha256Bytes(data []byte) []byte {
|
||||
|
||||
@@ -102,13 +102,16 @@ func TestCheckStagesUpdate(t *testing.T) {
|
||||
withPublicKey(t, f.pubPEM)
|
||||
exe := fakeExe(t)
|
||||
|
||||
staged, err := f.updater("v0.1.2").Check(context.Background(), exe)
|
||||
staged, to, err := f.updater("v0.1.2").Check(context.Background(), exe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !staged {
|
||||
t.Fatal("expected staged update")
|
||||
}
|
||||
if to != "v9.9.9" {
|
||||
t.Fatalf("to = %q, want v9.9.9", to)
|
||||
}
|
||||
content, _ := os.ReadFile(exe)
|
||||
if string(content) != "new-binary" {
|
||||
t.Fatalf("exe content = %q", content)
|
||||
@@ -125,7 +128,7 @@ func TestCheckRejectsTamperedSignature(t *testing.T) {
|
||||
withPublicKey(t, f.pubPEM)
|
||||
exe := fakeExe(t)
|
||||
|
||||
staged, err := f.updater("v0.1.2").Check(context.Background(), exe)
|
||||
staged, _, err := f.updater("v0.1.2").Check(context.Background(), exe)
|
||||
if err == nil {
|
||||
t.Fatal("expected signature error")
|
||||
}
|
||||
@@ -142,7 +145,7 @@ func TestCheckSkipsOlderOrEqual(t *testing.T) {
|
||||
for _, tag := range []string{"v0.1.2", "v0.1.1", "v0.0.9"} {
|
||||
f := newFakeGitea(t, tag, []byte("new-binary"))
|
||||
withPublicKey(t, f.pubPEM)
|
||||
staged, err := f.updater("v0.1.2").Check(context.Background(), fakeExe(t))
|
||||
staged, _, err := f.updater("v0.1.2").Check(context.Background(), fakeExe(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -155,7 +158,7 @@ func TestCheckSkipsOlderOrEqual(t *testing.T) {
|
||||
func TestCheckSkipsWithoutPublicKey(t *testing.T) {
|
||||
f := newFakeGitea(t, "v9.9.9", []byte("new-binary"))
|
||||
withPublicKey(t, "")
|
||||
staged, err := f.updater("v0.1.2").Check(context.Background(), fakeExe(t))
|
||||
staged, _, err := f.updater("v0.1.2").Check(context.Background(), fakeExe(t))
|
||||
if err != nil || staged {
|
||||
t.Fatalf("staged=%v err=%v, want no action without key", staged, err)
|
||||
}
|
||||
@@ -167,7 +170,7 @@ func TestCheckDevBuildGetsStable(t *testing.T) {
|
||||
f := newFakeGitea(t, "v9.9.9", []byte("new-binary"))
|
||||
withPublicKey(t, f.pubPEM)
|
||||
exe := fakeExe(t)
|
||||
staged, err := f.updater("dev").Check(context.Background(), exe)
|
||||
staged, _, err := f.updater("dev").Check(context.Background(), exe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -184,7 +187,7 @@ func TestCheckDesiredDevDisables(t *testing.T) {
|
||||
f := newFakeGitea(t, "v9.9.9", []byte("new-binary"))
|
||||
withPublicKey(t, f.pubPEM)
|
||||
for _, version := range []string{"dev", "v0.1.2"} {
|
||||
staged, err := f.updaterDesired(version, "dev").Check(context.Background(), fakeExe(t))
|
||||
staged, _, err := f.updaterDesired(version, "dev").Check(context.Background(), fakeExe(t))
|
||||
if err != nil || staged {
|
||||
t.Fatalf("version %s: staged=%v err=%v, want no action with APP_VER=dev", version, staged, err)
|
||||
}
|
||||
@@ -198,13 +201,16 @@ func TestCheckPinned(t *testing.T) {
|
||||
f := newFakeGitea(t, "v0.5.0", []byte("pinned-binary"))
|
||||
withPublicKey(t, f.pubPEM)
|
||||
exe := fakeExe(t)
|
||||
staged, err := f.updaterDesired(version, "v0.5.0").Check(context.Background(), exe)
|
||||
staged, to, err := f.updaterDesired(version, "v0.5.0").Check(context.Background(), exe)
|
||||
if err != nil {
|
||||
t.Fatalf("version %s: %v", version, err)
|
||||
}
|
||||
if !staged {
|
||||
t.Fatalf("version %s: expected pinned v0.5.0 to be staged", version)
|
||||
}
|
||||
if to != "v0.5.0" {
|
||||
t.Fatalf("version %s: to = %q, want v0.5.0", version, to)
|
||||
}
|
||||
content, _ := os.ReadFile(exe)
|
||||
if string(content) != "pinned-binary" {
|
||||
t.Fatalf("version %s: exe content = %q", version, content)
|
||||
@@ -213,7 +219,7 @@ func TestCheckPinned(t *testing.T) {
|
||||
|
||||
f := newFakeGitea(t, "v0.5.0", []byte("pinned-binary"))
|
||||
withPublicKey(t, f.pubPEM)
|
||||
staged, err := f.updaterDesired("v0.5.0", "v0.5.0").Check(context.Background(), fakeExe(t))
|
||||
staged, _, err := f.updaterDesired("v0.5.0", "v0.5.0").Check(context.Background(), fakeExe(t))
|
||||
if err != nil || staged {
|
||||
t.Fatalf("staged=%v err=%v, want no action when already on the pinned version", staged, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user