Skip to content

Commit 252b29f

Browse files
authored
Merge pull request #201 from replicatedhq/laverya/better-redactor-names
default redactor name improvements
2 parents bde8820 + 553718e commit 252b29f

File tree

1 file changed

+100
-40
lines changed

1 file changed

+100
-40
lines changed

pkg/redact/redact.go

Lines changed: 100 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,28 @@ func buildAdditionalRedactors(path string, redacts []*troubleshootv1beta1.Redact
103103
continue
104104
}
105105

106-
withinRedactNum := 0 // give unique redaction names
107-
108-
for _, re := range redact.Regex {
109-
r, err := NewSingleLineRedactor(re, MASK_TEXT, path, redactorName(i, withinRedactNum, redact.Name, "regex", ""))
106+
for j, re := range redact.Regex {
107+
r, err := NewSingleLineRedactor(re, MASK_TEXT, path, redactorName(i, j, redact.Name, "regex"))
110108
if err != nil {
111109
return nil, errors.Wrapf(err, "redactor %q", re)
112110
}
113111
additionalRedactors = append(additionalRedactors, r)
114112
}
115113

116-
for _, literal := range redact.Values {
117-
additionalRedactors = append(additionalRedactors, literalString(literal, path, redactorName(i, withinRedactNum, redact.Name, "literal", "")))
114+
for j, literal := range redact.Values {
115+
additionalRedactors = append(additionalRedactors, literalString(literal, path, redactorName(i, j, redact.Name, "literal")))
118116
}
119117

120-
for _, re := range redact.MultiLine {
121-
r, err := NewMultiLineRedactor(re.Selector, re.Redactor, MASK_TEXT, path, redactorName(i, withinRedactNum, redact.Name, "multiLine", ""))
118+
for j, re := range redact.MultiLine {
119+
r, err := NewMultiLineRedactor(re.Selector, re.Redactor, MASK_TEXT, path, redactorName(i, j, redact.Name, "multiLine"))
122120
if err != nil {
123121
return nil, errors.Wrapf(err, "multiline redactor %+v", re)
124122
}
125123
additionalRedactors = append(additionalRedactors, r)
126124
}
127125

128-
for _, yaml := range redact.Yaml {
129-
r := NewYamlRedactor(yaml, path, redactorName(i, withinRedactNum, redact.Name, "yaml", ""))
126+
for j, yaml := range redact.Yaml {
127+
r := NewYamlRedactor(yaml, path, redactorName(i, j, redact.Name, "yaml"))
130128
additionalRedactors = append(additionalRedactors, r)
131129
}
132130
}
@@ -171,39 +169,96 @@ func getRedactors(path string) ([]Redactor, error) {
171169
// (?i) makes it case insensitive
172170
// groups named with `?P<mask>` will be masked
173171
// groups named with `?P<drop>` will be removed (replaced with empty strings)
174-
singleLines := []string{
172+
singleLines := []struct {
173+
regex string
174+
name string
175+
}{
175176
// ipv4
176-
`(?P<mask>\b(?P<drop>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?P<drop>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?P<drop>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?P<drop>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)`,
177+
{
178+
regex: `(?P<mask>\b(?P<drop>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?P<drop>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?P<drop>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?P<drop>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)`,
179+
name: "default ipv4 redactor",
180+
},
177181
// TODO: ipv6
178182
// aws secrets
179-
`(?i)(\\\"name\\\":\\\"[^\"]*SECRET_?ACCESS_?KEY\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
180-
`(?i)(\\\"name\\\":\\\"[^\"]*ACCESS_?KEY_?ID\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
181-
`(?i)(\\\"name\\\":\\\"[^\"]*OWNER_?ACCOUNT\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
183+
{
184+
regex: `(?i)(\\\"name\\\":\\\"[^\"]*SECRET_?ACCESS_?KEY\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
185+
name: "default SECRET_ACCESS_KEY redactor",
186+
},
187+
{
188+
regex: `(?i)(\\\"name\\\":\\\"[^\"]*ACCESS_?KEY_?ID\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
189+
name: "default ACCESS_KEY_ID redactor",
190+
},
191+
{
192+
regex: `(?i)(\\\"name\\\":\\\"[^\"]*OWNER_?ACCOUNT\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
193+
name: "default OWNER_ACCOUNT redactor",
194+
},
182195
// passwords in general
183-
`(?i)(\\\"name\\\":\\\"[^\"]*password[^\"]*\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
196+
{
197+
regex: `(?i)(\\\"name\\\":\\\"[^\"]*password[^\"]*\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
198+
name: "default password redactor",
199+
},
184200
// tokens in general
185-
`(?i)(\\\"name\\\":\\\"[^\"]*token[^\"]*\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
186-
`(?i)(\\\"name\\\":\\\"[^\"]*database[^\"]*\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
187-
`(?i)(\\\"name\\\":\\\"[^\"]*user[^\"]*\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
201+
{
202+
regex: `(?i)(\\\"name\\\":\\\"[^\"]*token[^\"]*\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
203+
name: "default token redactor",
204+
},
205+
{
206+
regex: `(?i)(\\\"name\\\":\\\"[^\"]*database[^\"]*\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
207+
name: "default database redactor",
208+
},
209+
{
210+
regex: `(?i)(\\\"name\\\":\\\"[^\"]*user[^\"]*\\\",\\\"value\\\":\\\")(?P<mask>[^\"]*)(\\\")`,
211+
name: "default user redactor",
212+
},
188213
// connection strings with username and password
189214
// http://user:password@host:8888
190-
`(?i)(https?|ftp)(:\/\/)(?P<mask>[^:\"\/]+){1}(:)(?P<mask>[^@\"\/]+){1}(?P<host>@[^:\/\s\"]+){1}(?P<port>:[\d]+)?`,
215+
{
216+
regex: `(?i)(https?|ftp)(:\/\/)(?P<mask>[^:\"\/]+){1}(:)(?P<mask>[^@\"\/]+){1}(?P<host>@[^:\/\s\"]+){1}(?P<port>:[\d]+)?`,
217+
name: "default connection string redactor",
218+
},
191219
// user:password@tcp(host:3309)/db-name
192-
`\b(?P<mask>[^:\"\/]*){1}(:)(?P<mask>[^:\"\/]*){1}(@tcp\()(?P<mask>[^:\"\/]*){1}(?P<port>:[\d]*)?(\)\/)(?P<mask>[\w\d\S-_]+){1}\b`,
193-
// standard postgres and mysql connnection strings
194-
`(?i)(Data Source *= *)(?P<mask>[^\;]+)(;)`,
195-
`(?i)(location *= *)(?P<mask>[^\;]+)(;)`,
196-
`(?i)(User ID *= *)(?P<mask>[^\;]+)(;)`,
197-
`(?i)(password *= *)(?P<mask>[^\;]+)(;)`,
198-
`(?i)(Server *= *)(?P<mask>[^\;]+)(;)`,
199-
`(?i)(Database *= *)(?P<mask>[^\;]+)(;)`,
200-
`(?i)(Uid *= *)(?P<mask>[^\;]+)(;)`,
201-
`(?i)(Pwd *= *)(?P<mask>[^\;]+)(;)`,
220+
{
221+
regex: `\b(?P<mask>[^:\"\/]*){1}(:)(?P<mask>[^:\"\/]*){1}(@tcp\()(?P<mask>[^:\"\/]*){1}(?P<port>:[\d]*)?(\)\/)(?P<mask>[\w\d\S-_]+){1}\b`,
222+
name: "default db connection string redactor",
223+
},
224+
// standard postgres and mysql connection strings
225+
{
226+
regex: `(?i)(Data Source *= *)(?P<mask>[^\;]+)(;)`,
227+
name: "default Data Source redactor",
228+
},
229+
{
230+
regex: `(?i)(location *= *)(?P<mask>[^\;]+)(;)`,
231+
name: "default location redactor",
232+
},
233+
{
234+
regex: `(?i)(User ID *= *)(?P<mask>[^\;]+)(;)`,
235+
name: "default User ID redactor",
236+
},
237+
{
238+
regex: `(?i)(password *= *)(?P<mask>[^\;]+)(;)`,
239+
name: "default db-password redactor",
240+
},
241+
{
242+
regex: `(?i)(Server *= *)(?P<mask>[^\;]+)(;)`,
243+
name: "default server redactor",
244+
},
245+
{
246+
regex: `(?i)(Database *= *)(?P<mask>[^\;]+)(;)`,
247+
name: "default db-database redactor",
248+
},
249+
{
250+
regex: `(?i)(Uid *= *)(?P<mask>[^\;]+)(;)`,
251+
name: "default Uid redactor",
252+
},
253+
{
254+
regex: `(?i)(Pwd *= *)(?P<mask>[^\;]+)(;)`,
255+
name: "default Pwd redactor",
256+
},
202257
}
203258

204259
redactors := make([]Redactor, 0)
205-
for i, re := range singleLines {
206-
r, err := NewSingleLineRedactor(re, MASK_TEXT, path, redactorName(-1, i, "", "defaultRegex", re))
260+
for _, re := range singleLines {
261+
r, err := NewSingleLineRedactor(re.regex, MASK_TEXT, path, re.name)
207262
if err != nil {
208263
return nil, err // maybe skip broken ones?
209264
}
@@ -213,39 +268,47 @@ func getRedactors(path string) ([]Redactor, error) {
213268
doubleLines := []struct {
214269
line1 string
215270
line2 string
271+
name string
216272
}{
217273
{
218274
line1: `(?i)"name": *"[^\"]*SECRET_?ACCESS_?KEY[^\"]*"`,
219275
line2: `(?i)("value": *")(?P<mask>.*[^\"]*)(")`,
276+
name: "default multiline SECRET_ACCESS_KEY redactor",
220277
},
221278
{
222279
line1: `(?i)"name": *"[^\"]*ACCESS_?KEY_?ID[^\"]*"`,
223280
line2: `(?i)("value": *")(?P<mask>.*[^\"]*)(")`,
281+
name: "default multiline ACCESS_KEY_ID redactor",
224282
},
225283
{
226284
line1: `(?i)"name": *"[^\"]*OWNER_?ACCOUNT[^\"]*"`,
227285
line2: `(?i)("value": *")(?P<mask>.*[^\"]*)(")`,
286+
name: "default multiline OWNER_ACCOUNT redactor",
228287
},
229288
{
230289
line1: `(?i)"name": *".*password[^\"]*"`,
231290
line2: `(?i)("value": *")(?P<mask>.*[^\"]*)(")`,
291+
name: "default multiline password redactor",
232292
},
233293
{
234294
line1: `(?i)"name": *".*token[^\"]*"`,
235295
line2: `(?i)("value": *")(?P<mask>.*[^\"]*)(")`,
296+
name: "default multiline token redactor",
236297
},
237298
{
238299
line1: `(?i)"name": *".*database[^\"]*"`,
239300
line2: `(?i)("value": *")(?P<mask>.*[^\"]*)(")`,
301+
name: "default multiline database redactor",
240302
},
241303
{
242304
line1: `(?i)"name": *".*user[^\"]*"`,
243305
line2: `(?i)("value": *")(?P<mask>.*[^\"]*)(")`,
306+
name: "default multiline user redactor",
244307
},
245308
}
246309

247-
for i, l := range doubleLines {
248-
r, err := NewMultiLineRedactor(l.line1, l.line2, MASK_TEXT, path, redactorName(-1, i, "", "defaultMultiLine", l.line1))
310+
for _, l := range doubleLines {
311+
r, err := NewMultiLineRedactor(l.line1, l.line2, MASK_TEXT, path, l.name)
249312
if err != nil {
250313
return nil, err // maybe skip broken ones?
251314
}
@@ -302,12 +365,9 @@ func addRedaction(redaction Redaction) {
302365
}(redaction)
303366
}
304367

305-
func redactorName(redactorNum, withinRedactorNum int, redactorName, redactorType, redactorLiteral string) string {
368+
func redactorName(redactorNum, withinRedactorNum int, redactorName, redactorType string) string {
306369
if redactorName != "" {
307-
return fmt.Sprintf("%s-%d", redactorName, withinRedactorNum)
308-
}
309-
if redactorLiteral == "" {
310-
return fmt.Sprintf("unnamed-%d.%d-%s", redactorNum, withinRedactorNum, redactorType)
370+
return fmt.Sprintf("%s.%s.%d", redactorName, redactorType, withinRedactorNum)
311371
}
312-
return fmt.Sprintf("%s.%d-%q", redactorType, withinRedactorNum, redactorLiteral)
372+
return fmt.Sprintf("unnamed-%d.%s.%d", redactorNum, redactorType, withinRedactorNum)
313373
}

0 commit comments

Comments
 (0)