github.com/argoproj/argo-cd/v3@v3.2.1/util/git/workaround.go (about)

     1  package git
     2  
     3  import (
     4  	"fmt"
     5  	neturl "net/url"
     6  
     7  	"github.com/go-git/go-git/v5"
     8  	"github.com/go-git/go-git/v5/plumbing"
     9  	"github.com/go-git/go-git/v5/plumbing/transport"
    10  	"github.com/go-git/go-git/v5/plumbing/transport/client"
    11  	"github.com/go-git/go-git/v5/plumbing/transport/http"
    12  	"github.com/go-git/go-git/v5/utils/ioutil"
    13  )
    14  
    15  // Below is a workaround for https://github.com/src-d/go-git/issues/1177: the `github.com/src-d/go-git` does not support disable SSL cert verification is a single repo.
    16  // As workaround methods `newUploadPackSession`, `newClient` and `listRemote` were copied from https://github.com/src-d/go-git/blob/master/remote.go and modified to use
    17  // transport with InsecureSkipVerify flag is verification should be disabled.
    18  
    19  func newUploadPackSession(url string, auth transport.AuthMethod, insecure bool, creds Creds, proxy string, noProxy string) (transport.UploadPackSession, error) {
    20  	c, ep, err := newClient(url, insecure, creds, proxy, noProxy)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	return c.NewUploadPackSession(ep, auth)
    26  }
    27  
    28  func newClient(url string, insecure bool, creds Creds, proxy string, noProxy string) (transport.Transport, *transport.Endpoint, error) {
    29  	ep, err := transport.NewEndpoint(url)
    30  	if err != nil {
    31  		return nil, nil, err
    32  	}
    33  
    34  	if !IsHTTPSURL(url) && !IsHTTPURL(url) {
    35  		// use the default client for protocols other than HTTP/HTTPS
    36  		ep.InsecureSkipTLS = insecure
    37  		if proxy != "" {
    38  			parsedProxyURL, err := neturl.Parse(proxy)
    39  			if err != nil {
    40  				return nil, nil, fmt.Errorf("failed to create client for url '%s', error parsing proxy url '%s': %w", url, proxy, err)
    41  			}
    42  			var proxyUsername, proxyPasswd string
    43  			if parsedProxyURL.User != nil {
    44  				proxyUsername = parsedProxyURL.User.Username()
    45  				proxyPasswd, _ = parsedProxyURL.User.Password()
    46  			}
    47  			ep.Proxy = transport.ProxyOptions{
    48  				URL:      fmt.Sprintf("%s://%s:%s", parsedProxyURL.Scheme, parsedProxyURL.Hostname(), parsedProxyURL.Port()),
    49  				Username: proxyUsername,
    50  				Password: proxyPasswd,
    51  			}
    52  		}
    53  		c, err := client.NewClient(ep)
    54  		if err != nil {
    55  			return nil, nil, err
    56  		}
    57  		return c, ep, nil
    58  	}
    59  
    60  	return http.NewClient(GetRepoHTTPClient(url, insecure, creds, proxy, noProxy)), ep, nil
    61  }
    62  
    63  func listRemote(r *git.Remote, o *git.ListOptions, insecure bool, creds Creds, proxy string, noProxy string) (rfs []*plumbing.Reference, err error) {
    64  	s, err := newUploadPackSession(r.Config().URLs[0], o.Auth, insecure, creds, proxy, noProxy)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	defer ioutil.CheckClose(s, &err)
    70  
    71  	ar, err := s.AdvertisedReferences()
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	allRefs, err := ar.AllReferences()
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	refs, err := allRefs.IterReferences()
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	var resultRefs []*plumbing.Reference
    87  	_ = refs.ForEach(func(ref *plumbing.Reference) error {
    88  		if ref.Name().IsTag() {
    89  			if peeled, ok := ar.Peeled[ref.Name().String()]; ok {
    90  				resultRefs = append(resultRefs, plumbing.NewHashReference(ref.Name(), peeled))
    91  				return nil
    92  			}
    93  		}
    94  		resultRefs = append(resultRefs, ref)
    95  		return nil
    96  	})
    97  
    98  	return resultRefs, nil
    99  }