@@ -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"
@@ -62,13 +63,14 @@ type gitSrcCfg struct {
62
63
}
63
64
64
65
func buildGitConfig (ctx context.Context , c client.Client , originKey , srcKey types.NamespacedName , gitSpec * imagev1.GitSpec , opts SourceOptions ) (* gitSrcCfg , error ) {
66
+ var err error
65
67
cfg := & gitSrcCfg {
66
68
srcKey : srcKey ,
67
69
}
68
70
69
71
// Get the repo.
70
72
repo := & sourcev1.GitRepository {}
71
- if err : = c .Get (ctx , srcKey , repo ); err != nil {
73
+ if err = c .Get (ctx , srcKey , repo ); err != nil {
72
74
if client .IgnoreNotFound (err ) == nil {
73
75
return nil , fmt .Errorf ("referenced git repository does not exist: %w" , err )
74
76
}
@@ -94,14 +96,26 @@ func buildGitConfig(ctx context.Context, c client.Client, originKey, srcKey type
94
96
95
97
// Configure push first as the client options below depend on the push
96
98
// configuration.
97
- if err : = configurePush (cfg , gitSpec , cfg .checkoutRef ); err != nil {
99
+ if err = configurePush (cfg , gitSpec , cfg .checkoutRef ); err != nil {
98
100
return nil , err
99
101
}
100
102
101
- proxyOpts , proxyURL , err := getProxyOpts (ctx , c , repo )
102
- if err != nil {
103
- return nil , err
103
+ var proxyURL * url.URL
104
+ var proxyOpts * transport.ProxyOptions
105
+ // Check if a proxy secret reference is provided in the GitRepository spec.
106
+ if repo .Spec .ProxySecretRef != nil {
107
+ secretRef := types.NamespacedName {
108
+ Name : repo .Spec .ProxySecretRef .Name ,
109
+ Namespace : repo .GetNamespace (),
110
+ }
111
+ // Get the proxy URL from runtime/secret
112
+ proxyURL , err = secrets .ProxyURLFromSecretRef (ctx , c , secretRef )
113
+ if err != nil {
114
+ return nil , err
115
+ }
116
+ proxyOpts = & transport.ProxyOptions {URL : proxyURL .String ()}
104
117
}
118
+
105
119
cfg .authOpts , err = getAuthOpts (ctx , c , repo , opts , proxyURL )
106
120
if err != nil {
107
121
return nil , err
@@ -165,13 +179,15 @@ func configurePush(cfg *gitSrcCfg, gitSpec *imagev1.GitSpec, checkoutRef *source
165
179
166
180
func getAuthOpts (ctx context.Context , c client.Client , repo * sourcev1.GitRepository ,
167
181
srcOpts SourceOptions , proxyURL * url.URL ) (* git.AuthOptions , error ) {
182
+ var secret * corev1.Secret
168
183
var data map [string ][]byte
169
184
var err error
170
185
if repo .Spec .SecretRef != nil {
171
- data , err = getSecretData (ctx , c , repo .Spec .SecretRef .Name , repo .GetNamespace ())
186
+ secret , err = getSecret (ctx , c , repo .Spec .SecretRef .Name , repo .GetNamespace ())
172
187
if err != nil {
173
188
return nil , fmt .Errorf ("failed to get auth secret '%s/%s': %w" , repo .GetNamespace (), repo .Spec .SecretRef .Name , err )
174
189
}
190
+ data = secret .Data
175
191
}
176
192
177
193
u , err := url .Parse (repo .Spec .URL )
@@ -211,24 +227,34 @@ func getAuthOpts(ctx context.Context, c client.Client, repo *sourcev1.GitReposit
211
227
if repo .Spec .SecretRef == nil {
212
228
return nil , fmt .Errorf ("secretRef with github app data must be specified when provider is set to github: %w" , ErrInvalidSourceConfiguration )
213
229
}
230
+ targetURL := fmt .Sprintf ("%s://%s" , u .Scheme , u .Host )
231
+ authMethods , err := secrets .AuthMethodsFromSecret (ctx , secret , secrets .WithTargetURL (targetURL ), secrets .WithTLSSystemCertPool ())
232
+ if err != nil {
233
+ return nil , err
234
+ }
235
+ if ! authMethods .HasGitHubAppData () {
236
+ return nil , fmt .Errorf ("secretRef with github app data must be specified when provider is set to github: %w" , ErrInvalidSourceConfiguration )
237
+ }
214
238
215
239
getCreds = func () (* authutils.GitCredentials , error ) {
216
- var opts []github.OptFunc
240
+ var appOpts []github.OptFunc
217
241
218
- if len (data ) > 0 {
219
- opts = append (opts , github .WithAppData (data ))
220
- }
242
+ appOpts = append (appOpts , github .WithAppData (authMethods .GitHubAppData ))
221
243
222
244
if proxyURL != nil {
223
- opts = append (opts , github .WithProxyURL (proxyURL ))
245
+ appOpts = append (appOpts , github .WithProxyURL (proxyURL ))
224
246
}
225
247
226
248
if srcOpts .tokenCache != nil {
227
- opts = append (opts , github .WithCache (srcOpts .tokenCache , imagev1 .ImageUpdateAutomationKind ,
249
+ appOpts = append (appOpts , github .WithCache (srcOpts .tokenCache , imagev1 .ImageUpdateAutomationKind ,
228
250
srcOpts .objName , srcOpts .objNamespace , cache .OperationReconcile ))
229
251
}
230
252
231
- username , password , err := github .GetCredentials (ctx , opts ... )
253
+ if authMethods .HasTLS () {
254
+ appOpts = append (appOpts , github .WithTLSConfig (authMethods .TLS ))
255
+ }
256
+
257
+ username , password , err := github .GetCredentials (ctx , appOpts ... )
232
258
if err != nil {
233
259
return nil , err
234
260
}
@@ -255,45 +281,6 @@ func getAuthOpts(ctx context.Context, c client.Client, repo *sourcev1.GitReposit
255
281
return opts , nil
256
282
}
257
283
258
- func getProxyOpts (ctx context.Context , c client.Client , repo * sourcev1.GitRepository ) (* transport.ProxyOptions , * url.URL , error ) {
259
- if repo .Spec .ProxySecretRef == nil {
260
- return nil , nil , nil
261
- }
262
- name := repo .Spec .ProxySecretRef .Name
263
- namespace := repo .GetNamespace ()
264
- proxyData , err := getSecretData (ctx , c , name , namespace )
265
- if err != nil {
266
- return nil , nil , fmt .Errorf ("failed to get proxy secret '%s/%s': %w" , namespace , name , err )
267
- }
268
- b , ok := proxyData ["address" ]
269
- if ! ok {
270
- return nil , nil , fmt .Errorf ("invalid proxy secret '%s/%s': key 'address' is missing" , namespace , name )
271
- }
272
-
273
- address := string (b )
274
- username := string (proxyData ["username" ])
275
- password := string (proxyData ["password" ])
276
-
277
- proxyOpts := & transport.ProxyOptions {
278
- URL : address ,
279
- Username : username ,
280
- Password : password ,
281
- }
282
-
283
- proxyURL , err := url .Parse (string (address ))
284
- if err != nil {
285
- return nil , nil , fmt .Errorf ("invalid address in proxy secret '%s/%s': %w" , namespace , name , err )
286
- }
287
- switch {
288
- case username != "" && password == "" :
289
- proxyURL .User = url .User (username )
290
- case username != "" && password != "" :
291
- proxyURL .User = url .UserPassword (username , password )
292
- }
293
-
294
- return proxyOpts , proxyURL , nil
295
- }
296
-
297
284
func getSigningEntity (ctx context.Context , c client.Client , namespace string , gitSpec * imagev1.GitSpec ) (* openpgp.Entity , error ) {
298
285
secretName := gitSpec .Commit .SigningKey .SecretRef .Name
299
286
secretData , err := getSecretData (ctx , c , secretName , namespace )
@@ -330,13 +317,21 @@ func getSigningEntity(ctx context.Context, c client.Client, namespace string, gi
330
317
}
331
318
332
319
func getSecretData (ctx context.Context , c client.Client , name , namespace string ) (map [string ][]byte , error ) {
320
+ secret , err := getSecret (ctx , c , name , namespace )
321
+ if err != nil {
322
+ return nil , err
323
+ }
324
+ return secret .Data , nil
325
+ }
326
+
327
+ func getSecret (ctx context.Context , c client.Client , name , namespace string ) (* corev1.Secret , error ) {
333
328
key := types.NamespacedName {
334
329
Namespace : namespace ,
335
330
Name : name ,
336
331
}
337
- var secret corev1.Secret
338
- if err := c .Get (ctx , key , & secret ); err != nil {
332
+ secret := & corev1.Secret {}
333
+ if err := c .Get (ctx , key , secret ); err != nil {
339
334
return nil , err
340
335
}
341
- return secret . Data , nil
336
+ return secret , nil
342
337
}
0 commit comments