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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
sreya merged 1 commit intomainfromjon/filterenvs
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionagent/agentexec/cli_linux.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"runtime"
"slices"
"strconv"
"strings"
"syscall"
Expand DownExpand 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 deletionsagent/agentexec/cli_linux_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp