From cf115589eb9379c49d73c5b744e85f15c02ef924 Mon Sep 17 00:00:00 2001 From: Tom Kennedy Date: Tue, 6 Jun 2023 15:55:22 -0400 Subject: [PATCH] Remove custom http transport in git resolver - The default http transport will handle proxy logic so there isnt a need for us to create our own transport --- pkg/git/fetch.go | 33 --------------------------------- pkg/git/fetch_test.go | 13 +------------ pkg/git/remote_git_resolver.go | 14 -------------- 3 files changed, 1 insertion(+), 59 deletions(-) diff --git a/pkg/git/fetch.go b/pkg/git/fetch.go index 624ed21d3..999ca6568 100644 --- a/pkg/git/fetch.go +++ b/pkg/git/fetch.go @@ -2,19 +2,14 @@ package git import ( "log" - "net/http" - "net/url" "os" "path" - "time" "github.com/BurntSushi/toml" gogit "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/transport" - "github.com/go-git/go-git/v5/plumbing/transport/client" - githttp "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/pkg/errors" ) @@ -43,12 +38,6 @@ func (f Fetcher) Fetch(dir, gitURL, gitRevision, metadataDir string) error { return errors.Wrap(err, "creating remote") } - httpsTransport, err := getHttpsTransport() - if err != nil { - return err - } - client.InstallProtocol("https", httpsTransport) - err = remote.Fetch(&gogit.FetchOptions{ RefSpecs: []config.RefSpec{"refs/*:refs/*"}, Auth: auth, @@ -74,7 +63,6 @@ func (f Fetcher) Fetch(dir, gitURL, gitRevision, metadataDir string) error { return errors.Wrapf(err, "checking out revision") } - // Write the git revision to the metadata directory projectMetadataFile, err := os.Create(path.Join(metadataDir, "project-metadata.toml")) if err != nil { return errors.Wrapf(err, "invalid metadata destination '%s/project-metadata.toml' for git repository: %s", metadataDir, gitURL) @@ -101,27 +89,6 @@ func (f Fetcher) Fetch(dir, gitURL, gitRevision, metadataDir string) error { return nil } -func getHttpsTransport() (transport.Transport, error) { - if httpsProxy, exists := os.LookupEnv("HTTPS_PROXY"); exists { - parsedUrl, err := url.Parse(httpsProxy) - if err != nil { - return nil, errors.Wrap(err, "parsing HTTPS_PROXY url") - } - proxyClient := &http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyURL(parsedUrl), - }, - Timeout: 15 * time.Second, - CheckRedirect: func(req *http.Request, via []*http.Request) error { - return http.ErrUseLastResponse - }, - } - return githttp.NewClient(proxyClient), nil - } else { - return githttp.DefaultClient, nil - } -} - type project struct { Source source `toml:"source"` } diff --git a/pkg/git/fetch_test.go b/pkg/git/fetch_test.go index aceb6e643..6695372b5 100644 --- a/pkg/git/fetch_test.go +++ b/pkg/git/fetch_test.go @@ -15,7 +15,7 @@ import ( ) func TestGitCheckout(t *testing.T) { - spec.Run(t, "Test Describe Image", testGitCheckout) + spec.Run(t, "Test Git Checkout", testGitCheckout) } func testGitCheckout(t *testing.T, when spec.G, it spec.S) { @@ -35,8 +35,6 @@ func testGitCheckout(t *testing.T, when spec.G, it spec.S) { metadataDir, err = os.MkdirTemp("", "test-git") require.NoError(t, err) - - require.NoError(t, os.Unsetenv("HTTPS_PROXY")) }) it.After(func() { @@ -108,15 +106,6 @@ func testGitCheckout(t *testing.T, when spec.G, it spec.S) { err := fetcher.Fetch(testDir, "git@bitbucket.com:org/repo", "main", metadataDir) require.ErrorContains(t, err, "unable to fetch references for repository") }) - - it("uses the http proxy env vars", func() { - require.NoError(t, os.Setenv("HTTPS_PROXY", "http://invalid-proxy")) - defer os.Unsetenv("HTTPS_PROXY") - - err := fetcher.Fetch(testDir, "https://github.com/git-fixtures/basic", "master", metadataDir) - require.Error(t, err) - require.Contains(t, err.Error(), "proxyconnect tcp: dial tcp") - }) }) } diff --git a/pkg/git/remote_git_resolver.go b/pkg/git/remote_git_resolver.go index b9aad4c44..6aa366403 100644 --- a/pkg/git/remote_git_resolver.go +++ b/pkg/git/remote_git_resolver.go @@ -5,7 +5,6 @@ import ( "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/transport" - "github.com/go-git/go-git/v5/plumbing/transport/client" "github.com/go-git/go-git/v5/storage/memory" corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1" @@ -21,19 +20,6 @@ func (*remoteGitResolver) Resolve(auth transport.AuthMethod, sourceConfig corev1 URLs: []string{sourceConfig.Git.URL}, }) - httpsTransport, err := getHttpsTransport() - if err != nil { - return corev1alpha1.ResolvedSourceConfig{ - Git: &corev1alpha1.ResolvedGitSource{ - URL: sourceConfig.Git.URL, - Revision: sourceConfig.Git.Revision, - Type: corev1alpha1.Unknown, - SubPath: sourceConfig.SubPath, - }, - }, nil - } - client.InstallProtocol("https", httpsTransport) - refs, err := remote.List(&gogit.ListOptions{ Auth: auth, })