Skip to content

Commit

Permalink
executor: Support NO_AUTO_CREATE_USER sql mode (#8160)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgo authored and tiancaiamao committed Nov 12, 2018
1 parent f7d8ca6 commit ce6a715
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion executor/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func (e *GrantExec) Next(ctx context.Context, chk *chunk.Chunk) error {
if err != nil {
return errors.Trace(err)
}
if !exists {
if !exists && e.ctx.GetSessionVars().SQLMode.HasNoAutoCreateUserMode() {
return ErrPasswordNoMatch
} else if !exists {
pwd, ok := user.EncodedPassword()
if !ok {
return errors.Trace(ErrPasswordFormat)
Expand Down
14 changes: 14 additions & 0 deletions executor/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/util/testkit"
)

Expand Down Expand Up @@ -181,14 +183,26 @@ func (s *testSuite) TestIssue2456(c *C) {
tk.MustExec("GRANT ALL PRIVILEGES ON `dddb_%`.`te%` to 'dduser'@'%';")
}

func (s *testSuite) TestNoAutoCreateUser(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`DROP USER IF EXISTS 'test'@'%'`)
tk.MustExec(`SET sql_mode='NO_AUTO_CREATE_USER'`)
_, err := tk.Exec(`GRANT ALL PRIVILEGES ON *.* to 'test'@'%' IDENTIFIED BY 'xxx'`)
c.Check(err, NotNil)
c.Assert(terror.ErrorEqual(err, executor.ErrPasswordNoMatch), IsTrue)
}

func (s *testSuite) TestCreateUserWhenGrant(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`DROP USER IF EXISTS 'test'@'%'`)
// This only applies to sql_mode:NO_AUTO_CREATE_USER off
tk.MustExec(`SET SQL_MODE=''`)
tk.MustExec(`GRANT ALL PRIVILEGES ON *.* to 'test'@'%' IDENTIFIED BY 'xxx'`)
// Make sure user is created automatically when grant to a non-exists one.
tk.MustQuery(`SELECT user FROM mysql.user WHERE user='test' and host='%'`).Check(
testkit.Rows("test"),
)
tk.MustExec(`DROP USER IF EXISTS 'test'@'%'`)
}

func (s *testSuite) TestIssue2654(c *C) {
Expand Down
1 change: 0 additions & 1 deletion executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ func (s *testSuite) TestUser(c *C) {
"localhost test testDB Y Y Y Y Y Y Y N Y Y N N N N N N Y N N",
"localhost test testDB1 Y Y Y Y Y Y Y N Y Y N N N N N N Y N N",
"% dddb_% dduser Y Y Y Y Y Y Y N Y Y N N N N N N Y N N",
"% test test Y N N N N N N N N N N N N N N N N N N",
"localhost test testDBRevoke N N N N N N N N N N N N N N N N N N N",
))

Expand Down
5 changes: 3 additions & 2 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2034,8 +2034,9 @@ func (s *testSessionSuite) TestDBUserNameLength(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table if not exists t (a int)")
// Test user name length can be longer than 16.
tk.MustExec(`grant all privileges on test.* to 'abcddfjakldfjaldddds'@'%' identified by ''`)
tk.MustExec(`grant all privileges on test.t to 'abcddfjakldfjaldddds'@'%' identified by ''`)
tk.MustExec(`CREATE USER 'abcddfjakldfjaldddds'@'%' identified by ''`)
tk.MustExec(`grant all privileges on test.* to 'abcddfjakldfjaldddds'@'%'`)
tk.MustExec(`grant all privileges on test.t to 'abcddfjakldfjaldddds'@'%'`)
}

func (s *testSessionSuite) TestKVVars(c *C) {
Expand Down

0 comments on commit ce6a715

Please sign in to comment.