@@ -25,6 +25,7 @@ import (
25
25
"time"
26
26
27
27
"github.com/ProtonMail/go-crypto/openpgp"
28
+ "github.com/fluxcd/pkg/runtime/secrets"
28
29
"github.com/go-git/go-git/v5/plumbing/transport"
29
30
corev1 "k8s.io/api/core/v1"
30
31
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -165,13 +166,15 @@ func configurePush(cfg *gitSrcCfg, gitSpec *imagev1.GitSpec, checkoutRef *source
165
166
166
167
func getAuthOpts (ctx context.Context , c client.Client , repo * sourcev1.GitRepository ,
167
168
srcOpts SourceOptions , proxyURL * url.URL ) (* git.AuthOptions , error ) {
169
+ var secret * corev1.Secret
168
170
var data map [string ][]byte
169
171
var err error
170
172
if repo .Spec .SecretRef != nil {
171
- data , err = getSecretData (ctx , c , repo .Spec .SecretRef .Name , repo .GetNamespace ())
173
+ secret , err = getSecret (ctx , c , repo .Spec .SecretRef .Name , repo .GetNamespace ())
172
174
if err != nil {
173
175
return nil , fmt .Errorf ("failed to get auth secret '%s/%s': %w" , repo .GetNamespace (), repo .Spec .SecretRef .Name , err )
174
176
}
177
+ data = secret .Data
175
178
}
176
179
177
180
u , err := url .Parse (repo .Spec .URL )
@@ -211,12 +214,20 @@ func getAuthOpts(ctx context.Context, c client.Client, repo *sourcev1.GitReposit
211
214
if repo .Spec .SecretRef == nil {
212
215
return nil , fmt .Errorf ("secretRef with github app data must be specified when provider is set to github: %w" , ErrInvalidSourceConfiguration )
213
216
}
217
+ targetURL := fmt .Sprintf ("%s://%s" , u .Scheme , u .Host )
218
+ authMethods , err := secrets .AuthMethodsFromSecret (ctx , secret , secrets .WithTargetURL (targetURL ), secrets .WithTLSSystemCertPool ())
219
+ if err != nil {
220
+ return nil , err
221
+ }
222
+ if ! authMethods .HasGitHubAppData () {
223
+ return nil , fmt .Errorf ("secretRef with github app data must be specified when provider is set to github: %w" , ErrInvalidSourceConfiguration )
224
+ }
214
225
215
226
getCreds = func () (* authutils.GitCredentials , error ) {
216
227
var opts []github.OptFunc
217
228
218
229
if len (data ) > 0 {
219
- opts = append (opts , github .WithAppData (data ))
230
+ opts = append (opts , github .WithAppData (authMethods . GitHubAppData ))
220
231
}
221
232
222
233
if proxyURL != nil {
@@ -228,6 +239,10 @@ func getAuthOpts(ctx context.Context, c client.Client, repo *sourcev1.GitReposit
228
239
srcOpts .objName , srcOpts .objNamespace , cache .OperationReconcile ))
229
240
}
230
241
242
+ if authMethods .HasTLS () {
243
+ opts = append (opts , github .WithTLSConfig (authMethods .TLS ))
244
+ }
245
+
231
246
username , password , err := github .GetCredentials (ctx , opts ... )
232
247
if err != nil {
233
248
return nil , err
@@ -330,13 +345,21 @@ func getSigningEntity(ctx context.Context, c client.Client, namespace string, gi
330
345
}
331
346
332
347
func getSecretData (ctx context.Context , c client.Client , name , namespace string ) (map [string ][]byte , error ) {
348
+ secret , err := getSecret (ctx , c , name , namespace )
349
+ if err != nil {
350
+ return nil , err
351
+ }
352
+ return secret .Data , nil
353
+ }
354
+
355
+ func getSecret (ctx context.Context , c client.Client , name , namespace string ) (* corev1.Secret , error ) {
333
356
key := types.NamespacedName {
334
357
Namespace : namespace ,
335
358
Name : name ,
336
359
}
337
- var secret corev1.Secret
338
- if err := c .Get (ctx , key , & secret ); err != nil {
360
+ secret := & corev1.Secret {}
361
+ if err := c .Get (ctx , key , secret ); err != nil {
339
362
return nil , err
340
363
}
341
- return secret . Data , nil
364
+ return secret , nil
342
365
}
0 commit comments