Skip to content

Commit

Permalink
*: fix a bug for default_authentication_plugin (2) (#58723)
Browse files Browse the repository at this point in the history
ref #54138
  • Loading branch information
CbcWestwolf authored Jan 6, 2025
1 parent 88a2247 commit bee268d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ func (p *immutable) decodeUserTableRow(row chunk.Row, fs []*resolve.ResultField)
defaultAuthPlugin := ""
if p.globalVars != nil {
val, err := p.globalVars.GetGlobalSysVar(variable.DefaultAuthPlugin)
if err != nil {
if err == nil {
defaultAuthPlugin = val
}
}
Expand Down Expand Up @@ -1889,6 +1889,11 @@ func (p *MySQLPrivilege) getAllRoles(user, host string) []*auth.RoleIdentity {
return ret
}

// SetGlobalVarsAccessor is only used for test.
func (p *MySQLPrivilege) SetGlobalVarsAccessor(globalVars variable.GlobalVarAccessor) {
p.globalVars = globalVars
}

// Handle wraps MySQLPrivilege providing thread safe access.
type Handle struct {
sctx sqlexec.RestrictedSQLExecutor
Expand Down
11 changes: 11 additions & 0 deletions pkg/privilege/privileges/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package privileges_test

import (
"context"
"fmt"
"testing"
"time"

"github.com/pingcap/tidb/pkg/parser/auth"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/privilege/privileges"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/pingcap/tidb/pkg/util"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -66,6 +68,15 @@ func TestLoadUserTable(t *testing.T) {
require.Equal(t, false, user[6].PasswordExpired)
require.Equal(t, time.Date(2022, 10, 10, 12, 0, 0, 0, time.Local), user[6].PasswordLastChanged)
require.Equal(t, int64(-1), user[6].PasswordLifeTime)

// test switching default auth plugin
for _, plugin := range []string{mysql.AuthNativePassword, mysql.AuthCachingSha2Password, mysql.AuthTiDBSM3Password} {
p = privileges.MySQLPrivilege{}
p.SetGlobalVarsAccessor(se.GetSessionVars().GlobalVarsAccessor)
require.NoError(t, se.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(context.Background(), variable.DefaultAuthPlugin, plugin))
require.NoError(t, p.LoadUserTable(se.GetRestrictedSQLExecutor()))
require.Equal(t, plugin, p.User()[0].AuthPlugin)
}
}

func TestLoadGlobalPrivTable(t *testing.T) {
Expand Down

0 comments on commit bee268d

Please sign in to comment.