kayos@tcp.direct 2024-07-18 00:54:08 -07:00
parent e20248db68
commit 92c2ea4772
No known key found for this signature in database
GPG key ID: 4B841471B4BEE979
4 changed files with 79 additions and 15 deletions

View file

@ -120,17 +120,22 @@ func Clear() {
// - noisy determines if we should print the command to the user
// noisy isn't set to true by our copy caller, as it logs differently,
// but other callers set it.
func ExecAndLogSudo(isRoot, noisy bool, cmd string) error {
if !isRoot && !strings.HasPrefix(cmd, "sudo") {
cmd = fmt.Sprintf("sudo %s", cmd)
func ExecAndLogSudo(isRoot, noisy bool, exe string, args ...string) error {
if !isRoot && exe != "sudo" {
og := exe
exe = "sudo"
newArgs := make([]string, 0)
newArgs = append(newArgs, og)
newArgs = append(newArgs, args...)
args = newArgs
}
// Write to logger
logger.Printf("Executing (elevated): %s\n", cmd)
logger.Printf("Executing (elevated): %s %s\n", exe, strings.Join(args, " "))
if noisy {
// Print to the user
fmt.Printf("Executing (elevated): %s\nSee debug.log for detailed output\n", cmd)
fmt.Printf("Executing (elevated): %s %s\nSee debug.log for detailed output\n", exe, strings.Join(args, " "))
}
wd, err := os.Getwd()
@ -138,8 +143,7 @@ func ExecAndLogSudo(isRoot, noisy bool, cmd string) error {
return err
}
cs := strings.Fields(cmd)
r := exec.Command(cs[0], cs[1:]...)
r := exec.Command(exe, args...)
r.Dir = wd
cmdCombinedOut, err := r.CombinedOutput()
@ -155,7 +159,7 @@ func ExecAndLogSudo(isRoot, noisy bool, cmd string) error {
}
if err != nil {
err = fmt.Errorf("failed to execute %s: %w\n%s", cmd, err, outStr)
err = fmt.Errorf("failed to execute %s: %w\n%s", exe, err, outStr)
}
return err

View file

@ -0,0 +1,61 @@
package command
import (
"os"
"path/filepath"
"strings"
"testing"
)
const fakeSudo = `#!/bin/sh
"$@" -qptest`
const fakeUtil = `#!/bin/sh
echo "$@"
if [ "$4" = "-qptest" ]; then exit 0; else exit 1; fi`
func setupExecTestEnv(t *testing.T) (string, string) {
t.Helper()
tmpDir := t.TempDir()
fakeSudoPath := filepath.Join(tmpDir, "sudo")
fakeUtilPath := filepath.Join(tmpDir, "util")
if err := os.WriteFile(fakeSudoPath, []byte(fakeSudo), 0755); err != nil {
t.Fatalf("failed to write fake sudo stub: %s", err.Error())
}
if err := os.WriteFile(fakeUtilPath, []byte(fakeUtil), 0755); err != nil {
t.Fatalf("failed to write fake util stub: %s", err.Error())
}
t.Setenv("PATH", tmpDir+":"+os.Getenv("PATH"))
return fakeSudoPath, fakeUtilPath
}
func TestExecAndLogSudo(t *testing.T) {
_, fakeUtilPath := setupExecTestEnv(t)
args := []string{"i am a string with spaces", "i came to ruin parsers and chew bubble gum", "and I'm all out of bubblegum."}
t.Run("is_not_root", func(t *testing.T) {
if err := ExecAndLogSudo(false, false, "util", args...); err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
})
t.Run("is_root", func(t *testing.T) {
newFakeUtil := strings.Replace(fakeUtil, "exit 1", "exit 0", 1)
newFakeUtil = strings.Replace(newFakeUtil, "exit 0", "exit 1", 1)
if err := os.WriteFile(fakeUtilPath, []byte(newFakeUtil), 0755); err != nil {
t.Fatalf("failed to overwrite fake util with modified stub: %s", err.Error())
}
if err := ExecAndLogSudo(false, false, "util", args...); err == nil {
t.Errorf("expected error when using modified util with sudo, got nil")
}
if err := ExecAndLogSudo(true, true, "util", args...); err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
})
}