8000 add test · coder/coder@d53777d · GitHub
[go: up one dir, main page]

Skip to content

Commit d53777d

Browse files
committed
add test
1 parent 157e445 commit d53777d

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

agent/agentexec/cli_linux.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ func CLI() error {
6767

6868
// We drop effective caps prior to setting dumpable so that we limit the
6969
// impact of someone attempting to hijack the process (i.e. with a debugger)
70-
// to take advantage of the capabilities of the agent process.
70+
// to take advantage of the capabilities of the agent process. We encourage
71+
// users to set cap_net_admin on the agent binary for improved networking
72+
// performance and doing so results in the process having its SET_DUMPABLE
73+
// attribute disabled (meaning we cannot adjust the oom score).
7174
err = dropEffectiveCaps()
7275
if err != nil {
7376
printfStdErr("failed to drop effective caps: %v", err)

agent/agentexec/cli_linux_test.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/stretchr/testify/require"
2020
"golang.org/x/sys/unix"
21+
"golang.org/x/xerrors"
2122

2223
"github.com/coder/coder/v2/testutil"
2324
)
@@ -50,6 +51,32 @@ func TestCLI(t *testing.T) {
5051
requireOOMScore(t, cmd.Process.Pid, expectedOOM)
5152
requireNiceScore(t, cmd.Process.Pid, expectedNice)
5253
})
54+
55+
t.Run("Capabilities", func(t *testing.T) {
56+
testdir := filepath.Dir(TestBin)
57+
capDir := filepath.Join(testdir, "caps")
58+
err := os.Mkdir(capDir, 0o755)
59+
require.NoError(t, err)
60+
bin := buildBinary(capDir)
61+
// Try to set capabilities on the binary. This should work fine in CI but
62+
// it's possible some developers may be working in an environment where they don't have the necessary permissions.
63+
err = setCaps(t, bin, "cap_net_admin")
64+
if os.Getenv("CI") != "" {
65+
require.NoError(t, err)
66+
} else if err != nil {
67+
t.Skipf("unable to set capabilities for test: %v", err)
68+
}
69+
ctx := testutil.Context(t, testutil.WaitMedium)
70+
cmd, path := binCmd(ctx, t, bin, 123, 12)
71+
err = cmd.Start()
72+
require.NoError(t, err)
73+
go cmd.Wait()
74+
75+
waitForSentinel(ctx, t, cmd, path)
76+
// This is what we're really testing, a binary with added capabilities requires setting dumpable.
77+
requireOOMScore(t, cmd.Process.Pid, 123)
78+
requireNiceScore(t, cmd.Process.Pid, 12)
79+
})
5380
}
5481

5582
func requireNiceScore(t *testing.T, pid int, score int) {
@@ -94,7 +121,7 @@ func waitForSentinel(ctx context.Context, t *testing.T, cmd *exec.Cmd, path stri
94121
}
95122
}
96123

97-
func cmd(ctx context.Context, t *testing.T, oom, nice int) (*exec.Cmd, string) {
124+
func binCmd(ctx context.Context, t *testing.T, bin string, oom, nice int) (*exec.Cmd, string) {
98125
var (
99126
args = execArgs(oom, nice)
100127
dir = t.TempDir()
@@ -103,7 +130,7 @@ func cmd(ctx context.Context, t *testing.T, oom, nice int) (*exec.Cmd, string) {
103130

104131
args = append(args, "sh", "-c", fmt.Sprintf("touch %s && sleep 10m", file))
105132
//nolint:gosec
106-
cmd := exec.CommandContext(ctx, TestBin, args...)
133+
cmd := exec.CommandContext(ctx, bin, args...)
107134

108135
// We set this so we can also easily kill the sleep process the shell spawns.
109136
cmd.SysProcAttr = &syscall.SysProcAttr{
@@ -125,6 +152,11 @@ func cmd(ctx context.Context, t *testing.T, oom, nice int) (*exec.Cmd, string) {
125152
}
126153
})
127154
return cmd, file
155+
156+
}
157+
158+
func cmd(ctx context.Context, t *testing.T, oom, nice int) (*exec.Cmd, string) {
159+
return binCmd(ctx, t, TestBin, oom, nice)
128160
}
129161

130162
func expectedOOMScore(t *testing.T) int {
@@ -171,3 +203,14 @@ func execArgs(oom int, nice int) []string {
171203
execArgs = append(execArgs, "--")
172204
return execArgs
173205
}
206+
207+
func setCaps(t *testing.T, bin string, caps ...string) error {
208+
t.Helper()
209+
210+
setcap := fmt.Sprintf("sudo setcap %s=ep %s", strings.Join(caps, ", "), bin)
211+
out, err := exec.CommandContext(context.Background(), "sh", "-c", setcap).CombinedOutput()
212+
if err != nil {
213+
return xerrors.Errorf("setcap %q (%s): %w", setcap, out, err)
214+
}
215+
return nil
216+
}

0 commit comments

Comments
 (0)
0