From 5b557ee3393a1a3e075673dc307abdddd6662727 Mon Sep 17 00:00:00 2001 From: Massimiliano Adamo <maxadamo@gmail.com> Date: Thu, 1 Sep 2022 13:46:18 +0200 Subject: [PATCH] fix missing toekn --- main.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index e1ba79e..5074a0f 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) } } -- GitLab