Skip to content

Commit

Permalink
Bug 1876913: Configure proxy via git config
Browse files Browse the repository at this point in the history
Configure HTTP/HTTPS proxy using git command line, rather than
environment variables. This is a work-around for
[BZ 1875639](https://bugzilla.redhat.com/show_bug.cgi?id=1875639).
  • Loading branch information
adambkaplan committed Sep 9, 2020
1 parent 1d92cbe commit 3d4a3a4
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions pkg/build/builder/cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,39 @@ func (c *builderConfig) setupGitEnvironment() (string, []string, error) {
}
gitEnv = append(gitEnv, secretsEnv...)
}
if gitSource.HTTPProxy != nil && len(*gitSource.HTTPProxy) > 0 {
gitEnv = append(gitEnv, fmt.Sprintf("HTTP_PROXY=%s", *gitSource.HTTPProxy))
gitEnv = append(gitEnv, fmt.Sprintf("http_proxy=%s", *gitSource.HTTPProxy))
}
if gitSource.HTTPSProxy != nil && len(*gitSource.HTTPSProxy) > 0 {
gitEnv = append(gitEnv, fmt.Sprintf("HTTPS_PROXY=%s", *gitSource.HTTPSProxy))
gitEnv = append(gitEnv, fmt.Sprintf("https_proxy=%s", *gitSource.HTTPSProxy))
}
// Bug 1875639: git commands fail if HTTP_PROXY and HTTPS_PROXY are set alongside
// NO_PROXY
if gitSource.NoProxy != nil && len(*gitSource.NoProxy) > 0 {
gitEnv = append(gitEnv, fmt.Sprintf("NO_PROXY=%s", *gitSource.NoProxy))
gitEnv = append(gitEnv, fmt.Sprintf("no_proxy=%s", *gitSource.NoProxy))
}
return c.sourceSecretDir, bld.MergeEnv(os.Environ(), gitEnv), nil
}

// setupProxyConfig sets up a global git proxy configuration with the provided git client.
// This is a work-around for Bug 1875639.
//
// See also: https://bugzilla.redhat.com/show_bug.cgi?id=1875639
func (c *builderConfig) setupProxyConfig(gitClient git.Repository) error {
gitSource := c.build.Spec.Source.Git
if gitSource == nil {
return nil
}
if gitSource.HTTPProxy != nil && len(*gitSource.HTTPProxy) > 0 {
err := gitClient.AddGlobalConfig("http.proxy", *gitSource.HTTPProxy)
if err != nil {
return err
}
}
if gitSource.HTTPSProxy != nil && len(*gitSource.HTTPSProxy) > 0 {
err := gitClient.AddGlobalConfig("https.proxy", *gitSource.HTTPSProxy)
if err != nil {
return err
}
}
return nil
}

// clone is responsible for cloning the source referenced in the buildconfig
func (c *builderConfig) clone() error {
ctx := timing.NewContext(context.Background())
Expand All @@ -247,7 +265,10 @@ func (c *builderConfig) clone() error {
defer os.RemoveAll(secretTmpDir)

gitClient := git.NewRepositoryWithEnv(gitEnv)

err = c.setupProxyConfig(gitClient)
if err != nil {
return err
}
buildDir := bld.InputContentPath
sourceInfo, err := bld.GitClone(ctx, gitClient, c.build.Spec.Source.Git, c.build.Spec.Revision, buildDir)
if err != nil {
Expand Down

0 comments on commit 3d4a3a4

Please sign in to comment.