Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add run-as to run hook in other user context #645

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Hook-Definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Hooks are defined as objects in the JSON or YAML hooks configuration file. Pleas

* `id` - specifies the ID of your hook. This value is used to create the HTTP endpoint (http://yourserver:port/hooks/your-hook-id)
* `execute-command` - specifies the command that should be executed when the hook is triggered
* `run-as` - specifies a different user to run the command with
* `command-working-directory` - specifies the working directory that will be used for the script when it's executed
* `response-message` - specifies the string that will be returned to the hook initiator
* `response-headers` - specifies the list of headers in format `{"name": "X-Example-Header", "value": "it works"}` that will be returned in HTTP response for the hook
Expand Down
1 change: 1 addition & 0 deletions internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ func (h *HooksFiles) Set(value string) error {
type Hook struct {
ID string `json:"id,omitempty"`
ExecuteCommand string `json:"execute-command,omitempty"`
RunAs string `json:"run-as,omitempty"`
CommandWorkingDirectory string `json:"command-working-directory,omitempty"`
ResponseMessage string `json:"response-message,omitempty"`
ResponseHeaders ResponseHeaders `json:"response-headers,omitempty"`
Expand Down
32 changes: 32 additions & 0 deletions setuser.go
Original file line number Diff line number Diff line change
@@ -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)}}
}
10 changes: 10 additions & 0 deletions setuser_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build windows
// +build windows

package main

import "os/exec"

func setUser(cmd *exec.Cmd, username string) {
// NOOP: Windows doesn't have setuid setgid equivalent to the Unix world.
}
3 changes: 3 additions & 0 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,9 @@ func handleHook(h *hook.Hook, r *hook.Request) (string, error) {
}

cmd := exec.Command(cmdPath)
if h.RunAs != "" {
setUser(cmd, h.RunAs)
}
cmd.Dir = h.CommandWorkingDirectory

cmd.Args, errors = h.ExtractCommandArguments(r)
Expand Down