Skip to content

Commit 40f021e

Browse files
authored
[opt](privilege) Grant check name (#39597) (#39858)
pick from master #39597
1 parent 2df6c84 commit 40f021e

File tree

4 files changed

+79
-10
lines changed

4 files changed

+79
-10
lines changed

fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
import org.apache.doris.analysis.TablePattern;
3535
import org.apache.doris.analysis.UserIdentity;
3636
import org.apache.doris.analysis.WorkloadGroupPattern;
37+
import org.apache.doris.catalog.DatabaseIf;
3738
import org.apache.doris.catalog.Env;
3839
import org.apache.doris.catalog.InfoSchemaDb;
40+
import org.apache.doris.catalog.TableIf;
3941
import org.apache.doris.cluster.ClusterNamespace;
4042
import org.apache.doris.common.AnalysisException;
4143
import org.apache.doris.common.AuthenticationException;
@@ -51,6 +53,7 @@
5153
import org.apache.doris.common.PatternMatcherException;
5254
import org.apache.doris.common.UserException;
5355
import org.apache.doris.common.io.Writable;
56+
import org.apache.doris.datasource.CatalogIf;
5457
import org.apache.doris.datasource.InternalCatalog;
5558
import org.apache.doris.ldap.LdapManager;
5659
import org.apache.doris.ldap.LdapUserInfo;
@@ -81,6 +84,7 @@
8184
import java.util.List;
8285
import java.util.Map;
8386
import java.util.Map.Entry;
87+
import java.util.Objects;
8488
import java.util.Set;
8589
import java.util.concurrent.locks.ReentrantReadWriteLock;
8690
import java.util.stream.Collectors;
@@ -578,6 +582,7 @@ private void grantInternal(UserIdentity userIdent, String role, TablePattern tbl
578582
throws DdlException {
579583
writeLock();
580584
try {
585+
checkTablePatternExist(tblPattern);
581586
if (role == null) {
582587
if (!doesUserExist(userIdent)) {
583588
throw new DdlException("user " + userIdent + " does not exist");
@@ -596,6 +601,32 @@ private void grantInternal(UserIdentity userIdent, String role, TablePattern tbl
596601
}
597602
}
598603

604+
private void checkTablePatternExist(TablePattern tablePattern) throws DdlException {
605+
Objects.requireNonNull(tablePattern, "tablePattern can not be null");
606+
PrivLevel privLevel = tablePattern.getPrivLevel();
607+
if (privLevel == PrivLevel.GLOBAL) {
608+
return;
609+
}
610+
CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(tablePattern.getQualifiedCtl());
611+
if (catalog == null) {
612+
throw new DdlException("catalog:" + tablePattern.getQualifiedCtl() + " does not exist");
613+
}
614+
if (privLevel == PrivLevel.CATALOG) {
615+
return;
616+
}
617+
DatabaseIf db = catalog.getDbNullable(tablePattern.getQualifiedDb());
618+
if (db == null) {
619+
throw new DdlException("database:" + tablePattern.getQualifiedDb() + " does not exist");
620+
}
621+
if (privLevel == PrivLevel.DATABASE) {
622+
return;
623+
}
624+
TableIf table = db.getTableNullable(tablePattern.getTbl());
625+
if (table == null) {
626+
throw new DdlException("table:" + tablePattern.getTbl() + " does not exist");
627+
}
628+
}
629+
599630
// grant for ResourcePattern
600631
private void grantInternal(UserIdentity userIdent, String role, ResourcePattern resourcePattern, PrivBitSet privs,
601632
boolean errOnNonExist, boolean isReplay) throws DdlException {

fe/fe-core/src/test/java/org/apache/doris/datasource/CatalogMgrTest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ protected void runBeforeAll() throws Exception {
101101
// grant with no catalog is switched, internal catalog works.
102102
CreateRoleStmt createRole1 = (CreateRoleStmt) parseAndAnalyzeStmt("create role role1;", rootCtx);
103103
auth.createRole(createRole1);
104-
GrantStmt grantRole1 = (GrantStmt) parseAndAnalyzeStmt("grant grant_priv on tpch.* to role 'role1';", rootCtx);
105-
auth.grant(grantRole1);
106-
// grant with ctl.db.tbl. grant can succeed even if the catalog does not exist
107-
GrantStmt grantRole1WithCtl = (GrantStmt) parseAndAnalyzeStmt(
108-
"grant select_priv on testc.testdb.* to role 'role1';", rootCtx);
109-
auth.grant(grantRole1WithCtl);
110-
// user1 can't switch to hive
111104
auth.createUser((CreateUserStmt) parseAndAnalyzeStmt(
112105
"create user 'user1'@'%' identified by 'pwd1' default role 'role1';", rootCtx));
113106
user1 = new UserIdentity("user1", "%");
@@ -152,7 +145,8 @@ protected void runBeforeAll() throws Exception {
152145
env.changeCatalog(rootCtx, switchHive.getCatalogName());
153146
CreateRoleStmt createRole2 = (CreateRoleStmt) parseAndAnalyzeStmt("create role role2;", rootCtx);
154147
auth.createRole(createRole2);
155-
GrantStmt grantRole2 = (GrantStmt) parseAndAnalyzeStmt("grant grant_priv on tpch.customer to role 'role2';",
148+
GrantStmt grantRole2 = (GrantStmt) parseAndAnalyzeStmt(
149+
"grant grant_priv, select_priv on hive.*.* to role 'role2';",
156150
rootCtx);
157151
auth.grant(grantRole2);
158152
auth.createUser((CreateUserStmt) parseAndAnalyzeStmt(
@@ -366,7 +360,7 @@ public void testSwitchCommand() throws Exception {
366360
Assert.assertEquals(user2Ctx.getDefaultCatalog(), "hive");
367361
// user2 can grant select_priv to tpch.customer
368362
GrantStmt user2GrantHiveTable = (GrantStmt) parseAndAnalyzeStmt(
369-
"grant select_priv on tpch.customer to 'user2'@'%';", user2Ctx);
363+
"grant select_priv on hive.*.* to 'user2'@'%';", user2Ctx);
370364
auth.grant(user2GrantHiveTable);
371365

372366
showCatalogSql = "SHOW CATALOGS";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
import org.junit.Assert;
19+
20+
suite("test_grant_nonexist_table","p0,auth") {
21+
String suiteName = "test_grant_nonexist_table"
22+
String dbName = context.config.getDbNameByFile(context.file)
23+
String user = "${suiteName}_user"
24+
String pwd = 'C123_567p'
25+
try_sql("DROP USER ${user}")
26+
sql """CREATE USER '${user}' IDENTIFIED BY '${pwd}'"""
27+
28+
test {
29+
sql """grant select_priv on non_exist_catalog.*.* to ${user}"""
30+
exception "catalog"
31+
}
32+
33+
test {
34+
sql """grant select_priv on internal.non_exist_db.* to ${user}"""
35+
exception "database"
36+
}
37+
38+
test {
39+
sql """grant select_priv on internal.${dbName}.non_exist_table to ${user}"""
40+
exception "table"
41+
}
42+
43+
44+
try_sql("DROP USER ${user}")
45+
}

regression-test/suites/ccr_mow_syncer_p0/test_get_binlog.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ suite("test_mow_get_binlog_case") {
133133
sql """DROP USER IF EXISTS ${noPrivUser}"""
134134
sql """CREATE USER ${noPrivUser} IDENTIFIED BY '123456'"""
135135
sql """GRANT ALL ON ${context.config.defaultDb}.* TO ${noPrivUser}"""
136-
sql """GRANT ALL ON TEST_${context.dbName}.${emptyTable} TO ${noPrivUser}"""
137136
syncer.context.user = "${noPrivUser}"
138137
syncer.context.passwd = "123456"
139138
assertTrue((syncer.getBinlog("${seqTableName}")) == false)

0 commit comments

Comments
 (0)