Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions go/extractor/registries/registryproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ func getEnvVars() []string {
slog.Error("Failed to close the temporary certificate file", slog.String("error", err.Error()))
} else {
proxy_cert_file = f.Name()
result = append(result, fmt.Sprintf("SSL_CERT_FILE=%s", proxy_cert_file))
// `SSL_CERT_FILE` is used by Go itself; `GIT_SSL_CAINFO` is used by the `git`
// processes that `go` spawns to fetch `git_source` modules. Setting it here scopes
// the certificate to our subprocesses instead of modifying the global git configuration.
result = append(
result,
fmt.Sprintf("SSL_CERT_FILE=%s", proxy_cert_file),
fmt.Sprintf("GIT_SSL_CAINFO=%s", proxy_cert_file),
)
}
}

Expand Down Expand Up @@ -160,18 +167,6 @@ func getEnvVars() []string {

if len(git_sources) > 0 {
goprivate = append(goprivate, git_sources...)

if proxy_cert_file != "" {
slog.Info("Configuring `git` to use proxy certificate", slog.String("path", proxy_cert_file))
cmd := exec.Command("git", "config", "--global", "http.sslCAInfo", proxy_cert_file)

out, cmdErr := cmd.CombinedOutput()
slog.Info(string(out))

if cmdErr != nil {
slog.Error("Failed to configure `git` to accept the certificate file", slog.String("error", cmdErr.Error()))
}
}
}

result = append(result, fmt.Sprintf("GOPRIVATE=%s", strings.Join(goprivate, ",")))
Expand Down
22 changes: 22 additions & 0 deletions go/extractor/registries/registryproxy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package registries

import (
"os"
"slices"
"testing"
)

Expand Down Expand Up @@ -131,3 +133,23 @@ func TestParseRegistryConfigsMultipleGoProxyServers(t *testing.T) {
t.Fatalf("Expected `URL` to be `https://goproxy.io`, but got `%s`", second.URL)
}
}

func TestGetEnvVarsScopesCertificateToSubprocesses(t *testing.T) {
t.Setenv(PROXY_HOST, "")
t.Setenv(PROXY_PORT, "")
t.Setenv(PROXY_URLS, "")
t.Setenv(PROXY_CA_CERTIFICATE, "dummy certificate")

vars := getEnvVars()
defer os.Remove(proxy_cert_file)

if proxy_cert_file == "" {
t.Fatal("Expected a temporary certificate file to be created.")
}

for _, expected := range []string{"SSL_CERT_FILE=" + proxy_cert_file, "GIT_SSL_CAINFO=" + proxy_cert_file} {
if !slices.Contains(vars, expected) {
t.Errorf("Expected `getEnvVars` to return %q, but got %v.", expected, vars)
}
}
}
Loading