@@ -28,6 +28,7 @@ import (
28
28
29
29
securejoin "github.com/cyphar/filepath-securejoin"
30
30
"github.com/fluxcd/pkg/auth/azure"
31
+ "github.com/fluxcd/pkg/auth/github"
31
32
"github.com/fluxcd/pkg/runtime/logger"
32
33
"github.com/go-git/go-git/v5/plumbing/transport"
33
34
corev1 "k8s.io/api/core/v1"
@@ -504,13 +505,8 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
504
505
505
506
authOpts , err := r .getAuthOpts (ctx , obj , * u )
506
507
if err != nil {
507
- e := serror .NewGeneric (
508
- fmt .Errorf ("failed to configure authentication options: %w" , err ),
509
- sourcev1 .AuthenticationFailedReason ,
510
- )
511
- conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , "%s" , e )
512
508
// Return error as the world as observed may change
513
- return sreconcile .ResultEmpty , e
509
+ return sreconcile .ResultEmpty , err
514
510
}
515
511
516
512
// Fetch the included artifact metadata.
@@ -637,26 +633,63 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
637
633
var err error
638
634
authData , err = r .getSecretData (ctx , obj .Spec .SecretRef .Name , obj .GetNamespace ())
639
635
if err != nil {
640
- return nil , fmt .Errorf ("failed to get secret '%s/%s': %w" , obj .GetNamespace (), obj .Spec .SecretRef .Name , err )
636
+ e := serror .NewGeneric (
637
+ fmt .Errorf ("failed to get secret '%s/%s': %w" , obj .GetNamespace (), obj .Spec .SecretRef .Name , err ),
638
+ sourcev1 .AuthenticationFailedReason ,
639
+ )
640
+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , "%s" , e )
641
+ return nil , e
641
642
}
642
643
}
643
644
644
645
// Configure authentication strategy to access the source
645
646
authOpts , err := git .NewAuthOptions (u , authData )
646
647
if err != nil {
647
- return nil , err
648
+ e := serror .NewGeneric (
649
+ fmt .Errorf ("failed to configure authentication options: %w" , err ),
650
+ sourcev1 .AuthenticationFailedReason ,
651
+ )
652
+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , "%s" , e )
653
+ return nil , e
648
654
}
649
655
650
656
// Configure provider authentication if specified in spec
651
- if obj .GetProvider () == sourcev1 .GitProviderAzure {
657
+ switch obj .GetProvider () {
658
+ case sourcev1 .GitProviderAzure :
652
659
authOpts .ProviderOpts = & git.ProviderOptions {
653
- Name : obj . GetProvider () ,
660
+ Name : sourcev1 . GitProviderAzure ,
654
661
AzureOpts : []azure.OptFunc {
655
662
azure .WithAzureDevOpsScope (),
656
663
},
657
664
}
658
- }
665
+ case sourcev1 .GitProviderGitHub :
666
+ // if provider is github, but secret ref is not specified
667
+ if obj .Spec .SecretRef == nil {
668
+ e := serror .NewStalling (
669
+ fmt .Errorf ("secretRef with github app data must be specified when provider is set to github" ),
670
+ sourcev1 .InvalidProviderConfigurationReason ,
671
+ )
672
+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , "%s" , e )
673
+ return nil , e
674
+ }
659
675
676
+ authOpts .ProviderOpts = & git.ProviderOptions {
677
+ Name : sourcev1 .GitProviderGitHub ,
678
+ GitHubOpts : []github.OptFunc {
679
+ github .WithAppData (authData ),
680
+ },
681
+ }
682
+ default :
683
+ // analyze secret, if it has github app data, perhaps provider should have been github.
684
+ if appID := authData [github .AppIDKey ]; len (appID ) != 0 {
685
+ e := serror .NewStalling (
686
+ fmt .Errorf ("secretRef '%s/%s' has github app data but provider is not set to github" , obj .GetNamespace (), obj .Spec .SecretRef .Name ),
687
+ sourcev1 .InvalidProviderConfigurationReason ,
688
+ )
689
+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , "%s" , e )
690
+ return nil , e
691
+ }
692
+ }
660
693
return authOpts , nil
661
694
}
662
695
0 commit comments