@@ -28,6 +28,7 @@ import (
2828
2929 securejoin "github.com/cyphar/filepath-securejoin"
3030 "github.com/fluxcd/pkg/auth/azure"
31+ "github.com/fluxcd/pkg/auth/github"
3132 "github.com/fluxcd/pkg/runtime/logger"
3233 "github.com/go-git/go-git/v5/plumbing/transport"
3334 corev1 "k8s.io/api/core/v1"
@@ -504,13 +505,8 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
504505
505506 authOpts , err := r .getAuthOpts (ctx , obj , * u )
506507 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 )
512508 // Return error as the world as observed may change
513- return sreconcile .ResultEmpty , e
509+ return sreconcile .ResultEmpty , err
514510 }
515511
516512 // Fetch the included artifact metadata.
@@ -637,26 +633,63 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
637633 var err error
638634 authData , err = r .getSecretData (ctx , obj .Spec .SecretRef .Name , obj .GetNamespace ())
639635 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
641642 }
642643 }
643644
644645 // Configure authentication strategy to access the source
645646 authOpts , err := git .NewAuthOptions (u , authData )
646647 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
648654 }
649655
650656 // Configure provider authentication if specified in spec
651- if obj .GetProvider () == sourcev1 .GitProviderAzure {
657+ switch obj .GetProvider () {
658+ case sourcev1 .GitProviderAzure :
652659 authOpts .ProviderOpts = & git.ProviderOptions {
653- Name : obj . GetProvider () ,
660+ Name : sourcev1 . GitProviderAzure ,
654661 AzureOpts : []azure.OptFunc {
655662 azure .WithAzureDevOpsScope (),
656663 },
657664 }
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+ }
659675
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+ }
660693 return authOpts , nil
661694}
662695
0 commit comments