@@ -21,8 +21,6 @@ import (
2121 "crypto/tls"
2222 "errors"
2323 "fmt"
24- "os"
25- "path"
2624
2725 "github.com/google/go-containerregistry/pkg/authn"
2826 helmgetter "helm.sh/helm/v3/pkg/getter"
@@ -69,7 +67,7 @@ func (o ClientOpts) MustLoginToRegistry() bool {
6967// auth mechanisms.
7068// A temporary directory is created to store the certs files if needed and its path is returned along with the options object. It is the
7169// caller's responsibility to clean up the directory.
72- func GetClientOpts (ctx context.Context , c client.Client , obj * sourcev1.HelmRepository , url string ) (* ClientOpts , string , error ) {
70+ func GetClientOpts (ctx context.Context , c client.Client , obj * sourcev1.HelmRepository , url string ) (* ClientOpts , error ) {
7371 // This function configures authentication for Helm repositories based on the provided secrets:
7472 // - CertSecretRef: TLS client certificates (always takes priority)
7573 // - SecretRef: Can contain Basic Auth or TLS certificates (deprecated)
@@ -84,17 +82,16 @@ func GetClientOpts(ctx context.Context, c client.Client, obj *sourcev1.HelmRepos
8482 }
8583
8684 // Process secrets and configure authentication
87- deprecatedTLS , certSecret , authSecret , err := configureAuthentication (ctx , c , obj , opts , url )
85+ deprecatedTLS , _ , authSecret , err := configureAuthentication (ctx , c , obj , opts )
8886 if err != nil {
89- return nil , "" , err
87+ return nil , err
9088 }
9189
9290 // Setup OCI registry specific configurations if needed
93- var tempCertDir string
9491 if obj .Spec .Type == sourcev1 .HelmRepositoryTypeOCI {
95- tempCertDir , err = configureOCIRegistryWithSecrets (ctx , obj , opts , url , certSecret , authSecret )
92+ err = configureOCIRegistryWithSecrets (ctx , obj , opts , url , authSecret )
9693 if err != nil {
97- return nil , "" , err
94+ return nil , err
9895 }
9996 }
10097
@@ -103,15 +100,15 @@ func GetClientOpts(ctx context.Context, c client.Client, obj *sourcev1.HelmRepos
103100 deprecatedErr = ErrDeprecatedTLSConfig
104101 }
105102
106- return opts , tempCertDir , deprecatedErr
103+ return opts , deprecatedErr
107104}
108105
109106// configureAuthentication processes all secret references and sets up authentication.
110107// Returns (deprecatedTLS, certSecret, authSecret, error) where:
111108// - deprecatedTLS: true if TLS config comes from SecretRef (deprecated pattern)
112109// - certSecret: the secret from CertSecretRef (nil if not specified)
113110// - authSecret: the secret from SecretRef (nil if not specified)
114- func configureAuthentication (ctx context.Context , c client.Client , obj * sourcev1.HelmRepository , opts * ClientOpts , url string ) (bool , * corev1.Secret , * corev1.Secret , error ) {
111+ func configureAuthentication (ctx context.Context , c client.Client , obj * sourcev1.HelmRepository , opts * ClientOpts ) (bool , * corev1.Secret , * corev1.Secret , error ) {
115112 var deprecatedTLS bool
116113 var certSecret , authSecret * corev1.Secret
117114
@@ -171,12 +168,12 @@ func configureAuthentication(ctx context.Context, c client.Client, obj *sourcev1
171168}
172169
173170// configureOCIRegistryWithSecrets sets up OCI-specific configurations using pre-fetched secrets
174- func configureOCIRegistryWithSecrets (ctx context.Context , obj * sourcev1.HelmRepository , opts * ClientOpts , url string , certSecret , authSecret * corev1.Secret ) ( string , error ) {
171+ func configureOCIRegistryWithSecrets (ctx context.Context , obj * sourcev1.HelmRepository , opts * ClientOpts , url string , authSecret * corev1.Secret ) error {
175172 // Configure OCI authentication from authSecret if available
176173 if authSecret != nil {
177174 keychain , err := registry .LoginOptionFromSecret (url , * authSecret )
178175 if err != nil {
179- return "" , fmt .Errorf ("failed to configure login options: %w" , err )
176+ return fmt .Errorf ("failed to configure login options: %w" , err )
180177 }
181178 opts .Keychain = keychain
182179 }
@@ -185,48 +182,22 @@ func configureOCIRegistryWithSecrets(ctx context.Context, obj *sourcev1.HelmRepo
185182 if obj .Spec .SecretRef == nil && obj .Spec .Provider != "" && obj .Spec .Provider != sourcev1 .GenericOCIProvider {
186183 authenticator , err := soci .OIDCAuth (ctx , url , obj .Spec .Provider )
187184 if err != nil {
188- return "" , fmt .Errorf ("failed to get credential from '%s': %w" , obj .Spec .Provider , err )
185+ return fmt .Errorf ("failed to get credential from '%s': %w" , obj .Spec .Provider , err )
189186 }
190187 opts .Authenticator = authenticator
191188 }
192189
193190 // Setup registry login options
194191 loginOpt , err := registry .NewLoginOption (opts .Authenticator , opts .Keychain , url )
195192 if err != nil {
196- return "" , err
193+ return err
197194 }
198195
199196 if loginOpt != nil {
200197 opts .RegLoginOpts = []helmreg.LoginOption {loginOpt , helmreg .LoginOptInsecure (obj .Spec .Insecure )}
201198 }
202199
203- // Handle TLS certificate files for OCI
204- var tempCertDir string
205- if opts .TlsConfig != nil {
206- tempCertDir , err = os .MkdirTemp ("" , "helm-repo-oci-certs" )
207- if err != nil {
208- return "" , fmt .Errorf ("cannot create temporary directory: %w" , err )
209- }
210-
211- var tlsSecret * corev1.Secret
212- if certSecret != nil {
213- tlsSecret = certSecret
214- } else if authSecret != nil {
215- tlsSecret = authSecret
216- }
217-
218- certFile , keyFile , caFile , err := storeTLSCertificateFilesForOCI (ctx , tlsSecret , nil , tempCertDir )
219- if err != nil {
220- return "" , fmt .Errorf ("cannot write certs files to path: %w" , err )
221- }
222-
223- tlsLoginOpt := registry .TLSLoginOption (certFile , keyFile , caFile )
224- if tlsLoginOpt != nil {
225- opts .RegLoginOpts = append (opts .RegLoginOpts , tlsLoginOpt )
226- }
227- }
228-
229- return tempCertDir , nil
200+ return nil
230201}
231202
232203func fetchSecret (ctx context.Context , c client.Client , name , namespace string ) (* corev1.Secret , error ) {
@@ -240,57 +211,3 @@ func fetchSecret(ctx context.Context, c client.Client, name, namespace string) (
240211 }
241212 return & secret , nil
242213}
243-
244- // storeTLSCertificateFilesForOCI writes TLS certificate data from secrets to files for OCI registry authentication.
245- // Helm OCI registry client requires certificate file paths rather than in-memory data,
246- // so we need to temporarily write the certificate data to disk.
247- // Returns paths to the written cert, key, and CA files (any of which may be empty if not present).
248- func storeTLSCertificateFilesForOCI (ctx context.Context , certSecret , authSecret * corev1.Secret , path string ) (string , string , string , error ) {
249- var (
250- certFile string
251- keyFile string
252- caFile string
253- err error
254- )
255-
256- // Try to get TLS data from certSecret first, then authSecret
257- var tlsSecret * corev1.Secret
258- if certSecret != nil {
259- tlsSecret = certSecret
260- } else if authSecret != nil {
261- tlsSecret = authSecret
262- }
263-
264- if tlsSecret != nil {
265- if certData , exists := tlsSecret .Data [secrets .KeyTLSCert ]; exists {
266- if keyData , keyExists := tlsSecret .Data [secrets .KeyTLSPrivateKey ]; keyExists {
267- certFile , err = writeToFile (certData , certFileName , path )
268- if err != nil {
269- return "" , "" , "" , err
270- }
271- keyFile , err = writeToFile (keyData , keyFileName , path )
272- if err != nil {
273- return "" , "" , "" , err
274- }
275- }
276- }
277-
278- if caData , exists := tlsSecret .Data [secrets .KeyCACert ]; exists {
279- caFile , err = writeToFile (caData , caFileName , path )
280- if err != nil {
281- return "" , "" , "" , err
282- }
283- }
284- }
285-
286- return certFile , keyFile , caFile , nil
287- }
288-
289- func writeToFile (data []byte , filename , tmpDir string ) (string , error ) {
290- file := path .Join (tmpDir , filename )
291- err := os .WriteFile (file , data , 0o600 )
292- if err != nil {
293- return "" , err
294- }
295- return file , nil
296- }
0 commit comments