Skip to content

Commit fd6f9cd

Browse files
authored
Merge pull request #20368 from github/mbg/go/support-git-source
Go: Support `git_source`
2 parents e78d057 + 8c84992 commit fd6f9cd

File tree

2 files changed

+82
-14
lines changed

2 files changed

+82
-14
lines changed

go/extractor/util/registryproxy.go

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"log/slog"
7+
"net/url"
78
"os"
89
"os/exec"
910
"strings"
@@ -14,6 +15,7 @@ const PROXY_PORT = "CODEQL_PROXY_PORT"
1415
const PROXY_CA_CERTIFICATE = "CODEQL_PROXY_CA_CERTIFICATE"
1516
const PROXY_URLS = "CODEQL_PROXY_URLS"
1617
const GOPROXY_SERVER = "goproxy_server"
18+
const GIT_SOURCE = "git_source"
1719

1820
type RegistryConfig struct {
1921
Type string `json:"type"`
@@ -26,9 +28,11 @@ var proxy_address string
2628
// The path to the temporary file that stores the proxy certificate, if any.
2729
var proxy_cert_file string
2830

29-
// An array of registry configurations that are relevant to Go.
30-
// This excludes other registry configurations that may be available, but are not relevant to Go.
31-
var proxy_configs []RegistryConfig
31+
// An array of goproxy server URLs.
32+
var goproxy_servers []string
33+
34+
// An array of Git URLs.
35+
var git_sources []string
3236

3337
// Stores the environment variables that we wish to pass on to `go` commands.
3438
var proxy_vars []string = nil
@@ -53,7 +57,13 @@ func getEnvVars() []string {
5357
if proxy_host, proxy_host_set := os.LookupEnv(PROXY_HOST); proxy_host_set && proxy_host != "" {
5458
if proxy_port, proxy_port_set := os.LookupEnv(PROXY_PORT); proxy_port_set && proxy_port != "" {
5559
proxy_address = fmt.Sprintf("http://%s:%s", proxy_host, proxy_port)
56-
result = append(result, fmt.Sprintf("HTTP_PROXY=%s", proxy_address), fmt.Sprintf("HTTPS_PROXY=%s", proxy_address))
60+
result = append(
61+
result,
62+
fmt.Sprintf("HTTP_PROXY=%s", proxy_address),
63+
fmt.Sprintf("HTTPS_PROXY=%s", proxy_address),
64+
fmt.Sprintf("http_proxy=%s", proxy_address),
65+
fmt.Sprintf("https_proxy=%s", proxy_address),
66+
)
5767

5868
slog.Info("Found private registry proxy", slog.String("proxy_address", proxy_address))
5969
}
@@ -91,20 +101,49 @@ func getEnvVars() []string {
91101
// filter others out at this point.
92102
for _, cfg := range val {
93103
if cfg.Type == GOPROXY_SERVER {
94-
proxy_configs = append(proxy_configs, cfg)
104+
goproxy_servers = append(goproxy_servers, cfg.URL)
95105
slog.Info("Found GOPROXY server", slog.String("url", cfg.URL))
106+
} else if cfg.Type == GIT_SOURCE {
107+
parsed, err := url.Parse(cfg.URL)
108+
if err == nil && parsed.Hostname() != "" {
109+
git_source := parsed.Hostname() + parsed.Path + "*"
110+
git_sources = append(git_sources, git_source)
111+
slog.Info("Found Git source", slog.String("source", git_source))
112+
} else {
113+
slog.Warn("Not a valid URL for Git source", slog.String("url", cfg.URL))
114+
}
96115
}
97116
}
98117

99-
if len(proxy_configs) > 0 {
118+
goprivate := []string{}
119+
120+
if len(goproxy_servers) > 0 {
100121
goproxy_val := "https://proxy.golang.org,direct"
101122

102-
for _, cfg := range proxy_configs {
103-
goproxy_val = cfg.URL + "," + goproxy_val
123+
for _, url := range goproxy_servers {
124+
goproxy_val = url + "," + goproxy_val
104125
}
105126

106-
result = append(result, fmt.Sprintf("GOPROXY=%s", goproxy_val), "GOPRIVATE=", "GONOPROXY=")
127+
result = append(result, fmt.Sprintf("GOPROXY=%s", goproxy_val), "GONOPROXY=")
128+
}
129+
130+
if len(git_sources) > 0 {
131+
goprivate = append(goprivate, git_sources...)
132+
133+
if proxy_cert_file != "" {
134+
slog.Info("Configuring `git` to use proxy certificate", slog.String("path", proxy_cert_file))
135+
cmd := exec.Command("git", "config", "--global", "http.sslCAInfo", proxy_cert_file)
136+
137+
out, cmdErr := cmd.CombinedOutput()
138+
slog.Info(string(out))
139+
140+
if cmdErr != nil {
141+
slog.Error("Failed to configure `git` to accept the certificate file", slog.String("error", cmdErr.Error()))
142+
}
143+
}
107144
}
145+
146+
result = append(result, fmt.Sprintf("GOPRIVATE=%s", strings.Join(goprivate, ",")))
108147
}
109148
}
110149

@@ -113,11 +152,6 @@ func getEnvVars() []string {
113152

114153
// Applies private package proxy related environment variables to `cmd`.
115154
func ApplyProxyEnvVars(cmd *exec.Cmd) {
116-
slog.Debug(
117-
"Applying private registry proxy environment variables",
118-
slog.String("cmd_args", strings.Join(cmd.Args, " ")),
119-
)
120-
121155
// If we haven't done so yet, check whether the proxy environment variables are set
122156
// and extract information from them.
123157
if !proxy_vars_checked {
@@ -131,4 +165,10 @@ func ApplyProxyEnvVars(cmd *exec.Cmd) {
131165
if proxy_vars != nil {
132166
cmd.Env = append(os.Environ(), proxy_vars...)
133167
}
168+
169+
slog.Debug(
170+
"Applying private registry proxy environment variables",
171+
slog.String("cmd_args", strings.Join(cmd.Args, " ")),
172+
slog.String("proxy_vars", strings.Join(proxy_vars, ",")),
173+
)
134174
}

go/extractor/util/registryproxy_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,31 @@ func TestParseRegistryConfigs(t *testing.T) {
4747
t.Fatalf("Expected `URL` to be `https://proxy.example.com/mod`, but got `%s`", first.URL)
4848
}
4949
}
50+
51+
func TestParseRegistryConfigsMultiple(t *testing.T) {
52+
multiple := parseRegistryConfigsSuccess(t, "[{ \"type\": \"git_source\", \"url\": \"https://github.com/github\" }, { \"type\": \"goproxy_server\", \"url\": \"https://proxy.example.com/mod\" }]")
53+
54+
if len(multiple) != 2 {
55+
t.Fatalf("Expected `parseRegistryConfigs` to return two configurations, but got %d.", len(multiple))
56+
}
57+
58+
first := multiple[0]
59+
60+
if first.Type != "git_source" {
61+
t.Fatalf("Expected `Type` to be `git_source`, but got `%s`", first.Type)
62+
}
63+
64+
if first.URL != "https://github.com/github" {
65+
t.Fatalf("Expected `URL` to be `https://github.com/github`, but got `%s`", first.URL)
66+
}
67+
68+
second := multiple[1]
69+
70+
if second.Type != "goproxy_server" {
71+
t.Fatalf("Expected `Type` to be `goproxy_server`, but got `%s`", second.Type)
72+
}
73+
74+
if second.URL != "https://proxy.example.com/mod" {
75+
t.Fatalf("Expected `URL` to be `https://proxy.example.com/mod`, but got `%s`", second.URL)
76+
}
77+
}

0 commit comments

Comments
 (0)