Skip to content

Commit

Permalink
🔒 Redact password from logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Sep 18, 2022
1 parent b9563e1 commit 0f2051e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions internal/log_hooks/redact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package log_hooks

import (
"fmt"
log "github.com/sirupsen/logrus"
"strings"
)

// Redact will redact a secret from log output
type Redact string

func (r Redact) Levels() []log.Level {
return log.AllLevels
}

func (r Redact) Fire(entry *log.Entry) error {
entry.Message = strings.ReplaceAll(entry.Message, string(r), "***")

for i, field := range entry.Data {
switch field := field.(type) {
case string:
entry.Data[i] = strings.ReplaceAll(field, string(r), "***")
default:
if field, ok := field.(fmt.Stringer); ok {
entry.Data[i] = strings.ReplaceAll(field.String(), string(r), "***")
}
}
}
return nil
}
2 changes: 2 additions & 0 deletions internal/util/cmd_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/clevyr/kubedb/internal/config"
"github.com/clevyr/kubedb/internal/database"
"github.com/clevyr/kubedb/internal/kubernetes"
"github.com/clevyr/kubedb/internal/log_hooks"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -139,6 +140,7 @@ func DefaultSetup(cmd *cobra.Command, conf *config.Global) (err error) {
return err
}
}
log.AddHook(log_hooks.Redact(conf.Password))

return nil
}

0 comments on commit 0f2051e

Please sign in to comment.