@@ -20,10 +20,23 @@ type RegistryConfig struct {
2020 URL string `json:"url"`
2121}
2222
23+ // The address of the proxy including protocol and port (e.g. http://localhost:1234)
2324var proxy_address string
25+
26+ // The path to the temporary file that stores the proxy certificate, if any.
2427var 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.
2531var proxy_configs []RegistryConfig
2632
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+
2740// Tries to parse the given string value into an array of RegistryConfig values.
2841func parseRegistryConfigs (str string ) ([]RegistryConfig , error ) {
2942 var configs []RegistryConfig
@@ -34,10 +47,21 @@ func parseRegistryConfigs(str string) ([]RegistryConfig, error) {
3447 return configs , nil
3548}
3649
37- func checkEnvVars () {
50+ func getEnvVars () []string {
51+ // If `proxy_vars` has been initialised, then we have already performed
52+ // these checks and don't need to do so again. We assume that the environment
53+ // variables are constant throughout the run of the autobuilder.
54+ if proxy_vars != nil {
55+ return proxy_vars
56+ }
57+
58+ var result []string
59+
3860 if proxy_host , proxy_host_set := os .LookupEnv (PROXY_HOST ); proxy_host_set {
3961 if proxy_port , proxy_port_set := os .LookupEnv (PROXY_PORT ); proxy_port_set {
4062 proxy_address = fmt .Sprintf ("http://%s:%s" , proxy_host , proxy_port )
63+ result = append (result , fmt .Sprintf ("HTTP_PROXY=%s" , proxy_address ), fmt .Sprintf ("HTTPS_PROXY=%s" , proxy_address ))
64+
4165 slog .Info ("Found private registry proxy" , slog .String ("proxy_address" , proxy_address ))
4266 }
4367 }
@@ -61,6 +85,7 @@ func checkEnvVars() {
6185 slog .Error ("Failed to close the temporary certificate file" , slog .String ("error" , err .Error ()))
6286 } else {
6387 proxy_cert_file = f .Name ()
88+ result = append (result , fmt .Sprintf ("SSL_CERT_FILE=%s" , proxy_cert_file ))
6489 }
6590 }
6691
@@ -71,47 +96,43 @@ func checkEnvVars() {
7196 } else {
7297 // We only care about private registry configurations that are relevant to Go and
7398 // filter others out at this point.
74- proxy_configs = make ([]RegistryConfig , 0 )
7599 for _ , cfg := range val {
76100 if cfg .Type == GOPROXY_SERVER {
77101 proxy_configs = append (proxy_configs , cfg )
78102 slog .Info ("Found GOPROXY server" , slog .String ("url" , cfg .URL ))
79103 }
80104 }
105+
106+ if len (proxy_configs ) > 0 {
107+ goproxy_val := "https://proxy.golang.org,direct"
108+
109+ for _ , cfg := range proxy_configs {
110+ goproxy_val = cfg .URL + "," + goproxy_val
111+ }
112+
113+ result = append (result , fmt .Sprintf ("GOPROXY=%s" , goproxy_val ), "GOPRIVATE=" , "GONOPROXY=" )
114+ }
81115 }
82116 }
83117}
84118
85119// Applies private package proxy related environment variables to `cmd`.
86120func ApplyProxyEnvVars (cmd * exec.Cmd ) {
87- slog .Info (
121+ slog .Debug (
88122 "Applying private registry proxy environment variables" ,
89123 slog .String ("cmd_args" , strings .Join (cmd .Args , " " )),
90124 )
91125
92- checkEnvVars ()
93-
94- // Preserve environment variables
95- cmd .Env = os .Environ ()
96-
97- if proxy_address != "" {
98- cmd .Env = append (cmd .Env , fmt .Sprintf ("HTTP_PROXY=%s" , proxy_address ))
99- cmd .Env = append (cmd .Env , fmt .Sprintf ("HTTPS_PROXY=%s" , proxy_address ))
126+ // If we haven't done so yet, check whether the proxy environment variables are set
127+ // and extract information from them.
128+ if ! proxy_vars_checked {
129+ proxy_vars = getEnvVars ()
130+ proxy_vars_checked = true
100131 }
101- if proxy_cert_file != "" {
102- slog .Info ("Setting SSL_CERT_FILE" , slog .String ("proxy_cert_file" , proxy_cert_file ))
103- cmd .Env = append (cmd .Env , fmt .Sprintf ("SSL_CERT_FILE=%s" , proxy_cert_file ))
104- }
105-
106- if len (proxy_configs ) > 0 {
107- goproxy_val := "https://proxy.golang.org,direct"
108-
109- for _ , cfg := range proxy_configs {
110- goproxy_val = cfg .URL + "," + goproxy_val
111- }
112132
113- cmd .Env = append (cmd .Env , fmt .Sprintf ("GOPROXY=%s" , goproxy_val ))
114- cmd .Env = append (cmd .Env , "GOPRIVATE=" )
115- cmd .Env = append (cmd .Env , "GONOPROXY=" )
133+ // If the proxy is configured, `proxy_vars` will be not `nil`. We append those
134+ // variables Preserve environment variables
135+ if proxy_vars != nil {
136+ cmd .Env = append (os .Environ (), proxy_vars ... )
116137 }
117138}
0 commit comments