Skip to content

Commit c921cbd

Browse files
committed
Add a temporary credential file for login operation
Signed-off-by: Soule BA <soule@weave.works>
1 parent 0c0095c commit c921cbd

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

controllers/helmchart_controller.go

+13
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,19 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
515515
}
516516

517517
logOpts := append([]registry.LoginOption{}, logOpt)
518+
519+
// create a temporary file to store the credentials
520+
// this is needed because otherwise the credentials are stored in ~/.docker/config.json.
521+
// TODO@souleb: remove this once the registry move to Oras v2
522+
// or rework to enable reusing credentials to avoid the unneccessary handshake operations
523+
credentialFile, err := os.CreateTemp("", "credentials")
524+
if err != nil {
525+
return chartRepoErrorReturn(err, obj)
526+
}
527+
defer os.Remove(credentialFile.Name())
528+
529+
// set the credentials file to the registry client
530+
registry.ClientOptCredentialsFile(credentialFile.Name())(r.RegistryClient)
518531
err = ociChartRepo.Login(logOpts...)
519532
if err != nil {
520533
return chartRepoErrorReturn(err, obj)

controllers/helmrepository_controller_oci.go

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"os"
2223
"strings"
2324
"time"
2425

@@ -305,6 +306,21 @@ func (r *HelmRepositoryOCIReconciler) validateSource(ctx context.Context, obj *s
305306

306307
// Attempt to login to the registry if credentials are provided.
307308
if loginOpts != nil {
309+
// create a temporary file to store the credentials
310+
// this is needed because otherwise the credentials are stored in ~/.docker/config.json.
311+
credentialFile, err := os.CreateTemp("", "credentials")
312+
if err != nil {
313+
e := &serror.Event{
314+
Err: fmt.Errorf("failed to create temporary file: %w", err),
315+
Reason: "ValidationError",
316+
}
317+
conditions.MarkFalse(obj, sourcev1.SourceValidCondition, e.Reason, e.Err.Error())
318+
return sreconcile.ResultEmpty, e
319+
}
320+
defer os.Remove(credentialFile.Name())
321+
322+
// set the credentials file to the registry client
323+
registry.ClientOptCredentialsFile(credentialFile.Name())(r.RegistryClient)
308324
err = chartRepo.Login(loginOpts...)
309325
if err != nil {
310326
e := &serror.Event{

docs/api/source.md

+26
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,19 @@ references to this object.
848848
NOTE: Not implemented, provisional as of <a href="https://github.com/fluxcd/flux2/pull/2092">https://github.com/fluxcd/flux2/pull/2092</a></p>
849849
</td>
850850
</tr>
851+
<tr>
852+
<td>
853+
<code>type</code><br>
854+
<em>
855+
string
856+
</em>
857+
</td>
858+
<td>
859+
<em>(Optional)</em>
860+
<p>Type of the HelmRepository.
861+
When this field is set to &ldquo;OCI&rdquo;, the URL field value must be prefixed with &ldquo;oci://&rdquo;.</p>
862+
</td>
863+
</tr>
851864
</table>
852865
</td>
853866
</tr>
@@ -2093,6 +2106,19 @@ references to this object.
20932106
NOTE: Not implemented, provisional as of <a href="https://github.com/fluxcd/flux2/pull/2092">https://github.com/fluxcd/flux2/pull/2092</a></p>
20942107
</td>
20952108
</tr>
2109+
<tr>
2110+
<td>
2111+
<code>type</code><br>
2112+
<em>
2113+
string
2114+
</em>
2115+
</td>
2116+
<td>
2117+
<em>(Optional)</em>
2118+
<p>Type of the HelmRepository.
2119+
When this field is set to &ldquo;OCI&rdquo;, the URL field value must be prefixed with &ldquo;oci://&rdquo;.</p>
2120+
</td>
2121+
</tr>
20962122
</tbody>
20972123
</table>
20982124
</div>

0 commit comments

Comments
 (0)