github.com/kastenhq/syft@v0.0.0-20230821225854-0710af25cdbe/syft/formats/internal/testutils/redactor.go (about) 1 package testutils 2 3 import ( 4 "bytes" 5 "regexp" 6 ) 7 8 var ( 9 _ Redactor = (*RedactorFn)(nil) 10 _ Redactor = (*PatternReplacement)(nil) 11 _ Redactor = (*ValueReplacement)(nil) 12 _ Redactor = (*Redactions)(nil) 13 ) 14 15 type Redactor interface { 16 Redact([]byte) []byte 17 } 18 19 // Replace by function ////////////////////////////// 20 21 type RedactorFn func([]byte) []byte 22 23 func (r RedactorFn) Redact(b []byte) []byte { 24 return r(b) 25 } 26 27 // Replace by regex ////////////////////////////// 28 29 type PatternReplacement struct { 30 Search *regexp.Regexp 31 Replace string 32 } 33 34 func NewPatternReplacement(r *regexp.Regexp) PatternReplacement { 35 return PatternReplacement{ 36 Search: r, 37 Replace: "redacted", 38 } 39 } 40 41 func (p PatternReplacement) Redact(b []byte) []byte { 42 return p.Search.ReplaceAll(b, []byte(p.Replace)) 43 } 44 45 // Replace by value ////////////////////////////// 46 47 type ValueReplacement struct { 48 Search string 49 Replace string 50 } 51 52 func NewValueReplacement(v string) ValueReplacement { 53 return ValueReplacement{ 54 Search: v, 55 Replace: "redacted", 56 } 57 } 58 59 func (v ValueReplacement) Redact(b []byte) []byte { 60 return bytes.ReplaceAll(b, []byte(v.Search), []byte(v.Replace)) 61 } 62 63 // Handle a collection of redactors ////////////////////////////// 64 65 type Redactions struct { 66 redactors []Redactor 67 } 68 69 func NewRedactions(redactors ...Redactor) *Redactions { 70 r := &Redactions{ 71 redactors: redactors, 72 } 73 74 return r.WithFunctions(carriageRedactor) 75 } 76 77 func (r *Redactions) WithPatternRedactors(values map[string]string) *Redactions { 78 for k, v := range values { 79 r.redactors = append(r.redactors, 80 PatternReplacement{ 81 Search: regexp.MustCompile(k), 82 Replace: v, 83 }, 84 ) 85 } 86 return r 87 } 88 89 func (r *Redactions) WithValueRedactors(values map[string]string) *Redactions { 90 for k, v := range values { 91 r.redactors = append(r.redactors, 92 ValueReplacement{ 93 Search: k, 94 Replace: v, 95 }, 96 ) 97 } 98 return r 99 } 100 101 func (r *Redactions) WithPatternsRedacted(values ...string) *Redactions { 102 for _, pattern := range values { 103 r.redactors = append(r.redactors, 104 NewPatternReplacement(regexp.MustCompile(pattern)), 105 ) 106 } 107 return r 108 } 109 110 func (r *Redactions) WithValuesRedacted(values ...string) *Redactions { 111 for _, v := range values { 112 r.redactors = append(r.redactors, 113 NewValueReplacement(v), 114 ) 115 } 116 return r 117 } 118 119 func (r *Redactions) WithFunctions(values ...func([]byte) []byte) *Redactions { 120 for _, fn := range values { 121 r.redactors = append(r.redactors, 122 RedactorFn(fn), 123 ) 124 } 125 return r 126 } 127 128 func (r *Redactions) WithRedactors(rs ...Redactor) *Redactions { 129 r.redactors = append(r.redactors, rs...) 130 return r 131 } 132 133 func (r Redactions) Redact(b []byte) []byte { 134 for _, redactor := range r.redactors { 135 b = redactor.Redact(b) 136 } 137 return b 138 } 139 140 func carriageRedactor(s []byte) []byte { 141 return bytes.ReplaceAll(s, []byte("\r\n"), []byte("\n")) 142 }