From 720729aa37036f74850fef217637e0a8ee156c6c Mon Sep 17 00:00:00 2001 From: Bobby Brennan Date: Fri, 17 May 2019 20:26:03 +0000 Subject: [PATCH] run audit on every page load --- main.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 0e7ef1997..9caa63e7d 100644 --- a/main.go +++ b/main.go @@ -89,28 +89,24 @@ func main() { if *webhook { startWebhookServer(c, *disableWebhookConfigInstaller, *webhookPort) } else if *dashboard { - k, err := kube.CreateResourceProvider(*auditPath) - if err != nil { - logrus.Errorf("Error fetching Kubernetes resources %v", err) - os.Exit(1) - } - startDashboardServer(c, k, *dashboardPort) + startDashboardServer(c, *dashboardPort) } else if *audit { - k, err := kube.CreateResourceProvider(*auditPath) - if err != nil { - logrus.Errorf("Error fetching Kubernetes resources %v", err) - os.Exit(1) - } - runAudit(c, k, *auditOutputFile, *auditOutputURL) + runAudit(c, *auditPath, *auditOutputFile, *auditOutputURL) } } -func startDashboardServer(c conf.Configuration, k *kube.ResourceProvider, port int) { +func startDashboardServer(c conf.Configuration, port int) { router := mux.NewRouter() router.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("OK")) }) router.HandleFunc("/results.json", func(w http.ResponseWriter, r *http.Request) { + k, err := kube.CreateResourceProvider("") + if err != nil { + logrus.Errorf("Error fetching Kubernetes resources %v", err) + http.Error(w, "Error fetching Kubernetes resources", http.StatusInternalServerError) + return + } dashboard.EndpointHandler(w, r, c, k) }) router.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { @@ -128,6 +124,12 @@ func startDashboardServer(c conf.Configuration, k *kube.ResourceProvider, port i http.NotFound(w, r) return } + k, err := kube.CreateResourceProvider("") + if err != nil { + logrus.Errorf("Error fetching Kubernetes resources %v", err) + http.Error(w, "Error fetching Kubernetes resources", http.StatusInternalServerError) + return + } auditData, err := validator.RunAudit(c, k) if err != nil { logrus.Errorf("Error getting audit data: %v", err) @@ -213,7 +215,12 @@ func startWebhookServer(c conf.Configuration, disableWebhookConfigInstaller bool } } -func runAudit(c conf.Configuration, k *kube.ResourceProvider, outputFile string, outputURL string) { +func runAudit(c conf.Configuration, auditPath string, outputFile string, outputURL string) { + k, err := kube.CreateResourceProvider(auditPath) + if err != nil { + logrus.Errorf("Error fetching Kubernetes resources %v", err) + os.Exit(1) + } auditData, err := validator.RunAudit(c, k) if err != nil {