From a9f399e81c331733c95cd5fbda8f6b2b11d9d3b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Dugovi=C4=8D?= Date: Tue, 20 Aug 2024 17:43:20 +0200 Subject: [PATCH] RHINENG-11685: add temp debug logs for middleware --- manager/middlewares/limits.go | 4 ++++ manager/middlewares/rbac.go | 22 +++++++++++++--------- manager/middlewares/timeout.go | 2 ++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/manager/middlewares/limits.go b/manager/middlewares/limits.go index 0df52a124..db162e93f 100644 --- a/manager/middlewares/limits.go +++ b/manager/middlewares/limits.go @@ -10,6 +10,7 @@ import ( func LimitRequestBodySize(size int64) gin.HandlerFunc { return func(c *gin.Context) { + tempLogDebugGinContextRequestHeader(c, "LimitRequestBodySize") if c.Request != nil && c.Request.Body != nil { c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, size) } @@ -19,6 +20,7 @@ func LimitRequestBodySize(size int64) gin.HandlerFunc { func LimitRequestHeaders(maxHeaderCount int) gin.HandlerFunc { return func(c *gin.Context) { + tempLogDebugGinContextRequestHeader(c, "LimitRequestHeaders") if len(c.Request.Header) > maxHeaderCount { c.AbortWithStatusJSON(http.StatusRequestEntityTooLarge, utils.ErrorResponse{Error: "too many headers"}) } @@ -28,6 +30,7 @@ func LimitRequestHeaders(maxHeaderCount int) gin.HandlerFunc { func MaxConnections(max int) gin.HandlerFunc { conns := make(chan struct{}, max) return func(c *gin.Context) { + tempLogDebugGinContextRequestHeader(c, "MaxConnections") conns <- struct{}{} defer func() { <-conns }() c.Next() @@ -37,6 +40,7 @@ func MaxConnections(max int) gin.HandlerFunc { func Ratelimit(max int) gin.HandlerFunc { rl := ratelimit.New(max) return func(c *gin.Context) { + tempLogDebugGinContextRequestHeader(c, "Ratelimit") rl.Take() c.Next() } diff --git a/manager/middlewares/rbac.go b/manager/middlewares/rbac.go index 1fd723814..f09fc8cba 100644 --- a/manager/middlewares/rbac.go +++ b/manager/middlewares/rbac.go @@ -104,12 +104,6 @@ func isAccessGranted(c *gin.Context) bool { client := makeClient(c.GetHeader("x-rh-identity")) access := rbac.AccessPagination{} res, err := client.Request(&base.Context, http.MethodGet, rbacURL, nil, &access) - if res != nil { - utils.LogDebug("response_headers", res.Header, "request_headers", res.Request.Header, "isAccessGranted rbac") - } - if c.Request != nil { - utils.LogDebug("gin_context_req_header", c.Request.Header, "isAccessGranted rbac") - } if res != nil && res.Body != nil { defer res.Body.Close() } @@ -198,13 +192,23 @@ func RBAC() gin.HandlerFunc { } return func(c *gin.Context) { + tempLogDebugGinContextRequestHeader(c, "RBAC") if isAccessGranted(c) { return } - if c.Request != nil { - utils.LogDebug("context_req_header", c.Request.Header, "RBAC") - } c.AbortWithStatusJSON(http.StatusUnauthorized, utils.ErrorResponse{Error: "You don't have access to this application"}) } } + +func tempLogDebugGinContextRequestHeader(c *gin.Context, origin string) { + if c != nil { + if c.Request != nil { + utils.LogDebug("origin", origin, "gin_context_req_header", c.Request.Header, "gin context request handler") + } else { + utils.LogDebug("origin", origin, "gin_context_req", nil, "gin context request handler") + } + } else { + utils.LogDebug("origin", origin, "gin_context", nil, "gin context request handler") + } +} diff --git a/manager/middlewares/timeout.go b/manager/middlewares/timeout.go index 2bcbc90ae..3e06a9b9e 100644 --- a/manager/middlewares/timeout.go +++ b/manager/middlewares/timeout.go @@ -13,9 +13,11 @@ func WithTimeout(seconds time.Duration) gin.HandlerFunc { return timeout.New( timeout.WithTimeout(seconds*time.Second), timeout.WithHandler(func(c *gin.Context) { + tempLogDebugGinContextRequestHeader(c, "TimeoutWithHandler") c.Next() }), timeout.WithResponse(func(c *gin.Context) { + tempLogDebugGinContextRequestHeader(c, "TimeoutWithResponse") c.AbortWithStatusJSON(http.StatusRequestTimeout, utils.ErrorResponse{Error: "Request timeout"}) c.Done() }),