Skip to content

Commit 37f87ca

Browse files
committed
fix: filter agent-exec env vars
- Filters env vars specific to agent-exec from the exec'd process. This is to prevent any issues when developing Coder in Coder, particularly agent tests in the cli pkg.
1 parent f8d938d commit 37f87ca

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

agent/agentexec/cli_linux.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"runtime"
12+
"slices"
1213
"strconv"
1314
"strings"
1415
"syscall"
@@ -111,7 +112,16 @@ func CLI() error {
111112
return xerrors.Errorf("look path: %w", err)
112113
}
113114

114-
return syscall.Exec(path, args, os.Environ())
115+
// Remove environment variables specific to the agentexec command. This is
116+
// especially important for environments that are attempting to develop Coder in Coder.
117+
env := os.Environ()
118+
env = slices.DeleteFunc(env, func(e string) bool {
119+
return strings.HasPrefix(e, EnvProcPrioMgmt) ||
120+
strings.HasPrefix(e, EnvProcOOMScore) ||
121+
strings.HasPrefix(e, EnvProcNiceScore)
122+
})
123+
124+
return syscall.Exec(path, args, env)
115125
}
116126

117127
func defaultNiceScore() (int, error) {

agent/agentexec/cli_linux_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"slices"
1314
"strconv"
1415
"strings"
1516
"syscall"
@@ -20,6 +21,7 @@ import (
2021
"golang.org/x/sys/unix"
2122
"golang.org/x/xerrors"
2223

24+
"github.com/coder/coder/v2/agent/agentexec"
2325
"github.com/coder/coder/v2/testutil"
2426
)
2527

@@ -37,6 +39,28 @@ func TestCLI(t *testing.T) {
3739
requireNiceScore(t, cmd.Process.Pid, 12)
3840
})
3941

42+
t.Run("FiltersEnv", func(t *testing.T) {
43+
ctx := testutil.Context(t, testutil.WaitMedium)
44+
cmd, path := cmd(ctx, t, 123, 12)
45+
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=true", agentexec.EnvProcPrioMgmt))
46+
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=123", agentexec.EnvProcOOMScore))
47+
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=12", agentexec.EnvProcNiceScore))
48+
err := cmd.Start()
49+
require.NoError(t, err)
50+
go cmd.Wait()
51+
waitForSentinel(ctx, t, cmd, path)
52+
53+
env := procEnv(t, cmd.Process.Pid)
54+
hasExecEnvs := slices.ContainsFunc(
55+
env,
56+
func(e string) bool {
57+
return strings.HasPrefix(e, agentexec.EnvProcPrioMgmt) ||
58+
strings.HasPrefix(e, agentexec.EnvProcOOMScore) ||
59+
strings.HasPrefix(e, agentexec.EnvProcNiceScore)
60+
})
61+
require.False(t, hasExecEnvs, "expected environment variables to be filtered")
62+
})
63+
4064
t.Run("Defaults", func(t *testing.T) {
4165
ctx := testutil.Context(t, testutil.WaitMedium)
4266
cmd, path := cmd(ctx, t, 0, 0)
@@ -176,6 +200,15 @@ func expectedOOMScore(t *testing.T) int {
176200
return 998
177201
}
178202

203+
// procEnv returns the environment variables for a given process.
204+
func procEnv(t *testing.T, pid int) []string {
205+
t.Helper()
206+
207+
env, err := os.ReadFile(fmt.Sprintf("/proc/%d/environ", pid))
208+
require.NoError(t, err)
209+
return strings.Split(string(env), "\x00")
210+
}
211+
179212
func expectedNiceScore(t *testing.T) int {
180213
t.Helper()
181214

0 commit comments

Comments
 (0)