Skip to content

Commit

Permalink
Add a temporary credential file for login operation
Browse files Browse the repository at this point in the history
Signed-off-by: Soule BA <soule@weave.works>
  • Loading branch information
souleb committed May 10, 2022
1 parent 0c0095c commit c921cbd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions controllers/helmchart_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,19 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
}

logOpts := append([]registry.LoginOption{}, logOpt)

// create a temporary file to store the credentials
// this is needed because otherwise the credentials are stored in ~/.docker/config.json.
// TODO@souleb: remove this once the registry move to Oras v2
// or rework to enable reusing credentials to avoid the unneccessary handshake operations
credentialFile, err := os.CreateTemp("", "credentials")
if err != nil {
return chartRepoErrorReturn(err, obj)
}
defer os.Remove(credentialFile.Name())

// set the credentials file to the registry client
registry.ClientOptCredentialsFile(credentialFile.Name())(r.RegistryClient)
err = ociChartRepo.Login(logOpts...)
if err != nil {
return chartRepoErrorReturn(err, obj)
Expand Down
16 changes: 16 additions & 0 deletions controllers/helmrepository_controller_oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"os"
"strings"
"time"

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

// Attempt to login to the registry if credentials are provided.
if loginOpts != nil {
// create a temporary file to store the credentials
// this is needed because otherwise the credentials are stored in ~/.docker/config.json.
credentialFile, err := os.CreateTemp("", "credentials")
if err != nil {
e := &serror.Event{
Err: fmt.Errorf("failed to create temporary file: %w", err),
Reason: "ValidationError",
}
conditions.MarkFalse(obj, sourcev1.SourceValidCondition, e.Reason, e.Err.Error())
return sreconcile.ResultEmpty, e
}
defer os.Remove(credentialFile.Name())

// set the credentials file to the registry client
registry.ClientOptCredentialsFile(credentialFile.Name())(r.RegistryClient)
err = chartRepo.Login(loginOpts...)
if err != nil {
e := &serror.Event{
Expand Down
26 changes: 26 additions & 0 deletions docs/api/source.md
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,19 @@ references to this object.
NOTE: Not implemented, provisional as of <a href="https://github.com/fluxcd/flux2/pull/2092">https://github.com/fluxcd/flux2/pull/2092</a></p>
</td>
</tr>
<tr>
<td>
<code>type</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Type of the HelmRepository.
When this field is set to &ldquo;OCI&rdquo;, the URL field value must be prefixed with &ldquo;oci://&rdquo;.</p>
</td>
</tr>
</table>
</td>
</tr>
Expand Down Expand Up @@ -2093,6 +2106,19 @@ references to this object.
NOTE: Not implemented, provisional as of <a href="https://github.com/fluxcd/flux2/pull/2092">https://github.com/fluxcd/flux2/pull/2092</a></p>
</td>
</tr>
<tr>
<td>
<code>type</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Type of the HelmRepository.
When this field is set to &ldquo;OCI&rdquo;, the URL field value must be prefixed with &ldquo;oci://&rdquo;.</p>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down

0 comments on commit c921cbd

Please sign in to comment.