Skip to content

Commit

Permalink
Remove custom http transport in git resolver
Browse files Browse the repository at this point in the history
- The default http transport will handle proxy logic so there isnt a
  need for us to create our own transport
  • Loading branch information
tomkennedy513 committed Jun 7, 2023
1 parent 44ebcf9 commit cf11558
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 59 deletions.
33 changes: 0 additions & 33 deletions pkg/git/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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"`
}
Expand Down
13 changes: 1 addition & 12 deletions pkg/git/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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")
})
})
}

Expand Down
14 changes: 0 additions & 14 deletions pkg/git/remote_git_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
})
Expand Down

0 comments on commit cf11558

Please sign in to comment.