@@ -32,6 +32,7 @@ import (
3232 "sigs.k8s.io/controller-runtime/pkg/client"
3333
3434 "github.com/fluxcd/pkg/auth"
35+ authutils "github.com/fluxcd/pkg/auth/utils"
3536 "github.com/fluxcd/pkg/cache"
3637 "github.com/fluxcd/pkg/git"
3738 "github.com/fluxcd/pkg/git/github"
@@ -183,49 +184,74 @@ func getAuthOpts(ctx context.Context, c client.Client, repo *sourcev1.GitReposit
183184 return nil , fmt .Errorf ("failed to configure authentication options: %w" , err )
184185 }
185186
186- var authOpts []auth.Option
187-
188- if srcOpts .tokenCache != nil {
189- involvedObject := cache.InvolvedObject {
190- Kind : imagev1 .ImageUpdateAutomationKind ,
191- Name : srcOpts .objName ,
192- Namespace : srcOpts .objNamespace ,
193- Operation : cache .OperationReconcile ,
194- }
195- authOpts = append (authOpts , auth .WithCache (* srcOpts .tokenCache , involvedObject ))
196- }
187+ var getCreds func () (* authutils.GitCredentials , error )
188+ switch provider := repo .GetProvider (); provider {
189+ case sourcev1 .GitProviderAzure : // If AWS or GCP are added in the future they can be added here separated by a comma.
190+ getCreds = func () (* authutils.GitCredentials , error ) {
191+ var opts []auth.Option
192+
193+ if srcOpts .tokenCache != nil {
194+ involvedObject := cache.InvolvedObject {
195+ Kind : imagev1 .ImageUpdateAutomationKind ,
196+ Name : srcOpts .objName ,
197+ Namespace : srcOpts .objNamespace ,
198+ Operation : cache .OperationReconcile ,
199+ }
200+ opts = append (opts , auth .WithCache (* srcOpts .tokenCache , involvedObject ))
201+ }
197202
198- if proxyURL != nil {
199- authOpts = append (authOpts , auth .WithProxyURL (* proxyURL ))
200- }
203+ if proxyURL != nil {
204+ opts = append (opts , auth .WithProxyURL (* proxyURL ))
205+ }
201206
202- switch repo .GetProvider () {
203- case sourcev1 .GitProviderAzure :
204- opts .ProviderOpts = & git.ProviderOptions {
205- Name : sourcev1 .GitProviderAzure ,
206- AuthOpts : authOpts ,
207+ return authutils .GetGitCredentials (ctx , provider , opts ... )
207208 }
208209 case sourcev1 .GitProviderGitHub :
209210 // if provider is github, but secret ref is not specified
210211 if repo .Spec .SecretRef == nil {
211212 return nil , fmt .Errorf ("secretRef with github app data must be specified when provider is set to github: %w" , ErrInvalidSourceConfiguration )
212213 }
213- opts .ProviderOpts = & git.ProviderOptions {
214- Name : sourcev1 .GitProviderGitHub ,
215- GitHubOpts : []github.OptFunc {
216- github .WithAppData (data ),
217- github .WithProxyURL (proxyURL ),
218- github .WithCache (srcOpts .tokenCache , imagev1 .ImageUpdateAutomationKind ,
219- srcOpts .objName , srcOpts .objNamespace , cache .OperationReconcile ),
220- },
214+
215+ getCreds = func () (* authutils.GitCredentials , error ) {
216+ var opts []github.OptFunc
217+
218+ if len (data ) > 0 {
219+ opts = append (opts , github .WithAppData (data ))
220+ }
221+
222+ if proxyURL != nil {
223+ opts = append (opts , github .WithProxyURL (proxyURL ))
224+ }
225+
226+ if srcOpts .tokenCache != nil {
227+ opts = append (opts , github .WithCache (srcOpts .tokenCache , imagev1 .ImageUpdateAutomationKind ,
228+ srcOpts .objName , srcOpts .objNamespace , cache .OperationReconcile ))
229+ }
230+
231+ username , password , err := github .GetCredentials (ctx , opts ... )
232+ if err != nil {
233+ return nil , err
234+ }
235+ return & authutils.GitCredentials {
236+ Username : username ,
237+ Password : password ,
238+ }, nil
221239 }
222240 default :
223241 // analyze secret, if it has github app data, perhaps provider should have been github.
224242 if appID := data [github .AppIDKey ]; len (appID ) != 0 {
225243 return nil , fmt .Errorf ("secretRef '%s/%s' has github app data but provider is not set to github: %w" , repo .GetNamespace (), repo .Spec .SecretRef .Name , ErrInvalidSourceConfiguration )
226244 }
227245 }
228-
246+ if getCreds != nil {
247+ creds , err := getCreds ()
248+ if err != nil {
249+ return nil , fmt .Errorf ("failed to configure authentication options: %w" , err )
250+ }
251+ opts .BearerToken = creds .BearerToken
252+ opts .Username = creds .Username
253+ opts .Password = creds .Password
254+ }
229255 return opts , nil
230256}
231257
0 commit comments