diff --git a/certinspector/inspector.go b/certinspector/inspector.go index 8010ef11bd62224ec258acd4523c6c322c039fc8..b549f822142f2f2e407dcb1969ed37d9a498198e 100644 --- a/certinspector/inspector.go +++ b/certinspector/inspector.go @@ -172,7 +172,6 @@ func ProcessCertificates(baseDir, provider string, verboseBool bool) ([]byte, er return results[i].CertName < results[j].CertName }) - // Encode results directly into JSON jsonData, err := json.MarshalIndent(results, "", " ") if err != nil { return nil, fmt.Errorf("failed to encode JSON: %w", err) diff --git a/main.go b/main.go index 3d085b15f7d188edfc53c63282f9654af1c67ac8..74b689764d4dab366179416419866b63a2cb7dab 100644 --- a/main.go +++ b/main.go @@ -80,7 +80,6 @@ func renderPage(w http.ResponseWriter, req *http.Request) { // trigger puppet func triggerPuppet(w http.ResponseWriter, req *http.Request) { - fmt.Printf("triggerPuppet function\n") // content-type currently not working cmd := exec.Command("/usr/bin/pkill", "-f", "/opt/puppetlabs/puppet/bin/puppet", "--signal", "SIGUSR1") authToken := "BOFH" @@ -111,11 +110,72 @@ func triggerPuppet(w http.ResponseWriter, req *http.Request) { // redirect to /by_name.html func redirect(w http.ResponseWriter, req *http.Request) { - fmt.Printf("redirect function\n") redirectURL := filepath.Join(req.URL.Path, "/by_name.html") + if verboseBool { + DebugLogger.Printf("running redirect to: %v", redirectURL) + } http.Redirect(w, req, redirectURL, http.StatusMovedPermanently) } +// Custom HTTP handler with 404 fallback +func customHandler(w http.ResponseWriter, req *http.Request) { + path := req.URL.Path + + if path == "/" || path == "/index.html" || path == "/index.htm" { + //if verboseBool { + // DebugLogger.Printf("Serving file: %s", path) + //} + http.ServeFile(w, req, filepath.Join(webDir, "index.html")) + return + } + + if path == "/favicon.ico" { + http.ServeFile(w, req, filepath.Join(webDir, "static", "anvil.ico")) + } + + if path == "/ping" { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, err := w.Write([]byte(`{"status": "ok"}`)) + if err != nil { + WarningLogger.Printf("Failed to write ping response: %v", err) + } + return + } + + // Check if the path matches any valid routes + for _, apiElement := range apiURLs { + if path == apiElement { + renderJSON(w, req) + return + } + } + for _, element := range baseURLs { + if path == element { + redirect(w, req) + return + } + } + for _, otherElement := range otherURLs { + if path == otherElement { + renderPage(w, req) + return + } + } + if path == "/puppet" { + triggerPuppet(w, req) + return + } + if strings.HasPrefix(path, "/static/") { + fs := http.FileServer(http.Dir(filepath.Join(webDir, "static"))) + http.StripPrefix("/static/", fs).ServeHTTP(w, req) + return + } + + // If no route matches, return 404 + http.NotFound(w, req) +} + func main() { progName := filepath.Base(os.Args[0]) @@ -173,13 +233,13 @@ Options: baseURLs = append(baseURLs, "/"+provider, "/"+provider+"/") } for _, provider := range acmeProviders { - otherURLs = append(otherURLs, "/"+provider+"/by_name.html", "/"+provider+"/by_date.html") + otherURLs = append( + otherURLs, "/"+provider+"/by_name.html", "/"+provider+"/by_date.html", + "/"+provider+"/"+provider+".json", "/"+provider+"/"+provider+"_expired.json", + ) } for _, provider := range acmeProviders { - apiURLs = append( - apiURLs, "/api/"+provider, "/api/"+provider+"/", - "/api/"+provider+"/"+provider+".json", "/api/"+provider+"/"+provider+"_expired.json", - ) + apiURLs = append(apiURLs, "/api/"+provider, "/api/"+provider+"/") } if verboseBool { DebugLogger.Printf("Serving baseURLs: %v", baseURLs) @@ -187,28 +247,7 @@ Options: DebugLogger.Printf("Serving otherURLs: %v", otherURLs) } - puppetURL := "/puppet" - - fs := http.FileServer(http.Dir(filepath.Join(webDir, "static"))) - http.Handle("/static/", http.StripPrefix("/static/", fs)) - - http.HandleFunc(puppetURL, triggerPuppet) - - http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { - http.ServeFile(res, req, filepath.Join(webDir, "index.html")) - }) - - for _, apiElement := range apiURLs { - http.HandleFunc(apiElement, renderJSON) - } - - for _, element := range baseURLs { - http.HandleFunc(element, redirect) - } - - for _, otherElement := range otherURLs { - http.HandleFunc(otherElement, renderPage) - } + http.HandleFunc("/", customHandler) if listenAddress == "any" { log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", listenPort), nil))