-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrub.go
191 lines (172 loc) · 5.07 KB
/
scrub.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"reflect"
"regexp"
"strings"
"github.com/perimeterx/marshmallow"
)
// the only known property in the input json ahead of time
type userID struct {
ID int `json:"id"`
}
// return a json string with scrubbed sensitive information from the provided input and sensitive fields files
func ScrubPersonalInfo(inputPath, sensitiveFieldsPath string) (string, error) {
if inputPath == "" {
return "", errors.New("missing required input file path")
}
if sensitiveFieldsPath == "" {
return "", errors.New("missing required sensitiveFields file path")
}
inputFile, _ := ioutil.ReadFile(inputPath)
sensitiveFields, _ := getSensitiveFields(sensitiveFieldsPath)
uid := userID{}
input, err := marshmallow.Unmarshal([]byte(inputFile), &uid)
if err != nil {
log.Fatal(err)
return "", err
}
// keep track of original field values before scrub
savedValues := make([]any, 0)
// recursively find and scrub fields from input at any level
scrubRecursive(&input, "", sensitiveFields, &savedValues, true /* mask */, false /* doScrub */)
// get json from the scrub string to return
var b []byte
b, _ = json.Marshal(input)
// reset all scrubbed values back to their original values
scrubRecursive(input, "", sensitiveFields, &savedValues, false /* unmask */, false /* doScrub */)
// return the scrubbed string
return string(b), nil
}
// recursively work through the unstructured json to scrub sensitive fields
func scrubRecursive(field interface{}, fieldName string, sensitiveFields map[string]bool, savedValues *[]any, mask bool, doScrub bool) {
// field must be a pointer and addressable
addrValue := reflect.ValueOf(field)
if addrValue.Kind() != reflect.Ptr {
return
}
fieldValue := addrValue.Elem()
if !fieldValue.IsValid() {
return
}
fieldType := fieldValue.Type()
if fieldType.Kind() == reflect.Map {
// got an object, loop through each property
for _, fKey := range fieldValue.MapKeys() {
fValue := fieldValue.MapIndex(fKey).Interface()
scrubRecursive(&fValue, fKey.String(), sensitiveFields, savedValues, mask, doScrub)
fieldValue.SetMapIndex(fKey, reflect.ValueOf(fValue))
}
return
}
// skip if no field names
if fieldName == "" {
return
}
// skip these types
if !fieldValue.CanSet() || fieldValue.IsZero() {
return
}
if fieldType.Kind() == reflect.Interface {
_, doFieldScrub := sensitiveFields[strings.ToLower(fieldName)]
// if parent field is sensitive field, scrub all children (sensitive or not), track in doScrub
ok := doScrub || doFieldScrub
// scrub leaf nodes; otherwise, continue recursing
switch fValue := fieldValue.Interface().(type) {
case string:
if !ok {
return
}
if mask {
*savedValues = append(*savedValues, fValue)
sampleRegexp := regexp.MustCompile(`[A-Za-z0-9]`)
result := sampleRegexp.ReplaceAllString(fValue, "*")
fieldValue.Set(reflect.ValueOf(result))
} else {
fieldValue.Set(reflect.ValueOf((*savedValues)[0]))
*savedValues = (*savedValues)[1:]
}
case bool:
if !ok {
return
}
if mask {
*savedValues = append(*savedValues, fValue)
fieldValue.Set(reflect.ValueOf("-"))
} else {
fieldValue.Set(reflect.ValueOf((*savedValues)[0]))
*savedValues = (*savedValues)[1:]
}
case int:
if !ok {
return
}
case float64:
if !ok {
return
}
if mask {
format := "%g"
if fValue == math.Trunc(fValue) {
format = "%.0f"
}
*savedValues = append(*savedValues, fValue)
str := fmt.Sprintf(format, fValue)
exp := regexp.MustCompile(`[0-9]`)
result := exp.ReplaceAllString(str, "*")
fieldValue.Set(reflect.ValueOf(result))
} else {
fieldValue.Set(reflect.ValueOf((*savedValues)[0]))
*savedValues = (*savedValues)[1:]
}
case uint64:
if !ok {
return
}
if mask {
*savedValues = append(*savedValues, fieldValue.Uint())
exp := regexp.MustCompile(`[0-9]`)
str := fmt.Sprintf("%d", fieldValue.Uint())
result := exp.ReplaceAllString(str, "*")
fieldValue.Set(reflect.ValueOf(result))
} else {
fieldValue.Set(reflect.ValueOf((*savedValues)[0]))
*savedValues = (*savedValues)[1:]
}
case []interface{}:
// landed in an array
for key, value := range fValue {
scrubRecursive(&value, fieldName, sensitiveFields, savedValues, mask, false /* doScrub */)
fValue[key] = value
}
default:
// dealing with an object
value := fieldValue.Elem().Interface()
m := value.(map[string]interface{})
_, doScrub := sensitiveFields[strings.ToLower(fieldName)]
scrubRecursive(&m, fieldName, sensitiveFields, savedValues, mask, doScrub)
}
}
}
// return map of sensitive fields to scrub
func getSensitiveFields(sensitiveFieldsPath string) (map[string]bool, error) {
sensitiveFields := make(map[string]bool)
readFile, err := os.Open(sensitiveFieldsPath)
if err != nil {
fmt.Println(err)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
sensitiveFields[fileScanner.Text()] = true
}
readFile.Close()
return sensitiveFields, nil
}