forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
377 lines (361 loc) · 12.8 KB
/
user.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
// Copyright 2021-2022 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mysql_db
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/in_mem_table"
)
// User represents a user from the user Grant Table.
type User struct {
User string
Host string
PrivilegeSet PrivilegeSet
Plugin string
Password string
PasswordLastChanged time.Time
Locked bool
Attributes *string
Identity string
IsSuperUser bool
//TODO: add the remaining fields
// IsRole is an additional field that states whether the User represents a role or user. In MySQL this must be a
// hidden column, therefore it's represented here as an additional field.
IsRole bool
}
var _ in_mem_table.Entry = (*User)(nil)
// NewFromRow implements the interface in_mem_table.Entry.
func (u *User) NewFromRow(ctx *sql.Context, row sql.Row) (in_mem_table.Entry, error) {
if err := userTblSchema.CheckRow(row); err != nil {
return nil, err
}
//TODO: once the remaining fields are added, fill those in as well
var attributes *string
passwordLastChanged := time.Now().UTC()
if val, ok := row[userTblColIndex_User_attributes].(string); ok {
attributes = &val
}
if val, ok := row[userTblColIndex_password_last_changed].(time.Time); ok {
passwordLastChanged = val
}
return &User{
User: row[userTblColIndex_User].(string),
Host: row[userTblColIndex_Host].(string),
PrivilegeSet: u.rowToPrivSet(ctx, row),
Plugin: row[userTblColIndex_plugin].(string),
Password: row[userTblColIndex_authentication_string].(string),
PasswordLastChanged: passwordLastChanged,
Locked: row[userTblColIndex_account_locked].(uint16) == 2,
Attributes: attributes,
Identity: row[userTblColIndex_identity].(string),
IsRole: false,
}, nil
}
// UpdateFromRow implements the interface in_mem_table.Entry.
func (u *User) UpdateFromRow(ctx *sql.Context, row sql.Row) (in_mem_table.Entry, error) {
updatedEntry, err := u.NewFromRow(ctx, row)
if err != nil {
return nil, err
}
updatedEntry.(*User).IsRole = u.IsRole
return updatedEntry, nil
}
// ToRow implements the interface in_mem_table.Entry.
func (u *User) ToRow(ctx *sql.Context) sql.Row {
row := make(sql.Row, len(userTblSchema))
var err error
for i, col := range userTblSchema {
row[i], err = col.Default.Eval(ctx, nil)
if err != nil {
panic(err) // Should never happen, schema is static
}
}
//TODO: once the remaining fields are added, fill those in as well
row[userTblColIndex_User] = u.User
row[userTblColIndex_Host] = u.Host
row[userTblColIndex_plugin] = u.Plugin
row[userTblColIndex_authentication_string] = u.Password
row[userTblColIndex_password_last_changed] = u.PasswordLastChanged
row[userTblColIndex_identity] = u.Identity
if u.Locked {
row[userTblColIndex_account_locked] = uint16(2)
}
if u.Attributes != nil {
row[userTblColIndex_User_attributes] = *u.Attributes
}
u.privSetToRow(ctx, row)
return row
}
// Equals implements the interface in_mem_table.Entry.
func (u *User) Equals(ctx *sql.Context, otherEntry in_mem_table.Entry) bool {
otherUser, ok := otherEntry.(*User)
if !ok {
return false
}
// IsRole is not tested for equality, as it is additional information
//TODO: once the remaining fields are added, fill those in as well
if u.User != otherUser.User ||
u.Host != otherUser.Host ||
u.Plugin != otherUser.Plugin ||
u.Password != otherUser.Password ||
u.Identity != otherUser.Identity ||
!u.PasswordLastChanged.Equal(otherUser.PasswordLastChanged) ||
u.Locked != otherUser.Locked ||
!u.PrivilegeSet.Equals(otherUser.PrivilegeSet) ||
u.Attributes == nil && otherUser.Attributes != nil ||
u.Attributes != nil && otherUser.Attributes == nil ||
(u.Attributes != nil && *u.Attributes != *otherUser.Attributes) {
return false
}
return true
}
// Copy implements the interface in_mem_table.Entry.
func (u *User) Copy(ctx *sql.Context) in_mem_table.Entry {
uu := *u
uu.PrivilegeSet = NewPrivilegeSet()
uu.PrivilegeSet.UnionWith(u.PrivilegeSet)
return &uu
}
// FromJson implements the interface in_mem_table.Entry.
func (u User) FromJson(ctx *sql.Context, jsonStr string) (in_mem_table.Entry, error) {
newUser := &User{}
if err := json.Unmarshal([]byte(jsonStr), newUser); err != nil {
return nil, err
}
return newUser, nil
}
// ToJson implements the interface in_mem_table.Entry.
func (u *User) ToJson(ctx *sql.Context) (string, error) {
jsonData, err := json.Marshal(*u)
if err != nil {
return "", err
}
return string(jsonData), nil
}
// UserHostToString returns the user and host as a formatted string using the quotes given. Using the default root
// account with the backtick as the quote, root@localhost would become `root`@`localhost`. Different quotes are used
// in different places in MySQL. In addition, if the quote is used in a section as part of the name, it is escaped by
// doubling the quote (which also mimics MySQL behavior).
func (u User) UserHostToString(quote string) string {
replacement := quote + quote
user := strings.ReplaceAll(u.User, quote, replacement)
host := strings.ReplaceAll(u.Host, quote, replacement)
return fmt.Sprintf("%s%s%s@%s%s%s", quote, user, quote, quote, host, quote)
}
// rowToPrivSet returns a set of privileges for the given row.
func (u *User) rowToPrivSet(ctx *sql.Context, row sql.Row) PrivilegeSet {
privSet := NewPrivilegeSet()
for i, val := range row {
switch i {
case userTblColIndex_Select_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Select)
}
case userTblColIndex_Insert_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Insert)
}
case userTblColIndex_Update_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Update)
}
case userTblColIndex_Delete_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Delete)
}
case userTblColIndex_Create_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Create)
}
case userTblColIndex_Drop_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Drop)
}
case userTblColIndex_Reload_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Reload)
}
case userTblColIndex_Shutdown_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Shutdown)
}
case userTblColIndex_Process_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Process)
}
case userTblColIndex_File_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_File)
}
case userTblColIndex_Grant_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_GrantOption)
}
case userTblColIndex_References_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_References)
}
case userTblColIndex_Index_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Index)
}
case userTblColIndex_Alter_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Alter)
}
case userTblColIndex_Show_db_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_ShowDB)
}
case userTblColIndex_Super_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Super)
}
case userTblColIndex_Create_tmp_table_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_CreateTempTable)
}
case userTblColIndex_Lock_tables_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_LockTables)
}
case userTblColIndex_Execute_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Execute)
}
case userTblColIndex_Repl_slave_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_ReplicationSlave)
}
case userTblColIndex_Repl_client_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_ReplicationClient)
}
case userTblColIndex_Create_view_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_CreateView)
}
case userTblColIndex_Show_view_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_ShowView)
}
case userTblColIndex_Create_routine_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_CreateRoutine)
}
case userTblColIndex_Alter_routine_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_AlterRoutine)
}
case userTblColIndex_Create_user_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_CreateUser)
}
case userTblColIndex_Event_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Event)
}
case userTblColIndex_Trigger_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_Trigger)
}
case userTblColIndex_Create_tablespace_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_CreateTablespace)
}
case userTblColIndex_Create_role_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_CreateRole)
}
case userTblColIndex_Drop_role_priv:
if val.(uint16) == 2 {
privSet.AddGlobalStatic(sql.PrivilegeType_DropRole)
}
}
}
return privSet
}
// privSetToRow applies the this User's set of privileges to the given row. Only sets privileges that exist to "Y",
// therefore any privileges that do not exist will have their default values.
func (u *User) privSetToRow(ctx *sql.Context, row sql.Row) {
for _, priv := range u.PrivilegeSet.ToSlice() {
switch priv {
case sql.PrivilegeType_Select:
row[userTblColIndex_Select_priv] = uint16(2)
case sql.PrivilegeType_Insert:
row[userTblColIndex_Insert_priv] = uint16(2)
case sql.PrivilegeType_Update:
row[userTblColIndex_Update_priv] = uint16(2)
case sql.PrivilegeType_Delete:
row[userTblColIndex_Delete_priv] = uint16(2)
case sql.PrivilegeType_Create:
row[userTblColIndex_Create_priv] = uint16(2)
case sql.PrivilegeType_Drop:
row[userTblColIndex_Drop_priv] = uint16(2)
case sql.PrivilegeType_Reload:
row[userTblColIndex_Reload_priv] = uint16(2)
case sql.PrivilegeType_Shutdown:
row[userTblColIndex_Shutdown_priv] = uint16(2)
case sql.PrivilegeType_Process:
row[userTblColIndex_Process_priv] = uint16(2)
case sql.PrivilegeType_File:
row[userTblColIndex_File_priv] = uint16(2)
case sql.PrivilegeType_GrantOption:
row[userTblColIndex_Grant_priv] = uint16(2)
case sql.PrivilegeType_References:
row[userTblColIndex_References_priv] = uint16(2)
case sql.PrivilegeType_Index:
row[userTblColIndex_Index_priv] = uint16(2)
case sql.PrivilegeType_Alter:
row[userTblColIndex_Alter_priv] = uint16(2)
case sql.PrivilegeType_ShowDB:
row[userTblColIndex_Show_db_priv] = uint16(2)
case sql.PrivilegeType_Super:
row[userTblColIndex_Super_priv] = uint16(2)
case sql.PrivilegeType_CreateTempTable:
row[userTblColIndex_Create_tmp_table_priv] = uint16(2)
case sql.PrivilegeType_LockTables:
row[userTblColIndex_Lock_tables_priv] = uint16(2)
case sql.PrivilegeType_Execute:
row[userTblColIndex_Execute_priv] = uint16(2)
case sql.PrivilegeType_ReplicationSlave:
row[userTblColIndex_Repl_slave_priv] = uint16(2)
case sql.PrivilegeType_ReplicationClient:
row[userTblColIndex_Repl_client_priv] = uint16(2)
case sql.PrivilegeType_CreateView:
row[userTblColIndex_Create_view_priv] = uint16(2)
case sql.PrivilegeType_ShowView:
row[userTblColIndex_Show_view_priv] = uint16(2)
case sql.PrivilegeType_CreateRoutine:
row[userTblColIndex_Create_routine_priv] = uint16(2)
case sql.PrivilegeType_AlterRoutine:
row[userTblColIndex_Alter_routine_priv] = uint16(2)
case sql.PrivilegeType_CreateUser:
row[userTblColIndex_Create_user_priv] = uint16(2)
case sql.PrivilegeType_Event:
row[userTblColIndex_Event_priv] = uint16(2)
case sql.PrivilegeType_Trigger:
row[userTblColIndex_Trigger_priv] = uint16(2)
case sql.PrivilegeType_CreateTablespace:
row[userTblColIndex_Create_tablespace_priv] = uint16(2)
case sql.PrivilegeType_CreateRole:
row[userTblColIndex_Create_role_priv] = uint16(2)
case sql.PrivilegeType_DropRole:
row[userTblColIndex_Drop_role_priv] = uint16(2)
}
}
}