forked from nuclio/loggerus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redactor.go
126 lines (103 loc) · 3.1 KB
/
redactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package loggerus
import (
"fmt"
"io"
"regexp"
"strings"
)
type RedactingLogger interface {
GetRedactor() *Redactor
GetOutput() io.Writer
}
type Redactor struct {
disabled bool
output io.Writer
redactions []string
valueRedactions []string
replacementString string
valueReplacementString string
}
func NewRedactor(output io.Writer) *Redactor {
return &Redactor{
output: output,
redactions: []string{},
valueRedactions: []string{},
replacementString: "*****",
valueReplacementString: "[redacted]",
disabled: false,
}
}
func (r *Redactor) GetOutput() io.Writer {
return r.output
}
func (r *Redactor) AddValueRedactions(valueRedactions []string) {
r.valueRedactions = append(r.valueRedactions, valueRedactions...)
r.valueRedactions = r.removeDuplicates(r.valueRedactions)
}
func (r *Redactor) GetRedactions() []string {
return r.redactions
}
func (r *Redactor) AddRedactions(redactions []string) {
var nonEmptyRedactions []string
for _, redaction := range redactions {
if redaction != "" {
nonEmptyRedactions = append(nonEmptyRedactions, redaction)
}
}
r.redactions = append(r.redactions, nonEmptyRedactions...)
r.redactions = r.removeDuplicates(r.redactions)
}
func (r *Redactor) Write(p []byte) (n int, err error) {
redactedPrint := r.redact(string(p[:]))
n, err = r.output.Write([]byte(redactedPrint))
if err != nil {
return
}
if n != len(redactedPrint) {
err = io.ErrShortWrite
return
}
// HACK: let the caller know we wrote the original length of the text
// To prevent caller explode while validating the length of the written text (redaction might change the length)
return len(p), err
}
func (r *Redactor) Enable() {
r.disabled = false
}
func (r *Redactor) Disable() {
r.disabled = true
}
func (r *Redactor) redact(input string) string {
if r.disabled {
return input
}
redacted := input
// golang regex doesn't support lookarounds, so we will check things manually
matchKeyWithSeparatorTemplate := `\\*[\'"]?(?i)%s\\*[\'"]?\s*[=:]\s*`
matchValue := `\'[^\']*?\'|\"[^\"]*\"|\S*`
// redact values of either strings of type `valueRedaction=[value]` or `valueRedaction: [value]`
// w/wo single/double quotes
for _, redactionField := range r.valueRedactions {
matchKeyWithSeparator := fmt.Sprintf(matchKeyWithSeparatorTemplate, redactionField)
re := regexp.MustCompile(fmt.Sprintf(`(%s)(%s)`, matchKeyWithSeparator, matchValue))
redacted = re.ReplaceAllString(redacted, fmt.Sprintf(`$1%s`, r.valueReplacementString))
}
// replace the simple string redactions
for _, redactionField := range r.redactions {
redacted = strings.Replace(redacted, redactionField, r.replacementString, -1)
}
return redacted
}
func (r *Redactor) removeDuplicates(elements []string) []string {
encountered := map[string]bool{}
// Create a map of all unique elements.
for v := range elements {
encountered[elements[v]] = true
}
// Place all keys from the map into a slice.
var result []string
for key := range encountered {
result = append(result, key)
}
return result
}