Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: Support NO_AUTO_CREATE_USER sql mode #8160

Merged
merged 10 commits into from
Nov 12, 2018
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)
morgo marked this conversation as resolved.
Show resolved Hide resolved
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