Skip to content

Commit

Permalink
privileges: fix show create view doesn't need show view privilege (pi…
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Oct 14, 2021
1 parent e717f10 commit fac98dc
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 8 deletions.
27 changes: 21 additions & 6 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2357,21 +2357,36 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan,
return nil, ErrNoDB
}
case ast.ShowCreateTable, ast.ShowCreateSequence, ast.ShowPlacementForTable:
user := b.ctx.GetSessionVars().User
var err error
if user != nil {
err = ErrTableaccessDenied.GenWithStackByArgs("SHOW", user.AuthUsername, user.AuthHostname, show.Table.Name.L)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AllPrivMask, show.Table.Schema.L, show.Table.Name.L, "", err)
if table, err := b.is.TableByName(show.Table.Schema, show.Table.Name); err == nil {
isView = table.Meta().IsView()
isSequence = table.Meta().IsSequence()
}
user := b.ctx.GetSessionVars().User
if isView {
if user != nil {
err = ErrTableaccessDenied.GenWithStackByArgs("SHOW VIEW", user.AuthUsername, user.AuthHostname, show.Table.Name.L)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.ShowViewPriv, show.Table.Schema.L, show.Table.Name.L, "", err)
} else {
if user != nil {
err = ErrTableaccessDenied.GenWithStackByArgs("SHOW", user.AuthUsername, user.AuthHostname, show.Table.Name.L)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.AllPrivMask, show.Table.Schema.L, show.Table.Name.L, "", err)
}
case ast.ShowConfig:
privErr := ErrSpecificAccessDenied.GenWithStackByArgs("CONFIG")
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.ConfigPriv, "", "", "", privErr)
case ast.ShowCreateView:
err := ErrSpecificAccessDenied.GenWithStackByArgs("SHOW VIEW")
var err error
user := b.ctx.GetSessionVars().User
if user != nil {
err = ErrTableaccessDenied.GenWithStackByArgs("SELECT", user.AuthUsername, user.AuthHostname, show.Table.Name.L)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, show.Table.Schema.L, show.Table.Name.L, "", err)
if user != nil {
err = ErrTableaccessDenied.GenWithStackByArgs("SHOW VIEW", user.AuthUsername, user.AuthHostname, show.Table.Name.L)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.ShowViewPriv, show.Table.Schema.L, show.Table.Name.L, "", err)
case ast.ShowBackups:
err := ErrSpecificAccessDenied.GenWithStackByArgs("SUPER or BACKUP_ADMIN")
Expand Down
100 changes: 100 additions & 0 deletions privilege/privileges/privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,106 @@ func TestSelectViewSecurity(t *testing.T) {
require.EqualError(t, err, core.ErrViewInvalid.GenWithStackByArgs("test", "selectviewsecurity").Error())
}

func TestShowViewPriv(t *testing.T) {
t.Parallel()
store, clean := newStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec(`DROP VIEW IF EXISTS test.v`)
tk.MustExec(`CREATE VIEW test.v AS SELECT 1`)
tk.MustExec("CREATE USER vnobody, vshowview, vselect, vshowandselect")
tk.MustExec("GRANT SHOW VIEW ON test.v TO vshowview")
tk.MustExec("GRANT SELECT ON test.v TO vselect")
tk.MustExec("GRANT SHOW VIEW, SELECT ON test.v TO vshowandselect")

tests := []struct {
userName string
showViewErr string
showTableErr string
explainErr string
explainRes string
descErr string
descRes string
tablesNum string
columnsNum string
}{
{"vnobody",
"[planner:1142]SELECT command denied to user 'vnobody'@'%' for table 'v'",
"[planner:1142]SHOW VIEW command denied to user 'vnobody'@'%' for table 'v'",
"[executor:1142]SELECT command denied to user 'vnobody'@'%' for table 'v'",
"",
"[executor:1142]SELECT command denied to user 'vnobody'@'%' for table 'v'",
"",
"0",
"0",
},
{"vshowview",
"[planner:1142]SELECT command denied to user 'vshowview'@'%' for table 'v'",
"",
"",
"",
"",
"",
"1",
"0",
},
{"vselect",
"[planner:1142]SHOW VIEW command denied to user 'vselect'@'%' for table 'v'",
"[planner:1142]SHOW VIEW command denied to user 'vselect'@'%' for table 'v'",
"",
"1 bigint(1) NO <nil> ",
"",
"1 bigint(1) NO <nil> ",
"1",
"1",
},
{"vshowandselect",
"",
"",
"",
"1 bigint(1) NO <nil> ",
"",
"1 bigint(1) NO <nil> ",
"1",
"1",
},
}

for _, test := range tests {
tk.Session().Auth(&auth.UserIdentity{Username: test.userName, Hostname: "localhost"}, nil, nil)
err := tk.ExecToErr("SHOW CREATE VIEW test.v")
if test.showViewErr != "" {
require.EqualError(t, err, test.showViewErr, test)
} else {
require.NoError(t, err, test)
}
err = tk.ExecToErr("SHOW CREATE TABLE test.v")
if test.showTableErr != "" {
require.EqualError(t, err, test.showTableErr, test)
} else {
require.NoError(t, err, test)
}
if test.explainErr != "" {
err = tk.QueryToErr("explain test.v")
require.EqualError(t, err, test.explainErr, test)
} else {
// TODO: expecting empty set but got one row for vshowview.
// tk.MustQuery("explain test.v").Check(testkit.Rows(test.explainRes))
}
if test.descErr != "" {
err = tk.QueryToErr("explain test.v")
require.EqualError(t, err, test.descErr, test)
} else {
// TODO: expecting empty set but got one row for vshowview.
// tk.MustQuery("desc test.v").Check(testkit.Rows(test.descRes))
}
tk.MustQuery("select count(*) from information_schema.tables where table_schema='test' and table_name='v'").Check(testkit.Rows(test.tablesNum))
// TODO: expecting 0 but got 1 for vshowview.
// tk.MustQuery("select count(*) from information_schema.columns where table_schema='test' and table_name='v'").Check(testkit.Rows(test.columnsNum))
}
}

func TestRoleAdminSecurity(t *testing.T) {
t.Parallel()
store, clean := newStore(t)
Expand Down
4 changes: 2 additions & 2 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3254,8 +3254,8 @@ func (s *testSessionSuite2) TestGrantViewRelated(c *C) {
err = tkUser.ExecToErr("create view v_version29_c as select * from t;")
c.Assert(err, NotNil)

tkRoot.MustExec(`grant show view on v_version29 to 'u_version29'@'%'`)
tkRoot.MustQuery("select table_priv from mysql.tables_priv where host='%' and db='test' and user='u_version29' and table_name='v_version29'").Check(testkit.Rows("Show View"))
tkRoot.MustExec(`grant show view, select on v_version29 to 'u_version29'@'%'`)
tkRoot.MustQuery("select table_priv from mysql.tables_priv where host='%' and db='test' and user='u_version29' and table_name='v_version29'").Check(testkit.Rows("Select,Show View"))

tkUser.MustQuery("select current_user();").Check(testkit.Rows("u_version29@%"))
tkUser.MustQuery("show create view v_version29;")
Expand Down

0 comments on commit fac98dc

Please sign in to comment.