Skip to content

Commit 509e10f

Browse files
Jack Lindamoodandybons
Jack Lindamood
authored andcommitted
playground: allow explicit GOPROXY variable
Setting the PLAY_GOPROXY variable of the playground modifies where the playground fetches module information from. This allows users to install the playground inside their company VPN and use private modules. The variable isn't named "GOPROXY" to not confuse which context the URL is used for. The name PLAY_* is adopted from the existing env variable ALLOW_PLAY_MODULE_DOWNLOADS. Fixes golang/go#32740 Change-Id: Idcd0530c1c28342414e0bc5439fca6a2272e1eaa Reviewed-on: https://go-review.googlesource.com/c/playground/+/184558 Reviewed-by: Andrew Bonventre <[email protected]>
1 parent 5fdf601 commit 509e10f

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

sandbox.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func compileAndRun(req *request) (*response, error) {
370370
return nil, fmt.Errorf("error creating temp directory: %v", err)
371371
}
372372
defer os.RemoveAll(goPath)
373-
cmd.Env = append(cmd.Env, "GO111MODULE=on", "GOPROXY=https://proxy.golang.org")
373+
cmd.Env = append(cmd.Env, "GO111MODULE=on", "GOPROXY="+playgroundGoproxy())
374374
} else {
375375
goPath = os.Getenv("GOPATH") // contains old code.google.com/p/go-tour, etc
376376
cmd.Env = append(cmd.Env, "GO111MODULE=off") // in case it becomes on by default later
@@ -451,13 +451,24 @@ func allowModuleDownloads(files *fileSet) bool {
451451
// these packages to still run, so the Dockerfile adds these packages
452452
// at this name in $GOPATH. Any snippets using this old name wouldn't
453453
// have expected (or been able to use) third-party packages anyway,
454-
// so disabling modules and proxy.golang.org fetches is acceptable.
454+
// so disabling modules and proxy fetches is acceptable.
455455
return false
456456
}
457457
v, _ := strconv.ParseBool(os.Getenv("ALLOW_PLAY_MODULE_DOWNLOADS"))
458458
return v
459459
}
460460

461+
// playgroundGoproxy returns the GOPROXY environment config the playground should use.
462+
// It is fetched from the environment variable PLAY_GOPROXY. A missing or empty
463+
// value for PLAY_GOPROXY returns the default value of https://proxy.golang.org.
464+
func playgroundGoproxy() string {
465+
proxypath := os.Getenv("PLAY_GOPROXY")
466+
if proxypath != "" {
467+
return proxypath
468+
}
469+
return "https://proxy.golang.org"
470+
}
471+
461472
func (s *server) healthCheck() error {
462473
resp, err := compileAndRun(&request{Body: healthProg})
463474
if err != nil {

server_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,36 @@ func TestAllowModuleDownloads(t *testing.T) {
272272
t.Errorf("%d. allow = %v; want %v; files:\n%s", i, got, tt.want, filesAsString(files))
273273
}
274274
}
275+
}
276+
277+
func TestPlaygroundGoproxy(t *testing.T) {
278+
const envKey = "PLAY_GOPROXY"
279+
defer os.Setenv(envKey, os.Getenv(envKey))
275280

281+
tests := []struct {
282+
name string
283+
env string
284+
want string
285+
}{
286+
{name: "missing", env: "", want: "https://proxy.golang.org"},
287+
{name: "set_to_default", env: "https://proxy.golang.org", want: "https://proxy.golang.org"},
288+
{name: "changed", env: "https://company.intranet", want: "https://company.intranet"},
289+
}
290+
for _, tt := range tests {
291+
t.Run(tt.name, func(t *testing.T) {
292+
if tt.env != "" {
293+
if err := os.Setenv(envKey, tt.env); err != nil {
294+
t.Errorf("unable to set environment variable for test: %s", err)
295+
}
296+
} else {
297+
if err := os.Unsetenv(envKey); err != nil {
298+
t.Errorf("unable to unset environment variable for test: %s", err)
299+
}
300+
}
301+
got := playgroundGoproxy()
302+
if got != tt.want {
303+
t.Errorf("playgroundGoproxy = %s; want %s; env: %s", got, tt.want, tt.env)
304+
}
305+
})
306+
}
276307
}

vet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func vetCheckInDir(dir, goPath string, modules bool) (output string, execErr err
6060
if modules {
6161
cmd.Env = append(cmd.Env,
6262
"GO111MODULE=on",
63-
"GOPROXY=https://proxy.golang.org",
63+
"GOPROXY="+playgroundGoproxy(),
6464
)
6565
}
6666
out, err := cmd.CombinedOutput()

0 commit comments

Comments
 (0)