Skip to content

Commit ca01650

Browse files
committed
fix the issue that some privileges cannot be limited
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
1 parent bb0b895 commit ca01650

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

‎pkg/planner/core/planbuilder.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3963,7 +3963,7 @@ func collectVisitInfoFromGrantStmt(sctx base.PlanContext, vi []visitInfo, stmt *
39633963
for _, item := range stmt.Privs {
39643964
if semv2.IsEnabled() {
39653965
if (len(item.Name) > 0 && semv2.IsRestrictedPrivilege(item.Name)) ||
3966-
(len(item.Name) == 0 && semv2.IsRestrictedPrivilege(item.Priv.String())) {
3966+
(len(item.Name) == 0 && semv2.IsRestrictedPrivilege(strings.ToUpper(item.Priv.String()))) {
39673967
// In `semv2`, we'll support to limit non-dynamic privileges unless the user has the `RESTRICTED_PRIV_ADMIN` privilege.
39683968
// For example, `File` privilege might be restricted.
39693969
// It's also controlled by the `GRANT OPTION`, so the user will also need the `GRANT OPTION` for this privilege.

‎pkg/privilege/privileges/BUILD.bazel‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ go_test(
8383
"//pkg/util/hack",
8484
"//pkg/util/sem",
8585
"//pkg/util/sem/compat",
86+
"//pkg/util/sem/v2:sem",
8687
"//pkg/util/sqlescape",
8788
"@com_github_lestrrat_go_jwx_v2//jwa",
8889
"@com_github_lestrrat_go_jwx_v2//jwk",

‎pkg/privilege/privileges/privileges_test.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"github.com/pingcap/tidb/pkg/util/dbterror/plannererrors"
5151
semv1 "github.com/pingcap/tidb/pkg/util/sem"
5252
sem "github.com/pingcap/tidb/pkg/util/sem/compat"
53+
semv2 "github.com/pingcap/tidb/pkg/util/sem/v2"
5354
"github.com/pingcap/tidb/pkg/util/sqlescape"
5455
"github.com/stretchr/testify/require"
5556
)
@@ -2230,16 +2231,19 @@ func TestGrantOptionWithSEMv2(t *testing.T) {
22302231
rootTk.MustExec("CREATE USER varuser2")
22312232
rootTk.MustExec("CREATE USER varuser3")
22322233
rootTk.MustExec("CREATE USER varuser4")
2234+
rootTk.MustExec("CREATE USER varuser5")
22332235
rootTk.MustExec("CREATE USER grantee")
22342236

22352237
rootTk.MustExec("GRANT SYSTEM_VARIABLES_ADMIN, FILE ON *.* TO varuser1")
22362238
rootTk.MustExec("GRANT SYSTEM_VARIABLES_ADMIN, FILE ON *.* TO varuser2 WITH GRANT OPTION")
22372239
rootTk.MustExec("GRANT RESTRICTED_PRIV_ADMIN ON *.* TO varuser3")
22382240
rootTk.MustExec("GRANT RESTRICTED_PRIV_ADMIN ON *.* TO varuser4")
22392241
rootTk.MustExec("GRANT SYSTEM_VARIABLES_ADMIN, FILE ON *.* TO varuser4 WITH GRANT OPTION")
2242+
rootTk.MustExec("GRANT SYSTEM_VARIABLES_ADMIN, DROP ON *.* TO varuser5 WITH GRANT OPTION")
22402243

2241-
// SYSTEM_VARIABLES_ADMIN is not restricted, FILE is restricted.
2244+
// SYSTEM_VARIABLES_ADMIN is not restricted, FILE and Drop are restricted.
22422245
defer sem.SwitchToSEMForTest(t, sem.V2)()
2246+
semv2.AddRestrictedPrivilegesForTest("Drop")
22432247
// try to grant SYSTEM_VARIABLES_ADMIN and FILE privilege to grantee with different user
22442248
tk1 := testkit.NewTestKit(t, store)
22452249
require.NoError(t, tk1.Session().Auth(&auth.UserIdentity{Username: "varuser1", Hostname: "%"}, nil, nil, nil))
@@ -2268,4 +2272,10 @@ func TestGrantOptionWithSEMv2(t *testing.T) {
22682272
require.NoError(t, err)
22692273
err = tk4.ExecToErr("GRANT FILE ON *.* TO grantee")
22702274
require.NoError(t, err)
2275+
2276+
// Test grant drop
2277+
tk5 := testkit.NewTestKit(t, store)
2278+
require.NoError(t, tk5.Session().Auth(&auth.UserIdentity{Username: "varuser5", Hostname: "%"}, nil, nil, nil))
2279+
err = tk5.ExecToErr("GRANT drop ON *.* TO grantee")
2280+
require.EqualError(t, err, "[planner:1227]Access denied; you need (at least one of) the RESTRICTED_PRIV_ADMIN privilege(s) for this operation")
22712281
}

‎pkg/util/sem/v2/testhelper.go‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
package sem
1616

17-
import "github.com/pingcap/tidb/pkg/sessionctx/variable"
17+
import (
18+
"strings"
19+
20+
"github.com/pingcap/tidb/pkg/sessionctx/variable"
21+
)
1822

1923
// EnableFromPathForTest enables SEM v2 in test using a configuration file.
2024
func EnableFromPathForTest(configPath string) (func(), error) {
@@ -48,3 +52,12 @@ func EnableFromPathForTest(configPath string) (func(), error) {
4852
}
4953
}, nil
5054
}
55+
56+
// AddRestrictedPrivilegesForTest adds restricted privileges for test.
57+
func AddRestrictedPrivilegesForTest(privilege string) {
58+
if sem == nil {
59+
return
60+
}
61+
62+
sem.restrictedPrivileges[strings.ToUpper(privilege)] = struct{}{}
63+
}

0 commit comments

Comments
 (0)