From 6ad2cd03c2171cfecc29ca2e40a87ce56d769306 Mon Sep 17 00:00:00 2001 From: Pawel Kosiec Date: Sat, 17 Feb 2024 00:07:16 +0100 Subject: [PATCH] Use forked go-getter to fix the problem with additional query params --- go.mod | 4 +++- go.sum | 4 ++-- pkg/plugin/downloader.go | 21 +++++++++++++++------ pkg/plugin/manager.go | 2 +- test/go.mod | 2 +- test/go.sum | 4 ++-- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index d54bbe86b..2fe0e6207 100644 --- a/go.mod +++ b/go.mod @@ -34,7 +34,7 @@ require ( github.com/gookit/color v1.5.2 github.com/gorilla/mux v1.8.0 github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 - github.com/hashicorp/go-getter v1.7.1 + github.com/hashicorp/go-getter v1.7.3 github.com/hashicorp/go-hclog v1.5.0 github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-plugin v1.4.10 @@ -281,3 +281,5 @@ require ( sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect ) + +replace github.com/hashicorp/go-getter v1.7.3 => github.com/kubeshop/go-getter v0.0.0-20240216153945-6fd8292c88c9 diff --git a/go.sum b/go.sum index f9405f44e..95250b6c2 100644 --- a/go.sum +++ b/go.sum @@ -695,8 +695,6 @@ github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtng github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= -github.com/hashicorp/go-getter v1.7.1 h1:SWiSWN/42qdpR0MdhaOc/bLR48PLuP1ZQtYLRlM69uY= -github.com/hashicorp/go-getter v1.7.1/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI= github.com/hashicorp/go-hclog v0.8.0/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= github.com/hashicorp/go-hclog v0.12.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= @@ -820,6 +818,8 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kubeshop/go-getter v0.0.0-20240216153945-6fd8292c88c9 h1:u03HGchFR86+3ndmFMDMbJTSpPhhp//X6qJ4bPq6Fw8= +github.com/kubeshop/go-getter v0.0.0-20240216153945-6fd8292c88c9/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw= github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o= github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk= diff --git a/pkg/plugin/downloader.go b/pkg/plugin/downloader.go index be998820c..66faa3979 100644 --- a/pkg/plugin/downloader.go +++ b/pkg/plugin/downloader.go @@ -3,6 +3,7 @@ package plugin import ( "context" "fmt" + "net/url" "os" "path/filepath" "strings" @@ -16,7 +17,7 @@ var allowedExt = map[string]struct{}{ } // downloadBinary downloads binary into specific destination. -func downloadBinary(ctx context.Context, destPath string, url URL, autoDetectFilename bool) error { +func downloadBinary(ctx context.Context, destPath string, binaryURL URL, autoDetectFilename bool) error { dir, filename := filepath.Split(destPath) err := os.MkdirAll(dir, dirPerms) if err != nil { @@ -35,10 +36,18 @@ func downloadBinary(ctx context.Context, destPath string, url URL, autoDetectFil } } - urlWithGoGetterMagicParams := fmt.Sprintf("%s?filename=%s", url.URL, filename) - if url.Checksum != "" { - urlWithGoGetterMagicParams = fmt.Sprintf("%s&checksum=%s", urlWithGoGetterMagicParams, url.Checksum) + parsedURL, err := url.Parse(binaryURL.URL) + if err != nil { + return fmt.Errorf("while parsing URL %q: %w", binaryURL.URL, err) + } + // Add go-getter magic params + queryParams := parsedURL.Query() + queryParams.Set("filename", filename) + if binaryURL.Checksum != "" { + queryParams.Set("checksum", binaryURL.Checksum) } + parsedURL.RawQuery = queryParams.Encode() + urlWithGoGetterMagicParams := parsedURL.String() getterCli := &getter.Client{ Ctx: ctx, @@ -50,11 +59,11 @@ func downloadBinary(ctx context.Context, destPath string, url URL, autoDetectFil err = getterCli.Get() if err != nil { - return fmt.Errorf("while downloading binary from URL %q: %w", url, err) + return fmt.Errorf("while downloading binary with go-getter via url %s: %w", urlWithGoGetterMagicParams, err) } if stat, err := os.Stat(tmpDestPath); err == nil && stat.IsDir() { - if autoDetectFilename && hasArchiveExtension(url.URL) { + if autoDetectFilename && hasArchiveExtension(parsedURL.Path) { filename, err = getFirstFileInDirectory(tmpDestPath) if err != nil { return fmt.Errorf("while getting binary name") diff --git a/pkg/plugin/manager.go b/pkg/plugin/manager.go index 8c4612a6c..1e4a54953 100644 --- a/pkg/plugin/manager.go +++ b/pkg/plugin/manager.go @@ -545,7 +545,7 @@ func (m *Manager) ensurePluginDownloaded(ctx context.Context, binPath string, in err = downloadBinary(ctx, binPath, url, true) if err != nil { - return fmt.Errorf("while downloading dependency from URL %q: %w", url, err) + return fmt.Errorf("while downloading dependency from URL %q (checksum: %q): %w", url.URL, url.Checksum, err) } } diff --git a/test/go.mod b/test/go.mod index 8dd72124b..47532ef85 100644 --- a/test/go.mod +++ b/test/go.mod @@ -104,7 +104,7 @@ require ( github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect - github.com/hashicorp/go-getter v1.7.1 // indirect + github.com/hashicorp/go-getter v1.7.3 // indirect github.com/hashicorp/go-hclog v1.5.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-plugin v1.4.10 // indirect diff --git a/test/go.sum b/test/go.sum index 7a765a336..072ce8ec6 100644 --- a/test/go.sum +++ b/test/go.sum @@ -708,8 +708,8 @@ github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtng github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= -github.com/hashicorp/go-getter v1.7.1 h1:SWiSWN/42qdpR0MdhaOc/bLR48PLuP1ZQtYLRlM69uY= -github.com/hashicorp/go-getter v1.7.1/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= +github.com/hashicorp/go-getter v1.7.3 h1:bN2+Fw9XPFvOCjB0UOevFIMICZ7G2XSQHzfvLUyOM5E= +github.com/hashicorp/go-getter v1.7.3/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI= github.com/hashicorp/go-hclog v0.8.0/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= github.com/hashicorp/go-hclog v0.12.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=