-
Notifications
You must be signed in to change notification settings - Fork 1
/
inquiry.go
120 lines (104 loc) · 3.01 KB
/
inquiry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package redact
import (
"bytes"
"encoding/json"
"reflect"
"regexp"
)
// Inquiry provide ways to request data considering what needs to remain secret
type Inquiry struct {
SecretValues []*Secret
secretFieldPatterns []*regexp.Regexp
}
// New creates an empty inquiry
func New() *Inquiry {
return &Inquiry{}
}
// AddSecretValue adds a Secret definition to redact data values
func (inq *Inquiry) AddSecretValue(s *Secret) {
inq.SecretValues = append(inq.SecretValues, s)
}
// RedactValue redacts a value based on all Inquiry.SecretValues
func (inq *Inquiry) redactValue(data []byte) []byte {
redacted := data
for _, s := range inq.SecretValues {
if bytes.Compare(redacted, []byte("")) == 0 {
return redacted
}
redacted = s.Redact(redacted)
}
return redacted
}
// AddSecretField adds a secret field regexp patterns to redact maps by key.
// Map keys are redacted by deleting the key-value pair entirely
func (inq *Inquiry) AddSecretField(f string) error {
re, err := regexp.Compile(f)
if err != nil {
return err
}
inq.secretFieldPatterns = append(inq.secretFieldPatterns, re)
return nil
}
// isSecretField checks if string matches on of the secret fields
func (inq *Inquiry) isSecretField(f string) bool {
for _, sf := range inq.secretFieldPatterns {
if sf.Match([]byte(f)) {
return true
}
}
return false
}
// redact visits all fields of an object checking for secrets
func (inq *Inquiry) redact(copy, original reflect.Value) {
switch original.Kind() {
case reflect.Ptr:
originalValue := original.Elem()
if !originalValue.IsValid() {
return
}
copy.Set(reflect.New(originalValue.Type()))
inq.redact(copy.Elem(), originalValue)
case reflect.Interface:
originalValue := original.Elem()
copyValue := reflect.New(originalValue.Type()).Elem()
inq.redact(copyValue, originalValue)
copy.Set(copyValue)
case reflect.Struct:
for i := 0; i < original.NumField(); i++ {
inq.redact(copy.Field(i), original.Field(i))
}
case reflect.Slice:
copy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap()))
for i := 0; i < original.Len(); i++ {
inq.redact(copy.Index(i), original.Index(i))
}
case reflect.Map:
copy.Set(reflect.MakeMap(original.Type()))
for _, key := range original.MapKeys() {
if inq.isSecretField(key.Interface().(string)) {
continue
}
originalValue := original.MapIndex(key)
copyValue := reflect.New(originalValue.Type()).Elem()
inq.redact(copyValue, originalValue)
copy.SetMapIndex(key, copyValue)
}
case reflect.String:
redacted := inq.redactValue([]byte(original.Interface().(string)))
copy.SetString(string(redacted))
default:
copy.Set(original)
}
}
// Redact redacts the inquired data
func (inq *Inquiry) Redact(data []byte) ([]byte, error) {
var structData interface{}
err := json.Unmarshal(data, &structData)
if err != nil {
return inq.redactValue(data), nil
}
original := reflect.ValueOf(structData)
copy := reflect.New(original.Type()).Elem()
inq.redact(copy, original)
return json.Marshal(copy.Interface())
}