Skip to content

Commit

Permalink
*: add References_priv to mysql.user (pingcap#3343)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored May 26, 2017
1 parent ed32957 commit 9b6fc03
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
21 changes: 19 additions & 2 deletions bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
Drop_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Process_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Grant_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
References_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Alter_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Show_db_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Super_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Expand Down Expand Up @@ -190,6 +191,7 @@ const (
version8 = 8
version9 = 9
version10 = 10
version11 = 11
)

func checkBootstrapped(s Session) (bool, error) {
Expand Down Expand Up @@ -284,6 +286,10 @@ func upgrade(s Session) {
upgradeToVer10(s)
}

if ver < version11 {
upgradeToVer11(s)
}

updateBootstrapVer(s)
_, err = s.Execute("COMMIT")

Expand Down Expand Up @@ -361,7 +367,7 @@ func upgradeToVer8(s Session) {
func upgradeToVer9(s Session) {
doReentrantDDL(s, "ALTER TABLE mysql.user ADD COLUMN `Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N' AFTER `Create_user_priv`", infoschema.ErrColumnExists)
// For reasons of compatibility, set the non-exists privilege column value to 'Y', as TiDB doesn't check them in older versions.
s.Execute("UPDATE mysql.user SET Trigger_priv='Y'")
mustExecute(s, "UPDATE mysql.user SET Trigger_priv='Y'")
}

func doReentrantDDL(s Session, sql string, ignorableErrs ...error) {
Expand All @@ -384,6 +390,17 @@ func upgradeToVer10(s Session) {
doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms DROP COLUMN use_count_to_estimate", ddl.ErrCantDropFieldOrKey)
}

func upgradeToVer11(s Session) {
_, err := s.Execute("ALTER TABLE mysql.user ADD COLUMN `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N' AFTER `Grant_priv`")
if err != nil {
if terror.ErrorEqual(err, infoschema.ErrColumnExists) {
return
}
log.Fatal(err)
}
mustExecute(s, "UPDATE mysql.user SET References_priv='Y'")
}

// updateBootstrapVer updates bootstrap version variable in mysql.TiDB table.
func updateBootstrapVer(s Session) {
// Update bootstrap version.
Expand Down Expand Up @@ -437,7 +454,7 @@ func doDMLWorks(s Session) {

// Insert a default user with empty password.
mustExecute(s, `INSERT INTO mysql.user VALUES
("%", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")`)
("%", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")`)

// Init global system variables table.
values := make([]string, 0, len(variable.SysVars))
Expand Down
4 changes: 2 additions & 2 deletions bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *testBootstrapSuite) TestBootstrap(c *C) {
row, err := r.Next()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
match(c, row.Data, []byte("%"), []byte("root"), []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")
match(c, row.Data, []byte("%"), []byte("root"), []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")

c.Assert(se.Auth("root@anyhost", []byte(""), []byte("")), IsTrue)
mustExecSQL(c, se, "USE test;")
Expand Down Expand Up @@ -141,7 +141,7 @@ func (s *testBootstrapSuite) testBootstrapWithError(c *C) {
row, err := r.Next()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
match(c, row.Data, []byte("%"), []byte("root"), []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")
match(c, row.Data, []byte("%"), []byte("root"), []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")
mustExecSQL(c, se, "USE test;")
// Check privilege tables.
mustExecSQL(c, se, "SELECT * from mysql.db;")
Expand Down
2 changes: 1 addition & 1 deletion executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (s *testSuite) TestAggregation(c *C) {

result = tk.MustQuery("select count(*) from information_schema.columns")
// When adding new memory table in information_schema, please update this variable.
columnCountOfAllInformationSchemaTables := "716"
columnCountOfAllInformationSchemaTables := "717"
result.Check(testkit.Rows(columnCountOfAllInformationSchemaTables))

tk.MustExec("drop table if exists t1")
Expand Down
7 changes: 6 additions & 1 deletion mysql/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ const (
ProcessPriv
// GrantPriv is the privilege to grant privilege to user.
GrantPriv
// ReferencesPriv is not checked yet.
ReferencesPriv
// AlterPriv is the privilege to run alter statement.
AlterPriv
// ExecutePriv is the privilege to run execute statement.
Expand Down Expand Up @@ -205,6 +207,7 @@ var Priv2UserCol = map[PrivilegeType]string{
DropPriv: "Drop_priv",
ProcessPriv: "Process_priv",
GrantPriv: "Grant_priv",
ReferencesPriv: "References_priv",
AlterPriv: "Alter_priv",
ExecutePriv: "Execute_priv",
IndexPriv: "Index_priv",
Expand All @@ -224,13 +227,14 @@ var Col2PrivType = map[string]PrivilegeType{
"Drop_priv": DropPriv,
"Process_priv": ProcessPriv,
"Grant_priv": GrantPriv,
"References_priv": ReferencesPriv,
"Alter_priv": AlterPriv,
"Execute_priv": ExecutePriv,
"Index_priv": IndexPriv,
}

// AllGlobalPrivs is all the privileges in global scope.
var AllGlobalPrivs = []PrivilegeType{SelectPriv, InsertPriv, UpdatePriv, DeletePriv, CreatePriv, DropPriv, ProcessPriv, GrantPriv, AlterPriv, ShowDBPriv, SuperPriv, ExecutePriv, IndexPriv, CreateUserPriv, TriggerPriv}
var AllGlobalPrivs = []PrivilegeType{SelectPriv, InsertPriv, UpdatePriv, DeletePriv, CreatePriv, DropPriv, ProcessPriv, GrantPriv, ReferencesPriv, AlterPriv, ShowDBPriv, SuperPriv, ExecutePriv, IndexPriv, CreateUserPriv, TriggerPriv}

// Priv2Str is the map for privilege to string.
var Priv2Str = map[PrivilegeType]string{
Expand All @@ -246,6 +250,7 @@ var Priv2Str = map[PrivilegeType]string{
DropPriv: "Drop",
ProcessPriv: "Process",
GrantPriv: "Grant Option",
ReferencesPriv: "References",
AlterPriv: "Alter",
ExecutePriv: "Execute",
IndexPriv: "Index",
Expand Down
4 changes: 4 additions & 0 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -6179,6 +6179,10 @@ PrivType:
{
$$ = mysql.GrantPriv
}
| "REFERENCES"
{
$$ = mysql.ReferencesPriv
}

ObjectType:
{
Expand Down
2 changes: 1 addition & 1 deletion privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func noSuchTable(err error) bool {

// LoadUserTable loads the mysql.user table from database.
func (p *MySQLPrivilege) LoadUserTable(ctx context.Context) error {
return p.loadTable(ctx, "select Host,User,Password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Process_priv,Grant_priv,Alter_priv,Show_db_priv,Super_priv,Execute_priv,Index_priv,Create_user_priv,Trigger_priv from mysql.user order by host, user;", p.decodeUserTableRow)
return p.loadTable(ctx, "select Host,User,Password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Process_priv,Grant_priv,References_priv,Alter_priv,Show_db_priv,Super_priv,Execute_priv,Index_priv,Create_user_priv,Trigger_priv from mysql.user order by host, user;", p.decodeUserTableRow)
}

// LoadDBTable loads the mysql.db table from database.
Expand Down
12 changes: 6 additions & 6 deletions privilege/privileges/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func (s *testCacheSuite) TestLoadUserTable(c *C) {
c.Assert(err, IsNil)
c.Assert(len(p.User), Equals, 0)

// Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Process_priv | Grant_priv | Alter_priv | Show_db_priv | Super_priv | Execute_priv | Index_priv | Create_user_priv | Trigger_priv
// Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Process_priv | Grant_priv | References_priv | Alter_priv | Show_db_priv | Super_priv | Execute_priv | Index_priv | Create_user_priv | Trigger_priv
mustExec(c, se, `INSERT INTO mysql.user (Host, User, Password, Select_priv) VALUES ("%", "root", "", "Y")`)
mustExec(c, se, `INSERT INTO mysql.user (Host, User, Password, Insert_priv) VALUES ("%", "root1", "admin", "Y")`)
mustExec(c, se, `INSERT INTO mysql.user (Host, User, Password, Update_priv, Show_db_priv) VALUES ("%", "root11", "", "Y", "Y")`)
mustExec(c, se, `INSERT INTO mysql.user VALUES ("%", "root111", "", "N", "N", "N", "N", "N", "N", "N", "N", "N", "Y", "Y", "Y", "Y", "Y", "Y")`)
mustExec(c, se, `INSERT INTO mysql.user (Host, User, Password, Update_priv, Show_db_priv, References_priv) VALUES ("%", "root11", "", "Y", "Y", "Y")`)
mustExec(c, se, `INSERT INTO mysql.user (Host, User, Password, Create_user_priv, Index_priv, Execute_priv, Show_db_priv, Super_priv, Trigger_priv) VALUES ("%", "root111", "", "Y", "Y", "Y", "Y", "Y", "Y")`)

p = privileges.MySQLPrivilege{}
err = p.LoadUserTable(se)
Expand All @@ -66,7 +66,7 @@ func (s *testCacheSuite) TestLoadUserTable(c *C) {
c.Assert(user[0].User, Equals, "root")
c.Assert(user[0].Privileges, Equals, mysql.SelectPriv)
c.Assert(user[1].Privileges, Equals, mysql.InsertPriv)
c.Assert(user[2].Privileges, Equals, mysql.UpdatePriv|mysql.ShowDBPriv)
c.Assert(user[2].Privileges, Equals, mysql.UpdatePriv|mysql.ShowDBPriv|mysql.ReferencesPriv)
c.Assert(user[3].Privileges, Equals, mysql.CreateUserPriv|mysql.IndexPriv|mysql.ExecutePriv|mysql.ShowDBPriv|mysql.SuperPriv|mysql.TriggerPriv)
}

Expand Down Expand Up @@ -136,7 +136,7 @@ func (s *testCacheSuite) TestPatternMatch(c *C) {
defer se.Close()
mustExec(c, se, "USE MYSQL;")
mustExec(c, se, "TRUNCATE TABLE mysql.user")
mustExec(c, se, `INSERT INTO mysql.user VALUES ("10.0.%", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")`)
mustExec(c, se, `INSERT INTO mysql.user VALUES ("10.0.%", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")`)
var p privileges.MySQLPrivilege
err = p.LoadUserTable(se)
c.Assert(err, IsNil)
Expand All @@ -147,7 +147,7 @@ func (s *testCacheSuite) TestPatternMatch(c *C) {
c.Assert(p.RequestVerification("root", "114.114.114.114", "test", "", "", mysql.SelectPriv), IsFalse)

mustExec(c, se, "TRUNCATE TABLE mysql.user")
mustExec(c, se, `INSERT INTO mysql.user VALUES ("", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")`)
mustExec(c, se, `INSERT INTO mysql.user VALUES ("", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")`)
p = privileges.MySQLPrivilege{}
err = p.LoadUserTable(se)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ func createSession(store kv.Storage) (*session, error) {

const (
notBootstrapped = 0
currentBootstrapVersion = 10
currentBootstrapVersion = 11
)

func getStoreBootstrapVersion(store kv.Storage) int64 {
Expand Down

0 comments on commit 9b6fc03

Please sign in to comment.