Repository navigation
Expand file tree
/
Copy pathupdate_cmd.go
More file actions
741 lines (660 loc) · 26.5 KB
/
Copy pathupdate_cmd.go
File metadata and controls
741 lines (660 loc) · 26.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/spf13/cobra"
"go.uber.org/zap"
"golang.org/x/mod/semver"
clioutput "github.com/smart-mcp-proxy/mcpproxy-go/internal/cli/output"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/httpapi"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/updatecheck"
)
// update_cmd.go implements `mcpproxy update` (Spec 092 US3 / FR-020..FR-023):
// one command that knows how this binary was installed and either prints the
// exact package-manager command, points at the tray updater, or performs a
// verified self-replace — never corrupting a package manager's bookkeeping and
// never touching an app bundle.
// Actions the command can take. They are part of the -o json contract.
const (
actionUpToDate = "up-to-date"
actionCommand = "command" // a package manager owns the install
actionGuidance = "guidance" // no safe command exists
actionTray = "tray" // macOS app bundle: the tray owns updates
actionSelfUpdate = "self-update" // positively identified self-managed install
)
// Cosign identity pinning for checksums.txt (FR-021). These mirror the verify
// recipe documented next to the signing step in each workflow: releases are
// only ever cut from a tag, so the ref is pinned too — a workflow_dispatch run
// on a branch must not be able to sign an artifact this command will install.
//
// Two workflows, because RCs are cut by a separate pipeline (FR-014) and a
// user on the prerelease channel must be able to verify what it publishes.
// Both are tag-gated and both live in this repository, so accepting the second
// one widens the trusted set by nothing a tag push could not already do.
const (
cosignIdentityRegexp = `^https://github\.com/smart-mcp-proxy/mcpproxy-go/\.github/workflows/(release|prerelease)\.yml@refs/tags/v`
cosignOIDCIssuer = "https://token.actions.githubusercontent.com"
)
const (
checksumsAssetName = "checksums.txt"
cosignBundleName = "checksums.txt.cosign.bundle"
downloadTimeout = 10 * time.Minute
cosignTimeout = 2 * time.Minute
)
type updateFlags struct {
checkOnly bool
self bool
targetVersion string
force bool
allowUnverified bool
}
// releaseSource is the release-metadata seam. Tests point it at an httptest
// server; production uses internal/updatecheck's GitHub client.
type releaseSource interface {
Latest(includePrereleases bool) (*updatecheck.GitHubRelease, error)
ByTag(tag string) (*updatecheck.GitHubRelease, error)
}
type githubReleaseSource struct {
client *updatecheck.GitHubClient
}
func (g *githubReleaseSource) Latest(includePrereleases bool) (*updatecheck.GitHubRelease, error) {
return g.client.GetRelease(includePrereleases)
}
func (g *githubReleaseSource) ByTag(tag string) (*updatecheck.GitHubRelease, error) {
return g.client.GetReleaseByTag(tag)
}
// updateReport is the machine-readable result of the command (-o json/yaml)
// and the source of the human-readable rendering.
type updateReport struct {
CurrentVersion string `json:"current_version" yaml:"current_version"`
LatestVersion string `json:"latest_version,omitempty" yaml:"latest_version,omitempty"`
Channel string `json:"channel" yaml:"channel"`
UpdateAvailable bool `json:"update_available" yaml:"update_available"`
Action string `json:"action" yaml:"action"`
Command string `json:"command,omitempty" yaml:"command,omitempty"`
Guidance string `json:"guidance,omitempty" yaml:"guidance,omitempty"`
ReleaseURL string `json:"release_url,omitempty" yaml:"release_url,omitempty"`
IsPrerelease bool `json:"is_prerelease,omitempty" yaml:"is_prerelease,omitempty"`
Applied bool `json:"applied" yaml:"applied"`
Message string `json:"message,omitempty" yaml:"message,omitempty"`
}
// updateRunner carries every dependency the command touches so the whole
// decision table is testable without a network, a real install, or cobra.
type updateRunner struct {
out io.Writer
errOut io.Writer
format string
currentVersion string
channel string
execPath string // symlink-resolved path of the binary to replace
includePrereleases bool
flags updateFlags
releases releaseSource
httpClient *http.Client
cosignVerify func(checksumsPath, bundlePath string) error
verifyInstalled func(path, version string) error
cosignAvailable func() bool
// selfUpdateFn performs the download/verify/swap. Indirected so the
// decision-table tests can assert "this branch would (not) have installed
// something" without staging a real release.
selfUpdateFn func(*updatecheck.GitHubRelease) error
}
// GetUpdateCommand returns the `update` subcommand.
func GetUpdateCommand() *cobra.Command {
flags := updateFlags{}
cmd := &cobra.Command{
Use: "update",
Short: "Update MCPProxy, or show how to update it for your install channel",
Long: `Check for a newer MCPProxy release and update it the way your install expects.
MCPProxy detects how it was installed and acts accordingly:
homebrew / deb / rpm / go-install prints the exact upgrade command
docker / windows-installer prints channel-appropriate guidance
dmg (macOS app bundle) points at the tray updater; never touches the bundle
tarball (official release archive) downloads, verifies and swaps the binary in place
unknown guidance only, unless you assert ownership with --self
Self-update verifies the release's signed checksum manifest before replacing
anything, keeps the previous binary until the new one runs, and never
escalates privileges.
Examples:
mcpproxy update --check # report current/latest/channel, change nothing
mcpproxy update # do the right thing for this install
mcpproxy update --self # assert a self-managed install on an unknown channel
mcpproxy update --version v0.54.0 --force # deliberate downgrade`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
runner, err := newUpdateRunner(cmd.OutOrStdout(), cmd.ErrOrStderr(), flags)
if err != nil {
return err
}
return runner.run()
},
}
cmd.Flags().BoolVar(&flags.checkOnly, "check", false, "Report current version, latest version and install channel without changing anything")
cmd.Flags().BoolVar(&flags.self, "self", false, "Assert that you manage this binary yourself (allows self-update on the unknown channel)")
cmd.Flags().StringVar(&flags.targetVersion, "version", "", "Install this exact release instead of the latest (required for a downgrade)")
cmd.Flags().BoolVar(&flags.force, "force", false, "Allow installing a version that is not newer (requires --version)")
cmd.Flags().BoolVar(&flags.allowUnverified, "allow-unverified-signature", false,
"Proceed when the release signature cannot be verified (checksum verification still applies)")
return cmd
}
// newUpdateRunner wires the production dependencies.
func newUpdateRunner(out, errOut io.Writer, flags updateFlags) (*updateRunner, error) {
version := httpapi.GetBuildVersion()
channel := updatecheck.DetectChannel(version)
// go-install builds carry the non-semver ldflags default; promote to the
// module version exactly like the daemon's checker does, so the channel
// precedence below classifies the build identically (FR-023).
version = updatecheck.PromoteGoInstallVersion(version, channel)
execPath, err := resolvedExecutablePath()
if err != nil {
return nil, fmt.Errorf("cannot determine the running binary's path: %w", err)
}
client := updatecheck.NewGitHubClient(zap.NewNop())
return &updateRunner{
out: out,
errOut: errOut,
format: clioutput.ResolveFormat(globalOutputFormat, globalJSONOutput),
currentVersion: version,
channel: channel,
execPath: execPath,
includePrereleases: prereleasePreference(version),
flags: flags,
releases: &githubReleaseSource{client: client},
httpClient: &http.Client{Timeout: downloadTimeout},
cosignVerify: verifyWithCosignBinary,
verifyInstalled: verifyInstalledVersion,
cosignAvailable: cosignOnPath,
}, nil
}
// prereleasePreference mirrors the checker's precedence (Spec 079 FR-014):
// the released build's own identity is authoritative, then
// MCPPROXY_ALLOW_PRERELEASE_UPDATES wins over update_check.channel, so
// `mcpproxy update` offers exactly what the daemon's nudge offered (FR-023).
func prereleasePreference(buildVersion string) bool {
cfgPrerelease := false
if cfg, err := loadCLIConfig(configFile); err == nil && cfg != nil {
cfgPrerelease = cfg.UpdateCheck.IncludePrereleases()
}
return prereleasePreferenceFor(buildVersion, cfgPrerelease)
}
// prereleasePreferenceFor is the config-independent core of
// prereleasePreference, split out for tests.
func prereleasePreferenceFor(buildVersion string, cfgPrerelease bool) bool {
return updatecheck.IncludePrereleasesForBuild(buildVersion, cfgPrerelease)
}
// resolvedExecutablePath returns the symlink-resolved path of the running
// binary. Resolving matters for FR-021: a symlinked launcher
// (~/.local/bin/mcpproxy -> ~/opt/mcpproxy/mcpproxy) must have its destination
// replaced, not the symlink itself.
func resolvedExecutablePath() (string, error) {
raw, err := os.Executable()
if err != nil {
return "", err
}
if resolved, err := filepath.EvalSymlinks(raw); err == nil && resolved != "" {
return resolved, nil
}
return raw, nil
}
// insideAppBundle reports whether path lives in (or is a staged copy of) the
// macOS app bundle. FR-022 forbids modifying either, and the CLI must reach
// that conclusion from the path alone — the DMG heuristics in
// internal/updatecheck only run on darwin, while this guard must hold
// everywhere (a bundle copied onto another OS is still not ours to rewrite).
func insideAppBundle(path string) bool {
if path == "" {
return false
}
normalized := filepath.ToSlash(path)
return strings.Contains(normalized, "MCPProxy.app/") ||
strings.HasSuffix(normalized, "/Library/Application Support/mcpproxy/bin/mcpproxy")
}
// effectiveChannel applies the app-bundle override on top of the detected
// channel.
func (r *updateRunner) effectiveChannel() string {
if insideAppBundle(r.execPath) {
return updatecheck.ChannelDMG
}
return r.channel
}
// decideAction maps a channel to what the command will do (FR-020).
func decideAction(channel string, self bool) string {
switch channel {
case updatecheck.ChannelHomebrew, updatecheck.ChannelDeb, updatecheck.ChannelRPM, updatecheck.ChannelGoInstall:
return actionCommand
case updatecheck.ChannelDocker, updatecheck.ChannelWindowsInstaller:
return actionGuidance
case updatecheck.ChannelDMG:
return actionTray
case updatecheck.ChannelTarball:
return actionSelfUpdate
case updatecheck.ChannelUnknown:
// FR-020: writability is not ownership. Only an explicit user
// assertion turns unknown into a self-managed install.
if self {
return actionSelfUpdate
}
return actionGuidance
default:
return actionGuidance
}
}
func (r *updateRunner) run() error {
channel := r.effectiveChannel()
release, err := r.resolveRelease()
if err != nil {
return err
}
latest := release.TagName
newer := isNewer(r.currentVersion, latest)
report := &updateReport{
CurrentVersion: normalizeVersion(r.currentVersion),
LatestVersion: normalizeVersion(latest),
Channel: channel,
UpdateAvailable: newer,
ReleaseURL: release.HTMLURL,
IsPrerelease: release.Prerelease,
}
action := decideAction(channel, r.flags.self)
r.annotateAction(report, action, release)
// A development build has no place on the release timeline: comparing it
// would either offer every release as an "update" or silently claim it is
// current. Say so instead (FR-006: malformed versions are a logged
// no-decision, not a guess).
devBuild := !semver.IsValid(normalizeVersion(r.currentVersion))
if devBuild {
report.Message = fmt.Sprintf(
"Running a development build (%s), which cannot be compared to released versions. "+
"Install a specific release with --version <tag> --force.", report.CurrentVersion)
}
// --check never acts; it reports what would happen (FR-023).
if r.flags.checkOnly {
if !newer && !devBuild {
report.Message = "Already up to date."
}
return r.emit(report)
}
if devBuild && r.flags.targetVersion == "" {
return r.emit(report)
}
// Nothing newer and no explicit target: same output as --check (FR-023).
if !newer && r.flags.targetVersion == "" {
report.Action = actionUpToDate
report.Message = "Already up to date."
return r.emit(report)
}
// An explicit target that is not newer is a downgrade/reinstall and needs
// --force as well (FR-022).
if !newer && !r.flags.force {
return fmt.Errorf("refusing to install %s: it is not newer than the running %s. "+
"Pass --force together with --version to install it deliberately",
normalizeVersion(latest), normalizeVersion(r.currentVersion))
}
switch action {
case actionSelfUpdate:
// FR-020: --self is the user asserting ownership of a binary mcpproxy
// could NOT positively identify. Say plainly what that assertion means
// before acting on it — an AUR/MacPorts/Nix-style layout is writable
// and still owned by a package manager whose bookkeeping this would
// silently invalidate. Positively identified tarball installs get no
// warning; there is nothing uncertain about them.
if channel == updatecheck.ChannelUnknown {
fmt.Fprintf(r.errOut,
"WARNING: --self: mcpproxy could not identify how %s was installed and is replacing it "+
"because you asserted you manage it yourself. If a package manager owns this path, "+
"its bookkeeping will no longer match what is on disk.\n",
r.execPath)
}
apply := r.selfUpdateFn
if apply == nil {
apply = r.selfUpdate
}
if err := apply(release); err != nil {
return err
}
report.Applied = true
report.Message = fmt.Sprintf("Updated %s to %s. Restart any running mcpproxy process to pick it up.",
r.execPath, normalizeVersion(latest))
return r.emit(report)
default:
// Every other channel is guidance-only and exits 0 having changed
// nothing (FR-020, SC-005).
return r.emit(report)
}
}
// annotateAction fills in the action-specific command/guidance text.
func (r *updateRunner) annotateAction(report *updateReport, action string, release *updatecheck.GitHubRelease) {
report.Action = action
switch action {
case actionCommand:
// A prerelease is only ever published to the GitHub prerelease
// channel, so the generic package-manager command would install a
// different (stable) version than the one advertised — mirror the
// checker's rule and let it degrade to guidance instead (FR-009).
if release.Prerelease {
report.Command = updatecheck.PrereleaseUpdateCommand(report.Channel, release.TagName)
} else {
report.Command = updatecheck.UpdateCommand(report.Channel)
}
if report.Command == "" {
// A prerelease on a package-manager channel has no command that
// would actually deliver it; fall back to guidance.
report.Action = actionGuidance
report.Guidance = updatecheck.GuidanceLine(report.Channel, release.HTMLURL)
}
case actionGuidance:
report.Guidance = updatecheck.GuidanceLine(report.Channel, release.HTMLURL)
if report.Channel == updatecheck.ChannelUnknown && !r.flags.self {
report.Guidance += ". If you manage this binary yourself (an extracted release archive), " +
"re-run with --self to let mcpproxy replace it"
}
case actionTray:
report.Guidance = "MCPProxy is installed as a macOS app bundle — use the menu bar app: " +
"MCPProxy menu -> Check for Updates. `mcpproxy update` never modifies an app bundle " +
"or its staged copies"
case actionSelfUpdate:
report.Command = "mcpproxy update"
}
}
// resolveRelease picks the release to act on: the explicitly requested tag, or
// the newest one on the selected channel.
func (r *updateRunner) resolveRelease() (*updatecheck.GitHubRelease, error) {
if r.flags.targetVersion != "" {
tag := normalizeVersion(r.flags.targetVersion)
release, err := r.releases.ByTag(tag)
if err != nil {
return nil, err
}
return release, nil
}
release, err := r.releases.Latest(r.includePrereleases)
if err != nil {
return nil, fmt.Errorf("could not determine the latest release: %w", err)
}
return release, nil
}
// selfUpdate downloads, verifies and installs the release (FR-021).
func (r *updateRunner) selfUpdate(release *updatecheck.GitHubRelease) error {
target := r.execPath
if insideAppBundle(target) {
// Defence in depth: decideAction already routes bundles to the tray.
return fmt.Errorf("refusing to modify %s: it is inside a macOS app bundle", target)
}
// Checked here as well as inside applyNewBinary so a concurrent update is
// caught before ~90 MB is downloaded on its behalf.
if aliasErr := refuseAliasedUpdateTarget(target); aliasErr != nil {
return aliasErr
}
// Fail before spending a ~90 MB download on an install we cannot write.
if err := ensureTargetWritable(target); err != nil {
return err
}
version := normalizeVersion(release.TagName)
assetName := archiveAssetName(version, runtime.GOOS, runtime.GOARCH)
archiveURL, err := assetURL(release, assetName)
if err != nil {
return err
}
checksumsURL, err := assetURL(release, checksumsAssetName)
if err != nil {
return fmt.Errorf("release %s publishes no %s, so its artifacts cannot be verified: %w",
version, checksumsAssetName, err)
}
bundleURL, bundleErr := assetURL(release, cosignBundleName)
workDir, err := os.MkdirTemp("", "mcpproxy-update-*")
if err != nil {
return fmt.Errorf("create work directory: %w", err)
}
defer os.RemoveAll(workDir)
checksumsPath := filepath.Join(workDir, checksumsAssetName)
if err := r.download(checksumsURL, checksumsPath); err != nil {
return err
}
// Signature verification of the checksum manifest (FR-021).
if err := r.verifySignature(workDir, checksumsPath, bundleURL, bundleErr); err != nil {
return err
}
manifest, err := openChecksums(checksumsPath)
if err != nil {
return err
}
wantDigest, ok := manifest[assetName]
if !ok {
return fmt.Errorf("%s is not listed in %s for release %s; refusing to install an unlisted artifact",
assetName, checksumsAssetName, version)
}
archivePath := filepath.Join(workDir, assetName)
if err := r.download(archiveURL, archivePath); err != nil {
return err
}
if err := verifyFileSHA256(archivePath, wantDigest); err != nil {
return err
}
// Stage inside the target directory: a rename across filesystems fails
// (EXDEV), and FR-021 requires the swap itself to be a rename.
stagedPath := filepath.Join(filepath.Dir(target), fmt.Sprintf(".%s.new-%d", filepath.Base(target), os.Getpid()))
_ = os.Remove(stagedPath)
defer os.Remove(stagedPath)
if err := extractBinary(archivePath, coreBinaryName(), stagedPath); err != nil {
return err
}
verify := r.verifyInstalled
if verify == nil {
verify = verifyInstalledVersion
}
return applyNewBinary(target, stagedPath, func(path string) error {
return verify(path, version)
})
}
// verifySignature verifies the cosign bundle over checksums.txt.
//
// COMPROMISE (see the report's open decision #5): this shells out to a
// user-installed cosign instead of verifying in-process with sigstore-go.
// sigstore-go v1.3.0 pulls 72 additional linked modules (+16 MB to a
// hello-world binary) and requires bumping go.mod's Go directive — a
// dependency decision the maintainer has not made. Until then: the sha256
// comparison against checksums.txt is unconditional, and signature
// verification is REQUIRED by default — a missing cosign aborts the update
// rather than silently downgrading the trust model. --allow-unverified-
// signature is the explicit, loudly warned opt-out.
func (r *updateRunner) verifySignature(workDir, checksumsPath, bundleURL string, bundleErr error) error {
if r.flags.allowUnverified {
fmt.Fprintf(r.errOut,
"WARNING: --allow-unverified-signature: installing after checksum verification only. "+
"The release signature was NOT checked, so a compromised %s would not be detected.\n",
checksumsAssetName)
return nil
}
if bundleErr != nil || bundleURL == "" {
return fmt.Errorf("release publishes no %s, so its checksums cannot be authenticated. "+
"Re-run with --allow-unverified-signature to accept checksum-only verification", cosignBundleName)
}
available := r.cosignAvailable
if available == nil {
available = cosignOnPath
}
if !available() {
return fmt.Errorf("cosign is required to verify the release signature but was not found on PATH.\n" +
"Install it (https://docs.sigstore.dev/cosign/installation/) and re-run, or accept " +
"checksum-only verification with --allow-unverified-signature")
}
bundlePath := filepath.Join(workDir, cosignBundleName)
if err := r.download(bundleURL, bundlePath); err != nil {
return err
}
verify := r.cosignVerify
if verify == nil {
verify = verifyWithCosignBinary
}
if err := verify(checksumsPath, bundlePath); err != nil {
return fmt.Errorf("release signature verification failed, nothing was installed: %w", err)
}
return nil
}
func cosignOnPath() bool {
_, err := exec.LookPath("cosign")
return err == nil
}
// verifyWithCosignBinary runs the same verification the release workflow
// documents next to its signing step, with the identity and issuer pinned.
func verifyWithCosignBinary(checksumsPath, bundlePath string) error {
ctx, cancel := context.WithTimeout(context.Background(), cosignTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "cosign", "verify-blob", // #nosec G204 -- fixed argv; only file paths this process created vary
"--bundle", bundlePath,
"--certificate-identity-regexp", cosignIdentityRegexp,
"--certificate-oidc-issuer", cosignOIDCIssuer,
checksumsPath,
)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out)))
}
return nil
}
func openChecksums(path string) (map[string]string, error) {
f, err := os.Open(path) // #nosec G304 -- file this process just downloaded into its own temp dir
if err != nil {
return nil, fmt.Errorf("open %s: %w", checksumsAssetName, err)
}
defer f.Close()
return parseChecksums(f)
}
// download fetches url into dest.
func (r *updateRunner) download(url, dest string) error {
client := r.httpClient
if client == nil {
client = &http.Client{Timeout: downloadTimeout}
}
resp, err := client.Get(url) // #nosec G107 -- url comes from the GitHub release metadata we just fetched
if err != nil {
return fmt.Errorf("download %s: %w", filepath.Base(dest), err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download %s: server returned status %d", filepath.Base(dest), resp.StatusCode)
}
out, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600) // #nosec G304 -- dest is inside our own temp dir
if err != nil {
return fmt.Errorf("create %s: %w", dest, err)
}
if _, err := io.Copy(out, io.LimitReader(resp.Body, maxArchiveMemberBytes+1)); err != nil {
out.Close()
return fmt.Errorf("write %s: %w", dest, err)
}
return out.Close()
}
// assetURL returns the download URL of the named release asset.
func assetURL(release *updatecheck.GitHubRelease, name string) (string, error) {
for i := range release.Assets {
if release.Assets[i].Name == name {
return release.Assets[i].BrowserDownloadURL, nil
}
}
return "", fmt.Errorf("release %s has no asset named %s (this platform may not be published for that release)",
release.TagName, name)
}
// archiveAssetName builds the release archive filename the pipeline produces:
// mcpproxy-<version-without-v>-<goos>-<goarch>.<tar.gz|zip>.
func archiveAssetName(version, goos, goarch string) string {
ext := ".tar.gz"
if goos == "windows" {
ext = ".zip"
}
return fmt.Sprintf("mcpproxy-%s-%s-%s%s", strings.TrimPrefix(version, "v"), goos, goarch, ext)
}
// coreBinaryName is the archive member to install. Archives always store the
// canonical name, whatever the user renamed their local copy to. The server
// edition ships as mcpproxy-server, so a server binary must never extract the
// personal-edition member over itself.
func coreBinaryName() string {
name := "mcpproxy"
if Edition == "server" {
name = "mcpproxy-server"
}
if runtime.GOOS == "windows" {
name += ".exe"
}
return name
}
// isNewer reports whether latest is strictly newer than current under SemVer
// precedence (FR-006: rc.10 > rc.2). A non-semver current version (a dev
// build) is never "older": offering a release against it would be a guess.
func isNewer(current, latest string) bool {
c := normalizeVersion(current)
l := normalizeVersion(latest)
if !semver.IsValid(c) || !semver.IsValid(l) {
return false
}
return semver.Compare(c, l) < 0
}
// normalizeVersion adds the "v" prefix semver.Compare needs, but only to
// something that actually looks like a version — "development" must stay
// "development" rather than being rendered as "vdevelopment".
func normalizeVersion(v string) string {
v = strings.TrimSpace(v)
if v == "" || strings.HasPrefix(v, "v") {
return v
}
if v[0] >= '0' && v[0] <= '9' {
return "v" + v
}
return v
}
// emit renders the report in the requested format.
func (r *updateRunner) emit(report *updateReport) error {
switch strings.ToLower(r.format) {
case "json", "yaml":
formatter, err := clioutput.NewFormatter(r.format)
if err != nil {
return err
}
out, err := formatter.Format(report)
if err != nil {
return err
}
fmt.Fprintln(r.out, out)
return nil
default:
r.printReport(report)
return nil
}
}
func (r *updateRunner) printReport(report *updateReport) {
fmt.Fprintln(r.out, "MCPProxy update")
fmt.Fprintf(r.out, " %-10s %s\n", "Current:", report.CurrentVersion)
if report.LatestVersion != "" {
label := report.LatestVersion
if report.IsPrerelease {
label += " (prerelease)"
}
fmt.Fprintf(r.out, " %-10s %s\n", "Latest:", label)
}
fmt.Fprintf(r.out, " %-10s %s\n", "Channel:", report.Channel)
if report.ReleaseURL != "" {
fmt.Fprintf(r.out, " %-10s %s\n", "Release:", report.ReleaseURL)
}
fmt.Fprintln(r.out)
switch {
case report.Applied:
fmt.Fprintln(r.out, report.Message)
case report.Message != "" && !report.UpdateAvailable:
fmt.Fprintln(r.out, report.Message)
case report.Action == actionUpToDate || !report.UpdateAvailable:
fmt.Fprintln(r.out, "Already up to date.")
case report.Action == actionSelfUpdate && r.flags.checkOnly:
fmt.Fprintln(r.out, "Run `mcpproxy update` to download, verify and install it.")
case report.Command != "":
fmt.Fprintf(r.out, "Run: %s\n", report.Command)
case report.Guidance != "":
fmt.Fprintln(r.out, report.Guidance)
}
}