Skip to content

Commit ac59cbf

Browse files
authored
Merge pull request #22897 from github/go/proxy-git-cainfo-env-pr2
Go: Pass proxy certificate to git via GIT_SSL_CAINFO
2 parents 1bc4e40 + 18094d8 commit ac59cbf

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎go/extractor/registries/registryproxy.go‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ func getEnvVars() []string {
103103
slog.Error("Failed to close the temporary certificate file", slog.String("error", err.Error()))
104104
} else {
105105
proxy_cert_file = f.Name()
106-
result = append(result, fmt.Sprintf("SSL_CERT_FILE=%s", proxy_cert_file))
106+
// `SSL_CERT_FILE` is used by Go itself; `GIT_SSL_CAINFO` is used by the `git`
107+
// processes that `go` spawns to fetch `git_source` modules.
108+
result = append(
109+
result,
110+
fmt.Sprintf("SSL_CERT_FILE=%s", proxy_cert_file),
111+
fmt.Sprintf("GIT_SSL_CAINFO=%s", proxy_cert_file),
112+
)
107113
}
108114
}
109115

@@ -160,18 +166,6 @@ func getEnvVars() []string {
160166

161167
if len(git_sources) > 0 {
162168
goprivate = append(goprivate, git_sources...)
163-
164-
if proxy_cert_file != "" {
165-
slog.Info("Configuring `git` to use proxy certificate", slog.String("path", proxy_cert_file))
166-
cmd := exec.Command("git", "config", "--global", "http.sslCAInfo", proxy_cert_file)
167-
168-
out, cmdErr := cmd.CombinedOutput()
169-
slog.Info(string(out))
170-
171-
if cmdErr != nil {
172-
slog.Error("Failed to configure `git` to accept the certificate file", slog.String("error", cmdErr.Error()))
173-
}
174-
}
175169
}
176170

177171
result = append(result, fmt.Sprintf("GOPRIVATE=%s", strings.Join(goprivate, ",")))

‎go/extractor/registries/registryproxy_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package registries
22

33
import (
4+
"os"
5+
"slices"
46
"testing"
57
)
68

@@ -131,3 +133,23 @@ func TestParseRegistryConfigsMultipleGoProxyServers(t *testing.T) {
131133
t.Fatalf("Expected `URL` to be `https://goproxy.io`, but got `%s`", second.URL)
132134
}
133135
}
136+
137+
func TestGetEnvVarsScopesCertificateToSubprocesses(t *testing.T) {
138+
t.Setenv(PROXY_HOST, "")
139+
t.Setenv(PROXY_PORT, "")
140+
t.Setenv(PROXY_URLS, "")
141+
t.Setenv(PROXY_CA_CERTIFICATE, "dummy certificate")
142+
143+
vars := getEnvVars()
144+
defer os.Remove(proxy_cert_file)
145+
146+
if proxy_cert_file == "" {
147+
t.Fatal("Expected a temporary certificate file to be created.")
148+
}
149+
150+
for _, expected := range []string{"SSL_CERT_FILE=" + proxy_cert_file, "GIT_SSL_CAINFO=" + proxy_cert_file} {
151+
if !slices.Contains(vars, expected) {
152+
t.Errorf("Expected `getEnvVars` to return %q, but got %v.", expected, vars)
153+
}
154+
}
155+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: fix
3+
---
4+
* The Go extractor no longer modifies the global Git configuration when private `git_source` registries are configured. The registry proxy certificate is now passed to Git through the `GIT_SSL_CAINFO` environment variable instead, which only affects the Git processes spawned by `go`.

0 commit comments

Comments
 (0)