Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Mask token in log
  • Loading branch information
jmattheis committed Sep 27, 2021
1 parent d2e5dee commit 8affece
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion router/router.go
@@ -1,6 +1,8 @@
package router

import (
"fmt"
"regexp"
"time"

"github.com/gin-contrib/cors"
Expand All @@ -22,7 +24,7 @@ import (
func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Configuration) (*gin.Engine, func()) {
g := gin.New()

g.Use(gin.Logger(), gin.Recovery(), error.Handler(), location.Default())
g.Use(gin.LoggerWithFormatter(logFormatter), gin.Recovery(), error.Handler(), location.Default())
g.NoRoute(error.NotFound())

streamHandler := stream.New(time.Duration(conf.Server.Stream.PingPeriodSeconds)*time.Second, 15*time.Second, conf.Server.Stream.AllowedOrigins)
Expand Down Expand Up @@ -167,3 +169,28 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co
}
return g, streamHandler.Close
}

var tokenRegexp = regexp.MustCompile("token=[^&]+")

func logFormatter(param gin.LogFormatterParams) string {
var statusColor, methodColor, resetColor string
if param.IsOutputColor() {
statusColor = param.StatusCodeColor()
methodColor = param.MethodColor()
resetColor = param.ResetColor()
}

if param.Latency > time.Minute {
param.Latency = param.Latency - param.Latency%time.Second
}
path := tokenRegexp.ReplaceAllString(param.Path, "token=[masked]")
return fmt.Sprintf("[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
statusColor, param.StatusCode, resetColor,
param.Latency,
param.ClientIP,
methodColor, param.Method, resetColor,
path,
param.ErrorMessage,
)
}

0 comments on commit 8affece

Please sign in to comment.