Skip to content

fix: filter agent-exec env vars #15764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion agent/agentexec/cli_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"runtime"
"slices"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -111,7 +112,16 @@ func CLI() error {
return xerrors.Errorf("look path: %w", err)
}

return syscall.Exec(path, args, os.Environ())
// Remove environment variables specific to the agentexec command. This is
// especially important for environments that are attempting to develop Coder in Coder.
env := os.Environ()
env = slices.DeleteFunc(env, func(e string) bool {
return strings.HasPrefix(e, EnvProcPrioMgmt) ||
strings.HasPrefix(e, EnvProcOOMScore) ||
strings.HasPrefix(e, EnvProcNiceScore)
})

return syscall.Exec(path, args, env)
}

func defaultNiceScore() (int, error) {
Expand Down
37 changes: 37 additions & 0 deletions agent/agentexec/cli_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"syscall"
Expand All @@ -20,6 +21,7 @@ import (
"golang.org/x/sys/unix"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/agent/agentexec"
"github.com/coder/coder/v2/testutil"
)

Expand All @@ -37,6 +39,32 @@ func TestCLI(t *testing.T) {
requireNiceScore(t, cmd.Process.Pid, 12)
})

t.Run("FiltersEnv", func(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)
cmd, path := cmd(ctx, t, 123, 12)
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=true", agentexec.EnvProcPrioMgmt))
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=123", agentexec.EnvProcOOMScore))
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=12", agentexec.EnvProcNiceScore))
// Ensure unrelated environment variables are preserved.
cmd.Env = append(cmd.Env, "CODER_TEST_ME_AGENTEXEC=true")
err := cmd.Start()
require.NoError(t, err)
go cmd.Wait()
waitForSentinel(ctx, t, cmd, path)

env := procEnv(t, cmd.Process.Pid)
hasExecEnvs := slices.ContainsFunc(
env,
func(e string) bool {
return strings.HasPrefix(e, agentexec.EnvProcPrioMgmt) ||
strings.HasPrefix(e, agentexec.EnvProcOOMScore) ||
strings.HasPrefix(e, agentexec.EnvProcNiceScore)
})
require.False(t, hasExecEnvs, "expected environment variables to be filtered")
userEnv := slices.Contains(env, "CODER_TEST_ME_AGENTEXEC=true")
require.True(t, userEnv, "expected user environment variables to be preserved")
})

t.Run("Defaults", func(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)
cmd, path := cmd(ctx, t, 0, 0)
Expand Down Expand Up @@ -176,6 +204,15 @@ func expectedOOMScore(t *testing.T) int {
return 998
}

// procEnv returns the environment variables for a given process.
func procEnv(t *testing.T, pid int) []string {
t.Helper()

env, err := os.ReadFile(fmt.Sprintf("/proc/%d/environ", pid))
require.NoError(t, err)
return strings.Split(string(env), "\x00")
}

func expectedNiceScore(t *testing.T) int {
t.Helper()

Expand Down
Loading