mirror of
https://github.com/adnanh/webhook.git
synced 2025-06-17 18:12:28 +00:00
add run-as to run hook in other user context
This commit is contained in:
parent
f187592147
commit
af3ceffdac
5 changed files with 47 additions and 0 deletions
32
setuser.go
Normal file
32
setuser.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// sets user for the command to execute
|
||||
func setUser(cmd *exec.Cmd, username string) {
|
||||
user, err := user.Lookup(username)
|
||||
if err != nil {
|
||||
log.Printf("[%s] error lookup user: %s\n", username, err)
|
||||
return
|
||||
}
|
||||
uid, err := strconv.ParseUint(user.Uid, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("Uid [%s] is not an decimal value: %s\n", user.Uid, err)
|
||||
return
|
||||
}
|
||||
gid, err := strconv.ParseUint(user.Gid, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("Uid [%s] is not an decimal value: %s\n", user.Uid, err)
|
||||
return
|
||||
}
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Credential: &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue