@@ -31,6 +31,7 @@ import (
31
31
authutils "github.com/fluxcd/pkg/auth/utils"
32
32
"github.com/fluxcd/pkg/git/github"
33
33
"github.com/fluxcd/pkg/runtime/logger"
34
+ "github.com/fluxcd/pkg/runtime/secrets"
34
35
"github.com/go-git/go-git/v5/plumbing/transport"
35
36
corev1 "k8s.io/api/core/v1"
36
37
"k8s.io/apimachinery/pkg/runtime"
@@ -486,7 +487,11 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
486
487
var proxyURL * url.URL
487
488
if obj .Spec .ProxySecretRef != nil {
488
489
var err error
489
- proxyOpts , proxyURL , err = r .getProxyOpts (ctx , obj .Spec .ProxySecretRef .Name , obj .GetNamespace ())
490
+ secretRef := types.NamespacedName {
491
+ Name : obj .Spec .ProxySecretRef .Name ,
492
+ Namespace : obj .GetNamespace (),
493
+ }
494
+ proxyURL , err = secrets .ProxyURLFromSecretRef (ctx , r .Client , secretRef )
490
495
if err != nil {
491
496
e := serror .NewGeneric (
492
497
fmt .Errorf ("failed to configure proxy options: %w" , err ),
@@ -496,6 +501,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
496
501
// Return error as the world as observed may change
497
502
return sreconcile .ResultEmpty , e
498
503
}
504
+ proxyOpts = & transport.ProxyOptions {URL : proxyURL .String ()}
499
505
}
500
506
501
507
u , err := url .Parse (obj .Spec .URL )
@@ -618,52 +624,16 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
618
624
return sreconcile .ResultSuccess , nil
619
625
}
620
626
621
- // getProxyOpts fetches the secret containing the proxy settings, constructs a
622
- // transport.ProxyOptions object using those settings and then returns it.
623
- func (r * GitRepositoryReconciler ) getProxyOpts (ctx context.Context , proxySecretName ,
624
- proxySecretNamespace string ) (* transport.ProxyOptions , * url.URL , error ) {
625
- proxyData , err := r .getSecretData (ctx , proxySecretName , proxySecretNamespace )
626
- if err != nil {
627
- return nil , nil , fmt .Errorf ("failed to get proxy secret '%s/%s': %w" , proxySecretNamespace , proxySecretName , err )
628
- }
629
- b , ok := proxyData ["address" ]
630
- if ! ok {
631
- return nil , nil , fmt .Errorf ("invalid proxy secret '%s/%s': key 'address' is missing" , proxySecretNamespace , proxySecretName )
632
- }
633
-
634
- address := string (b )
635
- username := string (proxyData ["username" ])
636
- password := string (proxyData ["password" ])
637
-
638
- proxyOpts := & transport.ProxyOptions {
639
- URL : address ,
640
- Username : username ,
641
- Password : password ,
642
- }
643
-
644
- proxyURL , err := url .Parse (string (address ))
645
- if err != nil {
646
- return nil , nil , fmt .Errorf ("invalid address in proxy secret '%s/%s': %w" , proxySecretNamespace , proxySecretName , err )
647
- }
648
- switch {
649
- case username != "" && password == "" :
650
- proxyURL .User = url .User (username )
651
- case username != "" && password != "" :
652
- proxyURL .User = url .UserPassword (username , password )
653
- }
654
-
655
- return proxyOpts , proxyURL , nil
656
- }
657
-
658
627
// getAuthOpts fetches the secret containing the auth options (if specified),
659
628
// constructs a git.AuthOptions object using those options along with the provided
660
629
// URL and returns it.
661
630
func (r * GitRepositoryReconciler ) getAuthOpts (ctx context.Context , obj * sourcev1.GitRepository ,
662
631
u url.URL , proxyURL * url.URL ) (* git.AuthOptions , error ) {
632
+ var secret * corev1.Secret
663
633
var authData map [string ][]byte
664
634
if obj .Spec .SecretRef != nil {
665
635
var err error
666
- authData , err = r .getSecretData (ctx , obj .Spec .SecretRef .Name , obj .GetNamespace ())
636
+ secret , err = r .getSecret (ctx , obj .Spec .SecretRef .Name , obj .GetNamespace ())
667
637
if err != nil {
668
638
e := serror .NewGeneric (
669
639
fmt .Errorf ("failed to get secret '%s/%s': %w" , obj .GetNamespace (), obj .Spec .SecretRef .Name , err ),
@@ -672,6 +642,7 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
672
642
conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , "%s" , e )
673
643
return nil , e
674
644
}
645
+ authData = secret .Data
675
646
}
676
647
677
648
// Configure authentication strategy to access the source
@@ -718,24 +689,38 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
718
689
conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , "%s" , e )
719
690
return nil , e
720
691
}
721
-
692
+ targetURL := fmt .Sprintf ("%s://%s" , u .Scheme , u .Host )
693
+ authMethods , err := secrets .AuthMethodsFromSecret (ctx , secret , secrets .WithTargetURL (targetURL ), secrets .WithTLSSystemCertPool ())
694
+ if err != nil {
695
+ return nil , err
696
+ }
697
+ if ! authMethods .HasGitHubAppData () {
698
+ e := serror .NewGeneric (
699
+ fmt .Errorf ("secretRef with github app data must be specified when provider is set to github" ),
700
+ sourcev1 .InvalidProviderConfigurationReason ,
701
+ )
702
+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , "%s" , e )
703
+ return nil , e
704
+ }
722
705
getCreds = func () (* authutils.GitCredentials , error ) {
723
- var opts []github.OptFunc
706
+ var appOpts []github.OptFunc
724
707
725
- if len (authData ) > 0 {
726
- opts = append (opts , github .WithAppData (authData ))
727
- }
708
+ appOpts = append (appOpts , github .WithAppData (authMethods .GitHubAppData ))
728
709
729
710
if proxyURL != nil {
730
- opts = append (opts , github .WithProxyURL (proxyURL ))
711
+ appOpts = append (appOpts , github .WithProxyURL (proxyURL ))
731
712
}
732
713
733
714
if r .TokenCache != nil {
734
- opts = append (opts , github .WithCache (r .TokenCache , sourcev1 .GitRepositoryKind ,
715
+ appOpts = append (appOpts , github .WithCache (r .TokenCache , sourcev1 .GitRepositoryKind ,
735
716
obj .GetName (), obj .GetNamespace (), cache .OperationReconcile ))
736
717
}
737
718
738
- username , password , err := github .GetCredentials (ctx , opts ... )
719
+ if authMethods .HasTLS () {
720
+ appOpts = append (appOpts , github .WithTLSConfig (authMethods .TLS ))
721
+ }
722
+
723
+ username , password , err := github .GetCredentials (ctx , appOpts ... )
739
724
if err != nil {
740
725
return nil , err
741
726
}
@@ -772,16 +757,16 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
772
757
return opts , nil
773
758
}
774
759
775
- func (r * GitRepositoryReconciler ) getSecretData (ctx context.Context , name , namespace string ) (map [ string ][] byte , error ) {
760
+ func (r * GitRepositoryReconciler ) getSecret (ctx context.Context , name , namespace string ) (* corev1. Secret , error ) {
776
761
key := types.NamespacedName {
777
762
Namespace : namespace ,
778
763
Name : name ,
779
764
}
780
- var secret corev1.Secret
781
- if err := r .Client .Get (ctx , key , & secret ); err != nil {
782
- return nil , err
765
+ secret := & corev1.Secret {}
766
+ if err := r .Client .Get (ctx , key , secret ); err != nil {
767
+ return nil , fmt . Errorf ( "failed to get secret '%s/%s': %w" , namespace , name , err )
783
768
}
784
- return secret . Data , nil
769
+ return secret , nil
785
770
}
786
771
787
772
// reconcileArtifact archives a new Artifact to the Storage, if the current
0 commit comments