-
Notifications
You must be signed in to change notification settings - Fork 30
/
masker.go
405 lines (380 loc) · 12 KB
/
masker.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package masker
import (
"fmt"
"reflect"
)
const tagName = "mask"
// MaskerType is a string type for masker type
type MaskerType string
// MaskerType constants
const (
MaskerTypeNone MaskerType = "none"
MaskerTypePassword MaskerType = "password"
MaskerTypeName MaskerType = "name"
MaskerTypeAddress MaskerType = "addr"
MaskerTypeEmail MaskerType = "email"
MaskerTypeMobile MaskerType = "mobile"
MaskerTypeTel MaskerType = "tel"
MaskerTypeID MaskerType = "id"
MaskerTypeCredit MaskerType = "credit"
MaskerTypeURL MaskerType = "url"
MaskerTypeStruct MaskerType = "struct"
)
// Masker is an interface for masking sensitive data
type Masker interface {
Marshal(string, string) string
}
// MaskerMarshaler is a masker marshaler
type MaskerMarshaler struct {
Maskers map[MaskerType]Masker
masker string // default masker
}
// Marshal returns a masked value by masker type
// It is used for masking sensitive data
// Example:
//
// m := masker.NewMaskerMarshaler()
// log.Println(m.Marshal(masker.MaskerTypeNone, "none")) // none <nil>
// log.Println(m.Marshal(masker.MaskerTypePassword, "password")) // ************** <nil>
// log.Println(m.Marshal(masker.MaskerTypeName, "name")) // n**e <nil>
// log.Println(m.Marshal(masker.MaskerTypeAddress, "address")) // addres****** <nil>
// log.Println(m.Marshal(masker.MaskerTypeEmail, "email")) // ema**** <nil>
// log.Println(m.Marshal(masker.MaskerTypeMobile, "mobile")) // mobi*** <nil>
// log.Println(m.Marshal(masker.MaskerTypeTel, "tel")) // tel <nil>
// log.Println(m.Marshal(masker.MaskerTypeID, "id")) // id**** <nil>
// log.Println(m.Marshal(masker.MaskerTypeCredit, "4111111111111111")) // 411111******1111 <nil>
// log.Println(m.Marshal(masker.MaskerTypeURL, "http://john:password@localhost:3000")) // http://john:xxxxx@localhost:3000 <nil>
func (m *MaskerMarshaler) Marshal(t MaskerType, value string) (string, error) {
masker, ok := m.Maskers[t]
if !ok {
return "", fmt.Errorf("masker %v not found", t)
}
return masker.Marshal(m.masker, value), nil
}
// Register adds a masker by masker type
// It is used for adding or override a masker by masker type
// Example:
//
// m := masker.NewMaskerMarshaler()
// m.Register(masker.MaskerTypePassword, &PasswordMasker{})
// log.Println(m.List()) // [password name addr email tel id url none mobile credit]
func (m *MaskerMarshaler) Register(t MaskerType, masker Masker) {
m.Maskers[t] = masker
}
// Unregister removes a masker by masker type
// It is used for removing a masker by masker type
// Example:
//
// m := masker.NewMaskerMarshaler()
// m.Unregister(masker.MaskerTypePassword)
// log.Println(m.List()) // [name addr email tel id url none mobile credit]
func (m *MaskerMarshaler) Unregister(t MaskerType) {
delete(m.Maskers, t)
}
// Get returns a masker by masker type
// It is used for getting a masker by masker type
// Example:
//
// m := masker.NewMaskerMarshaler()
// masker, _ := m.Get(masker.MaskerTypePassword)
// log.Println(masker) // &{PasswordMasker}
func (m *MaskerMarshaler) Get(t MaskerType) (Masker, error) {
masker, ok := m.Maskers[t]
if !ok {
return nil, fmt.Errorf("masker %v not found", t)
}
return masker, nil
}
// List returns a list of masker types
// It is used for listing all masker types
// Example:
//
// m := masker.NewMaskerMarshaler()
// log.Println(m.List()) // [password name addr email tel id url none mobile credit]
func (m *MaskerMarshaler) List() []MaskerType {
var list []MaskerType
for t := range m.Maskers {
list = append(list, t)
}
return list
}
func (m *MaskerMarshaler) SetMasker(masker string) {
m.masker = masker
}
// Struct must input a interface{}, add tag mask on struct fields, after Struct(), return a pointer interface{} of input type and it will be masked with the tag format type
//
// Example:
//
// type Foo struct {
// Name string `mask:"name"`
// Email string `mask:"email"`
// Password string `mask:"password"`
// ID string `mask:"id"`
// Address string `mask:"addr"`
// Mobile string `mask:"mobile"`
// Telephone string `mask:"tel"`
// Credit string `mask:"credit"`
// URL string `mask:"url"`
// Foo *Foo `mask:"struct"`
// }
//
// func main() {
// m := masker.NewMaskerMarshaler()
// log.Println(m.List()) // [password name addr email tel id url none mobile credit]
// foo1 := &Foo{
// Name: "John Doe",
// Email: "john@gmail.com",
// Password: "password",
// ID: "1234567890",
// Address: "123 Main St",
// Mobile: "1234567890",
// Telephone: "1234567890",
// Credit: "4111111111111111",
// URL: "http://john:password@localhost:3000",
// Foo: &Foo{
// Name: "John Doe",
// Email: "john@gmail.com",
// Password: "password",
// ID: "1234567890",
// Address: "123 Main St",
// Mobile: "1234567890",
// Telephone: "1234567890",
// Credit: "4111111111111111",
// URL: "http://john:password@localhost:3000",
// },
// }
//
// foo2, _ := m.Struct(foo1)
//
// log.Println(foo1) // &{John Doe john@gmail.com password 1234567890 123 Main St 1234567890 1234567890 4111111111111111 http://john:password@localhost:3000 0xc0000001e0}
// log.Println(foo1.Foo) // &{John Doe john@gmail.com password 1234567890 123 Main St 1234567890 1234567890 4111111111111111 http://john:password@localhost:3000 <nil>}
// log.Println(foo2) // &{J**n D**e joh****@gmail.com ************** 123456**** 123 Ma****** 1234***890 (12)3456-**** 411111******1111 http://john:xxxxx@localhost:3000 0xc000000320}
// log.Println(foo2.(*Foo).Foo) // &{J**n D**e joh****@gmail.com ************** 123456**** 123 Ma****** 1234***890 (12)3456-**** 411111******1111 http://john:xxxxx@localhost:3000 <nil>}
// }
func (m *MaskerMarshaler) Struct(s interface{}) (interface{}, error) {
if s == nil {
return nil, fmt.Errorf("input is nil")
}
var selem, tptr reflect.Value
st := reflect.TypeOf(s)
if st.Kind() == reflect.Ptr {
tptr = reflect.New(st.Elem())
selem = reflect.ValueOf(s).Elem()
} else {
tptr = reflect.New(st)
selem = reflect.ValueOf(s)
}
for i := 0; i < selem.NumField(); i++ {
if !selem.Type().Field(i).IsExported() {
continue
}
mtag := selem.Type().Field(i).Tag.Get(tagName)
if len(mtag) == 0 {
tptr.Elem().Field(i).Set(selem.Field(i))
continue
}
switch selem.Field(i).Type().Kind() {
default:
tptr.Elem().Field(i).Set(selem.Field(i))
case reflect.String:
v, err := m.Marshal(MaskerType(mtag), selem.Field(i).String())
if err != nil {
return nil, err
}
tptr.Elem().Field(i).SetString(v)
case reflect.Struct:
if MaskerType(mtag) == MaskerTypeStruct {
_t, err := m.Struct(selem.Field(i).Interface())
if err != nil {
return nil, err
}
tptr.Elem().Field(i).Set(reflect.ValueOf(_t).Elem())
}
case reflect.Ptr:
if selem.Field(i).IsNil() {
continue
}
if MaskerType(mtag) == MaskerTypeStruct {
_t, err := m.Struct(selem.Field(i).Interface())
if err != nil {
return nil, err
}
tptr.Elem().Field(i).Set(reflect.ValueOf(_t))
}
case reflect.Slice:
if selem.Field(i).IsNil() {
continue
}
if selem.Field(i).Type().Elem().Kind() == reflect.String {
orgval := selem.Field(i).Interface().([]string)
newval := make([]string, len(orgval))
for i, val := range selem.Field(i).Interface().([]string) {
v, err := m.Marshal(MaskerType(mtag), val)
if err != nil {
return nil, err
}
newval[i] = v
}
tptr.Elem().Field(i).Set(reflect.ValueOf(newval))
continue
}
if selem.Field(i).Type().Elem().Kind() == reflect.Struct && MaskerType(mtag) == MaskerTypeStruct {
newval := reflect.MakeSlice(selem.Field(i).Type(), 0, selem.Field(i).Len())
for j, l := 0, selem.Field(i).Len(); j < l; j++ {
_n, err := m.Struct(selem.Field(i).Index(j).Interface())
if err != nil {
return nil, err
}
newval = reflect.Append(newval, reflect.ValueOf(_n).Elem())
}
tptr.Elem().Field(i).Set(newval)
continue
}
if selem.Field(i).Type().Elem().Kind() == reflect.Ptr && MaskerType(mtag) == MaskerTypeStruct {
newval := reflect.MakeSlice(selem.Field(i).Type(), 0, selem.Field(i).Len())
for j, l := 0, selem.Field(i).Len(); j < l; j++ {
_n, err := m.Struct(selem.Field(i).Index(j).Interface())
if err != nil {
return nil, err
}
newval = reflect.Append(newval, reflect.ValueOf(_n))
}
tptr.Elem().Field(i).Set(newval)
continue
}
if selem.Field(i).Type().Elem().Kind() == reflect.Interface && MaskerType(mtag) == MaskerTypeStruct {
newval := reflect.MakeSlice(selem.Field(i).Type(), 0, selem.Field(i).Len())
for j, l := 0, selem.Field(i).Len(); j < l; j++ {
_n, err := m.Struct(selem.Field(i).Index(j).Interface())
if err != nil {
return nil, err
}
if reflect.TypeOf(selem.Field(i).Index(j).Interface()).Kind() != reflect.Ptr {
newval = reflect.Append(newval, reflect.ValueOf(_n).Elem())
} else {
newval = reflect.Append(newval, reflect.ValueOf(_n))
}
}
tptr.Elem().Field(i).Set(newval)
continue
}
case reflect.Interface:
if selem.Field(i).IsNil() {
continue
}
if MaskerType(mtag) != MaskerTypeStruct {
continue
}
_t, err := m.Struct(selem.Field(i).Interface())
if err != nil {
return nil, err
}
if reflect.TypeOf(selem.Field(i).Interface()).Kind() != reflect.Ptr {
tptr.Elem().Field(i).Set(reflect.ValueOf(_t).Elem())
} else {
tptr.Elem().Field(i).Set(reflect.ValueOf(_t))
}
}
}
return tptr.Interface(), nil
}
// NewMaskerMarshaler returns a new masker marshaler
// It has default maskers and default masker
// Default maskers are:
// - NoneMasker
// - PasswordMasker
// - NameMasker
// - AddressMasker
// - EmailMasker
// - MobileMasker
// - TelephoneMasker
// - IDMasker
// - CreditMasker
// - URLMasker
//
// Default masker is "*"
// It is used for masking sensitive data
func NewMaskerMarshaler() *MaskerMarshaler {
return &MaskerMarshaler{
Maskers: map[MaskerType]Masker{
MaskerTypeNone: &NoneMasker{},
MaskerTypePassword: &PasswordMasker{},
MaskerTypeName: &NameMasker{},
MaskerTypeAddress: &AddressMasker{},
MaskerTypeEmail: &EmailMasker{},
MaskerTypeMobile: &MobileMasker{},
MaskerTypeTel: &TelephoneMasker{},
MaskerTypeID: &IDMasker{},
MaskerTypeCredit: &CreditMasker{},
MaskerTypeURL: &URLMasker{},
},
masker: "*",
}
}
// DefaultMaskerMarshaler is a default masker marshaler
// It has default maskers and default masker
// Default maskers are:
// - NoneMasker
// - PasswordMasker
// - NameMasker
// - AddressMasker
// - EmailMasker
// - MobileMasker
// - TelephoneMasker
// - IDMasker
// - CreditMasker
// - URLMasker
//
// Default masker is "*"
// It is used for masking sensitive data
var DefaultMaskerMarshaler = &MaskerMarshaler{
Maskers: map[MaskerType]Masker{
MaskerTypeNone: &NoneMasker{},
MaskerTypePassword: &PasswordMasker{},
MaskerTypeName: &NameMasker{},
MaskerTypeAddress: &AddressMasker{},
MaskerTypeEmail: &EmailMasker{},
MaskerTypeMobile: &MobileMasker{},
MaskerTypeTel: &TelephoneMasker{},
MaskerTypeID: &IDMasker{},
MaskerTypeCredit: &CreditMasker{},
MaskerTypeURL: &URLMasker{},
},
masker: "*",
}
func strLoop(str string, length int) string {
var mask string
for i := 1; i <= length; i++ {
mask += str
}
return mask
}
func overlay(str string, overlay string, start int, end int) (overlayed string) {
r := []rune(str)
l := len([]rune(r))
if l == 0 {
return ""
}
if start < 0 {
start = 0
}
if start > l {
start = l
}
if end < 0 {
end = 0
}
if end > l {
end = l
}
if start > end {
tmp := start
start = end
end = tmp
}
overlayed = ""
overlayed += string(r[:start])
overlayed += overlay
overlayed += string(r[end:])
return overlayed
}