-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Go: Support private registries via GOPROXY
#19248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ded27bc
Go: Replace `exec.Command("go"` with `toolchain.GoCommand(`
mbg 0f78e11
Go: Detect and apply proxy settings (WIP)
mbg e210be7
Go: Preserve environment variables in `ApplyProxyEnvVars`
mbg cafe1ef
Go: Refactor `ApplyProxyEnvVars`
mbg e805d1e
Merge remote-tracking branch 'origin/main' into mbg/go/private-regist…
mbg 9cfa451
Go: Fix/improve comment about environment variable preservation
mbg 5172a4d
Go: Remove check from `getEnvVars`
mbg 91a7944
Go: Change "Unable" to "Failed" for consistency
mbg 7592ce4
Go: Restore `parseRegistryConfigsFail` test for the empty string
mbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
const PROXY_HOST = "CODEQL_PROXY_HOST" | ||
const PROXY_PORT = "CODEQL_PROXY_PORT" | ||
const PROXY_CA_CERTIFICATE = "CODEQL_PROXY_CA_CERTIFICATE" | ||
const PROXY_URLS = "CODEQL_PROXY_URLS" | ||
const GOPROXY_SERVER = "goproxy_server" | ||
|
||
type RegistryConfig struct { | ||
Type string `json:"type"` | ||
URL string `json:"url"` | ||
} | ||
|
||
// The address of the proxy including protocol and port (e.g. http://localhost:1234) | ||
var proxy_address string | ||
|
||
// The path to the temporary file that stores the proxy certificate, if any. | ||
var proxy_cert_file string | ||
|
||
// An array of registry configurations that are relevant to Go. | ||
// This excludes other registry configurations that may be available, but are not relevant to Go. | ||
var proxy_configs []RegistryConfig | ||
|
||
// Stores the environment variables that we wish to pass on to `go` commands. | ||
var proxy_vars []string = nil | ||
|
||
// Keeps track of whether we have inspected the proxy environment variables. | ||
// Needed since `proxy_vars` may be nil either way. | ||
var proxy_vars_checked bool = false | ||
|
||
// Tries to parse the given string value into an array of RegistryConfig values. | ||
func parseRegistryConfigs(str string) ([]RegistryConfig, error) { | ||
var configs []RegistryConfig | ||
if err := json.Unmarshal([]byte(str), &configs); err != nil { | ||
return nil, err | ||
} | ||
|
||
return configs, nil | ||
} | ||
|
||
func getEnvVars() []string { | ||
var result []string | ||
|
||
if proxy_host, proxy_host_set := os.LookupEnv(PROXY_HOST); proxy_host_set { | ||
if proxy_port, proxy_port_set := os.LookupEnv(PROXY_PORT); proxy_port_set { | ||
proxy_address = fmt.Sprintf("http://%s:%s", proxy_host, proxy_port) | ||
result = append(result, fmt.Sprintf("HTTP_PROXY=%s", proxy_address), fmt.Sprintf("HTTPS_PROXY=%s", proxy_address)) | ||
|
||
slog.Info("Found private registry proxy", slog.String("proxy_address", proxy_address)) | ||
} | ||
} | ||
|
||
if proxy_cert, proxy_cert_set := os.LookupEnv(PROXY_CA_CERTIFICATE); proxy_cert_set { | ||
// Write the certificate to a temporary file | ||
slog.Info("Found certificate") | ||
|
||
f, err := os.CreateTemp("", "codeql-proxy.crt") | ||
if err != nil { | ||
slog.Error("Failed to create temporary file for the proxy certificate", slog.String("error", err.Error())) | ||
} | ||
|
||
_, err = f.WriteString(proxy_cert) | ||
if err != nil { | ||
slog.Error("Failed to write to the temporary certificate file", slog.String("error", err.Error())) | ||
} | ||
|
||
err = f.Close() | ||
if err != nil { | ||
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)) | ||
} | ||
} | ||
|
||
if proxy_urls, proxy_urls_set := os.LookupEnv(PROXY_URLS); proxy_urls_set { | ||
val, err := parseRegistryConfigs(proxy_urls) | ||
if err != nil { | ||
slog.Error("Unable to parse proxy configurations", slog.String("error", err.Error())) | ||
} else { | ||
// We only care about private registry configurations that are relevant to Go and | ||
// filter others out at this point. | ||
for _, cfg := range val { | ||
if cfg.Type == GOPROXY_SERVER { | ||
proxy_configs = append(proxy_configs, cfg) | ||
slog.Info("Found GOPROXY server", slog.String("url", cfg.URL)) | ||
} | ||
} | ||
|
||
if len(proxy_configs) > 0 { | ||
goproxy_val := "https://proxy.golang.org,direct" | ||
|
||
for _, cfg := range proxy_configs { | ||
goproxy_val = cfg.URL + "," + goproxy_val | ||
} | ||
|
||
result = append(result, fmt.Sprintf("GOPROXY=%s", goproxy_val), "GOPRIVATE=", "GONOPROXY=") | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// Applies private package proxy related environment variables to `cmd`. | ||
func ApplyProxyEnvVars(cmd *exec.Cmd) { | ||
slog.Debug( | ||
"Applying private registry proxy environment variables", | ||
slog.String("cmd_args", strings.Join(cmd.Args, " ")), | ||
) | ||
|
||
// If we haven't done so yet, check whether the proxy environment variables are set | ||
// and extract information from them. | ||
if !proxy_vars_checked { | ||
proxy_vars = getEnvVars() | ||
proxy_vars_checked = true | ||
} | ||
|
||
// If the proxy is configured, `proxy_vars` will be not `nil`. We append those | ||
// variables to the existing environment to preserve those environment variables. | ||
// If `cmd.Env` is not changed, then the existing environment is also preserved. | ||
if proxy_vars != nil { | ||
cmd.Env = append(os.Environ(), proxy_vars...) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func parseRegistryConfigsFail(t *testing.T, str string) { | ||
_, err := parseRegistryConfigs(str) | ||
|
||
if err == nil { | ||
t.Fatal("Expected `parseRegistryConfigs` to fail, but it succeeded.") | ||
} | ||
} | ||
|
||
func parseRegistryConfigsSuccess(t *testing.T, str string) []RegistryConfig { | ||
val, err := parseRegistryConfigs(str) | ||
|
||
if err != nil { | ||
t.Fatalf("Expected `parseRegistryConfigs` to succeed, but it failed: %s", err.Error()) | ||
} | ||
|
||
return val | ||
} | ||
|
||
func TestParseRegistryConfigs(t *testing.T) { | ||
parseRegistryConfigsFail(t, "") | ||
|
||
empty := parseRegistryConfigsSuccess(t, "[]") | ||
|
||
if len(empty) != 0 { | ||
t.Fatal("Expected `parseRegistryConfigs(\"[]\")` to return no configurations, but got some.") | ||
} | ||
|
||
single := parseRegistryConfigsSuccess(t, "[{ \"type\": \"goproxy_server\", \"url\": \"https://proxy.example.com/mod\" }]") | ||
|
||
if len(single) != 1 { | ||
t.Fatalf("Expected `parseRegistryConfigs` to return one configuration, but got %d.", len(single)) | ||
} | ||
|
||
first := single[0] | ||
|
||
if first.Type != "goproxy_server" { | ||
t.Fatalf("Expected `Type` to be `goproxy_server`, but got `%s`", first.Type) | ||
} | ||
|
||
if first.URL != "https://proxy.example.com/mod" { | ||
t.Fatalf("Expected `URL` to be `https://proxy.example.com/mod`, but got `%s`", first.URL) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These no longer need to be global variables. I think it would be clearer if they were just local variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I somewhat intentionally left these as global variables so that we can access their values elsewhere if needed going forward.
I don't feel strongly about this though, so if you would prefer it to be locals for now while they don't have to be globals, then I can make that change.