diff --git a/main.go b/main.go index 57787e41e521345b4d73794dd7eec2eb578d5bdf..4a56fa9c5f8af6de03601a55e916d27dab1f24e8 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,15 @@ func init() { ErrorLogger = log.New(os.Stdout, "ERROR: ", log.Ldate|log.Ltime) } +// write to file +func writeFile(fileContent string, filePath string) { + content := []byte(fileContent) + err := os.WriteFile(filePath, content, 0644) + if err != nil { + WarningLogger.Println(err) + } +} + // serve certificates JSON func renderJSON(w http.ResponseWriter, req *http.Request) { provider := strings.Split(req.URL.Path, "/")[2] @@ -69,21 +78,15 @@ func renderPage(w http.ResponseWriter, req *http.Request) { // trigger puppet func triggerPuppet(w http.ResponseWriter, req *http.Request) { - cmd := exec.Command("/usr/bin/pkill", "-f", "/opt/puppetlabs/puppet/bin/puppet", "-s", "SIGUSR1") + //cmd := exec.Command("/usr/bin/pkill", "-f", "/opt/puppetlabs/puppet/bin/puppet", "-s", "SIGUSR1") + cmd := exec.Command("/usr/bin/touch", "/TEST") authToken := strings.Split(req.Header.Get("Authorization"), "Bearer ")[1] statusFile := "/tmp/200.json" - fmt.Fprintf(w, "Welcome, "+authToken+"\r\n") - // create json files on the fly - okMsg := []byte("{\n \"status\": \"OK\",\n \"response\": 200\n }") - koMsg := []byte("{\n \"status\": \"KO\",\n \"response\": 503\n }") - okErr := os.WriteFile("/tmp/200.json", okMsg, 0644) - koErr := os.WriteFile("/tmp/503.json", koMsg, 0644) - if okErr != nil || koErr != nil { - statusFile = "/tmp/503.json" - w.WriteHeader(http.StatusUnauthorized) - } else if authToken != bearerToken { + writeFile(fmt.Sprintf("{\n \"status\": \"OK\",\n \"response\": 200\n \"token\": %v}", authToken), "/tmp/200.json") + writeFile(fmt.Sprintf("{\n \"status\": \"KO\",\n \"response\": 503\n \"token\": %v}", authToken), "/tmp/503.json") + if authToken != bearerToken { statusFile = "/tmp/503.json" - w.WriteHeader(http.StatusUnauthorized) + //w.WriteHeader(http.StatusUnauthorized) } else { cmd.Run() if verboseBool {