|
| 1 | +package redact |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "io" |
| 7 | + "io/ioutil" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "gopkg.in/yaml.v2" |
| 12 | +) |
| 13 | + |
| 14 | +type YamlRedactor struct { |
| 15 | + maskPath []string |
| 16 | + foundMatch bool |
| 17 | +} |
| 18 | + |
| 19 | +func NewYamlRedactor(yamlPath string) *YamlRedactor { |
| 20 | + pathComponents := strings.Split(yamlPath, ".") |
| 21 | + return &YamlRedactor{maskPath: pathComponents} |
| 22 | +} |
| 23 | + |
| 24 | +func (r *YamlRedactor) Redact(input io.Reader) io.Reader { |
| 25 | + reader, writer := io.Pipe() |
| 26 | + go func() { |
| 27 | + var err error |
| 28 | + defer func() { |
| 29 | + if err == io.EOF { |
| 30 | + writer.Close() |
| 31 | + } else { |
| 32 | + writer.CloseWithError(err) |
| 33 | + } |
| 34 | + }() |
| 35 | + reader := bufio.NewReader(input) |
| 36 | + |
| 37 | + var doc []byte |
| 38 | + doc, err = ioutil.ReadAll(reader) |
| 39 | + var yamlInterface interface{} |
| 40 | + err = yaml.Unmarshal(doc, &yamlInterface) |
| 41 | + if err != nil { |
| 42 | + buf := bytes.NewBuffer(doc) |
| 43 | + buf.WriteTo(writer) |
| 44 | + err = nil // this is not a fatal error |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + newYaml := r.redactYaml(yamlInterface, r.maskPath) |
| 49 | + if !r.foundMatch { |
| 50 | + // no match found, so make no changes |
| 51 | + buf := bytes.NewBuffer(doc) |
| 52 | + buf.WriteTo(writer) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + var newBytes []byte |
| 57 | + newBytes, err = yaml.Marshal(newYaml) |
| 58 | + if err != nil { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + buf := bytes.NewBuffer(newBytes) |
| 63 | + buf.WriteTo(writer) |
| 64 | + return |
| 65 | + }() |
| 66 | + return reader |
| 67 | +} |
| 68 | + |
| 69 | +func (r *YamlRedactor) redactYaml(in interface{}, path []string) interface{} { |
| 70 | + if len(path) == 0 { |
| 71 | + r.foundMatch = true |
| 72 | + return MASK_TEXT |
| 73 | + } |
| 74 | + switch typed := in.(type) { |
| 75 | + case []interface{}: |
| 76 | + // check if first path element is * - if it is, run redact on all children |
| 77 | + if path[0] == "*" { |
| 78 | + var newArr []interface{} |
| 79 | + for _, child := range typed { |
| 80 | + newChild := r.redactYaml(child, path[1:]) |
| 81 | + newArr = append(newArr, newChild) |
| 82 | + } |
| 83 | + return newArr |
| 84 | + } |
| 85 | + // check if first path element is an integer - if it is, run redact on that child |
| 86 | + pathIdx, err := strconv.Atoi(path[0]) |
| 87 | + if err != nil { |
| 88 | + return typed |
| 89 | + } |
| 90 | + if len(typed) > pathIdx { |
| 91 | + child := typed[pathIdx] |
| 92 | + typed[pathIdx] = r.redactYaml(child, path[1:]) |
| 93 | + return typed |
| 94 | + } |
| 95 | + return typed |
| 96 | + case map[interface{}]interface{}: |
| 97 | + child, ok := typed[path[0]] |
| 98 | + if ok { |
| 99 | + newChild := r.redactYaml(child, path[1:]) |
| 100 | + typed[path[0]] = newChild |
| 101 | + } |
| 102 | + return typed |
| 103 | + default: |
| 104 | + return typed |
| 105 | + } |
| 106 | +} |
0 commit comments