-
Notifications
You must be signed in to change notification settings - Fork 154
/
sdkmask.go
214 lines (202 loc) · 6.35 KB
/
sdkmask.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Package dlp sdkmask.go implements Mask API
package dlp
import (
"fmt"
"github.com/bytedance/godlp/dlpheader"
"github.com/bytedance/godlp/errlist"
"github.com/bytedance/godlp/mask"
"reflect"
)
// public func
// Mask will return masked text directly based on methodName
func (I *Engine) Mask(inputText string, methodName string) (outputText string, err error) {
defer I.recoveryImpl()
if !I.hasConfiged() { // not configed
panic(errlist.ERR_HAS_NOT_CONFIGED)
}
if I.hasClosed() {
return "", errlist.ERR_PROCESS_AFTER_CLOSE
}
if len(inputText) > DEF_MAX_INPUT {
return inputText, fmt.Errorf("DEF_MAX_INPUT: %d , %w", DEF_MAX_INPUT, errlist.ERR_MAX_INPUT_LIMIT)
}
if maskWorker, ok := I.maskerMap[methodName]; ok {
return maskWorker.Mask(inputText)
} else {
return inputText, fmt.Errorf("methodName: %s, error: %w", methodName, errlist.ERR_MASKWORKER_NOTFOUND)
}
}
// MaskStruct will mask a strcut object by tag mask info
// 根据tag mask里定义的脱敏规则对struct object直接脱敏, 会修改obj本身,传入指针,返回指针
func (I *Engine) MaskStruct(inPtr interface{}) (outPtr interface{}, retErr error) {
outPtr = inPtr // fail back to inPtr
retErr = errlist.ERR_MASK_STRUCT_OUTPUT // default return err if panic
defer I.recoveryImpl()
if !I.hasConfiged() { // not configed
panic(errlist.ERR_HAS_NOT_CONFIGED)
}
if I.hasClosed() {
return inPtr, errlist.ERR_PROCESS_AFTER_CLOSE
}
if inPtr == nil {
return nil, errlist.ERR_MASK_STRUCT_INPUT
}
outPtr, retErr = I.maskStructImpl(inPtr, DEF_MAX_CALL_DEEP)
return
}
// Register DIY Masker
// 注册自定义打码函数
func (I *Engine) RegisterMasker(maskName string, maskFunc func(string) (string, error)) error {
defer I.recoveryImpl()
if !I.hasConfiged() { // not configed
panic(errlist.ERR_HAS_NOT_CONFIGED)
}
if I.hasClosed() {
return errlist.ERR_PROCESS_AFTER_CLOSE
}
if _, ok := I.maskerMap[maskName]; ok {
return errlist.ERR_MASKName_CONFLICT
} else {
if worker, err := I.NewDIYMaskWorker(maskName, maskFunc); err == nil {
I.maskerMap[maskName] = worker
return nil
} else {
return err
}
}
}
// private func
// DIYMaskWorker stores maskFuc and maskName
type DIYMaskWorker struct {
maskFunc func(string) (string, error)
maskName string
}
// GetRuleName is required by mask.MaskAPI
func (I *DIYMaskWorker) GetRuleName() string {
return I.maskName
}
// Mask is required by mask.MaskAPI
func (I *DIYMaskWorker) Mask(in string) (string, error) {
return I.maskFunc(in)
}
// MaskResult is required by mask.MaskAPI
func (I *DIYMaskWorker) MaskResult(res *dlpheader.DetectResult) error {
if out, err := I.Mask(res.Text); err == nil {
res.MaskText = out
return nil
} else {
return err
}
}
// NewDIYMaskWorker creates mask.MaskAPI object
func (I *Engine) NewDIYMaskWorker(maskName string, maskFunc func(string) (string, error)) (mask.MaskAPI, error) {
worker := new(DIYMaskWorker)
worker.maskName = maskName
worker.maskFunc = maskFunc
return worker, nil
}
// maskStructImpl will mask a strcut object by tag mask info
// 根据tag mask里定义的脱敏规则对struct object直接脱敏, 会修改obj本身,传入指针,返回指针
func (I *Engine) maskStructImpl(inPtr interface{}, level int) (interface{}, error) {
//log.Errorf("[DLP] level:%d, maskStructImpl: %+v", level, inPtr)
if level <= 0 { // call deep check
//log.Errorf("[DLP] !call deep loop detected!")
//log.Errorf("obj: %+v", inPtr)
return inPtr, nil
}
valPtr := reflect.ValueOf(inPtr)
if valPtr.Kind() != reflect.Ptr || valPtr.IsNil() || !valPtr.IsValid() || valPtr.IsZero() {
return inPtr, errlist.ERR_MASK_STRUCT_INPUT
}
val := reflect.Indirect(valPtr)
var retErr error
if val.CanSet() {
if val.Kind() == reflect.Struct {
sz := val.NumField()
if sz > DEF_MAX_INPUT {
return inPtr, fmt.Errorf("DEF_MAX_INPUT: %d , %w", DEF_MAX_INPUT, errlist.ERR_MAX_INPUT_LIMIT)
}
for i := 0; i < sz; i++ {
valField := val.Field(i)
typeField := val.Type().Field(i)
inStr := valField.String()
outStr := inStr // default is orignal str
methodName, ok := typeField.Tag.Lookup("mask")
if !ok { // mask tag not found
continue
}
if valField.CanSet() {
switch valField.Kind() {
case reflect.String:
if len(methodName) > 0 {
if maskWorker, ok := I.maskerMap[methodName]; ok {
if masked, err := maskWorker.Mask(inStr); err == nil {
outStr = masked
valField.SetString(outStr)
}
}
}
case reflect.Struct:
if valField.CanAddr() {
//log.Errorf("[DLP] Struct, %s", typeField.Name)
_, retErr = I.maskStructImpl(valField.Addr().Interface(), level-1)
}
case reflect.Ptr:
if !valField.IsNil() {
//log.Errorf("[DLP] Ptr, %s", typeField.Name)
_, retErr = I.maskStructImpl(valField.Interface(), level-1)
}
case reflect.Interface:
if valField.CanInterface() {
valInterFace := valField.Interface()
if inStr, ok := valInterFace.(string); ok {
outStr := inStr
if len(methodName) > 0 {
if maskWorker, ok := I.maskerMap[methodName]; ok {
if masked, err := maskWorker.Mask(inStr); err == nil {
outStr = masked
if valField.CanSet() {
valField.Set(reflect.ValueOf(outStr))
}
}
}
}
}
}
case reflect.Slice, reflect.Array:
length := valField.Len()
for i := 0; i < length; i++ {
item := valField.Index(i)
if item.Kind() == reflect.String {
inStr := item.String()
outStr := inStr
// use parent mask info
if len(methodName) > 0 {
if maskWorker, ok := I.maskerMap[methodName]; ok {
if masked, err := maskWorker.Mask(inStr); err == nil {
outStr = masked
if item.CanSet() {
item.SetString(outStr)
}
}
}
}
} else if item.Kind() == reflect.Ptr {
if !item.IsNil() {
//log.Errorf("[DLP] Ptr, %s", item.Type().Name())
_, retErr = I.maskStructImpl(item.Interface(), level-1)
}
} else if item.Kind() == reflect.Struct {
if item.CanAddr() {
//log.Errorf("[DLP] Struct, %s", item.Type().Name())
_, retErr = I.maskStructImpl(item.Addr().Interface(), level-1)
}
}
}
}
}
}
}
}
return inPtr, retErr
}