diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 11fd8a68..d5032f04 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -455,6 +455,9 @@ func deleteFile(deletePath string) error { if isSysErrNotEmpty(e) { return nil } + if os.IsNotExist(e) { + return nil + } return e } diff --git a/cmd/progress-bar.go b/cmd/progress-bar.go index 5bbc29fa..1bf85471 100644 --- a/cmd/progress-bar.go +++ b/cmd/progress-bar.go @@ -18,6 +18,7 @@ package cmd import ( + "io" "runtime" "strings" "time" @@ -33,6 +34,53 @@ type progressBar struct { *pb.ProgressBar } +func newProgressReader(r io.Reader, caption string, total int64) *pb.Reader { + // Progress bar specific theme customization. + console.SetColor("Bar", color.New(color.FgGreen, color.Bold)) + + // get the new original progress bar. + bar := pb.New64(total) + + // Set new human friendly print units. + bar.SetUnits(pb.U_BYTES) + + // Refresh rate for progress bar is set to 125 milliseconds. + bar.SetRefreshRate(time.Millisecond * 125) + + // Do not print a newline by default handled, it is handled manually. + bar.NotPrint = true + + // Show current speed is true. + bar.ShowSpeed = true + + // Custom callback with colorized bar. + bar.Callback = func(s string) { + console.Print(console.Colorize("Bar", "\r"+s)) + } + + // Use different unicodes for Linux, OS X and Windows. + switch runtime.GOOS { + case "linux": + // Need to add '\x00' as delimiter for unicode characters. + bar.Format("┃\x00▓\x00█\x00░\x00┃") + case "darwin": + // Need to add '\x00' as delimiter for unicode characters. + bar.Format(" \x00▓\x00 \x00░\x00 ") + default: + // Default to non unicode characters. + bar.Format("[=> ]") + } + + // Start the progress bar. + bar.Start() + + if caption != "" { + bar.Prefix(caption) + } + + return bar.NewProxyReader(r) +} + // newProgressBar - instantiate a progress bar. func newProgressBar(total int64) *progressBar { // Progress bar speific theme customization. diff --git a/cmd/update-main.go b/cmd/update-main.go index cee39668..636d1c46 100644 --- a/cmd/update-main.go +++ b/cmd/update-main.go @@ -19,22 +19,29 @@ package cmd import ( "crypto" + "crypto/tls" "encoding/hex" + "errors" "fmt" + "io" "io/ioutil" + "net" "net/http" + "net/url" "os" + "path" "path/filepath" "runtime" "strings" "time" "github.com/fatih/color" - "github.com/inconshreveable/go-update" isatty "github.com/mattn/go-isatty" "github.com/minio/cli" json "github.com/minio/colorjson" "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/env" + "github.com/minio/selfupdate" _ "github.com/minio/sha256-simd" // Needed for sha256 hash verifier. ) @@ -74,6 +81,8 @@ const ( mcReleaseTagTimeLayout = "2006-01-02T15-04-05Z" mcOSARCH = runtime.GOOS + "-" + runtime.GOARCH mcReleaseURL = "https://dl.min.io/client/mc/release/" + mcOSARCH + "/" + + envMinisignPubKey = "MC_UPDATE_MINISIGN_PUBKEY" ) var ( @@ -320,12 +329,7 @@ func getDownloadURL(releaseTag string) (downloadURL string) { return fmt.Sprintf("docker pull minio/mc:%s", releaseTag) } - // For binary only installations, we return link to the latest binary. - if runtime.GOOS == "windows" { - return mcReleaseURL + "mc.exe" - } - - return mcReleaseURL + "mc" + return mcReleaseURL + "archive/" + "mc." + releaseTag } func getUpdateInfo(timeout time.Duration) (updateMsg string, sha256Hex string, currentReleaseTime, latestReleaseTime time.Time, err *probe.Error) { @@ -378,33 +382,93 @@ var ( }() ) +func getUpdateTransport(timeout time.Duration) http.RoundTripper { + var updateTransport http.RoundTripper = &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: timeout, + KeepAlive: timeout, + DualStack: true, + }).DialContext, + IdleConnTimeout: timeout, + TLSHandshakeTimeout: timeout, + ExpectContinueTimeout: timeout, + TLSClientConfig: &tls.Config{ + RootCAs: globalRootCAs, + }, + DisableCompression: true, + } + return updateTransport +} + +func getUpdateReaderFromURL(u *url.URL, transport http.RoundTripper) (io.ReadCloser, error) { + clnt := &http.Client{ + Transport: transport, + } + req, e := http.NewRequest(http.MethodGet, u.String(), nil) + if e != nil { + return nil, e + } + req.Header.Set("User-Agent", getUserAgent()) + + resp, e := clnt.Do(req) + if e != nil { + return nil, e + } + + if resp.StatusCode != http.StatusOK { + return nil, errors.New(resp.Status) + } + + return newProgressReader(resp.Body, "mc", resp.ContentLength), nil +} + func doUpdate(sha256Hex string, latestReleaseTime time.Time, ok bool) (updateStatusMsg string, err *probe.Error) { if !ok { updateStatusMsg = colorGreenBold("mc update to version RELEASE.%s canceled.", latestReleaseTime.Format(mcReleaseTagTimeLayout)) return updateStatusMsg, nil } - var sha256Sum []byte - var e error - sha256Sum, e = hex.DecodeString(sha256Hex) + + sha256Sum, e := hex.DecodeString(sha256Hex) if e != nil { return updateStatusMsg, probe.NewError(e) } - resp, e := http.Get(getDownloadURL(releaseTimeToReleaseTag(latestReleaseTime))) + releaseTag := releaseTimeToReleaseTag(latestReleaseTime) + + u, e := url.Parse(getDownloadURL(releaseTag)) + if err != nil { + return updateStatusMsg, probe.NewError(e) + } + + transport := getUpdateTransport(30 * time.Second) + + rc, e := getUpdateReaderFromURL(u, transport) if e != nil { return updateStatusMsg, probe.NewError(e) } - defer resp.Body.Close() + defer rc.Close() - // FIXME: add support for gpg verification as well. - if e = update.Apply(resp.Body, - update.Options{ - Hash: crypto.SHA256, - Checksum: sha256Sum, - }, - ); e != nil { - return updateStatusMsg, probe.NewError(e) + opts := selfupdate.Options{ + Hash: crypto.SHA256, + Checksum: sha256Sum, + } + + minisignPubkey := env.Get(envMinisignPubKey, "") + if minisignPubkey != "" { + v := selfupdate.NewVerifier() + u.Path = path.Dir(u.Path) + "/mc." + releaseTag + ".minisig" + if e = v.LoadFromURL(u.String(), minisignPubkey, transport); e != nil { + return updateStatusMsg, probe.NewError(e) + } + opts.Verifier = v + } + + if e = selfupdate.Apply(rc, opts); e != nil { + if re := selfupdate.RollbackError(e); re != nil { + return updateStatusMsg, probe.NewError(e) + } } return colorGreenBold("mc updated to version RELEASE.%s successfully.", diff --git a/go.mod b/go.mod index b201f051..26e7d090 100644 --- a/go.mod +++ b/go.mod @@ -7,12 +7,12 @@ require ( github.com/dustin/go-humanize v1.0.0 github.com/fatih/color v1.12.0 github.com/golang-jwt/jwt v3.2.1+incompatible - github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf github.com/json-iterator/go v1.1.11 github.com/klauspost/compress v1.12.2 github.com/kr/text v0.2.0 // indirect github.com/mattn/go-ieproxy v0.0.1 github.com/mattn/go-isatty v0.0.13 + github.com/mattn/go-runewidth v0.0.12 // indirect github.com/minio/cli v1.22.0 github.com/minio/colorjson v1.0.1 github.com/minio/filepath v1.0.0 @@ -20,6 +20,7 @@ require ( github.com/minio/md5-simd v1.1.1 // indirect github.com/minio/minio-go/v7 v7.0.11-0.20210607181445-e162fdb8e584 github.com/minio/pkg v1.0.10 + github.com/minio/selfupdate v0.3.1 github.com/minio/sha256-simd v1.0.0 github.com/mitchellh/go-homedir v1.1.0 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect @@ -28,6 +29,7 @@ require ( github.com/posener/complete v1.2.3 github.com/prometheus/client_golang v1.8.0 github.com/prometheus/procfs v0.6.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect github.com/rjeczalik/notify v0.9.2 github.com/rs/xid v1.2.1 github.com/shirou/gopsutil/v3 v3.21.6 diff --git a/go.sum b/go.sum index 945c9d07..9c82cd62 100644 --- a/go.sum +++ b/go.sum @@ -177,8 +177,6 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= -github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8= -github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= @@ -247,8 +245,9 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA= github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= -github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= @@ -272,6 +271,8 @@ github.com/minio/minio-go/v7 v7.0.11-0.20210607181445-e162fdb8e584/go.mod h1:Woy github.com/minio/pkg v1.0.3/go.mod h1:obU54TZ9QlMv0TRaDgQ/JTzf11ZSXxnSfLrm4tMtBP8= github.com/minio/pkg v1.0.10 h1:fohpAm/0ttQFf4BzmzH5r6A9JUIfg63AyGCPM0f9/9U= github.com/minio/pkg v1.0.10/go.mod h1:32x/3OmGB0EOi1N+3ggnp+B5VFkSBBB9svPMVfpnf14= +github.com/minio/selfupdate v0.3.1 h1:BWEFSNnrZVMUWXbXIgLDNDjbejkmpAmZvy/nCz1HlEs= +github.com/minio/selfupdate v0.3.1/go.mod h1:b8ThJzzH7u2MkF6PcIra7KaXO9Khf6alWPvMSyTDCFM= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g= github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= @@ -371,6 +372,9 @@ github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8= github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=