77 "io"
88 "io/ioutil"
99 "regexp"
10+ "sync"
1011
1112 "github.com/gobwas/glob"
1213 "github.com/pkg/errors"
@@ -17,12 +18,36 @@ const (
1718 MASK_TEXT = "***HIDDEN***"
1819)
1920
21+ var allRedactions RedactionList
22+ var redactionListMut sync.Mutex
23+ var pendingRedactions sync.WaitGroup
24+
25+ func init () {
26+ allRedactions = RedactionList {
27+ ByRedactor : map [string ][]Redaction {},
28+ ByFile : map [string ][]Redaction {},
29+ }
30+ }
31+
2032type Redactor interface {
2133 Redact (input io.Reader ) io.Reader
2234}
2335
36+ // Redactions are indexed both by the file affected and by the name of the redactor
37+ type RedactionList struct {
38+ ByRedactor map [string ][]Redaction
39+ ByFile map [string ][]Redaction
40+ }
41+
42+ type Redaction struct {
43+ RedactorName string
44+ CharactersRemoved int
45+ Line int
46+ File string
47+ }
48+
2449func Redact (input []byte , path string , additionalRedactors []* troubleshootv1beta1.Redact ) ([]byte , error ) {
25- redactors , err := getRedactors ()
50+ redactors , err := getRedactors (path )
2651 if err != nil {
2752 return nil , err
2853 }
@@ -46,9 +71,25 @@ func Redact(input []byte, path string, additionalRedactors []*troubleshootv1beta
4671 return redacted , nil
4772}
4873
74+ func GetRedactionList () RedactionList {
75+ pendingRedactions .Wait ()
76+ redactionListMut .Lock ()
77+ defer redactionListMut .Unlock ()
78+ return allRedactions
79+ }
80+
81+ func ResetRedactionList () {
82+ redactionListMut .Lock ()
83+ defer redactionListMut .Unlock ()
84+ allRedactions = RedactionList {
85+ ByRedactor : map [string ][]Redaction {},
86+ ByFile : map [string ][]Redaction {},
87+ }
88+ }
89+
4990func buildAdditionalRedactors (path string , redacts []* troubleshootv1beta1.Redact ) ([]Redactor , error ) {
5091 additionalRedactors := []Redactor {}
51- for _ , redact := range redacts {
92+ for i , redact := range redacts {
5293 if redact == nil {
5394 continue
5495 }
@@ -62,28 +103,30 @@ func buildAdditionalRedactors(path string, redacts []*troubleshootv1beta1.Redact
62103 continue
63104 }
64105
106+ withinRedactNum := 0 // give unique redaction names
107+
65108 for _ , re := range redact .Regex {
66- r , err := NewSingleLineRedactor (re , MASK_TEXT )
109+ r , err := NewSingleLineRedactor (re , MASK_TEXT , path , redactorName ( i , withinRedactNum , redact . Name , "regex" , "" ) )
67110 if err != nil {
68111 return nil , errors .Wrapf (err , "redactor %q" , re )
69112 }
70113 additionalRedactors = append (additionalRedactors , r )
71114 }
72115
73116 for _ , literal := range redact .Values {
74- additionalRedactors = append (additionalRedactors , literalString (literal ))
117+ additionalRedactors = append (additionalRedactors , literalString (literal , path , redactorName ( i , withinRedactNum , redact . Name , "literal" , "" ) ))
75118 }
76119
77120 for _ , re := range redact .MultiLine {
78- r , err := NewMultiLineRedactor (re .Selector , re .Redactor , MASK_TEXT )
121+ r , err := NewMultiLineRedactor (re .Selector , re .Redactor , MASK_TEXT , path , redactorName ( i , withinRedactNum , redact . Name , "multiLine" , "" ) )
79122 if err != nil {
80123 return nil , errors .Wrapf (err , "multiline redactor %+v" , re )
81124 }
82125 additionalRedactors = append (additionalRedactors , r )
83126 }
84127
85128 for _ , yaml := range redact .Yaml {
86- r := NewYamlRedactor (yaml )
129+ r := NewYamlRedactor (yaml , path , redactorName ( i , withinRedactNum , redact . Name , "yaml" , "" ) )
87130 additionalRedactors = append (additionalRedactors , r )
88131 }
89132 }
@@ -122,7 +165,7 @@ func redactMatchesPath(path string, redact *troubleshootv1beta1.Redact) (bool, e
122165 return false , nil
123166}
124167
125- func getRedactors () ([]Redactor , error ) {
168+ func getRedactors (path string ) ([]Redactor , error ) {
126169 // TODO: Make this configurable
127170
128171 // (?i) makes it case insensitive
@@ -159,8 +202,8 @@ func getRedactors() ([]Redactor, error) {
159202 }
160203
161204 redactors := make ([]Redactor , 0 )
162- for _ , re := range singleLines {
163- r , err := NewSingleLineRedactor (re , MASK_TEXT )
205+ for i , re := range singleLines {
206+ r , err := NewSingleLineRedactor (re , MASK_TEXT , path , redactorName ( - 1 , i , "" , "defaultRegex" , re ) )
164207 if err != nil {
165208 return nil , err // maybe skip broken ones?
166209 }
@@ -201,8 +244,8 @@ func getRedactors() ([]Redactor, error) {
201244 },
202245 }
203246
204- for _ , l := range doubleLines {
205- r , err := NewMultiLineRedactor (l .line1 , l .line2 , MASK_TEXT )
247+ for i , l := range doubleLines {
248+ r , err := NewMultiLineRedactor (l .line1 , l .line2 , MASK_TEXT , path , redactorName ( - 1 , i , "" , "defaultMultiLine" , l . line1 ) )
206249 if err != nil {
207250 return nil , err // maybe skip broken ones?
208251 }
@@ -247,3 +290,24 @@ func readLine(r *bufio.Reader) (string, error) {
247290 }
248291 return string (completeLine ), nil
249292}
293+
294+ func addRedaction (redaction Redaction ) {
295+ pendingRedactions .Add (1 )
296+ go func (redaction Redaction ) {
297+ redactionListMut .Lock ()
298+ defer redactionListMut .Unlock ()
299+ defer pendingRedactions .Done ()
300+ allRedactions .ByRedactor [redaction .RedactorName ] = append (allRedactions .ByRedactor [redaction .RedactorName ], redaction )
301+ allRedactions .ByFile [redaction .File ] = append (allRedactions .ByFile [redaction .File ], redaction )
302+ }(redaction )
303+ }
304+
305+ func redactorName (redactorNum , withinRedactorNum int , redactorName , redactorType , redactorLiteral string ) string {
306+ 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 )
311+ }
312+ return fmt .Sprintf ("%s.%d-%q" , redactorType , withinRedactorNum , redactorLiteral )
313+ }
0 commit comments