Skip to content

Commit e0fc0d0

Browse files
committed
Normalize path in url
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
1 parent 1ec8277 commit e0fc0d0

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

controllers/helmchart_controller.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,10 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
503503
ctxTimeout, cancel := context.WithTimeout(ctx, repo.Spec.Timeout.Duration)
504504
defer cancel()
505505

506-
normalizedURL := repository.NormalizeURL(repo.Spec.URL)
506+
normalizedURL, err := repository.NormalizeURL(repo.Spec.URL)
507+
if err != nil {
508+
return chartRepoConfigErrorReturn(err, obj)
509+
}
507510
// Construct the Getter options from the HelmRepository data
508511
clientOpts := []helmgetter.Option{
509512
helmgetter.WithURL(normalizedURL),
@@ -1009,7 +1012,10 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
10091012
authenticator authn.Authenticator
10101013
keychain authn.Keychain
10111014
)
1012-
normalizedURL := repository.NormalizeURL(url)
1015+
normalizedURL, err := repository.NormalizeURL(url)
1016+
if err != nil {
1017+
return nil, err
1018+
}
10131019
repo, err := r.resolveDependencyRepository(ctx, url, namespace)
10141020
if err != nil {
10151021
// Return Kubernetes client errors, but ignore others
@@ -1180,8 +1186,8 @@ func (r *HelmChartReconciler) indexHelmRepositoryByURL(o client.Object) []string
11801186
if !ok {
11811187
panic(fmt.Sprintf("Expected a HelmRepository, got %T", o))
11821188
}
1183-
u := repository.NormalizeURL(repo.Spec.URL)
1184-
if u != "" {
1189+
u, err := repository.NormalizeURL(repo.Spec.URL)
1190+
if u != "" && err != nil {
11851191
return []string{u}
11861192
}
11871193
return nil

internal/helm/chart/dependency_manager.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ func (dm *DependencyManager) resolveRepository(url string) (repo repository.Down
266266
dm.mu.Lock()
267267
defer dm.mu.Unlock()
268268

269-
nUrl := repository.NormalizeURL(url)
269+
nUrl, err := repository.NormalizeURL(url)
270+
if err != nil {
271+
return nil, err
272+
}
270273
err = repository.ValidateDepURL(nUrl)
271274
if err != nil {
272275
return

internal/helm/repository/chart_repository.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,10 @@ func (r *ChartRepository) DownloadChart(chart *repo.ChartVersion) (*bytes.Buffer
274274
err = fmt.Errorf("invalid chart repository URL format '%s': %w", r.URL, err)
275275
return nil, err
276276
}
277-
q := repoURL.Query()
278277
// Trailing slash is required for ResolveReference to work
279278
repoURL.Path = strings.TrimSuffix(repoURL.Path, "/") + "/"
280279
u = repoURL.ResolveReference(u)
281-
u.RawQuery = q.Encode()
280+
u.RawQuery = repoURL.RawQuery
282281
}
283282

284283
t := transport.NewOrIdle(r.tlsConfig)

internal/helm/repository/utils.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package repository
1818

1919
import (
2020
"fmt"
21+
"net/url"
2122
"strings"
2223

2324
helmreg "helm.sh/helm/v3/pkg/registry"
@@ -35,17 +36,23 @@ var (
3536
)
3637

3738
// NormalizeURL normalizes a ChartRepository URL by its scheme.
38-
func NormalizeURL(repositoryURL string) string {
39+
func NormalizeURL(repositoryURL string) (string, error) {
3940
if repositoryURL == "" {
40-
return ""
41+
return "", nil
4142
}
4243

43-
if strings.Contains(repositoryURL, helmreg.OCIScheme) {
44-
return strings.TrimRight(repositoryURL, "/")
44+
u, err := url.Parse(repositoryURL)
45+
if err != nil {
46+
return "", err
4547
}
4648

47-
return strings.TrimRight(repositoryURL, "/") + "/"
49+
if strings.Contains(repositoryURL, helmreg.OCIScheme) {
50+
u.Path = strings.TrimRight(u.Path, "/")
51+
return u.String(), nil
52+
}
4853

54+
u.Path = strings.TrimRight(u.Path, "/") + "/"
55+
return u.String(), nil
4956
}
5057

5158
// ValidateDepURL returns an error if the given depended repository URL declaration is not supported

internal/helm/repository/utils_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,23 @@ func TestNormalizeURL(t *testing.T) {
5858
url: "oci://example.com//",
5959
want: "oci://example.com",
6060
},
61+
{
62+
name: "url with query",
63+
url: "http://example.com?st=pr",
64+
want: "http://example.com/?st=pr",
65+
},
66+
{
67+
name: "url with slash and query",
68+
url: "http://example.com/?st=pr",
69+
want: "http://example.com/?st=pr",
70+
},
6171
}
6272
for _, tt := range tests {
6373
t.Run(tt.name, func(t *testing.T) {
6474
g := NewWithT(t)
6575

66-
got := NormalizeURL(tt.url)
76+
got, err := NormalizeURL(tt.url)
77+
g.Expect(err).To(Not(HaveOccurred()))
6778
g.Expect(got).To(Equal(tt.want))
6879
})
6980
}

0 commit comments

Comments
 (0)