|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const PROXY_HOST = "CODEQL_PROXY_HOST" |
| 13 | +const PROXY_PORT = "CODEQL_PROXY_PORT" |
| 14 | +const PROXY_CA_CERTIFICATE = "CODEQL_PROXY_CA_CERTIFICATE" |
| 15 | +const PROXY_URLS = "CODEQL_PROXY_URLS" |
| 16 | +const GOPROXY_SERVER = "goproxy_server" |
| 17 | + |
| 18 | +type RegistryConfig struct { |
| 19 | + Type string `json:"type"` |
| 20 | + URL string `json:"url"` |
| 21 | +} |
| 22 | + |
| 23 | +// The address of the proxy including protocol and port (e.g. http://localhost:1234) |
| 24 | +var proxy_address string |
| 25 | + |
| 26 | +// The path to the temporary file that stores the proxy certificate, if any. |
| 27 | +var proxy_cert_file string |
| 28 | + |
| 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 |
| 32 | + |
| 33 | +// Stores the environment variables that we wish to pass on to `go` commands. |
| 34 | +var proxy_vars []string = nil |
| 35 | + |
| 36 | +// Keeps track of whether we have inspected the proxy environment variables. |
| 37 | +// Needed since `proxy_vars` may be nil either way. |
| 38 | +var proxy_vars_checked bool = false |
| 39 | + |
| 40 | +// Tries to parse the given string value into an array of RegistryConfig values. |
| 41 | +func parseRegistryConfigs(str string) ([]RegistryConfig, error) { |
| 42 | + var configs []RegistryConfig |
| 43 | + if err := json.Unmarshal([]byte(str), &configs); err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + return configs, nil |
| 48 | +} |
| 49 | + |
| 50 | +func getEnvVars() []string { |
| 51 | + var result []string |
| 52 | + |
| 53 | + if proxy_host, proxy_host_set := os.LookupEnv(PROXY_HOST); proxy_host_set { |
| 54 | + if proxy_port, proxy_port_set := os.LookupEnv(PROXY_PORT); proxy_port_set { |
| 55 | + 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)) |
| 57 | + |
| 58 | + slog.Info("Found private registry proxy", slog.String("proxy_address", proxy_address)) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if proxy_cert, proxy_cert_set := os.LookupEnv(PROXY_CA_CERTIFICATE); proxy_cert_set { |
| 63 | + // Write the certificate to a temporary file |
| 64 | + slog.Info("Found certificate") |
| 65 | + |
| 66 | + f, err := os.CreateTemp("", "codeql-proxy.crt") |
| 67 | + if err != nil { |
| 68 | + slog.Error("Failed to create temporary file for the proxy certificate", slog.String("error", err.Error())) |
| 69 | + } |
| 70 | + |
| 71 | + _, err = f.WriteString(proxy_cert) |
| 72 | + if err != nil { |
| 73 | + slog.Error("Failed to write to the temporary certificate file", slog.String("error", err.Error())) |
| 74 | + } |
| 75 | + |
| 76 | + err = f.Close() |
| 77 | + if err != nil { |
| 78 | + slog.Error("Failed to close the temporary certificate file", slog.String("error", err.Error())) |
| 79 | + } else { |
| 80 | + proxy_cert_file = f.Name() |
| 81 | + result = append(result, fmt.Sprintf("SSL_CERT_FILE=%s", proxy_cert_file)) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if proxy_urls, proxy_urls_set := os.LookupEnv(PROXY_URLS); proxy_urls_set { |
| 86 | + val, err := parseRegistryConfigs(proxy_urls) |
| 87 | + if err != nil { |
| 88 | + slog.Error("Unable to parse proxy configurations", slog.String("error", err.Error())) |
| 89 | + } else { |
| 90 | + // We only care about private registry configurations that are relevant to Go and |
| 91 | + // filter others out at this point. |
| 92 | + for _, cfg := range val { |
| 93 | + if cfg.Type == GOPROXY_SERVER { |
| 94 | + proxy_configs = append(proxy_configs, cfg) |
| 95 | + slog.Info("Found GOPROXY server", slog.String("url", cfg.URL)) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if len(proxy_configs) > 0 { |
| 100 | + goproxy_val := "https://proxy.golang.org,direct" |
| 101 | + |
| 102 | + for _, cfg := range proxy_configs { |
| 103 | + goproxy_val = cfg.URL + "," + goproxy_val |
| 104 | + } |
| 105 | + |
| 106 | + result = append(result, fmt.Sprintf("GOPROXY=%s", goproxy_val), "GOPRIVATE=", "GONOPROXY=") |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return result |
| 112 | +} |
| 113 | + |
| 114 | +// Applies private package proxy related environment variables to `cmd`. |
| 115 | +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 | + |
| 121 | + // If we haven't done so yet, check whether the proxy environment variables are set |
| 122 | + // and extract information from them. |
| 123 | + if !proxy_vars_checked { |
| 124 | + proxy_vars = getEnvVars() |
| 125 | + proxy_vars_checked = true |
| 126 | + } |
| 127 | + |
| 128 | + // If the proxy is configured, `proxy_vars` will be not `nil`. We append those |
| 129 | + // variables to the existing environment to preserve those environment variables. |
| 130 | + // If `cmd.Env` is not changed, then the existing environment is also preserved. |
| 131 | + if proxy_vars != nil { |
| 132 | + cmd.Env = append(os.Environ(), proxy_vars...) |
| 133 | + } |
| 134 | +} |
0 commit comments