|
| 1 | +// Copyright 2023 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package planbuilder |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/dolthub/vitess/go/mysql" |
| 22 | + ast "github.com/dolthub/vitess/go/vt/sqlparser" |
| 23 | + |
| 24 | + "github.com/dolthub/go-mysql-server/sql" |
| 25 | + "github.com/dolthub/go-mysql-server/sql/mysql_db" |
| 26 | +) |
| 27 | + |
| 28 | +// TODO: doc |
| 29 | +var Authorization = func(b *Builder, auth ast.AuthInformation) { |
| 30 | + // TODO: expose necessary stuff from Builder to public |
| 31 | + ctx := b.ctx |
| 32 | + // TODO: database loading shouldn't be done for every call, this needs to be cached for each Parse call in some way |
| 33 | + db, err := b.cat.Database(ctx, "mysql") |
| 34 | + if err != nil { |
| 35 | + b.handleErr(err) |
| 36 | + } |
| 37 | + mysqlDb, ok := db.(*mysql_db.MySQLDb) |
| 38 | + if !ok { |
| 39 | + b.handleErr(fmt.Errorf("FOR TESTING: could not load the `mysql` database")) // TODO: Check if this is likely |
| 40 | + } |
| 41 | + if !mysqlDb.Enabled() { |
| 42 | + return |
| 43 | + } |
| 44 | + // TODO: cache that the user exists |
| 45 | + client := ctx.Session.Client() |
| 46 | + user := func() *mysql_db.User { |
| 47 | + rd := mysqlDb.Reader() |
| 48 | + defer rd.Close() |
| 49 | + return mysqlDb.GetUser(rd, client.User, client.Address, false) |
| 50 | + }() |
| 51 | + if user == nil { |
| 52 | + b.handleErr(mysql.NewSQLError(mysql.ERAccessDeniedError, mysql.SSAccessDeniedError, "Access denied for user '%s'", client.User)) |
| 53 | + } |
| 54 | + // TODO: cache for the call |
| 55 | + privSet := mysqlDb.UserActivePrivilegeSet(ctx) |
| 56 | + |
| 57 | + var privilegeTypes []sql.PrivilegeType |
| 58 | + switch auth.AuthType { |
| 59 | + case ast.AuthType_IGNORE: |
| 60 | + // This means that authorization is being handled elsewhere (such as a child or parent), and should be ignored here |
| 61 | + return |
| 62 | + case ast.AuthType_DELETE: |
| 63 | + privilegeTypes = []sql.PrivilegeType{sql.PrivilegeType_Delete} |
| 64 | + case ast.AuthType_INSERT: |
| 65 | + privilegeTypes = []sql.PrivilegeType{sql.PrivilegeType_Insert} |
| 66 | + case ast.AuthType_REPLACE: |
| 67 | + privilegeTypes = []sql.PrivilegeType{sql.PrivilegeType_Insert, sql.PrivilegeType_Delete} |
| 68 | + case ast.AuthType_SELECT: |
| 69 | + privilegeTypes = []sql.PrivilegeType{sql.PrivilegeType_Select} |
| 70 | + case ast.AuthType_UPDATE: |
| 71 | + privilegeTypes = []sql.PrivilegeType{sql.PrivilegeType_Update} |
| 72 | + default: |
| 73 | + b.handleErr(fmt.Errorf("FOR TESTING: default case hit for AuthType")) |
| 74 | + } |
| 75 | + |
| 76 | + hasPrivileges := true |
| 77 | + switch auth.TargetType { |
| 78 | + case ast.AuthTargetType_SingleTableIdentifier: |
| 79 | + dbName := auth.TargetNames[0] |
| 80 | + tableName := auth.TargetNames[1] |
| 81 | + if strings.EqualFold(dbName, "information_schema") { |
| 82 | + return |
| 83 | + } |
| 84 | + authCheckDatabaseTableNames(b, privSet, user.User, dbName, tableName) |
| 85 | + subject := sql.PrivilegeCheckSubject{ |
| 86 | + Database: authDatabaseName(ctx, dbName), |
| 87 | + Table: tableName, |
| 88 | + } |
| 89 | + hasPrivileges = mysqlDb.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(subject, privilegeTypes...)) |
| 90 | + case ast.AuthTargetType_MultipleTableIdentifiers: |
| 91 | + for i := 0; i < len(auth.TargetNames) && hasPrivileges; i += 2 { |
| 92 | + dbName := auth.TargetNames[i] |
| 93 | + tableName := auth.TargetNames[i+1] |
| 94 | + if strings.EqualFold(dbName, "information_schema") { |
| 95 | + continue |
| 96 | + } |
| 97 | + authCheckDatabaseTableNames(b, privSet, user.User, dbName, tableName) |
| 98 | + subject := sql.PrivilegeCheckSubject{ |
| 99 | + Database: authDatabaseName(ctx, dbName), |
| 100 | + Table: tableName, |
| 101 | + } |
| 102 | + hasPrivileges = hasPrivileges && mysqlDb.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(subject, privilegeTypes...)) |
| 103 | + } |
| 104 | + default: |
| 105 | + b.handleErr(fmt.Errorf("FOR TESTING: default case hit for TargetType")) |
| 106 | + } |
| 107 | + |
| 108 | + if !hasPrivileges { |
| 109 | + b.handleErr(sql.ErrPrivilegeCheckFailed.New(user.UserHostToString("'"))) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// authDatabaseName uses the current database from the context if a database is not specified, otherwise it returns the |
| 114 | +// given database name. |
| 115 | +func authDatabaseName(ctx *sql.Context, dbName string) string { |
| 116 | + if len(dbName) == 0 { |
| 117 | + return ctx.GetCurrentDatabase() |
| 118 | + } |
| 119 | + return dbName |
| 120 | +} |
| 121 | + |
| 122 | +// authCheckDatabaseTableNames errors if the user does not have access to the database or table in any capacity, |
| 123 | +// regardless of the command. |
| 124 | +func authCheckDatabaseTableNames(b *Builder, privSet mysql_db.PrivilegeSet, userName string, dbName string, tableName string) { |
| 125 | + dbSet := privSet.Database(dbName) |
| 126 | + // If there are no usable privileges for this database then the table is inaccessible. |
| 127 | + if privSet.Count() == 0 && !dbSet.HasPrivileges() { |
| 128 | + b.handleErr(sql.ErrDatabaseAccessDeniedForUser.New(userName, dbName)) |
| 129 | + } |
| 130 | + tblSet := dbSet.Table(tableName) |
| 131 | + // If the user has no global static privileges, database-level privileges, or table-relevant privileges then the |
| 132 | + // table is not accessible. |
| 133 | + if privSet.Count() == 0 && dbSet.Count() == 0 && !tblSet.HasPrivileges() { |
| 134 | + b.handleErr(sql.ErrTableAccessDeniedForUser.New(userName, tableName)) |
| 135 | + } |
| 136 | +} |
0 commit comments