Skip to content

Commit d37ace6

Browse files
committed
better naming scheme that does not leak secrets
1 parent b6e92e1 commit d37ace6

File tree

7 files changed

+46
-30
lines changed

7 files changed

+46
-30
lines changed

pkg/redact/literal.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import (
1010
type literalRedactor struct {
1111
matchString string
1212
filePath string
13+
redactName string
1314
}
1415

15-
func literalString(matchString, path string) Redactor {
16+
func literalString(matchString, path, name string) Redactor {
1617
return literalRedactor{
1718
matchString: matchString,
1819
filePath: path,
20+
redactName: name,
1921
}
2022
}
2123

@@ -52,7 +54,7 @@ func (r literalRedactor) Redact(input io.Reader) io.Reader {
5254

5355
if clean != line {
5456
go addRedaction(Redaction{
55-
RedactorName: fmt.Sprintf("literal %q", r.matchString),
57+
RedactorName: r.redactName,
5658
CharactersRemoved: len(line) - len(clean),
5759
Line: lineNum,
5860
File: r.filePath,

pkg/redact/multi_line.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
)
99

1010
type MultiLineRedactor struct {
11-
re1 *regexp.Regexp
12-
re2 *regexp.Regexp
13-
maskText string
14-
filePath string
11+
re1 *regexp.Regexp
12+
re2 *regexp.Regexp
13+
maskText string
14+
filePath string
15+
redactName string
1516
}
1617

17-
func NewMultiLineRedactor(re1, re2, maskText, path string) (*MultiLineRedactor, error) {
18+
func NewMultiLineRedactor(re1, re2, maskText, path, name string) (*MultiLineRedactor, error) {
1819
compiled1, err := regexp.Compile(re1)
1920
if err != nil {
2021
return nil, err
@@ -23,7 +24,7 @@ func NewMultiLineRedactor(re1, re2, maskText, path string) (*MultiLineRedactor,
2324
if err != nil {
2425
return nil, err
2526
}
26-
return &MultiLineRedactor{re1: compiled1, re2: compiled2, maskText: maskText, filePath: path}, nil
27+
return &MultiLineRedactor{re1: compiled1, re2: compiled2, maskText: maskText, filePath: path, redactName: name}, nil
2728
}
2829

2930
func (r *MultiLineRedactor) Redact(input io.Reader) io.Reader {
@@ -70,7 +71,7 @@ func (r *MultiLineRedactor) Redact(input io.Reader) io.Reader {
7071
// if clean is not equal to line2, a redaction was performed
7172
if clean != line2 {
7273
go addRedaction(Redaction{
73-
RedactorName: fmt.Sprintf("multiline %q/%q", r.re1, r.re2),
74+
RedactorName: r.redactName,
7475
CharactersRemoved: len(line2) - len(clean),
7576
Line: lineNum,
7677
File: r.filePath,

pkg/redact/redact.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func Redact(input []byte, path string, additionalRedactors []*troubleshootv1beta
7979

8080
func buildAdditionalRedactors(path string, redacts []*troubleshootv1beta1.Redact) ([]Redactor, error) {
8181
additionalRedactors := []Redactor{}
82-
for _, redact := range redacts {
82+
for i, redact := range redacts {
8383
if redact == nil {
8484
continue
8585
}
@@ -93,28 +93,30 @@ func buildAdditionalRedactors(path string, redacts []*troubleshootv1beta1.Redact
9393
continue
9494
}
9595

96+
withinRedactNum := 0 // give unique redaction names
97+
9698
for _, re := range redact.Regex {
97-
r, err := NewSingleLineRedactor(re, MASK_TEXT, path)
99+
r, err := NewSingleLineRedactor(re, MASK_TEXT, path, redactorName(i, withinRedactNum, redact.Name, "regex", ""))
98100
if err != nil {
99101
return nil, errors.Wrapf(err, "redactor %q", re)
100102
}
101103
additionalRedactors = append(additionalRedactors, r)
102104
}
103105

104106
for _, literal := range redact.Values {
105-
additionalRedactors = append(additionalRedactors, literalString(literal, path))
107+
additionalRedactors = append(additionalRedactors, literalString(literal, path, redactorName(i, withinRedactNum, redact.Name, "literal", "")))
106108
}
107109

108110
for _, re := range redact.MultiLine {
109-
r, err := NewMultiLineRedactor(re.Selector, re.Redactor, MASK_TEXT, path)
111+
r, err := NewMultiLineRedactor(re.Selector, re.Redactor, MASK_TEXT, path, redactorName(i, withinRedactNum, redact.Name, "multiLine", ""))
110112
if err != nil {
111113
return nil, errors.Wrapf(err, "multiline redactor %+v", re)
112114
}
113115
additionalRedactors = append(additionalRedactors, r)
114116
}
115117

116118
for _, yaml := range redact.Yaml {
117-
r := NewYamlRedactor(yaml, path)
119+
r := NewYamlRedactor(yaml, path, redactorName(i, withinRedactNum, redact.Name, "yaml", ""))
118120
additionalRedactors = append(additionalRedactors, r)
119121
}
120122
}
@@ -190,8 +192,8 @@ func getRedactors(path string) ([]Redactor, error) {
190192
}
191193

192194
redactors := make([]Redactor, 0)
193-
for _, re := range singleLines {
194-
r, err := NewSingleLineRedactor(re, MASK_TEXT, path)
195+
for i, re := range singleLines {
196+
r, err := NewSingleLineRedactor(re, MASK_TEXT, path, redactorName(-1, i, "", "defaultRegex", re))
195197
if err != nil {
196198
return nil, err // maybe skip broken ones?
197199
}
@@ -232,8 +234,8 @@ func getRedactors(path string) ([]Redactor, error) {
232234
},
233235
}
234236

235-
for _, l := range doubleLines {
236-
r, err := NewMultiLineRedactor(l.line1, l.line2, MASK_TEXT, path)
237+
for i, l := range doubleLines {
238+
r, err := NewMultiLineRedactor(l.line1, l.line2, MASK_TEXT, path, redactorName(-1, i, "", "defaultMultiLine", l.line1))
237239
if err != nil {
238240
return nil, err // maybe skip broken ones?
239241
}
@@ -278,3 +280,13 @@ func readLine(r *bufio.Reader) (string, error) {
278280
}
279281
return string(completeLine), nil
280282
}
283+
284+
func redactorName(redactorNum, withinRedactorNum int, redactorName, redactorType, redactorLiteral string) string {
285+
if redactorName != "" {
286+
return fmt.Sprintf("%s-%d", redactorName, withinRedactorNum)
287+
}
288+
if redactorLiteral == "" {
289+
return fmt.Sprintf("unnamed-%d.%d-%s", redactorNum, withinRedactorNum, redactorType)
290+
}
291+
return fmt.Sprintf("%s.%d-%q", redactorType, withinRedactorNum, redactorLiteral)
292+
}

pkg/redact/single_line.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import (
88
)
99

1010
type SingleLineRedactor struct {
11-
re *regexp.Regexp
12-
maskText string
13-
filePath string
11+
re *regexp.Regexp
12+
maskText string
13+
filePath string
14+
redactName string
1415
}
1516

16-
func NewSingleLineRedactor(re, maskText, path string) (*SingleLineRedactor, error) {
17+
func NewSingleLineRedactor(re, maskText, path, name string) (*SingleLineRedactor, error) {
1718
compiled, err := regexp.Compile(re)
1819
if err != nil {
1920
return nil, err
2021
}
21-
return &SingleLineRedactor{re: compiled, maskText: maskText, filePath: path}, nil
22+
return &SingleLineRedactor{re: compiled, maskText: maskText, filePath: path, redactName: name}, nil
2223
}
2324

2425
func (r *SingleLineRedactor) Redact(input io.Reader) io.Reader {
@@ -62,7 +63,7 @@ func (r *SingleLineRedactor) Redact(input io.Reader) io.Reader {
6263
// if clean is not equal to line, a redaction was performed
6364
if clean != line {
6465
go addRedaction(Redaction{
65-
RedactorName: fmt.Sprintf("regex %q", r.re),
66+
RedactorName: r.redactName,
6667
CharactersRemoved: len(line) - len(clean),
6768
Line: lineNum,
6869
File: r.filePath,

pkg/redact/single_line_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestNewSingleLineRedactor(t *testing.T) {
4141
defer scopetest.End()
4242

4343
req := require.New(t)
44-
reRunner, err := NewSingleLineRedactor(tt.re, MASK_TEXT, "testfile")
44+
reRunner, err := NewSingleLineRedactor(tt.re, MASK_TEXT, "testfile", tt.name)
4545
req.NoError(err)
4646

4747
outReader := reRunner.Redact(bytes.NewReader([]byte(tt.inputString)))

pkg/redact/yaml.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package redact
33
import (
44
"bufio"
55
"bytes"
6-
"fmt"
76
"io"
87
"io/ioutil"
98
"strconv"
@@ -16,11 +15,12 @@ type YamlRedactor struct {
1615
maskPath []string
1716
foundMatch bool
1817
filePath string
18+
redactName string
1919
}
2020

21-
func NewYamlRedactor(yamlPath, filePath string) *YamlRedactor {
21+
func NewYamlRedactor(yamlPath, filePath, name string) *YamlRedactor {
2222
pathComponents := strings.Split(yamlPath, ".")
23-
return &YamlRedactor{maskPath: pathComponents, filePath: filePath}
23+
return &YamlRedactor{maskPath: pathComponents, filePath: filePath, redactName: name}
2424
}
2525

2626
func (r *YamlRedactor) Redact(input io.Reader) io.Reader {
@@ -65,7 +65,7 @@ func (r *YamlRedactor) Redact(input io.Reader) io.Reader {
6565
buf.WriteTo(writer)
6666

6767
go addRedaction(Redaction{
68-
RedactorName: fmt.Sprintf("yaml %q", strings.Join(r.maskPath, ".")),
68+
RedactorName: r.redactName,
6969
CharactersRemoved: len(doc) - len(newBytes),
7070
Line: 0, // line 0 because we have no way to tell what line was impacted
7171
File: r.filePath,

pkg/redact/yaml_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ xyz:
180180
defer scopetest.End()
181181

182182
req := require.New(t)
183-
yamlRunner := NewYamlRedactor(strings.Join(tt.path, "."), "testfile")
183+
yamlRunner := NewYamlRedactor(strings.Join(tt.path, "."), "testfile", tt.name)
184184

185185
outReader := yamlRunner.Redact(bytes.NewReader([]byte(tt.inputString)))
186186
gotBytes, err := ioutil.ReadAll(outReader)

0 commit comments

Comments
 (0)