-
Notifications
You must be signed in to change notification settings - Fork 5
/
redactor.go
189 lines (156 loc) · 5.06 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Copyright 2021 The Nuclio Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nucliozap
import (
"bytes"
"fmt"
"io"
"regexp"
)
type RedactingLogger interface {
// GetOutput returns redactor writer
GetOutput() io.Writer
// GetRedactor returns redactor instance
GetRedactor() *Redactor
}
type Redactor struct {
disabled bool
output io.Writer
redactions [][]byte
valueRedactions [][]byte
valueRedactionsRegexps []regexp.Regexp
replacement []byte
valueReplacement []byte
// must have same signature as io.Writer Write
redactFunc func(p []byte) (n int, err error)
}
func NewRedactor(output io.Writer) *Redactor {
redactor := &Redactor{
output: output,
redactions: [][]byte{},
valueRedactions: [][]byte{},
valueRedactionsRegexps: []regexp.Regexp{},
replacement: []byte("*****"),
valueReplacement: []byte(`$1"[redacted]"`),
disabled: false,
}
redactor.redactFunc = redactor.redactEnabled
return redactor
}
// SetOutput sets redactor output
func (r *Redactor) SetOutput(output io.Writer) {
r.output = output
}
// GetOutput returns redactor writer
func (r *Redactor) GetOutput() io.Writer {
return r.output
}
// AddValueRedactions redacts key:[value] or key=[value] kind of strings
func (r *Redactor) AddValueRedactions(valueRedactions []string) {
for _, valueRedaction := range valueRedactions {
r.valueRedactions = append(r.valueRedactions, []byte(valueRedaction))
}
r.valueRedactions = r.removeDuplicates(r.valueRedactions)
r.prepareReplacements()
}
// AddRedactions redacts simple strings
func (r *Redactor) AddRedactions(redactions []string) {
var nonEmptyRedactions []string
for _, redaction := range redactions {
if redaction != "" {
nonEmptyRedactions = append(nonEmptyRedactions, redaction)
}
}
for _, nonEmptyRedaction := range nonEmptyRedactions {
r.redactions = append(r.redactions, []byte(nonEmptyRedaction))
}
r.redactions = r.removeDuplicates(r.redactions)
}
// SetDisabled turns logger redaction on/off
func (r *Redactor) SetDisabled(disable bool) {
r.disabled = disable
if disable {
r.redactFunc = r.redactDisabled
} else {
r.redactFunc = r.redactEnabled
}
}
// Write writes to output
func (r *Redactor) Write(p []byte) (n int, err error) {
return r.redactFunc(p)
}
func (r *Redactor) GetRedactions() [][]byte {
return r.redactions
}
func (r *Redactor) Enable() {
r.disabled = false
}
func (r *Redactor) Disable() {
r.disabled = true
}
func (r *Redactor) prepareReplacements() {
// redact values of either strings of type `valueRedaction=[value]` or `valueRedaction: [value]`
// w/wo single/double quotes
// golang regex doesn't support lookarounds, so we will check things manually
matchKeyWithSeparatorTemplate := `\\*[\'"]?(?i)%s\\*[\'"]?\s*[=:]\s*`
matchValue := `\'[^\']*?\'|\"[^\"]*\"|\[[^\]]*?\]|\{[^\}]*?\}|\S*`
// reset to avoid duplicates
r.valueRedactionsRegexps = make([]regexp.Regexp, len(r.valueRedactions))
for idx, redactionField := range r.valueRedactions {
matchKeyWithSeparator := fmt.Sprintf(matchKeyWithSeparatorTemplate, redactionField)
r.valueRedactionsRegexps[idx] = *regexp.MustCompile(
fmt.Sprintf(`(%s)(%s)`, matchKeyWithSeparator, matchValue),
)
}
}
func (r *Redactor) redactDisabled(p []byte) (n int, err error) {
return r.output.Write(p)
}
func (r *Redactor) redactEnabled(p []byte) (n int, err error) {
redactedPrint := r.redact(p)
n, err = r.output.Write(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) redact(inputToRedact []byte) []byte {
// replace key=value or key: value
for _, valueRedactionsRegexp := range r.valueRedactionsRegexps {
inputToRedact = valueRedactionsRegexp.ReplaceAll(inputToRedact, r.valueReplacement)
}
// replace the simple string redactions
for _, redactionField := range r.redactions {
inputToRedact = bytes.ReplaceAll(inputToRedact, redactionField, r.replacement)
}
return inputToRedact
}
func (r *Redactor) removeDuplicates(elements [][]byte) [][]byte {
encountered := map[string]bool{}
// Create a map of all unique elements.
for v := range elements {
encountered[string(elements[v])] = true
}
// Place all keys from the map into a slice.
var result [][]byte
for key := range encountered {
result = append(result, []byte(key))
}
return result
}