func (h *HandlersAdmin) QueryRunGETHandler(w http.ResponseWriter, r *http.Request) {
	h.Inc(metricAdminReq)
	utils.DebugHTTPDump(r, h.Settings.DebugHTTP(settings.ServiceAdmin), false)
	ctx := r.Context().Value(sessions.ContextKey("session")).(sessions.ContextValue)
	if !h.Users.CheckPermissions(ctx[sessions.CtxUser], users.QueryLevel, users.NoEnvironment) {
		log.Printf("%s has insuficient permissions", ctx[sessions.CtxUser])
		h.Inc(metricAdminErr)
		return
	}
	t, err := template.ParseFiles(
		templatesFilesFolder+"/queries-run.html",
		templatesFilesFolder+"/components/page-head.html",
		templatesFilesFolder+"/components/page-js.html",
		templatesFilesFolder+"/components/page-aside-right.html",
		templatesFilesFolder+"/components/page-aside-left.html",
		templatesFilesFolder+"/components/page-header.html",
		templatesFilesFolder+"/components/page-modals.html")
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting table template: %v", err)
		return
	}
	envAll, err := h.Envs.All()
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting environments %v", err)
		return
	}
	platforms, err := h.Nodes.GetAllPlatforms()
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting platforms: %v", err)
		return
	}
	nodes, err := h.Nodes.Gets("active", h.Settings.InactiveHours())
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting all nodes: %v", err)
		return
	}
	var uuids, hosts []string
	for _, n := range nodes {
		uuids = append(uuids, n.UUID)
		hosts = append(hosts, n.Localname)
	}
	templateData := QueryRunTemplateData{
		Title:         "Query osquery Nodes",
		Metadata:      h.TemplateMetadata(ctx, h.ServiceVersion),
		Environments:  h.allowedEnvironments(ctx[sessions.CtxUser], envAll),
		Platforms:     platforms,
		UUIDs:         uuids,
		Hosts:         hosts,
		Tables:        h.OsqueryTables,
		TablesVersion: osqueryTablesVersion,
	}
	if err := t.Execute(w, templateData); err != nil {
		h.Inc(metricAdminErr)
		log.Printf("template error %v", err)
		return
	}
	if h.Settings.DebugService(settings.ServiceAdmin) {
		log.Println("DebugService: Query run template served")
	}
	h.Inc(metricAdminOK)
}
QueryRunGETHandler for GET requests to run queries
package handlers
import (
	"html/template"
	"log"
	"net/http"
	"strings"
	"github.com/gorilla/mux"
	"github.com/jmpsec/osctrl/admin/sessions"
	"github.com/jmpsec/osctrl/carves"
	"github.com/jmpsec/osctrl/environments"
	"github.com/jmpsec/osctrl/settings"
	"github.com/jmpsec/osctrl/users"
	"github.com/jmpsec/osctrl/utils"
)
const (
	templatesFilesFolder string = "tmpl_admin"
)
type TemplateFiles struct {
	filepaths []string
}
func (h *HandlersAdmin) TemplateMetadata(ctx sessions.ContextValue, version string) TemplateMetadata {
	return TemplateMetadata{
		Username:       ctx[sessions.CtxUser],
		Level:          ctx[sessions.CtxLevel],
		CSRFToken:      ctx[sessions.CtxCSRF],
		Service:        "osctrl-admin",
		Version:        version,
		TLSDebug:       h.Settings.DebugService(settings.ServiceTLS),
		AdminDebug:     h.Settings.DebugService(settings.ServiceAdmin),
		APIDebug:       h.Settings.DebugService(settings.ServiceAPI),
		AdminDebugHTTP: h.Settings.DebugHTTP(settings.ServiceAdmin),
		APIDebugHTTP:   h.Settings.DebugHTTP(settings.ServiceAPI),
	}
}
func NewTemplateFiles(base string, layoutFilename string) *TemplateFiles {
	paths := []string{
		base + "/" + layoutFilename,
		base + "/components/page-head.html",
		base + "/components/page-js.html",
		base + "/components/page-header.html",
		base + "/components/page-aside-left.html",
		base + "/components/page-aside-right.html",
		base + "/components/page-modals.html",
	}
	tf := TemplateFiles{filepaths: paths}
	return &tf
}
func (h *HandlersAdmin) LoginHandler(w http.ResponseWriter, r *http.Request) {
	h.Inc(metricAdminReq)
	utils.DebugHTTPDump(r, h.Settings.DebugHTTP(settings.ServiceAdmin), false)
	t, err := template.ParseFiles(
		templatesFilesFolder+"/login.html",
		templatesFilesFolder+"/components/page-head.html",
		templatesFilesFolder+"/components/page-js.html")
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting login template: %v", err)
		return
	}
	templateData := LoginTemplateData{
		Title:   "Login to osctrl",
		Project: "osctrl",
	}
	if err := t.Execute(w, templateData); err != nil {
		h.Inc(metricAdminErr)
		log.Printf("template error %v", err)
		return
	}
	if h.Settings.DebugService(settings.ServiceAdmin) {
		log.Println("DebugService: Login template served")
	}
	h.Inc(metricAdminOK)
}
func (h *HandlersAdmin) EnvironmentHandler(w http.ResponseWriter, r *http.Request) {
	h.Inc(metricAdminReq)
	utils.DebugHTTPDump(r, h.Settings.DebugHTTP(settings.ServiceAdmin), false)
	vars := mux.Vars(r)
	envVar, ok := vars["environment"]
	if !ok {
		h.Inc(metricAdminErr)
		log.Println("error getting environment")
		return
	}
	if !h.Envs.Exists(envVar) {
		h.Inc(metricAdminErr)
		log.Printf("error unknown environment (%s)", envVar)
		return
	}
	env, err := h.Envs.Get(envVar)
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting environment: %v", err)
		return
	}
	ctx := r.Context().Value(sessions.ContextKey("session")).(sessions.ContextValue)
	if !h.Users.CheckPermissions(ctx[sessions.CtxUser], users.EnvLevel, env.Name) {
		log.Printf("%s has insuficient permissions", ctx[sessions.CtxUser])
		h.Inc(metricTokenErr)
		return
	}
	target, ok := vars["target"]
	if !ok {
		h.Inc(metricAdminErr)
		log.Println("error getting target")
		return
	}
	tempateFiles := NewTemplateFiles(templatesFilesFolder, "table.html").filepaths
	t, err := template.ParseFiles(tempateFiles...)
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting table template: %v", err)
		return
	}
	tags, err := h.Tags.All()
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting tags %v", err)
		return
	}
	envAll, err := h.Envs.All()
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting environments %v", err)
		return
	}
	platforms, err := h.Nodes.GetAllPlatforms()
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting platforms: %v", err)
		return
	}
	templateData := TableTemplateData{
		Title:        "Nodes in " + env.Name,
		Metadata:     h.TemplateMetadata(ctx, h.ServiceVersion),
		Selector:     "environment",
		SelectorName: env.Name,
		Target:       target,
		Tags:         tags,
		Environments: h.allowedEnvironments(ctx[sessions.CtxUser], envAll),
		Platforms:    platforms,
	}
	if err := t.Execute(w, templateData); err != nil {
		h.Inc(metricAdminErr)
		log.Printf("template error %v", err)
		return
	}
	if h.Settings.DebugService(settings.ServiceAdmin) {
		log.Println("DebugService: Environment table template served")
	}
	h.Inc(metricAdminOK)
}
func (h *HandlersAdmin) PlatformHandler(w http.ResponseWriter, r *http.Request) {
	h.Inc(metricAdminReq)
	utils.DebugHTTPDump(r, h.Settings.DebugHTTP(settings.ServiceAdmin), false)
	vars := mux.Vars(r)
	platform, ok := vars["platform"]
	if !ok {
		h.Inc(metricAdminErr)
		log.Println("error getting platform")
		return
	}
	target, ok := vars["target"]
	if !ok {
		h.Inc(metricAdminErr)
		log.Println("error getting target")
		return
	}
	ctx := r.Context().Value(sessions.ContextKey("session")).(sessions.ContextValue)
	if !h.Users.CheckPermissions(ctx[sessions.CtxUser], users.AdminLevel, users.NoEnvironment) {
		log.Printf("%s has insuficient permissions", ctx[sessions.CtxUser])
		h.Inc(metricTokenErr)
		return
	}
	t, err := template.ParseFiles(
		templatesFilesFolder+"/table.html",
		templatesFilesFolder+"/components/page-head.html",
		templatesFilesFolder+"/components/page-js.html",
		templatesFilesFolder+"/components/page-aside-right.html",
		templatesFilesFolder+"/components/page-aside-left.html",
		templatesFilesFolder+"/components/page-header.html",
		templatesFilesFolder+"/components/page-modals.html")
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting table template: %v", err)
		return
	}
	tags, err := h.Tags.All()
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting tags %v", err)
		return
	}
	envAll, err := h.Envs.All()
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting environments %v", err)
		return
	}
	platforms, err := h.Nodes.GetAllPlatforms()
	if err != nil {
		h.Inc(metricAdminErr)
		log.Printf("error getting platforms: %v", err)
		return
	}
	templateData := TableTemplateData{
		Title:        "Nodes in " + platform,
		Metadata:     h.TemplateMetadata(ctx, h.ServiceVersion),
		Selector:     "platform",
		SelectorName: platform,
		Target:       target,
		Tags:         tags,
		Environments: h.allowedEnvironments(ctx[sessions.CtxUser], envAll),
		Platforms:    platforms,
	}
	if err := t.Execute(w, templateData); err != nil {
		h.Inc(metricAdminErr)
		log.Printf("template error %v", err)
		return
	}
	if h.Settings.DebugService(settings.ServiceAdmin) {
		log.Println("DebugService: Platform table template served")
	}
	h.Inc(metricAdminOK)
}