diff --git a/main.go b/main.go index e1ba79ee98b48e69f07e3d82d684115904009a09..5074a0f6e592a68e5d92672d4206fd9b995c2fa8 100644 --- a/main.go +++ b/main.go @@ -82,25 +82,26 @@ func renderPage(w http.ResponseWriter, req *http.Request) { func triggerPuppet(w http.ResponseWriter, req *http.Request) { cmd := exec.Command("/usr/bin/pkill", "-f", "/opt/puppetlabs/puppet/bin/puppet", "-s", "SIGUSR1") authToken := "BOFH" - if len(req.Header.Get("Authorization")) > 0 { + _, ok := req.Header["Authorization"] + if ok { authToken = strings.Split(req.Header.Get("Authorization"), "Bearer ")[1] } - - //statusFile := "/tmp/200.json" - ok := fmt.Sprintln("{\n \"status\": \"OK\",\n \"response\": 200\n}") - ko := fmt.Sprintln("{\n \"status\": \"OK\",\n \"response\": 401\n}") - //writeFile(ok, "/tmp/200.json") - //writeFile(ko, "/tmp/401.json") + okMsg := fmt.Sprintln("{\n \"status\": \"OK\",\n \"response\": 200\n}") + koMsg := fmt.Sprintln("{\n \"status\": \"OK\",\n \"response\": 401\n}") w.Header().Set("Content-Type", "application/json") if authToken != bearerToken { - http.Error(w, ko, http.StatusUnauthorized) + http.Error(w, koMsg, http.StatusUnauthorized) } else { - cmd.Run() - if verboseBool { - InfoLogger.Printf("HTTP Status %v", http.StatusOK) + err := cmd.Run() + if err != nil { + WarningLogger.Println(err) + w.WriteHeader(http.StatusServiceUnavailable) + } else { + if verboseBool { + InfoLogger.Printf("HTTP Status %v", http.StatusOK) + } + http.Error(w, okMsg, http.StatusAccepted) } - //http.ServeFile(w, req, statusFile) - http.Error(w, ok, http.StatusAccepted) } }