diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index 17aef5d3..27807e6e 100644 --- a/pkg/chart/chart.go +++ b/pkg/chart/chart.go @@ -350,17 +350,21 @@ func (t *Testing) processCharts(action func(chart *Chart) TestResult) ([]TestRes repoArgs := map[string][]string{} - for _, repo := range t.config.HelmRepoExtraArgs { - repoSlice := strings.SplitN(repo, "=", 2) - name := repoSlice[0] - repoExtraArgs := strings.Fields(repoSlice[1]) - repoArgs[name] = repoExtraArgs + // The values can hold credentials, so the errors below name the entry by + // position instead of printing it. + for i, repo := range t.config.HelmRepoExtraArgs { + name, extraArgs, found := strings.Cut(repo, "=") + if !found { + return nil, fmt.Errorf("invalid --helm-repo-extra-args entry %d: expected format 'name=args'", i+1) + } + repoArgs[name] = strings.Fields(extraArgs) } - for _, repo := range t.config.ChartRepos { - repoSlice := strings.SplitN(repo, "=", 2) - name := repoSlice[0] - url := repoSlice[1] + for i, repo := range t.config.ChartRepos { + name, url, found := strings.Cut(repo, "=") + if !found { + return nil, fmt.Errorf("invalid --chart-repos entry %d: expected format 'name=url'", i+1) + } repoExtraArgs := repoArgs[name] if err := t.helm.AddRepo(name, url, repoExtraArgs); err != nil { diff --git a/pkg/chart/chart_test.go b/pkg/chart/chart_test.go index f849cbd7..6b618609 100644 --- a/pkg/chart/chart_test.go +++ b/pkg/chart/chart_test.go @@ -431,6 +431,56 @@ func TestLintDependencyExtraArgs(t *testing.T) { }) } +func TestLintChartsInvalidRepoConfig(t *testing.T) { + testCases := []struct { + name string + chartRepos []string + helmRepoExtraArgs []string + expectedError string + secret string + }{ + { + name: "chart repo without name", + chartRepos: []string{"https://charts.bitnami.com/bitnami"}, + expectedError: "invalid --chart-repos entry 1: expected format 'name=url'", + }, + { + name: "chart repo with credentials in the url and no name", + chartRepos: []string{"stable=https://charts.helm.sh/stable", "https://demo:s3cr3t@charts.example.com"}, + expectedError: "invalid --chart-repos entry 2: expected format 'name=url'", + secret: "s3cr3t", + }, + { + name: "helm repo extra args without repo name", + helmRepoExtraArgs: []string{"--force-update"}, + expectedError: "invalid --helm-repo-extra-args entry 1: expected format 'name=args'", + }, + { + name: "helm repo extra args with a password and no repo name", + helmRepoExtraArgs: []string{"--username demo --password s3cr3t"}, + expectedError: "invalid --helm-repo-extra-args entry 1: expected format 'name=args'", + secret: "s3cr3t", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + cfg := config.Configuration{ + Charts: []string{"testdata/test_lints"}, + ChartRepos: tc.chartRepos, + HelmRepoExtraArgs: tc.helmRepoExtraArgs, + } + ct := newTestingMock(cfg) + + _, err := ct.LintCharts() + assert.EqualError(t, err, tc.expectedError) + if tc.secret != "" { + assert.NotContains(t, err.Error(), tc.secret) + } + }) + } +} + func TestGenerateInstallConfig(t *testing.T) { type testData struct { name string