@@ -110,12 +110,18 @@ func (impl *GitManagerBaseImpl) Fetch(gitCtx GitContext, rootDir string) (respon
110110 impl .logger .Debugw ("git fetch " , "location" , rootDir )
111111 cmd , cancel := impl .createCmdWithContext (gitCtx , "git" , "-C" , rootDir , "fetch" , "origin" , "--tags" , "--force" )
112112 defer cancel ()
113- output , errMsg , err := impl .runCommandWithCred (cmd , gitCtx .Username , gitCtx .Password )
113+ tlsPathInfo , err := createFilesForTlsData (gitCtx )
114+ if err != nil {
115+ //making it non-blocking
116+ impl .logger .Errorw ("error encountered in createFilesForTlsData" , "err" , err )
117+ }
118+ defer impl .deleteTlsFiles (tlsPathInfo )
119+ output , errMsg , err := impl .runCommandWithCred (cmd , gitCtx .Username , gitCtx .Password , tlsPathInfo )
114120 if strings .Contains (output , LOCK_REF_MESSAGE ) {
115121 impl .logger .Info ("error in fetch, pruning local refs and retrying" , "rootDir" , rootDir )
116122 // running git remote prune origin and retrying fetch. gitHub issue - https://github.com/devtron-labs/devtron/issues/4605
117123 pruneCmd , pruneCmdCancel := impl .createCmdWithContext (gitCtx , "git" , "-C" , rootDir , "remote" , "prune" , "origin" )
118- pruneOutput , pruneMsg , pruneErr := impl .runCommandWithCred (pruneCmd , gitCtx .Username , gitCtx .Password )
124+ pruneOutput , pruneMsg , pruneErr := impl .runCommandWithCred (pruneCmd , gitCtx .Username , gitCtx .Password , tlsPathInfo )
119125 defer pruneCmdCancel ()
120126 if pruneErr != nil {
121127 impl .logger .Errorw ("error in pruning local refs that do not exist at remote" )
@@ -125,7 +131,7 @@ func (impl *GitManagerBaseImpl) Fetch(gitCtx GitContext, rootDir string) (respon
125131 retryFetchCmd , retryFetchCancel := impl .createCmdWithContext (gitCtx , "git" , "-C" , rootDir , "fetch" , "origin" , "--tags" , "--force" )
126132 defer retryFetchCancel ()
127133
128- output , errMsg , err = impl .runCommandWithCred (retryFetchCmd , gitCtx .Username , gitCtx .Password )
134+ output , errMsg , err = impl .runCommandWithCred (retryFetchCmd , gitCtx .Username , gitCtx .Password , tlsPathInfo )
129135 }
130136 impl .logger .Debugw ("fetch output" , "root" , rootDir , "opt" , output , "errMsg" , errMsg , "error" , err )
131137 return output , errMsg , err
@@ -165,12 +171,33 @@ func (impl *GitManagerBaseImpl) LogMergeBase(gitCtx GitContext, rootDir, from st
165171 return commits , nil
166172}
167173
168- func (impl * GitManagerBaseImpl ) runCommandWithCred (cmd * exec.Cmd , userName , password string ) (response , errMsg string , err error ) {
174+ func (impl * GitManagerBaseImpl ) runCommandWithCred (cmd * exec.Cmd , userName , password string , tlsPathInfo * TlsPathInfo ) (response , errMsg string , err error ) {
169175 cmd .Env = append (os .Environ (),
170176 fmt .Sprintf ("GIT_ASKPASS=%s" , GIT_ASK_PASS ),
171177 fmt .Sprintf ("GIT_USERNAME=%s" , userName ),
172178 fmt .Sprintf ("GIT_PASSWORD=%s" , password ),
173179 )
180+ if tlsPathInfo != nil {
181+ if tlsPathInfo .TlsKeyPath != "" && tlsPathInfo .TlsCertPath != "" {
182+ cmd .Env = append (cmd .Env ,
183+ fmt .Sprintf ("GIT_SSL_KEY=%s" , tlsPathInfo .TlsKeyPath ),
184+ fmt .Sprintf ("GIT_SSL_CERT=%s" , tlsPathInfo .TlsCertPath ))
185+ }
186+ if tlsPathInfo .CaCertPath != "" {
187+ cmd .Env = append (cmd .Env , fmt .Sprintf ("GIT_SSL_CAINFO=%s" , tlsPathInfo .CaCertPath ))
188+ }
189+ }
190+ return impl .runCommand (cmd )
191+ }
192+
193+ func (impl * GitManagerBaseImpl ) runCommandWithTlsData (cmd * exec.Cmd , tlsPathInfo * TlsPathInfo ) (response , errMsg string , err error ) {
194+ if tlsPathInfo != nil {
195+ cmd .Env = append (os .Environ (),
196+ fmt .Sprintf ("GIT_SSL_CAINFO=%s" , tlsPathInfo .CaCertPath ),
197+ fmt .Sprintf ("GIT_SSL_KEY=%s" , tlsPathInfo .TlsKeyPath ),
198+ fmt .Sprintf ("GIT_SSL_CERT=%s" , tlsPathInfo .TlsCertPath ),
199+ )
200+ }
174201 return impl .runCommand (cmd )
175202}
176203
@@ -296,8 +323,13 @@ func (impl *GitManagerBaseImpl) FetchDiffStatBetweenCommits(gitCtx GitContext, o
296323 }
297324 cmd , cancel := impl .createCmdWithContext (gitCtx , "git" , "-C" , rootDir , "diff" , "--numstat" , oldHash , newHash )
298325 defer cancel ()
299-
300- output , errMsg , err := impl .runCommandWithCred (cmd , gitCtx .Username , gitCtx .Password )
326+ tlsPathInfo , err := createFilesForTlsData (gitCtx )
327+ if err != nil {
328+ //making it non-blocking
329+ impl .logger .Errorw ("error encountered in createFilesForTlsData" , "err" , err )
330+ }
331+ defer impl .deleteTlsFiles (tlsPathInfo )
332+ output , errMsg , err := impl .runCommandWithCred (cmd , gitCtx .Username , gitCtx .Password , tlsPathInfo )
301333 impl .logger .Debugw ("root" , rootDir , "opt" , output , "errMsg" , errMsg , "error" , err )
302334 if err != nil || len (errMsg ) > 0 {
303335 impl .logger .Errorw ("error in fetching fileStat diff btw commits: " , "oldHash" , oldHash , "newHash" , newHash , "checkoutPath" , rootDir , "errorMsg" , errMsg , "err" , err )
@@ -333,6 +365,63 @@ func (impl *GitManagerBaseImpl) getCommandTimeout(command string) int {
333365func (impl * GitManagerBaseImpl ) ExecuteCustomCommand (gitContext GitContext , name string , arg ... string ) (response , errMsg string , err error ) {
334366 cmd , cancel := impl .createCmdWithContext (gitContext , name , arg ... )
335367 defer cancel ()
336- output , errMsg , err := impl .runCommandWithCred (cmd , gitContext .Username , gitContext .Password )
368+ tlsPathInfo , err := createFilesForTlsData (gitContext )
369+ if err != nil {
370+ //making it non-blocking
371+ impl .logger .Errorw ("error encountered in createFilesForTlsData" , "err" , err )
372+ }
373+ defer impl .deleteTlsFiles (tlsPathInfo )
374+ output , errMsg , err := impl .runCommandWithCred (cmd , gitContext .Username , gitContext .Password , tlsPathInfo )
337375 return output , errMsg , err
338376}
377+
378+ func createFilesForTlsData (gitContext GitContext ) (* TlsPathInfo , error ) {
379+ var tlsKeyFilePath string
380+ var tlsCertFilePath string
381+ var caCertFilePath string
382+ var err error
383+ if gitContext .TLSKey != "" && gitContext .TLSCertificate != "" {
384+ tlsKeyFilePath , err = CreateTlsPathFilesWithData (gitContext .GitProviderId , gitContext .TLSKey , TLS_KEY_FILE_NAME )
385+ if err != nil {
386+ return nil , err
387+ }
388+ tlsCertFilePath , err = CreateTlsPathFilesWithData (gitContext .GitProviderId , gitContext .TLSCertificate , TLS_CERT_FILE_NAME )
389+ if err != nil {
390+ return nil , err
391+ }
392+ }
393+ if gitContext .CACert != "" {
394+ caCertFilePath , err = CreateTlsPathFilesWithData (gitContext .GitProviderId , gitContext .CACert , CA_CERT_FILE_NAME )
395+ if err != nil {
396+ return nil , err
397+ }
398+ }
399+ return BuildTlsInfoPath (caCertFilePath , tlsKeyFilePath , tlsCertFilePath ), nil
400+
401+ }
402+
403+ func (impl * GitManagerBaseImpl ) deleteTlsFiles (pathInfo * TlsPathInfo ) {
404+ if pathInfo == nil {
405+ return
406+ }
407+ if pathInfo .TlsKeyPath != "" {
408+ err := DeleteAFileIfExists (pathInfo .TlsKeyPath )
409+ if err != nil {
410+ impl .logger .Errorw ("error in deleting file" , "tlsKeyPath" , pathInfo .TlsKeyPath , "err" , err )
411+ }
412+ }
413+
414+ if pathInfo .TlsCertPath != "" {
415+ err := DeleteAFileIfExists (pathInfo .TlsCertPath )
416+ if err != nil {
417+ impl .logger .Errorw ("error in deleting file" , "TlsCertPath" , pathInfo .TlsCertPath , "err" , err )
418+ }
419+ }
420+ if pathInfo .CaCertPath != "" {
421+ err := DeleteAFileIfExists (pathInfo .CaCertPath )
422+ if err != nil {
423+ impl .logger .Errorw ("error in deleting file" , "CaCertPath" , pathInfo .CaCertPath , "err" , err )
424+ }
425+ }
426+ return
427+ }
0 commit comments