Skip to content

Commit

Permalink
executor: populate correct user in set pwd for error msg (#54040)
Browse files Browse the repository at this point in the history
close #54039
  • Loading branch information
yzhan1 authored Jul 17, 2024
1 parent c4fd1b2 commit a48a4a2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2488,7 +2488,8 @@ func (e *SimpleExec) executeSetPwd(ctx context.Context, s *ast.SetPwdStmt) error
checker := privilege.GetPrivilegeManager(e.Ctx())
activeRoles := e.Ctx().GetSessionVars().ActiveRoles
if checker != nil && !checker.RequestVerification(activeRoles, "", "", "", mysql.SuperPriv) {
return exeerrors.ErrDBaccessDenied.GenWithStackByArgs(u, h, "mysql")
currUser := e.Ctx().GetSessionVars().User
return exeerrors.ErrDBaccessDenied.GenWithStackByArgs(currUser.Username, currUser.Hostname, "mysql")
}
}
exists, err := userExistsInternal(ctx, sqlExecutor, u, h)
Expand Down
13 changes: 13 additions & 0 deletions pkg/executor/test/simpletest/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,19 @@ func TestSetPwd(t *testing.T) {
tk.MustExec(setPwdSQL)
result = tk.MustQuery(`SELECT authentication_string FROM mysql.User WHERE User="testpwd" and Host="localhost"`)
result.Check(testkit.Rows(auth.EncodePassword("pwd")))

// Test running SET PASSWORD FOR without sufficient privileges.
// Create user u1 with super privilege.
tk.MustExec("create user 'u1'")
tk.MustExec("grant super on *.* to u1")
// Create user u2 with create user privilege.
tk.MustExec("create user 'u2'")
tk.MustExec("grant create user on *.* to u2")

tk2 := testkit.NewTestKit(t, store)
require.NoError(t, tk2.Session().Auth(&auth.UserIdentity{Username: "u2", Hostname: "localhost"}, nil, nil, nil))
// Should have the correct error message saying u2 does not have enough privileges.
tk2.MustContainErrMsg("set password for 'u1'='randompassword'", "[executor:1044]Access denied for user 'u2'")
}

func TestFlushPrivilegesPanic(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tests/integrationtest/r/privilege/privileges.result
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ GRANT ALL ON *.* TO 'superuser';
SET PASSWORD for 'nobodyuser' = 'newpassword';
SET PASSWORD for 'nobodyuser' = '';
SET PASSWORD for 'superuser' = 'newpassword';
Error 1044 (42000): Access denied for user 'superuser'@'%' to database 'mysql'
Error 1044 (42000): Access denied for user 'nobodyuser'@'127.0.0.1' to database 'mysql'
CREATE ROLE tsg_r1;
CREATE USER tsg_u1, tsg_u2;
GRANT CONNECTION_ADMIN, ROLE_ADMIN, SYSTEM_VARIABLES_ADMIN, PROCESS ON *.* TO tsg_r1;
Expand Down

0 comments on commit a48a4a2

Please sign in to comment.