Skip to content

Commit

Permalink
fix: #2 permission denied
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jul 3, 2024
1 parent 23009d8 commit 2cbf0b7
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,23 @@ func createHookHandler(appFlags flags.AppFlags) func(w http.ResponseWriter, r *h
}
}

func makeSureCallable(h *hook.Hook, r *hook.Request, lookpath string) (string, error) {
func makeSureCallable(h *hook.Hook, r *hook.Request) (string, error) {
// check the command exists
var lookpath string
if filepath.IsAbs(h.ExecuteCommand) || h.CommandWorkingDirectory == "" {
lookpath = h.ExecuteCommand
} else {
lookpath = filepath.Join(h.CommandWorkingDirectory, h.ExecuteCommand)
}

cmdPath, err := exec.LookPath(lookpath)
if err != nil {
log.Printf("[%s] error in %s", r.ID, err)

if strings.Contains(err.Error(), "permission denied") {
// try to make the command executable
// #nosec
err2 := os.Chmod(cmdPath, 0o755)
err2 := os.Chmod(lookpath, 0o755)
if err2 != nil {
log.Printf("[%s] make command script executable error in %s", r.ID, err2)

Expand All @@ -307,7 +315,7 @@ func makeSureCallable(h *hook.Hook, r *hook.Request, lookpath string) (string, e

log.Printf("[%s] make command script executable success", r.ID)
// retry
return makeSureCallable(h, r, lookpath)
return makeSureCallable(h, r)
}

// check if parameters specified in execute-command by mistake
Expand All @@ -325,15 +333,7 @@ func makeSureCallable(h *hook.Hook, r *hook.Request, lookpath string) (string, e
func handleHook(h *hook.Hook, r *hook.Request, w http.ResponseWriter) (string, error) {
var errors []error

// check the command exists
var lookpath string
if filepath.IsAbs(h.ExecuteCommand) || h.CommandWorkingDirectory == "" {
lookpath = h.ExecuteCommand
} else {
lookpath = filepath.Join(h.CommandWorkingDirectory, h.ExecuteCommand)
}

cmdPath, err := makeSureCallable(h, r, lookpath)
cmdPath, err := makeSureCallable(h, r)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 2cbf0b7

Please sign in to comment.