From dfcede03f2983d273922b0200ffe82bf66c5efa5 Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Wed, 22 Nov 2023 10:03:07 +0530 Subject: [PATCH] helmrepo: only configure tls login option when required Modify `GetHelmClientOpts()` to only configure the TLS login option when an authentication login option is configured. This prevents the reconciler from trying to authenticate against public registries. Signed-off-by: Sanskar Jaiswal --- .../controller/helmchart_controller_test.go | 41 ++++++++++++------- internal/helm/getter/client_opts.go | 8 ++-- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/internal/controller/helmchart_controller_test.go b/internal/controller/helmchart_controller_test.go index ee9a3775..796bfdcd 100644 --- a/internal/controller/helmchart_controller_test.go +++ b/internal/controller/helmchart_controller_test.go @@ -2376,23 +2376,32 @@ func TestHelmChartReconciler_reconcileSourceFromOCI_authStrategy(t *testing.T) { }, }, { - name: "HTTPS With CA cert", + name: "HTTPS With CA cert only", + want: sreconcile.ResultSuccess, + registryOpts: registryOptions{ + withTLS: true, + }, + certSecret: &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: "certs-secretref", + }, + Type: corev1.SecretTypeOpaque, + Data: map[string][]byte{ + "ca.crt": tlsCA, + }, + }, + assertConditions: []metav1.Condition{ + *conditions.TrueCondition(meta.ReconcilingCondition, meta.ProgressingReason, "building artifact: pulled 'helmchart' chart with version '0.1.0'"), + *conditions.UnknownCondition(meta.ReadyCondition, meta.ProgressingReason, "building artifact: pulled 'helmchart' chart with version '0.1.0'"), + }, + }, + { + name: "HTTPS With CA cert and client cert auth", want: sreconcile.ResultSuccess, registryOpts: registryOptions{ withTLS: true, withClientCertAuth: true, }, - secretOpts: secretOptions{ - username: testRegistryUsername, - password: testRegistryPassword, - }, - secret: &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: "auth-secretref", - }, - Type: corev1.SecretTypeDockerConfigJson, - Data: map[string][]byte{}, - }, certSecret: &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: "certs-secretref", @@ -2526,8 +2535,12 @@ func TestHelmChartReconciler_reconcileSourceFromOCI_authStrategy(t *testing.T) { sp := patch.NewSerialPatcher(obj, r.Client) got, err := r.reconcileSource(ctx, sp, obj, &b) - g.Expect(err != nil).To(Equal(tt.wantErr)) - g.Expect(got).To(Equal(tt.want)) + if tt.wantErr { + g.Expect(err).To(HaveOccurred()) + } else { + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(got).To(Equal(tt.want)) + } g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions)) }) } diff --git a/internal/helm/getter/client_opts.go b/internal/helm/getter/client_opts.go index f746684b..ce7620ca 100644 --- a/internal/helm/getter/client_opts.go +++ b/internal/helm/getter/client_opts.go @@ -162,10 +162,10 @@ func GetClientOpts(ctx context.Context, c client.Client, obj *helmv1.HelmReposit } if loginOpt != nil { hrOpts.RegLoginOpts = []helmreg.LoginOption{loginOpt} - } - tlsLoginOpt := registry.TLSLoginOption(certFile, keyFile, caFile) - if tlsLoginOpt != nil { - hrOpts.RegLoginOpts = append(hrOpts.RegLoginOpts, tlsLoginOpt) + tlsLoginOpt := registry.TLSLoginOption(certFile, keyFile, caFile) + if tlsLoginOpt != nil { + hrOpts.RegLoginOpts = append(hrOpts.RegLoginOpts, tlsLoginOpt) + } } } if deprecatedTLSConfig {