Skip to content

Commit d37f522

Browse files
committed
Determine existing sslCAInfo config and restore it after a command has run
1 parent a48745a commit d37f522

1 file changed

Lines changed: 66 additions & 13 deletions

File tree

‎go/extractor/registries/registryproxy.go‎

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,57 @@ func parseRegistryConfigs(str string) ([]RegistryConfig, error) {
6767
return configs, nil
6868
}
6969

70+
// The previous `http.sslCAInfo` setting that should be restored after a command is run, if any.
71+
var previous_sslCAInfo string = ""
72+
73+
// Tries to identify a current value for the `http.sslCAInfo` setting.
74+
func getCurrentGitSslInfo() string {
75+
slog.Debug("Checking for current `git` certificate configuration")
76+
cmd := exec.Command("git", "config", "get", "--global", "http.sslCAInfo")
77+
out, err := cmd.CombinedOutput()
78+
79+
if err == nil {
80+
val := strings.TrimSpace(string(out))
81+
82+
if val != "" {
83+
slog.Info("Determined current `git` certificate configuration", slog.String("value", val))
84+
return val
85+
}
86+
} else {
87+
slog.Warn("Unable to determine current `git` certificate configuration", slog.String("error", err.Error()))
88+
}
89+
90+
return ""
91+
}
92+
93+
// Applies a `http.sslCAInfo` setting for `git`.
94+
func applyGitSslInfo(val string) {
95+
cmd := exec.Command("git", "config", "--global", "http.sslCAInfo", val)
96+
97+
out, cmdErr := cmd.CombinedOutput()
98+
slog.Info(string(out))
99+
100+
if cmdErr != nil {
101+
slog.Error("Failed to configure `http.sslCAInfo` for `git`", slog.String("path", val), slog.String("error", cmdErr.Error()))
102+
}
103+
}
104+
105+
// Configures `git` to trust the registry proxy.
106+
func applyProxyCertificateToGit() {
107+
slog.Info("Configuring `git` to use proxy certificate", slog.String("path", proxy_cert_file))
108+
applyGitSslInfo(proxy_cert_file)
109+
}
110+
111+
// Restores the `http.sslCAInfo` setting for `git` to its original setting, if any.
112+
func RestoreGitSslInfo() {
113+
if previous_sslCAInfo == "" {
114+
return
115+
}
116+
117+
slog.Info("Restoring `http.sslCAInfo` setting for `git`", slog.String("path", previous_sslCAInfo))
118+
applyGitSslInfo(previous_sslCAInfo)
119+
}
120+
70121
func getEnvVars() []string {
71122
var result []string
72123

@@ -162,17 +213,8 @@ func getEnvVars() []string {
162213
if len(git_sources) > 0 {
163214
goprivate = append(goprivate, git_sources...)
164215

165-
if proxy_cert_file != "" {
166-
slog.Info("Configuring `git` to use proxy certificate", slog.String("path", proxy_cert_file))
167-
cmd := exec.Command("git", "config", "--global", "http.sslCAInfo", proxy_cert_file)
168-
169-
out, cmdErr := cmd.CombinedOutput()
170-
slog.Info(string(out))
171-
172-
if cmdErr != nil {
173-
slog.Error("Failed to configure `git` to accept the certificate file", slog.String("error", cmdErr.Error()))
174-
}
175-
}
216+
// Determine if `git` has an existing `http.sslCAInfo` config and store it.
217+
previous_sslCAInfo = getCurrentGitSslInfo()
176218
}
177219

178220
result = append(result, fmt.Sprintf("GOPRIVATE=%s", strings.Join(goprivate, ",")))
@@ -183,7 +225,7 @@ func getEnvVars() []string {
183225
}
184226

185227
// Applies private package proxy related environment variables to `cmd`.
186-
func ApplyProxyEnvVars(cmd *exec.Cmd) {
228+
func applyProxyEnvVars(cmd *exec.Cmd) {
187229
// If we haven't done so yet, check whether the proxy environment variables are set
188230
// and extract information from them.
189231
if !proxy_vars_checked {
@@ -220,7 +262,18 @@ func Command(name string, arg ...string) *ProxiedCmd {
220262
// with the underlying command as argument.
221263
func run[R any](proxiedCmd *ProxiedCmd, fn func(cmd *exec.Cmd) R) R {
222264
// Apply registry proxy environment variables to the command.
223-
ApplyProxyEnvVars(proxiedCmd.cmd)
265+
applyProxyEnvVars(proxiedCmd.cmd)
266+
267+
// If there are `git_source` registries, configure `git` to trust the proxy certificate.
268+
if len(git_sources) > 0 {
269+
applyProxyCertificateToGit()
270+
}
271+
272+
// Clean up after the command has run.
273+
defer func() {
274+
// Restore the previous `http.sslCAInfo` config for `git` if there was one.
275+
RestoreGitSslInfo()
276+
}()
224277

225278
return fn(proxiedCmd.cmd)
226279
}

0 commit comments

Comments
 (0)