@@ -62,6 +62,8 @@ import (
62
62
sreconcile "github.com/fluxcd/source-controller/internal/reconcile"
63
63
"github.com/fluxcd/source-controller/internal/reconcile/summarize"
64
64
"github.com/fluxcd/source-controller/internal/util"
65
+ "github.com/fluxcd/source-controller/internal/tls"
66
+ gitclient "github.com/go-git/go-git/v5/plumbing/transport/client"
65
67
)
66
68
67
69
// gitRepositoryReadyCondition contains the information required to summarize a
@@ -148,6 +150,100 @@ func (r *GitRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
148
150
return r .SetupWithManagerAndOptions (mgr , GitRepositoryReconcilerOptions {})
149
151
}
150
152
153
+ // Interface co configure gitclient with custom TLS options
154
+ // used for application firewall authentication.
155
+ type GitClientConfigurer interface {
156
+ ConfigureGitClient (ctx context.Context , obj * sourcev1.GitRepository )
157
+ IsValid () bool
158
+ backupHttpsTransport ()
159
+ }
160
+
161
+ type GitClientHttpConfigurer struct {
162
+ SSLCertificateData map [string ][]byte
163
+ ProxyOpts * transport.ProxyOptions
164
+ Valid bool
165
+ DefaultTransport transport.Transport
166
+ AppFirewallTransport transport.Transport
167
+ }
168
+
169
+ func (c * GitClientHttpConfigurer ) IsValid () bool {
170
+ return c .Valid
171
+ }
172
+
173
+ func (c * GitClientHttpConfigurer ) SetValid () {
174
+ c .Valid = true
175
+ }
176
+
177
+ func (c * GitClientHttpConfigurer ) SetInvalid () {
178
+ c .Valid = false
179
+ }
180
+
181
+ func (r * GitRepositoryReconciler ) isCertificateDataValid (sslCertificateData map [string ][]byte ) bool {
182
+ certBytes , keyBytes := sslCertificateData ["certFile" ], sslCertificateData ["keyFile" ]
183
+ // Validate that both the certificate and key data are present
184
+ return len (certBytes ) > 0 && len (keyBytes ) > 0
185
+ }
186
+
187
+ func (h * GitClientHttpConfigurer ) backupHttpsTransport () {
188
+ h .DefaultTransport = gitclient .Protocols ["https" ]
189
+ }
190
+
191
+ func (h * GitClientHttpConfigurer ) ConfigureGitClient (ctx context.Context , obj * sourcev1.GitRepository ) {
192
+
193
+ if obj .Spec .SecretRef != nil {
194
+ // var secretName = obj.Spec.SecretRef.Name
195
+ // if secretName == "waf-authentication" {
196
+ sslCertificate := & corev1.Secret {
197
+ Data : h .SSLCertificateData ,
198
+ }
199
+ tlsConfig , _ , err := tls .TLSClientConfigFromSecret (* sslCertificate , "" )
200
+ if err != nil {
201
+ fmt .Println ("Error generating TLS config:" , err )
202
+ return
203
+ }
204
+ h .backupHttpsTransport ()
205
+
206
+ transportHttp , err := HttpTransportwithCustomCerts (tlsConfig , h .ProxyOpts , ctx )
207
+ if err != nil {
208
+ fmt .Println ("Error setting up transport:" , err )
209
+ return
210
+ }
211
+
212
+ gitclient .InstallProtocol ("https" , transportHttp )
213
+
214
+ }
215
+ // }
216
+ }
217
+
218
+
219
+
220
+ // configureHttpTransport sets up the HTTP transport configuration for the Git client.
221
+ func (r * GitRepositoryReconciler ) configureHttpTransport (ctx context.Context , obj * sourcev1.GitRepository ) (* GitClientHttpConfigurer , error ) {
222
+ httpTransportConfig := & GitClientHttpConfigurer {} // Initialize with defaults configuration
223
+
224
+ // Check if SecretRef is specified for the repository
225
+ if obj .Spec .SecretRef != nil {
226
+ // Fetch the SSL certificate data from the specified secret
227
+ sslCertificateData , err := r .getSecretData (ctx , obj .Spec .SecretRef .Name , obj .Namespace )
228
+ if err != nil {
229
+ return nil , fmt .Errorf ("failed to get secret '%s/%s': %w" , obj .Namespace , obj .Spec .SecretRef .Name , err )
230
+ }
231
+
232
+ // Set up the HTTP transport configuration with the fetched certificate data
233
+ httpTransportConfig .SSLCertificateData = sslCertificateData
234
+ if r .isCertificateDataValid (sslCertificateData ) {
235
+ httpTransportConfig .SetValid ()
236
+ } else {
237
+ httpTransportConfig .SetInvalid ()
238
+ }
239
+ } else {
240
+ // If no SecretRef is provided, mark the transport config as invalid or set defaults
241
+ httpTransportConfig .SetInvalid ()
242
+ }
243
+
244
+ return httpTransportConfig , nil
245
+ }
246
+
151
247
func (r * GitRepositoryReconciler ) SetupWithManagerAndOptions (mgr ctrl.Manager , opts GitRepositoryReconcilerOptions ) error {
152
248
r .patchOptions = getPatchOptions (gitRepositoryReadyCondition .Owned , r .ControllerName )
153
249
@@ -535,7 +631,12 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
535
631
// Persist the ArtifactSet.
536
632
* includes = * artifacts
537
633
538
- c , err := r .gitCheckout (ctx , obj , authOpts , proxyOpts , dir , true )
634
+ httpTransportConfig , err := r .configureHttpTransport (ctx , obj )
635
+ if err != nil {
636
+ return sreconcile .ResultEmpty , err
637
+ }
638
+
639
+ c , err := r .gitCheckout (ctx , obj , authOpts , proxyOpts , dir , true , httpTransportConfig )
539
640
if err != nil {
540
641
return sreconcile .ResultEmpty , err
541
642
}
@@ -579,7 +680,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
579
680
580
681
// If we can't skip the reconciliation, checkout again without any
581
682
// optimization.
582
- c , err := r .gitCheckout (ctx , obj , authOpts , proxyOpts , dir , false )
683
+ c , err := r .gitCheckout (ctx , obj , authOpts , proxyOpts , dir , false , httpTransportConfig )
583
684
if err != nil {
584
685
return sreconcile .ResultEmpty , err
585
686
}
@@ -832,7 +933,7 @@ func (r *GitRepositoryReconciler) reconcileInclude(ctx context.Context, sp *patc
832
933
// gitCheckout builds checkout options with the given configurations and
833
934
// performs a git checkout.
834
935
func (r * GitRepositoryReconciler ) gitCheckout (ctx context.Context , obj * sourcev1.GitRepository ,
835
- authOpts * git.AuthOptions , proxyOpts * transport.ProxyOptions , dir string , optimized bool ) (* git.Commit , error ) {
936
+ authOpts * git.AuthOptions , proxyOpts * transport.ProxyOptions , dir string , optimized bool , clientConf GitClientConfigurer ) (* git.Commit , error ) {
836
937
// Configure checkout strategy.
837
938
cloneOpts := repository.CloneConfig {
838
939
RecurseSubmodules : obj .Spec .RecurseSubmodules ,
@@ -866,6 +967,9 @@ func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context, obj *sourcev1
866
967
clientOpts = append (clientOpts , gogit .WithProxy (* proxyOpts ))
867
968
}
868
969
970
+ if clientConf .IsValid () {
971
+ clientConf .ConfigureGitClient (ctx , obj )
972
+ }
869
973
gitReader , err := gogit .NewClient (dir , authOpts , clientOpts ... )
870
974
if err != nil {
871
975
e := serror .NewGeneric (
0 commit comments