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

     1  package git
     2  
     3  import (
     4  	"gopkg.in/src-d/go-git.v4"
     5  	"gopkg.in/src-d/go-git.v4/plumbing"
     6  	"gopkg.in/src-d/go-git.v4/plumbing/transport"
     7  	"gopkg.in/src-d/go-git.v4/plumbing/transport/client"
     8  	"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
     9  	"gopkg.in/src-d/go-git.v4/utils/ioutil"
    10  )
    11  
    12  // 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.
    13  // 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
    14  // transport with InsecureSkipVerify flag is verification should be disabled.
    15  
    16  func newUploadPackSession(url string, auth transport.AuthMethod, insecure bool, creds Creds) (transport.UploadPackSession, error) {
    17  	c, ep, err := newClient(url, insecure, creds)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	return c.NewUploadPackSession(ep, auth)
    23  }
    24  
    25  func newClient(url string, insecure bool, creds Creds) (transport.Transport, *transport.Endpoint, error) {
    26  	ep, err := transport.NewEndpoint(url)
    27  	if err != nil {
    28  		return nil, nil, err
    29  	}
    30  
    31  	var c transport.Transport
    32  	// For HTTPS repositories, we get a custom Transport. Everything else will
    33  	// be default.
    34  	if IsHTTPSURL(url) {
    35  		c = http.NewClient(GetRepoHTTPClient(url, insecure, creds))
    36  	} else {
    37  		c, err = client.NewClient(ep)
    38  		if err != nil {
    39  			return nil, nil, err
    40  		}
    41  	}
    42  	return c, ep, err
    43  }
    44  
    45  func listRemote(r *git.Remote, o *git.ListOptions, insecure bool, creds Creds) (rfs []*plumbing.Reference, err error) {
    46  	s, err := newUploadPackSession(r.Config().URLs[0], o.Auth, insecure, creds)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	defer ioutil.CheckClose(s, &err)
    52  
    53  	ar, err := s.AdvertisedReferences()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	allRefs, err := ar.AllReferences()
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	refs, err := allRefs.IterReferences()
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	var resultRefs []*plumbing.Reference
    69  	_ = refs.ForEach(func(ref *plumbing.Reference) error {
    70  		resultRefs = append(resultRefs, ref)
    71  		return nil
    72  	})
    73  
    74  	return resultRefs, nil
    75  }