diff options
author | Jacob Vosmaer <jacob@gitlab.com> | 2018-09-19 13:00:31 +0300 |
---|---|---|
committer | Jacob Vosmaer <jacob@gitlab.com> | 2018-09-19 13:00:31 +0300 |
commit | 3ed43d613d0e9131c33ba4f5cc15e6e3520429a8 (patch) | |
tree | f9d470e23f44ae28c32116b0b20609b537ed3c95 | |
parent | 889333e29c31ef19c421ca93e8655c950de30cf6 (diff) | |
parent | 244902bd255411913d8666e8f2d7590c4542b495 (diff) |
Merge branch 'sh-export-http-proxy' into 'master'
Export HTTP proxy environment variables to Gitaly
Closes gitlab-ce#51517
See merge request gitlab-org/gitaly!885
-rw-r--r-- | changelogs/unreleased/sh-export-http-proxy.yml | 5 | ||||
-rw-r--r-- | internal/command/command.go | 9 | ||||
-rw-r--r-- | internal/command/command_test.go | 49 | ||||
-rw-r--r-- | internal/git/command_test.go | 39 |
4 files changed, 102 insertions, 0 deletions
diff --git a/changelogs/unreleased/sh-export-http-proxy.yml b/changelogs/unreleased/sh-export-http-proxy.yml new file mode 100644 index 000000000..7ddd8e564 --- /dev/null +++ b/changelogs/unreleased/sh-export-http-proxy.yml @@ -0,0 +1,5 @@ +--- +title: Export HTTP proxy environment variables to Gitaly +merge_request: 885 +author: +type: fixed diff --git a/internal/command/command.go b/internal/command/command.go index 1e0ae018b..e57862ad6 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -31,6 +31,15 @@ var exportedEnvVars = []string{ "GIT_TRACE_PACKET", "GIT_TRACE_PERFORMANCE", "GIT_TRACE_SETUP", + + // Git HTTP proxy settings: https://git-scm.com/docs/git-config#git-config-httpproxy + "all_proxy", + "http_proxy", + "HTTP_PROXY", + "https_proxy", + "HTTPS_PROXY", + // libcurl settings: https://curl.haxx.se/libcurl/c/CURLOPT_NOPROXY.html + "no_proxy", } // Command encapsulates a running exec.Cmd. The embedded exec.Cmd is diff --git a/internal/command/command_test.go b/internal/command/command_test.go index 2c7495a7b..22337c27e 100644 --- a/internal/command/command_test.go +++ b/internal/command/command_test.go @@ -3,6 +3,7 @@ package command import ( "bytes" "context" + "fmt" "os" "os/exec" "strings" @@ -44,6 +45,54 @@ func TestNewCommandExtraEnv(t *testing.T) { require.Contains(t, strings.Split(buff.String(), "\n"), extraVar) } +func TestNewCommandProxyEnv(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + testCases := []struct { + key string + value string + }{ + { + key: "all_proxy", + value: "http://localhost:4000", + }, + { + key: "http_proxy", + value: "http://localhost:5000", + }, + { + key: "HTTP_PROXY", + value: "http://localhost:6000", + }, + { + key: "https_proxy", + value: "https://localhost:5000", + }, + { + key: "HTTPS_PROXY", + value: "https://localhost:6000", + }, + { + key: "no_proxy", + value: "https://excluded:5000", + }, + } + + for _, tc := range testCases { + t.Run(tc.key, func(t *testing.T) { + extraVar := fmt.Sprintf("%s=%s", tc.key, tc.value) + buff := &bytes.Buffer{} + cmd, err := New(ctx, exec.Command("/usr/bin/env"), nil, buff, nil, extraVar) + + require.NoError(t, err) + require.NoError(t, cmd.Wait()) + + require.Contains(t, strings.Split(buff.String(), "\n"), extraVar) + }) + } +} + func TestRejectEmptyContextDone(t *testing.T) { defer func() { p := recover() diff --git a/internal/git/command_test.go b/internal/git/command_test.go new file mode 100644 index 000000000..f915d3e73 --- /dev/null +++ b/internal/git/command_test.go @@ -0,0 +1,39 @@ +package git + +import ( + "context" + "io/ioutil" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGitCommandProxy(t *testing.T) { + requestReceived := false + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requestReceived = true + })) + defer ts.Close() + + oldHTTPProxy := os.Getenv("http_proxy") + defer os.Setenv("http_proxy", oldHTTPProxy) + + os.Setenv("http_proxy", ts.URL) + + ctx, _ := context.WithCancel(context.Background()) + + dir, err := ioutil.TempDir("", "test-clone") + require.NoError(t, err) + defer os.RemoveAll(dir) + + cmd, err := CommandWithoutRepo(ctx, "clone", "http://gitlab.com/bogus-repo", dir) + require.NoError(t, err) + + err = cmd.Wait() + require.NoError(t, err) + require.True(t, requestReceived) +} |