From 95df8a4bb540a77fb29a1eb97b6b309cbcf8d701 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Wed, 17 Jan 2024 16:07:27 -0500 Subject: [PATCH] fix: Initialize LoggingMiddleware after ContextMiddleware to ensure it is populated for log generation (#323) --- cmd/api/src/api/registration/registration.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cmd/api/src/api/registration/registration.go b/cmd/api/src/api/registration/registration.go index b81f4f2757..76204dd8ef 100644 --- a/cmd/api/src/api/registration/registration.go +++ b/cmd/api/src/api/registration/registration.go @@ -17,6 +17,8 @@ package registration import ( + "net/http" + "github.com/specterops/bloodhound/cache" "github.com/specterops/bloodhound/dawgs/graph" "github.com/specterops/bloodhound/src/api" @@ -28,19 +30,18 @@ import ( "github.com/specterops/bloodhound/src/config" "github.com/specterops/bloodhound/src/daemons/datapipe" "github.com/specterops/bloodhound/src/database" - "net/http" ) func RegisterFossGlobalMiddleware(routerInst *router.Router, cfg config.Configuration, identityResolver auth.IdentityResolver, authenticator api.Authenticator) { - // Set up logging - if cfg.EnableAPILogging { - routerInst.UsePrerouting(middleware.LoggingMiddleware(cfg, identityResolver)) - } - // Set up the middleware stack routerInst.UsePrerouting(middleware.ContextMiddleware) routerInst.UsePrerouting(middleware.CORSMiddleware()) + // Set up logging. This must be done after ContextMiddleware is initialized so the context can be accessed in the log logic + if cfg.EnableAPILogging { + routerInst.UsePrerouting(middleware.LoggingMiddleware(cfg, identityResolver)) + } + routerInst.UsePostrouting( middleware.PanicHandler, middleware.AuthMiddleware(authenticator),