-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package tablehelpers | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os/exec" | ||
"os/user" | ||
"strconv" | ||
"syscall" | ||
) | ||
|
||
func runCmdAsUser(cmd *exec.Cmd, uid string) ([]byte, error) { | ||
Check failure on line 12 in ee/tables/tablehelpers/run_as_user.go GitHub Actions / lint (ubuntu-latest)
Check failure on line 12 in ee/tables/tablehelpers/run_as_user.go GitHub Actions / lint (ubuntu-latest)
Check failure on line 12 in ee/tables/tablehelpers/run_as_user.go GitHub Actions / lint (macos-latest)
|
||
currentUser, err := user.Current() | ||
if err != nil { | ||
return nil, fmt.Errorf("getting current user: %w", err) | ||
} | ||
|
||
runningUser, err := user.LookupId(uid) | ||
if err != nil { | ||
return nil, fmt.Errorf("looking up user with uid %s: %w", uid, err) | ||
} | ||
|
||
if currentUser.Uid != "0" && currentUser.Uid != runningUser.Uid { | ||
return nil, fmt.Errorf("current user %s is not root and can't start process for other user %s", currentUser.Uid, uid) | ||
} | ||
|
||
runningUserUid, err := strconv.ParseUint(runningUser.Uid, 10, 32) | ||
if err != nil { | ||
return nil, fmt.Errorf("converting uid %s to int: %w", runningUser.Uid, err) | ||
} | ||
|
||
runningUserGid, err := strconv.ParseUint(runningUser.Gid, 10, 32) | ||
if err != nil { | ||
return nil, fmt.Errorf("converting gid %s to int: %w", runningUser.Gid, err) | ||
} | ||
|
||
cmd.SysProcAttr = &syscall.SysProcAttr{ | ||
Credential: &syscall.Credential{ | ||
Check failure on line 38 in ee/tables/tablehelpers/run_as_user.go GitHub Actions / govulncheck (windows-latest)
Check failure on line 38 in ee/tables/tablehelpers/run_as_user.go GitHub Actions / govulncheck (windows-latest)
Check failure on line 38 in ee/tables/tablehelpers/run_as_user.go GitHub Actions / launcher (windows-latest)
Check failure on line 38 in ee/tables/tablehelpers/run_as_user.go GitHub Actions / launcher (windows-latest)
Check failure on line 38 in ee/tables/tablehelpers/run_as_user.go GitHub Actions / launcher (windows-latest)
|
||
Uid: uint32(runningUserUid), | ||
Gid: uint32(runningUserGid), | ||
}, | ||
} | ||
|
||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return nil, fmt.Errorf("assigning command StdoutPipe: %w", err) | ||
} | ||
|
||
if err := cmd.Start(); err != nil { | ||
return nil, fmt.Errorf("starting command: %w", err) | ||
} | ||
|
||
data, err := io.ReadAll(stdout) | ||
if err != nil { | ||
return nil, fmt.Errorf("ReadAll command stdout: %w", err) | ||
} | ||
|
||
if err := cmd.Wait(); err != nil { | ||
return nil, fmt.Errorf("deallocate command: %w", err) | ||
} | ||
|
||
return data, nil | ||
} |