Skip to content

Commit dfb25e1

Browse files
committedNov 9, 2022
harmonize URL reference resolving
Fixes #11472 Signed-off-by: Philipp Stehle <philipp.stehle@sap.com>
1 parent 9a5eb70 commit dfb25e1

File tree

3 files changed

+35
-63
lines changed

3 files changed

+35
-63
lines changed
 

‎pkg/downloader/chart_downloader.go

+3-22
Original file line numberDiff line numberDiff line change
@@ -294,32 +294,13 @@ func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, er
294294
}
295295

296296
// TODO: Seems that picking first URL is not fully correct
297-
u, err = url.Parse(cv.URLs[0])
297+
resolvedURL, err := repo.ResolveReferenceURL(rc.URL, cv.URLs[0])
298+
298299
if err != nil {
299300
return u, errors.Errorf("invalid chart URL format: %s", ref)
300301
}
301302

302-
// If the URL is relative (no scheme), prepend the chart repo's base URL
303-
if !u.IsAbs() {
304-
repoURL, err := url.Parse(rc.URL)
305-
if err != nil {
306-
return repoURL, err
307-
}
308-
q := repoURL.Query()
309-
// We need a trailing slash for ResolveReference to work, but make sure there isn't already one
310-
repoURL.RawPath = strings.TrimSuffix(repoURL.RawPath, "/") + "/"
311-
repoURL.Path = strings.TrimSuffix(repoURL.Path, "/") + "/"
312-
u = repoURL.ResolveReference(u)
313-
u.RawQuery = q.Encode()
314-
// TODO add user-agent
315-
if _, err := getter.NewHTTPGetter(getter.WithURL(rc.URL)); err != nil {
316-
return repoURL, err
317-
}
318-
return u, err
319-
}
320-
321-
// TODO add user-agent
322-
return u, nil
303+
return url.Parse(resolvedURL)
323304
}
324305

325306
// VerifyChart takes a path to a chart archive and a keyring, and verifies the chart.

‎pkg/repo/chartrepo.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"log"
2626
"net/url"
2727
"os"
28-
"path"
2928
"path/filepath"
3029
"strings"
3130

@@ -116,14 +115,11 @@ func (r *ChartRepository) Load() error {
116115

117116
// DownloadIndexFile fetches the index from a repository.
118117
func (r *ChartRepository) DownloadIndexFile() (string, error) {
119-
parsedURL, err := url.Parse(r.Config.URL)
118+
indexURL, err := ResolveReferenceURL(r.Config.URL, "index.yaml")
120119
if err != nil {
121120
return "", err
122121
}
123-
parsedURL.RawPath = path.Join(parsedURL.RawPath, "index.yaml")
124-
parsedURL.Path = path.Join(parsedURL.Path, "index.yaml")
125122

126-
indexURL := parsedURL.String()
127123
// TODO add user-agent
128124
resp, err := r.Client.Get(indexURL,
129125
getter.WithURL(r.Config.URL),
@@ -290,18 +286,27 @@ func FindChartInAuthAndTLSAndPassRepoURL(repoURL, username, password, chartName,
290286
// ResolveReferenceURL resolves refURL relative to baseURL.
291287
// If refURL is absolute, it simply returns refURL.
292288
func ResolveReferenceURL(baseURL, refURL string) (string, error) {
293-
// We need a trailing slash for ResolveReference to work, but make sure there isn't already one
294-
parsedBaseURL, err := url.Parse(strings.TrimSuffix(baseURL, "/") + "/")
289+
parsedRefURL, err := url.Parse(refURL)
295290
if err != nil {
296-
return "", errors.Wrapf(err, "failed to parse %s as URL", baseURL)
291+
return "", errors.Wrapf(err, "failed to parse %s as URL", refURL)
297292
}
298293

299-
parsedRefURL, err := url.Parse(refURL)
294+
if parsedRefURL.IsAbs() {
295+
return refURL, nil
296+
}
297+
298+
parsedBaseURL, err := url.Parse(baseURL)
300299
if err != nil {
301-
return "", errors.Wrapf(err, "failed to parse %s as URL", refURL)
300+
return "", errors.Wrapf(err, "failed to parse %s as URL", baseURL)
302301
}
303302

304-
return parsedBaseURL.ResolveReference(parsedRefURL).String(), nil
303+
// We need a trailing slash for ResolveReference to work, but make sure there isn't already one
304+
parsedBaseURL.RawPath = strings.TrimSuffix(parsedBaseURL.RawPath, "/") + "/"
305+
parsedBaseURL.Path = strings.TrimSuffix(parsedBaseURL.Path, "/") + "/"
306+
307+
resolvedURL := parsedBaseURL.ResolveReference(parsedRefURL)
308+
resolvedURL.RawQuery = parsedBaseURL.RawQuery
309+
return resolvedURL.String(), nil
305310
}
306311

307312
func (e *Entry) String() string {

‎pkg/repo/chartrepo_test.go

+16-30
Original file line numberDiff line numberDiff line change
@@ -385,35 +385,21 @@ func TestErrorFindChartInRepoURL(t *testing.T) {
385385
}
386386

387387
func TestResolveReferenceURL(t *testing.T) {
388-
chartURL, err := ResolveReferenceURL("http://localhost:8123/charts/", "nginx-0.2.0.tgz")
389-
if err != nil {
390-
t.Errorf("%s", err)
391-
}
392-
if chartURL != "http://localhost:8123/charts/nginx-0.2.0.tgz" {
393-
t.Errorf("%s", chartURL)
394-
}
395-
396-
chartURL, err = ResolveReferenceURL("http://localhost:8123/charts-with-no-trailing-slash", "nginx-0.2.0.tgz")
397-
if err != nil {
398-
t.Errorf("%s", err)
399-
}
400-
if chartURL != "http://localhost:8123/charts-with-no-trailing-slash/nginx-0.2.0.tgz" {
401-
t.Errorf("%s", chartURL)
402-
}
403-
404-
chartURL, err = ResolveReferenceURL("http://localhost:8123", "https://charts.helm.sh/stable/nginx-0.2.0.tgz")
405-
if err != nil {
406-
t.Errorf("%s", err)
407-
}
408-
if chartURL != "https://charts.helm.sh/stable/nginx-0.2.0.tgz" {
409-
t.Errorf("%s", chartURL)
410-
}
411-
412-
chartURL, err = ResolveReferenceURL("http://localhost:8123/charts%2fwith%2fescaped%2fslash", "nginx-0.2.0.tgz")
413-
if err != nil {
414-
t.Errorf("%s", err)
415-
}
416-
if chartURL != "http://localhost:8123/charts%2fwith%2fescaped%2fslash/nginx-0.2.0.tgz" {
417-
t.Errorf("%s", chartURL)
388+
for _, tt := range []struct {
389+
baseURL, refURL, chartURL string
390+
}{
391+
{"http://localhost:8123/charts/", "nginx-0.2.0.tgz", "http://localhost:8123/charts/nginx-0.2.0.tgz"},
392+
{"http://localhost:8123/charts-with-no-trailing-slash", "nginx-0.2.0.tgz", "http://localhost:8123/charts-with-no-trailing-slash/nginx-0.2.0.tgz"},
393+
{"http://localhost:8123", "https://charts.helm.sh/stable/nginx-0.2.0.tgz", "https://charts.helm.sh/stable/nginx-0.2.0.tgz"},
394+
{"http://localhost:8123/charts%2fwith%2fescaped%2fslash", "nginx-0.2.0.tgz", "http://localhost:8123/charts%2fwith%2fescaped%2fslash/nginx-0.2.0.tgz"},
395+
{"http://localhost:8123/charts?with=queryparameter", "nginx-0.2.0.tgz", "http://localhost:8123/charts/nginx-0.2.0.tgz?with=queryparameter"},
396+
} {
397+
chartURL, err := ResolveReferenceURL(tt.baseURL, tt.refURL)
398+
if err != nil {
399+
t.Errorf("unexpected error in ResolveReferenceURL(%q, %q): %s", tt.baseURL, tt.refURL, err)
400+
}
401+
if chartURL != tt.chartURL {
402+
t.Errorf("expected ResolveReferenceURL(%q, %q) to equal %q, got %q", tt.baseURL, tt.refURL, tt.chartURL, chartURL)
403+
}
418404
}
419405
}

0 commit comments

Comments
 (0)
Please sign in to comment.