From cf7f27ce5689c3d73656efa877572184451b47d3 Mon Sep 17 00:00:00 2001 From: Evans Mungai Date: Thu, 20 Mar 2025 19:04:49 +0000 Subject: [PATCH] fix: prevent panic when ChartDownloader is called Return an error instead of panicing if OCI registry client is not provided Signed-off-by: Evans Mungai --- pkg/downloader/chart_downloader.go | 4 ++++ pkg/downloader/chart_downloader_test.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/downloader/chart_downloader.go b/pkg/downloader/chart_downloader.go index f5d1deac9..35405dfbb 100644 --- a/pkg/downloader/chart_downloader.go +++ b/pkg/downloader/chart_downloader.go @@ -164,6 +164,10 @@ func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, er } if registry.IsOCI(u.String()) { + if c.RegistryClient == nil { + return nil, fmt.Errorf("unable to resolve chart version for %s, missing registry client", ref) + } + return c.RegistryClient.ValidateReference(ref, version, u) } diff --git a/pkg/downloader/chart_downloader_test.go b/pkg/downloader/chart_downloader_test.go index 26dcc58ff..0762650cf 100644 --- a/pkg/downloader/chart_downloader_test.go +++ b/pkg/downloader/chart_downloader_test.go @@ -20,6 +20,8 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/assert" + "helm.sh/helm/v4/internal/test/ensure" "helm.sh/helm/v4/pkg/cli" "helm.sh/helm/v4/pkg/getter" @@ -324,6 +326,22 @@ func TestDownloadTo_VerifyLater(t *testing.T) { } } +func TestDownloadTo_MissingRegistryClient(t *testing.T) { + c := ChartDownloader{ + Getters: getter.Providers{ + getter.Provider{ + Schemes: []string{"oci"}, + New: getter.NewOCIGetter, + }, + }, + } + + ref := "oci://someurl" + version := "latest" + _, _, err := c.DownloadTo(ref, version, t.TempDir()) + assert.Error(t, err) +} + func TestScanReposForURL(t *testing.T) { c := ChartDownloader{ Out: os.Stderr,