update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
88
log/redactrus.go
Executable file
88
log/redactrus.go
Executable file
@@ -0,0 +1,88 @@
|
||||
package log
|
||||
|
||||
// Copied from https://github.com/whuang8/redactrus (MIT License)
|
||||
// Copyright (c) 2018 William Huang
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Hook is a logrus hook for redacting information from logs
|
||||
type Hook struct {
|
||||
// Messages with a log level not contained in this array
|
||||
// will not be dispatched. If empty, all messages will be dispatched.
|
||||
AcceptedLevels []logrus.Level
|
||||
RedactionList []string
|
||||
redactionKeys []*regexp.Regexp
|
||||
}
|
||||
|
||||
// Levels returns the user defined AcceptedLevels
|
||||
// If AcceptedLevels is empty, all logrus levels are returned
|
||||
func (h *Hook) Levels() []logrus.Level {
|
||||
if len(h.AcceptedLevels) == 0 {
|
||||
return logrus.AllLevels
|
||||
}
|
||||
return h.AcceptedLevels
|
||||
}
|
||||
|
||||
// Fire redacts values in a log Entry that match
|
||||
// with keys defined in the RedactionList
|
||||
func (h *Hook) Fire(e *logrus.Entry) error {
|
||||
if err := h.initRedaction(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, re := range h.redactionKeys {
|
||||
// Redact based on key matching in Data fields
|
||||
for k, v := range e.Data {
|
||||
if re.MatchString(k) {
|
||||
e.Data[k] = "[REDACTED]"
|
||||
continue
|
||||
}
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
switch reflect.TypeOf(v).Kind() {
|
||||
case reflect.String:
|
||||
e.Data[k] = re.ReplaceAllString(v.(string), "$1[REDACTED]$2")
|
||||
continue
|
||||
case reflect.Map:
|
||||
s := fmt.Sprintf("%+v", v)
|
||||
e.Data[k] = re.ReplaceAllString(s, "$1[REDACTED]$2")
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Redact based on text matching in the Message field
|
||||
e.Message = re.ReplaceAllString(e.Message, "$1[REDACTED]$2")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Hook) initRedaction() error {
|
||||
if len(h.redactionKeys) == 0 {
|
||||
for _, redactionKey := range h.RedactionList {
|
||||
re, err := regexp.Compile(redactionKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.redactionKeys = append(h.redactionKeys, re)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Hook) redact(msg string) (string, error) {
|
||||
if err := h.initRedaction(); err != nil {
|
||||
return msg, err
|
||||
}
|
||||
for _, re := range h.redactionKeys {
|
||||
msg = re.ReplaceAllString(msg, "$1[REDACTED]$2")
|
||||
}
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user