Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wl/stored audits #188

Merged
merged 16 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func main() {
auditOutputURL := flag.String("output-url", "", "Destination URL to send audit results")
auditOutputFile := flag.String("output-file", "", "Destination file for audit results")
auditOutputFormat := flag.String("output-format", "json", "Output format for results - json, yaml, or score")
loadAuditFile := flag.String("load-audit-file", "", "Runs the dashboard with data saved from a past audit.")
endzyme marked this conversation as resolved.
Show resolved Hide resolved
displayName := flag.String("display-name", "", "An optional identifier for the audit")
configPath := flag.String("config", "", "Location of Polaris configuration file")
logLevel := flag.String("log-level", logrus.InfoLevel.String(), "Logrus log level")
Expand Down Expand Up @@ -103,7 +104,37 @@ func main() {
if *webhook {
startWebhookServer(c, *disableWebhookConfigInstaller, *webhookPort)
} else if *dashboard {
startDashboardServer(c, *auditPath, *dashboardPort, *dashboardBasePath)
if *loadAuditFile == "" {
endzyme marked this conversation as resolved.
Show resolved Hide resolved
startDashboardServer(c, *auditPath, *dashboardPort, *dashboardBasePath, nil)
} else {
auditData := validator.AuditData{}
willfairwinds marked this conversation as resolved.
Show resolved Hide resolved
oldFileBytes, err := ioutil.ReadFile(*loadAuditFile)
if err != nil {
logrus.Errorf("Unable to read contents of loaded file: %v", err)
os.Exit(1)
}

if err != nil {
willfairwinds marked this conversation as resolved.
Show resolved Hide resolved
logrus.Errorf("deconding config failed: %v", err)
os.Exit(1)
}
if strings.Contains(*loadAuditFile, ".json") {
err = json.Unmarshal(oldFileBytes, &auditData)
} else if strings.Contains(*loadAuditFile, ".yaml") {
err = yaml.Unmarshal(oldFileBytes, &auditData)
} else {
logrus.Errorf("Invalid configuration type")
os.Exit(1)
}

if err != nil {
logrus.Errorf("Error parsing file contents into auditData: %v", err)
os.Exit(1)
}
startDashboardServer(c, *auditPath, *dashboardPort, *dashboardBasePath, &auditData)

}

} else if *audit {
auditData := runAndReportAudit(c, *auditPath, *auditOutputFile, *auditOutputURL, *auditOutputFormat)

Expand All @@ -118,8 +149,8 @@ func main() {
}
}

func startDashboardServer(c conf.Configuration, auditPath string, port int, basePath string) {
router := dashboard.GetRouter(c, auditPath, port, basePath)
func startDashboardServer(c conf.Configuration, auditPath string, port int, basePath string, auditData *validator.AuditData) {
router := dashboard.GetRouter(c, auditPath, port, basePath, auditData)
router.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
Expand Down
54 changes: 32 additions & 22 deletions pkg/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"net/http"
"strings"

packr "github.com/gobuffalo/packr/v2"
"github.com/gorilla/mux"
conf "github.com/fairwindsops/polaris/pkg/config"
"github.com/fairwindsops/polaris/pkg/kube"
"github.com/fairwindsops/polaris/pkg/validator"
packr "github.com/gobuffalo/packr/v2"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"gitlab.com/golang-commonmark/markdown"
)
Expand Down Expand Up @@ -136,7 +136,7 @@ func writeTemplate(tmpl *template.Template, data *templateData, w http.ResponseW
}

// GetRouter returns a mux router serving all routes necessary for the dashboard
func GetRouter(c conf.Configuration, auditPath string, port int, basePath string) *mux.Router {
func GetRouter(c conf.Configuration, auditPath string, port int, basePath string, auditData *validator.AuditData) *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
Expand All @@ -151,13 +151,23 @@ func GetRouter(c conf.Configuration, auditPath string, port int, basePath string
w.Write(favicon)
})
router.HandleFunc("/results.json", func(w http.ResponseWriter, r *http.Request) {
k, err := kube.CreateResourceProvider(auditPath)
if err != nil {
logrus.Errorf("Error fetching Kubernetes resources %v", err)
http.Error(w, "Error fetching Kubernetes resources", http.StatusInternalServerError)
return
if auditData == nil {
willfairwinds marked this conversation as resolved.
Show resolved Hide resolved
k, err := kube.CreateResourceProvider(auditPath)
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)
willfairwinds marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
http.Error(w, "Error Fetching Deployments", http.StatusInternalServerError)
return
}
}
JSONHandler(w, r, c, k)

JSONHandler(w, r, auditData)

})
router.HandleFunc("/details/{category}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Expand All @@ -178,13 +188,19 @@ func GetRouter(c conf.Configuration, auditPath string, port int, basePath string
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)
http.Error(w, "Error running audit", 500)
return
if auditData == nil {
auditData, err := validator.RunAudit(c, k)

if err != nil {
logrus.Errorf("Error getting audit data: %v", err)
http.Error(w, "Error running audit", 500)
return
}
MainHandler(w, r, auditData, basePath)
} else {
MainHandler(w, r, *auditData, basePath)
}
MainHandler(w, r, auditData, basePath)

})
return router
}
Expand Down Expand Up @@ -213,13 +229,7 @@ func MainHandler(w http.ResponseWriter, r *http.Request, auditData validator.Aud
}

// JSONHandler gets template data and renders json with it.
func JSONHandler(w http.ResponseWriter, r *http.Request, c conf.Configuration, kubeResources *kube.ResourceProvider) {
auditData, err := validator.RunAudit(c, kubeResources)
if err != nil {
http.Error(w, "Error Fetching Deployments", http.StatusInternalServerError)
return
}

func JSONHandler(w http.ResponseWriter, r *http.Request, auditData *validator.AuditData) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(auditData)
Expand Down
Empty file added pkg/test.json
Empty file.