@@ -28,6 +28,7 @@ import (
2828
2929 securejoin "github.com/cyphar/filepath-securejoin"
3030 "github.com/fluxcd/pkg/auth"
31+ authutils "github.com/fluxcd/pkg/auth/utils"
3132 "github.com/fluxcd/pkg/git/github"
3233 "github.com/fluxcd/pkg/runtime/logger"
3334 "github.com/go-git/go-git/v5/plumbing/transport"
@@ -683,28 +684,28 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
683684 return nil , e
684685 }
685686
686- var authOpts []auth.Option
687+ // Configure provider authentication if specified.
688+ var getCreds func () (* authutils.GitCredentials , error )
689+ switch provider := obj .GetProvider (); provider {
690+ case sourcev1 .GitProviderAzure : // If AWS or GCP are added in the future they can be added here separated by a comma.
691+ getCreds = func () (* authutils.GitCredentials , error ) {
692+ var opts []auth.Option
687693
688- if r .TokenCache != nil {
689- involvedObject := cache.InvolvedObject {
690- Kind : sourcev1 .GitRepositoryKind ,
691- Name : obj .GetName (),
692- Namespace : obj .GetNamespace (),
693- Operation : cache .OperationReconcile ,
694- }
695- authOpts = append (authOpts , auth .WithCache (* r .TokenCache , involvedObject ))
696- }
694+ if r .TokenCache != nil {
695+ involvedObject := cache.InvolvedObject {
696+ Kind : sourcev1 .GitRepositoryKind ,
697+ Name : obj .GetName (),
698+ Namespace : obj .GetNamespace (),
699+ Operation : cache .OperationReconcile ,
700+ }
701+ opts = append (opts , auth .WithCache (* r .TokenCache , involvedObject ))
702+ }
697703
698- if proxyURL != nil {
699- authOpts = append (authOpts , auth .WithProxyURL (* proxyURL ))
700- }
704+ if proxyURL != nil {
705+ opts = append (opts , auth .WithProxyURL (* proxyURL ))
706+ }
701707
702- // Configure provider authentication if specified in spec
703- switch obj .GetProvider () {
704- case sourcev1 .GitProviderAzure :
705- opts .ProviderOpts = & git.ProviderOptions {
706- Name : sourcev1 .GitProviderAzure ,
707- AuthOpts : authOpts ,
708+ return authutils .GetGitCredentials (ctx , provider , opts ... )
708709 }
709710 case sourcev1 .GitProviderGitHub :
710711 // if provider is github, but secret ref is not specified
@@ -717,14 +718,30 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
717718 return nil , e
718719 }
719720
720- opts .ProviderOpts = & git.ProviderOptions {
721- Name : sourcev1 .GitProviderGitHub ,
722- GitHubOpts : []github.OptFunc {
723- github .WithAppData (authData ),
724- github .WithProxyURL (proxyURL ),
725- github .WithCache (r .TokenCache , sourcev1 .GitRepositoryKind ,
726- obj .GetName (), obj .GetNamespace (), cache .OperationReconcile ),
727- },
721+ getCreds = func () (* authutils.GitCredentials , error ) {
722+ var opts []github.OptFunc
723+
724+ if len (authData ) > 0 {
725+ opts = append (opts , github .WithAppData (authData ))
726+ }
727+
728+ if proxyURL != nil {
729+ opts = append (opts , github .WithProxyURL (proxyURL ))
730+ }
731+
732+ if r .TokenCache != nil {
733+ opts = append (opts , github .WithCache (r .TokenCache , sourcev1 .GitRepositoryKind ,
734+ obj .GetName (), obj .GetNamespace (), cache .OperationReconcile ))
735+ }
736+
737+ username , password , err := github .GetCredentials (ctx , opts ... )
738+ if err != nil {
739+ return nil , err
740+ }
741+ return & authutils.GitCredentials {
742+ Username : username ,
743+ Password : password ,
744+ }, nil
728745 }
729746 default :
730747 // analyze secret, if it has github app data, perhaps provider should have been github.
@@ -737,6 +754,20 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
737754 return nil , e
738755 }
739756 }
757+ if getCreds != nil {
758+ creds , err := getCreds ()
759+ if err != nil {
760+ e := serror .NewGeneric (
761+ fmt .Errorf ("failed to configure authentication options: %w" , err ),
762+ sourcev1 .AuthenticationFailedReason ,
763+ )
764+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , "%s" , e )
765+ return nil , e
766+ }
767+ opts .BearerToken = creds .BearerToken
768+ opts .Username = creds .Username
769+ opts .Password = creds .Password
770+ }
740771 return opts , nil
741772}
742773
0 commit comments