@@ -20,10 +20,23 @@ type RegistryConfig struct {
20
20
URL string `json:"url"`
21
21
}
22
22
23
+ // The address of the proxy including protocol and port (e.g. http://localhost:1234)
23
24
var proxy_address string
25
+
26
+ // The path to the temporary file that stores the proxy certificate, if any.
24
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.
25
31
var proxy_configs []RegistryConfig
26
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
+
27
40
// Tries to parse the given string value into an array of RegistryConfig values.
28
41
func parseRegistryConfigs (str string ) ([]RegistryConfig , error ) {
29
42
var configs []RegistryConfig
@@ -34,10 +47,21 @@ func parseRegistryConfigs(str string) ([]RegistryConfig, error) {
34
47
return configs , nil
35
48
}
36
49
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
+
38
60
if proxy_host , proxy_host_set := os .LookupEnv (PROXY_HOST ); proxy_host_set {
39
61
if proxy_port , proxy_port_set := os .LookupEnv (PROXY_PORT ); proxy_port_set {
40
62
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
+
41
65
slog .Info ("Found private registry proxy" , slog .String ("proxy_address" , proxy_address ))
42
66
}
43
67
}
@@ -61,6 +85,7 @@ func checkEnvVars() {
61
85
slog .Error ("Failed to close the temporary certificate file" , slog .String ("error" , err .Error ()))
62
86
} else {
63
87
proxy_cert_file = f .Name ()
88
+ result = append (result , fmt .Sprintf ("SSL_CERT_FILE=%s" , proxy_cert_file ))
64
89
}
65
90
}
66
91
@@ -71,47 +96,43 @@ func checkEnvVars() {
71
96
} else {
72
97
// We only care about private registry configurations that are relevant to Go and
73
98
// filter others out at this point.
74
- proxy_configs = make ([]RegistryConfig , 0 )
75
99
for _ , cfg := range val {
76
100
if cfg .Type == GOPROXY_SERVER {
77
101
proxy_configs = append (proxy_configs , cfg )
78
102
slog .Info ("Found GOPROXY server" , slog .String ("url" , cfg .URL ))
79
103
}
80
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
+ }
112
+
113
+ result = append (result , fmt .Sprintf ("GOPROXY=%s" , goproxy_val ), "GOPRIVATE=" , "GONOPROXY=" )
114
+ }
81
115
}
82
116
}
83
117
}
84
118
85
119
// Applies private package proxy related environment variables to `cmd`.
86
120
func ApplyProxyEnvVars (cmd * exec.Cmd ) {
87
- slog .Info (
121
+ slog .Debug (
88
122
"Applying private registry proxy environment variables" ,
89
123
slog .String ("cmd_args" , strings .Join (cmd .Args , " " )),
90
124
)
91
125
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
100
131
}
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
- }
112
132
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 ... )
116
137
}
117
138
}
0 commit comments