Skip to content

Commit e399256

Browse files
committed
OCIRepository: Configure proxy for OIDC auth
Signed-off-by: Stefan Prodan <[email protected]>
1 parent cc1762b commit e399256

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

internal/controller/ocirepository_controller.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,19 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
354354
return sreconcile.ResultEmpty, e
355355
}
356356

357+
proxyURL, err := r.getProxyURL(ctx, obj)
358+
if err != nil {
359+
e := serror.NewGeneric(
360+
fmt.Errorf("failed to get proxy address: %w", err),
361+
sourcev1.AuthenticationFailedReason,
362+
)
363+
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, "%s", e)
364+
return sreconcile.ResultEmpty, e
365+
}
366+
357367
if _, ok := keychain.(soci.Anonymous); obj.Spec.Provider != ociv1.GenericOCIProvider && ok {
358368
var authErr error
359-
auth, authErr = soci.OIDCAuth(ctxTimeout, obj.Spec.URL, obj.Spec.Provider)
369+
auth, authErr = soci.OIDCAuth(ctxTimeout, obj.Spec.URL, obj.Spec.Provider, proxyURL)
360370
if authErr != nil && !errors.Is(authErr, oci.ErrUnconfiguredProvider) {
361371
e := serror.NewGeneric(
362372
fmt.Errorf("failed to get credential from %s: %w", obj.Spec.Provider, authErr),
@@ -368,7 +378,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
368378
}
369379

370380
// Generate the transport for remote operations
371-
transport, err := r.transport(ctx, obj)
381+
transport, err := r.transport(ctx, obj, proxyURL)
372382
if err != nil {
373383
e := serror.NewGeneric(
374384
fmt.Errorf("failed to generate transport for '%s': %w", obj.Spec.URL, err),
@@ -927,7 +937,7 @@ func (r *OCIRepositoryReconciler) keychain(ctx context.Context, obj *ociv1.OCIRe
927937
// the returned transport will include the TLS client and/or CA certificates.
928938
// If the insecure flag is set, the transport will skip the verification of the server's certificate.
929939
// Additionally, if a proxy is specified, transport will use it.
930-
func (r *OCIRepositoryReconciler) transport(ctx context.Context, obj *ociv1.OCIRepository) (*http.Transport, error) {
940+
func (r *OCIRepositoryReconciler) transport(ctx context.Context, obj *ociv1.OCIRepository, proxyURL *url.URL) (*http.Transport, error) {
931941
transport := remote.DefaultTransport.(*http.Transport).Clone()
932942

933943
tlsConfig, err := r.getTLSConfig(ctx, obj)
@@ -938,10 +948,6 @@ func (r *OCIRepositoryReconciler) transport(ctx context.Context, obj *ociv1.OCIR
938948
transport.TLSClientConfig = tlsConfig
939949
}
940950

941-
proxyURL, err := r.getProxyURL(ctx, obj)
942-
if err != nil {
943-
return nil, err
944-
}
945951
if proxyURL != nil {
946952
transport.Proxy = http.ProxyURL(proxyURL)
947953
}

internal/helm/getter/client_opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func GetClientOpts(ctx context.Context, c client.Client, obj *sourcev1.HelmRepos
137137
}
138138
}
139139
} else if obj.Spec.Provider != sourcev1beta2.GenericOCIProvider && obj.Spec.Type == sourcev1.HelmRepositoryTypeOCI && ociRepo {
140-
authenticator, authErr := soci.OIDCAuth(ctx, obj.Spec.URL, obj.Spec.Provider)
140+
authenticator, authErr := soci.OIDCAuth(ctx, obj.Spec.URL, obj.Spec.Provider, nil)
141141
if authErr != nil && !errors.Is(authErr, oci.ErrUnconfiguredProvider) {
142142
return nil, "", fmt.Errorf("failed to get credential from '%s': %w", obj.Spec.Provider, authErr)
143143
}

internal/oci/auth.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package oci
1919
import (
2020
"context"
2121
"fmt"
22+
"net/url"
2223
"strings"
2324

2425
"github.com/fluxcd/pkg/oci/auth/login"
@@ -40,7 +41,7 @@ func (a Anonymous) Resolve(_ authn.Resource) (authn.Authenticator, error) {
4041
}
4142

4243
// OIDCAuth generates the OIDC credential authenticator based on the specified cloud provider.
43-
func OIDCAuth(ctx context.Context, url, provider string) (authn.Authenticator, error) {
44+
func OIDCAuth(ctx context.Context, url, provider string, proxyURL *url.URL) (authn.Authenticator, error) {
4445
u := strings.TrimPrefix(url, sourcev1.OCIRepositoryPrefix)
4546
ref, err := name.ParseReference(u)
4647
if err != nil {
@@ -57,5 +58,5 @@ func OIDCAuth(ctx context.Context, url, provider string) (authn.Authenticator, e
5758
opts.GcpAutoLogin = true
5859
}
5960

60-
return login.NewManager().Login(ctx, u, ref, opts)
61+
return login.NewManager(login.WithProxyURL(proxyURL)).Login(ctx, u, ref, opts)
6162
}

0 commit comments

Comments
 (0)