-
Notifications
You must be signed in to change notification settings - Fork 265
/
authenticate.go
9634 lines (8542 loc) · 283 KB
/
authenticate.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2021 Matrix Origin
//
// 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 frontend
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"math/bits"
"os"
"path"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/tidwall/btree"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/clusterservice"
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/common/mpool"
"github.com/matrixorigin/matrixone/pkg/config"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/defines"
"github.com/matrixorigin/matrixone/pkg/fileservice"
"github.com/matrixorigin/matrixone/pkg/logutil"
"github.com/matrixorigin/matrixone/pkg/pb/metadata"
"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/pb/query"
"github.com/matrixorigin/matrixone/pkg/queryservice"
"github.com/matrixorigin/matrixone/pkg/sql/parsers"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
plan2 "github.com/matrixorigin/matrixone/pkg/sql/plan"
"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
"github.com/matrixorigin/matrixone/pkg/util/metric/mometric"
"github.com/matrixorigin/matrixone/pkg/util/sysview"
"github.com/matrixorigin/matrixone/pkg/util/trace"
"github.com/matrixorigin/matrixone/pkg/util/trace/impl/motrace"
"github.com/matrixorigin/matrixone/pkg/vm/engine/disttae/route"
)
type TenantInfo struct {
mu sync.Mutex
Tenant string
User string
DefaultRole string
TenantID uint32
UserID uint32
DefaultRoleID uint32
// true: use secondary role all
// false: use secondary role none
useAllSecondaryRole bool
delimiter byte
version string
}
func (ti *TenantInfo) String() string {
ti.mu.Lock()
defer ti.mu.Unlock()
delimiter := ti.delimiter
if !strconv.IsPrint(rune(delimiter)) {
delimiter = ':'
}
return fmt.Sprintf("{account %s%c%s%c%s -- %d%c%d%c%d}",
ti.Tenant, delimiter, ti.User, delimiter, ti.DefaultRole,
ti.TenantID, delimiter, ti.UserID, delimiter, ti.DefaultRoleID)
}
func (ti *TenantInfo) GetTenant() string {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.getTenantUnsafe()
}
func (ti *TenantInfo) getTenantUnsafe() string {
return ti.Tenant
}
func (ti *TenantInfo) GetTenantID() uint32 {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.TenantID
}
func (ti *TenantInfo) SetTenantID(id uint32) {
ti.mu.Lock()
defer ti.mu.Unlock()
ti.TenantID = id
}
func (ti *TenantInfo) GetUser() string {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.User
}
func (ti *TenantInfo) SetUser(user string) {
ti.mu.Lock()
defer ti.mu.Unlock()
ti.User = user
}
func (ti *TenantInfo) GetUserID() uint32 {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.UserID
}
func (ti *TenantInfo) SetUserID(id uint32) {
ti.mu.Lock()
defer ti.mu.Unlock()
ti.UserID = id
}
func (ti *TenantInfo) GetDefaultRole() string {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.getDefaultRoleUnsafe()
}
func (ti *TenantInfo) getDefaultRoleUnsafe() string {
return ti.DefaultRole
}
func (ti *TenantInfo) SetDefaultRole(r string) {
ti.mu.Lock()
defer ti.mu.Unlock()
ti.DefaultRole = r
}
func (ti *TenantInfo) HasDefaultRole() bool {
ti.mu.Lock()
defer ti.mu.Unlock()
return len(ti.getDefaultRoleUnsafe()) != 0
}
func (ti *TenantInfo) GetDefaultRoleID() uint32 {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.DefaultRoleID
}
func (ti *TenantInfo) SetDefaultRoleID(id uint32) {
ti.mu.Lock()
defer ti.mu.Unlock()
ti.DefaultRoleID = id
}
func (ti *TenantInfo) IsSysTenant() bool {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.isSysTenantUnsafe()
}
func (ti *TenantInfo) isSysTenantUnsafe() bool {
if ti != nil {
return strings.ToLower(ti.getTenantUnsafe()) == GetDefaultTenant()
}
return false
}
func (ti *TenantInfo) IsDefaultRole() bool {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.getDefaultRoleUnsafe() == GetDefaultRole()
}
func (ti *TenantInfo) IsMoAdminRole() bool {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.isMoAdminRoleUnsafe()
}
func (ti *TenantInfo) isMoAdminRoleUnsafe() bool {
return ti.isSysTenantUnsafe() && strings.ToLower(ti.getDefaultRoleUnsafe()) == moAdminRoleName
}
func (ti *TenantInfo) IsAccountAdminRole() bool {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.isAccountAdminRoleUnsafe()
}
func (ti *TenantInfo) isAccountAdminRoleUnsafe() bool {
return !ti.isSysTenantUnsafe() && strings.ToLower(ti.getDefaultRoleUnsafe()) == accountAdminRoleName
}
func (ti *TenantInfo) IsAdminRole() bool {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.isMoAdminRoleUnsafe() || ti.isAccountAdminRoleUnsafe()
}
func (ti *TenantInfo) IsNameOfAdminRoles(name string) bool {
if ti == nil {
return false
}
ti.mu.Lock()
defer ti.mu.Unlock()
n := strings.ToLower(name)
if ti.isSysTenantUnsafe() {
return n == moAdminRoleName
} else {
return n == accountAdminRoleName
}
}
func (ti *TenantInfo) SetUseSecondaryRole(v bool) {
ti.mu.Lock()
defer ti.mu.Unlock()
ti.useAllSecondaryRole = v
}
func (ti *TenantInfo) GetUseSecondaryRole() bool {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.useAllSecondaryRole
}
func (ti *TenantInfo) GetVersion() string {
ti.mu.Lock()
defer ti.mu.Unlock()
return ti.version
}
func (ti *TenantInfo) SetVersion(version string) {
ti.mu.Lock()
defer ti.mu.Unlock()
ti.version = version
}
func GetDefaultTenant() string {
return sysAccountName
}
func GetSysTenantId() uint32 {
return sysAccountID
}
func GetUserRoot() string {
return rootName
}
func GetUserRootId() uint32 {
return rootID
}
func GetDefaultRole() string {
return moAdminRoleName
}
func GetDefaultRoleId() uint32 {
return moAdminRoleID
}
func GetAccountAdminRole() string {
return accountAdminRoleName
}
func GetAccountAdminRoleId() uint32 {
return accountAdminRoleID
}
func GetAdminUserId() uint32 {
return dumpID + 1
}
func GetBackgroundTenant() *TenantInfo {
return &TenantInfo{
Tenant: GetDefaultTenant(),
User: GetUserRoot(),
DefaultRole: GetDefaultRole(),
TenantID: GetSysTenantId(),
UserID: GetUserRootId(),
DefaultRoleID: GetDefaultRoleId(),
}
}
func isCaseInsensitiveEqual(n string, role string) bool {
return strings.ToLower(n) == role
}
func isPublicRole(n string) bool {
return isCaseInsensitiveEqual(n, publicRoleName)
}
func isPredefinedRole(r string) bool {
n := strings.ToLower(r)
return n == publicRoleName || n == accountAdminRoleName || n == moAdminRoleName
}
func isSysTenant(n string) bool {
return isCaseInsensitiveEqual(n, sysAccountName)
}
// splitUserInput splits user input into account info
func splitUserInput(ctx context.Context, userInput string, delimiter byte) (*TenantInfo, error) {
p := strings.IndexByte(userInput, delimiter)
if p == -1 {
return &TenantInfo{
Tenant: GetDefaultTenant(),
User: userInput,
delimiter: delimiter,
}, nil
} else {
tenant := userInput[:p]
if len(tenant) == 0 {
return &TenantInfo{}, moerr.NewInternalError(ctx, "invalid tenant name '%s'", tenant)
}
userRole := userInput[p+1:]
p2 := strings.IndexByte(userRole, delimiter)
if p2 == -1 {
//tenant:user
user := userRole
if len(user) == 0 {
return &TenantInfo{}, moerr.NewInternalError(ctx, "invalid user name '%s'", user)
}
return &TenantInfo{
Tenant: tenant,
User: user,
delimiter: delimiter,
}, nil
} else {
user := userRole[:p2]
if len(user) == 0 {
return &TenantInfo{}, moerr.NewInternalError(ctx, "invalid user name '%s'", user)
}
role := userRole[p2+1:]
if len(role) == 0 {
return &TenantInfo{}, moerr.NewInternalError(ctx, "invalid role name '%s'", role)
}
return &TenantInfo{
Tenant: tenant,
User: user,
DefaultRole: role,
delimiter: delimiter,
}, nil
}
}
}
//GetTenantInfo extract tenant info from the input of the user.
/**
The format of the user
1. tenant:user:role
2. tenant:user
3. user
a new format:
1. tenant#user#role
2. tenant#user
*/
func GetTenantInfo(ctx context.Context, userInput string) (*TenantInfo, error) {
userInput = getUserPart(userInput)
if strings.IndexByte(userInput, ':') != -1 {
return splitUserInput(ctx, userInput, ':')
} else if strings.IndexByte(userInput, '#') != -1 {
return splitUserInput(ctx, userInput, '#')
} else if strings.Contains(userInput, "%3A") {
newUserInput := strings.ReplaceAll(userInput, "%3A", ":")
return splitUserInput(ctx, newUserInput, ':')
}
return splitUserInput(ctx, userInput, ':')
}
// getUserPart gets the username part from the full string.
// The full string could contain CN label information which
// is used by proxy module.
func getUserPart(user string) string {
if pos := strings.IndexByte(user, '?'); pos != -1 {
return user[:pos]
}
return user
}
// getLabelPart gets the label part from the full string.
// The full string could contain CN label information which
// is used by proxy module.
func getLabelPart(user string) string {
parts := strings.Split(user, "?")
if len(parts) > 1 {
return parts[1]
}
return ""
}
// ParseLabel parses the label string. The labels are separated by
// ",", key and value are separated by "=".
func ParseLabel(labelStr string) (map[string]string, error) {
labelMap := make(map[string]string)
if len(labelStr) == 0 {
return labelMap, nil
}
const delimiter1 = ","
const delimiter2 = "="
kvs := strings.Split(labelStr, delimiter1)
for _, label := range kvs {
parts := strings.Split(label, delimiter2)
if len(parts) == 2 && len(parts[0]) != 0 && len(parts[1]) != 0 {
labelMap[parts[0]] = parts[1]
} else {
return nil, moerr.NewInternalErrorNoCtx("invalid label format: should be like 'a=b'")
}
}
return labelMap, nil
}
// initUser for initialization or something special
type initUser struct {
account *TenantInfo
password []byte
}
var (
specialUsers struct {
sync.RWMutex
users map[string]*initUser
}
)
func setSpecialUser(userName string, user *initUser) {
specialUsers.Lock()
if specialUsers.users == nil {
specialUsers.users = make(map[string]*initUser)
}
specialUsers.users[userName] = user
specialUsers.Unlock()
}
// SetSpecialUser saves the user for initialization
// !!!NOTE: userName must not contain Colon ':'
func SetSpecialUser(username string, password []byte) {
acc := &TenantInfo{
Tenant: sysAccountName,
User: username,
DefaultRole: moAdminRoleName,
TenantID: sysAccountID,
UserID: math.MaxUint32,
DefaultRoleID: moAdminRoleID,
}
user := &initUser{
account: acc,
password: password,
}
setSpecialUser(username, user)
}
// isSpecialUser checks the user is the one for initialization
func isSpecialUser(userName string) (bool, []byte, *TenantInfo) {
specialUsers.RLock()
defer specialUsers.RUnlock()
if user, ok := specialUsers.users[userName]; ok {
return true, user.password, user.account
}
return false, nil, nil
}
const (
// createMoUserIndex = 0
// createMoAccountIndex = 1
// createMoRoleIndex = 2
// createMoUserGrantIndex = 3
// createMoRoleGrantIndex = 4
// createMoRolePrivIndex = 5
)
const (
//tenant
sysAccountID = 0
sysAccountName = "sys"
sysAccountStatus = "open"
sysAccountComments = "system account"
//role
moAdminRoleID = 0
moAdminRoleName = "moadmin"
// moAdminRoleComment = "super admin role"
publicRoleID = 1
publicRoleName = "public"
// publicRoleComment = "public role"
accountAdminRoleID = 2
accountAdminRoleName = "accountadmin"
// accountAdminRoleComment = "account admin role"
//user
userStatusLock = "lock"
userStatusUnlock = "unlock"
defaultPasswordEnv = "DEFAULT_PASSWORD"
rootID = 0
rootHost = "localhost"
rootName = "root"
rootPassword = "111"
rootStatus = userStatusUnlock
rootExpiredTime = "NULL"
rootLoginType = "PASSWORD"
rootCreatorID = rootID
rootOwnerRoleID = moAdminRoleID
rootDefaultRoleID = moAdminRoleID
dumpID = 1
dumpHost = "localhost"
dumpName = "dump"
//dumpPassword = "111"
dumpStatus = userStatusUnlock
dumpExpiredTime = "NULL"
dumpLoginType = "PASSWORD"
dumpCreatorID = rootID
dumpOwnerRoleID = moAdminRoleID
dumpDefaultRoleID = moAdminRoleID
moCatalog = "mo_catalog"
SaveQueryResult = "save_query_result"
QueryResultMaxsize = "query_result_maxsize"
QueryResultTimeout = "query_result_timeout"
LowerCaseTableNames = "lower_case_table_names"
)
type objectType int
const (
objectTypeDatabase objectType = iota
objectTypeTable
objectTypeFunction
objectTypeAccount
objectTypeNone
objectIDAll = 0 //denotes all objects in the object type
)
func (ot objectType) String() string {
switch ot {
case objectTypeDatabase:
return "database"
case objectTypeTable:
return "table"
case objectTypeFunction:
return "function"
case objectTypeAccount:
return "account"
case objectTypeNone:
return "none"
}
panic("unsupported object type")
}
type privilegeLevelType int
const (
//*
privilegeLevelStar privilegeLevelType = iota
//*.*
privilegeLevelStarStar
//db_name
privilegeLevelDatabase
//db_name.*
privilegeLevelDatabaseStar
//db_name.tbl_name
privilegeLevelDatabaseTable
//tbl_name
privilegeLevelTable
//db_name.routine_name
privilegeLevelRoutine
//
privilegeLevelEnd
)
func (plt privilegeLevelType) String() string {
switch plt {
case privilegeLevelStar:
return "*"
case privilegeLevelStarStar:
return "*.*"
case privilegeLevelDatabase:
return "d"
case privilegeLevelDatabaseStar:
return "d.*"
case privilegeLevelDatabaseTable:
return "d.t"
case privilegeLevelTable:
return "t"
case privilegeLevelRoutine:
return "r"
}
panic(fmt.Sprintf("no such privilege level type %d", plt))
}
type PrivilegeType int
const (
PrivilegeTypeCreateAccount PrivilegeType = iota
PrivilegeTypeDropAccount
PrivilegeTypeAlterAccount
PrivilegeTypeCreateUser
PrivilegeTypeDropUser
PrivilegeTypeAlterUser
PrivilegeTypeCreateRole
PrivilegeTypeDropRole
PrivilegeTypeAlterRole
PrivilegeTypeCreateDatabase
PrivilegeTypeDropDatabase
PrivilegeTypeShowDatabases
PrivilegeTypeConnect
PrivilegeTypeManageGrants
PrivilegeTypeAccountAll
PrivilegeTypeAccountOwnership
PrivilegeTypeUserOwnership
PrivilegeTypeRoleOwnership
PrivilegeTypeShowTables
PrivilegeTypeCreateObject //includes: table, view, stream, sequence, function, dblink,etc
PrivilegeTypeCreateTable
PrivilegeTypeCreateView
PrivilegeTypeDropObject
PrivilegeTypeDropTable
PrivilegeTypeDropView
PrivilegeTypeAlterObject
PrivilegeTypeAlterTable
PrivilegeTypeAlterView
PrivilegeTypeDatabaseAll
PrivilegeTypeDatabaseOwnership
PrivilegeTypeSelect
PrivilegeTypeInsert
PrivilegeTypeUpdate
PrivilegeTypeTruncate
PrivilegeTypeDelete
PrivilegeTypeReference
PrivilegeTypeIndex //include create/alter/drop index
PrivilegeTypeTableAll
PrivilegeTypeTableOwnership
PrivilegeTypeExecute
PrivilegeTypeCanGrantRoleToOthersInCreateUser // used in checking the privilege of CreateUser with the default role
PrivilegeTypeValues
PrivilegeTypeUpgradeAccount
)
type PrivilegeScope uint8
const (
PrivilegeScopeSys PrivilegeScope = 1
PrivilegeScopeAccount PrivilegeScope = 2
PrivilegeScopeUser PrivilegeScope = 4
PrivilegeScopeRole PrivilegeScope = 8
PrivilegeScopeDatabase PrivilegeScope = 16
PrivilegeScopeTable PrivilegeScope = 32
PrivilegeScopeRoutine PrivilegeScope = 64
)
func (ps PrivilegeScope) String() string {
sb := strings.Builder{}
first := true
for i := 0; i < 8; i++ {
var s string
switch ps & (1 << i) {
case PrivilegeScopeSys:
s = "sys"
case PrivilegeScopeAccount:
s = "account"
case PrivilegeScopeUser:
s = "user"
case PrivilegeScopeRole:
s = "role"
case PrivilegeScopeDatabase:
s = "database"
case PrivilegeScopeTable:
s = "table"
case PrivilegeScopeRoutine:
s = "routine"
default:
s = ""
}
if len(s) != 0 {
if !first {
sb.WriteString(",")
} else {
first = false
}
sb.WriteString(s)
}
}
return sb.String()
}
func (pt PrivilegeType) String() string {
switch pt {
case PrivilegeTypeCreateAccount:
return "create account"
case PrivilegeTypeDropAccount:
return "drop account"
case PrivilegeTypeAlterAccount:
return "alter account"
case PrivilegeTypeUpgradeAccount:
return "upgrade account"
case PrivilegeTypeCreateUser:
return "create user"
case PrivilegeTypeDropUser:
return "drop user"
case PrivilegeTypeAlterUser:
return "alter user"
case PrivilegeTypeCreateRole:
return "create role"
case PrivilegeTypeDropRole:
return "drop role"
case PrivilegeTypeAlterRole:
return "alter role"
case PrivilegeTypeCreateDatabase:
return "create database"
case PrivilegeTypeDropDatabase:
return "drop database"
case PrivilegeTypeShowDatabases:
return "show databases"
case PrivilegeTypeConnect:
return "connect"
case PrivilegeTypeManageGrants:
return "manage grants"
case PrivilegeTypeAccountAll:
return "account all"
case PrivilegeTypeAccountOwnership:
return "account ownership"
case PrivilegeTypeUserOwnership:
return "user ownership"
case PrivilegeTypeRoleOwnership:
return "role ownership"
case PrivilegeTypeShowTables:
return "show tables"
case PrivilegeTypeCreateObject:
return "create object"
case PrivilegeTypeCreateTable:
return "create table"
case PrivilegeTypeCreateView:
return "create view"
case PrivilegeTypeDropObject:
return "drop object"
case PrivilegeTypeDropTable:
return "drop table"
case PrivilegeTypeDropView:
return "drop view"
case PrivilegeTypeAlterObject:
return "alter object"
case PrivilegeTypeAlterTable:
return "alter table"
case PrivilegeTypeAlterView:
return "alter view"
case PrivilegeTypeDatabaseAll:
return "database all"
case PrivilegeTypeDatabaseOwnership:
return "database ownership"
case PrivilegeTypeSelect:
return "select"
case PrivilegeTypeInsert:
return "insert"
case PrivilegeTypeUpdate:
return "update"
case PrivilegeTypeTruncate:
return "truncate"
case PrivilegeTypeDelete:
return "delete"
case PrivilegeTypeReference:
return "reference"
case PrivilegeTypeIndex:
return "index"
case PrivilegeTypeTableAll:
return "table all"
case PrivilegeTypeTableOwnership:
return "table ownership"
case PrivilegeTypeExecute:
return "execute"
case PrivilegeTypeValues:
return "values"
}
panic(fmt.Sprintf("no such privilege type %d", pt))
}
func (pt PrivilegeType) Scope() PrivilegeScope {
switch pt {
case PrivilegeTypeCreateAccount:
return PrivilegeScopeSys
case PrivilegeTypeDropAccount:
return PrivilegeScopeSys
case PrivilegeTypeAlterAccount:
return PrivilegeScopeSys
case PrivilegeTypeUpgradeAccount:
return PrivilegeScopeSys
case PrivilegeTypeCreateUser:
return PrivilegeScopeAccount
case PrivilegeTypeDropUser:
return PrivilegeScopeAccount
case PrivilegeTypeAlterUser:
return PrivilegeScopeAccount
case PrivilegeTypeCreateRole:
return PrivilegeScopeAccount
case PrivilegeTypeDropRole:
return PrivilegeScopeAccount
case PrivilegeTypeAlterRole:
return PrivilegeScopeAccount
case PrivilegeTypeCreateDatabase:
return PrivilegeScopeAccount
case PrivilegeTypeDropDatabase:
return PrivilegeScopeAccount
case PrivilegeTypeShowDatabases:
return PrivilegeScopeAccount
case PrivilegeTypeConnect:
return PrivilegeScopeAccount
case PrivilegeTypeManageGrants:
return PrivilegeScopeAccount
case PrivilegeTypeAccountAll:
return PrivilegeScopeAccount
case PrivilegeTypeAccountOwnership:
return PrivilegeScopeAccount
case PrivilegeTypeUserOwnership:
return PrivilegeScopeUser
case PrivilegeTypeRoleOwnership:
return PrivilegeScopeRole
case PrivilegeTypeShowTables:
return PrivilegeScopeDatabase
case PrivilegeTypeCreateObject, PrivilegeTypeCreateTable, PrivilegeTypeCreateView:
return PrivilegeScopeDatabase
case PrivilegeTypeDropObject, PrivilegeTypeDropTable, PrivilegeTypeDropView:
return PrivilegeScopeDatabase
case PrivilegeTypeAlterObject, PrivilegeTypeAlterTable, PrivilegeTypeAlterView:
return PrivilegeScopeDatabase
case PrivilegeTypeDatabaseAll:
return PrivilegeScopeDatabase
case PrivilegeTypeDatabaseOwnership:
return PrivilegeScopeDatabase
case PrivilegeTypeSelect:
return PrivilegeScopeTable
case PrivilegeTypeInsert:
return PrivilegeScopeTable
case PrivilegeTypeUpdate:
return PrivilegeScopeTable
case PrivilegeTypeTruncate:
return PrivilegeScopeTable
case PrivilegeTypeDelete:
return PrivilegeScopeTable
case PrivilegeTypeReference:
return PrivilegeScopeTable
case PrivilegeTypeIndex:
return PrivilegeScopeTable
case PrivilegeTypeTableAll:
return PrivilegeScopeTable
case PrivilegeTypeTableOwnership:
return PrivilegeScopeTable
case PrivilegeTypeExecute:
return PrivilegeScopeTable
case PrivilegeTypeValues:
return PrivilegeScopeTable
}
panic(fmt.Sprintf("no such privilege type %d", pt))
}
var (
sysWantedDatabases = map[string]int8{
"mo_catalog": 0,
"information_schema": 0,
"system": 0,
"system_metrics": 0,
}
sysDatabases = map[string]int8{
"mo_catalog": 0,
"information_schema": 0,
"system": 0,
"system_metrics": 0,
"mysql": 0,
"mo_task": 0,
}
sysWantedTables = map[string]int8{
"mo_user": 0,
"mo_account": 0,
"mo_role": 0,
"mo_user_grant": 0,
"mo_role_grant": 0,
"mo_role_privs": 0,
"mo_user_defined_function": 0,
"mo_stored_procedure": 0,
"mo_mysql_compatibility_mode": 0,
"mo_stages": 0,
catalog.MOAutoIncrTable: 0,
"mo_sessions": 0,
"mo_configurations": 0,
"mo_locks": 0,
"mo_variables": 0,
"mo_transactions": 0,
"mo_cache": 0,
"mo_snapshots": 0,
}
configInitVariables = map[string]int8{
"save_query_result": 0,
"query_result_maxsize": 0,
"query_result_timeout": 0,
"lower_case_table_names": 0,
}
sysAccountTables = map[string]struct{}{
catalog.MOVersionTable: {},
catalog.MOUpgradeTable: {},
catalog.MOUpgradeTenantTable: {},
}
//predefined tables of the database mo_catalog in every account
predefinedTables = map[string]int8{
"mo_database": 0,
"mo_tables": 0,
"mo_columns": 0,
"mo_account": 0,
"mo_user": 0,
"mo_role": 0,
"mo_user_grant": 0,
"mo_role_grant": 0,
"mo_role_privs": 0,
"mo_user_defined_function": 0,
"mo_stored_procedure": 0,
"mo_mysql_compatibility_mode": 0,
catalog.MOAutoIncrTable: 0,
"mo_indexes": 0,
"mo_table_partitions": 0,
"mo_pubs": 0,
"mo_stages": 0,
"mo_sessions": 0,
"mo_configurations": 0,
"mo_locks": 0,
"mo_variables": 0,
"mo_transactions": 0,
"mo_cache": 0,
"mo_foreign_keys": 0,
"mo_snapshots": 0,
}
createDbInformationSchemaSql = "create database information_schema;"
createAutoTableSql = MoCatalogMoAutoIncrTableDDL
// mo_indexes is a data dictionary table, must be created first when creating tenants, and last when deleting tenants
// mo_indexes table does not have `auto_increment` column,
createMoIndexesSql = MoCatalogMoIndexesDDL
createMoForeignKeysSql = MoCatalogMoForeignKeysDDL
createMoTablePartitionsSql = MoCatalogMoTablePartitionsDDL
//the sqls creating many tables for the tenant.
//Wrap them in a transaction
createSqls = []string{
MoCatalogMoUserDDL,
MoCatalogMoAccountDDL,
MoCatalogMoRoleDDL,
MoCatalogMoUserGrantDDL,
MoCatalogMoRoleGrantDDL,
MoCatalogMoRolePrivsDDL,
MoCatalogMoUserDefinedFunctionDDL,
MoCatalogMoMysqlCompatibilityModeDDL,
MoCatalogMoSnapshotsDDL,
MoCatalogMoPubsDDL,
MoCatalogMoStoredProcedureDDL,
MoCatalogMoStagesDDL,
MoCatalogMoSessionsDDL,
MoCatalogMoConfigurationsDDL,
MoCatalogMoLocksDDL,
MoCatalogMoVariablesDDL,
MoCatalogMoTransactionsDDL,
MoCatalogMoCacheDDL,
}
//drop tables for the tenant
dropSqls = []string{
`drop table if exists mo_catalog.mo_user;`,
`drop table if exists mo_catalog.mo_role;`,
`drop table if exists mo_catalog.mo_user_grant;`,
`drop table if exists mo_catalog.mo_role_grant;`,
`drop table if exists mo_catalog.mo_role_privs;`,
`drop table if exists mo_catalog.mo_user_defined_function;`,
`drop table if exists mo_catalog.mo_stored_procedure;`,
`drop table if exists mo_catalog.mo_stages;`,
`drop view if exists mo_catalog.mo_sessions;`,
`drop view if exists mo_catalog.mo_configurations;`,
`drop view if exists mo_catalog.mo_locks;`,