Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 15 additions & 6 deletions cmd/admin/handlers/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
"github.com/jmpsec/osctrl/cmd/admin/sessions"
"github.com/jmpsec/osctrl/pkg/carves"
"github.com/jmpsec/osctrl/pkg/config"
"github.com/jmpsec/osctrl/pkg/settings"
"github.com/jmpsec/osctrl/pkg/users"
"github.com/jmpsec/osctrl/pkg/utils"
"github.com/rs/zerolog/log"
)

// FaviconHandler for the favicon
func (h *HandlersAdmin) FaviconHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
w.Header().Set(utils.ContentType, "image/png")
http.ServeFile(w, r, "/static/favicon.png")
}
Expand All @@ -36,20 +37,26 @@ func (h *HandlersAdmin) ErrorHandler(w http.ResponseWriter, r *http.Request) {
// ForbiddenHandler for forbidden error requests
func (h *HandlersAdmin) ForbiddenHandler(w http.ResponseWriter, r *http.Request) {
// Debug HTTP for environment
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), true)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Send response
utils.HTTPResponse(w, "", http.StatusForbidden, errorContent)
}

// RootHandler - Handler for the root path
func (h *HandlersAdmin) RootHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
http.Redirect(w, r, "/dashboard", http.StatusFound)
}

// PermissionsGETHandler for platform/environment stats in JSON
func (h *HandlersAdmin) PermissionsGETHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Extract username and verify
usernameVar := r.PathValue("username")
if usernameVar == "" || !h.Users.Exists(usernameVar) {
Expand All @@ -76,7 +83,9 @@ func (h *HandlersAdmin) PermissionsGETHandler(w http.ResponseWriter, r *http.Req

// CarvesDownloadHandler for GET requests to download carves
func (h *HandlersAdmin) CarvesDownloadHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Get context data
ctx := r.Context().Value(sessions.ContextKey(sessions.CtxSession)).(sessions.ContextValue)
// Extract environment
Expand Down
24 changes: 24 additions & 0 deletions cmd/admin/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/jmpsec/osctrl/pkg/tags"
"github.com/jmpsec/osctrl/pkg/types"
"github.com/jmpsec/osctrl/pkg/users"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -42,6 +43,8 @@ type HandlersAdmin struct {
OsqueryTables []types.OsqueryTable
AdminConfig *config.JSONConfigurationService
DBLogger *logging.LoggerDB
DebugHTTP *zerolog.Logger
DebugHTTPConfig *config.DebugHTTPConfiguration
}

type HandlersOption func(*HandlersAdmin)
Expand Down Expand Up @@ -181,6 +184,27 @@ func WithDBLogger(dbfile string, config *backend.JSONConfigurationDB) HandlersOp
}
}

func WithDebugHTTP(cfg *config.DebugHTTPConfiguration) HandlersOption {
return func(h *HandlersAdmin) {
h.DebugHTTPConfig = cfg
h.DebugHTTP = nil
if cfg.Enabled {
l, err := logging.CreateDebugHTTP(cfg.File, logging.LumberjackConfig{
MaxSize: 25,
MaxBackups: 5,
MaxAge: 10,
Compress: true,
})
if err != nil {
log.Err(err).Msg("error creating debug HTTP logger")
l = nil
h.DebugHTTPConfig.Enabled = false
}
h.DebugHTTP = l
}
}
}

// CreateHandlersAdmin to initialize the Admin handlers struct
func CreateHandlersAdmin(opts ...HandlersOption) *HandlersAdmin {
h := &HandlersAdmin{}
Expand Down
6 changes: 3 additions & 3 deletions cmd/admin/handlers/json-carves.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (

"github.com/jmpsec/osctrl/cmd/admin/sessions"
"github.com/jmpsec/osctrl/pkg/carves"
"github.com/jmpsec/osctrl/pkg/config"
"github.com/jmpsec/osctrl/pkg/queries"
"github.com/jmpsec/osctrl/pkg/settings"
"github.com/jmpsec/osctrl/pkg/users"
"github.com/jmpsec/osctrl/pkg/utils"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -53,7 +51,9 @@ type CarveTarget struct {

// JSONCarvesHandler for JSON carves by target
func (h *HandlersAdmin) JSONCarvesHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Get context data
ctx := r.Context().Value(sessions.ContextKey(sessions.CtxSession)).(sessions.ContextValue)
// Check permissions
Expand Down
9 changes: 6 additions & 3 deletions cmd/admin/handlers/json-logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/jmpsec/osctrl/cmd/admin/sessions"
"github.com/jmpsec/osctrl/pkg/config"
"github.com/jmpsec/osctrl/pkg/settings"
"github.com/jmpsec/osctrl/pkg/types"
"github.com/jmpsec/osctrl/pkg/users"
"github.com/jmpsec/osctrl/pkg/utils"
Expand Down Expand Up @@ -60,7 +59,9 @@ type QueryLogJSON struct {

// JSONLogsHandler GET requests for JSON status/result logs by node and environment
func (h *HandlersAdmin) JSONLogsHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Extract type
logType := r.PathValue("type")
if logType == "" {
Expand Down Expand Up @@ -167,7 +168,9 @@ func (h *HandlersAdmin) JSONLogsHandler(w http.ResponseWriter, r *http.Request)

// JSONQueryLogsHandler for JSON query logs by query name
func (h *HandlersAdmin) JSONQueryLogsHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Get context data
ctx := r.Context().Value(sessions.ContextKey(sessions.CtxSession)).(sessions.ContextValue)
// Check permissions
Expand Down
9 changes: 6 additions & 3 deletions cmd/admin/handlers/json-nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/jmpsec/osctrl/cmd/admin/sessions"
"github.com/jmpsec/osctrl/pkg/config"
"github.com/jmpsec/osctrl/pkg/settings"
"github.com/jmpsec/osctrl/pkg/users"
"github.com/jmpsec/osctrl/pkg/utils"
Expand Down Expand Up @@ -41,7 +40,9 @@ type NodeJSON struct {

// JSONEnvironmentHandler - Handler for JSON endpoints by environment
func (h *HandlersAdmin) JSONEnvironmentHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
Expand Down Expand Up @@ -113,7 +114,9 @@ func (h *HandlersAdmin) JSONEnvironmentHandler(w http.ResponseWriter, r *http.Re

// JSONPlatformHandler - Handler for JSON endpoints by platform
func (h *HandlersAdmin) JSONPlatformHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Get context data
ctx := r.Context().Value(sessions.ContextKey(sessions.CtxSession)).(sessions.ContextValue)
// Check permissions
Expand Down
6 changes: 3 additions & 3 deletions cmd/admin/handlers/json-queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"net/http"

"github.com/jmpsec/osctrl/cmd/admin/sessions"
"github.com/jmpsec/osctrl/pkg/config"
"github.com/jmpsec/osctrl/pkg/queries"
"github.com/jmpsec/osctrl/pkg/settings"
"github.com/jmpsec/osctrl/pkg/users"
"github.com/jmpsec/osctrl/pkg/utils"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -125,7 +123,9 @@ func (h *HandlersAdmin) JSONSavedJSON(q queries.SavedQuery) SavedJSON {

// JSONQueryHandler - Handler for JSON queries by target
func (h *HandlersAdmin) JSONQueryHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Get context data
ctx := r.Context().Value(sessions.ContextKey(sessions.CtxSession)).(sessions.ContextValue)
// Check permissions
Expand Down
5 changes: 3 additions & 2 deletions cmd/admin/handlers/json-stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/jmpsec/osctrl/cmd/admin/sessions"
"github.com/jmpsec/osctrl/pkg/config"
"github.com/jmpsec/osctrl/pkg/nodes"
"github.com/jmpsec/osctrl/pkg/settings"
"github.com/jmpsec/osctrl/pkg/users"
Expand All @@ -22,7 +21,9 @@ var (

// JSONStatsHandler for platform/environment stats in JSON
func (h *HandlersAdmin) JSONStatsHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Get context data
ctx := r.Context().Value(sessions.ContextKey(sessions.CtxSession)).(sessions.ContextValue)
// Extract stats target
Expand Down
6 changes: 3 additions & 3 deletions cmd/admin/handlers/json-tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"net/http"

"github.com/jmpsec/osctrl/cmd/admin/sessions"
"github.com/jmpsec/osctrl/pkg/config"
"github.com/jmpsec/osctrl/pkg/settings"
"github.com/jmpsec/osctrl/pkg/users"
"github.com/jmpsec/osctrl/pkg/utils"
"github.com/rs/zerolog/log"
)

// JSONTagsHandler for platform/environment stats in JSON
func (h *HandlersAdmin) JSONTagsHandler(w http.ResponseWriter, r *http.Request) {
utils.DebugHTTPDump(r, h.Settings.DebugHTTP(config.ServiceAdmin, settings.NoEnvironmentID), false)
if h.DebugHTTPConfig.Enabled {
utils.DebugHTTPDump(h.DebugHTTP, r, h.DebugHTTPConfig.ShowBody)
}
// Get context data
ctx := r.Context().Value(sessions.ContextKey(sessions.CtxSession)).(sessions.ContextValue)
// Check permissions
Expand Down
Loading