Skip to content

Commit

Permalink
RHINENG-11685: add temp debug logs for middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Dugowitch committed Aug 21, 2024
1 parent 4e865ec commit a9f399e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions manager/middlewares/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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"})
}
Expand All @@ -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()
Expand All @@ -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()
}
Expand Down
22 changes: 13 additions & 9 deletions manager/middlewares/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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")
}
}
2 changes: 2 additions & 0 deletions manager/middlewares/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}),
Expand Down

0 comments on commit a9f399e

Please sign in to comment.