/
callback.go
209 lines (190 loc) · 5.59 KB
/
callback.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
package gormplugin2
import (
"context"
"strings"
"unsafe"
"github.com/andeya/gust"
"github.com/andeya/mysql-aes/modifier"
_ "github.com/pingcap/parser/test_driver"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
var dbMap = gust.NewMutex(make(map[uintptr]gust.Errable[error]))
func RegisterAesCallback(db *gorm.DB) gust.Errable[error] {
if !modifier.AllowMysqlAES() {
return gust.NonErrable[error]()
}
m := dbMap.Lock()
defer dbMap.Unlock(m)
key := uintptr(unsafe.Pointer(db))
if err, ok := m[key]; ok {
return err
}
ctl, regErr := newDbController(db)
if regErr != nil {
m[key] = gust.ToErrable[error](regErr)
return m[key]
}
gust.TryPanic(db.Callback().Query().Before("gorm:query").Register("aes:query", ctl.queryCallback))
gust.TryPanic(db.Callback().Delete().Before("gorm:delete").Register("aes:delete", ctl.deleteCallback))
gust.TryPanic(db.Callback().Create().Before("gorm:create").Register("aes:before_create", ctl.createCallback))
gust.TryPanic(db.Callback().Update().Before("gorm:update").Register("aes:before_update", ctl.updateCallback))
gust.TryPanic(db.Callback().Raw().Before("gorm:raw").Register("aes:raw", ctl.rawCallback))
gust.TryPanic(db.Callback().Row().Before("gorm:row").Register("aes:row", ctl.rawCallback))
m[key] = gust.NonErrable[error]()
return m[key]
}
// RegisterAesTable Register the AES information of the specified DB Table
// NOTE:
// Raw().Rows() and Raw().Row() methods do not support transparent AES decryption
// When calling Raw().Scan(&v), if v is not the corresponding Model, Model(m).Raw().Scan(&v) should be called instead
// The length of the ciphertext is: if the length of the plaintext is 16n+m (m<16), then the length of the ciphertext is (16n+16)x4/3, and the ciphertext has increased by at least 1/3
func RegisterAesTable(db *gorm.DB, tableName, aesKey string, aesColumnNames []string) gust.Errable[error] {
rawDB, err := db.DB()
if err != nil {
return gust.ToErrable(err)
}
return gust.ToErrable(modifier.RegisterAesTable(rawDB, tableName, aesKey, aesColumnNames))
}
type dbController struct {
dbName string
}
func newDbController(db *gorm.DB) (*dbController, error) {
rawDB, err := db.DB()
if err != nil {
return nil, err
}
dbName, err := modifier.QueryDbName(rawDB)
if err != nil {
return nil, err
}
return &dbController{dbName: dbName}, nil
}
type aesCallback struct {
dbName string
*modifier.AesConverter
*gorm.DB
}
func (c *dbController) newAesCallback(scope *gorm.DB) gust.Option[aesCallback] {
ctl := modifier.NewAesConverter(c.dbName)
if ctl.IsSome() {
return gust.Some(aesCallback{dbName: c.dbName, DB: scope, AesConverter: ctl.UnwrapUnchecked()})
}
return gust.None[aesCallback]()
}
func (c *dbController) queryCallback(scope *gorm.DB) {
if scope.Error != nil {
return
}
c.newAesCallback(scope).Inspect(func(a aesCallback) {
queryAesScope{
aesCallback: a,
}.callback()
})
}
func (c *dbController) createCallback(scope *gorm.DB) {
if scope.Error != nil {
return
}
c.newAesCallback(scope).Inspect(func(a aesCallback) {
createAesScope{
aesCallback: a,
}.callback()
})
}
func (c *dbController) deleteCallback(scope *gorm.DB) {
if scope.Error != nil {
return
}
c.newAesCallback(scope).Inspect(func(a aesCallback) {
queryAesScope{
aesCallback: a,
}.callback()
})
}
func (c *dbController) updateCallback(scope *gorm.DB) {
if scope.Error != nil {
return
}
c.newAesCallback(scope).Inspect(func(a aesCallback) {
updateAesScope{
aesCallback: a,
}.callback()
})
}
func (c *dbController) rawCallback(scope *gorm.DB) {
if scope.Error != nil {
return
}
ctl := modifier.NewAesConverter(c.dbName)
if ctl.IsNone() {
return
}
rawAesScope{
aesCallback: aesCallback{dbName: c.dbName, DB: scope, AesConverter: ctl.UnwrapUnchecked()},
}.callback()
}
func (a aesCallback) getExprSql(c clause.Expression) string {
originSQL := a.DB.Statement.SQL
a.DB.Statement.SQL = strings.Builder{}
c.Build(a.DB.Statement)
s := a.DB.Statement.SQL.String()
a.DB.Statement.SQL = originSQL
return s
}
func (a aesCallback) decryptClauses() {
for name, c := range a.DB.Statement.Clauses {
switch c.Name {
case "WHERE":
oldClause := c
c.Builder = func(c clause.Clause, builder clause.Builder) {
newSql, _ := a.ToDecryptedWhereConditions(a.DB.Statement.Table, a.getExprSql(oldClause), true)
_, _ = builder.WriteString(newSql)
}
a.DB.Statement.Clauses[name] = c
case "SELECT":
if a.hasJoins() {
// count(*)
oldClause := c
c.Builder = func(c clause.Clause, builder clause.Builder) {
newSql, _ := a.ToDecryptedSelectFields(a.DB.Statement.Table, true, a.getExprSql(oldClause))
_, _ = builder.WriteString("SELECT ")
_, _ = builder.WriteString(strings.Join(newSql, ","))
}
a.DB.Statement.Clauses[name] = c
}
default:
continue
}
}
}
type joinCtxKey int8
func (a aesCallback) initJoinsCtx() bool {
if a.DB.Statement.Context != nil {
has, ok := a.DB.Statement.Context.Value(joinCtxKey(0)).(bool)
if has {
return true
}
if ok {
return false
}
}
if len(a.DB.Statement.Joins) > 0 {
if a.DB.Statement.Context != nil {
a.DB.Statement.Context = context.WithValue(a.DB.Statement.Context, joinCtxKey(0), true)
} else {
a.DB.Statement.Context = context.WithValue(context.Background(), joinCtxKey(0), true)
}
return true
}
if a.DB.Statement.Context != nil {
a.DB.Statement.Context = context.WithValue(a.DB.Statement.Context, joinCtxKey(0), false)
} else {
a.DB.Statement.Context = context.WithValue(context.Background(), joinCtxKey(0), false)
}
return false
}
func (a aesCallback) hasJoins() bool {
has, _ := a.DB.Statement.Context.Value(joinCtxKey(0)).(bool)
return has
}