fix(agentexec): run gate commands as the harness user and reap the process group after each invocation
This commit is contained in:
3 files changed
+33
-4
No files matched your search
@@ -11,6 +11,9 @@ import (
|
||||
// process groups are unavailable.
|
||||
func setProcessGroup(*exec.Cmd) {}
|
||||
|
||||
// reapGroup has nothing to do where process groups are unavailable.
|
||||
func reapGroup(*exec.Cmd) {}
|
||||
|
||||
// runAsHarnessUser refuses a named harness user where user switching is
|
||||
// unavailable, rather than running the harness as the worker.
|
||||
func runAsHarnessUser(_ *exec.Cmd, name string) error {
|
||||
|
||||
@@ -22,6 +22,14 @@ func setProcessGroup(cmd *exec.Cmd) {
|
||||
cmd.Cancel = func() error { return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) }
|
||||
}
|
||||
|
||||
// reapGroup kills whatever the command left running in its process group, so a
|
||||
// daemonised child cannot outlive the invocation that started it.
|
||||
func reapGroup(cmd *exec.Cmd) {
|
||||
if cmd.Process != nil {
|
||||
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
}
|
||||
}
|
||||
|
||||
// runAsHarnessUser drops the invocation to the Kit's user. A worker running
|
||||
// as root refuses to start a harness with no user, because the harness would
|
||||
// then share the worker's user and could read its credentials.
|
||||
|
||||
@@ -133,7 +133,7 @@ func (r *HarnessRunner) run(ctx context.Context, workspace string, req agentrun.
|
||||
return res, err
|
||||
}
|
||||
|
||||
gateOutput, gateErr := runHarnessGate(runCtx, req.Gate.Commands, workspace)
|
||||
gateOutput, gateErr := runHarnessGate(runCtx, req.Harness, req.Gate.Commands, workspace)
|
||||
if gateErr == nil {
|
||||
res.Status = agentrun.StatusPassed
|
||||
return res, nil
|
||||
@@ -270,8 +270,16 @@ func invoke(ctx context.Context, workspace string, h *agentrun.HarnessSpec, argv
|
||||
for scanner.Scan() {
|
||||
out.Line(scanner.Bytes(), report)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, output)
|
||||
if err := cmd.Wait(); err != nil {
|
||||
if err := scanner.Err(); err != nil {
|
||||
// Stop the CLI rather than leave it writing into a pipe nobody parses.
|
||||
_ = cmd.Cancel()
|
||||
_, _ = io.Copy(io.Discard, output)
|
||||
_ = cmd.Wait()
|
||||
return fmt.Errorf("read harness output: %w", err)
|
||||
}
|
||||
waitErr := cmd.Wait()
|
||||
reapGroup(cmd)
|
||||
if err := waitErr; err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
@@ -304,14 +312,24 @@ func harnessPrompt(workspace string, req agentrun.Request) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func runHarnessGate(ctx context.Context, commands []agentrun.Command, workspace string) (string, error) {
|
||||
// runHarnessGate runs the gate commands confined like the harness: they
|
||||
// execute code the harness just wrote, so they get its user, its environment
|
||||
// and its process group rather than the worker's.
|
||||
func runHarnessGate(ctx context.Context, h *agentrun.HarnessSpec, commands []agentrun.Command, workspace string) (string, error) {
|
||||
for _, c := range commands {
|
||||
if len(c.Argv) == 0 {
|
||||
continue
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, c.Argv[0], c.Argv[1:]...)
|
||||
cmd.Dir = workspace
|
||||
cmd.WaitDelay = killGrace
|
||||
cmd.Env = append([]string{}, h.Env...)
|
||||
setProcessGroup(cmd)
|
||||
if err := runAsHarnessUser(cmd, h.User); err != nil {
|
||||
return "", err
|
||||
}
|
||||
output, err := cmd.CombinedOutput()
|
||||
reapGroup(cmd)
|
||||
switch {
|
||||
case c.ExpectFailure && err == nil:
|
||||
return fmt.Sprintf("[%s] expected this command to fail, but it passed:\n%s", c.Name, clipGate(output)),
|
||||
|
||||
Reference in new issue
Block a user