Skip to content

Commit b1e4be1

Browse files
authored
fix: fix unsetOptionsEnv, add integration test (coder#242)
1 parent de6fc15 commit b1e4be1

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

envbuilder.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"os/exec"
1919
"os/user"
2020
"path/filepath"
21-
"reflect"
2221
"sort"
2322
"strconv"
2423
"strings"
@@ -1064,16 +1063,19 @@ func createPostStartScript(path string, postStartCommand devcontainer.LifecycleS
10641063
// unsetOptionsEnv unsets all environment variables that are used
10651064
// to configure the options.
10661065
func unsetOptionsEnv() {
1067-
val := reflect.ValueOf(&Options{}).Elem()
1068-
typ := val.Type()
1069-
1070-
for i := 0; i < val.NumField(); i++ {
1071-
fieldTyp := typ.Field(i)
1072-
env := fieldTyp.Tag.Get("env")
1073-
if env == "" {
1066+
var o Options
1067+
for _, opt := range o.CLI() {
1068+
if opt.Env == "" {
1069+
continue
1070+
}
1071+
// Do not strip options that do not have the magic prefix!
1072+
// For example, CODER_AGENT_URL, CODER_AGENT_TOKEN, CODER_AGENT_SUBSYSTEM.
1073+
if !strings.HasPrefix(opt.Env, envPrefix) {
10741074
continue
10751075
}
1076-
os.Unsetenv(env)
1076+
// Strip both with and without prefix.
1077+
os.Unsetenv(opt.Env)
1078+
os.Unsetenv(strings.TrimPrefix(opt.Env, envPrefix))
10771079
}
10781080
}
10791081

integration/integration_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,46 @@ PATH=/usr/local/bin:/bin:/go/bin:/opt
695695
REMOTE_BAR=bar`)
696696
}
697697

698+
func TestUnsetOptionsEnv(t *testing.T) {
699+
t.Parallel()
700+
701+
// Ensures that a Git repository with a devcontainer.json is cloned and built.
702+
srv := createGitServer(t, gitServerOptions{
703+
files: map[string]string{
704+
".devcontainer/devcontainer.json": `{
705+
"name": "Test",
706+
"build": {
707+
"dockerfile": "Dockerfile"
708+
},
709+
}`,
710+
".devcontainer/Dockerfile": "FROM " + testImageAlpine + "\nENV FROM_DOCKERFILE=foo",
711+
},
712+
})
713+
ctr, err := runEnvbuilder(t, options{env: []string{
714+
envbuilderEnv("GIT_URL", srv.URL),
715+
"GIT_URL", srv.URL,
716+
envbuilderEnv("GIT_PASSWORD", "supersecret"),
717+
"GIT_PASSWORD", "supersecret",
718+
envbuilderEnv("INIT_SCRIPT", "env > /root/env.txt && sleep infinity"),
719+
"INIT_SCRIPT", "env > /root/env.txt && sleep infinity",
720+
}})
721+
require.NoError(t, err)
722+
723+
output := execContainer(t, ctr, "cat /root/env.txt")
724+
var os envbuilder.Options
725+
for _, s := range strings.Split(strings.TrimSpace(output), "\n") {
726+
for _, o := range os.CLI() {
727+
if strings.HasPrefix(s, o.Env) {
728+
assert.Fail(t, "environment variable should be stripped when running init script", s)
729+
}
730+
optWithoutPrefix := strings.TrimPrefix(o.Env, envbuilder.WithEnvPrefix(""))
731+
if strings.HasPrefix(s, optWithoutPrefix) {
732+
assert.Fail(t, "environment variable should be stripped when running init script", s)
733+
}
734+
}
735+
}
736+
}
737+
698738
func TestLifecycleScripts(t *testing.T) {
699739
t.Parallel()
700740

0 commit comments

Comments
 (0)