Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit354d0fc

Browse files
authored
fix: filter agent-exec env vars (#15764)
- Filters env vars specific to agent-exec from the exec'd process. Thisis to prevent any issues when developing Coder in Coder, particularlyagent tests in the cli pkg.
1 parentf8d938d commit354d0fc

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-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
returnxerrors.Errorf("look path: %w",err)
112113
}
113114

114-
returnsyscall.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(estring)bool {
119+
returnstrings.HasPrefix(e,EnvProcPrioMgmt)||
120+
strings.HasPrefix(e,EnvProcOOMScore)||
121+
strings.HasPrefix(e,EnvProcNiceScore)
122+
})
123+
124+
returnsyscall.Exec(path,args,env)
115125
}
116126

117127
funcdefaultNiceScore() (int,error) {

‎agent/agentexec/cli_linux_test.go‎

Lines changed: 37 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,32 @@ 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+
// Ensure unrelated environment variables are preserved.
49+
cmd.Env=append(cmd.Env,"CODER_TEST_ME_AGENTEXEC=true")
50+
err:=cmd.Start()
51+
require.NoError(t,err)
52+
gocmd.Wait()
53+
waitForSentinel(ctx,t,cmd,path)
54+
55+
env:=procEnv(t,cmd.Process.Pid)
56+
hasExecEnvs:=slices.ContainsFunc(
57+
env,
58+
func(estring)bool {
59+
returnstrings.HasPrefix(e,agentexec.EnvProcPrioMgmt)||
60+
strings.HasPrefix(e,agentexec.EnvProcOOMScore)||
61+
strings.HasPrefix(e,agentexec.EnvProcNiceScore)
62+
})
63+
require.False(t,hasExecEnvs,"expected environment variables to be filtered")
64+
userEnv:=slices.Contains(env,"CODER_TEST_ME_AGENTEXEC=true")
65+
require.True(t,userEnv,"expected user environment variables to be preserved")
66+
})
67+
4068
t.Run("Defaults",func(t*testing.T) {
4169
ctx:=testutil.Context(t,testutil.WaitMedium)
4270
cmd,path:=cmd(ctx,t,0,0)
@@ -176,6 +204,15 @@ func expectedOOMScore(t *testing.T) int {
176204
return998
177205
}
178206

207+
// procEnv returns the environment variables for a given process.
208+
funcprocEnv(t*testing.T,pidint) []string {
209+
t.Helper()
210+
211+
env,err:=os.ReadFile(fmt.Sprintf("/proc/%d/environ",pid))
212+
require.NoError(t,err)
213+
returnstrings.Split(string(env),"\x00")
214+
}
215+
179216
funcexpectedNiceScore(t*testing.T)int {
180217
t.Helper()
181218

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp