-
Notifications
You must be signed in to change notification settings - Fork 0
/
masking.go
227 lines (204 loc) · 6.24 KB
/
masking.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
package gorm_masking
import (
"fmt"
"github.com/spf13/cast"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"reflect"
)
// base data type
type MaskingType string
type DataMasking interface {
//
// Making
// @Description: desensitization and encryption data
// @param src
// @param field
// @param db
// @return string desensitization value
// @return string encryption value
//
Making(src string, field *schema.Field, db *gorm.DB) (string, string)
//
// UnMasking
// @Description: unmasking data
// @param v encryption value
// @param field
// @param db
// @return string unmasking value
//
UnMasking(v string, field *schema.Field, db *gorm.DB) string
}
// plugin has implemented plugin type
const (
Common MaskingType = "common"
)
// system const value
const (
// struct tag name
MaskingTag = "masking"
// struct tag name
MaskingEncryptColumnTag = "masking_encrypt_column"
// context key,value is []string
UnMaskingContext = "unmasking"
)
var typeOptions map[MaskingType]DataMasking
var visitLog VisitLog
func RegisterTypeOptions(maskingType MaskingType, dFunc DataMasking) {
typeOptions[maskingType] = dFunc
}
func GetTypeOption(maskingType MaskingType) DataMasking {
return typeOptions[maskingType]
}
type Config struct {
// encryption and decryption key
Key string
VisitLog VisitLog
}
func New(config *Config) *Masking {
if config == nil {
panic("config is nil")
}
if len(config.Key) == 0 {
panic("lost required config key")
}
if config.VisitLog == nil {
visitLog = &NonVisitLog{}
} else {
visitLog = config.VisitLog
}
return &Masking{config: config}
}
type Masking struct {
config *Config
}
func (m *Masking) Name() string {
return "masking"
}
func (m *Masking) Initialize(db *gorm.DB) error {
typeOptions = make(map[MaskingType]DataMasking)
RegisterTypeOptions(Common, NewCommonMasking(m.config.Key, float32(0.5), Middle))
db.Callback().Create().Before("gorm:create").Register("update_created_at", CreateMasking)
db.Callback().Update().Before("gorm:update").Register("my_plugin:before_update", UpdateMasking)
db.Callback().Query().After("gorm:query").Register("my_plugin:after_query", QueryMasking)
return nil
}
func CreateMasking(db *gorm.DB) {
var storeFunc DataStore
if v, ok := db.Statement.ReflectValue.Type().(DataStore); ok {
storeFunc = v
} else {
storeFunc = &SameTableDataStore{}
}
fieldName := db.Statement.Schema.FieldsByName
for k, _ := range fieldName {
v := fieldName[k]
if desensitizationType, ok := v.Tag.Lookup(MaskingTag); ok {
if v.FieldType.Kind() != reflect.String {
panic(fmt.Sprintf("struct=%s proertty=%s masking field type must be string", db.Statement.Schema.Name, k))
}
dType := MaskingType(desensitizationType)
dFunc := typeOptions[dType]
switch db.Statement.ReflectValue.Kind() {
case reflect.Slice:
for i := 0; i < db.Statement.ReflectValue.Len(); i++ {
sliceV := db.Statement.ReflectValue.Index(i)
if actualValue, isZero := v.ValueOf(db.Statement.Context, sliceV); !isZero {
dValue, encryptionValue := dFunc.Making(cast.ToString(actualValue), v, db)
v.Set(db.Statement.Context, sliceV, dValue)
if err := storeFunc.Create(v, sliceV, dValue, encryptionValue, actualValue, dType, db); err != nil {
panic(err)
}
} else {
continue
}
}
case reflect.Struct:
if actualValue, isZero := v.ValueOf(db.Statement.Context, db.Statement.ReflectValue); !isZero {
dValue, encryptionValue := dFunc.Making(cast.ToString(actualValue), v, db)
v.Set(db.Statement.Context, db.Statement.ReflectValue, dValue)
if err := storeFunc.Create(v, db.Statement.ReflectValue, dValue, encryptionValue, actualValue, dType, db); err != nil {
panic(err)
}
} else {
continue
}
default:
db.Config.Logger.Error(db.Statement.Context, "unsupport kind [%d] of value", db.Statement.ReflectValue.Kind())
}
}
}
}
func UpdateMasking(db *gorm.DB) {
var storeFunc DataStore
if v, ok := db.Statement.ReflectValue.Type().(DataStore); ok {
storeFunc = v
} else {
storeFunc = &SameTableDataStore{}
}
if updateInfo, ok := db.Statement.Dest.(map[string]interface{}); ok {
for updateColumn := range updateInfo {
updateV := updateInfo[updateColumn]
updateField := db.Statement.Schema.LookUpField(updateColumn)
if desensitizationType, ok := updateField.Tag.Lookup(MaskingTag); ok {
dType := MaskingType(desensitizationType)
dFunc := typeOptions[dType]
dValue, encryptionValue := dFunc.Making(cast.ToString(updateV), updateField, db)
if err := storeFunc.Update(updateField, dValue, encryptionValue, updateV, dType, db); err != nil {
}
updateInfo[updateColumn] = dValue
}
}
return
}
destType := reflect.TypeOf(db.Statement.Dest)
destValue := reflect.ValueOf(db.Statement.Dest)
if destType.Elem().Kind() == reflect.Struct {
destType = destType.Elem()
destValue = destValue.Elem()
for i := 0; i < destType.NumField(); i++ {
field := destType.Field(i)
if desensitizationType, ok := field.Tag.Lookup(MaskingTag); ok {
val := destValue.Field(i).String()
if len(val) == 0 {
continue
}
dType := MaskingType(desensitizationType)
dFunc := typeOptions[dType]
dbField := db.Statement.Schema.LookUpField(field.Name)
dValue, encryptionValue := dFunc.Making(cast.ToString(val), dbField, db)
if err := storeFunc.Update(dbField, dValue, encryptionValue, val, dType, db); err != nil {
}
destValue.Field(i).SetString(dValue)
}
}
}
}
func QueryMasking(db *gorm.DB) {
val := db.Statement.Context.Value(UnMaskingContext)
if val != nil {
if columns, ok := val.([]string); ok && len(columns) > 0 {
refVal := reflect.ValueOf(db.Statement.Model).Elem()
switch refVal.Kind() {
case reflect.Struct:
for i := range columns {
decrypt(refVal, refVal.FieldByName(columns[i]), columns[i], db)
}
case reflect.Slice:
if refVal.Len() >= 1 {
for i := 0; i < refVal.Len(); i++ {
curVal := refVal.Index(i)
if curVal.Kind() == reflect.Pointer {
curVal = curVal.Elem()
}
for j := range columns {
decrypt(curVal, curVal.FieldByName(columns[j]), columns[j], db)
}
}
}
default:
db.Config.Logger.Error(db.Statement.Context, "无法支持的类型:"+refVal.Kind().String())
}
}
}
}