Merge pull request #6602 from karuppiah7890/fix-url-decode-v3

fix repo url being decoded while downloading repo index
pull/6691/head
Matthew Fisher 5 years ago committed by GitHub
commit b38c413d1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,6 +23,7 @@ import (
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -109,14 +110,14 @@ func (r *ChartRepository) Load() error {
// DownloadIndexFile fetches the index from a repository. // DownloadIndexFile fetches the index from a repository.
func (r *ChartRepository) DownloadIndexFile() (string, error) { func (r *ChartRepository) DownloadIndexFile() (string, error) {
var indexURL string
parsedURL, err := url.Parse(r.Config.URL) parsedURL, err := url.Parse(r.Config.URL)
if err != nil { if err != nil {
return "", err return "", err
} }
parsedURL.Path = strings.TrimSuffix(parsedURL.Path, "/") + "/index.yaml" parsedURL.RawPath = path.Join(parsedURL.RawPath, "index.yaml")
parsedURL.Path = path.Join(parsedURL.Path, "index.yaml")
indexURL = parsedURL.String() indexURL := parsedURL.String()
// TODO add user-agent // TODO add user-agent
resp, err := r.Client.Get(indexURL, resp, err := r.Client.Get(indexURL,
getter.WithURL(r.Config.URL), getter.WithURL(r.Config.URL),

@ -18,12 +18,14 @@ package repo
import ( import (
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"testing" "testing"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/chart"
) )
const ( const (
@ -128,39 +130,87 @@ func TestMerge(t *testing.T) {
} }
func TestDownloadIndexFile(t *testing.T) { func TestDownloadIndexFile(t *testing.T) {
srv, err := startLocalServerForTests(nil) t.Run("should download index file", func(t *testing.T) {
if err != nil { srv, err := startLocalServerForTests(nil)
t.Fatal(err) if err != nil {
} t.Fatal(err)
defer srv.Close() }
defer srv.Close()
r, err := NewChartRepository(&Entry{
Name: testRepo,
URL: srv.URL,
}, getter.All(&cli.EnvSettings{}))
if err != nil {
t.Errorf("Problem creating chart repository from %s: %v", testRepo, err)
}
r, err := NewChartRepository(&Entry{ idx, err := r.DownloadIndexFile()
Name: testRepo, if err != nil {
URL: srv.URL, t.Fatalf("Failed to download index file to %s: %#v", idx, err)
}, getter.All(&cli.EnvSettings{})) }
if err != nil {
t.Errorf("Problem creating chart repository from %s: %v", testRepo, err)
}
idx, err := r.DownloadIndexFile() if _, err := os.Stat(idx); err != nil {
if err != nil { t.Fatalf("error finding created index file: %#v", err)
t.Fatalf("Failed to download index file to %s: %#v", idx, err) }
}
if _, err := os.Stat(idx); err != nil { b, err := ioutil.ReadFile(idx)
t.Fatalf("error finding created index file: %#v", err) if err != nil {
} t.Fatalf("error reading index file: %#v", err)
}
b, err := ioutil.ReadFile(idx) i, err := loadIndex(b)
if err != nil { if err != nil {
t.Fatalf("error reading index file: %#v", err) t.Fatalf("Index %q failed to parse: %s", testfile, err)
} }
verifyLocalIndex(t, i)
})
t.Run("should not decode the path in the repo url while downloading index", func(t *testing.T) {
chartRepoURLPath := "/some%2Fpath/test"
fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml")
if err != nil {
t.Fatal(err)
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.RawPath == chartRepoURLPath+"/index.yaml" {
w.Write(fileBytes)
}
})
srv, err := startLocalServerForTests(handler)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
r, err := NewChartRepository(&Entry{
Name: testRepo,
URL: srv.URL + chartRepoURLPath,
}, getter.All(&cli.EnvSettings{}))
if err != nil {
t.Errorf("Problem creating chart repository from %s: %v", testRepo, err)
}
i, err := loadIndex(b) idx, err := r.DownloadIndexFile()
if err != nil { if err != nil {
t.Fatalf("Index %q failed to parse: %s", testfile, err) t.Fatalf("Failed to download index file to %s: %#v", idx, err)
} }
verifyLocalIndex(t, i)
if _, err := os.Stat(idx); err != nil {
t.Fatalf("error finding created index file: %#v", err)
}
b, err := ioutil.ReadFile(idx)
if err != nil {
t.Fatalf("error reading index file: %#v", err)
}
i, err := loadIndex(b)
if err != nil {
t.Fatalf("Index %q failed to parse: %s", testfile, err)
}
verifyLocalIndex(t, i)
})
} }
func verifyLocalIndex(t *testing.T, i *IndexFile) { func verifyLocalIndex(t *testing.T, i *IndexFile) {

Loading…
Cancel
Save