Skip to content

Commit a567968

Browse files
authored
Merge pull request #2063 from ogulcanaydogan/fix/654-descriptive-helm-repo-error
Add the repository URL to the Helm index parse failure message
2 parents d594142 + c516e47 commit a567968

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎internal/controller/helmrepository_controller.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ func (r *HelmRepositoryReconciler) reconcileSource(ctx context.Context, sp *patc
488488
// Load the cached repository index to ensure it passes validation.
489489
if err := chartRepo.LoadFromPath(); err != nil {
490490
e := serror.NewGeneric(
491-
fmt.Errorf("failed to load Helm repository from index YAML: %w", err),
491+
fmt.Errorf("failed to load Helm repository from index YAML at %s: %w", redactURL(obj.Spec.URL), err),
492492
sourcev1.IndexationFailedReason,
493493
)
494494
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, "%s", e)
@@ -732,3 +732,13 @@ func (r *HelmRepositoryReconciler) migrationToStatic(ctx context.Context, sp *pa
732732

733733
return ctrl.Result{}, nil
734734
}
735+
736+
// redactURL returns the given URL with the password of its user info
737+
// replaced, so it can be added to conditions and events.
738+
func redactURL(repoURL string) string {
739+
u, err := url.Parse(repoURL)
740+
if err != nil {
741+
return "<invalid URL>"
742+
}
743+
return u.Redacted()
744+
}

‎internal/controller/helmrepository_controller_test.go‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,12 +1157,39 @@ func TestHelmRepositoryReconciler_reconcileSource_cachedIndexFile(t *testing.T)
11571157
got, err := newReconciler(g, c).reconcileSource(context.TODO(), patch.NewSerialPatcher(obj, c), obj, &meta.Artifact{}, &chartRepo)
11581158

11591159
g.Expect(err).To(HaveOccurred())
1160-
g.Expect(err.Error()).To(ContainSubstring("failed to load Helm repository from index YAML"))
1160+
g.Expect(err.Error()).To(ContainSubstring("failed to load Helm repository from index YAML at " + server.URL() + ":"))
11611161
g.Expect(got).To(Equal(sreconcile.ResultEmpty))
11621162
g.Expect(conditions.IsTrue(obj, sourcev1.FetchFailedCondition)).To(BeTrue())
1163+
g.Expect(conditions.GetMessage(obj, sourcev1.FetchFailedCondition)).To(ContainSubstring("failed to load Helm repository from index YAML at " + server.URL() + ":"))
11631164
g.Expect(chartIndexFiles(g, dir)).To(BeEmpty())
11641165
})
11651166

1167+
t.Run("invalid index message redacts credentials in the URL", func(t *testing.T) {
1168+
g := NewWithT(t)
1169+
dir := t.TempDir()
1170+
t.Setenv("TMPDIR", dir)
1171+
1172+
server := startServer(g, []byte("{}\n"))
1173+
defer server.Stop()
1174+
defer os.RemoveAll(server.Root())
1175+
1176+
c := fakeclient.NewClientBuilder().
1177+
WithScheme(testEnv.GetScheme()).
1178+
WithStatusSubresource(&sourcev1.HelmRepository{}).
1179+
Build()
1180+
obj := newObject(strings.Replace(server.URL(), "http://", "http://user:s3cr3t@", 1))
1181+
g.Expect(c.Create(context.TODO(), obj)).To(Succeed())
1182+
1183+
var chartRepo repository.ChartRepository
1184+
_, err := newReconciler(g, c).reconcileSource(context.TODO(), patch.NewSerialPatcher(obj, c), obj, &meta.Artifact{}, &chartRepo)
1185+
1186+
g.Expect(err).To(HaveOccurred())
1187+
g.Expect(err.Error()).To(ContainSubstring("failed to load Helm repository from index YAML at"))
1188+
g.Expect(err.Error()).To(ContainSubstring("user:xxxxx@"))
1189+
g.Expect(err.Error()).ToNot(ContainSubstring("s3cr3t"))
1190+
g.Expect(conditions.GetMessage(obj, sourcev1.FetchFailedCondition)).ToNot(ContainSubstring("s3cr3t"))
1191+
})
1192+
11661193
t.Run("status patch failure removes cached file", func(t *testing.T) {
11671194
g := NewWithT(t)
11681195
dir := t.TempDir()

0 commit comments

Comments
 (0)