implements allowing sudo prefixes in front of the command

This commit is contained in:
Jakob Oberhummer 2018-05-08 20:36:07 +02:00
parent 10396a5434
commit b65c836bcd

View File

@ -19,11 +19,12 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/satori/go.uuid" "github.com/satori/go.uuid"
fsnotify "gopkg.in/fsnotify.v1" "gopkg.in/fsnotify.v1"
) )
const ( const (
version = "2.6.8" version = "2.6.8"
sudoPrefix = "sudo"
) )
var ( var (
@ -202,7 +203,8 @@ func main() {
func hookHandler(w http.ResponseWriter, r *http.Request) { func hookHandler(w http.ResponseWriter, r *http.Request) {
// generate a request id for logging // generate a request id for logging
rid := uuid.NewV4().String()[:6] newUid, _ := uuid.NewV4()
rid := string(newUid[:6])
log.Printf("[%s] incoming HTTP request from %s\n", rid, r.RemoteAddr) log.Printf("[%s] incoming HTTP request from %s\n", rid, r.RemoteAddr)
@ -323,17 +325,9 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
func handleHook(h *hook.Hook, rid string, headers, query, payload *map[string]interface{}, body *[]byte) (string, error) { func handleHook(h *hook.Hook, rid string, headers, query, payload *map[string]interface{}, body *[]byte) (string, error) {
var errors []error var errors []error
// check the command exists cmdPath, err := getAndCheckCommandPath(h.ExecuteCommand)
cmdPath, err := exec.LookPath(h.ExecuteCommand)
if err != nil { if err != nil {
log.Printf("unable to locate command: '%s'", h.ExecuteCommand)
// check if parameters specified in execute-command by mistake
if strings.IndexByte(h.ExecuteCommand, ' ') != -1 {
s := strings.Fields(h.ExecuteCommand)[0]
log.Printf("use 'pass-arguments-to-command' to specify args for '%s'", s)
}
return "", err return "", err
} }
@ -528,3 +522,38 @@ func valuesToMap(values map[string][]string) map[string]interface{} {
return ret return ret
} }
func getAndCheckCommandPath(command string) (string, error) {
var newCommand string
if command[:4] == sudoPrefix {
newCommand = command[4:]
_, err := exec.LookPath(sudoPrefix)
if err != nil {
log.Printf(sudoPrefix + " command not found")
return "", err
}
} else {
newCommand = command
}
// check the command exists
cmdPath, err := exec.LookPath(newCommand)
if err != nil {
log.Printf("unable to locate command: '%s'", newCommand)
// check if parameters specified in execute-command by mistake
if strings.IndexByte(newCommand, ' ') != -1 {
s := strings.Fields(newCommand)[0]
log.Printf("use 'pass-arguments-to-command' to specify args for '%s'", s)
}
return "", err
}
return sudoPrefix + " " + cmdPath, nil
}