Skip to content

Commit

Permalink
*: Create privilege tables in bootstrap stage
Browse files Browse the repository at this point in the history
Prepare for grant statement.
  • Loading branch information
shenli committed Sep 27, 2015
1 parent 1759a35 commit 198099d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
65 changes: 63 additions & 2 deletions bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,58 @@ import (

const (
// CreateUserTable is the SQL statement creates User table in system db.
CreateUserTable = "CREATE TABLE if not exists mysql.user (Host CHAR(64), User CHAR(16), Password CHAR(41), PRIMARY KEY (Host, User));"
CreateUserTable = `CREATE TABLE if not exists mysql.user (
Host CHAR(64),
User CHAR(16),
Password CHAR(41),
Select_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Insert_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Update_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Delete_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Create_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Drop_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Grant_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',
Execute_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
Create_user_priv ENUM('N','Y') NOT NULL DEFAULT 'N',
PRIMARY KEY (Host, User));`
// CreateDBPrivTable is the SQL statement creates DB scope privilege table in system db.
CreateDBPrivTable = `CREATE TABLE if not exists mysql.db (
Host CHAR(60),
DB CHAR(64),
User CHAR(16),
Select_priv ENUM('N','Y') Not Null DEFAULT 'N',
Insert_priv ENUM('N','Y') Not Null DEFAULT 'N',
Update_priv ENUM('N','Y') Not Null DEFAULT 'N',
Delete_priv ENUM('N','Y') Not Null DEFAULT 'N',
Create_priv ENUM('N','Y') Not Null DEFAULT 'N',
Drop_priv ENUM('N','Y') Not Null DEFAULT 'N',
Grant_priv ENUM('N','Y') Not Null DEFAULT 'N',
Alter_priv ENUM('N','Y') Not Null DEFAULT 'N',
Execute_priv ENUM('N','Y') Not Null DEFAULT 'N',
PRIMARY KEY (Host, DB, User));`
// CreateTablePrivTable is the SQL statement creates table scope privilege table in system db.
CreateTablePrivTable = `CREATE TABLE if not exists mysql.tables_priv (
Host CHAR(60),
DB CHAR(64),
User CHAR(16),
Table_name CHAR(64),
Grantor CHAR(77),
Timestamp Timestamp DEFAULT CURRENT_TIMESTAMP,
Table_priv SET('Select','Insert','Update','Delete','Create','Drop','Grant', 'Index','Alter'),
Column_priv SET('Select','Insert','Update'),
PRIMARY KEY (Host, DB, User, Table_name));`
// CreateColumnPrivTable is the SQL statement creates column scope privilege table in system db.
CreateColumnPrivTable = `CREATE TABLE if not exists mysql.columns_priv(
Host CHAR(60),
DB CHAR(64),
User CHAR(16),
Table_name CHAR(64),
Column_name CHAR(64),
Timestamp Timestamp DEFAULT CURRENT_TIMESTAMP,
Column_priv SET('Select','Insert','Update'),
PRIMARY KEY (Host, DB, User, Table_name, Column_name));`
)

// Bootstrap initiates system DB for a store.
Expand All @@ -47,12 +98,22 @@ func bootstrap(s Session) {
}
mustExecute(s, fmt.Sprintf("CREATE DATABASE %s;", mysql.SystemDB))
initUserTable(s)
initPrivTables(s)
}

func initUserTable(s Session) {
mustExecute(s, CreateUserTable)
// Insert a default user with empty password.
mustExecute(s, `INSERT INTO mysql.user VALUES ("localhost", "root", ""), ("127.0.0.1", "root", ""), ("::1", "root", "");`)
mustExecute(s, `INSERT INTO mysql.user VALUES ("localhost", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
("127.0.0.1", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
("::1", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y");`)
}

// Initiates privilege tables including mysql.db, mysql.tables_priv and mysql.column_priv.
func initPrivTables(s Session) {
mustExecute(s, CreateDBPrivTable)
mustExecute(s, CreateTablePrivTable)
mustExecute(s, CreateColumnPrivTable)
}

func mustExecute(s Session, sql string) {
Expand Down
2 changes: 1 addition & 1 deletion plan/plans/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (p *testInfoSchemaSuit) TestInfoSchema(c *C) {
cnt = mustQuery(c, testDB, "select * from information_schema.columns")
c.Assert(cnt, Greater, 0)
cnt = mustQuery(c, testDB, "select * from information_schema.statistics")
c.Assert(cnt, Equals, 2)
c.Assert(cnt, Equals, 14)
cnt = mustQuery(c, testDB, "select * from information_schema.character_sets")
c.Assert(cnt, Greater, 0)
cnt = mustQuery(c, testDB, "select * from information_schema.collations")
Expand Down
1 change: 1 addition & 0 deletions stmt/stmts/account_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (s *CreateUserStmt) Exec(ctx context.Context) (rset.Recordset, error) {
Name: model.NewCIStr(mysql.UserTable),
Schema: model.NewCIStr(mysql.SystemDB),
},
ColNames: []string{"Host", "User", "Password"},
}
values := make([][]expression.Expression, 0, len(s.Specs))
for _, spec := range s.Specs {
Expand Down
2 changes: 1 addition & 1 deletion stmt/stmts/account_manage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (s *testStmtSuite) TestCreateUserStmt(c *C) {

func (s *testStmtSuite) TestSetPwdStmt(c *C) {
tx := mustBegin(c, s.testDB)
tx.Query(`INSERT INTO mysql.User VALUES ("localhost", "root", ""), ("127.0.0.1", "root", "")`)
tx.Query(`INSERT INTO mysql.User VALUES ("localhost", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"), ("127.0.0.1", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")`)
rows, err := tx.Query(`SELECT Password FROM mysql.User WHERE User="root" and Host="localhost"`)
c.Assert(err, IsNil)
rows.Next()
Expand Down
8 changes: 6 additions & 2 deletions tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,12 +901,16 @@ func (s *testSessionSuite) TestBootstrap(c *C) {
row, err := r.Next()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
match(c, row.Data, "localhost", "root", "")
match(c, row.Data, "localhost", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")
row, err = r.Next()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
match(c, row.Data, "127.0.0.1", "root", "")
match(c, row.Data, "127.0.0.1", "root", "", "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;")
mustExecSQL(c, se, "SELECT * from mysql.tables_priv;")
mustExecSQL(c, se, "SELECT * from mysql.columns_priv;")
}

func (s *testSessionSuite) TestEnum(c *C) {
Expand Down
4 changes: 4 additions & 0 deletions util/types/etc.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func TypeToStr(tp byte, binary bool) string {
return "timestamp"
case mysql.TypeBit:
return "bit"
case mysql.TypeEnum:
return "enum"
case mysql.TypeSet:
return "set"
default:
log.Errorf("unkown type %d, binary %v", tp, binary)
}
Expand Down
2 changes: 2 additions & 0 deletions util/types/etc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func (s *testTypeEtcSuite) TestTypeToStr(c *C) {
testTypeToStr(c, mysql.TypeDecimal, true, "decimal")
testTypeToStr(c, 0xdd, true, "")
testTypeToStr(c, mysql.TypeBit, true, "bit")
testTypeToStr(c, mysql.TypeEnum, true, "enum")
testTypeToStr(c, mysql.TypeSet, true, "set")
}

func (s *testTypeEtcSuite) TestEOFAsNil(c *C) {
Expand Down

0 comments on commit 198099d

Please sign in to comment.